Skip to content

Commit fe42ffe

Browse files
miniupnpd: rearrange init functions
Arrange `start_service` and main init functions first (to merge with prior) Signed-off-by: Self-Hosting-Group <[email protected]>
1 parent 433e3ed commit fe42ffe

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

net/miniupnpd/files/miniupnpd.init

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,52 @@ USE_PROCD=1
77
PROG=/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+
upnpd_uci_migration
12+
config_load "upnpd"
13+
local enabled config_file log_output conf
14+
config_get enabled settings enabled 0
15+
config_get config_file settings config_file
16+
config_get log_output settings log_output
17+
[ "$enabled" != "1" ] && log "Service disabled, UCI enabled is not set" && exit 1
18+
if [ -n "$config_file" ]; then
19+
conf="$config_file"
20+
else
21+
local tmpconf="/var/etc/miniupnpd.conf"
22+
conf="$tmpconf"
23+
mkdir -p /var/etc
24+
upnpd_generate_config "$tmpconf"
25+
fi
26+
if [ "$FW" = "fw4" ]; then
27+
nft -s -t -n list chain inet fw4 upnp_forward >/dev/null 2>&1 || fw4 reload
28+
else
29+
iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
30+
fi
31+
32+
procd_open_instance
33+
procd_set_param file "$conf" "/etc/config/firewall"
34+
procd_set_param command "$PROG"
35+
procd_append_param command -f "$conf"
36+
[ "$log_output" = "info" ] && procd_append_param command -v
37+
[ "$log_output" = "debug" ] && procd_append_param command -d
38+
procd_close_instance
1739
}
1840

