admin2.cgi 23 KB

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