Merge pull request #14146 from neheb/summ
[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 USE_PROCD=1
7 PROG=/usr/sbin/miniupnpd
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 action int_addr
30 local ext_start ext_end int_start int_end comment
31
32 config_get action "$cfg" action "deny" # allow or deny
33 upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
34 config_get int_addr "$cfg" int_addr "0.0.0.0/0" # ip or network and subnet mask (internal)
35 upnpd_get_port_range "int" "$cfg" int_ports "0-65535" # internal ports: x, x-y, x:y or range
36 config_get comment "$cfg" comment "ACL" # comment
37
38 # Make a single IP IP/32 so that miniupnpd.conf can use it.
39 [ "${int_addr%/*}" = "$int_addr" ] && int_addr="$int_addr/32"
40
41 echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment"
42 }
43
44 upnpd_write_bool() {
45 local opt="$1"
46 local def="${2:-0}"
47 local alt="${3:-$opt}"
48 local val
49
50 config_get_bool val config "$opt" "$def"
51 if [ "$val" -eq 0 ]; then
52 echo "$alt=no"
53 else
54 echo "$alt=yes"
55 fi
56 }
57
58 upnpd() {
59 config_load "upnpd"
60 local external_iface external_iface6 external_zone external_ip internal_iface
61 local upload download log_output port config_file serial_number model_number
62 local use_stun stun_host stun_port uuid notify_interval presentation_url
63 local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
64 local ipv6_disable
65 local ext_ip_reserved_ignore
66
67 local enabled
68 config_get_bool enabled config enabled 1
69 [ "$enabled" -eq 0 ] && return 1
70
71 config_get external_iface config external_iface
72 config_get external_iface6 config external_iface6
73 config_get external_zone config external_zone
74 config_get external_ip config external_ip
75 config_get internal_iface config internal_iface
76 config_get port config port 5000
77 config_get upload config upload
78 config_get download config download
79 config_get_bool log_output config log_output 0
80 config_get config_file config config_file
81 config_get serial_number config serial_number
82 config_get model_number config model_number
83 config_get uuid config uuid
84 config_get use_stun config use_stun 0
85 config_get stun_host config stun_host
86 config_get stun_port config stun_port
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_disable config ipv6_disable 0
93 config_get ext_ip_reserved_ignore config ext_ip_reserved_ignore 0
94
95 local conf ifname ifname6
96
97 . /lib/functions/network.sh
98
99 if [ -n "$external_iface" ] ; then
100 network_get_device ifname "$external_iface"
101 else
102 if [ -n "$external_zone" ] ; then
103 ifname=$(fw3 -q zone "$external_zone" 2>/dev/null | head -1)
104 else
105 network_find_wan external_iface && \
106 network_get_device ifname "$external_iface"
107 fi
108 fi
109 if [ -n "$external_iface6" ] ; then
110 network_get_device ifname6 "$external_iface6"
111 else
112 if [ -n "$external_zone" ] ; then
113 ifname6=$(fw3 -q zone "$external_zone" 2>/dev/null | head -1)
114 else
115 network_find_wan6 external_iface6 && \
116 network_get_device ifname6 "$external_iface6"
117 fi
118 fi
119
120 if [ -n "$config_file" ]; then
121 conf="$config_file"
122 else
123 local tmpconf="/var/etc/miniupnpd.conf"
124 conf="$tmpconf"
125 mkdir -p /var/etc
126
127 {
128 echo "ext_ifname=$ifname"
129 echo "ext_ifname6=$ifname6"
130 [ -n "$external_ip" ] && echo "ext_ip=$external_ip"
131
132 local iface
133 for iface in ${internal_iface:-lan}; do
134 local device
135 network_get_device device "$iface" && echo "listening_ip=$device"
136 done
137
138 config_load "upnpd"
139 upnpd_write_bool enable_natpmp 1
140 upnpd_write_bool enable_upnp 1
141 upnpd_write_bool secure_mode 1
142 upnpd_write_bool system_uptime 1
143 upnpd_write_bool igdv1 0 force_igd_desc_v1
144 upnpd_write_bool use_stun 0 ext_perform_stun
145 upnpd_write_bool ipv6_disable $ipv6_disable
146 upnpd_write_bool ext_ip_reserved_ignore $ext_ip_reserved_ignore
147
148 [ "$use_stun" -eq 0 ] || {
149 [ -n "$stun_host" ] && echo "ext_stun_host=$stun_host"
150 [ -n "$stun_port" ] && echo "ext_stun_port=$stun_port"
151 }
152
153 [ -n "$upload" ] && [ -n "$download" ] && {
154 echo "bitrate_down=$((download * 1024 * 8))"
155 echo "bitrate_up=$((upload * 1024 * 8))"
156 }
157
158 [ -n "$upnp_lease_file" ] && touch "$upnp_lease_file" && echo "lease_file=$upnp_lease_file"
159 [ -n "$presentation_url" ] && echo "presentation_url=$presentation_url"
160 [ -n "$notify_interval" ] && echo "notify_interval=$notify_interval"
161 [ -n "$clean_ruleset_threshold" ] && echo "clean_ruleset_threshold=$clean_ruleset_threshold"
162 [ -n "$clean_ruleset_interval" ] && echo "clean_ruleset_interval=$clean_ruleset_interval"
163 [ -n "$serial_number" ] && echo "serial=$serial_number"
164 [ -n "$model_number" ] && echo "model_number=$model_number"
165 [ -n "$port" ] && echo "port=$port"
166
167 [ -z "$uuid" ] && {
168 uuid="$(cat /proc/sys/kernel/random/uuid)"
169 uci set upnpd.config.uuid="$uuid"
170 uci commit upnpd
171 }
172
173 [ "$uuid" = "nocli" ] || echo "uuid=$uuid"
174
175 config_foreach conf_rule_add perm_rule
176
177 } > "$tmpconf"
178 fi
179
180 if [ -n "$ifname" ]; then
181 # start firewall
182 iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
183 else
184 logger -t "upnp daemon" "external interface not found, not starting"
185 fi
186
187 procd_open_instance
188 procd_set_param command "$PROG"
189 procd_append_param command -f "$conf"
190 [ "$log_output" = "1" ] && procd_append_param command -d
191 procd_close_instance
192 }
193
194 stop_service() {
195 iptables -t nat -F MINIUPNPD 2>/dev/null
196 iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
197 iptables -t filter -F MINIUPNPD 2>/dev/null
198
199 [ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2>/dev/null
200 }
201
202 start_service() {
203 config_load "upnpd"
204 config_foreach upnpd "upnpd"
205 }
206
207 service_triggers() {
208 procd_add_reload_trigger "upnpd"
209 }