uhttpd: expose missing options to uci
[openwrt/staging/lynxis/omap.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 SERVICE_DAEMONIZE=1
7 SERVICE_WRITE_PID=1
8
9 UHTTPD_BIN="/usr/sbin/uhttpd"
10 PX5G_BIN="/usr/sbin/px5g"
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" ] && append UHTTPD_ARGS "$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 ] && append UHTTPD_ARGS "$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 [ -x "$PX5G_BIN" ] && {
48 $PX5G_BIN selfsigned -der \
49 -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "$UHTTPD_KEY" -out "$UHTTPD_CERT" \
50 -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/CN="${commonname:-OpenWrt}"
51 }
52 }
53
54 start_instance()
55 {
56 UHTTPD_ARGS=""
57 UHTTPD_CERT=""
58 UHTTPD_KEY=""
59
60 local cfg="$1"
61 local realm="$(uci_get system.@system[0].hostname)"
62 local listen http https interpreter indexes path
63
64 append_arg "$cfg" home "-h"
65 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
66 append_arg "$cfg" config "-c"
67 append_arg "$cfg" cgi_prefix "-x"
68 append_arg "$cfg" lua_prefix "-l"
69 append_arg "$cfg" lua_handler "-L"
70 append_arg "$cfg" ubus_prefix "-u"
71 append_arg "$cfg" ubus_socket "-U"
72 append_arg "$cfg" script_timeout "-t"
73 append_arg "$cfg" network_timeout "-T"
74 append_arg "$cfg" http_keepalive "-k"
75 append_arg "$cfg" tcp_keepalive "-A"
76 append_arg "$cfg" error_page "-E"
77 append_arg "$cfg" max_requests "-n" 3
78 append_arg "$cfg" max_connections "-N"
79
80 append_bool "$cfg" no_ubusauth "-a" 0
81 append_bool "$cfg" no_symlinks "-S" 0
82 append_bool "$cfg" no_dirlists "-D" 0
83 append_bool "$cfg" rfc1918_filter "-R" 0
84
85 config_get http "$cfg" listen_http
86 for listen in $http; do
87 append UHTTPD_ARGS "-p $listen"
88 done
89
90 config_get interpreter "$cfg" interpreter
91 for path in $interpreter; do
92 append UHTTPD_ARGS "-i $path"
93 done
94
95 config_get indexes "$cfg" index_page
96 for path in $indexes; do
97 append UHTTPD_ARGS "-I $path"
98 done
99
100 config_get https "$cfg" listen_https
101 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
102 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
103
104 [ -n "$https" ] && {
105 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
106 config_foreach generate_keys cert
107 }
108
109 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
110 append_arg "$cfg" cert "-C"
111 append_arg "$cfg" key "-K"
112
113 for listen in $https; do
114 append UHTTPD_ARGS "-s $listen"
115 done
116 }
117 }
118
119 SERVICE_PID_FILE=/var/run/uhttpd_${cfg}.pid
120 service_start $UHTTPD_BIN -f $UHTTPD_ARGS
121
122 # Check if daemon is running, if not then
123 # re-execute in foreground to display error.
124 sleep 1 && service_check $UHTTPD_BIN || \
125 $UHTTPD_BIN -f $UHTTPD_ARGS
126 }
127
128 stop_instance()
129 {
130 local cfg="$1"
131
132 SERVICE_PID_FILE=/var/run/uhttpd_${cfg}.pid
133 service_stop $UHTTPD_BIN
134 }
135
136 start() {
137 config_load uhttpd
138 config_foreach start_instance uhttpd
139 }
140
141 stop() {
142 config_load uhttpd
143 config_foreach stop_instance uhttpd
144 }