admin2.cgi 23 KB

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