uhttpd: - cope with options instead of lists in uci config - fix compilation without...
[openwrt/openwrt.git] / package / uhttpd / files / uhttpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2010 Jo-Philipp Wich
3
4 START=50
5 UHTTPD_BIN="/usr/sbin/uhttpd"
6 PX5G_BIN="/usr/sbin/px5g"
7
8
9 append_arg() {
10 local cfg="$1"
11 local var="$2"
12 local opt="$3"
13 local def="$4"
14 local val
15
16 config_get val "$cfg" "$var"
17 [ -n "$val" -o -n "$def" ] && append UHTTPD_ARGS "$opt ${val:-$def}"
18 }
19
20 generate_keys() {
21 local cfg="$1"
22 local key="$2"
23 local crt="$3"
24 local days bits country state location commonname
25
26 config_get days "$cfg" days
27 config_get bits "$cfg" bits
28 config_get country "$cfg" country
29 config_get state "$cfg" state
30 config_get location "$cfg" location
31 config_get commonname "$cfg" commonname
32
33 [ -x "$PX5G_BIN" ] && {
34 $PX5G_BIN selfsigned -der \
35 -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "$UHTTPD_KEY" -out "$UHTTPD_CERT" \
36 -subj /C=${country:-DE}/ST=${state:-Saxony}/L=${location:-Leipzig}/CN=${commonname:-OpenWrt}
37 } || {
38 echo "WARNING: the specified certificate and key" \
39 "files do not exist and the px5g generator" \
40 "is not available, skipping SSL setup."
41 }
42 }
43
44 start_instance()
45 {
46 UHTTPD_ARGS=""
47 UHTTPD_CERT=""
48 UHTTPD_KEY=""
49
50 local cfg="$1"
51 local realm="$(uci get system.@system[0].hostname 2>/dev/null)"
52 local listen http https
53
54 append_arg "$cfg" home "-h"
55 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
56 append_arg "$cfg" config "-c"
57 append_arg "$cfg" cgi_prefix "-x"
58 append_arg "$cfg" lua_prefix "-l"
59 append_arg "$cfg" lua_handler "-L"
60 append_arg "$cfg" script_timeout "-t"
61
62 config_get http "$cfg" listen_http
63 for listen in $http; do
64 append UHTTPD_ARGS "-p $listen"
65 done
66
67 config_get https "$cfg" listen_https
68 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
69 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
70
71 [ -n "$https" ] && {
72 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
73 config_foreach generate_keys cert
74 }
75
76 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
77 append_arg "$cfg" cert "-C"
78 append_arg "$cfg" key "-K"
79
80 for listen in $https; do
81 append UHTTPD_ARGS "-s $listen"
82 done
83 }
84 }
85
86 start-stop-daemon -S -x $UHTTPD_BIN \
87 -p /var/run/uhttpd_${cfg}.pid \
88 -m -b -- -f $UHTTPD_ARGS
89 }
90
91 stop_instance()
92 {
93 local cfg="$1"
94
95 [ -f /var/run/uhttpd_${cfg}.pid ] && {
96 start-stop-daemon -K -q -n ${UHTTPD_BIN##*/} \
97 -p /var/run/uhttpd_${cfg}.pid -s TERM
98
99 rm -f /var/run/uhttpd_${cfg}.pid
100 }
101 }
102
103 start() {
104 config_load uhttpd
105 config_foreach start_instance uhttpd
106 }
107
108 stop() {
109 config_load uhttpd
110 config_foreach stop_instance uhttpd
111 }