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}" \
61 -addext extendedKeyUsage=serverAuth -addext subjectAltName=DNS:"${commonname:-OpenWrt}"
63 mv "${UHTTPD_KEY}.new
" "${UHTTPD_KEY}"
64 mv "${UHTTPD_CERT}.new
" "${UHTTPD_CERT}"
70 local prefix username password
72 config_get prefix "$cfg" prefix
73 config_get username "$cfg" username
74 config_get password "$cfg" password
76 if [ -z "$prefix" ] || [ -z "$username" ] || [ -z "$password" ]; then
79 echo "${prefix}:${username}:${password}" >>$httpdconf
85 local prefix="${v%%=*}"
86 local handler="${v#*=}"
88 if [ "$prefix" != "$handler" ] && [ -n "$prefix" ] && [ -f "$handler" ]; then
89 procd_append_param command -l "$prefix" -L "$handler"
91 echo "Skipping invalid Lua prefix
\"$v\"" >&2
95 append_ucode_prefix() {
97 local prefix="${v%%=*}"
98 local handler="${v#*=}"
100 if [ "$prefix" != "$handler" ] && [ -n "$prefix" ] && [ -f "$handler" ]; then
101 procd_append_param command -o "$prefix" -O "$handler"
103 echo "Skipping invalid ucode prefix
\"$v\"" >&2
113 local realm="$
(uci_get system.@system
[0].hostname
)"
114 local listen http https interpreter indexes path handler httpdconf haveauth
117 config_get_bool enabled "$cfg" 'enabled' 1
118 [ $enabled -gt 0 ] || return
121 procd_set_param respawn
122 procd_set_param stderr 1
123 procd_set_param command "$UHTTPD_BIN" -f
125 config_get config "$cfg" config
126 if [ -z "$config" ]; then
127 mkdir -p /var/etc/uhttpd
128 httpdconf="/var
/etc
/uhttpd
/httpd.
${cfg}.conf
"
130 config_list_foreach "$cfg" httpauth create_httpauth
131 if [ "$haveauth" = "1" ]; then
132 procd_append_param command -c ${httpdconf}
133 [ -r /etc/httpd.conf ] && cat /etc/httpd.conf >>/var/etc/uhttpd/httpd.${cfg}.conf
137 append_arg "$cfg" home "-h"
138 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
139 append_arg "$cfg" config "-c"
140 append_arg "$cfg" cgi_prefix "-x"
141 [ -f /usr/lib/uhttpd_lua.so ] && {
143 config_get len "$cfg" lua_prefix_LENGTH
145 if [ -n "$len" ]; then
146 config_list_foreach "$cfg" lua_prefix append_lua_prefix
148 config_get prefix "$cfg" lua_prefix
149 config_get handler "$cfg" lua_handler
150 append_lua_prefix "$prefix=$handler"
153 [ -f /usr/lib/uhttpd_ubus.so ] && {
154 append_arg "$cfg" ubus_prefix "-u"
155 append_arg "$cfg" ubus_socket "-U"
156 append_bool "$cfg" ubus_cors "-X" 0
158 [ -f /usr/lib/uhttpd_ucode.so ] && {
159 config_list_foreach "$cfg" ucode_prefix append_ucode_prefix
161 append_arg "$cfg" script_timeout "-t"
162 append_arg "$cfg" network_timeout "-T"
163 append_arg "$cfg" http_keepalive "-k"
164 append_arg "$cfg" tcp_keepalive "-A"
165 append_arg "$cfg" error_page "-E"
166 append_arg "$cfg" max_requests "-n" 3
167 append_arg "$cfg" max_connections "-N"
169 append_bool "$cfg" no_ubusauth "-a" 0
170 append_bool "$cfg" no_symlinks "-S" 0
171 append_bool "$cfg" no_dirlists "-D" 0
172 append_bool "$cfg" rfc1918_filter "-R" 0
174 config_get alias_list "$cfg" alias
175 for alias in $alias_list; do
176 procd_append_param command -y "$alias"
179 config_get http "$cfg" listen_http
180 for listen in $http; do
181 procd_append_param command -p "$listen"
184 config_get interpreter "$cfg" interpreter
185 for path in $interpreter; do
186 procd_append_param command -i "$path"
189 config_get indexes "$cfg" index_page
190 for path in $indexes; do
191 procd_append_param command -I "$path"
194 config_get https "$cfg" listen_https
195 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
196 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
198 [ -f /lib/libustream-ssl.so ] && [ -n "$https" ] && {
199 [ -s "$UHTTPD_CERT" -a -s "$UHTTPD_KEY" ] || {
200 config_foreach generate_keys cert
203 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
204 append_arg "$cfg" cert "-C"
205 append_arg "$cfg" key "-K"
207 for listen in $https; do
208 procd_append_param command -s "$listen"
212 append_bool "$cfg" redirect_https "-q" 0
215 config_get json_script "$cfg" json_script
216 for file in $json_script; do
217 [ -s "$file" ] && procd_append_param command -H "$file"
226 local http https listen ips
228 config_get http "$cfg" listen_http
229 config_get https "$cfg" listen_https
230 for listen in $http $https; do
238 ips="$ips ${listen%%:*}
243 ips="$ips ${listen%%]:*}
248 ips="$
( echo "$ips" |
sort -u )"
255 local ipaddr ipaddrs testip="$2"
257 config_get ipaddrs "$cfg" ipaddr
258 for ipaddr in $ipaddrs; do
259 [ "$ipaddr" = "$testip" ] && echo "$cfg"
263 get_interface_by_ip()
266 config_foreach resolve_iface interface "$@
"
271 local iface ifaces all=0
273 procd_add_reload_trigger "uhttpd
"
274 procd_add_raw_trigger acme.renew 5000 /etc/init.d/uhttpd reload
277 ips="$
(config_foreach uhttpd_interfaces uhttpd
)"
278 [ -z "$ips" ] && return 0
281 iface="$
(get_interface_by_ip
$ip)"
282 [ -z "$iface" ] && all=1
283 ifaces="$ifaces $iface"
286 if [ "$all" = "1" ]; then
287 procd_add_raw_trigger "interface.
*.up
" 1000 /etc/init.d/uhttpd start
289 for iface in $ifaces; do
290 procd_add_raw_trigger "interface.
$iface.up
" 1000 /etc/init.d/uhttpd start
297 config_foreach start_instance uhttpd