#!/bin/bash# IPC - Functions - 32bit
#
#
# set -x #debug
#++++++++++++++++++++++++++++++ VARIABLES ++++++++++++++++++++++++++++++
IPCVERSION="11.5"
AUTORUN=/etc/rc.local
IPCCONFIG="/var/emu/conf/ipc.cfg"
CAMCONFIGS="/var/etc"
CAMBINS="/var/emu"
CAMLOGS="/var/log/ipc"
CAMJOBS=/etc/crontab
CCNAME="CCcam"
CCVERSION="CCcam.x86"
CCBIN="$CAMBINS/cccam/$CCVERSION"
CCURL="localhost"
CCWWW="/var/www/ipc"
CCCFG="$CAMCONFIGS/CCcam.cfg"
CCCFGNEW="$CAMCONFIGS/CCcam.cfg.new"
CCCFGBAK="$CAMCONFIGS/CCcam.cfg.old"
CCCHANGELOG="$CAMLOGS/CCcam-changes.log"
OENAME="OSEmu"
OEVERSION="OSEmu.armel"
OEBIN="$CAMBINS/osemu/$OEVERSION"
OEUSER="emu"
OEPASS="emu"
OEPORT="54321"
OSNAME="OScam"
OSVERSION="oscam.armel"
OSBIN="$CAMBINS/oscam/$OSVERSION"
OSCFG="$CAMCONFIGS/oscam.conf"
OSSERVER="$CAMCONFIGS/oscam.server"
OSUSER="$CAMCONFIGS/oscam.user"
OSERVICES="$CAMCONFIGS/oscam.services"
OSRVID="$CAMCONFIGS/oscam.srvid"
OSLOGFILE="$CAMLOGS/OScam.log"
OSTMP="/tmp/.oscam"
CDNAME="Camd3"
CDVERSION="camd3.x86"
CDBIN="$CAMBINS/camd3/$CDVERSION"
CDCFG="$CAMCONFIGS/camd3.config"
CDSERVERS="$CAMCONFIGS/camd3.servers"
CDUSERS="$CAMCONFIGS/camd3.users"
CDLOGFILE="$CAMLOGS/camd3.log"
NCSNAME="NewCS"
NCSVERSION="newcs.armel"
NCSBIN="$CAMBINS/newcs/$NCSVERSION"
NCSXML="$CAMCONFIGS/newcs.xml"
NCSLOG="$CAMLOGS/newcs.log"
SBNAME="sBox"
SBVERSION="sbox.x86"
SBBIN="$CAMBINS/sbox/$SBVERSION"
SBCFG="$CAMCONFIGS/sbox.conf"
SBLOGFILE="/tmp/sbox.log"
CUSTOMNAME=""
CUSTOMVERSION="oscam.x86"
CUSTOMBIN="$CAMBINS/oscam/$CUSTOMVERSION"
CUSTOMCFG="$CAMCONFIGS/oscam.conf"
CUSTOMLOGFILE="$CAMLOGS/OScam.log"
CUSTOMTMP=""
BACKUPDIR="/var/backups/ipc"
TMPDIR="/var/emu/tmp/script"
TMPFILE1="$TMPDIR/CCcam1.tmp"
TMPFILE2="$TMPDIR/CCcam2.tmp"
TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
TIMESTAMP2=`date +"%Y-%m-%d %H:%M"`
INFOPHP_CONFIG=/var/www/ipc/infophp/config.php
INFOPHP_WORKPATH='/var/emu/tmp/infophp/'
INFOPHP_UPDATEFROMBUTTON='true'
INFOPHP_FULLRESHARE='true'
INFOPHP_COUNTRYWHOIS='true'
IPURL="http://ipc.pebkac.at/ipc"
FMURL="http://ipc.pebkac.at/forum"
WIKIURL="http://ipc.pebkac.at/wiki"
SHSETUP=ipcsetup.sh
SupportedCams="OScam OSEmu NewCS"
CAMSbackup="$CAMBINS/BACKUP"
WEBIF_MenueAccessFile="/var/emu/conf/.webmenueaccess"
WEBIF_DEFAULTmenueaccess="Home;Forum"
WEBIF_EMailsDB="/var/www/ipc/page/include/EMails.db"
WEBIF_DefaultStartFile="/var/emu/conf/.webdefaultstart"
WEBIF_SQLITEdb="/var/www/ipc/page/include/webif.db"
WEBIF_FAILEDauthLOG="/var/www/ipc/page/include/WEBIF_FAILEDauth.log"
SQLITEtable["1"]="CREATE TABLE IF NOT EXISTS auth (id INTEGER PRIMARY KEY,user TEXT,pass TEXT,created INT,lastvisit INT,online INT DEFAULT '0',ip TEXT,lastlogin INT);"
SQLITEtable["2"]="CREATE TABLE IF NOT EXISTS pm (id INTEGER PRIMARY KEY,fromid INT,toid INT,created INT,message TEXT);"
SQLITEtable["3"]="CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY,user_id INT,setting TEXT,value TEXT);"
SQLITEtable["4"]="CREATE TABLE IF NOT EXISTS emails (id INTEGER PRIMARY KEY,user TEXT,email TEXT,grp TEXT);"
SQLITEtableName["1"]="auth"
SQLITEtableName["2"]="pm"
SQLITEtableName["3"]="settings"
SQLITEtableName["4"]="emails"
AddOnInfos="/var/emu/conf/addons.info"
NOADDON="camd3-softkey.sh oscam-compile.sh oscam-precomp.sh softkey.sh update_webif_help.sh"
#++++++++++++++++++++++++++++++ VARIABLES ++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++ FUNCTIONS ++++++++++++++++++++++++++++++
# Favorit Editor..
[ -z "$useEDITOR" ] && useEDITOR="nano"
# Tempdir erstellen, Rechte setzen, leeren
tempdir() {
#echo "Function: tempdir"
[ ! -d $TMPDIR ] && mkdir -p $TMPDIR && chmod 777 -R $TMPDIR >/dev/null 2>&1
rm -f $TMPDIR/* >/dev/null 2>&1
cd $TMPDIR
}
# Rechte setzen
setrights() {
#check /etc/rc.local
[ ! -x "/etc/rc.local" ] && chmod u+x /etc/rc.local >/dev/null 2>&1
OWNER="www-data:www-data"
chown -R $OWNER /var/etc /var/keys > /dev/null 2>&1
chmod 755 /var/etc > /dev/null 2>&1
#CCcam.cfg - Rechte und Besitzer
chown $OWNER $CCCFG > /dev/null 2>&1
#CCcam key files
chmod 755 /var/keys/CCcam.* >/dev/null 2>&1
chown $OWNER /var/keys/CCcam.* >/dev/null 2>&1
#OScam config files
chmod 755 $OSCFG >/dev/null 2>&1
chmod 755 $OSSERVER >/dev/null 2>&1
chmod 755 $OSUSER >/dev/null 2>&1
chmod 755 $OSERVICES >/dev/null 2>&1
chmod 755 $OSRVID >/dev/null 2>&1
chown $OWNER $OSCFG >/dev/null 2>&1
chown $OWNER $OSSERVER >/dev/null 2>&1
chown $OWNER $OSUSER >/dev/null 2>&1
chown $OWNER $OSERVICES >/dev/null 2>&1
chown $OWNER $OSRVID >/dev/null 2>&1
#NewCS config file
chmod 755 $NCSXML >/dev/null 2>&1
chown $OWNER $NCSXML >/dev/null 2>&1
#EMU
chmod 755 -R /var/emu > /dev/null 2>&1
chmod 777 -R /var/emu/tmp > /dev/null 2>&1
chown -R $OWNER /var/emu/tmp > /dev/null 2>&1
#Changelog - Erstellen, Rechte, Besitzer
[ ! -f $CCCHANGELOG ] && echo "" > $CCCHANGELOG > /dev/null 2>&1
chmod 755 $CCCHANGELOG > /dev/null 2>&1
chown $OWNER $CCCHANGELOG > /dev/null 2>&1
#InfoPHP Tempdir und Rechte
[ ! -d $INFOPHP_WORKPATH ] && mkdir -p $INFOPHP_WORKPATH > /dev/null 2>&1
chmod 777 -R $INFOPHP_WORKPATH > /dev/null 2>&1
chown -R $OWNER $INFOPHP_WORKPATH > /dev/null 2>&1
chown $OWNER $INFOPHP_CONFIG > /dev/null 2>&1
#WebAccess
chown $OWNER $WEBIF_webaccess >/dev/null 2>&1
chown $OWNER $WEBIF_MenueAccessFile >/dev/null 2>&1
#WebIf
chown -R $OWNER /var/www/ipc >/dev/null 2>&1
[ -d "/var/www/oscaminfo" ] && chown -R $OWNER /var/www/oscaminfo >/dev/null 2>&1
[ -d "/var/www/ui" ] && chown -R $OWNER /var/www/ui >/dev/null 2>&1
}
# Paket Konfiguration auslesen
readipccfg() {
#echo "Function: readipccfg"
source $IPCCONFIG
}
# CCcam.cfg Konfiguration auslesen
readcccamcfg() {
#echo "Function: readcccamcfg"
CCUSER=$(grep -i "WEBINFO USERNAME" $CCCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
CCPASS=$(grep -i "WEBINFO PASSWORD" $CCCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
CCPORT=$(grep -i "WEBINFO LISTEN PORT" $CCCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
CCCHAN=$(grep -i "CHANNELINFO FILE" $CCCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
CCPROV=$(grep -i "PROVIDERINFO FILE" $CCCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
CCSPORT=$(grep -i "SERVER LISTEN PORT" $CCCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
[ "$CCSPORT" = "" ] && CCSPORT="12000"
[ "$CCPORT" = "" ] && CCPORT="16001"
#echo "CCUSER: $CCUSER"
#echo "CCPASS: $CCPASS"
#echo "CCPORT: $CCPORT"
}
# Teste ob CCcam laeuft
cccamrun() {
#echo "Function: cccamrun"
PID="$(pgrep -x $CCVERSION)"
if [[ ! -z $PID ]] ; then
dummy="1"
#echo "CCcam laeuft"
else
echo "CCcam ist nicht gestartet"
exit 1
fi
}
# Weblogin testen
weblogon() {
#echo "Function: weblogon"
WUP=""
[ "$CCUSER" != "" ] && WUP="$CCUSER:$CCPASS@"
CCWEBIF="http://$WUP$CCURL:$CCPORT"
WEBLOGIN=$(wget -q -O - $CCWEBIF)
[[ -z $WEBLOGIN ]] && clear && echo "CCcam Weblogon ERROR!" && exit 1
}
# E-Mail senden
mailsend() {
readipccfg
if [ -n "$SMTPTLS" ]; then
[[ $SMTPTLS == 1 ]] && OPT="-o tls=yes" || OPT="-o tls=no"
else
OPT=""
fi
echo "Sende E-Mail..."
sendEmail -f $SMTPFROM -t $SMTPTO -u $1 -m $2 $OPT -s $SMTPSERVER -xu $SMTPUSER -xp $SMTPPASS
}
# InfoPHP Konfiguration schreiben
infophpconfwrite() {
echo "<?php" >> $TMPFILE1
echo "" >> $TMPFILE1
echo "// CONFIG CCcamInfoPhp" >> $TMPFILE1
echo "// Diese Config wird automatisch beim schliessen von [cccam cfg] erzeugt!" >> $TMPFILE1
echo "" >> $TMPFILE1
echo '$CCCamWebInfo[] = array("'$CCURL'","'$CCPORT'","'$CCUSER'","'$CCPASS'"); //Hier die Infos aus der CCcam.cfg eintragen' >> $TMPFILE1
echo "" >> $TMPFILE1
echo '$work_path = "'$INFOPHP_WORKPATH'"; // Verzeichnis der temporaeren InfoPhp Dateien' >> $TMPFILE1
echo '$update_from_button = '$INFOPHP_UPDATEFROMBUTTON'; // Update Button anzeigen?' >> $TMPFILE1
echo '$fullReshare = '$INFOPHP_FULLRESHARE'; // Reshareanzahl der Pairkarten anzeigen?' >> $TMPFILE1
echo '$country_whois = '$INFOPHP_COUNTRYWHOIS'; // Laenderkennzeichen anzeigen?' >> $TMPFILE1
echo '$password = "'$INFOPHP_PASS'"; // InfoPhp Loginseite Kennwort' >> $TMPFILE1
echo "" >> $TMPFILE1
echo "?>" >> $TMPFILE1
if [ -f $INFOPHP_CONFIG ] ; then
cp -f $TMPFILE1 $INFOPHP_CONFIG
echo ""
echo -e "\033[1;37mInfoPHP Konfiguration wurde uebertragen\033[0m"
else
echo -e "\033[1;31m$INFOPHP_CONFIG existiert nicht!\033[0m"
fi
rm -f $TMPDIR/*
}
# enable/disable cam in /etc/crontab and /etc/rc.local
function change_cam_jobs_as () {
CAM=$1
ACTION=$2
if [ "$ACTION" == "on" ]; then
F=crontab
sed -e "s=^#\(.*$CAM-watchdog\)=\1=" /etc/$F >/tmp/$F && mv /tmp/$F /etc/$F
F=rc.local
sed -e "s=^#\(.*$CAM start\)=\1=" /etc/$F >/tmp/$F && mv /tmp/$F /etc/$F
fi
if [ "$ACTION" == "off" ]; then
F=crontab
sed -e "/^[^#].*$CAM-watchdog/s/^/#/" /etc/$F >/tmp/$F && mv /tmp/$F /etc/$F
F=rc.local
sed -e "/^[^#].*$CAM start/s/^/#/" /etc/$F >/tmp/$F && mv /tmp/$F /etc/$F
fi
chmod u+x /etc/rc.local >/dev/null 2>&1
}
# which Language..
function GetLanguage () {
readipccfg
IPCconfDIR=$(echo $IPCCONFIG|sed -e "s/$(basename $IPCCONFIG)//g")
for l in $(ls ${IPCconfDIR}langs/*); do
if [ "$DEFAULTlanguage" == "$(basename $l .sh)" ]; then
. $l
break
fi
done
}
function collect_needed_packs () {
InstallPAK=""
for PACK in $1; do
if [ -z "$(dpkg -l | grep "ii" | awk {'print $2'} | grep -xi $PACK)" ]; then
[ -z "$InstallPAK" ] && InstallPAK="$PACK" || InstallPAK="$InstallPAK $PACK"
fi
done
}
function check4installed_sqlite () {
#collect_needed_packs "sqlite3 php5-sqlite"
if [ -n "$InstallPAK" ]; then
apt-get clean >/dev/null
apt-get install -yf $InstallPAK >/dev/null 2>&1
fi
}
function sqlite_check_table () {
dbRESULT=$(sqlite3 $WEBIF_SQLITEdb "SELECT count(name) FROM sqlite_master WHERE type='table' AND name=\"$1\"")
echo "$dbRESULT"
}
function sqlite_create_tables () {
#check4installed_sqlite
RETURN=0
if [ -z "$1" ]; then
for index in ${!SQLITEtableName
[*]}; do
[ "$(sqlite_check_table ${SQLITEtableName[$index]})" == "1" ] && continue
dbRESULT=$(sqlite3 $WEBIF_SQLITEdb "${SQLITEtable[$index]}")
[ -z "$dbRESULT" ] && RETURN=1
done
else
if [ "$(sqlite_check_table $1)" == "1" ]; then
RETURN=1
else
for index in ${!SQLITEtableName
[*]}; do
if [ "${SQLITEtableName[$index]}" = "$1" ]; then
dbRESULT=$(sqlite3 $WEBIF_SQLITEdb "${SQLITEtable[$index]}")
[ -z "$dbRESULT" ] && RETURN=1
fi
done
fi
fi
echo $RETURN
}
# sqlite - create webif auth db file..
function sqlite_create_auth_db () {
#check4installed_sqlite
#STRUCTURE="${SQLITEtable[1]}"
#cat /dev/null > $WEBIF_SQLITEdb
#echo "$STRUCTURE" > /tmp/tmpstructure
#sqlite $WEBIF_SQLITEdb < /tmp/tmpstructure
#rm -f /tmp/tmpstructure
sqlite3 $WEBIF_SQLITEdb "${SQLITEtable[1]}"
chown www-data:www-data $WEBIF_SQLITEdb
}
# sqlite - get webif user fields
function sqlite_get_user () {
[ ! -f $WEBIF_SQLITEdb -o $(sqlite_check_table auth) == 0 ] && sqlite_create_auth_db
dbRESULT=$(sqlite3 $WEBIF_SQLITEdb "SELECT user FROM auth WHERE user=\"$1\"")
if [ ! -z "$dbRESULT" ]; then
echo $(sqlite3 $WEBIF_SQLITEdb "SELECT $2 FROM auth WHERE user=\"$1\"")
else
echo ""
fi
}
# sqlite - write webif user/pass
function sqlite_write_userpass () {
[ ! -f $WEBIF_SQLITEdb -o $(sqlite_check_table auth) == 0 ] && sqlite_create_auth_db
dbRESULT=$(sqlite_get_user "$1" pass)
md5passhash=$(echo -n "$2"|md5sum|cut -d" " -f1)
if [ ! -z "$dbRESULT" ]; then
dbEXEC=$(sqlite3 $WEBIF_SQLITEdb "UPDATE auth SET pass=\"$md5passhash\" WHERE user=\"$1\""
else
dbEXEC=$(sqlite3 $WEBIF_SQLITEdb "INSERT INTO auth (user,pass,created) VALUES (\"$1\",\"$md5passhash\",\"$(date +%s)\")"
fi
# dbLIST=( $(sqlite3 $WEBIF_SQLITEdb "SELECT * FROM auth WHERE 1") )
# oldIFS=$IFS ; IFS=$'\n'
# for ROW in "${dbLIST[@]}"; do
# # Parsing data (sqlite returns a pipe separated string)
# ID=$(echo $ROW | awk '{split($0,a,"|"); print a[1]}')
# USER=$(echo $ROW | awk '{split($0,a,"|"); print a[2]}')
# PASS=$(echo $ROW | awk '{split($0,a,"|"); print a[3]}')
# # Printing my data
# echo "$ID) "$USER" -> "$PASS;
# done
# IFS=$oldIFS
}
# sqlite - delete webif user
function sqlite_delete_user () {
[ ! -f $WEBIF_SQLITEdb -o $(sqlite_check_table auth) == 0 ] && sqlite_create_auth_db
dbRESULT=$(sqlite3 $WEBIF_SQLITEdb "SELECT user FROM auth WHERE user=\"$1\"")
if [ ! -z "$dbRESULT" ]; then
echo $(sqlite3 $WEBIF_SQLITEdb "DELETE FROM auth WHERE user=\"$1\"")
else
echo ""
fi
}
# get all oscam protocol ports
function get_oscam_ports () {
declare -A OSPORT
OSPORT["newcamd"]=0 ;#newcamd (tcp)
OSPORT["cccam"]=0 ;#cccam (tcp)
OSPORT["csp"]=0 ;#csp (tcp)
OSPORT["camd33"]=0 ;#camd33 (tcp)
OSPORT["cs357x"]=0 ;#camd35 / cs357x (udp)
OSPORT["cs378x"]=0 ;#camd35 / cs378x (tcp)
OSPORT["radegast"]=0 ;#radegast (tcp)
OSPORT["gbox"]=0 ;#gbox (tcp)
UDP="cs357x"
UDPuse=0
while read line; do
Break=0
line=$(echo $line | sed -e 's/ //g')
for index in ${!OSPORT
[*]}; do
if [ "${OSPORT[$index]}" == "1" ]&&[ "$(echo $line | cut -d'=' -f 1)" = "port" ]; then
if [ "$index" == "newcamd" ]; then
ports=""
PORTSline=$(echo $line | cut -d'=' -f 2-)
for p in $(echo $PORTSline | tr ';' ' '); do
for pp in $(echo $p | cut -d'@' -f1); do
[ -z "$ports" ] && ports="$pp" || ports+=" $pp"
done
done
OSPORT["$index"]=$ports
elif [ "$index" == "cccam" ]; then
OSPORT["$index"]=$(echo $line | cut -d'=' -f 2- | tr "," " ")
else
OSPORT["$index"]=$(echo $line | cut -d'=' -f 2-)
fi
Break=1
break
fi
done
[ "$Break" == "1" ] && continue
for index in ${!OSPORT
[*]}; do
if [[ "$line" = "[$index]" ]]; then
OSPORT["$index"]=1
fi
done
done < $OSCFG
for index in ${!OSPORT
[*]}; do
[ "${OSPORT[$index]}" == "0" -o "${OSPORT[$index]}" == "1" ] && unset OSPORT[$index] >/dev/null 2>&1
done
# check if there are udp ports in use..
for udp in $UDP; do
[ ! -z "${OSPORT[$udp]}" ] && UDPuse=1
done
# only for debug:
if [ -n "$1" -a "$1" == "-debug" ]; then
for p in ${!OSPORT
[*]}; do
echo "$p: ${OSPORT[$p]}"
done
fi
}