admin2.cgi 23 KB

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