upload.cgi 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/haserl --shell=/bin/bash --upload-limit=32768 --upload-dir=/tmp
  2. <%# upload limit: 32Mb %>
  3. <%
  4. #some path variables
  5. _WWW='/www'
  6. _TMP="${_WWW}/tmp"
  7. _LOG="${_WWW}/log/upload.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. log() {
  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. return 0
  24. }
  25. head() {
  26. case "$1" in
  27. 200|'') printf '%b' 'HTTP/1.1 200 OK\nAccess-Control-Allow-Origin: *\n\n';;
  28. 405) printf '%b' 'HTTP/1.1 405 Method Not Allowed\n\n';;
  29. 406) printf '%b' 'HTTP/1.1 406 Not Acceptable\n\n';;
  30. *) printf '%b' 'HTTP/1.1 400 Bad Request\n\n';;
  31. esac
  32. }
  33. #_REF="$HTTP_REFERER"
  34. #_SESS="$SESSIONID"
  35. #log $_REF $_SESS
  36. ## checks and sanitation
  37. [[ ${CONTENT_TYPE^^} == MULTIPART/FORM-DATA* ]] || err 'wrong content type' '406'
  38. [[ "${REQUEST_METHOD^^}" == "POST" ]] || err 'wrong method, not a post' '405'
  39. _UPLD="${HASERL_fwupload_path##*/}"
  40. mv "$_TMP/$_UPLD" "$_TMP/fwupload.bin" 2>/dev/null || err 'error renaming upload'
  41. log 'upload OK'
  42. head '200'
  43. #UPLD="${HASERL_fwupload_path##*/}"
  44. #UPLD="${_UPLD//[^a-zA-Z0-9_.-]/}"
  45. #[ -n "$_UPLD" ] || err 'empty filename value, sanitation failed?'
  46. #[ -f "$_TMP/$_UPLD" ] || err 'can not access uploaded file, sanitation failed?'
  47. #log "$_UPLD"
  48. %>