admin2.cgi 23 KB

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