miniupnpd: fix error message during shutdown
[feed/routing.git] / miniupnpd / files / miniupnpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2011 OpenWrt.org
3
4 START=95
5 STOP=15
6
7 SERVICE_USE_PID=1
8
9 upnpd_get_port_range() {
10 local _var="$1"; shift
11 local _val
12
13 config_get _val "$@"
14
15 case "$_val" in
16 [0-9]*[:-][0-9]*)
17 export -n -- "${_var}_start=${_val%%[:-]*}"
18 export -n -- "${_var}_end=${_val##*[:-]}"
19 ;;
20 [0-9]*)
21 export -n -- "${_var}_start=$_val"
22 export -n -- "${_var}_end="
23 ;;
24 esac
25 }
26
27 conf_rule_add() {
28 local cfg="$1"
29 local tmpconf="$2"
30 local action external_port_start external_port_end int_addr
31 local internal_port_start internal_port_end
32
33 config_get action "$cfg" action "deny" # allow or deny
34 upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
35 config_get int_addr "$cfg" int_addr "0.0.0.0/0" # ip or network and subnet mask (internal)
36 upnpd_get_port_range "int" "$cfg" int_ports "0-65535" # internal ports: x, x-y, x:y or range
37
38 # Make a single IP IP/32 so that miniupnpd.conf can use it.
39 case "$int_addr" in
40 */*) ;;
41 *) int_addr="$int_addr/32" ;;
42 esac
43
44 echo "${action} ${ext_start}${ext_end:+-}${ext_end} ${int_addr} ${int_start}${int_end:+-}${int_end}" >>$tmpconf
45 }
46
47 upnpd_write_bool() {
48 local opt="$1"
49 local def="${2:-0}"
50 local alt="$3"
51 local val
52
53 config_get_bool val config "$opt" "$def"
54 if [ "$val" -eq 0 ]; then
55 echo "${alt:-$opt}=no" >> $tmpconf
56 else
57 echo "${alt:-$opt}=yes" >> $tmpconf
58 fi
59 }
60
61 boot() {
62 return
63 }
64
65 start() {
66 config_load "upnpd"
67 local extiface intiface upload download logging secure enabled natpmp
68 local extip port usesysuptime conffile serial_number model_number
69 local uuid notify_interval presentation_url enable_upnp
70 local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
71 local ipv6_listening_ip
72
73 config_get extiface config external_iface
74 config_get extzone config external_zone
75 config_get intiface config internal_iface
76 config_get extip config external_ip
77 config_get port config port 5000
78 config_get upload config upload
79 config_get download config download
80 config_get_bool logging config log_output 0
81 config_get conffile config config_file
82 config_get serial_number config serial_number
83 config_get model_number config model_number
84 config_get uuid config uuid
85 config_get notify_interval config notify_interval
86 config_get presentation_url config presentation_url
87 config_get upnp_lease_file config upnp_lease_file
88 config_get clean_ruleset_threshold config clean_ruleset_threshold
89 config_get clean_ruleset_interval config clean_ruleset_interval
90 config_get ipv6_listening_ip config ipv6_listening_ip
91
92 local args
93
94 . /lib/functions/network.sh
95
96 local ifname
97
98 # manual external interface overrides everything
99 if [ -z "$extiface" ] ; then
100 # manual external zone (if dynamically find interfaces
101 # belonging to it) overrides network_find_wan*
102 if [ -n "$extzone" ] ; then
103 ifname=$(fw3 -q zone $extzone | head -1)
104 fi
105 [ -n "$extiface" ] || network_find_wan extiface
106 [ -n "$extiface" ] || network_find_wan6 extiface
107 fi
108
109 [ -n "$ifname" ] || network_get_device ifname ${extiface}
110
111 if [ -n "$conffile" ]; then
112 args="-f $conffile"
113 else
114 local tmpconf="/var/etc/miniupnpd.conf"
115 args="-f $tmpconf"
116 mkdir -p /var/etc
117
118 echo "ext_ifname=$ifname" >$tmpconf
119
120 [ -n "$extip" ] && \
121 echo "ext_ip=$extip" >>$tmpconf
122
123 local iface
124 for iface in ${intiface:-lan}; do
125 local device
126 network_get_device device "$iface" && {
127 echo "listening_ip=$device" >>$tmpconf
128 }
129 done
130
131 [ "$port" != "auto" ] && \
132 echo "port=$port" >>$tmpconf
133
134 config_load "upnpd"
135 upnpd_write_bool enable_natpmp 1
136 upnpd_write_bool enable_upnp 1
137 upnpd_write_bool secure_mode 1
138 upnpd_write_bool pcp_allow_thirdparty 0
139 upnpd_write_bool system_uptime 1
140
141 [ -n "$upnp_lease_file" ] && \
142 echo "lease_file=$upnp_lease_file" >>$tmpconf
143
144 [ -n "$upload" -a -n "$download" ] && {
145 echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
146 echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
147 }
148
149 [ -n "${presentation_url}" ] && \
150 echo "presentation_url=${presentation_url}" >>$tmpconf
151
152 [ -n "${notify_interval}" ] && \
153 echo "notify_interval=${notify_interval}" >>$tmpconf
154
155 [ -n "${clean_ruleset_threshold}" ] && \
156 echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf
157
158 [ -n "${clean_ruleset_interval}" ] && \
159 echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$tmpconf
160
161 [ -n "${ipv6_listening_ip}" ] && \
162 echo "ipv6_listening_ip=${ipv6_listening_ip}" >>$tmpconf
163
164 [ -z "$uuid" ] && {
165 uuid="$(cat /proc/sys/kernel/random/uuid)"
166 uci set upnpd.config.uuid=$uuid
167 uci commit upnpd
168 }
169
170 [ "$uuid" = "nocli" ] || \
171 echo "uuid=$uuid" >>$tmpconf
172
173 [ -n "${serial_number}" ] && \
174 echo "serial=${serial_number}" >>$tmpconf
175
176 [ -n "${model_number}" ] && \
177 echo "model_number=${model_number}" >>$tmpconf
178
179 config_foreach conf_rule_add perm_rule "$tmpconf"
180 fi
181
182
183 if [ -n "$ifname" ]; then
184 # start firewall
185 iptables -L MINIUPNPD >/dev/null 2>/dev/null || fw3 reload
186
187 if [ "$logging" = "1" ]; then
188 SERVICE_DAEMONIZE=1 \
189 service_start /usr/sbin/miniupnpd $args -d
190 else
191 SERVICE_DAEMONIZE= \
192 service_start /usr/sbin/miniupnpd $args
193 fi
194 else
195 logger -t "upnp daemon" "external interface not found, not starting"
196 fi
197 }
198
199 stop() {
200 service_stop /usr/sbin/miniupnpd
201
202 iptables -t nat -F MINIUPNPD 2>/dev/null
203 iptables -t filter -F MINIUPNPD 2>/dev/null
204
205 [ -x /usr/sbin/ip6tables ] && {
206 ip6tables -t filter -F MINIUPNPD 2>/dev/null
207 }
208 }