@@ -7,34 +7,56 @@ USE_PROCD=1
77PROG=/usr/sbin/miniupnpd
88[ -x " $( command -v nft) " ] && FW=" fw4" || FW=" fw3"
99
10- is_port_or_range () {
11- [ " $1 " = " 0" ] && return 1
12- [ " $1 " -ge " 1" ] 2> /dev/null && [ " $1 " -le " 65535" ] 2> /dev/null && return 0
13- [ " $2 " = " port0inrange" ] && local minport=0 || local minport=1
14- [ " ${1%% -* } " -ge " $minport " ] 2> /dev/null && [ " ${1%% -* } " -le " 65535" ] 2> /dev/null &&
15- [ " ${1##* -} " -ge " $minport " ] 2> /dev/null && [ " ${1##* -} " -le " 65535" ] 2> /dev/null &&
16- [ " ${1##* -} " -ge " ${1%% -* } " ] 2> /dev/null && return 0 || return 1
10+ start_service () {
11+ config_load " upnpd"
12+ local enabled config_file log_output conf
13+ config_get enabled settings enabled 0
14+ config_get config_file settings config_file
15+ config_get log_output settings log_output
16+ if [ " $enabled " != " 1" ]; then
17+ log " Service disabled, UCI enabled is not set"
18+ return 1
19+ fi
20+
21+ if [ -n " $config_file " ]; then
22+ conf=" $config_file "
23+ else
24+ local tmpconf=" /var/etc/miniupnpd.conf"
25+ conf=" $tmpconf "
26+ mkdir -p /var/etc
27+ upnpd_generate_config " $tmpconf " || return 1
28+ fi
29+
30+ if [ " $FW " = " fw4" ]; then
31+ nft -s -t -n list chain inet fw4 upnp_forward > /dev/null 2>&1 || fw4 reload
32+ else
33+ iptables -L MINIUPNPD > /dev/null 2>&1 || fw3 reload
34+ fi
35+
36+ procd_open_instance
37+ procd_set_param file " $conf "
38+ procd_set_param command " $PROG "
39+ procd_append_param command -f " $conf "
40+ [ " $log_output " = " info" ] && procd_append_param command -v
41+ [ " $log_output " = " debug" ] && procd_append_param command -d
42+ procd_close_instance
1743}
1844
19- upnpd_add_custom_acl_entry () {
20- local cfg=" $1 "
21- local comment int_addr int_port ext_port descr_filter action
22- config_get comment " $cfg " comment " unspecified" # comment
23- config_get int_addr " $cfg " int_addr " 0.0.0.0/0" # IPv4 or network and subnet mask (internal)
24- config_get int_port " $cfg " int_port " 1-65535" # internal port/range: x or x-y
25- config_get ext_port " $cfg " ext_port " 1-65535" # external port/range: x or x-y
26- config_get descr_filter " $cfg " descr_filter # description regex filter (must be built in)
27- config_get action " $cfg " action # accept/reject/ignore
28- ! is_port_or_range " $int_port " port0inrange &&
29- log " Custom ACL: Entry with invalid port or port range ($int_port ) in int_port ignored" daemon.warn && int_port=1-65535
30- ! is_port_or_range " $ext_port " port0inrange &&
31- log " Custom ACL: Entry with invalid port or port range ($ext_port ) in ext_port ignored" daemon.warn && ext_port=1-65535
32- [ " $descr_filter " != " " ] && descr_filter=" \" $descr_filter \" "
33- [ " $action " = " accept" ] && action=allow
34- [ " $action " = " reject" ] && action=deny
35- [ " $action " = " ignore" ] && return 0
36- [ " $action " = " " ] && log " Custom ACL: Entry with no action ignored" daemon.warn && return 0
37- echo " $action $ext_port $int_addr $int_port ${descr_filter} # $comment "
45+ stop_service () {
46+ if [ " $FW " = " fw3" ]; then
47+ iptables -t filter -F MINIUPNPD 2> /dev/null
48+ [ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2> /dev/null
49+ iptables -t nat -F MINIUPNPD 2> /dev/null
50+ iptables -t nat -F MINIUPNPD-POSTROUTING 2> /dev/null
51+ else
52+ nft flush chain inet fw4 upnp_forward 2> /dev/null
53+ nft flush chain inet fw4 upnp_prerouting 2> /dev/null
54+ nft flush chain inet fw4 upnp_postrouting 2> /dev/null
55+ fi
56+ }
57+
58+ service_triggers () {
59+ procd_add_reload_trigger " upnpd" " firewall"
3860}
3961
4062upnpd_generate_config () {
@@ -166,58 +188,6 @@ upnpd_generate_config() {
166188 } > " $1 "
167189}
168190
169- stop_service () {
170- if [ " $FW " = " fw3" ]; then
171- iptables -t filter -F MINIUPNPD 2> /dev/null
172- [ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2> /dev/null
173- iptables -t nat -F MINIUPNPD 2> /dev/null
174- iptables -t nat -F MINIUPNPD-POSTROUTING 2> /dev/null
175- else
176- nft flush chain inet fw4 upnp_forward 2> /dev/null
177- nft flush chain inet fw4 upnp_prerouting 2> /dev/null
178- nft flush chain inet fw4 upnp_postrouting 2> /dev/null
179- fi
180- }
181-
182- start_service () {
183- config_load " upnpd"
184- local enabled config_file log_output conf
185- config_get enabled settings enabled 0
186- config_get config_file settings config_file
187- config_get log_output settings log_output
188- if [ " $enabled " != " 1" ]; then
189- log " Service disabled, UCI enabled is not set"
190- return 1
191- fi
192-
193- if [ -n " $config_file " ]; then
194- conf=" $config_file "
195- else
196- local tmpconf=" /var/etc/miniupnpd.conf"
197- conf=" $tmpconf "
198- mkdir -p /var/etc
199- upnpd_generate_config " $tmpconf " || return 1
200- fi
201-
202- if [ " $FW " = " fw4" ]; then
203- nft -s -t -n list chain inet fw4 upnp_forward > /dev/null 2>&1 || fw4 reload
204- else
205- iptables -L MINIUPNPD > /dev/null 2>&1 || fw3 reload
206- fi
207-
208- procd_open_instance
209- procd_set_param file " $conf "
210- procd_set_param command " $PROG "
211- procd_append_param command -f " $conf "
212- [ " $log_output " = " info" ] && procd_append_param command -v
213- [ " $log_output " = " debug" ] && procd_append_param command -d
214- procd_close_instance
215- }
216-
217- service_triggers () {
218- procd_add_reload_trigger " upnpd" " firewall"
219- }
220-
221191log () {
222192 logger -s -p " ${2:- daemon.notice} " -t " upnpd" " $1 " || echo " upnpd: $1 " >&2
223193}
@@ -227,6 +197,15 @@ xml_encode() {
227197 echo " $1 " | sed " s/&/\&/g; s/</\</g; s/>/\>/g"
228198}
229199
200+ is_port_or_range () {
201+ [ " $1 " = " 0" ] && return 1
202+ [ " $1 " -ge " 1" ] 2> /dev/null && [ " $1 " -le " 65535" ] 2> /dev/null && return 0
203+ [ " $2 " = " port0inrange" ] && local minport=0 || local minport=1
204+ [ " ${1%% -* } " -ge " $minport " ] 2> /dev/null && [ " ${1%% -* } " -le " 65535" ] 2> /dev/null &&
205+ [ " ${1##* -} " -ge " $minport " ] 2> /dev/null && [ " ${1##* -} " -le " 65535" ] 2> /dev/null &&
206+ [ " ${1##* -} " -ge " ${1%% -* } " ] 2> /dev/null && return 0 || return 1
207+ }
208+
230209upnpd_add_int_network_and_preset () {
231210 local cfg=" $1 "
232211 local interface access_preset accept_ports reject_ports custom_acl_before
@@ -271,3 +250,24 @@ upnpd_add_int_network_and_preset() {
271250 echo " deny 1-65535 $subnet 1-65535 # Reject ACL by default on $interface "
272251 fi
273252}
253+
254+ upnpd_add_custom_acl_entry () {
255+ local cfg=" $1 "
256+ local comment int_addr int_port ext_port descr_filter action
257+ config_get comment " $cfg " comment " unspecified" # comment
258+ config_get int_addr " $cfg " int_addr " 0.0.0.0/0" # IPv4 or network and subnet mask (internal)
259+ config_get int_port " $cfg " int_port " 1-65535" # internal port/range: x or x-y
260+ config_get ext_port " $cfg " ext_port " 1-65535" # external port/range: x or x-y
261+ config_get descr_filter " $cfg " descr_filter # description regex filter (must be built in)
262+ config_get action " $cfg " action # accept/reject/ignore
263+ ! is_port_or_range " $int_port " port0inrange &&
264+ log " Custom ACL: Entry with invalid port or port range ($int_port ) in int_port ignored" daemon.warn && int_port=1-65535
265+ ! is_port_or_range " $ext_port " port0inrange &&
266+ log " Custom ACL: Entry with invalid port or port range ($ext_port ) in ext_port ignored" daemon.warn && ext_port=1-65535
267+ [ " $descr_filter " != " " ] && descr_filter=" \" $descr_filter \" "
268+ [ " $action " = " accept" ] && action=allow
269+ [ " $action " = " reject" ] && action=deny
270+ [ " $action " = " ignore" ] && return 0
271+ [ " $action " = " " ] && log " Custom ACL: Entry with no action ignored" daemon.warn && return 0
272+ echo " $action $ext_port $int_addr $int_port ${descr_filter} # $comment "
273+ }
0 commit comments