admin2.cgi 22 KB

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