contrib/imagebuilder: Cleanups, seperate basic and network config of firewall and...
[project/luci.git] / contrib / package / meshwizard / files / usr / bin / meshwizard / helpers / setup_firewall.sh
index e3eb6a7..f5c4416 100755 (executable)
@@ -1,75 +1,63 @@
 #!/bin/sh
-# This will add $net to the zone firewall (and remove it from other zones where it is referenced)
-# It will also setup rules defined in /etc/config/freifunk and /etc/config/profile_<community>
-# Arg $1 = $net
+# Add "freifunk" firewall zone
+# If wan is used for olsr then delete wan zone and all wan rules
+# Also setup rules defined in /etc/config/freifunk and /etc/config/profile_<community>
 
-net=$1
 . /etc/functions.sh
 . $dir/functions.sh
-config_load firewall
 
-# Get some variables
-type="$(uci -q get wireless.$net.type)"
-vap="$(uci -q get meshwizard.netconfig.$net\_vap)"
+wan_is_olsr=$(uci -q get meshwizard.netconfig.wan_config)
+
+config_load firewall
 
-# Add local_restrict to wan firewall zone
+# Add local_restrict to wan firewall zone (if wan is not used for olsr)
+# If wan is used for olsr then remove the firewall zone wan
 handle_zonewan() {
        config_get name "$1" name
        if [ "$name" == "wan" ]; then
-               uci set firewall.$1.local_restrict=1
+               if  [ "$wan_is_olsr" == 1 ]; then
+                       uci del firewall.$1 && uci_commitverbose "WAN is used for olsr, delete firewall zone wan" firewall
+               else
+                       uci set firewall.$1.local_restrict=1 && uci_commitverbose "Enable local_restrict for zone wan" firewall
+               fi
        fi
 }
-config_foreach handle_zonewan zone && uci_commitverbose "Enable local_restrict for zone wan" firewall
+config_foreach handle_zonewan zone
 
-# Delete old firewall zone for freifunk
+# Rename firewall zone for freifunk if unnamed and delete wan zone if it is used for olsr; else enable local restrict
 handle_fwzone() {
        config_get name "$1" name
        config_get network "$1" network
 
-       if [ "$2" == "zoneconf" ]; then
-               if [ "$name" == "freifunk" ]; then
-                       # rename section if unnamed
-                       if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
-                               section_rename firewall $1 zone_freifunk
-                       fi
+       if [ "$name" == "freifunk" ]; then
+               # rename section if unnamed
+               if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
+                       section_rename firewall $1 zone_freifunk
+               fi
+       fi
+
+       if [ "$name" == "wan" ]; then
+               if  [ "$wan_is_olsr" == 1 ]; then
+                       uci del firewall.$1 && uci_commitverbose "WAN is used for olsr, delete firewall zone wan" firewall
                else
-                       if [ -n "$netrenamed" -a -n "$(echo $network | grep $netrenamed)" ] && [ ! "$name" == "freifunk" ]; then
-                               echo "    Removed $netrenamed from firewall zone $name."
-                               network_new=$(echo $network | sed -e 's/'$netrenamed'//' -e 's/^ //' -e 's/  / /' -e 's/ $//')
-                               uci set firewall.$1.network="$network_new"
-                       fi
+                       uci set firewall.$1.local_restrict=1 && uci_commitverbose "Enable local_restrict for zone wan" firewall
                fi
        fi
 }
 
-config_foreach handle_fwzone zone zoneconf
-
-# setup freifunk firewall zone
-# add $netrenamed and if needed ${netrenamed}dhcp to the networks for this zone
-config_get network zone_freifunk network
-
-# remove ${netrenamed}dhcp from networks list
-[ -n "$network" -a -n "$net" ] && network="${network/${netrenamed}dhcp/}"
-network=$(echo $network) # Removes leading and trailing whitespaces
-
-[ -n "$netrenamed" ] && [ -z "$(echo $network | grep $netrenamed)" ] && network="$network $netrenamed"
-
-if [ "$type" == "atheros" -a "$vap" == 1 ]; then
-        [ -n "$netrenamed" ] && [ "$network" == "${network/${netrenamed}dhcp/}" ] && network="$network ${netrenamed}dhcp"
-fi
+config_foreach handle_fwzone zone
 
 uci batch << EOF
 set firewall.zone_freifunk="zone"
 set firewall.zone_freifunk.name="freifunk"
