-
Notifications
You must be signed in to change notification settings - Fork 13
/
softcam (+ bckgrnd script to checking the reader entitlements)
305 lines (270 loc) · 14.9 KB
/
softcam (+ bckgrnd script to checking the reader entitlements)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
# HISTORY OF CHANGES:
#
# 2018/07/16 - created shell-script: /etc/init.d/softcam
# 2019/06/15 - remake the logging concept (logging output via function)
# - some cosmetic improvements
# 2019/06/23 - some changes in start/stop/restart functions
# 2020/02/19 - improved and complete instructions how-to install the Softcam
# 2020/08/30 - no longer use the SIGTERM (No.15) signal, but only SIGKILL (No.9), as some OSCam builds ignore the regular stop by the SIGTERM (No.15) signal
# - removed 'stdout' redirection to 'null' device at softcam execution/starting command-line
# 2020/11/10 - improved installation instructions (OSCam / CCcam)
# 2021/03/24 - simplified (abbreviated) script source code
# 2021/04/07 - updated return error codes for "argument status" as well as for "exit status"
# 2021/09/26 - some improvements of the script function for Auto-Updating (AU) of "Entitlements" on a decoding card (IRDETO mode), which are updated via EMMs
# 2022/03/12 - improved installation instructions (clarity)
# ####################### HOW-TO #########################
#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Enigma2 distributions with built-in SoftCAM support : (OpenATV 6.x and OpenPLi 7.x)
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# 1) Download the "softcam" script file directly from my github + ADD the ".oscam" file extension + SET the execution rights/attributes:
# wget -O /etc/init.d/softcam.oscam --no-check-certificate https://github.com/s3n0/e2scripts/raw/master/softcam && chmod a+x /etc/init.d/softcam.oscam
#
# If you plan to modify and use this script for CCcam, then the correct file extension is ".cccam" or ".CCcam":
# wget -O /etc/init.d/softcam.CCcam --no-check-certificate https://github.com/s3n0/e2scripts/raw/master/softcam && chmod a+x /etc/init.d/softcam.CCcam
#
# 2) If your Enigma2 distribution has built-in softcam support, then there is no need to create any symbolic link at this point.
# I mean the "/etc/init.d/softcam.*" script auto-start feature, which is implemented in many new Enigma2 distributions (based on OE2.0 core), directly in the Enigma2 source code.
#
# 3) Copy your configuration files (using the FTP connection) into the particular folder:
# For the OSCam: "/etc/tuxbox/config/oscam"
# For the CCcam: "/etc" - where the main configuration file is "/etc/CCcam.cfg" !!!
#
# 4) Download a specific softcam binary file and upload it to the set-top box - usually to the "/usr/bin" folder.
# Set its executable attributes:
# chmod a+x /usr/bin/softcam-name
# You can also use the following online shell script to download the OSCam binary file (I recommend it only for OpenATV):
# wget -q -O - --no-check-certificate "https://github.com/s3n0/e2scripts/raw/master/oscam-new-version-updater.sh" | bash
#
# 5) Restart the set-top box via the GUI-MENU or via the Shell:
# reboot
#
# 6) Select and activate the particular softcam via the GUI-MENU:
# MENU -> Info Panel -> Softcam Panel -> select your softcam (OSCam or CCcam) by pressing the left/right buttons and then activate it with the green button
#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# All other or unknown Enigma2 distributions - without built-in SoftCAM support :
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# 1) Download the "softcam" script file directly from my github + SET the execution rights/attributes:
# wget -O /etc/init.d/softcam --no-check-certificate https://github.com/s3n0/e2scripts/raw/master/softcam && chmod a+x /etc/init.d/softcam
#
# 2) Probably, the "/etc/init.d/softcam" script auto-start feature is not implemented in your Enigma2 distribution (in Python algorithm),
# so you must additionally create a symbolic link, and thus you have two options to choose from:
#
# A) You can use the automated system tool:
# For add (install) the startup sym-links to all default run-levels:
# update-rc.d softcam defaults 90
# For remove (uninstall) startup sym-links from all run-levels use the following command:
# update-rc.d -f softcam remove
#
# B) If the automated system tool does not work or is not installed, use the manual creation of a symbolic link:
# For add (install) the startup sym-links to run-level No.3 (Enigma2 default run-level):
# ln -sf /etc/init.d/softcam /etc/rc3.d/S90softcam
# For remove (uninstall) the symbolic link you can simply delete this link:
# rm -f /etc/rc3.d/S90softcam # or the same way with "unlink" command: unlink /etc/rc3.d/S90softcam
#
# 3) Copy your configuration files (using the FTP connection) into the particular folder:
# For the OSCam: "/etc/tuxbox/config/oscam"
# For the CCcam: "/etc" - where the main configuration file is "/etc/CCcam.cfg" !!!
#
# 4) Download a specific softcam binary file and upload it to the set-top box - usually to the "/usr/bin" folder.
# Set its executable attributes:
# chmod a+x /usr/bin/softcam-name
# You can also use the following online shell script to download the OSCam binary file (I recommend it only for OpenATV):
# wget -q -O - --no-check-certificate "https://github.com/s3n0/e2scripts/raw/master/oscam-new-version-updater.sh" | bash
#
# 5) Then restart the set-top box via the GUI-MENU or via the Shell:
# reboot
#
# ##########################################################
# ###################### USER SETUP ######################
#
# SOFTCAM BINARY DIRECTORY:
BINDIR="/usr/bin"
#
# SOFTCAM PROCESS NAME (BINARY FILE NAME):
BINFILENAME="oscam" # for CCcam: BINFILENAME="CCcam"
#
# SOFTCAM ARGUMENTS:
BINARGS="-b -r 2" # for CCcam: BINARGS="-stl"
#
# SOFTCAM EXECUTION COMMAND LINE:
CMDEXEC="${BINDIR}/${BINFILENAME} ${BINARGS}" # for CCcam: CMDEXEC="${BINDIR}/${BINFILENAME} ${BINARGS} > /dev/null 2>&1 &"
#
# LOG FILE FOR THE SOFTCAM HANDLING:
LOGFILE="/tmp/${BINFILENAME}_handling.log"
LOGSIZE=45000 # limit maximum file size [Bytes]
#
# CHANNEL (SERVICE REFERENCE CODE) FOR AUTO-UPDATE OF PREPAID TV-PACKAGES (ENTITLEMENTS) + WAITING AUTO-UPDATE TIME (SECONDS):
# A NOTE: Auto-Updating via EMM packets is required after a Softcam start/restart, for some encryption cards, for example when the card is operating in IRDETO mode.
ZAPCHANNEL="1:0:19:3731:C8E:3:EB0000:0:0:0:"
ZAPDELAY=30
AUENABLED="yes" # "no" / "yes" - run the AutoUpdate script-function in the background permanently, with Entitlements checking
OSCAMWEBIF="http://YOUR-USERNAME:[email protected]:8888" # if you do not use the password for Oscam-Webif: OSCAMWEBIF="http://127.0.0.1:8888"
OSCAMREADER="READER_LABEL"
#
# AUXILIARY SCRIPT VARIABLES:
HRLINE="--------------------------------------------------------------------------"
MANPAGE="PROPER USE OF THE SCRIPT:
${0} <ARGUMENT>
POSSIBLE ARGUMENTS:
$(cat ${0} | grep -wE '^[[:space:]]+[a-z\|\-]+\)$' | tr -d ')')"
#
# ############### END OF THE USER SETUP ##################
# ##################### FUNCTIONS ########################
fLog()
{
if [ "$1" = "$HRLINE" ]; then
MSG="$1"
else
MSG="$(date '+%Y-%m-%d %H:%M:%S') $1" # add a timestamp if it's not a separator line (header line)
fi
#echo "$MSG" > /dev/null ### null device <--- log will disabled (do nothing)
#echo "$MSG" >> $LOGFILE ### file
#echo "$MSG" >> $LOGFILE 2>&1 ### file + stderr
#echo "$MSG" | tee -a $LOGFILE ### file + stdout(display)
echo "$MSG" 2>&1 | tee -a $LOGFILE ### file + stdout(display) + stderr
#### reduction the log filesize, if neccessary (delete first 50 lines):
if [ -f "$LOGFILE" ] && [ $(wc -c <"$LOGFILE") -gt $LOGSIZE ]; then sed -i -e 1,50d "$LOGFILE"; fi
}
fStop()
{
fLog "Stopping '$1'..."
if ! pidof $1 > /dev/null; then
fLog "...no '$1' process is running."
fLog "$HRLINE"
exit 7 # 1=generic or unspecified error ; 2=invalid or excess argument(s) ; 3=unimplemented feature (for example, "reload") ; 4=user had insufficient privilege ; 5=program is not installed ; 6=program is not configured ; 7=program is not running
else
[ "$AUENABLED" = "yes" ] && fBackgroundScript stop
fLog "...sending the SIGKILL signal to the service '$1' / PID '$(pidof $1)'"
killall -9 $1 > /dev/null
sleep 0.5s # changed from 1s to 0.5s, as the '/lib/python/Screens/SoftcamSetup.py' (OpenATV github) restarts the softcam script too quickly with the 'stop' argument and 1 sec. later it immediately starts the script again with the argument 'start' i.e. without using the 'restart' argument ! unfortunately my softcam script is testing as the first normal softcam stopping, which can take about 0.3 - 0.7 secs
if pidof $1 > /dev/null; then
fLog "...'$1' was not killed by the SIGKILL signal. The process is still running. Probably a system error ?!"
fLog "$HRLINE"
exit 1
else
fLog "...'$1' was successfully killed."
fi
fi
}
fStart()
{
fLog "Starting '$1'..."
if pidof $1 > /dev/null; then
fLog "...'$1' process has already started and is still running."
else
fLog "...command-line used to start the process: ${CMDEXEC}"
$CMDEXEC
sleep 0.8s
if pidof $1 > /dev/null; then
fLog "...'$1' was successfully started."
[ "$AUENABLED" = "yes" ] && fBackgroundScript start
else
fLog "ERROR! '$1' start failed!"
fLog "$HRLINE"
exit 1
fi
fi
}
fRestart()
{
if pidof $1 > /dev/null; then fStop $1; fi # the process will be stopped / killed only if it's running
fStart $1
}
fGetVer()
{
case `echo "$BINFILENAME" | tr '[:upper:]' '[:lower:]'` in # `echo "$BINFILENAME" | awk '{print tolower($0)}'`
"oscam"*|"ncam"*) VER=$($BINDIR/$BINFILENAME -V | grep 'Version\|IPv6' | sed 's/Version:[ ]*//' | sed 's/IPv6.*yes/with IPv6/g' | sed 's/IPv6.*no/IPv4-only/g' | sed ':a;N;$!ba;s/\n/ /g') ;;
"cccam"*) VER=$(strings $BINDIR/$BINFILENAME | grep -E 'CCcam[[:space:]][0-9/./-]+') ;; # it is not completely regular to get the CCcam version, but it works :)
*) VER="ERROR! version/info detection for '${BINFILENAME}' has not yet been implemented in the script algorithm" ;;
esac
[ -z "$VER" ] && VER="ERROR! failed to get version/info of softcam '${BINFILENAME}'"
echo "$VER"
}
E2inStandby()
{
wget -q -O - http://127.0.0.1/api/powerstate | grep -qi 'instandby.*true' # check if Enigma2 is in standby mode
}
fBackgroundScript()
{
case "$1" in # start | stop the background shell-script
start)
# prepare the background shell-script (in '/tmp' folder)
if [ ! -f "/tmp/softcam_AU_on_background.sh" ]; then
CMD="#!/bin/bash
trap 'exit' INT TERM ERR
trap 'kill 0' EXIT
while true
do
if wget -qO- 'http://127.0.0.1:80/api/powerstate' | grep -qi 'instandby.*true' && wget -qO- '${OSCAMWEBIF}/status.html' | grep '${OSCAMREADER}' | grep -qiE 'no[[:space:]]entitlements'; then
wget -qO- 'http://127.0.0.1:80/api/zap?sRef=${ZAPCHANNEL}' > /dev/null 2>&1
date '+%Y-%m-%d %H:%M:%S' | tr -d '\n' >> ${LOGFILE}; echo -e ' Zap the tuner to the '${ZAPCHANNEL}' channel/SRC. The tuner will turn off automatically after '${ZAPDELAY}' seconds.' >> ${LOGFILE}
sleep ${ZAPDELAY}
wget -qO- 'http://127.0.0.1:80/api/zap?sRef=0' > /dev/null 2>&1
date '+%Y-%m-%d %H:%M:%S' | tr -d '\n' >> ${LOGFILE}; echo -e ' The tuner has been turned off (zapped to the null channel/SRC).\n'${HRLINE}' ' >> ${LOGFILE}
fi
sleep 600 # 600 sec. = 10 min.
done
exit 0
"
echo "${CMD}" > /tmp/softcam_AU_on_background.sh
chmod a+x /tmp/softcam_AU_on_background.sh
fi
/bin/bash "/tmp/softcam_AU_on_background.sh" &>/dev/null & disown
fLog "...the '/tmp/softcam_AU_on_background.sh' script was started"
;;
stop)
PID_SCRIPT=$(ps aux | grep 'softcam_AU_on_background.sh' | grep -v 'grep' | awk '{print $2}')
#PID_CHILDS=$(pgrep -P $PID_SCRIPT)
#kill $PID_SCRIPT $PID_CHILDS
pkill -P $PID_SCRIPT
rm -f "/tmp/softcam_AU_on_background.sh"
fLog "...the '/tmp/softcam_AU_on_background.sh' script was stopped"
;;
esac
}
# ##########################################################
# ##########################################################
# ##########################################################
# CHECKING THE EXISTENCE OF THE BINARY/EXEC FILE
if [ ! -x "$BINDIR/$BINFILENAME" ]; then
fLog "ERROR! Executable binary file '$BINDIR/$BINFILENAME' does not exist!"
fLog "$HRLINE"
exit 5 # 1=generic or unspecified error (current practice) ; 2=invalid or excess argument(s) ; 3=unimplemented feature (for example "reload") ; 4=user had insufficient privilege ; 5=program is not installed ; 6=program is not configured ; 7=program is not running
fi
# CALL FUNCTIONS ACCORDING TO INPUT ARGUMENTS OF THE SHELL SCRIPT **************************
case "$1" in
start)
fStart "$BINFILENAME"
fLog "$HRLINE"
;;
stop)
fStop "$BINFILENAME"
fLog "$HRLINE"
;;
restart|reload|force-reload)
fRestart "$BINFILENAME"
fLog "$HRLINE"
;;
status)
echo -n "$BINFILENAME "
if pidof $BINFILENAME > /dev/null # RETURNED STATUS:
then echo "running"; exit 0 # 0 = program is running -or- service is OK ; 1 = program is dead with "/var/run" pid-file exists ; 2 = program is dead with "/var/lock" lock-file exists ;
else echo "stopped"; exit 3 # 3 = program is not running ; 4 = status is unknown ; 5-99 = reserved for future LSB use ; 100-254 = reserved
fi
;;
version|info)
fGetVer
;;
*)
fLog "Unknown or missing arguments !"
echo "$MANPAGE" >$(tty)
fLog "$HRLINE"
exit 2 # 1=generic or unspecified error ; 2=invalid or excess argument(s) ; 3=unimplemented feature (for example, "reload") ; 4=user had insufficient privilege ; 5=program is not installed ; 6=program is not configured ; 7=program is not running
;;
esac
exit 0