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