#!/usr/bin/haserl --shell=/bin/bash --upload-limit=32768 --upload-dir=/tmp <%# upload limit: 32Mb %> <% ## SuperGlue project | http://superglue.it | 2014-2015 | 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="/etc/lighhtpd/htpasswd" readonly _HOSTPSK='/etc/host.psk' readonly _TMP='/tmp' readonly _LOG="${_WWW}/log/admin.log" readonly _SCRIPTS='/opt/lib/scripts' 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" -eq 0 ]] || _TYPE='E' local _TIME; printf -v _TIME '%(%Y-%m-%d %H:%M:%S)T' -1 printf '%b\n' "$_TIME: [$_TYPE] ${@} " >> "$_LOG" [[ "$_DEBUG" -le 1 ]] || printf '%b\n' "[verbose] $_TYPE ${1}" return $_ERR } headerPrint() { case "$1" in 200|'') printf '%b' 'Status: 200 OK\r\n';; 301) printf '%b' "Status: 301 Moved Permanently\r\nLocation: ${HTTP_REFERER:=$2}\r\n";; 403) printf '%b' 'Status: 403 Forbidden\r\n';; 405) printf '%b' 'Status: 405 Method Not Allowed\r\n';; 406) printf '%b' 'Status: 406 Not Acceptable\r\n';; *) printf '%b' 'Status: 400 Bad Request\r\n';; esac printf '%b' 'Content-Type: text/html\r\n\r\n'; } ## faster echo _echo() { printf "%b" "${*}" } urlDec() { local value=${*//+/%20} for part in ${value//%/ \\x}; do printf "%b%s" "${part:0:4}" "${part:4}" done } ## only useful for debugging (remove?) setQueryVars() { if [[ "$_DEBUG" -gt 0 ]]; then _VARS=( ${!POST_*} ) local v for v in ${_VARS[@]}; do logThis "$v=${!v}" done fi } 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() { local _USER='admin' local _REALM='superglue' local _HASH _x [[ -e $_PWDFILE ]] || showMesg 'Password file not found' [[ -z "${POST_pwd##$POST_pwdd}" ]] || showMesg 'Passwords did not match' [[ ${#POST_pwd} -ge 6 ]] || showMesg 'Password must be at least 6 characters long' read _HASH _x < <(printf '%s' "$_USER:$_REALM:${POST_pwd}" | md5sum) printf '%b' "${POST_pwd}\n${POST_pwd}\n" | passwd root &>/dev/null && printf '%b' "$_USER:$_REALM:$_HASH\n" > $_PWDFILE && showMesg 'Password is changed' || showMesg 'Password change failed!' '5' } lanAddr() { logThis "new LAN addr is: $POST_lanipaddr" validIp $POST_lanipaddr || showMesg 'Not valid network address' doUci set lanipaddr $POST_lanipaddr && doUci commit network && dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null && showMesg 'New network address is set' '30' "Your server is now accessible under http://superglue.local/admin" || showMesg 'Setting network address failed' } wanSet() { if [[ ! -z $POST_wanifname ]]; then ## eth and wlan wan cases are different! ## eth wan requires: ## config interface 'wan' ## option proto 'dhcp' ## option ifname 'eth0' ## ## wlan wan requires: ## config interface 'wan' ## option proto 'dhcp' (and no 'option ifname' specified!) logThis "wan.ifname=$POST_wanifname" if [[ $POST_wanifname =~ 'eth' ]]; then doUci set wanifname $POST_wanifname doUci set wanwifacedis '1' elif [[ $POST_wanifname =~ 'wlan' ]]; then doUci set wanifname '' doUci set wanwifacedis '' fi ## case of dir505 if [[ $POST_wanifname == 'eth1' ]]; then doUci set lanifname '' elif [[ $POST_wanifname == 'wlan0' ]]; then doUci set lanifname 'eth1' fi if [[ $POST_wanproto == 'dhcp' ]]; then doUci set wanproto dhcp doUci set wanipaddr '' doUci set wanmask '' doUci set wangw '' doUci set wandns '' elif [[ $POST_wanproto == 'static' ]]; then logThis "wan.ipaddr=$POST_wanipaddr" validIp $POST_wanipaddr || showMesg 'Our IP address is not valid' '3' 'make sure to use correct address notation' validIp $POST_wangw || showMesg 'Gateway is not a valid IP address' '3' 'make sure to use correct address notation' validIp $POST_wandns || showMesg 'DNS is not a valid IP address' '3' 'make sure to use correct address notation' 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 =~ 'wlan' ]]; then ssidChange || showMesg 'wanSet: Wireless configuration failed' else doUci commit network && doUci commit wireless && dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null fi _ERR=$? [[ $_ERR -eq 0 ]] && showMesg 'Internet connection is being configured' '25' 'check your Internet connection on completion' || showMesg 'Configuring Internet connection failed' '5' fi } ssidChange() { ## check for iface [[ $POST_iface =~ ^('wan'|'lan')$ ]] || showMesg 'Error changing wireless settings' '5' 'unknown or unconfigured interface' logThis "$POST_iface is being set" setQueryVars _p=$POST_iface ## default enc for now local _enc='none' if [[ $POST_wanenc == 'wpa2' ]]; then _enc='psk2' elif [[ $POST_wanenc == 'wpa1' ]]; then _enc='psk' fi if [[ ! -z $POST_lankey ]]; then _enc='psk2' fi 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 [[ ${#_ssid} -ge 3 ]] || showMesg 'SSID must be at least 3 characters long' doUci set $_p'ssid' "${_ssid}" 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 [[ ${#_key} -ge 8 ]] || showMesg 'Passphrase must be at least 8 characters long' doUci set $_p'key' "${_key}" && doUci set $_p'enc' "${_enc}" fi [[ $_ERR -eq 0 ]] || showMesg 'ssidChange: Wireless configuration failed' if [[ $POST_iface == 'lan' ]]; then if [[ "$(doUci get lanipaddr)" != "${POST_lanipaddr}" ]]; then logThis 'local IP was changed' lanAddr fi fi doUci commit network doUci commit wireless _ERR=$? [[ $_ERR -eq 0 ]] && dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null || showMesg 'Wireless configuration failed' '5' [[ $POST_iface == 'lan' ]] && showMesg 'Local network configuration is progress' '30' 'check your connection on completion' || return 0 ## in this case wanSet() handles success message } showMesg() { local _RET=$? local _MSG=$1 local _TIMEOUT=$2 local _SUBMSG=$3 ## if set, global _ERR value prevails [[ $_ERR -gt $_RET ]] && _RET=$_ERR _MSG=${_MSG:='Configuration'} _TIMEOUT=${_TIMEOUT:='5'} _SUBMSG="${_SUBMSG} ${_TIMEOUT} seconds to get ready.." if [[ $_RET -gt 0 ]]; then local _TYPE='ERROR: ' headerPrint 406 else local _TYPE='OK: ' headerPrint 200 fi htmlHead "" _echo "