#!/usr/bin/haserl --shell=/bin/bash --upload-limit=32768 --upload-dir=/www/tmp <%# upload limit: 32Mb %> <% ## SuperGlue project | http://superglue.it | 2014 | GPLv3 ## http://git.superglue.it/superglue/serverfiles ## ## admin2.cgi - control panel for Superglue personal server ## ## example POST request: ## curl --data-urlencode 'key=value' http://host/uri ## ## returns: 200 (+ output of operation) on success ## 406 (+ error message in debug mode) on error readonly _WWW='/www' readonly _PWDFILE="/opt/lib/htpasswd" readonly _TMP="${_WWW}/tmp" readonly _LOG="${_WWW}/log/admin.log" readonly _DEBUG=1 readonly _IFS=$IFS err() { _ERR="$?" [[ "$_ERR" -gt 0 ]] || return 0 logThis "$1" headerPrint "${2:='400'}" exit "$_ERR" } logThis() { [[ "$_DEBUG" -gt 0 ]] || return 0 local _TYPE='I:' [[ "$_ERR" -gt 0 ]] && _TYPE='E:' local _TIME; printf -v _TIME '%(%d.%m.%Y %H:%M:%S)T' -1 printf '%b\n' "$_TIME $_TYPE ${@} " >> "$_LOG" [[ "$_DEBUG" -gt 1 ]] && printf '%b\n' "[verbose] $_TYPE ${1}" } headerPrint() { case "$1" in 200|'') printf '%b' 'HTTP/1.1 200 OK\r\n';; 301) printf '%b' "HTTP/1.1 301 Moved Permanently\r\nLocation: $HTTP_REFERER\r\n";; 403) printf '%b' 'HTTP/1.1 403 Forbidden\r\n';; 405) printf '%b' 'HTTP/1.1 405 Method Not Allowed\r\n';; 406) printf '%b' 'HTTP/1.1 406 Not Acceptable\r\n';; *) printf '%b' 'HTTP/1.1 400 Bad Request\r\n';; esac printf '%b' 'Content-Type: text/html\r\n\r\n'; } ## faster echo _echo() { printf "%s" "${*}" } htDigest() { _USER='admin' _PWD=$1 _REALM='superglue' _HASH=$(echo -n "$_USER:$_REALM:$_PWD" | md5sum | cut -b -32) printf "%s" "$_USER:$_REALM:$_HASH" } urlDec() { local value=${*//+/%20} for part in ${value//%/ \\x}; do printf "%b%s" "${part:0:4}" "${part:4}" done } setQueryVars() { _VARS=( ${!POST_*} ) # local v # for v in ${_VARS[@]}; do # echo $v # v=$(urlDec "${v}") # eval "_${v//POST_/}=${!v}"; # done local v for v in ${_VARS[@]}; do logThis "$v=${!v}" done #echo $POST_lanssid #env } runSuid() { local _SID=$(/usr/bin/ps -p $$ -o sid=) ## pass session id to the child local _CMD=$@ /usr/bin/sudo ./suid.sh $_CMD $_SID 2>/dev/null } getQueryFile() { local _UPLD="${HASERL_fwupload_path##*/}" logThis "'multipart': decoding stream" mv "$_TMP/$_UPLD" "$_TMP/fwupload.bin" 2>/dev/null || _ERR=$? if [[ $_ERR -gt 0 ]]; then showMesg 'Firmware upload has failed' '60' 'Reboot your Superglue server and try again' fi } validIp() { local _IP=$1 local _RET=1 if [[ $_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then OIFS=$IFS IFS='.' _IP=($_IP) IFS=$OIFS [[ ${_IP[0]} -le 255 && ${_IP[1]} -le 255 && ${_IP[2]} -le 255 && ${_IP[3]} -le 255 ]] _RET=$? fi return $_RET } pwdChange() { if [[ ! -z "${POST_pwd##$POST_pwdd}" ]]; then _ERR=1 showMesg 'Passwords did not match' fi if [[ ${#POST_pwd} -lt 6 ]]; then _ERR=1 showMesg 'Password must be at least 6 characters long' fi runSuid "echo -e \"$POST_pwd\n$POST_pwd\" | passwd root" runSuid "echo $(htDigest $POST_pwd) > $_PWDFILE" _ERR=$? if [[ $_ERR -gt 0 ]]; then showMesg 'Password change failed' else showMesg 'Password is changed' fi } lanAddr() { logThis "new LAN addr is: $POST_laddr" validIp $POST_laddr || showMesg 'Not valid network address' doUci set laddr $POST_laddr _ERR=$? if [[ $_ERR -gt 0 ]]; then showMesg 'Setting network address failed' else (sleep 1; doUci commit network; doUci commit wireless;)& showMesg 'New network address is set' '30' "Your server is now accessible under http://superglue.local/admin" fi } wanSet() { if [[ ! -z $POST_wanifname ]]; then ## eth and wlan wan cases are different! ## eth wan requires: ## config interface 'wan' ## option ifname 'eth0' ## ## config wifi-iface ## option device 'radio0' ## option network 'wan' ## option disabled '1' (or no 'config wifi-iface' section at all) ## ## wlan wan requires: ## config interface 'wan' ## option proto 'dhcp' ## (without 'option ifname' specified!) ## ## config wifi-iface ## option device 'radio0' ## option network 'wan' logThis "wan.ifname=$POST_wanifname" if [[ $POST_wanifname == 'eth0' ]]; then doUci set wanifname $POST_wanifname doUci set wanwifacedis '1' elif [[ $POST_wanifname == 'wlan1' ]]; then doUci set wanifname '' doUci set wanwifacedis '' fi if [[ $POST_wanproto == 'dhcp' ]]; then doUci set wanproto dhcp elif [[ $POST_wanproto == 'static' ]]; then logThis "wan.ipaddr=$POST_wanipaddr" doUci set wanproto static doUci set wanipaddr $POST_wanipaddr doUci set wanmask '255.255.255.0' ## fix me doUci set wangw $POST_wangw doUci set wandns $POST_wandns fi if [[ $POST_wanifname == 'wlan1' ]]; then ssidChange || showMesg 'Wireless changes failed' fi ## background the following doUci commit network && showMesg 'Internet connection is being configured' '25' 'initializing - ' || showMesg 'Configuring Internet connection failed' fi logThis "new WAN iface is: $POST_wanifname" } ssidChange() { ## check for iface [[ ! $POST_iface =~ ^('wan'|'lan')$ ]] && showMesg 'Error changing wireless settings' '30' 'unknown/unconfigured interface' logThis "$POST_iface is being set" _p=$POST_iface ## default enc for now local _enc='psk2' if [[ $POST_iface == 'wan' ]]; then local _mode='sta' local _ssid="${POST_wanssid}" local _key="${POST_wankey}" else local _mode='ap' local _ssid="${POST_lanssid}" local _key="${POST_lankey}" fi logThis "ssid: $_ssid [$_mode], key: $_key [$_enc]" #logThis $POST_wanssid if [[ ${#_ssid} -lt 4 ]]; then _ERR=1 showMesg 'SSID must be at least 4 characters long' fi doUci set $_p'ssid' "${_ssid}" _ERR=$? [[ $_ERR -gt 0 ]] && showMesg 'New SSID is not set' if [[ -z $_key ]]; then ## if key is empty set encryption to none and remove key doUci set $_p'key' && doUci set $_p'enc' 'none' _ERR=$? else if [[ ${#_key} -lt 8 ]]; then _ERR=1 showMesg 'Passphrase must be at least 8 characters long' fi doUci set $_p'key' "${_key}" && doUci set $_p'enc' "${_enc}" _ERR=$? [[ $_ERR -gt 0 ]] && showMesg 'Passphrase is not set' fi [[ $_ERR -gt 0 ]] && return $_ERR ##showMesg 'Wireless changes failed' doUci commit wireless && showMesg 'Wireless configuration applied' '10' } #showError() { # headerPrint 406 # logThis "$@" # echo "ERROR: $@" # exit 1 #} showMesg() { logThis "$@" local _MSG=$1 local _TIMEOUT=$2 local _SUBMSG=$3 _MSG=${_MSG:='Configuration'} _TIMEOUT=${_TIMEOUT:='5'} _SUBMSG="${_SUBMSG} waiting ${_TIMEOUT} seconds to get ready.." if [[ $_ERR -gt 0 ]]; then local _TYPE='ERROR: ' headerPrint 406 else local _TYPE='OK: ' headerPrint 200 fi htmlHead "" _echo "