Quellcode durchsuchen

Add domain blocking to admin control panel

Bob Mottram vor 8 Jahren
Ursprung
Commit
27f819f4ef
2 geänderte Dateien mit 133 neuen und 19 gelöschten Zeilen
  1. 93
    19
      src/freedombone-controlpanel
  2. 40
    0
      src/freedombone-utils-firewall

+ 93
- 19
src/freedombone-controlpanel Datei anzeigen

@@ -1835,6 +1835,78 @@ function menu_email {
1835 1835
     done
1836 1836
 }
1837 1837
 
1838
+function domain_blocking_add {
1839
+    data=$(tempfile 2>/dev/null)
1840
+    trap "rm -f $data" 0 1 2 5 15
1841
+    dialog --title $"Block a domain name" \
1842
+           --backtitle $"Freedombone Control Panel" \
1843
+           --inputbox $"Enter the domain name that you wish to block" 8 60 "" 2>$data
1844
+    sel=$?
1845
+    case $sel in
1846
+        0)
1847
+            blocked_domain=$(<$data)
1848
+            if [ ${#blocked_domain} -gt 2 ]; then
1849
+                firewall_block_domain $blocked_domain
1850
+                dialog --title $"Block a domain" \
1851
+                       --msgbox $"The domain $blocked_domain has been blocked" 6 40
1852
+            fi
1853
+            ;;
1854
+    esac
1855
+}
1856
+
1857
+function domain_blocking_remove {
1858
+    data=$(tempfile 2>/dev/null)
1859
+    trap "rm -f $data" 0 1 2 5 15
1860
+    dialog --title $"Unblock a domain name" \
1861
+           --backtitle $"Freedombone Control Panel" \
1862
+           --inputbox $"Enter the domain name that you wish to unblock" 8 60 "" 2>$data
1863
+    sel=$?
1864
+    case $sel in
1865
+        0)
1866
+            unblocked_domain=$(<$data)
1867
+            if [ ${#unblocked_domain} -gt 2 ]; then
1868
+                firewall_unblock_domain $unblocked_domain
1869
+                dialog --title $"Unblock a domain" \
1870
+                       --msgbox $"The domain $unblocked_domain has been unblocked" 6 40
1871
+            fi
1872
+            ;;
1873
+    esac
1874
+}
1875
+
1876
+function domain_blocking_show {
1877
+    if [ -f $FIREWALL_DOMAINS ]; then
1878
+        clear
1879
+        cat $FIREWALL_DOMAINS | sort
1880
+        any_key
1881
+    fi
1882
+}
1883
+
1884
+function domain_blocking {
1885
+    while true
1886
+    do
1887
+        data=$(tempfile 2>/dev/null)
1888
+        trap "rm -f $data" 0 1 2 5 15
1889
+        dialog --backtitle $"Freedombone Control Panel" \
1890
+               --title $"Domain Blocking" \
1891
+               --radiolist $"Choose an operation:" 12 60 4 \
1892
+               1 $"Block a domain" off \
1893
+               2 $"Unblock a domain" off \
1894
+               3 $"Show blocked domains" off \
1895
+               4 $"Back to main menu" on 2> $data
1896
+        sel=$?
1897
+        case $sel in
1898
+            1) break;;
1899
+            255) break;;
1900
+        esac
1901
+        case $(cat $data) in
1902
+            1) domain_blocking_add;;
1903
+            2) domain_blocking_remove;;
1904
+            3) domain_blocking_show;;
1905
+            4) break;;
1906
+        esac
1907
+    done
1908
+}
1909
+
1838 1910
 function menu_users {
1839 1911
     while true
1840 1912
     do
@@ -1969,7 +2041,7 @@ function menu_top_level {
1969 2041
         trap "rm -f $data" 0 1 2 5 15
1970 2042
         dialog --backtitle $"Freedombone Control Panel" \
1971 2043
                --title $"Control Panel" \
1972
-               --radiolist $"Choose an operation:" 28 70 21 \
2044
+               --radiolist $"Choose an operation:" 29 70 21 \
1973 2045
                1 $"About this system" off \
1974 2046
                2 $"Passwords" off \
1975 2047
                3 $"Backup and Restore" off \
@@ -1981,15 +2053,16 @@ function menu_top_level {
1981 2053
                9 $"Ping enable/disable" off \
1982 2054
                10 $"Manage Users" off \
1983 2055
                11 $"Email Menu" off \
1984
-               12 $"Security Settings" off \
1985
-               13 $"Set the main repository (repo mirrors)" off \
1986
-               14 $"Change the name of this system" off \
1987
-               15 $"Set a static local IP address" off \
1988
-               16 $"Wifi menu" off \
1989
-               17 $"Check for updates" off \
1990
-               18 $"Power off the system" off \
1991
-               19 $"Restart the system" off \
1992
-               20 $"Exit" on 2> $data
2056
+               12 $"Domain blocking" off \
2057
+               13 $"Security Settings" off \
2058
+               14 $"Set the main repository (repo mirrors)" off \
2059
+               15 $"Change the name of this system" off \
2060
+               16 $"Set a static local IP address" off \
2061
+               17 $"Wifi menu" off \
2062
+               18 $"Check for updates" off \
2063
+               19 $"Power off the system" off \
2064
+               20 $"Restart the system" off \
2065
+               21 $"Exit" on 2> $data
1993 2066
         sel=$?
1994 2067
         case $sel in
1995 2068
             1) exit 1;;
@@ -2011,15 +2084,16 @@ function menu_top_level {
2011 2084
             9) ping_enable_disable;;
2012 2085
             10) menu_users;;
2013 2086
             11) menu_email;;