-set firewall.zone_freifunk.network="$network"
 set firewall.zone_freifunk.input="$zone_freifunk_input"
 set firewall.zone_freifunk.forward="$zone_freifunk_forward"
 set firewall.zone_freifunk.output="$zone_freifunk_output"
 EOF
 
-uci_commitverbose "Setup freifunk firewall zone" firewall
+uci_commitverbose "Setup firewall zones" firewall
 
-# Usually we need to setup masquerading for lan, except lan is an olsr interface or has an olsr hna
+# Usually we need to setup masquerading for lan, except lan is an olsr interface or has an olsr hna-entry
 
 handle_interface() {
         config_get interface "$1" interface
@@ -80,13 +68,16 @@ handle_interface() {
 config_load olsrd
 config_foreach handle_interface Interface
 
-handle_hna() {
-        config_get netaddr "$1" netaddr
-        if [ "$NETWORK" == "$netaddr" ]; then
-                no_masq_lan=1
-        fi
-}
-config_foreach handle_hna Hna4
+LANIP="$(uci -q get network.lan.ipaddr)"
+if [ -n "$LANIP" ]; then
+       handle_hna() {
+               config_get netaddr "$1" netaddr
+                       if [ "$LANIP" == "$netaddr" ]; then
+                       no_masq_lan=1
+               fi
+       }
+       config_foreach handle_hna Hna4
+fi
 
 currms=$(uci -q get firewall.zone_freifunk.masq_src)
 if [ ! "$no_masq_lan" == "1" ]; then
@@ -94,22 +85,8 @@ if [ ! "$no_masq_lan" == "1" ]; then
        [ -z "$(echo $currms |grep lan)" ] && uci add_list firewall.zone_freifunk.masq_src="lan"
 fi
 
-# If wifi-interfaces are outside of the mesh network they should be natted
-for i in $networks; do
-        # Get dhcprange and meshnet
-        dhcprange=$(uci get meshwizard.netconfig.$i\_dhcprange)
-        meshnet="$(uci get profile_$community.profile.mesh_network)"
-        # check if the dhcprange is inside meshnet
-        dhcpinmesh="$($dir/helpers/check-range-in-range.sh $dhcprange $meshnet)"
-        if [ ! "$dhcpinmesh" == 1 ]; then
-                [ -z "$(echo $currms |grep ${netrenamed}dhcp)" ] && uci add_list firewall.zone_freifunk.masq_src="${netrenamed}dhcp"
-        fi
-done
-
-uci_commitverbose "Setup masquerading rules" firewall
 
 # Rules, Forwardings, advanced config and includes
-# Clear firewall configuration
 
 for config in freifunk profile_$community; do
 
@@ -126,6 +103,22 @@ for config in freifunk profile_$community; do
                config_foreach handle_firewall $section
        done
 done
-
 uci_commitverbose "Setup rules, forwardings, advanced config and includes." firewall
 
+# If wan is used for olsr we need to cleanup old wan (forward) rules
+
+if  [ "$wan_is_olsr" == 1 ]; then
+       handle_wanrules() {
+       config_get src "$1" src
+               config_get dest "$1" dest
+               if [ "$src" == "wan" ] || [ "$dest" == "wan" ]; then
+                       uci del firewall.$1
+               fi
+       }
+       for i in rule forwarding; do
+               config_load firewall
+               config_foreach handle_wanrules $i
+       done
+       uci_commitverbose "Wan is used for olsr, delete wan firewall rules and forwardings" firewall
+fi
+