admin2.cgi 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. #!/usr/bin/haserl --shell=/bin/bash --upload-limit=32768 --upload-dir=/tmp
  2. <%# upload limit: 32Mb %>
  3. <%
  4. ## SuperGlue project | http://superglue.it | 2014-2015 | GPLv3
  5. ## http://git.superglue.it/superglue/serverfiles
  6. ##
  7. ## admin2.cgi - control panel for Superglue personal server
  8. ##
  9. ## example POST request:
  10. ## curl --data-urlencode 'key=value' http://host/uri
  11. ##
  12. ## returns: 200 (+ output of operation) on success
  13. ## 406 (+ error message in debug mode) on error
  14. readonly _WWW='/www'
  15. readonly _PWDFILE="/etc/lighhtpd/htpasswd"
  16. readonly _HOSTPSK='/etc/openvpn/host.psk'
  17. readonly _TMP='/tmp'
  18. readonly _LOG="${_WWW}/log/admin.log"
  19. readonly _SCRIPTS='/opt/lib/scripts'
  20. readonly _DEBUG=1
  21. readonly _IFS=$IFS
  22. err() {
  23. _ERR="$?"
  24. [[ "$_ERR" -gt 0 ]] || return 0
  25. logThis "$1"
  26. headerPrint "${2:='400'}"
  27. exit "$_ERR"
  28. }
  29. logThis() {
  30. [[ "$_DEBUG" -gt 0 ]] || return 0
  31. local _TYPE='I'
  32. [[ "$_ERR" -eq 0 ]] || _TYPE='E'
  33. local _TIME; printf -v _TIME '%(%Y-%m-%d %H:%M:%S)T' -1
  34. printf '%b\n' "$_TIME: [$_TYPE] ${@} " >> "$_LOG"
  35. [[ "$_DEBUG" -le 1 ]] || printf '%b\n' "[verbose] $_TYPE ${1}"
  36. return $_ERR
  37. }
  38. headerPrint() {
  39. case "$1" in
  40. 200|'') printf '%b' 'Status: 200 OK\r\n';;
  41. 301) printf '%b' "Status: 301 Moved Permanently\r\nLocation: ${HTTP_REFERER:=$2}\r\n";;
  42. 403) printf '%b' 'Status: 403 Forbidden\r\n';;
  43. 405) printf '%b' 'Status: 405 Method Not Allowed\r\n';;
  44. 406) printf '%b' 'Status: 406 Not Acceptable\r\n';;
  45. *) printf '%b' 'Status: 400 Bad Request\r\n';;
  46. esac
  47. printf '%b' 'Content-Type: text/html\r\n\r\n';
  48. }
  49. ## faster echo
  50. _echo() {
  51. printf "%b" "${*}"
  52. }
  53. urlDec() {
  54. local value=${*//+/%20}
  55. for part in ${value//%/ \\x}; do
  56. printf "%b%s" "${part:0:4}" "${part:4}"
  57. done
  58. }
  59. ## only useful for debugging (remove?)
  60. setQueryVars() {
  61. if [[ "$_DEBUG" -gt 0 ]]; then
  62. _VARS=( ${!POST_*} )
  63. local v
  64. for v in ${_VARS[@]}; do
  65. logThis "$v=${!v}"
  66. done
  67. fi
  68. }
  69. getQueryFile() {
  70. local _UPLD="${HASERL_fwupload_path##*/}"
  71. logThis "'multipart': decoding stream"
  72. mv "$_TMP/$_UPLD" "$_TMP/fwupload.bin" 2>/dev/null || _ERR=$?
  73. if [[ $_ERR -gt 0 ]]; then
  74. showMesg 'Firmware upload has failed' '60' 'Reboot your Superglue server and try again'
  75. fi
  76. }
  77. validIp() {
  78. local _IP=$1
  79. local _RET=1
  80. if [[ $_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  81. OIFS=$IFS
  82. IFS='.'
  83. _IP=($_IP)
  84. IFS=$OIFS
  85. [[ ${_IP[0]} -le 255 && ${_IP[1]} -le 255 && ${_IP[2]} -le 255 && ${_IP[3]} -le 255 ]]
  86. _RET=$?
  87. fi
  88. return $_RET
  89. }
  90. pwdChange() {
  91. local _USER='admin'
  92. local _REALM='superglue'
  93. local _HASH _x
  94. [[ -e $_PWDFILE ]] || showMesg 'Password file not found'
  95. [[ -z "${POST_pwd##$POST_pwdd}" ]] || showMesg 'Passwords did not match'
  96. [[ ${#POST_pwd} -ge 6 ]] || showMesg 'Password must be at least 6 characters long'
  97. read _HASH _x < <(printf '%s' "$_USER:$_REALM:${POST_pwd}" | md5sum)
  98. printf '%b' "${POST_pwd}\n${POST_pwd}\n" | passwd root &>/dev/null &&
  99. printf '%b' "$_USER:$_REALM:$_HASH\n" > $_PWDFILE &&
  100. showMesg 'Password is changed' ||
  101. showMesg 'Password change failed!' '5'
  102. }
  103. lanAddr() {
  104. logThis "new LAN addr is: $POST_lanipaddr"
  105. validIp $POST_lanipaddr || showMesg 'Not valid network address'
  106. doUci set lanipaddr $POST_lanipaddr &&
  107. doUci commit network &&
  108. dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null &&
  109. showMesg 'New network address is set' '30' "Your server is now accessible under <a href='http://superglue.local/admin'>http://superglue.local/admin</a>" ||
  110. showMesg 'Setting network address failed'
  111. }
  112. wanSet() {
  113. if [[ ! -z $POST_wanifname ]]; then
  114. ## eth and wlan wan cases are different!
  115. ## eth wan requires:
  116. ## config interface 'wan'
  117. ## option proto 'dhcp'
  118. ## option ifname 'eth0'
  119. ##
  120. ## wlan wan requires:
  121. ## config interface 'wan'
  122. ## option proto 'dhcp' (and no 'option ifname' specified!)
  123. logThis "wan.ifname=$POST_wanifname"
  124. if [[ $POST_wanifname =~ 'eth' ]]; then
  125. doUci set wanifname $POST_wanifname
  126. doUci set wanwifacedis '1'
  127. elif [[ $POST_wanifname =~ 'wlan' ]]; then
  128. doUci set wanifname ''
  129. doUci set wanwifacedis ''
  130. fi
  131. ## case of dir505
  132. if [[ $POST_wanifname == 'eth1' ]]; then
  133. doUci set lanifname ''
  134. elif [[ $POST_wanifname == 'wlan0' ]]; then
  135. doUci set lanifname 'eth1'
  136. fi
  137. if [[ $POST_wanproto == 'dhcp' ]]; then
  138. doUci set wanproto dhcp
  139. doUci set wanipaddr ''
  140. doUci set wanmask ''
  141. doUci set wangw ''
  142. doUci set wandns ''
  143. elif [[ $POST_wanproto == 'static' ]]; then
  144. logThis "wan.ipaddr=$POST_wanipaddr"
  145. validIp $POST_wanipaddr || showMesg 'Our IP address is not valid' '3' 'make sure to use correct address notation'
  146. validIp $POST_wangw || showMesg 'Gateway is not a valid IP address' '3' 'make sure to use correct address notation'
  147. validIp $POST_wandns || showMesg 'DNS is not a valid IP address' '3' 'make sure to use correct address notation'
  148. doUci set wanproto static
  149. doUci set wanipaddr $POST_wanipaddr
  150. doUci set wanmask '255.255.255.0' ## fix me
  151. doUci set wangw $POST_wangw
  152. doUci set wandns $POST_wandns
  153. fi
  154. if [[ $POST_wanifname =~ 'wlan' ]]; then
  155. ssidChange || showMesg 'wanSet: Wireless configuration failed'
  156. else
  157. doUci commit network &&
  158. doUci commit wireless
  159. dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null
  160. fi
  161. _ERR=$?
  162. [[ $_ERR -eq 0 ]] &&
  163. showMesg 'Internet connection is being configured' '25' 'check your Internet connection on completion' ||
  164. showMesg 'Configuring Internet connection failed' '5'
  165. fi
  166. }
  167. ssidChange() {
  168. ## check for iface
  169. [[ $POST_iface =~ ^('wan'|'lan')$ ]] || showMesg 'Error changing wireless settings' '5' 'unknown or unconfigured interface'
  170. logThis "$POST_iface is being set"
  171. setQueryVars
  172. _p=$POST_iface
  173. ## default enc for now
  174. local _enc='none'
  175. if [[ $POST_wanenc == 'wpa2' ]]; then
  176. _enc='psk2'
  177. elif [[ $POST_wanenc == 'wpa1' ]]; then
  178. _enc='psk'
  179. fi
  180. if [[ ! -z $POST_lankey ]]; then
  181. _enc='psk2'
  182. fi
  183. if [[ $POST_iface == 'wan' ]]; then
  184. local _mode='sta'
  185. local _ssid="${POST_wanssid}"
  186. local _key="${POST_wankey}"
  187. else
  188. local _mode='ap'
  189. local _ssid="${POST_lanssid}"
  190. local _key="${POST_lankey}"
  191. fi
  192. #logThis "ssid: $_ssid [$_mode], key: $_key [$_enc]"
  193. #logThis $POST_wanssid
  194. [[ ${#_ssid} -ge 3 ]] || showMesg 'SSID must be at least 3 characters long'
  195. doUci set $_p'ssid' "${_ssid}"
  196. if [[ -z $_key ]]; then
  197. ## if key is empty set encryption to none and remove key
  198. doUci set $_p'key' && doUci set $_p'enc' 'none'
  199. _ERR=$?
  200. else
  201. [[ ${#_key} -ge 8 ]] || showMesg 'Passphrase must be at least 8 characters long'
  202. doUci set $_p'key' "${_key}" && doUci set $_p'enc' "${_enc}"
  203. fi
  204. [[ $_ERR -eq 0 ]] || showMesg 'ssidChange: Wireless configuration failed'
  205. if [[ $POST_iface == 'lan' ]]; then
  206. if [[ "$(doUci get lanipaddr)" != "${POST_lanipaddr}" ]]; then
  207. logThis 'local IP was changed'
  208. lanAddr
  209. fi
  210. fi
  211. doUci commit network
  212. doUci commit wireless
  213. _ERR=$?
  214. [[ $_ERR -eq 0 ]] &&
  215. dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null ||
  216. showMesg 'Wireless configuration failed' '5'
  217. [[ $POST_iface == 'lan' ]] &&
  218. showMesg 'Local network configuration is progress' '30' 'check your connection on completion' || return 0
  219. ## in this case wanSet() handles success message
  220. }
  221. showMesg() {
  222. local _RET=$?
  223. local _MSG=$1
  224. local _TIMEOUT=$2
  225. local _SUBMSG=$3
  226. ## if set, global _ERR value prevails
  227. [[ $_ERR -gt $_RET ]] && _RET=$_ERR
  228. _MSG=${_MSG:='Configuration'}
  229. _TIMEOUT=${_TIMEOUT:='5'}
  230. _SUBMSG="${_SUBMSG} <span id='timeout'>${_TIMEOUT}</span> seconds to get ready.."
  231. if [[ $_RET -gt 0 ]]; then
  232. local _TYPE='ERROR: '
  233. headerPrint 406
  234. else
  235. local _TYPE='OK: '
  236. headerPrint 200
  237. fi
  238. htmlHead "<meta http-equiv='refresh' content='${_TIMEOUT};url=http://${HTTP_HOST}/admin'>"
  239. _echo "<body>
  240. <h1>Superglue control panel</h1>
  241. <img src='http://${HTTP_HOST}/resources/img/superglueLogo.png' class='logo'>
  242. <hr>
  243. <h2 style='display:inline'>$_TYPE $_MSG</h2>
  244. <span style='display:block'>$_SUBMSG</span>
  245. <hr>
  246. </body>
  247. <script type='text/javascript'>(function(e){var t=document.getElementById(e);var n=t.innerHTML;var r=setInterval(function(){if(n==0){t.innerHTML='0';clearInterval(r);return}t.innerHTML=n;n--},1e3)})('timeout')
  248. </script>
  249. </html>"
  250. exit $_RET
  251. }
  252. updateFw() {
  253. local _FWFILE="${_TMP}/fwupload.bin"
  254. local _FWRESET='-c' ## preserve changes in /etc/ by default
  255. _OUT="$(sysupgrade -T $_FWFILE 2>&1)" ||
  256. { _ERR=$?; rm -rf $_FWFILE; showMesg 'This is not a firmware!' '3' "$_OUT"; }
  257. [[ $POST_fwreset == 'on' ]] && { _FWRESET='-n'; logThis 'fw reset requested'; }
  258. ## using dtach to prevent sysupgrade getting killed
  259. dtach -n -zE sysupgrade $_FWRESET $_FWFILE 2>&1 &&
  260. showMesg 'Firmware update is in progress..' '120' 'DO NOT INTERRUPT!' ||
  261. { _ERR=$?; rm -rf $_FWFILE; showMesg 'Firmware update failed..' '3'; }
  262. }
  263. usbInit() {
  264. . $_SCRIPTS/usb-part.sh
  265. _OUT="$(usbPart)" &&
  266. showMesg 'USB storage initialization is completed' '30' ||
  267. showMesg "USB init failed!\n$_OUT"
  268. # logThis 'usb init..'
  269. }
  270. rebootNow() {
  271. logThis "reboot: now!"
  272. # reboot
  273. reboot -d 3 || reboot -f
  274. showMesg 'Rebooting..' '60'
  275. }
  276. upTime() {
  277. local _T="$(uptime)" &&
  278. { headerPrint 200; printf '%b' "$_T\n"; exit 0; } ||
  279. headerPrint 406
  280. }
  281. iwScan() {
  282. . $_SCRIPTS/iw-scan.sh
  283. headerPrint 200
  284. iwScan
  285. exit 0
  286. }
  287. findUsbstor() {
  288. local _P='/sys/block/'
  289. local _D _DEV
  290. for _D in ${_P}sd*; do
  291. _DEV=$(readlink -f ${_D}/device)
  292. if [[ ${_DEV/usb} != $_DEV ]]; then
  293. _USBDEV="/dev/${_D/$_P}"
  294. fi
  295. done
  296. [[ $_USBDEV ]] || return 1
  297. }
  298. storageInfo() {
  299. if mountpoint -q $_WWW; then
  300. IFS=$'\n' _STOR=( $(df -h $_WWW) ) IFS=$_IFS
  301. _STOR=( ${_STOR[1]} )
  302. else
  303. return 1
  304. fi
  305. }
  306. swapInfo() {
  307. IFS=$'\n' _SWAP=( $(swapon -s) ) IFS=$_IFS
  308. if [[ ${_SWAP[1]} ]]; then
  309. IFS=$'\t' _SWAP=( ${_SWAP[1]} ) IFS=$_IFS
  310. ## for the lack of floats add trailing 0
  311. ## divide by 1023 and split last digit by a period
  312. _SWAP[1]="$((${_SWAP[1]}0/1023))"
  313. _SWAP[1]="${_SWAP[1]%?}.${_SWAP[1]/??}M"
  314. else
  315. unset _SWAP
  316. return 1
  317. fi
  318. }
  319. trimSpaces() {
  320. local v="$*"
  321. v="${v#"${v%%[![:space:]]*}"}"
  322. v="${v%"${v##*[![:space:]]}"}"
  323. return "$v"
  324. }
  325. sgDdns() {
  326. [[ $POST_sgddnsdomain && $POST_sgddnssubdomain ]] || showMesg 'All values must be set!'
  327. [[ ! "$POST_sgddnssubdomain" =~ [^a-zA-Z0-9$] ]] || showMesg 'Invalid domain name'
  328. [[ ! "$POST_sgddnsupdateurl" == '' ]] || showMesg 'Invalid update URL'
  329. local _CLIENT _DOMAIN _JSON _UPDATEURL _MSG _HTTPCODE _x
  330. [[ -r $_HOSTPSK ]] || showMesg 'Local PSK is not available'
  331. ## produce md5 hash from client PSK
  332. hashClient() {
  333. local _L
  334. while read -r _L; do
  335. printf "%s" ${_L%%[\#\-]*}
  336. done < $_HOSTPSK | md5sum
  337. }
  338. read -r _CLIENT _x < <(hashClient)
  339. logThis "sgvpn client hash: $_CLIENT"
  340. _UPDATEURL="$POST_sgddnsupdateurl"
  341. doUci set sgddnssubdomain "${POST_sgddnssubdomain,,}" &&
  342. doUci set sgddnsdomain "${POST_sgddnsdomain,,}" &&
  343. doUci set sgddnsclient "$_CLIENT" &&
  344. doUci set sgddnsurl "$_UPDATEURL" &&
  345. doUci set sgddnsenable '1' || showMesg 'Error setting DDNS configuration'
  346. doUci commit dyndns
  347. ## attempt first update to check if domain is available and everything checks out
  348. IFS=$'\t'; read -r _MSG _HTTPCODE < <($_SCRIPTS/dyndns-update.sh); IFS=$_IFS
  349. $_MSG="${_MSG:='Unknown error'}"
  350. logThis "sgvpn check: $_MSG"
  351. if [[ ! $_HTTPCODE == 200 ]]; then
  352. _ERR=1
  353. logThis 'error in dns update, disabling configuration'
  354. doUci set sgddnsdomain ''
  355. doUci set sgddnssubdomain ''
  356. doUci set sgddnsclient ''
  357. doUci set sgddnsurl ''
  358. doUci set sgddnsenable '0'
  359. doUci commit dyndns
  360. showMesg "$_MSG" '10'
  361. fi
  362. showMesg 'Domain configuration is in progress..' '10' 'Your address will become available right away'
  363. }
  364. sgOpenvpn() {
  365. logThis "enabling SG openvpn"
  366. if [[ $(doUci get sgopenvpnenable) == '0' ]]; then
  367. doUci set sgopenvpnenable '1' &&
  368. doUci commit openvpn &&
  369. /etc/init.d/openvpn start &&
  370. showMesg 'VPN service enabled' '5'
  371. else
  372. doUci set sgopenvpnenable '0' &&
  373. doUci commit openvpn &&
  374. /etc/init.d/openvpn stop &&
  375. showMesg 'VPN service disabled' '5'
  376. fi
  377. showMesg 'VPN configuration failed' '5' 'make sure your device is online'
  378. }
  379. doUci() {
  380. local _CMD=''
  381. local _ARG=''
  382. case $1 in
  383. get|set|commit) _CMD=$1;;
  384. *) logThis 'bad UCI command'; headerPrint 405; echo 'bad UCI command'; exit 1 ;;
  385. esac
  386. case $2 in
  387. lanssid) _ARG='wireless.@wifi-iface[0].ssid';;
  388. lanenc) _ARG='wireless.@wifi-iface[0].encryption';;
  389. lankey) _ARG='wireless.@wifi-iface[0].key';;
  390. wanwifacedis) _ARG='wireless.@wifi-iface[1].disabled';;
  391. wanssid) _ARG='wireless.@wifi-iface[1].ssid';;
  392. wanenc) _ARG='wireless.@wifi-iface[1].encryption';;
  393. wankey) _ARG='wireless.@wifi-iface[1].key';;
  394. lanifname) _ARG='network.lan.ifname';;
  395. lanipaddr) _ARG='network.lan.ipaddr';;
  396. wanifname) _ARG='network.wan.ifname';;
  397. wanproto) _ARG='network.wan.proto';;
  398. wanipaddr) _ARG='network.wan.ipaddr';;
  399. wanmask) _ARG='network.wan.netmask';;
  400. wangw) _ARG='network.wan.gateway';;
  401. wandns) _ARG='network.wan.dns';;
  402. sgddnsenable) _ARG='dyndns.superglue.enable';;
  403. sgddnsurl) _ARG='dyndns.superglue.updateurl';;
  404. sgddnsclient) _ARG='dyndns.superglue.client';;
  405. sgddnssubdomain) _ARG='dyndns.superglue.subdomain';;
  406. sgddnsdomain) _ARG='dyndns.superglue.domain';;
  407. sgopenvpnenable) _ARG='openvpn.superglue.enable';;
  408. *) if [[ $_CMD == 'commit' ]]; then
  409. _ARG=$2
  410. else
  411. logThis "bad UCI entry: $2"
  412. _ERR=1
  413. showMesg 'bad UCI entry'
  414. fi ;;
  415. esac
  416. if [[ $_CMD == 'get' ]]; then
  417. if [ ! -z $_ARG ]; then
  418. /sbin/uci -q get $_ARG || return $?
  419. fi
  420. fi
  421. if [[ $_CMD == 'set' ]]; then
  422. local _VAL=$3
  423. if [ -z $_VAL ]; then
  424. logThis "empty $_ARG value, removing record"
  425. /sbin/uci delete $_ARG || ( echo "uci delete $_ARG: error"; exit 1; )
  426. fi
  427. if [ ! -z $_ARG ]; then
  428. logThis "setting $_ARG value"
  429. /sbin/uci set $_ARG=$_VAL || ( echo "uci set $_ARG: error"; exit 1; )
  430. fi
  431. fi
  432. if [[ $_CMD == 'commit' ]]; then
  433. /sbin/uci commit $_ARG || echo "uci commit $_ARG: error"
  434. fi
  435. }
  436. ## call with argument to inject additional lines
  437. ## ie: htmlhead "<meta http-equiv='refresh' content='2;URL=http://${HTTP_REFERER}'>"
  438. htmlHead() {
  439. _echo "<!doctype html>
  440. <html>
  441. <head>
  442. <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  443. <meta http-equiv='Cache-Control' content='no-cache, no-store, must-revalidate' />
  444. <meta http-equiv='Pragma' content='no-cache' />
  445. <meta http-equiv='Expires' content='0' />
  446. <link rel='icon' href='http://${HTTP_HOST}/resources/img/favicon.ico' type='image/x-icon' />
  447. <title>Superglue | Control panel</title>
  448. <link rel='stylesheet' type='text/css' href='http://${HTTP_HOST}/resources/admin/admin.css' />
  449. $@
  450. </head>"
  451. }
  452. footerBody() {
  453. _echo "</body>
  454. <script type='text/javascript' src='http://${HTTP_HOST}/resources/admin/admin.js'></script>
  455. </html>"
  456. }
  457. if [[ "${REQUEST_METHOD^^}" == "POST" ]]; then
  458. [[ $CONTENT_LENGTH -gt 0 ]] || err 'content length is zero, 301 back to referer' '301'
  459. case "${CONTENT_TYPE^^}" in
  460. APPLICATION/X-WWW-FORM-URLENCODED*) setQueryVars;;
  461. MULTIPART/FORM-DATA*) getQueryFile;;
  462. *) _ERR=1; _OUT='this is not a post';;
  463. esac
  464. case $REQUEST_URI in
  465. *pwdchange) pwdChange;;
  466. *ssidchange) ssidChange;;
  467. *lanaddr) lanAddr;;
  468. *updatefw) updateFw;;
  469. *usbinit) usbInit;;
  470. *rebootnow) rebootNow;;
  471. *wan) wanSet;;
  472. *uptime) upTime;;
  473. *iwscan) iwScan;;
  474. *sgddns) sgDdns;;
  475. *sgvpn) sgOpenvpn;;
  476. *) logThis 'bad action'; headerPrint 405;
  477. echo 'no such thing'; exit 1;;
  478. esac
  479. fi
  480. ## all other requests redirect to /admin/
  481. if [[ "${REQUEST_URI/\/admin\//}" ]]; then
  482. headerPrint '301' '/admin/'
  483. fi
  484. headerPrint '200'
  485. htmlHead
  486. read sgver < /etc/superglue_version
  487. read devmod < /etc/superglue_model
  488. read openwrt < /etc/openwrt_version
  489. . /opt/lib/scripts/jshn-helper.sh
  490. ## this does not work when iface isn't configured
  491. IFS=','
  492. wan=( $(ifaceStat wan) )
  493. IFS=$_IFS
  494. eth='eth0'
  495. wlan='wlan1'
  496. if [[ ${devmod} =~ 'DIR-505' ]]; then
  497. eth='eth1'
  498. wlan='wlan0'
  499. fi
  500. sgvpn=$(doUci get sgopenvpnenable)
  501. ddomain=$(doUci get sgddnsdomain)
  502. dsubdomain=$(doUci get sgddnssubdomain)
  503. wanifname=${wan[3]}
  504. wanproto=$(doUci get wanproto)
  505. wanipaddr=${wan[0]}
  506. wangw=${wan[2]}
  507. wandns=${wan[5]}
  508. wanuptime=${wan[4]}
  509. wanssid=$(doUci get wanssid)
  510. wankey=$(doUci get wankey)
  511. %>
  512. <body>
  513. <h1>Superglue control panel</h1>
  514. <img src='http://<% _echo "${HTTP_HOST}" %>/resources/img/superglueLogo.png' class='logo'>
  515. <section class='inert'>
  516. <span style='display:block;'><% printf "System version: %s | Device: %s | OpenWRT: %s" "$sgver" "$devmod" "$openwrt" %></span>
  517. <span style='display:block;' id='uptime'><% uptime %></span>
  518. </section>
  519. <section>
  520. <h2>Internet connection</h2>
  521. <form method='post' action='/admin/wan' name='wan' class='elem' id='wanconf'>
  522. <div style='display:inline-flex'>
  523. <div style='display:inline-block;'>
  524. <select name='wanifname' id='wanifname' style='display:block'>
  525. <option value='<% _echo $eth %>' id='eth' <% ( [[ $wanifname =~ ('eth') ]] && _echo 'selected' ) %>>Wired (WAN port)</option>
  526. <option value='<% _echo $wlan %>' id='wlan' <% ( [[ $wanifname =~ ('wlan') ]] && _echo 'selected' ) %>>Wireless (WiFi)</option>
  527. </select>
  528. <fieldset id='wanwifi' <% ( [[ $wanifname =~ ('wlan') ]] && _echo "class='show elem'" || _echo "class='hide elem'" ) %>>
  529. <select name='wanssid' id='wanssid' class='elem' style='display:block'>
  530. <% if [[ -z $wanssid ]]; then
  531. _echo "<option id='scan' disabled>select a network..</option>"
  532. else
  533. _echo "<option id=$wanssid selected>$wanssid</option>"
  534. fi %>
  535. </select>
  536. <input type='hidden' id='wanenc' name='wanenc' class='elem' value='none'>
  537. <input type='password' name='wankey' placeholder='wpa passphrase' value='<% _echo $wankey %>'>
  538. </fieldset>
  539. <span class='help'>help</span>
  540. </div>
  541. <div style='display:inline-block;'>
  542. <select name='wanproto' id='wanproto' style='display:block'>
  543. <option value='dhcp' name='dhcp' id='dhcp' <% ([[ "$wanproto" == 'dhcp' ]] && _echo 'selected') %>>Automatic (DHCP)</option>
  544. <option value='static' name='stat' id='stat' <% ([[ "$wanproto" == 'static' ]] && _echo 'selected') %>>Manual (Static IP)</option>
  545. </select>
  546. <fieldset id='wanaddr' class='elem'>
  547. <input type='text' name='wanipaddr' id='wanipaddr' value='<% _echo $wanipaddr %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='ip address'>
  548. <input type='text' name='wangw' id='wangw' value='<% _echo $wangw %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='gateway/router'>
  549. <input type='text' name='wandns' id='wandns' value='<% _echo $wandns %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='dns server'>
  550. </fieldset>
  551. </div>
  552. </div>
  553. <div>
  554. <input type='hidden' name='iface' value='wan' class='inline'>
  555. <input type='submit' id='wansubmit' value='Apply' class='inline'>
  556. </form>
  557. <form method='post' action='/admin/sgvpn' class='inline'>
  558. <input type='hidden' name='sgvpn' value='toggle' class='inline'>
  559. <input type='submit' value='<% [[ "$sgvpn" == '1' ]] && _echo 'Disable Superglue VPN service' || _echo 'Enable Superglue VPN service' %>' class='inline'>
  560. </form>
  561. </div>
  562. <span class='help'>help</span>
  563. </section>
  564. <section>
  565. <h2>Domain name</h2>
  566. <form method='post' action='/admin/sgddns' name='sgddns' id='sgddns'>
  567. <div style='display:inline-flex'>
  568. <div style='display:inline-block;'>
  569. <input type='text' name='sgddnssubdomain' placeholder='domain name' value='<% _echo $dsubdomain %>' class='inline'>
  570. </div>
  571. <div style='display:inline-block;'>
  572. <select name='sgddnsdomain' class='inline'>
  573. <option value='spg.lu' <% [[ "$ddomain" == 'spg.lu' ]] && _echo 'selected' %>>.spg.lu</option>
  574. <option value='spgl.it' <% [[ "$ddomain" == 'spgl.it' ]] && _echo 'selected' %>>.spgl.it</option>
  575. <option value='spgl.cc' <% [[ "$ddomain" == 'spgl.cc' ]] && _echo 'selected' %>>.spgl.cc</option>
  576. <option value='superglue.it' <% [[ "$ddomain" == 'superglue.it' ]] && _echo 'selected' %>>.superglue.it</option>
  577. </select>
  578. </div>
  579. </div>
  580. <input type='submit' name='submit' value="Apply">
  581. <input type='hidden' name='sgddnsupdateurl' value='https://superglue.it/ddns/update'>
  582. </form>
  583. </section>
  584. <section>
  585. <h2>Local wireless network</h2>
  586. <form method='post' action='/admin/ssidchange'>
  587. <div style='display:inline-flex'>
  588. <div style='display:inline-block;'>
  589. <input type='text' name='lanssid' value='<% doUci get lanssid %>'>
  590. <input type='password' name='lankey' value='<% doUci get lankey %>' placeholder='passphrase'>
  591. </div>
  592. <div style='display:inline-block;'>
  593. <input type='text' name='lanipaddr' value='<% doUci get lanipaddr %>'>
  594. <input type='hidden' name='iface' value='lan'>
  595. </div>
  596. </div>
  597. <input type='submit' value='Apply' data-wait='Configuring..'>
  598. </form>
  599. <span class='help'>help</span>
  600. </section>
  601. <section>
  602. <h2>USB storage</h2>
  603. <% if findUsbstor; then %>
  604. <% if storageInfo; then %>
  605. <div>File storage: <% _echo "${_STOR[2]} used, ${_STOR[3]} available" %></div>
  606. <div>Swap: <% swapInfo && _echo "${_SWAP[1]}" || _echo '<b>n/a</b>' %></div>
  607. <% else %>
  608. <div>USB storage device must be initialized</div>
  609. <form method='post' action='/admin/usbinit'>
  610. <input type='hidden' name='dev' value='<% _echo $_USBDEV %>'>
  611. <input type='submit' value='Initialize'>
  612. </form>
  613. <% fi %>
  614. <% else %>
  615. <div><h3>USB storage device not found!</h3>Please check and try again</div>
  616. <% fi %>
  617. <span class='help'>help</span>
  618. </section>
  619. <section>
  620. <h2>Change password</h2>
  621. <form method='post' action='/admin/pwdchange'>
  622. <div style='display:inline-flex'>
  623. <div style='display:inline-block;'>
  624. <input type='text' name='usr' value='admin' readonly>
  625. </div>
  626. <div style='display:inline-block;'>
  627. <input type='password' name='pwd' placeholder='password' value=''>
  628. <input type='password' name='pwdd' placeholder='password again' value=''>
  629. </div>
  630. </div>
  631. <input type='submit' value='Apply'>
  632. </form>
  633. <span class='help'>help</span>
  634. </section>
  635. <section>
  636. <h2>Update firmware</h2>
  637. <form method='post' action='/admin/updatefw' enctype='multipart/form-data'>
  638. <div id='uploadbox'>
  639. <input id='uploadfile' placeholder='Select a file..' class='elem' disabled='disabled'>
  640. <input id='uploadbtn' class='elem' name='fwupload' type='file'>
  641. </div>
  642. <div style='display:inline-block;'>
  643. <input type='checkbox' name='fwreset' id='fw-resetbox' />
  644. <label for='fw-resetbox'>Reset all settings</label>
  645. </div>
  646. <input type='submit' value='Upload' data-wait='Uploading, do NOT interrupt!'>
  647. </form>
  648. <span class='help'>help</span>
  649. </section>
  650. <section>
  651. <h2></h2>
  652. <form action='/admin/rebootnow' method='post' class='inline'>
  653. <input type='hidden' name='reboot' value='now' class='inline'>
  654. <input type='submit' value='Reboot' class='inline'>
  655. </form>
  656. <form method='post' action='http://logout@<% _echo ${HTTP_HOST} %>/admin' class='inline'>
  657. <input type='submit' value='Logout' class='inline'>
  658. </form>
  659. </section>
  660. <div style='height:200px' name='big-spacer'></div>
  661. <section>
  662. <h2>Under the hood</h2>
  663. <h4>Memory</h4>
  664. <pre><% free %></pre>
  665. <hr>
  666. <h4>Storage</h4>
  667. <pre><% df -h %></pre>
  668. <hr>
  669. <h4>Recent log entries</h4>
  670. <pre><% tail -n10 /www/log/*.log %></pre>
  671. <hr>
  672. <h4>Environment</h4>
  673. <pre><% env %></pre>
  674. <hr>
  675. </section>
  676. <%
  677. footerBody
  678. exit 0
  679. %>