admin2.cgi 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. doUci() {
  267. local _CMD=''
  268. local _ARG=''
  269. case $1 in
  270. get|set|commit) _CMD=$1;;
  271. *) logThis 'bad UCI command'; headerPrint 405; echo 'bad UCI command'; exit 1 ;;
  272. esac
  273. case $2 in
  274. lanssid) _ARG='wireless.@wifi-iface[0].ssid';;
  275. lanenc) _ARG='wireless.@wifi-iface[0].encryption';;
  276. lankey) _ARG='wireless.@wifi-iface[0].key';;
  277. lanipaddr) _ARG='network.lan.ipaddr';;
  278. wanifname) _ARG='network.wan.ifname';;
  279. wanproto) _ARG='network.wan.proto';;
  280. wanipaddr) _ARG='network.wan.ipaddr';;
  281. wannetmask) _ARG='network.wan.netmask';;
  282. wanwifacedis) _ARG='wireless.@wifi-iface[1].disabled';;
  283. wanssid) _ARG='wireless.@wifi-iface[1].ssid';;
  284. wanenc) _ARG='wireless.@wifi-iface[1].encryption';;
  285. wankey) _ARG='wireless.@wifi-iface[1].key';;
  286. *) if [[ $_CMD == 'commit' ]]; then
  287. _ARG=$2
  288. else
  289. logThis "bad UCI entry: $2"
  290. _ERR=1
  291. showMesg 'bad UCI entry'
  292. fi ;;
  293. esac
  294. if [[ $_CMD == 'get' ]]; then
  295. if [ ! -z $_ARG ]; then
  296. /sbin/uci -q get $_ARG || return $?
  297. fi
  298. fi
  299. if [[ $_CMD == 'set' ]]; then
  300. local _VAL=$3
  301. if [ -z $_VAL ]; then
  302. logThis "empty $_ARG value, removing record"
  303. runSuid /sbin/uci delete $_ARG || ( echo "uci delete $_ARG: error"; exit 1; )
  304. fi
  305. if [ ! -z $_ARG ]; then
  306. logThis "setting $_ARG value"
  307. runSuid /sbin/uci set $_ARG=$_VAL || ( echo "uci set $_ARG: error"; exit 1; )
  308. fi
  309. fi
  310. if [[ $_CMD == 'commit' ]]; then
  311. runSuid /sbin/uci commit $_ARG|| echo "uci commit $_ARG: error"
  312. if [[ "$_ARG" == 'wireless' ]]; then
  313. runSuid /sbin/wifi || echo 'wifi: error'
  314. fi
  315. if [[ "$_ARG" == 'network' ]]; then
  316. runSuid /etc/init.d/dnsmasq restart && runSuid /etc/init.d/network restart || echo 'network: error'
  317. fi
  318. fi
  319. }
  320. getStat() {
  321. . /usr/share/libubox/jshn.sh
  322. local _IFACE=$1
  323. local _IFSTAT=$(runSuid ubus call network.interface.wan status 2>/dev/null)
  324. logThis "$_IFSTAT"
  325. json_get_type _IFSTAT ipv4_address
  326. if json_get_type _IFSTAT ipv4_address && [[ "$_IFSTAT" == 'array' ]]; then
  327. json_select ipv4_address
  328. json_get_type _IFSTAT 1
  329. if [[ "$_IFSTAT" == 'object' ]]; then
  330. json_select 1
  331. json_get_var IP4 address
  332. json_get_var Subnet4 mask
  333. [[ "$IP4" != '' ]] && [[ "$Subnet4" != '' ]] && IP4="$IP4/$Subnet4"
  334. fi
  335. fi
  336. logThis $IP4
  337. }
  338. ## call with argument to inject additional lines
  339. ## ie: htmlhead "<meta http-equiv='refresh' content='2;URL=http://${HTTP_REFERER}'>"
  340. htmlHead() {
  341. _echo "<!-- obnoxious code below, keep your ports tight -->
  342. <!doctype html>
  343. <html>
  344. <head>
  345. <link rel='icon' href='http://${HTTP_HOST}/resources/img/favicon.ico' type='image/x-icon'>
  346. <title>Superglue server | Control panel</title>
  347. <link rel='stylesheet' type='text/css' href='http://${HTTP_HOST}/resources/admin/admin.css'>
  348. $@
  349. </head>"
  350. }
  351. footerBody() {
  352. _echo "</body>
  353. <script type='text/javascript' src='http://${HTTP_HOST}/resources/admin/admin.js'></script>
  354. </html>"
  355. }
  356. if [[ "${REQUEST_METHOD^^}" == "POST" ]]; then
  357. [[ $CONTENT_LENGTH -gt 0 ]] || err 'content length is zero, 301 back to referer' '301'
  358. case "${CONTENT_TYPE^^}" in
  359. APPLICATION/X-WWW-FORM-URLENCODED*) setQueryVars;;
  360. MULTIPART/FORM-DATA*) getQueryFile;;
  361. *) _ERR=1; _OUT='this is not a post';;
  362. esac
  363. case $REQUEST_URI in
  364. *pwdchange) pwdChange;;
  365. *ssidchange) ssidChange;;
  366. *lanaddr) lanAddr;;
  367. *updatefw) updateFw;;
  368. *rebootnow) rebootNow;;
  369. *wan) wanSet;;
  370. *) logThis 'bad action'; headerPrint 405;
  371. echo 'no such thing'; exit 1;;
  372. esac
  373. fi
  374. headerPrint '200'
  375. ## html head
  376. htmlHead
  377. sgver=$(cat /etc/superglue_version)
  378. devmod=$(cat /etc/superglue_model)
  379. openwrt=$(cat /etc/openwrt_version)
  380. wanifname=$(doUci get wanifname || echo 'wlan0') ## TODO fix this
  381. wanproto=$(doUci get wanproto)
  382. wanipaddr=$(doUci get wanipaddr)
  383. wannetmask=$(doUci get wannetmask)
  384. wanssid=$(doUci get wanssid)
  385. wankey=$(doUci get wankey)
  386. ipaddr="$(ifconfig $wanifname | sed -n '/dr:/{;s/.*dr://;s/ .*//;p;}')"
  387. %>
  388. <body>
  389. <h1>Superglue server control panel</h1>
  390. <img src='http://<% _echo "${HTTP_HOST}" %>/resources/img/superglueLogo.png' class='logo'>
  391. <section class='inert'>
  392. <span style='display:block;'><% printf "System version: %s | Device: %s | OpenWRT: %s" "$sgver" "$devmod" "$openwrt" %></span>
  393. <span style='display:block;'><% uptime %></span>
  394. </section>
  395. <section>
  396. <h2>Internet connection: <% _echo $ipaddr %></h3>
  397. <form method='post' action='/admin/wan' name='wan' id='wanconf'> <!-- onchange='formChange();' -->
  398. <div style='display:inline-flex'>
  399. <div style='display:inline-block;'>
  400. <select name='wanifname' id='wanifname' style='display:block'>
  401. <option value='eth0' id='eth' <% ( [[ $wanifname =~ ('eth') ]] && _echo 'selected' ) %> >Wired (WAN port)</option>
  402. <option value='wlan1' id='wlan' <% ( [[ $wanifname =~ ('wlan') ]] && _echo 'selected' ) %> >Wireless (Wi-Fi)</option>
  403. </select>
  404. <fieldset id='wanwifi' class='hide'>
  405. <input type='text' name='wanssid' value='<% _echo $wanssid %>'>
  406. <input type='password' name='wankey' value='<% _echo $wankey %>'>
  407. </fieldset>
  408. <span class='help'>help</span>
  409. </div>
  410. <div style='display:inline-block;'>
  411. <select name='wanproto' id='wanproto' style='display:block'>
  412. <option value='dhcp' name='dhcp' id='dhcp' <% ([[ $wanproto == 'dhcp' ]] && _echo 'selected') %>>Automatic (DHCP)</option>
  413. <option value='stat' name='dhcp' id='stat' <% ([[ $wanproto == 'static' ]] && _echo 'selected') %>>Manual (Static IP)</option>
  414. </select>
  415. <fieldset id='wanaddr' class='hide' >
  416. <input type='text' name='wanipaddr' id='wanipaddr' value='<% _echo $wanipaddr %>'>
  417. <input type='text' name='wangw' id='wannetmask' value='<% _echo $wannetmask %>'>
  418. </fieldset>
  419. </div>
  420. </div>
  421. <input type='hidden' name='iface' value='wan' class='inline'>
  422. <input type='submit' value='Apply'>
  423. </form>
  424. <span class='help'>help</span>
  425. </section>
  426. <section>
  427. <h2>Local wireless network:</h2>
  428. <form method='post' action='/admin/ssidchange'>
  429. <div style='display:inline-flex'>
  430. <div style='display:inline-block;'>
  431. <input type='text' name='lanssid' value='<% doUci get lanssid %>'>
  432. <input type='password' name='lankey' value='<% doUci get lankey %>'>
  433. </div>
  434. <div style='display:inline-block;'>
  435. <input type='text' name='lanipaddr' value='<% doUci get lanipaddr %>'>
  436. <input type='hidden' name='iface' value='lan' class='inline'>
  437. </div>
  438. </div>
  439. <input type='submit' value='Apply'>
  440. </form>
  441. <span class='help'>help</span>
  442. </section>
  443. <section>
  444. <h2>Change password:</h2>
  445. <form method='post' action='/admin/pwdchange'>
  446. <div style='display:inline-flex'>
  447. <div style='display:inline-block;'>
  448. <input type='text' name='usr' value='admin' readonly>
  449. </div>
  450. <div style='display:inline-block;'>
  451. <input type='password' name='pwd' value=''>
  452. <input type='password' name='pwdd' value=''>
  453. </div>
  454. </div>
  455. <input type='submit' value='Apply'>
  456. </form>
  457. <span class='help'>help</span>
  458. </section>
  459. <section>
  460. <h2>Update firmware:</h2>
  461. <form method='post' action='/admin/updatefw' enctype='multipart/form-data'>
  462. <div id='uploadbox'>
  463. <input id='uploadfile' placeholder='Choose file' disabled='disabled'>
  464. <input id='uploadbtn' name='fwupload' type='file'>
  465. </div>
  466. <input type='submit' value='Upload'>
  467. </form>
  468. <span class='help'>help</span>
  469. </section>
  470. <section>
  471. <h2></h2>
  472. <form action='/admin/rebootnow' method='post' class='inline'>
  473. <input type='hidden' name='reboot' value='now' class='inline'>
  474. <input type='submit' value='Reboot' class='inline'>
  475. </form>
  476. <form action='http://logout@<% _echo ${HTTP_HOST} %>/admin' method='get' class='inline'>
  477. <input type='submit' value='Logout' class='inline'>
  478. </form>
  479. </section>
  480. <div style='height:200px'></div>
  481. <hr>
  482. Memory:
  483. <pre><% free %></pre>
  484. <hr>
  485. Storage:
  486. <pre><% df -h %></pre>
  487. <hr>
  488. Environment:
  489. <pre><% env %></pre>
  490. <hr>
  491. <%
  492. footerBody
  493. exit 0
  494. %>