[packages] miniupnpd: convert netmask to prefix and write listen address in CIDR...
[packages.git] / net / miniupnpd / files / miniupnpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2011 OpenWrt.org
3
4 START=95
5
6 SERVICE_USE_PID=1
7
8 upnpd_get_port_range() {
9         local _var="$1"; shift
10         local _val
11
12         config_get _val "$@"
13
14         case "$_val" in
15                 [0-9]*[:-][0-9]*)
16                         export -n -- "${_var}_start=${_val%%[:-]*}"
17                         export -n -- "${_var}_end=${_val##*[:-]}"
18                 ;;
19                 [0-9]*)
20                         export -n -- "${_var}_start=$_val"
21                         export -n -- "${_var}_end="
22                 ;;
23         esac
24 }
25
26 conf_rule_add() {
27         local cfg="$1"
28         local tmpconf="$2"
29         local action external_port_start external_port_end int_addr
30         local internal_port_start internal_port_end
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
37         # Make a single IP IP/32 so that miniupnpd.conf can use it.
38         case "$int_addr" in
39                 */*) ;;
40                 *) int_addr="$int_addr/32" ;;
41         esac
42
43         echo "${action} ${ext_start}${ext_end:+-}${ext_end} ${int_addr} ${int_start}${int_end:+-}${int_end}" >>$tmpconf
44 }
45
46 upnpd_write_bool() {
47         local opt="$1"
48         local def="${2:-0}"
49         local alt="$3"
50         local val
51
52         config_get_bool val config "$opt" "$def"
53         if [ "$val" -eq 0 ]; then
54                 echo "${alt:-$opt}=no" >> $tmpconf
55         else
56                 echo "${alt:-$opt}=yes" >> $tmpconf
57         fi
58 }
59
60 upnpd_netmask2prefix() {
61         local octet
62         local prefix=0
63         local IFS="."
64
65         set -- $1
66
67         for octet in $1 $2 $3 $4; do
68                 while [ $octet -gt 0 ]; do
69                         prefix=$(($prefix + ($octet & 1)))
70                         octet=$(($octet >> 1))
71                 done
72         done
73
74         return $prefix
75 }
76
77 start() {
78         type miniupnpd_add_rules >/dev/null 2>/dev/null || \
79                 ACTION=- . /etc/hotplug.d/firewall/50-miniupnpd
80
81         config_load "upnpd"
82         local extiface intiface upload download logging secure enabled natpmp
83         local extip port usesysuptime conffile serial_number model_number
84         local uuid notify_interval presentation_url enable_upnp
85         local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
86
87         config_get extiface config external_iface
88         config_get intiface config internal_iface
89         config_get extip config external_ip
90         config_get port config port 5000
91         config_get upload   config upload
92         config_get download config download
93         config_get_bool logging config log_output 0
94         config_get conffile config config_file
95         config_get serial_number config serial_number
96         config_get model_number config model_number
97         config_get uuid config uuid
98         config_get notify_interval config notify_interval
99         config_get presentation_url config presentation_url
100         config_get upnp_lease_file config upnp_lease_file
101         config_get clean_ruleset_threshold config clean_ruleset_threshold
102         config_get clean_ruleset_interval config clean_ruleset_interval
103
104         local args
105
106         include /lib/network
107         scan_interfaces
108
109         local ifname
110         config_get ifname ${extiface:-wan} ifname
111
112         if [ -n "$conffile" ]; then
113                 args="-f $conffile"
114         else
115                 local tmpconf="/var/etc/miniupnpd.conf"
116                 args="-f $tmpconf"
117                 mkdir -p /var/etc
118
119                 echo "ext_ifname=$ifname" >$tmpconf
120
121                 [ -n "$extip" ] && \
122                         echo "ext_ip=$extip" >>$tmpconf
123
124                 local iface
125                 for iface in ${intiface:-lan}; do
126                         local ipaddr netmask
127                         config_get ipaddr "$iface" ipaddr
128                         config_get netmask "$iface" netmask "255.255.255.0"
129                         [ -n "$ipaddr" ] && {
130                                 upnpd_netmask2prefix "$netmask"
131                                 echo "listening_ip=$ipaddr/$?" >>$tmpconf
132                         }
133                 done
134
135                 [ "$port" != "auto" ] && \
136                         echo "port=$port" >>$tmpconf
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
144                 [ -n "$upnp_lease_file" ] && \
145                         echo "lease_file=$upnp_lease_file" >>$tmpconf
146
147                 [ -n "$upload" -a -n "$download" ] && {
148                         echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
149                         echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
150                 }
151
152                 [ -n "${presentation_url}" ] && \
153                         echo "presentation_url=${presentation_url}" >>$tmpconf
154
155                 [ -n "${notify_interval}" ] && \
156                         echo "notify_interval=${notify_interval}" >>$tmpconf
157
158                 [ -n "${clean_ruleset_threshold}" ] && \
159                         echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf
160
161                 [ -n "${clean_ruleset_interval}" ] && \
162                         echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$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                 local zone
186                 config_load firewall
187                 config_get zone core "${extiface:-wan}_zone"
188
189             [ -n "$zone" ] && \
190                         miniupnpd_add_rules "$zone" "${extiface:-wan}" "$ifname"
191
192                 if [ "$logging" = "1" ]; then
193                         SERVICE_DAEMONIZE=1 \
194                         service_start /usr/sbin/miniupnpd $args -d
195                 else
196                         SERVICE_DAEMONIZE= \
197                         service_start /usr/sbin/miniupnpd $args
198                 fi
199         else
200                 logger -t "upnp daemon" "external interface not found, not starting"
201         fi
202 }
203
204 stop() {
205         service_stop /usr/sbin/miniupnpd
206
207         type miniupnpd_remove_rules >/dev/null 2>/dev/null || \
208                 ACTION=- . /etc/hotplug.d/firewall/50-miniupnpd
209
210         miniupnpd_remove_rules
211
212         iptables -t nat -F MINIUPNPD 2>/dev/null
213         iptables -t nat -X MINIUPNPD 2>/dev/null
214         iptables -t filter -F MINIUPNPD 2>/dev/null
215         iptables -t filter -X MINIUPNPD 2>/dev/null
216 }