uhttpd.init 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010 Jo-Philipp Wich
  3. START=50
  4. SERVICE_DAEMONIZE=1
  5. SERVICE_WRITE_PID=1
  6. UHTTPD_BIN="/usr/sbin/uhttpd"
  7. PX5G_BIN="/usr/sbin/px5g"
  8. append_arg() {
  9. local cfg="$1"
  10. local var="$2"
  11. local opt="$3"
  12. local def="$4"
  13. local val
  14. config_get val "$cfg" "$var"
  15. [ -n "$val" -o -n "$def" ] && append UHTTPD_ARGS "$opt ${val:-$def}"
  16. }
  17. append_bool() {
  18. local cfg="$1"
  19. local var="$2"
  20. local opt="$3"
  21. local def="$4"
  22. local val
  23. config_get_bool val "$cfg" "$var" "$def"
  24. [ "$val" = 1 ] && append UHTTPD_ARGS "$opt"
  25. }
  26. generate_keys() {
  27. local cfg="$1"
  28. local key="$2"
  29. local crt="$3"
  30. local days bits country state location commonname
  31. config_get days "$cfg" days
  32. config_get bits "$cfg" bits
  33. config_get country "$cfg" country
  34. config_get state "$cfg" state
  35. config_get location "$cfg" location
  36. config_get commonname "$cfg" commonname
  37. [ -x "$PX5G_BIN" ] && {
  38. $PX5G_BIN selfsigned -der \
  39. -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "$UHTTPD_KEY" -out "$UHTTPD_CERT" \
  40. -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/CN="${commonname:-OpenWrt}"
  41. }
  42. }
  43. start_instance()
  44. {
  45. UHTTPD_ARGS=""
  46. UHTTPD_CERT=""
  47. UHTTPD_KEY=""
  48. local cfg="$1"
  49. local realm="$(uci_get system.@system[0].hostname)"
  50. local listen http https interpreter path
  51. append_arg "$cfg" home "-h"
  52. append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
  53. append_arg "$cfg" config "-c"
  54. append_arg "$cfg" cgi_prefix "-x"
  55. append_arg "$cfg" lua_prefix "-l"
  56. append_arg "$cfg" lua_handler "-L"
  57. append_arg "$cfg" script_timeout "-t"
  58. append_arg "$cfg" network_timeout "-T"
  59. append_arg "$cfg" tcp_keepalive "-A"
  60. append_arg "$cfg" error_page "-E"
  61. append_arg "$cfg" index_page "-I"
  62. append_bool "$cfg" no_symlinks "-S" 0
  63. append_bool "$cfg" no_dirlists "-D" 0
  64. append_bool "$cfg" rfc1918_filter "-R" 0
  65. config_get http "$cfg" listen_http
  66. for listen in $http; do
  67. append UHTTPD_ARGS "-p $listen"
  68. done
  69. config_get interpreter "$cfg" interpreter
  70. for path in $interpreter; do
  71. append UHTTPD_ARGS "-i $path"
  72. done
  73. config_get https "$cfg" listen_https
  74. config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
  75. config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
  76. [ -n "$https" ] && {
  77. [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
  78. config_foreach generate_keys cert
  79. }
  80. [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
  81. append_arg "$cfg" cert "-C"
  82. append_arg "$cfg" key "-K"
  83. for listen in $https; do
  84. append UHTTPD_ARGS "-s $listen"
  85. done
  86. }
  87. }
  88. SERVICE_PID_FILE=/var/run/uhttpd_${cfg}.pid
  89. service_start $UHTTPD_BIN -f $UHTTPD_ARGS
  90. # Check if daemon is running, if not then
  91. # re-execute in foreground to display error.
  92. sleep 1 && service_check $UHTTPD_BIN || \
  93. $UHTTPD_BIN -f $UHTTPD_ARGS
  94. }
  95. stop_instance()
  96. {
  97. local cfg="$1"
  98. SERVICE_PID_FILE=/var/run/uhttpd_${cfg}.pid
  99. service_stop $UHTTPD_BIN
  100. }
  101. start() {
  102. config_load uhttpd
  103. config_foreach start_instance uhttpd
  104. }
  105. stop() {
  106. config_load uhttpd
  107. config_foreach stop_instance uhttpd
  108. }