[package] uhttpd:
[openwrt/svn-archive/archive.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 append_arg "$cfg" script_timeout "-t"
69
70 config_list_foreach "$cfg" listen_http \
71 append_listen_http
72
73 config_get ssl "$cfg" listen_https
74 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
75 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
76
77 [ -n "$ssl" ] && {
78 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
79 config_foreach generate_keys cert
80 }
81
82 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
83 append_arg "$cfg" cert "-C"
84 append_arg "$cfg" key "-K"
85
86 config_list_foreach "$cfg" listen_https \
87 append_listen_https
88 }
89 }
90
91 start-stop-daemon -S -x $UHTTPD_BIN \
92 -p /var/run/uhttpd_${cfg}.pid \
93 -m -b -- -f $UHTTPD_ARGS
94 }
95
96 stop_instance()
97 {
98 local cfg="$1"
99
100 [ -f /var/run/uhttpd_${cfg}.pid ] && {
101 start-stop-daemon -K -q -n ${UHTTPD_BIN##*/} \
102 -p /var/run/uhttpd_${cfg}.pid -s TERM
103
104 rm -f /var/run/uhttpd_${cfg}.pid
105 }
106 }
107
108 start() {
109 config_load uhttpd
110 config_foreach start_instance uhttpd
111 }
112
113 stop() {
114 config_load uhttpd
115 config_foreach stop_instance uhttpd
116 }