1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2010 Jo-Philipp Wich
8 UHTTPD_BIN
="/usr/sbin/uhttpd"
9 PX5G_BIN
="/usr/sbin/px5g"
10 OPENSSL_BIN
="/usr/bin/openssl"
19 config_get val
"$cfg" "$var"
20 [ -n "$val" -o -n "$def" ] && procd_append_param
command "$opt" "${val:-$def}"
30 config_get_bool val
"$cfg" "$var" "$def"
31 [ "$val" = 1 ] && procd_append_param
command "$opt"
38 local days bits country state location organization commonname
40 config_get days
"$cfg" days
41 config_get bits
"$cfg" bits
42 config_get country
"$cfg" country
43 config_get state
"$cfg" state
44 config_get location
"$cfg" location
45 config_get organization
"$cfg" organization
46 config_get commonname
"$cfg" commonname
47 config_get key_type
"$cfg" key_type
48 config_get ec_curve
"$cfg" ec_curve
50 # Prefer px5g for certificate generation (existence evaluated last)
52 local KEY_OPTS
="rsa:${bits:-2048}"
53 local UNIQUEID
=$
(dd if=/dev
/urandom bs
=1 count
=4 |
hexdump -e '1/1 "%02x"')
54 [ "$key_type" = "ec" ] && KEY_OPTS
="ec -pkeyopt ec_paramgen_curve:${ec_curve:-P-256}"
55 [ -x "$OPENSSL_BIN" ] && GENKEY_CMD
="$OPENSSL_BIN req -x509 -sha256 -outform der -nodes"
56 [ -x "$PX5G_BIN" ] && GENKEY_CMD
="$PX5G_BIN selfsigned -der"
57 [ -n "$GENKEY_CMD" ] && {
59 -days ${days:-730} -newkey ${KEY_OPTS} -keyout "${UHTTPD_KEY}.new" -out "${UHTTPD_CERT}.new
" \
60 -subj /C="${country:-ZZ}"/ST="${state:-Somewhere}"/L="${location:-Unknown}"/O="${organization:-OpenWrt$UNIQUEID}"/CN="${commonname:-OpenWrt}"
62 mv "${UHTTPD_KEY}.new
" "${UHTTPD_KEY}"
63 mv "${UHTTPD_CERT}.new
" "${UHTTPD_CERT}"
69 local prefix username password
71 config_get prefix "$cfg" prefix
72 config_get username "$cfg" username
73 config_get password "$cfg" password
75 if [ -z "$prefix" ] || [ -z "$username" ] || [ -z "$password" ]; then
78 echo "${prefix}:${username}:${password}" >>$httpdconf
84 local prefix="${v%%=*}"
85 local handler="${v#*=}"
87 if [ "$prefix" != "$handler" ] && [ -n "$prefix" ] && [ -f "$handler" ]; then
88 procd_append_param command -l "$prefix" -L "$handler"
90 echo "Skipping invalid Lua prefix
\"$v\"" >&2
100 local realm="$
(uci_get system.@system
[0].hostname
)"
101 local listen http https interpreter indexes path handler httpdconf haveauth
104 config_get_bool enabled "$cfg" 'enabled' 1
105 [ $enabled -gt 0 ] || return
108 procd_set_param respawn
109 procd_set_param stderr 1
110 procd_set_param command "$UHTTPD_BIN" -f
112 config_get config "$cfg" config
113 if [ -z "$config" ]; then
114 mkdir -p /var/etc/uhttpd
115 httpdconf="/var
/etc
/uhttpd
/httpd.
${cfg}.conf
"
117 config_list_foreach "$cfg" httpauth create_httpauth
118 if [ "$haveauth" = "1" ]; then
119 procd_append_param command -c ${httpdconf}
120 [ -r /etc/httpd.conf ] && cat /etc/httpd.conf >>/var/etc/uhttpd/httpd.${cfg}.conf
124 append_arg "$cfg" home "-h"
125 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
126 append_arg "$cfg" config "-c"
127 append_arg "$cfg" cgi_prefix "-x"
128 [ -f /usr/lib/uhttpd_lua.so ] && {
130 config_get len "$cfg" lua_prefix_LENGTH
132 if [ -n "$len" ]; then
133 config_list_foreach "$cfg" lua_prefix append_lua_prefix
135 config_get prefix "$cfg" lua_prefix
136 config_get handler "$cfg" lua_handler
137 append_lua_prefix "$prefix=$handler"
140 [ -f /usr/lib/uhttpd_ubus.so ] && {
141 append_arg "$cfg" ubus_prefix "-u"
142 append_arg "$cfg" ubus_socket "-U"
143 append_bool "$cfg" ubus_cors "-X" 0
145 append_arg "$cfg" script_timeout "-t"
146 append_arg "$cfg" network_timeout "-T"
147 append_arg "$cfg" http_keepalive "-k"
148 append_arg "$cfg" tcp_keepalive "-A"
149 append_arg "$cfg" error_page "-E"
150 append_arg "$cfg" max_requests "-n" 3
151 append_arg "$cfg" max_connections "-N"
153 append_bool "$cfg" no_ubusauth "-a" 0
154 append_bool "$cfg" no_symlinks "-S" 0
155 append_bool "$cfg" no_dirlists "-D" 0
156 append_bool "$cfg" rfc1918_filter "-R" 0
158 config_get alias_list "$cfg" alias
159 for alias in $alias_list; do
160 procd_append_param command -y "$alias"
163 config_get http "$cfg" listen_http
164 for listen in $http; do
165 procd_append_param command -p "$listen"
168 config_get interpreter "$cfg" interpreter
169 for path in $interpreter; do
170 procd_append_param command -i "$path"
173 config_get indexes "$cfg" index_page
174 for path in $indexes; do
175 procd_append_param command -I "$path"
178 config_get https "$cfg" listen_https
179 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
180 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
182 [ -f /lib/libustream-ssl.so ] && [ -n "$https" ] && {
183 [ -s "$UHTTPD_CERT" -a -s "$UHTTPD_KEY" ] || {
184 config_foreach generate_keys cert
187 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
188 append_arg "$cfg" cert "-C"
189 append_arg "$cfg" key "-K"
191 for listen in $https; do
192 procd_append_param command -s "$listen"
196 append_bool "$cfg" redirect_https "-q" 0
199 config_get json_script "$cfg" json_script
200 for file in $json_script; do
201 [ -s "$file" ] && procd_append_param command -H "$file"
209 procd_add_reload_trigger "uhttpd
"
214 config_foreach start_instance uhttpd