uhttpd: Add Basic Auth config
[openwrt/staging/yousong.git] / package / network / services / uhttpd / files / uhttpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2010 Jo-Philipp Wich
3
4 START=50
5
6 USE_PROCD=1
7
8 UHTTPD_BIN="/usr/sbin/uhttpd"
9 PX5G_BIN="/usr/sbin/px5g"
10 OPENSSL_BIN="/usr/bin/openssl"
11
12 append_arg() {
13 local cfg="$1"
14 local var="$2"
15 local opt="$3"
16 local def="$4"
17 local val
18
19 config_get val "$cfg" "$var"
20 [ -n "$val" -o -n "$def" ] && procd_append_param command "$opt" "${val:-$def}"
21 }
22
23 append_bool() {
24 local cfg="$1"
25 local var="$2"
26 local opt="$3"
27 local def="$4"
28 local val
29
30 config_get_bool val "$cfg" "$var" "$def"
31 [ "$val" = 1 ] && procd_append_param command "$opt"
32 }
33
34 generate_keys() {
35 local cfg="$1"
36 local key="$2"
37 local crt="$3"
38 local days bits country state location commonname
39
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 commonname "$cfg" commonname
46
47 # Prefer px5g for certificate generation (existence evaluated last)
48 local GENKEY_CMD=""
49 local UNIQUEID=$(dd if=/dev/urandom bs=1 count=4 | hexdump -e '1/1 "%02x"')
50 [ -x "$OPENSSL_BIN" ] && GENKEY_CMD="$OPENSSL_BIN req -x509 -outform der -nodes"
51 [ -x "$PX5G_BIN" ] && GENKEY_CMD="$PX5G_BIN selfsigned -der"
52 [ -n "$GENKEY_CMD" ] && {
53 $GENKEY_CMD \
54 -days ${days:-730} -newkey rsa:${bits:-2048} -keyout "${UHTTPD_KEY}.new" -out "${UHTTPD_CERT}.new" \
55 -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/O="${commonname:-Lede}$UNIQUEID"/CN="${commonname:-Lede}"
56 sync
57 mv "${UHTTPD_KEY}.new" "${UHTTPD_KEY}"
58 mv "${UHTTPD_CERT}.new" "${UHTTPD_CERT}"
59 }
60 }
61
62 create_httpauth() {
63 local cfg="$1"
64 local prefix username password
65
66 config_get prefix "$cfg" prefix
67 config_get username "$cfg" username
68 config_get password "$cfg" password
69
70 if [ -z "$prefix" ] || [ -z "$username" ] || [ -z "$password" ]; then
71 return
72 fi
73 echo "${prefix}:${username}:${password}" >>$httpdconf
74 haveauth=1
75 }
76
77 start_instance()
78 {
79 UHTTPD_CERT=""
80 UHTTPD_KEY=""
81
82 local cfg="$1"
83 local realm="$(uci_get system.@system[0].hostname)"
84 local listen http https interpreter indexes path handler httpdconf haveauth
85
86 procd_open_instance
87 procd_set_param respawn
88 procd_set_param stderr 1
89 procd_set_param command "$UHTTPD_BIN" -f
90
91 config_get config "$cfg" config
92 if [ -z "$config" ]; then
93 mkdir -p /var/etc/uhttpd
94 httpdconf="/var/etc/uhttpd/httpd.${cfg}.conf"
95 rm -f ${httpdconf}
96 config_list_foreach "$cfg" httpauth create_httpauth
97 if [ "$haveauth" = "1" ]; then
98 procd_append_param command -c ${httpdconf}
99 [ -r /etc/httpd.conf ] && cat /etc/httpd.conf >>/var/etc/uhttpd/httpd.${cfg}.conf
100 fi
101 fi
102
103 append_arg "$cfg" home "-h"
104 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
105 append_arg "$cfg" config "-c"
106 append_arg "$cfg" cgi_prefix "-x"
107 [ -f /usr/lib/uhttpd_lua.so ] && {
108 config_get handler "$cfg" lua_handler
109 [ -f "$handler" ] && append_arg "$cfg" lua_prefix "-l" && {
110 procd_append_param command "-L" "$handler"
111 }
112 }
113 [ -f /usr/lib/uhttpd_ubus.so ] && {
114 append_arg "$cfg" ubus_prefix "-u"
115 append_arg "$cfg" ubus_socket "-U"
116 append_bool "$cfg" ubus_cors "-X" 0
117 }
118 append_arg "$cfg" script_timeout "-t"
119 append_arg "$cfg" network_timeout "-T"
120 append_arg "$cfg" http_keepalive "-k"
121 append_arg "$cfg" tcp_keepalive "-A"
122 append_arg "$cfg" error_page "-E"
123 append_arg "$cfg" max_requests "-n" 3
124 append_arg "$cfg" max_connections "-N"
125
126 append_bool "$cfg" no_ubusauth "-a" 0
127 append_bool "$cfg" no_symlinks "-S" 0
128 append_bool "$cfg" no_dirlists "-D" 0
129 append_bool "$cfg" rfc1918_filter "-R" 0
130
131 config_get alias_list "$cfg" alias
132 for alias in $alias_list; do
133 procd_append_param command -y "$alias"
134 done
135
136 config_get http "$cfg" listen_http
137 for listen in $http; do
138 procd_append_param command -p "$listen"
139 done
140
141 config_get interpreter "$cfg" interpreter
142 for path in $interpreter; do
143 procd_append_param command -i "$path"
144 done
145
146 config_get indexes "$cfg" index_page
147 for path in $indexes; do
148 procd_append_param command -I "$path"
149 done
150
151 config_get https "$cfg" listen_https
152 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
153 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
154
155 [ -f /lib/libustream-ssl.so ] && [ -n "$https" ] && {
156 [ -s "$UHTTPD_CERT" -a -s "$UHTTPD_KEY" ] || {
157 config_foreach generate_keys cert
158 }
159
160 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
161 append_arg "$cfg" cert "-C"
162 append_arg "$cfg" key "-K"
163
164 for listen in $https; do
165 procd_append_param command -s "$listen"
166 done
167 }
168
169 append_bool "$cfg" redirect_https "-q" 0
170 }
171
172 for file in /etc/uhttpd/*.json; do
173 [ -s "$file" ] && procd_append_param command -H "$file"
174 done
175
176 procd_close_instance
177 }
178
179 service_triggers()
180 {
181 procd_add_reload_trigger "uhttpd"
182 }
183
184 start_service() {
185 config_load uhttpd
186 config_foreach start_instance uhttpd
187 }