fwknop: init script improvements
[feed/packages.git] / net / miniupnpd / files / miniupnpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2014 OpenWrt.org
3
4 START=94
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 comment
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 config_get comment "$cfg" comment "ACL" # comment
38
39 # Make a single IP IP/32 so that miniupnpd.conf can use it.
40 [ "${int_addr%/*}" = "$int_addr" ] && int_addr="$int_addr/32"
41
42 echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment" >>$tmpconf
43 }
44
45 upnpd_write_bool() {
46 local opt="$1"
47 local def="${2:-0}"
48 local alt="${3:-$opt}"
49 local val
50
51 config_get_bool val config "$opt" "$def"
52 if [ "$val" -eq 0 ]; then
53 echo "$alt=no" >> $tmpconf
54 else
55 echo "$alt=yes" >> $tmpconf
56 fi
57 }
58
59 boot() {
60 return
61 }
62
63 start() {
64 config_load "upnpd"
65 local extiface intiface upload download logging secure enabled natpmp
66 local extip port usesysuptime conffile serial_number model_number
67 local uuid notify_interval presentation_url enable_upnp
68 local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
69 local ipv6_listening_ip enabled
70
71 config_get_bool enabled config enabled 1
72
73 [ "$enabled" -eq 0 ] && return 1
74
75 config_get extiface config external_iface
76 config_get extzone config external_zone
77 config_get intiface config internal_iface
78 config_get extip config external_ip
79 config_get port config port 5000
80 config_get upload config upload
81 config_get download config download
82 config_get_bool logging config log_output 0
83 config_get conffile config config_file
84 config_get serial_number config serial_number
85 config_get model_number config model_number
86 config_get uuid config uuid
87 config_get notify_interval config notify_interval
88 config_get presentation_url config presentation_url
89 config_get upnp_lease_file config upnp_lease_file
90 config_get clean_ruleset_threshold config clean_ruleset_threshold
91 config_get clean_ruleset_interval config clean_ruleset_interval
92 config_get ipv6_listening_ip config ipv6_listening_ip
93
94 local args ifname
95
96 . /lib/functions/network.sh
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 upnpd_write_bool igdv1 0 force_igd_desc_v1
141
142 [ -n "$upnp_lease_file" ] && \
143 echo "lease_file=$upnp_lease_file" >>$tmpconf
144
145 [ -n "$upload" -a -n "$download" ] && {
146 echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
147 echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
148 }
149
150 [ -n "${presentation_url}" ] && \
151 echo "presentation_url=${presentation_url}" >>$tmpconf
152
153 [ -n "${notify_interval}" ] && \
154 echo "notify_interval=${notify_interval}" >>$tmpconf
155
156 [ -n "${clean_ruleset_threshold}" ] && \
157 echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf
158
159 [ -n "${clean_ruleset_interval}" ] && \
160 echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$tmpconf
161
162 [ -n "${ipv6_listening_ip}" ] && \
163 echo "ipv6_listening_ip=${ipv6_listening_ip}" >>$tmpconf
164
165 [ -z "$uuid" ] && {
166 uuid="$(cat /proc/sys/kernel/random/uuid)"
167 uci set upnpd.config.uuid=$uuid
168 uci commit upnpd
169 }
170
171 [ "$uuid" = "nocli" ] || \
172 echo "uuid=$uuid" >>$tmpconf
173
174 [ -n "${serial_number}" ] && \
175 echo "serial=${serial_number}" >>$tmpconf
176
177 [ -n "${model_number}" ] && \
178 echo "model_number=${model_number}" >>$tmpconf
179
180 config_foreach conf_rule_add perm_rule "$tmpconf"
181 fi
182
183
184 if [ -n "$ifname" ]; then
185 # start firewall
186 iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
187
188 if [ "$logging" = "1" ]; then
189 SERVICE_DAEMONIZE=1 \
190 service_start /usr/sbin/miniupnpd $args -d
191 else
192 SERVICE_DAEMONIZE= \
193 service_start /usr/sbin/miniupnpd $args
194 fi
195 else
196 logger -t "upnp daemon" "external interface not found, not starting"
197 fi
198 }
199
200 stop() {
201 service_stop /usr/sbin/miniupnpd
202
203 iptables -t nat -F MINIUPNPD 2>/dev/null
204 iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
205 iptables -t filter -F MINIUPNPD 2>/dev/null
206
207 [ -x /usr/sbin/ip6tables ] && {
208 ip6tables -t filter -F MINIUPNPD 2>/dev/null
209 }
210 }