a25bf789cbbb9903e3988a3c96a677f36036ba9c
[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_listen_http() {
10 append UHTTPD_ARGS "-p $1"
11 }
12
13 append_listen_https() {
14 append UHTTPD_ARGS "-s $1"
15 }
16
17 append_arg() {
18 local cfg="$1"
19 local var="$2"
20 local opt="$3"
21 local def="$4"
22 local val
23
24 config_get val "$cfg" "$var"
25 [ -n "$val" -o -n "$def" ] && append UHTTPD_ARGS "$opt ${val:-$def}"
26 }
27
28 generate_keys() {
29 local cfg="$1"
30 local key="$2"
31 local crt="$3"
32 local days bits country state location commonname
33
34 config_get days "$cfg" days
35 config_get bits "$cfg" bits
36 config_get country "$cfg" country
37 config_get state "$cfg" state
38 config_get location "$cfg" location
39 config_get commonname "$cfg" commonname
40
41 [ -x "$PX5G_BIN" ] && {
42 $PX5G_BIN selfsigned -der \
43 -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "$UHTTPD_KEY" -out "$UHTTPD_CERT" \
44 -subj /C=${country:-DE}/ST=${state:-Saxony}/L=${location:-Leipzig}/CN=${commonname:-OpenWrt}
45 } || {
46 echo "WARNING: the specified certificate and key" \
47 "files do not exist and the px5g generator" \
48 "is not available, skipping SSL setup."
49 }
50 }
51
52 start_instance()
53 {
54 UHTTPD_ARGS=""
55 UHTTPD_CERT=""
56 UHTTPD_KEY=""
57
58 local cfg="$1"
59 local realm="$(uci get system.@system[0].hostname 2>/dev/null)"
60 local ssl
61
62 append_arg "$cfg" home "-h"
63 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
64 append_arg "$cfg" config "-c"
65 append_arg "$cfg" cgi_prefix "-x"
66 append_arg "$cfg" lua_prefix "-l"
67 append_arg "$cfg" lua_handler "-L"
68
69 config_list_foreach "$cfg" listen_http \
70 append_listen_http
71
72 config_get ssl "$cfg" listen_https
73 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
74 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
75
76 [ -n "$ssl" ] && {
77 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
78 config_foreach generate_keys cert
79 }
80
81 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
82 append_arg "$cfg" cert "-C"
83 append_arg "$cfg" key "-K"
84
85 config_list_foreach "$cfg" listen_https \
86 append_listen_https
87 }
88 }
89
90 start-stop-daemon -S -x $UHTTPD_BIN \
91 -p /var/run/uhttpd_${cfg}.pid \
92 -m -b -- -f $UHTTPD_ARGS
93 }
94
95 stop_instance()
96 {
97 local cfg="$1"
98
99 [ -f /var/run/uhttpd_${cfg}.pid ] && {
100 start-stop-daemon -K -q -n ${UHTTPD_BIN##*/} \
101 -p /var/run/uhttpd_${cfg}.pid -s TERM
102
103 rm -f /var/run/uhttpd_${cfg}.pid
104 }
105 }
106
107 start() {
108 config_load uhttpd
109 config_foreach start_instance uhttpd
110 }
111
112 stop() {
113 config_load uhttpd
114 config_foreach stop_instance uhttpd
115 }
This page took 0.056468 seconds and 3 git commands to generate.