Skip to content

Commit 70ebea2

Browse files
miniupnpd: separate service start and config-gen
- Remove `config_foreach upnpd "upnpd"` and replace it with regular function call, as init was not designed for a multi-instance setup, as the same `tmpconf` will be used/overwritten, and non-anonymous section - Move code to make the custom vs. config file generation decision earlier, and only perform external interface detection with the second one, and rename function `upnpd` to `upnpd_generate_config` - Replace unnecessary `if` cases with `elif` in init/hotplug - Exit with 1 on errors to get an inactive service status - Do not restart daemon in hotplug when using a custom config file, as then this file will not be regenerated on restarts - Use `procd_add_reload_trigger "firewall"` instead of listening `/etc/config/firewall` (to merge with prior) Signed-off-by: Self-Hosting-Group <[email protected]>
1 parent c76aded commit 70ebea2

File tree

2 files changed

+75
-91
lines changed

2 files changed

+75
-91
lines changed

net/miniupnpd/files/miniupnpd.hotplug

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
#!/bin/sh
12
/etc/init.d/miniupnpd enabled || exit 0
23

3-
# If miniupnpd is not running:
4-
# - check on _any_ event (event updates may contribute to network_find_wan*)
5-
6-
# If miniupnpd _is_ running:
7-
# - check only on ifup (otherwise lease updates etc would cause
8-
# miniupnpd state loss)
4+
# If daemon is:
5+
# - not running: check on any event (event updates may contribute to network_find_wan*)
6+
# - running: check only on ifup (otherwise lease updates etc. would cause daemon state loss)
97

108
[ "$ACTION" != "ifup" ] && /etc/init.d/miniupnpd running && exit 0
9+
uci -q get upnpd.settings.config_file >/dev/null && exit 0
1110

1211
tmpconf="/var/etc/miniupnpd.conf"
1312
external_iface=$(uci -q get upnpd.settings.external_iface)
@@ -16,26 +15,19 @@ external_zone=$(uci -q get upnpd.settings.external_zone)
1615
[ -x "$(command -v nft)" ] && FW="fw4" || FW="fw3"
1716

1817
. /lib/functions/network.sh
19-
20-
if [ -n "$external_iface" ] ; then
18+
if [ -n "$external_iface" ]; then
2119
network_get_device ifname "$external_iface"
20+
elif [ -n "$external_zone" ]; then
21+
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
2222
else
23-
if [ -n "$external_zone" ] ; then
24-
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
25-
else
26-
network_find_wan external_iface && \
27-
network_get_device ifname "$external_iface"
28-
fi
23+
network_find_wan external_iface && network_get_device ifname "$external_iface"
2924
fi
30-
if [ -n "$external_iface6" ] ; then
25+
if [ -n "$external_iface6" ]; then
3126
network_get_device ifname6 "$external_iface6"
27+
elif [ -n "$external_zone" ]; then
28+
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
3229
else
33-
if [ -n "$external_zone" ] ; then
34-
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
35-
else
36-
network_find_wan6 external_iface6 && \
37-
network_get_device ifname6 "$external_iface6"
38-
fi
30+
network_find_wan6 external_iface6 && network_get_device ifname6 "$external_iface6"
3931
fi
4032

4133
[ "$DEVICE" != "$ifname" ] && [ "$DEVICE" != "$ifname6" ] && exit 0

net/miniupnpd/files/miniupnpd.init

Lines changed: 62 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,16 @@ upnpd_add_custom_acl_entry() {
3737
echo "$action $ext_port $int_addr $int_port${descr_filter} # $comment"
3838
}
3939

