[package] uhttpd:
[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)"
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 append_arg "$cfg" network_timeout "-T"
62
63 config_get http "$cfg" listen_http
64 for listen in $http; do
65 append UHTTPD_ARGS "-p $listen"
66 done
67
68 config_get https "$cfg" listen_https
69 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
70 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
71
72 [ -n "$https" ] && {
73 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
74 config_foreach generate_keys cert
75 }
76
77 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
78 append_arg "$cfg" cert "-C"
79 append_arg "$cfg" key "-K"
80
81 for listen in $https; do
82 append UHTTPD_ARGS "-s $listen"
83 done
84 }
85 }
86
87 start-stop-daemon -S -x $UHTTPD_BIN \
88 -p /var/run/uhttpd_${cfg}.pid \
89 -m -b -- -f $UHTTPD_ARGS
90 }
91
92 stop_instance()
93 {
94 local cfg="$1"
95
96 [ -f /var/run/uhttpd_${cfg}.pid ] && {
97 start-stop-daemon -K -q -n ${UHTTPD_BIN##*/} \
98 -p /var/run/uhttpd_${cfg}.pid -s TERM
99
100 rm -f /var/run/uhttpd_${cfg}.pid
101 }
102 }
103
104 start() {
105 config_load uhttpd
106 config_foreach start_instance uhttpd
107 }
108
109 stop() {
110 config_load uhttpd
111 config_foreach stop_instance uhttpd
112 }
This page took 0.046636 seconds and 5 git commands to generate.