Karl Perkins
Occasional Visitor
Code:
#!/bin/sh
# Karl Perkins - 25.9.2022
# This postconf (usually placed in /jffs/scripts/upnp.postconf, and then chmod a+x /jffs/scripts/upnp.postconf)
# enables /jffs/configis/upnp.add to actually work. I've written this to hopefully still work, even if the issue
# with the placement of the global allow and deny rule is fixed in a subsequent release of the merlin firmware.
if [ $# == 0 ] ; then
echo "$(basename $0) <path to upnp config file>"
echo " This is usually /etc/upnp/config"
exit 1
fi
CONFIG=$1
LAN_IP=$(ip -4 -o a show dev br0 | cut -d " " -f 7 | cut -d "/" -f 1)
source /usr/sbin/helper.sh
# Copy the original config, so we can see what it looked like before this postconf script ran
cp $CONFIG /tmp/upnp.old.$(basename $CONFIG)
# Get the current allow and deny all rules, and correct the subnet mask to CIDR if it's wrong
allow_all_line="$(grep "allow .-65535 $LAN_IP" $CONFIG | sed 's/\/255.255.255.0/\/24/')"
deny_all_line="$(grep "deny .-65535 0.0.0.0" $CONFIG | sed 's/\/0.0.0.0/\/0/')"
# Add a line before the beginning of the /jffs/configs/upnp.add in case anyone wants to insert rules before this
pc_insert "deny .-65535 0.0.0.0" "# START.OF.RULES" $CONFIG
pc_insert "START.OF.RULES" "" $CONFIG
# Removes the current allow all and deny all rules
pc_delete "allow .-65535 $LAN_IP" $CONFIG
pc_delete "deny .-65535 0.0.0.0" $CONFIG
cat <<EOF >> $CONFIG
# There's some code in $0 to move the global allow rule here
# END.OF.RULES.1
# There's some code in $0 to move the global deny rule here
# END.OF.RULES.2
EOF
# Adds in the right "allow all" rule, with the correct CIDR in the right place, almost at the end.
# If you comment out this following line, then every host will be denied, and then /jffs/configs/upnp.add
# should contain "allow" rules, rather than "deny" rules.
pc_insert "END.OF.RULES.1" "$allow_all_line" $CONFIG
# Adds in the right "deny all" rule, with the correct CIDR, right at the end
pc_insert "END.OF.RULES.2" "$deny_all_line" $CONFIG
Last edited: