olsrd: Merge branch 'olsrd_6and4' by Alessio Caiazza.
[lede-routing/.git] / 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 boot() {
61         return
62 }
63
64 start() {
65         config_load "upnpd"
66         local extiface intiface upload download logging secure enabled natpmp
67         local extip port usesysuptime conffile serial_number model_number
68         local uuid notify_interval presentation_url enable_upnp
69         local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
70         local ipv6_listening_ip
71
72         config_get extiface config external_iface
73         config_get intiface config internal_iface
74         config_get extip config external_ip
75         config_get port config port 5000
76         config_get upload   config upload
77         config_get download config download
78         config_get_bool logging config log_output 0
79         config_get conffile config config_file
80         config_get serial_number config serial_number
81         config_get model_number config model_number
82         config_get uuid config uuid
83         config_get notify_interval config notify_interval
84         config_get presentation_url config presentation_url
85         config_get upnp_lease_file config upnp_lease_file
86         config_get clean_ruleset_threshold config clean_ruleset_threshold
87         config_get clean_ruleset_interval config clean_ruleset_interval
88         config_get ipv6_listening_ip config ipv6_listening_ip
89
90         local args
91
92         . /lib/functions/network.sh
93
94         local ifname
95
96         [ -n "$extiface" ] || network_find_wan extiface
97         [ -n "$extiface" ] || network_find_wan6 extiface
98
99         network_get_device ifname ${extiface}
100
101         if [ -n "$conffile" ]; then
102                 args="-f $conffile"
103         else
104                 local tmpconf="/var/etc/miniupnpd.conf"
105                 args="-f $tmpconf"
106                 mkdir -p /var/etc
107
108                 echo "ext_ifname=$ifname" >$tmpconf
109
110                 [ -n "$extip" ] && \
111                         echo "ext_ip=$extip" >>$tmpconf
112
113                 local iface
114                 for iface in ${intiface:-lan}; do
115                         local device
116                         network_get_device device "$iface" && {
117                                 echo "listening_ip=$device" >>$tmpconf
118                         }
119                 done
120
121                 [ "$port" != "auto" ] && \
122                         echo "port=$port" >>$tmpconf
123
124                 config_load "upnpd"
125                 upnpd_write_bool enable_natpmp 1
126                 upnpd_write_bool enable_upnp 1
127                 upnpd_write_bool secure_mode 1
128                 upnpd_write_bool pcp_allow_thirdparty 0
129                 upnpd_write_bool system_uptime 1
130
131                 [ -n "$upnp_lease_file" ] && \
132                         echo "lease_file=$upnp_lease_file" >>$tmpconf
133
134                 [ -n "$upload" -a -n "$download" ] && {
135                         echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
136                         echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
137                 }
138
139                 [ -n "${presentation_url}" ] && \
140                         echo "presentation_url=${presentation_url}" >>$tmpconf
141
142                 [ -n "${notify_interval}" ] && \
143                         echo "notify_interval=${notify_interval}" >>$tmpconf
144
145                 [ -n "${clean_ruleset_threshold}" ] && \
146                         echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf
147
148                 [ -n "${clean_ruleset_interval}" ] && \
149                         echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$tmpconf
150
151                 [ -n "${ipv6_listening_ip}" ] && \
152                         echo "ipv6_listening_ip=${ipv6_listening_ip}" >>$tmpconf
153
154                 [ -z "$uuid" ] && {
155                         uuid="$(cat /proc/sys/kernel/random/uuid)"
156                         uci set upnpd.config.uuid=$uuid
157                         uci commit upnpd
158                 }
159
160                 [ "$uuid" = "nocli" ] || \
161                         echo "uuid=$uuid" >>$tmpconf
162
163                 [ -n "${serial_number}" ] && \
164                         echo "serial=${serial_number}" >>$tmpconf
165
166                 [ -n "${model_number}" ] && \
167                         echo "model_number=${model_number}" >>$tmpconf
168
169             config_foreach conf_rule_add perm_rule "$tmpconf"
170         fi
171
172
173         if [ -n "$ifname" ]; then
174                 # start firewall
175                 iptables -L MINIUPNPD >/dev/null 2>/dev/null || fw3 reload
176
177                 if [ "$logging" = "1" ]; then
178                         SERVICE_DAEMONIZE=1 \
179                         service_start /usr/sbin/miniupnpd $args -d
180                 else
181                         SERVICE_DAEMONIZE= \
182                         service_start /usr/sbin/miniupnpd $args
183                 fi
184         else
185                 logger -t "upnp daemon" "external interface not found, not starting"
186         fi
187 }
188
189 stop() {
190         service_stop /usr/sbin/miniupnpd
191
192         iptables -t nat -F MINIUPNPD 2>/dev/null
193         iptables -t filter -F MINIUPNPD 2>/dev/null
194
195         [ -x /usr/sbin/ip6tables ] && {
196             ip6tables -t filter -F MINIUPNPD 2>/dev/null
197         }
198 }