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