admin2.cgi 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #!/usr/bin/haserl --shell=/bin/bash --upload-limit=32768 --upload-dir=/www/tmp
  2. <%# upload limit: 32Mb %>
  3. <%
  4. _WWW='/www'
  5. _PWDFILE="/opt/lib/htpasswd"
  6. _TMP="${_WWW}/tmp"
  7. _LOG="${_WWW}/log/admin.log"
  8. _DEBUG=1
  9. err() {
  10. _ERR="$?"
  11. [[ "$_ERR" -gt 0 ]] || return 0
  12. log "$1"
  13. head "${2:='400'}"
  14. exit "$_ERR"
  15. }
  16. logThis() {
  17. [[ "$_DEBUG" -gt 0 ]] || return 0
  18. local _TYPE='I:'
  19. [[ "$_ERR" -gt 0 ]] && _TYPE='E:'
  20. local _TIME; printf -v _TIME '%(%d.%m.%Y %H:%M:%S)T' -1
  21. printf '%b\n' "$_TIME $_TYPE ${@} " >> "$_LOG"
  22. [[ "$_DEBUG" -gt 1 ]] && printf '%b\n' "[verbose] $_TYPE ${1}"
  23. }
  24. headerPrint() {
  25. case "$1" in
  26. 200|'') printf '%b' 'HTTP/1.1 200 OK\r\n';;
  27. 301) printf '%b' "HTTP/1.1 301 Moved Permanently\r\nLocation: $HTTP_REFERER\r\n";;
  28. 403) printf '%b' 'HTTP/1.1 403 Forbidden\r\n';;
  29. 405) printf '%b' 'HTTP/1.1 405 Method Not Allowed\r\n';;
  30. 406) printf '%b' 'HTTP/1.1 406 Not Acceptable\r\n';;
  31. *) printf '%b' 'HTTP/1.1 400 Bad Request\r\n';;
  32. esac
  33. printf '%b' 'Content-Type: text/html\r\n\r\n';
  34. }
  35. ## faster echo
  36. _echo() {
  37. printf "%s" "${*}"
  38. }
  39. htDigest() {
  40. _USER='admin'
  41. _PWD=$1
  42. _REALM='superglue'
  43. _HASH=$(echo -n "$_USER:$_REALM:$_PWD" | md5sum | cut -b -32)
  44. printf "%s" "$_USER:$_REALM:$_HASH"
  45. }
  46. urlDec() {
  47. local value=${*//+/%20}
  48. for part in ${value//%/ \\x}; do
  49. printf "%b%s" "${part:0:4}" "${part:4}"
  50. done
  51. }
  52. setQueryVars() {
  53. _VARS=( ${!POST_*} )
  54. # local v
  55. # for v in ${_VARS[@]}; do
  56. # echo $v
  57. # v=$(urlDec "${v}")
  58. # eval "_${v//POST_/}=${!v}";
  59. # done
  60. local v
  61. for v in ${_VARS[@]}; do
  62. logThis "$v=${!v}"
  63. done
  64. #echo $POST_lanssid
  65. #env
  66. }
  67. runSuid() {
  68. local _SID=$(/usr/bin/ps -p $$ -o sid=) ## pass session id to the child
  69. local _CMD=$@
  70. sudo ./suid.sh $_CMD $_SID 2>/dev/null
  71. }
  72. getQueryFile() {
  73. local _UPLD="${HASERL_fwupload_path##*/}"
  74. logThis "'multipart': decoding stream"
  75. mv "$_TMP/$_UPLD" "$_TMP/fwupload.bin" 2>/dev/null || _ERR=$?
  76. if [[ $_ERR -gt 0 ]]; then
  77. showMesg 'Firmware upload has failed' 'Reboot your Superglue server and try again'
  78. fi
  79. }
  80. validIp() {
  81. local _IP=$1
  82. local _RET=1
  83. if [[ $_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  84. OIFS=$IFS
  85. IFS='.'
  86. _IP=($_IP)
  87. IFS=$OIFS
  88. [[ ${_IP[0]} -le 255 && ${_IP[1]} -le 255 && ${_IP[2]} -le 255 && ${_IP[3]} -le 255 ]]
  89. _RET=$?
  90. fi
  91. return $_RET
  92. }
  93. pwdChange() {
  94. if [[ ! -z "${POST_pwd##$POST_pwdd}" ]]; then
  95. _ERR=1
  96. showMesg 'Passwords did not match'
  97. fi
  98. if [[ ${#POST_pwd} -lt 6 ]]; then
  99. _ERR=1
  100. showMesg 'Password must be at least 6 characters long'
  101. fi
  102. runSuid "echo -e \"$POST_pwd\n$POST_pwd\" | passwd root"
  103. runSuid "echo $(htDigest $POST_pwd) > $_PWDFILE"
  104. _ERR=$?
  105. if [[ $_ERR -gt 0 ]]; then
  106. showMesg 'Password change failed'
  107. else
  108. showMesg 'Password is changed'
  109. fi
  110. }
  111. lanAddr() {
  112. logThis "new LAN addr is: $POST_laddr"
  113. validIp $POST_laddr || showMesg 'Not valid network address'
  114. doUci set laddr $POST_laddr
  115. _ERR=$?
  116. if [[ $_ERR -gt 0 ]]; then
  117. showMesg 'Setting network address failed'
  118. else
  119. (sleep 1; doUci commit network; doUci commit wireless;)&
  120. showMesg 'New network address is set' "Your server is now accessible under <a href='http://superglue.local/admin'>http://superglue.local/admin</a>"
  121. fi
  122. }
  123. wanSet() {
  124. if [[ ! -z $POST_wanifname ]]; then
  125. ## eth and wlan wan cases are different!
  126. ## eth wan requires:
  127. ## config interface 'wan'
  128. ## option ifname 'eth0'
  129. ##
  130. ## config wifi-iface
  131. ## option device 'radio0'
  132. ## option network 'wan'
  133. ## option disabled '1' (or no 'config wifi-iface' section at all)
  134. ##
  135. ## wlan wan requires:
  136. ## config interface 'wan'
  137. ## option proto 'dhcp'
  138. ## (without 'option ifname' specified!)
  139. ##
  140. ## config wifi-iface
  141. ## option device 'radio0'
  142. ## option network 'wan'
  143. logThis "wan.ifname=$POST_wanifname"
  144. if [[ $POST_wanifname == 'eth0' ]]; then
  145. doUci set wanifname $POST_wanifname
  146. doUci set wanwifacedis '1'
  147. elif [[ $POST_wanifname == 'wlan1' ]]; then
  148. doUci set wanifname ''
  149. doUci set wanwifacedis ''
  150. fi
  151. if [[ $POST_wanproto == 'dhcp' ]]; then
  152. doUci set wanproto dhcp
  153. elif [[ $POST_wanproto == 'static' ]]; then
  154. logThis "wan.ipaddr=$POST_wanipaddr"
  155. doUci set wanproto static
  156. doUci set wanipaddr $POST_wanipaddr
  157. doUci set wannetmask $POST_wannetmask
  158. fi
  159. if [[ $POST_wanifname == 'wlan1' ]]; then
  160. ssidChange || showMesg 'Wireless changes failed'
  161. fi
  162. ## background the following
  163. doUci commit network &&
  164. showMesg 'Internet connection is configured' 'Waiting for device to get ready' ||
  165. showMesg 'Configuring Internet connection failed'
  166. fi
  167. logThis "new WAN iface is: $POST_wanifname"
  168. }
  169. ssidChange() {
  170. ## check for iface
  171. [[ ! $POST_iface =~ ^('wan'|'lan')$ ]] && showMesg 'Error changing wireless settings' 'unknown/unconfigured interface'
  172. logThis "$POST_iface is being set"
  173. _p=$POST_iface
  174. ## default enc for now
  175. local _enc='psk2'
  176. if [[ $POST_iface == 'wan' ]]; then
  177. local _mode='sta'
  178. local _ssid="${POST_wanssid}"
  179. local _key="${POST_wankey}"
  180. else
  181. local _mode='ap'
  182. local _ssid="${POST_lanssid}"
  183. local _key="${POST_lankey}"
  184. fi
  185. logThis "ssid: $_ssid [$_mode], key: $_key [$_enc]"
  186. logThis $POST_wanssid
  187. if [[ ${#_ssid} -lt 4 ]]; then
  188. _ERR=1
  189. showMesg 'SSID must be at least 4 characters long'
  190. fi
  191. doUci set $_p'ssid' "${_ssid}"
  192. _ERR=$?
  193. [[ $_ERR -gt 0 ]] && showMesg 'New SSID is not set'
  194. if [[ -z $_key ]]; then
  195. ## if key is empty set encryption to none and remove key
  196. doUci set $_p'key' && doUci set $_p'enc' 'none'
  197. _ERR=$?
  198. else
  199. if [[ ${#_key} -lt 8 ]]; then
  200. _ERR=1
  201. showMesg 'Passphrase must be at least 8 characters long'
  202. fi
  203. doUci set $_p'key' "${_key}" && doUci set $_p'enc' "${_enc}"
  204. _ERR=$?
  205. [[ $_ERR -gt 0 ]] && showMesg 'Passphrase is not set'
  206. fi
  207. [[ $_ERR -gt 0 ]] && return $_ERR ##showMesg 'Wireless changes failed'
  208. doUci commit wireless ##&& showMesg 'Wireless changes applied'
  209. }
  210. #showError() {
  211. # headerPrint 406
  212. # logThis "$@"
  213. # echo "ERROR: $@"
  214. # exit 1
  215. #}
  216. showMesg() {
  217. logThis "$@"
  218. local _MSG=$1
  219. local _SUBMSG=$2
  220. _MSG=${_MSG:='Not defined'}
  221. _SUBMSG=${_SUBMSG:='back to control panel in a second..'}
  222. if [[ $_ERR -gt 0 ]]; then
  223. local _TYPE='ERROR: '
  224. headerPrint 406
  225. else
  226. local _TYPE='OK: '
  227. headerPrint 200
  228. fi
  229. htmlHead "<meta http-equiv='refresh' content='3;url=${HTTP_REFERER}'>"
  230. _echo "<body>
  231. <h1>Superglue server control panel</h1>
  232. <img src='http://"${HTTP_HOST}"/resources/img/superglueLogo.png' class='logo'>"
  233. _echo "<hr>
  234. <h2 style='display:inline'>$_TYPE $_MSG</h2>
  235. <span style='display:inline; margin-left: 50px;'>$_SUBMSG</span>
  236. <hr>"
  237. footerBody
  238. exit 0
  239. # _echo "<body>
  240. #<h1>SG</h1>
  241. #<hr>
  242. #<h2 style='display:inline'>$_TYPE $_MSG</h2>
  243. #<span style='display:inline; margin-left: 50px;'>$_SUBMSG</span>
  244. #<hr>
  245. #</body></html>"
  246. # exit 0
  247. }
  248. updateFw() {
  249. logThis "updating fw"
  250. _FWFILE="${_TMP}/fwupload.bin"
  251. logThis "fwfile is: $(ls -lad $_FWFILE)"
  252. _OUT="$(/sbin/sysupgrade -T $_FWFILE 2>&1)"
  253. _ERR=$?
  254. [[ $_ERR -gt 0 ]] && showMesg "$_OUT"
  255. # _OUT="$(runSuid /sbin/mtd -e firmware -q write $_FWFILE firmware)"
  256. _ERR=$?
  257. [[ $_ERR -gt 0 ]] && showMesg "mtd failed, $_OUT"
  258. # runSuid reboot
  259. showMesg 'Firmware update is completed, rebooting..' 'this might take up to 60 seconds'
  260. }
  261. rebootNow() {
  262. logThis "reboot: now!"
  263. runSuid reboot
  264. showMesg 'Rebooting..' 'this might take up to 60 seconds'
  265. }
  266. upTime() {
  267. local _T="$(uptime)"
  268. _ERR=$?
  269. if [[ $_ERR -gt 0 ]]; then
  270. headerPrint 406
  271. exit 1
  272. else
  273. headerPrint 200
  274. printf '%b' "$_T\n"
  275. exit 0
  276. fi
  277. }
  278. doUci() {
  279. local _CMD=''
  280. local _ARG=''
  281. case $1 in
  282. get|set|commit) _CMD=$1;;
  283. *) logThis 'bad UCI command'; headerPrint 405; echo 'bad UCI command'; exit 1 ;;
  284. esac
  285. case $2 in
  286. lanssid) _ARG='wireless.@wifi-iface[0].ssid';;
  287. lanenc) _ARG='wireless.@wifi-iface[0].encryption';;
  288. lankey) _ARG='wireless.@wifi-iface[0].key';;
  289. lanipaddr) _ARG='network.lan.ipaddr';;
  290. wanifname) _ARG='network.wan.ifname';;
  291. wanproto) _ARG='network.wan.proto';;
  292. wanipaddr) _ARG='network.wan.ipaddr';;
  293. wannetmask) _ARG='network.wan.netmask';;
  294. wanwifacedis) _ARG='wireless.@wifi-iface[1].disabled';;
  295. wanssid) _ARG='wireless.@wifi-iface[1].ssid';;
  296. wanenc) _ARG='wireless.@wifi-iface[1].encryption';;
  297. wankey) _ARG='wireless.@wifi-iface[1].key';;
  298. *) if [[ $_CMD == 'commit' ]]; then
  299. _ARG=$2
  300. else
  301. logThis "bad UCI entry: $2"
  302. _ERR=1
  303. showMesg 'bad UCI entry'
  304. fi ;;
  305. esac
  306. if [[ $_CMD == 'get' ]]; then
  307. if [ ! -z $_ARG ]; then
  308. /sbin/uci -q get $_ARG || return $?
  309. fi
  310. fi
  311. if [[ $_CMD == 'set' ]]; then
  312. local _VAL=$3
  313. if [ -z $_VAL ]; then
  314. logThis "empty $_ARG value, removing record"
  315. runSuid /sbin/uci delete $_ARG || ( echo "uci delete $_ARG: error"; exit 1; )
  316. fi
  317. if [ ! -z $_ARG ]; then
  318. logThis "setting $_ARG value"
  319. runSuid /sbin/uci set $_ARG=$_VAL || ( echo "uci set $_ARG: error"; exit 1; )
  320. fi
  321. fi
  322. if [[ $_CMD == 'commit' ]]; then
  323. runSuid /sbin/uci commit $_ARG|| echo "uci commit $_ARG: error"
  324. if [[ "$_ARG" == 'wireless' ]]; then
  325. runSuid /sbin/wifi || echo 'wifi: error'
  326. fi
  327. if [[ "$_ARG" == 'network' ]]; then
  328. runSuid /etc/init.d/dnsmasq reload && runSuid /etc/init.d/network reload || echo 'network: error'
  329. fi
  330. fi
  331. }
  332. . /opt/lib/scripts/jshn-helper.sh
  333. ## call with argument to inject additional lines
  334. ## ie: htmlhead "<meta http-equiv='refresh' content='2;URL=http://${HTTP_REFERER}'>"
  335. htmlHead() {
  336. _echo "<!-- obnoxious code below, keep your ports tight -->
  337. <!doctype html>
  338. <html>
  339. <head>
  340. <link rel='icon' href='http://${HTTP_HOST}/resources/img/favicon.ico' type='image/x-icon'>
  341. <title>Superglue server | Control panel</title>
  342. <link rel='stylesheet' type='text/css' href='http://${HTTP_HOST}/resources/admin/admin.css'>
  343. $@
  344. </head>"
  345. }
  346. footerBody() {
  347. _echo "</body>
  348. <script type='text/javascript' src='http://${HTTP_HOST}/resources/admin/admin.js'></script>
  349. </html>"
  350. }
  351. if [[ "${REQUEST_METHOD^^}" == "POST" ]]; then
  352. [[ $CONTENT_LENGTH -gt 0 ]] || err 'content length is zero, 301 back to referer' '301'
  353. case "${CONTENT_TYPE^^}" in
  354. APPLICATION/X-WWW-FORM-URLENCODED*) setQueryVars;;
  355. MULTIPART/FORM-DATA*) getQueryFile;;
  356. *) _ERR=1; _OUT='this is not a post';;
  357. esac
  358. case $REQUEST_URI in
  359. *pwdchange) pwdChange;;
  360. *ssidchange) ssidChange;;
  361. *lanaddr) lanAddr;;
  362. *updatefw) updateFw;;
  363. *rebootnow) rebootNow;;
  364. *wan) wanSet;;
  365. *uptime) upTime;;
  366. *iwscan) iwScan;;
  367. *) logThis 'bad action'; headerPrint 405;
  368. echo 'no such thing'; exit 1;;
  369. esac
  370. fi
  371. headerPrint '200'
  372. ## html head
  373. htmlHead
  374. sgver=$(cat /etc/superglue_version)
  375. devmod=$(cat /etc/superglue_model)
  376. openwrt=$(cat /etc/openwrt_version)
  377. IFS=","
  378. wan=( $(ifaceStat wan) )
  379. IFS=$OFS
  380. #wanifname=$(doUci get wanifname || echo 'wlan0') ## TODO fix this
  381. wanifname=${wan[3]}
  382. wanproto=$(doUci get wanproto)
  383. #wanipaddr=$(doUci get wanipaddr)
  384. wanipaddr=${wan[0]}
  385. #wannetmask=$(doUci get wannetmask)
  386. wangw=${wan[2]}
  387. wandns=${wan[5]}
  388. wanuptime=${wan[4]}
  389. wanssid=$(doUci get wanssid)
  390. wankey=$(doUci get wankey)
  391. %>
  392. <body>
  393. <h1>Superglue server control panel</h1>
  394. <img src='http://<% _echo "${HTTP_HOST}" %>/resources/img/superglueLogo.png' class='logo'>
  395. <section class='inert'>
  396. <span style='display:block;'><% printf "System version: %s | Device: %s | OpenWRT: %s" "$sgver" "$devmod" "$openwrt" %></span>
  397. <span style='display:block;' id='uptime'><% uptime %></span>
  398. </section>
  399. <section>
  400. <h2>Internet connection: <% _echo "IP:$wanipaddr, Gateway:$wangw, DNS:$wandns, up for $wanuptime seconds" %></h3>
  401. <form method='post' action='/admin/wan' name='wan' id='wanconf'>
  402. <div style='display:inline-flex'>
  403. <div style='display:inline-block;'>
  404. <select name='wanifname' id='wanifname' style='display:block'>
  405. <option value='eth0' id='eth' <% ( [[ $wanifname =~ ('eth') ]] && _echo 'selected' ) %> >Wired (WAN port)</option>
  406. <option value='wlan1' id='wlan' <% ( [[ $wanifname =~ ('wlan') ]] && _echo 'selected' ) %> >Wireless (Wi-Fi)</option>
  407. </select>
  408. <fieldset id='wanwifi' <% ( [[ $wanifname =~ ('wlan') ]] && _echo "class='show'" || _echo "class='hide'" ) %>>
  409. <input type='text' name='wanssid' value='<% _echo $wanssid %>'>
  410. <input type='password' name='wankey' value='<% _echo $wankey %>'>
  411. </fieldset>
  412. <span class='help'>help</span>
  413. </div>
  414. <div style='display:inline-block;'>
  415. <select name='wanproto' id='wanproto' style='display:block'>
  416. <option value='dhcp' name='dhcp' id='dhcp' <% ([[ $wanproto == 'dhcp' ]] && _echo 'selected') %>>Automatic (DHCP)</option>
  417. <option value='stat' name='dhcp' id='stat' <% ([[ $wanproto == 'static' ]] && _echo 'selected') %>>Manual (Static IP)</option>
  418. </select>
  419. <fieldset id='wanaddr' <% ( [[ $wanproto =~ ('static') ]] && _echo "class='show'" || _echo "class='hide'" ) %>>
  420. <input type='text' name='wanipaddr' id='wanipaddr' value='<% _echo $wanipaddr %>'>
  421. <input type='text' name='wangw' id='wannetmask' value='<% _echo $wannetmask %>'>
  422. </fieldset>
  423. </div>
  424. </div>
  425. <input type='hidden' name='iface' value='wan' class='inline'>
  426. <input type='submit' value='Apply'>
  427. </form>
  428. <span class='help'>help</span>
  429. </section>
  430. <section>
  431. <h2>Local wireless network:</h2>
  432. <form method='post' action='/admin/ssidchange'>
  433. <div style='display:inline-flex'>
  434. <div style='display:inline-block;'>
  435. <input type='text' name='lanssid' value='<% doUci get lanssid %>'>
  436. <input type='password' name='lankey' value='<% doUci get lankey %>'>
  437. </div>
  438. <div style='display:inline-block;'>
  439. <input type='text' name='lanipaddr' value='<% doUci get lanipaddr %>'>
  440. <input type='hidden' name='iface' value='lan' class='inline'>
  441. </div>
  442. </div>
  443. <input type='submit' value='Apply'>
  444. </form>
  445. <span class='help'>help</span>
  446. </section>
  447. <section>
  448. <h2>Change password:</h2>
  449. <form method='post' action='/admin/pwdchange'>
  450. <div style='display:inline-flex'>
  451. <div style='display:inline-block;'>
  452. <input type='text' name='usr' value='admin' readonly>
  453. </div>
  454. <div style='display:inline-block;'>
  455. <input type='password' name='pwd' value=''>
  456. <input type='password' name='pwdd' value=''>
  457. </div>
  458. </div>
  459. <input type='submit' value='Apply'>
  460. </form>
  461. <span class='help'>help</span>
  462. </section>
  463. <section>
  464. <h2>Update firmware:</h2>
  465. <form method='post' action='/admin/updatefw' enctype='multipart/form-data'>
  466. <div id='uploadbox'>
  467. <input id='uploadfile' placeholder='Choose file' disabled='disabled'>
  468. <input id='uploadbtn' name='fwupload' type='file'>
  469. </div>
  470. <input type='submit' value='Upload'>
  471. </form>
  472. <span class='help'>help</span>
  473. </section>
  474. <section>
  475. <h2></h2>
  476. <form action='/admin/rebootnow' method='post' class='inline'>
  477. <input type='hidden' name='reboot' value='now' class='inline'>
  478. <input type='submit' value='Reboot' class='inline'>
  479. </form>
  480. <form action='http://logout@<% _echo ${HTTP_HOST} %>/admin' method='get' class='inline'>
  481. <input type='submit' value='Logout' class='inline'>
  482. </form>
  483. </section>
  484. <div style='height:200px'></div>
  485. <hr>
  486. Memory:
  487. <pre><% free %></pre>
  488. <hr>
  489. Storage:
  490. <pre><% df -h %></pre>
  491. <hr>
  492. Environment:
  493. <pre><% env %></pre>
  494. <hr>
  495. <%
  496. footerBody
  497. exit 0
  498. %>