rename 014-samsung_flash patch to a better name, thanks maddes
[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 append_bool() {
21 local cfg="$1"
22 local var="$2"
23 local opt="$3"
24 local def="$4"
25 local val
26
27 config_get_bool val "$cfg" "$var" "$def"
28 [ "$val" = 1 ] && append UHTTPD_ARGS "$opt"
29 }
30
31 generate_keys() {
32 local cfg="$1"
33 local key="$2"
34 local crt="$3"
35 local days bits country state location commonname
36
37 config_get days "$cfg" days
38 config_get bits "$cfg" bits
39 config_get country "$cfg" country
40 config_get state "$cfg" state
41 config_get location "$cfg" location
42 config_get commonname "$cfg" commonname
43
44 [ -x "$PX5G_BIN" ] && {
45 $PX5G_BIN selfsigned -der \
46 -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "$UHTTPD_KEY" -out "$UHTTPD_CERT" \
47 -subj /C=${country:-DE}/ST=${state:-Saxony}/L=${location:-Leipzig}/CN=${commonname:-OpenWrt}
48 } || {
49 echo "WARNING: the specified certificate and key" \
50 "files do not exist and the px5g generator" \
51 "is not available, skipping SSL setup."
52 }
53 }
54
55 start_instance()
56 {
57 UHTTPD_ARGS=""
58 UHTTPD_CERT=""
59 UHTTPD_KEY=""
60
61 local cfg="$1"
62 local realm="$(uci_get system.@system[0].hostname)"
63 local listen http https
64
65 append_arg "$cfg" home "-h"
66 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
67 append_arg "$cfg" config "-c"
68 append_arg "$cfg" cgi_prefix "-x"
69 append_arg "$cfg" lua_prefix "-l"
70 append_arg "$cfg" lua_handler "-L"
71 append_arg "$cfg" script_timeout "-t"
72 append_arg "$cfg" network_timeout "-T"
73 append_arg "$cfg" error_page "-E"
74 append_arg "$cfg" index_page "-I"
75
76 append_bool "$cfg" no_symlinks "-S" 0
77 append_bool "$cfg" no_dirlists "-D" 0
78
79 config_get http "$cfg" listen_http
80 for listen in $http; do
81 append UHTTPD_ARGS "-p $listen"
82 done
83
84 config_get https "$cfg" listen_https
85 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
86 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
87
88 [ -n "$https" ] && {
89 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
90 config_foreach generate_keys cert
91 }
92
93 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
94 append_arg "$cfg" cert "-C"
95 append_arg "$cfg" key "-K"
96
97 for listen in $https; do
98 append UHTTPD_ARGS "-s $listen"
99 done
100 }
101 }
102
103 start-stop-daemon -S -x $UHTTPD_BIN \
104 -p /var/run/uhttpd_${cfg}.pid \
105 -m -b -- -f $UHTTPD_ARGS
106 }
107
108 stop_instance()
109 {
110 local cfg="$1"
111
112 [ -f /var/run/uhttpd_${cfg}.pid ] && {
113 start-stop-daemon -K -q -n ${UHTTPD_BIN##*/} \
114 -p /var/run/uhttpd_${cfg}.pid -s TERM
115
116 rm -f /var/run/uhttpd_${cfg}.pid
117 }
118 }
119
120 start() {
121 config_load uhttpd
122 config_foreach start_instance uhttpd
123 }
124
125 stop() {
126 config_load uhttpd
127 config_foreach stop_instance uhttpd
128 }