19-
upnpd_add_custom_acl_entry() {
20-
local cfg="$1"
21-
local comment int_addr int_port ext_port desc_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 desc_filter "$cfg" desc_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-
[ "$desc_filter" != "" ] && desc_filter=" \"$desc_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${desc_filter} # $comment"
41+
stop_service() {
42+
if [ "$FW" = "fw3" ]; then
43+
iptables -t nat -F MINIUPNPD 2>/dev/null
44+
iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
45+
iptables -t filter -F MINIUPNPD 2>/dev/null
46+
[ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2>/dev/null
47+
else
48+
nft flush chain inet fw4 upnp_forward 2>/dev/null
49+
nft flush chain inet fw4 upnp_prerouting 2>/dev/null
50+
nft flush chain inet fw4 upnp_postrouting 2>/dev/null
51+
fi
52+
}
53+
54+
service_triggers() {
55+
procd_add_reload_trigger "upnpd"
3856
}
3957

4058
upnpd_generate_config() {
@@ -161,54 +179,6 @@ upnpd_generate_config() {
161179
} >"$1"
162180
}
163181

164-
stop_service() {
165-
if [ "$FW" = "fw3" ]; then
166-
iptables -t nat -F MINIUPNPD 2>/dev/null
167-
iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
168-
iptables -t filter -F MINIUPNPD 2>/dev/null
169-
[ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2>/dev/null
170-
else
171-
nft flush chain inet fw4 upnp_forward 2>/dev/null
172-
nft flush chain inet fw4 upnp_prerouting 2>/dev/null
173-
nft flush chain inet fw4 upnp_postrouting 2>/dev/null
174-
fi
175-
}
176-
177-
start_service() {
178-
upnpd_uci_migration
179-
config_load "upnpd"
180-
local enabled config_file log_output conf
181-
config_get enabled settings enabled 0
182-
config_get config_file settings config_file
183-
config_get log_output settings log_output
184-
[ "$enabled" != "1" ] && log "Service disabled, UCI enabled is not set" && exit 1
185-
if [ -n "$config_file" ]; then
186-
conf="$config_file"
187-
else
188-
local tmpconf="/var/etc/miniupnpd.conf"
189-
conf="$tmpconf"
190-
mkdir -p /var/etc
191-
upnpd_generate_config "$tmpconf"
192-
fi
193-
if [ "$FW" = "fw4" ]; then
194-
nft -s -t -n list chain inet fw4 upnp_forward >/dev/null 2>&1 || fw4 reload
195-
else
196-
iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
197-
fi
198-
199-
procd_open_instance
200-
procd_set_param file "$conf" "/etc/config/firewall"
201-
procd_set_param command "$PROG"
202-
procd_append_param command -f "$conf"
203-
[ "$log_output" = "info" ] && procd_append_param command -v
204-
[ "$log_output" = "debug" ] && procd_append_param command -d
205-
procd_close_instance
206-
}
207-
208-
service_triggers() {
209-
procd_add_reload_trigger "upnpd"
210-
}
211-
212182
log() {
213183
logger -s -p "${2:-daemon.notice}" -t "upnpd" "$1" || echo "upnpd: $1" >&2
214184
}
@@ -218,6 +188,15 @@ xml_encode() {
218188
echo "$1" | sed "s/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g"
219189
}
220190

191+
is_port_or_range() {
192+
[ "$1" = "0" ] && return 1
193+
[ "$1" -ge "1" ] 2>/dev/null && [ "$1" -le "65535" ] 2>/dev/null && return 0
194+
[ "$2" = "port0inrange" ] && local minport=0 || local minport=1
195+
[ "${1%%-*}" -ge "$minport" ] 2>/dev/null && [ "${1%%-*}" -le "65535" ] 2>/dev/null &&
196+
[ "${1##*-}" -ge "$minport" ] 2>/dev/null && [ "${1##*-}" -le "65535" ] 2>/dev/null &&
197+
[ "${1##*-}" -ge "${1%%-*}" ] 2>/dev/null && return 0 || return 1
198+
}
199+
221200
upnpd_add_int_network_preset() {
222201
local cfg="$1"
223202
local interface acl_preset acl_accept_ports acl_reject_ports acl_custom_first
@@ -263,6 +242,27 @@ upnpd_add_int_network_preset() {
263242
fi
264243
}
265244

245+
upnpd_add_custom_acl_entry() {
246+
local cfg="$1"
247+
local comment int_addr int_port ext_port desc_filter action
248+
config_get comment "$cfg" comment "unspecified" # comment
249+
config_get int_addr "$cfg" int_addr "0.0.0.0/0" # IPv4 or network and subnet mask (internal)
250+
config_get int_port "$cfg" int_port "1-65535" # internal port/range: x or x-y
251+
config_get ext_port "$cfg" ext_port "1-65535" # external port/range: x or x-y
252+
config_get desc_filter "$cfg" desc_filter # description regex filter (must be built in)
253+
config_get action "$cfg" action # accept/reject/ignore
254+
! is_port_or_range "$int_port" port0inrange &&
255+
log "Custom ACL: Entry with invalid port or port range ($int_port) in int_port ignored" daemon.warn && int_port=1-65535
256+
! is_port_or_range "$ext_port" port0inrange &&
257+
log "Custom ACL: Entry with invalid port or port range ($ext_port) in ext_port ignored" daemon.warn && ext_port=1-65535
258+
[ "$desc_filter" != "" ] && desc_filter=" \"$desc_filter\""
259+
[ "$action" = "accept" ] && action=allow
260+
[ "$action" = "reject" ] && action=deny
261+
[ "$action" = "ignore" ] && return 0
262+
[ "$action" = "" ] && log "Custom ACL: Entry with no action ignored" daemon.warn && return 0
263+
echo "$action $ext_port $int_addr $int_port${desc_filter} # $comment"
264+
}
265+
266266
upnpd_uci_migration() {
267267
{ uci -q get upnpd.settings >/dev/null || ! uci -q get upnpd.config >/dev/null; } && return 0
268268
log "Check UCI options in /etc/config/upnpd to be migrated to v2.0"

0 commit comments

Comments
 (0)