40-
upnpd() {
41-
config_load "upnpd"
42-
local enabled
43-
config_get enabled settings enabled 0
44-
if [ "$enabled" != "1" ]; then
45-
log "Service disabled, UCI enabled is not set"
46-
return 1
47-
fi
40+
upnpd_generate_config() {
4841
# Daemon
49-
local enabled_protocols allow_cgnat stun_host allow_third_party_mapping ipv6_disable system_uptime log_output lease_file config_file
42+
local enabled_protocols allow_cgnat stun_host allow_third_party_mapping ipv6_disable system_uptime lease_file
5043
config_get enabled_protocols settings enabled_protocols all
5144
config_get allow_cgnat settings allow_cgnat 0
5245
config_get stun_host settings stun_host stun.nextcloud.com
5346
config_get allow_third_party_mapping settings allow_third_party_mapping 0
5447
config_get ipv6_disable settings ipv6_disable 0
5548
config_get system_uptime settings system_uptime 1
56-
config_get log_output settings log_output
5749
config_get lease_file settings lease_file /run/miniupnpd.leases
58-
config_get config_file settings config_file
5950

6051
# UPnP IGD
6152
local upnp_igd_compat download_kbps upload_kbps friendly_name model_number serial_number presentation_url uuid http_port notify_interval
@@ -77,51 +68,39 @@ upnpd() {
7768
config_get external_zone settings external_zone
7869
config_get external_ip settings external_ip
7970

80-
local conf ifname ifname6
71+
local ifname ifname6
8172
. /lib/functions/network.sh
82-
83-
if [ -n "$external_iface" ] ; then
73+
if [ -n "$external_iface" ]; then
8474
network_get_device ifname "$external_iface"
75+
elif [ -n "$external_zone" ]; then
76+
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
8577
else
86-
if [ -n "$external_zone" ] ; then
87-
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
88-
else
89-
network_find_wan external_iface && \
90-
network_get_device ifname "$external_iface"
91-
fi
78+
network_find_wan external_iface && network_get_device ifname "$external_iface"
9279
fi
93-
if [ -n "$external_iface6" ] ; then
80+
if [ -n "$external_iface6" ]; then
9481
network_get_device ifname6 "$external_iface6"
82+
elif [ -n "$external_zone" ]; then
83+
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
9584
else
96-
if [ -n "$external_zone" ] ; then
97-
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
98-
else
99-
network_find_wan6 external_iface6 && \
100-
network_get_device ifname6 "$external_iface6"
101-
fi
85+
network_find_wan6 external_iface6 && network_get_device ifname6 "$external_iface6"
10286
fi
10387

104-
if [ -n "$config_file" ]; then
105-
conf="$config_file"
106-
else
107-
local tmpconf="/var/etc/miniupnpd.conf"
108-
conf="$tmpconf"
109-
mkdir -p /var/etc
110-
if [ "$ifname" = "" ]; then
111-
log "No external network interface found, not starting" daemon.err
112-
return 1
113-
fi
114-
if ! uci -q get upnpd.@internal_network[0].interface >/dev/null; then
115-
log "No internal networks configured, not starting" daemon.err
116-
return 1
117-
fi
118-
# Only perform an STUN CGNAT test if necessary, with a private/CGNAT external IPv4
119-
local extipv4 extipv4private
120-
network_get_ipaddr extipv4 "$external_iface"
121-
case "$extipv4" in
122-
10.* | 172.1[6-9].* | 172.2[0-9].* | 172.3[0-1].* | 192.168.* | 100.6[4-9].* | 100.[7-9][0-9].* | 100.1[0-1][0-9].* | 100.12[0-7].*) extipv4private=1 ;;
123-
esac
124-
{
88+
if [ "$ifname" = "" ]; then
89+
log "No external network interface found, not starting" daemon.err
90+
return 1
91+
fi
92+
if ! uci -q get upnpd.@internal_network[0].interface >/dev/null; then
93+
log "No internal networks configured, not starting" daemon.err
94+
return 1
95+
fi
96+
# Only perform an STUN CGNAT test if necessary, with a private/CGNAT external IPv4
97+
local extipv4 extipv4private
98+
network_get_ipaddr extipv4 "$external_iface"
99+
case "$extipv4" in
100+
10.* | 172.1[6-9].* | 172.2[0-9].* | 172.3[0-1].* | 192.168.* | 100.6[4-9].* | 100.[7-9][0-9].* | 100.1[0-1][0-9].* | 100.12[0-7].*) extipv4private=1 ;;
101+
esac
102+
103+
{
125104
echo "# Daemon"
126105
[ "$enabled_protocols" = "all" ] && echo "enable_upnp=yes" && echo "enable_pcp_pmp=yes"
127106
[ "$enabled_protocols" = "upnp-igd" ] && echo "enable_upnp=yes" && echo "enable_pcp_pmp=no"
@@ -184,32 +163,15 @@ upnpd() {
184163
config_foreach upnpd_add_int_network_and_preset internal_network postcustom
185164
echo "deny 1-65535 0.0.0.0/0 1-65535 # Reject ACL by default"
186165

187-
} > "$tmpconf"
188-
fi
189-
190-
if [ -n "$ifname" ]; then
191-
if [ "$FW" = "fw4" ]; then
192-
nft -s -t -n list chain inet fw4 upnp_forward >/dev/null 2>&1 || fw4 reload
193-
else
194-
iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
195-
fi
196-
fi
197-
198-
procd_open_instance
199-
procd_set_param file "$conf" "/etc/config/firewall"
200-
procd_set_param command "$PROG"
201-
procd_append_param command -f "$conf"
202-
[ "$log_output" = "info" ] && procd_append_param command -v
203-
[ "$log_output" = "debug" ] && procd_append_param command -d
204-
procd_close_instance
166+
} >"$1"
205167
}
206168

207169
stop_service() {
208170
if [ "$FW" = "fw3" ]; then
209-
iptables -t nat -F MINIUPNPD 2>/dev/null
210-
iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
211171
iptables -t filter -F MINIUPNPD 2>/dev/null
212172
[ -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
213175
else
214176
nft flush chain inet fw4 upnp_forward 2>/dev/null
215177
nft flush chain inet fw4 upnp_prerouting 2>/dev/null
@@ -219,11 +181,41 @@ stop_service() {
219181

220182
start_service() {
221183
config_load "upnpd"
222-
config_foreach upnpd "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
223215
}
224216

225217
service_triggers() {
226-
procd_add_reload_trigger "upnpd"
218+
procd_add_reload_trigger "upnpd" "firewall"
227219
}
228220

229221
log() {

0 commit comments

Comments
 (0)