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