2014
-            12) security_settings;;
2015
-            13) set_main_repo;;
2016
-            14) change_system_name;;
2017
-            15) set_static_IP;;
2018
-            16) menu_wifi;;
2019
-            17) check_for_updates;;
2020
-            18) shut_down_system;;
2021
-            19) restart_system;;
2022
-            20) break;;
2087
+            12) domain_blocking;;
2088
+            13) security_settings;;
2089
+            14) set_main_repo;;
2090
+            15) change_system_name;;
2091
+            16) set_static_IP;;
2092
+            17) menu_wifi;;
2093
+            18) check_for_updates;;
2094
+            19) shut_down_system;;
2095
+            20) restart_system;;
2096
+            21) break;;
2023 2097
         esac
2024 2098
     done
2025 2099
 }

+ 40
- 0
src/freedombone-utils-firewall Datei anzeigen

@@ -31,6 +31,7 @@
31 31
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
32 32
 
33 33
 FIREWALL_CONFIG=$HOME/${PROJECT_NAME}-firewall.cfg
34
+FIREWALL_DOMAINS=$HOME/${PROJECT_NAME}-firewall-domains.cfg
34 35
 
35 36
 function save_firewall_settings {
36 37
     iptables-save > /etc/firewall.conf
@@ -339,4 +340,43 @@ function firewall_remove {
339 340
     fi
340 341
 }
341 342
 
343
+function domain_to_hex_string {
344
+    domain="$1"
345
+    ctr = 1
346
+    segment=$(echo "$domain" | awk -F '.' "{print \$$ctr}")
347
+    while [ ${#segment} -gt 0 ]
348
+    do
349
+        if [ ${#segment} -lt 10 ]; then
350
+            echo -n "|0${#segment}|$segment"
351
+        else
352
+            echo -n "|${#segment}|$segment"
353
+        fi
354
+        ctr=$((ctr + 1))
355
+        segment=$(echo "$domain" | awk -F '.' "{print \$$ctr}")
356
+    done
357
+    echo ""
358
+}
359
+
360
+function firewall_block_domain {
361
+    blocked_domain="$1"
362
+    if ! grep "$blocked_domain" $FIREWALL_DOMAINS; then
363
+        hexstr=$(domain_to_hex_string $blocked_domain)
364
+        iptables -I FORWARD -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
365
+        iptables -I FORWARD -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
366
+        echo "${blocked_domain}" >> $FIREWALL_DOMAINS
367
+        save_firewall_settings
368
+    fi
369
+}
370
+
371
+function firewall_unblock_domain {
372
+    unblocked_domain="$1"
373
+    if grep "${unblocked_domain}" $FIREWALL_DOMAINS; then
374
+        hexstr=$(domain_to_hex_string $unblocked_domain)
375
+        iptables -D FORWARD -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
376
+        iptables -D FORWARD -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
377
+        sed -i "/${unblocked_domain}/d" $FIREWALL_DOMAINS
378
+        save_firewall_settings
379
+    fi
380
+}
381
+
342 382
 # NOTE: deliberately no exit 0