admin.sh 10 KB

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