admin.cgi-bup201014 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #!/bin/bash
  2. _WWW='/www'
  3. _PWDFILE="${_WWW}/lib/htpasswd"
  4. _TMP="${_WWW}/tmp"
  5. _LOG="${_WWW}/log/admin.log"
  6. _DEBUG=1
  7. ## logging
  8. logThis() {
  9. [[ $_DEBUG -gt 0 ]] || return 0
  10. [[ $_ERR -gt 0 ]] && _TYPE='E:' || _TYPE='I:' ## Info or Error indication
  11. local _TIME=$(printf '%(%d.%m.%Y %H:%M:%S)T' -1)
  12. printf '%b\n' "$_TIME $_TYPE ${1} " >> $_LOG
  13. [[ $_DEBUG -gt 1 ]] && printf '%b\n' "[verbose] $_TYPE ${1}"
  14. return 0
  15. }
  16. ## http response
  17. headerPrint() {
  18. case ${1} in
  19. 200) printf '%b' 'HTTP/1.1 200 OK\nAccess-Control-Allow-Origin: *\n\n';;
  20. 301) printf '%b' "HTTP/1.1 301 Moved Permanently\nLocation: ${HTTP_REFERER}\n\n";;
  21. 403) printf '%b' 'HTTP/1.1 403 Forbidden\n\n';;
  22. 405) printf '%b' 'HTTP/1.1 405 Method Not Allowed\n\n';;
  23. 406) printf '%b' 'HTTP/1.1 406 Not Acceptable\n\n';;
  24. esac
  25. return 0
  26. }
  27. htDigest() {
  28. _USER='admin'
  29. _PWD=$1
  30. _REALM='superglue'
  31. _HASH=$(echo -n "$_USER:$_REALM:$_PWD" | md5sum | cut -b -32)
  32. echo -n "$_USER:$_REALM:$_HASH"
  33. }
  34. urlDec() {
  35. local value=${*//+/%20}
  36. for part in ${value//%/ \\x}; do
  37. printf "%b%s" "${part:0:4}" "${part:4}"
  38. done
  39. }
  40. setQueryVars() {
  41. local _POST=$(cat)
  42. local vars=${_POST//\*/%2A}
  43. for var in ${vars//&/ }; do
  44. local value=$(urlDec "${var#*=}")
  45. value=${value//\\/\\\\}
  46. eval "_${var%=*}=\"${value//\"/\\\"}\""
  47. done
  48. }
  49. getQueryFile() {
  50. _POST_TMP=$(mktemp -p $_TMP) ## make tmp POST file
  51. cat > $_POST_TMP ## cautiously storing entire POST in a file
  52. logThis "'multipart': decoding stream"
  53. local _BND=$(findPostOpt 'boundary')
  54. ## bash is binary unsafe and eats away precious lines
  55. ## thus using gawk
  56. function cutFile() {
  57. gawk -v "want=$1" -v "bnd=$_BND" '
  58. BEGIN { RS="\r\n"; ORS="\r\n" }
  59. # reset based on boundaries
  60. $0 == "--"bnd"" { st=1; next; }
  61. $0 == "--"bnd"--" { st=0; next; }
  62. $0 == "--"bnd"--\r" { st=0; next; }
  63. # search for wanted file
  64. st == 1 && $0 ~ "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
  65. st == 1 && $0 == "" { st=9; next; }
  66. # wait for newline, then start printing
  67. st == 2 && $0 == "" { st=3; next; }
  68. st == 3 { print $0 }
  69. ' 2>&1
  70. }
  71. cutFile 'fwupload' < "${_POST_TMP}" > "${_TMP}/fwupload.bin"
  72. }
  73. ## find arbitrary option supplied in Content-Type header
  74. ## eg: "Content-Type:application/octet-stream; verbose=1"
  75. findPostOpt() {
  76. for i in "${CONTENT_TYPE[@]:1}"; do
  77. case "${i/=*}" in
  78. "$1") printf '%b' "${i/*=}" ;;
  79. esac
  80. done
  81. return 0
  82. }
  83. runSuid() {
  84. local _SID=$(/usr/bin/ps -p $$ -o sid=) ## pass session id to the child
  85. local _CMD=$@
  86. sudo ./suid.sh $_CMD $_SID 2>&1
  87. }
  88. validIp() {
  89. local _IP=$1
  90. local _RET=1
  91. if [[ $_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  92. OIFS=$IFS
  93. IFS='.'
  94. _IP=($_IP)
  95. IFS=$OIFS
  96. [[ ${_IP[0]} -le 255 && ${_IP[1]} -le 255 && ${_IP[2]} -le 255 && ${_IP[3]} -le 255 ]]
  97. _RET=$?
  98. fi
  99. return $_RET
  100. }
  101. pwdChange() {
  102. if [[ ! -z "${_pwd##$_pwdd}" ]]; then
  103. _ERR=1
  104. showMesg 'Passwords did not match'
  105. fi
  106. if [[ ${#_pwd} -lt 6 ]]; then
  107. _ERR=1
  108. showMesg 'Password must be at least 6 characters long'
  109. fi
  110. runSuid "echo -e \"$_pwd\n$_pwd\" | passwd root"
  111. runSuid "echo $(htDigest $_pwd) > $_PWDFILE"
  112. _ERR=$?
  113. if [[ $_ERR -gt 0 ]]; then
  114. showMesg 'Password change failed'
  115. else
  116. showMesg 'Password is changed'
  117. fi
  118. }
  119. lanAddr() {
  120. logThis "new LAN addr is: $_laddr"
  121. validIp $_laddr || showMesg 'Not valid network address'
  122. doUci set laddr $_laddr
  123. _ERR=$?
  124. if [[ $_ERR -gt 0 ]]; then
  125. showMesg 'Setting network address failed'
  126. else
  127. (sleep 1; doUci commit network; doUci commit wireless;)&
  128. showMesg 'New network address is set' "Your server is now accessible under <a href='http://$_laddr/admin'>http://$_laddr/admin</a>"
  129. fi
  130. }
  131. ssidChange() {
  132. ## default enc for now
  133. local _enc='psk2'
  134. logThis "new ssid is: $_ssid"
  135. logThis "new key is: $_key"
  136. if [[ ${#_ssid} -lt 4 ]]; then
  137. _ERR=1
  138. showMesg 'SSID must be at least 4 characters long'
  139. fi
  140. doUci set ssid $_ssid
  141. _ERR=$?
  142. [[ $_ERR -gt 0 ]] && showMesg 'New SSID is not set'
  143. if [[ -z $_key ]]; then
  144. ## if key is empty set encryption to none and remove key
  145. doUci set key && doUci set enc 'none'
  146. _ERR=$?
  147. else
  148. if [[ ${#_key} -lt 8 ]]; then
  149. _ERR=1
  150. showMesg 'Passphrase must be at least 8 characters long'
  151. fi
  152. doUci set key $_key && doUci set enc $_enc
  153. _ERR=$?
  154. [[ $_ERR -gt 0 ]] && showMesg 'Passphrase is not set'
  155. fi
  156. [[ $_ERR -gt 0 ]] && showMesg 'Wireless changes failed'
  157. doUci commit wireless && showMesg 'Wireless changes applied'
  158. }
  159. #showError() {
  160. # headerPrint 406
  161. # logThis "$@"
  162. # echo "ERROR: $@"
  163. # exit 1
  164. #}
  165. showMesg() {
  166. logThis "$@"
  167. local _MSG=$1
  168. local _SUBMSG=$2
  169. _MSG=${_MSG:='Not defined'}
  170. _SUBMSG=${_SUBMSG:='back to control panel in a second..'}
  171. if [[ $_ERR -gt 0 ]]; then
  172. local _TYPE='ERROR: '
  173. headerPrint 406
  174. else
  175. local _TYPE='OK: '
  176. headerPrint 200
  177. fi
  178. htmlHead "<meta http-equiv='refresh' content='5;url=${HTTP_REFERER}'>"
  179. echo "<body>
  180. <img src='/resources/img/superglueLogo.png' class='logo'>
  181. <hr>
  182. <h2 style='display:inline'>$_TYPE $_MSG</h2>
  183. <span style='display:inline; margin-left: 50px;'>$_SUBMSG</span>
  184. <hr>
  185. </body></html>"
  186. exit 0
  187. }
  188. updateFw() {
  189. logThis "updating fw"
  190. _FWFILE="${_TMP}/fwupload.bin"
  191. logThis "fwfile is: $(ls -lad $_FWFILE)"
  192. _OUT="$(/sbin/sysupgrade -T $_FWFILE 2>&1)"
  193. _ERR=$?
  194. [[ $_ERR -gt 0 ]] && showMesg "$_OUT"
  195. _OUT="$(runSuid /sbin/mtd -e firmware -q write $_FWFILE firmware)"
  196. _ERR=$?
  197. [[ $_ERR -gt 0 ]] && showMesg "mtd failed, $_OUT"
  198. runSuid reboot
  199. showMesg 'Firmware update is completed, rebooting..' 'this might take up to 60 seconds'
  200. }
  201. rebootNow() {
  202. logThis "reboot: now!"
  203. runSuid reboot
  204. showMesg 'Rebooting..' 'this might take up to 60 seconds'
  205. }
  206. doUci() {
  207. local _CMD=''
  208. local _ARG=''
  209. case $1 in
  210. get|set|commit) _CMD=$1;;
  211. *) logThis 'bad UCI command'; headerPrint 405; echo 'bad UCI command'; exit 1 ;;
  212. esac
  213. case $2 in
  214. ssid) _ARG='wireless.@wifi-iface[0].ssid';;
  215. enc) _ARG='wireless.@wifi-iface[0].encryption';;
  216. key) _ARG='wireless.@wifi-iface[0].key';;
  217. laddr) _ARG='network.lan.ipaddr';;
  218. *) if [[ $_CMD == 'commit' ]]; then
  219. _ARG=$2
  220. else
  221. logThis 'bad UCI entry'
  222. _ERR=1
  223. showMesg 'bad UCI entry'
  224. fi ;;
  225. esac
  226. if [[ $_CMD == 'get' ]]; then
  227. if [ ! -z $_ARG ]; then
  228. /sbin/uci get $_ARG
  229. fi
  230. fi
  231. if [[ $_CMD == 'set' ]]; then
  232. local _VAL=$3
  233. if [ -z $_VAL ]; then
  234. logThis "empty $_ARG value, removing record"
  235. runSuid /sbin/uci delete $_ARG || ( echo "uci delete $_ARG: error"; exit 1; )
  236. fi
  237. if [ ! -z $_ARG ]; then
  238. logThis "setting $_ARG value"
  239. runSuid /sbin/uci set $_ARG=$_VAL || ( echo "uci set $_ARG: error"; exit 1; )
  240. fi
  241. fi
  242. if [[ $_CMD == 'commit' ]]; then
  243. runSuid /sbin/uci commit $_ARG|| echo "uci commit $_ARG: error"
  244. if [[ "$_ARG" == 'wireless' ]]; then
  245. runSuid /sbin/wifi || echo 'wifi: error'
  246. fi
  247. if [[ "$_ARG" == 'network' ]]; then
  248. runSuid /etc/init.d/dnsmasq restart && runSuid /etc/init.d/network restart || echo 'network: error'
  249. fi
  250. fi
  251. }
  252. htmlHead() {
  253. echo "<!doctype html>
  254. <html>
  255. <head><title>SuperGlue | Administration</title>
  256. $@
  257. <style>
  258. body { background:#ccc; color:#000; margin: 20px 0 0 200px; font-family: TitilliumWeb;}
  259. input { display: block; }
  260. .inline { display: inline; }
  261. img.logo { position: absolute; left:50px; top: 20px;}
  262. pre { white-space: pre-wrap; }
  263. @font-face { font-family: TitilliumWeb; src: url('/resources/fonts/Titillium_Web/TitilliumWeb-Regular.ttf') format('truetype'); }
  264. @font-face { font-family: TitilliumWeb; font-weight: bold; src: url('/resources/fonts/Titillium_Web/TitilliumWeb-Bold.ttf') format('truetype'); }
  265. </style>
  266. </head>"
  267. }
  268. ## unless auth is disabled in lighttpd
  269. ## it should never come to this,
  270. if [[ -z $HTTP_AUTHORIZATION ]]; then
  271. logThis 'no auth'
  272. headerPrint 403
  273. echo 'no is no'
  274. exit 1
  275. else logThis 'auth OK'
  276. fi
  277. if [[ $REQUEST_METHOD == 'POST' ]]; then
  278. if [[ $CONTENT_LENGTH -gt 0 ]]; then
  279. CONTENT_TYPE=( ${CONTENT_TYPE} )
  280. _CONTENT_TYPE="${CONTENT_TYPE[0]/;}"
  281. _ENC="${HTTP_CONTENT_ENCODING}"
  282. case "${_CONTENT_TYPE}" in
  283. application/x-www-form-urlencoded) setQueryVars;;
  284. multipart/form-data) getQueryFile;;
  285. *) _ERR=1; _OUT='this is not a post' ;;
  286. esac
  287. case $REQUEST_URI in
  288. /admin/pwdchange) pwdChange;;
  289. /admin/ssidchange) ssidChange;;
  290. /admin/lanaddr) lanAddr;;
  291. /admin/updatefw) updateFw;;
  292. /admin/rebootnow) rebootNow;;
  293. *) logThis 'bad action'; headerPrint 405; echo 'no such thing'; exit 1;;
  294. esac
  295. fi
  296. headerPrint 301
  297. fi
  298. headerPrint 200
  299. ## html head
  300. htmlHead
  301. echo "<body>
  302. <img src='/resources/img/superglueLogo.png' class='logo'>
  303. <hr>
  304. <h2 style='display:inline'>Superglue server control panel</h2>
  305. <span style='display:inline; margin-left: 50px;'>System version: "$(cat /etc/superglue_version || echo 'n/a')" | Device: "$(cat /etc/superglue_model || echo 'n/a')" | OpenWRT: "$(cat /etc/openwrt_version || echo 'n/a')"</span>
  306. <span style='display:block;'>$(uptime)</span>
  307. <hr>
  308. Change password:
  309. <form method='post' action='/admin/pwdchange'>
  310. <input type='text' name='usr' value='admin' readonly>
  311. <input type='password' name='pwd'>
  312. <input type='password' name='pwdd'>
  313. <input type='submit' value='Apply'>
  314. </form>
  315. <hr>
  316. Configure wireless network:
  317. <form method='post' action='/admin/ssidchange'>
  318. <input type='text' name='ssid' value='$(doUci get ssid)'>
  319. <input type='text' name='key' value='$(doUci get key)'>
  320. <input type='submit' value='Apply'>
  321. </form>
  322. <hr>
  323. Configure network address:
  324. <form method='post' action='/admin/lanaddr'>
  325. <input type='text' name='laddr' value='$(doUci get laddr)'>
  326. <input type='submit' value='Apply'>
  327. </form>
  328. <hr>
  329. Update firmware:
  330. <form method='post' action='/admin/updatefw' enctype='multipart/form-data'>
  331. <input type='file' name='fwupload'>
  332. <input type='submit' id='upload' value='Upload'>
  333. </form>
  334. <hr>
  335. <form action='/admin/rebootnow' method='post' class='inline'>
  336. <input type='hidden' name='reboot' value='now' class='inline'>
  337. <input type='submit' value='Reboot' class='inline'>
  338. </form>
  339. <form action='http://logout@${HTTP_HOST}/admin' method='get' class='inline'>
  340. <input type='submit' value='Logout' class='inline'>
  341. </form>
  342. <hr>
  343. Memory:
  344. <pre>$(free)</pre>
  345. <hr>
  346. Storage:
  347. <pre>$(df -h)</pre>
  348. <hr>
  349. Environment:
  350. <pre>$(env)</pre>
  351. <hr>
  352. $_POST
  353. </body></html>"
  354. exit 0