summaryrefslogtreecommitdiffstats
path: root/net/miniupnpd/files/miniupnpd.init
blob: 9a85d0078bcf891b93e233e76a24284000527e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2014 OpenWrt.org

START=94
STOP=15

SERVICE_USE_PID=1

upnpd_get_port_range() {
	local var="$1"; shift
	local val

	config_get val "$@"

	case "$val" in
		[0-9]*[:-][0-9]*)
			export -n -- "${var}_start=${val%%[:-]*}"
			export -n -- "${var}_end=${val##*[:-]}"
		;;
		[0-9]*)
			export -n -- "${var}_start=$val"
			export -n -- "${var}_end="
		;;
	esac
}

conf_rule_add() {
	local cfg="$1"
	local tmpconf="$2"
	local action external_port_start external_port_end int_addr
	local internal_port_start internal_port_end comment

	config_get action "$cfg" action "deny"                # allow or deny
	upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
	config_get int_addr "$cfg" int_addr "0.0.0.0/0"       # ip or network and subnet mask (internal)
	upnpd_get_port_range "int" "$cfg" int_ports "0-65535" # internal ports: x, x-y, x:y or range
	config_get comment "$cfg" comment "ACL"		      # comment

	# Make a single IP IP/32 so that miniupnpd.conf can use it.
	[ "${int_addr%/*}" = "$int_addr" ] && int_addr="$int_addr/32"

	echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment" >>$tmpconf
}

upnpd_write_bool() {
	local opt="$1"
	local def="${2:-0}"
	local alt="${3:-$opt}"
	local val

	config_get_bool val config "$opt" "$def"
	if [ "$val" -eq 0 ]; then
		echo "$alt=no" >> $tmpconf
	else
		echo "$alt=yes" >> $tmpconf
	fi
}

boot() {
	return
}

start() {
	config_load "upnpd"
	local extiface intiface upload download logging secure enabled natpmp
	local extip port usesysuptime conffile serial_number model_number
	local uuid notify_interval presentation_url enable_upnp
	local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
	local ipv6_listening_ip enabled

	config_get_bool enabled config enabled 1

	[ "$enabled" -eq 0 ] && return 1

	config_get extiface config external_iface
	config_get extzone config external_zone
	config_get intiface config internal_iface
	config_get extip config external_ip
	config_get port config port 5000
	config_get upload config upload
	config_get download config download
	config_get_bool logging config log_output 0
	config_get conffile config config_file
	config_get serial_number config serial_number
	config_get model_number config model_number
	config_get uuid config uuid
	config_get notify_interval config notify_interval
	config_get presentation_url config presentation_url
	config_get upnp_lease_file config upnp_lease_file
	config_get clean_ruleset_threshold config clean_ruleset_threshold
	config_get clean_ruleset_interval config clean_ruleset_interval
	config_get ipv6_listening_ip config ipv6_listening_ip

	local args ifname

	. /lib/functions/network.sh

        # manual external interface overrides everything
        if [ -z "$extiface" ] ; then
            # manual external zone (if dynamically find interfaces
            # belonging to it) overrides network_find_wan*
            if [ -n "$extzone" ] ; then
                ifname=$(fw3 -q zone $extzone | head -1)
            fi
            [ -n "$extiface" ] || network_find_wan extiface
            [ -n "$extiface" ] || network_find_wan6 extiface
        fi

	[ -n "$ifname" ] || network_get_device ifname $extiface

	if [ -n "$conffile" ]; then
		args="-f $conffile"
	else
		local tmpconf="/var/etc/miniupnpd.conf"
		args="-f $tmpconf"
		mkdir -p /var/etc

		echo "ext_ifname=$ifname" >$tmpconf

		[ -n "$extip" ] && \
			echo "ext_ip=$extip" >>$tmpconf

		local iface
		for iface in ${intiface:-lan}; do
			local device
			network_get_device device "$iface" && {
				echo "listening_ip=$device" >>$tmpconf
			}
		done

		[ "$port" != "auto" ] && \
			echo "port=$port" >>$tmpconf

		config_load "upnpd"
		upnpd_write_bool enable_natpmp 1
		upnpd_write_bool enable_upnp 1
		upnpd_write_bool secure_mode 1
		upnpd_write_bool pcp_allow_thirdparty 0
		upnpd_write_bool system_uptime 1
		upnpd_write_bool igdv1 0 force_igd_desc_v1

		[ -n "$upnp_lease_file" ] && \
			echo "lease_file=$upnp_lease_file" >>$tmpconf

		[ -n "$upload" -a -n "$download" ] && {
			echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
			echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
		}

		[ -n "${presentation_url}" ] && \
			echo "presentation_url=${presentation_url}" >>$tmpconf

		[ -n "${notify_interval}" ] && \
			echo "notify_interval=${notify_interval}" >>$tmpconf

		[ -n "${clean_ruleset_threshold}" ] && \
			echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf

		[ -n "${clean_ruleset_interval}" ] && \
			echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$tmpconf

		[ -n "${ipv6_listening_ip}" ] && \
			echo "ipv6_listening_ip=${ipv6_listening_ip}" >>$tmpconf

		[ -z "$uuid" ] && {
			uuid="$(cat /proc/sys/kernel/random/uuid)"
			uci set upnpd.config.uuid=$uuid
			uci commit upnpd
		}

		[ "$uuid" = "nocli" ] || \
			echo "uuid=$uuid" >>$tmpconf

		[ -n "${serial_number}" ] && \
			echo "serial=${serial_number}" >>$tmpconf

		[ -n "${model_number}" ] && \
			echo "model_number=${model_number}" >>$tmpconf

		config_foreach conf_rule_add perm_rule "$tmpconf"
	fi


	if [ -n "$ifname" ]; then
		# start firewall
		iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload

		if [ "$logging" = "1" ]; then
			SERVICE_DAEMONIZE=1 \
			service_start /usr/sbin/miniupnpd $args -d
		else
			SERVICE_DAEMONIZE= \
			service_start /usr/sbin/miniupnpd $args
		fi
	else
		logger -t "upnp daemon" "external interface not found, not starting"
	fi
}

stop() {
	service_stop /usr/sbin/miniupnpd

	iptables -t nat -F MINIUPNPD 2>/dev/null
	iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
	iptables -t filter -F MINIUPNPD 2>/dev/null

        [ -x /usr/sbin/ip6tables ] && {
	    ip6tables -t filter -F MINIUPNPD 2>/dev/null
        }
}