1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468
|
#!/bin/bash
#
# Startup script to implement /etc/firehol/firehol.conf pre-defined rules.
#
# chkconfig: 2345 99 92
#
# description: creates stateful iptables packet filtering firewalls.
#
# by Costa Tsaousis <costa@tsaousis.gr>
#
# config: /etc/firehol/firehol.conf
#
# $Id: firehol.sh,v 1.231 2005/04/03 21:48:04 ktsaou Exp $
#
# Make sure only root can run us.
if [ ! "${UID}" = 0 ]
then
echo >&2
echo >&2
echo >&2 "Only user root can run FireHOL."
echo >&2
fi
# Remember who you are.
FIREHOL_FILE="${0}"
FIREHOL_DEFAULT_WORKING_DIRECTORY="${PWD}"
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# EXTERNAL/SYSTEM COMMANDS MANAGEMENT
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
export PATH="${PATH}:/bin:/usr/bin:/sbin:/usr/sbin"
# External commands FireHOL will need.
# If one of those is not found, FireHOL will refuse to run.
which_cmd() {
local block=1
if [ "a${1}" = "a-n" ]
then
local block=0
shift
fi
unalias $2 >/dev/null 2>&1
local cmd=`which $2 2>/dev/null | head -n 1`
if [ $? -gt 0 -o ! -x "${cmd}" ]
then
if [ ${block} -eq 1 ]
then
echo >&2
echo >&2 "ERROR: Command '$2' not found in the system path."
echo >&2 " FireHOL requires this command for its operation."
echo >&2 " Please install the required package and retry."
echo >&2
echo >&2 " Note that you need an operational 'which' command"
echo >&2 " for FireHOL to find all the external programs it"
echo >&2 " needs. Check it yourself. Run:"
echo >&2
echo >&2 " which $2"
exit 1
fi
return 1
fi
eval $1=${cmd}
return 0
}
# command on demand support.
require_cmd() {
local block=1
if [ "a$1" = "a-n" ]
then
local block=0
shift
fi
# if one is found, return success
for x in $1
do
eval var=`echo ${x} | tr 'a-z' 'A-Z'`_CMD
eval val=\$\{${var}\}
if [ -z "${val}" ]
then
which_cmd -n "${var}" "${x}"
test $? -eq 0 && return 0
fi
done
if [ $block -eq 1 ]
then
echo >&2
echo >&2 "ERROR: THE REQUESTED FEATURE REQUIRES THESE PROGRAMS:"
echo >&2
echo >&2 " $*"
echo >&2
echo >&2 " You have requested the use of an optional FireHOL"
echo >&2 " feature that requires certain external programs"
echo >&2 " to be installed in the running system."
echo >&2
echo >&2 " Please consult your Linux distribution manual to"
echo >&2 " install the package(s) that provide these external"
echo >&2 " programs and retry."
echo >&2
echo >&2 " Note that you need an operational 'which' command"
echo >&2 " for FireHOL to find all the external programs it"
echo >&2 " needs. Check it yourself. Run:"
echo >&2
for x in $1
do
echo >&2 " which $x"
done
exit 1
fi
return 1
}
# Currently the following commands are required only when needed.
# (i.e. Command on Demand)
#
# wget or curl (either is fine)
# gzcat
# ip
# netstat
# egrep
# date
# hostname
# Commands that are mandatory for FireHOL operation:
which_cmd CAT_CMD cat
which_cmd CUT_CMD cut
which_cmd CHOWN_CMD chown
which_cmd CHMOD_CMD chmod
which_cmd EXPR_CMD expr
which_cmd GAWK_CMD gawk
which_cmd GREP_CMD grep
which_cmd HEAD_CMD head
which_cmd IPTABLES_CMD iptables
which_cmd IPTABLES_SAVE_CMD iptables-save
which_cmd LESS_CMD less
which_cmd LSMOD_CMD lsmod
which_cmd MKDIR_CMD mkdir
which_cmd MV_CMD mv
which_cmd MODPROBE_CMD modprobe
which_cmd RENICE_CMD renice
which_cmd RM_CMD rm
which_cmd SED_CMD sed
which_cmd SORT_CMD sort
which_cmd SYSCTL_CMD sysctl
which_cmd TOUCH_CMD touch
which_cmd TR_CMD tr
which_cmd UNAME_CMD uname
which_cmd UNIQ_CMD uniq
# Make sure our generated files cannot be accessed by anyone else.
umask 077
# Be nice on production environments
${RENICE_CMD} 10 $$ >/dev/null 2>/dev/null
# Find our minor version
firehol_minor_version() {
${CAT_CMD} <<"EOF" | ${CUT_CMD} -d ' ' -f 3 | ${CUT_CMD} -d '.' -f 2
$Id: firehol.sh,v 1.231 2005/04/03 21:48:04 ktsaou Exp $
EOF
}
FIREHOL_MINOR_VERSION=`firehol_minor_version`
${EXPR_CMD} ${FIREHOL_MINOR_VERSION} + 0 >/dev/null 2>&1
if [ $? -ne 0 ]
then
FIREHOL_MINOR_VERSION=220
fi
# Initialize iptables
${IPTABLES_CMD} -nxvL >/dev/null 2>&1
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# GLOBAL DEFAULTS
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# ----------------------------------------------------------------------
# Directories and files
# These files will be created and deleted during our run.
FIREHOL_DIR="/tmp/.firehol-tmp-$$-${RANDOM}-${RANDOM}"
FIREHOL_CHAINS_DIR="${FIREHOL_DIR}/chains"
FIREHOL_OUTPUT="${FIREHOL_DIR}/firehol-out.sh"
FIREHOL_SAVED="${FIREHOL_DIR}/firehol-save.sh"
FIREHOL_TMP="${FIREHOL_DIR}/firehol-tmp.sh"
FIREHOL_SPOOL_DIR="/var/spool/firehol"
# The default configuration file
# It can be changed on the command line
FIREHOL_CONFIG_DIR="/etc/firehol"
FIREHOL_CONFIG="${FIREHOL_CONFIG_DIR}/firehol.conf"
# Where /etc/init.d/iptables expects its configuration?
# Leave it empty for automatic detection
FIREHOL_AUTOSAVE=
# ------------------------------------------------------------------------------
# Make sure we automatically cleanup when we exit.
# WHY:
# Even a CTRL-C will call this and we will not leave temp files.
# Also, if a configuration file breaks, we will detect this too.
firehol_exit() {
if [ -f "${FIREHOL_SAVED}" ]
then
echo
echo -n $"FireHOL: Restoring old firewall:"
iptables-restore <"${FIREHOL_SAVED}"
if [ $? -eq 0 ]
then
success $"FireHOL: Restoring old firewall:"
else
failure $"FireHOL: Restoring old firewall:"
fi
echo
fi
test -d "${FIREHOL_DIR}" && ${RM_CMD} -rf "${FIREHOL_DIR}"
return 0
}
# Run our exit even if we don't call exit.
trap firehol_exit EXIT
trap firehol_exit SIGHUP
# ------------------------------------------------------------------------------
# Create the directories we need.
if [ ! -d "${FIREHOL_CONFIG_DIR}" -a -f /etc/firehol.conf ]
then
"${MKDIR_CMD}" "${FIREHOL_CONFIG_DIR}" || exit 1
"${CHOWN_CMD}" root:root "${FIREHOL_CONFIG_DIR}" || exit 1
"${CHMOD_CMD}" 700 "${FIREHOL_CONFIG_DIR}" || exit 1
"${MV_CMD}" /etc/firehol.conf "${FIREHOL_CONFIG}" || exit 1
echo >&2
echo >&2
echo >&2 "NOTICE: Your config file /etc/firehol.conf has been moved to ${FIREHOL_CONFIG}"
echo >&2
sleep 5
fi
# Externally defined services can be placed in "${FIREHOL_CONFIG_DIR}/services/"
if [ ! -d "${FIREHOL_CONFIG_DIR}/services" ]
then
"${MKDIR_CMD}" "${FIREHOL_CONFIG_DIR}/services"
if [ $? -ne 0 ]
then
echo >&2
echo >&2
echo >&2 "FireHOL needs to create the directory '${FIREHOL_CONFIG_DIR}/services', but it cannot."
echo >&2 "Possibly you have a file with this name, or something else is happening."
echo >&2 "Please solve this issue and retry".
echo >&2
exit 1
fi
"${CHOWN_CMD}" root:root "${FIREHOL_CONFIG_DIR}/services"
"${CHMOD_CMD}" 700 "${FIREHOL_CONFIG_DIR}/services"
fi
# Remove any old directories that might be there.
if [ -d "${FIREHOL_DIR}" ]
then
"${RM_CMD}" -rf "${FIREHOL_DIR}"
if [ $? -ne 0 -o -e "${FIREHOL_DIR}" ]
then
echo >&2
echo >&2
echo >&2 "Cannot clean temporary directory '${FIREHOL_DIR}'."
echo >&2
exit 1
fi
fi
"${MKDIR_CMD}" "${FIREHOL_DIR}" || exit 1
"${MKDIR_CMD}" "${FIREHOL_CHAINS_DIR}" || exit 1
# Make sure we have a directory for our data.
if [ ! -d "${FIREHOL_SPOOL_DIR}" ]
then
"${MKDIR_CMD}" "${FIREHOL_SPOOL_DIR}" || exit 1
"${CHOWN_CMD}" root:root "${FIREHOL_CONFIG_DIR}" || exit 1
"${CHMOD_CMD}" 700 "${FIREHOL_CONFIG_DIR}" || exit 1
fi
# ------------------------------------------------------------------------------
# IP definitions
# IANA Reserved IPv4 address space
# Suggested by Fco.Felix Belmonte <ffelix@gescosoft.com>
# Optimized (CIDR) by Marc 'HE' Brockschmidt <marc@marcbrockschmidt.de>
# Further optimized and reduced by http://www.vergenet.net/linux/aggregate/
# The supplied get-iana.sh uses 'aggregate-flim' if it finds it in the path.
RESERVED_IPS="0.0.0.0/7 2.0.0.0/8 5.0.0.0/8 7.0.0.0/8 23.0.0.0/8 27.0.0.0/8 31.0.0.0/8 36.0.0.0/7 39.0.0.0/8 41.0.0.0/8 42.0.0.0/8 73.0.0.0/8 74.0.0.0/7 76.0.0.0/6 89.0.0.0/8 90.0.0.0/7 92.0.0.0/6 96.0.0.0/3 173.0.0.0/8 174.0.0.0/7 176.0.0.0/5 184.0.0.0/6 189.0.0.0/8 190.0.0.0/8 197.0.0.0/8 223.0.0.0/8 240.0.0.0/4"
# Private IPv4 address space
# Suggested by Fco.Felix Belmonte <ffelix@gescosoft.com>
# Revised by me according to RFC 3330. Explanation:
# 10.0.0.0/8 => RFC 1918: IANA Private Use
# 169.254.0.0/16 => Link Local
# 192.0.2.0/24 => Test Net
# 192.88.99.0/24 => RFC 3068: 6to4 anycast & RFC 2544: Benchmarking addresses
# 192.168.0.0/16 => RFC 1918: Private use
PRIVATE_IPS="10.0.0.0/8 169.254.0.0/16 172.16.0.0/12 192.0.2.0/24 192.88.99.0/24 192.168.0.0/16"
# The multicast address space
MULTICAST_IPS="224.0.0.0/4"
# A shortcut to have all the Internet unroutable addresses in one
# variable
UNROUTABLE_IPS="${RESERVED_IPS} ${PRIVATE_IPS}"
# ----------------------------------------------------------------------
# The default policy for the interface commands of the firewall.
# This can be controlled on a per interface basis using the
# policy interface subscommand.
DEFAULT_INTERFACE_POLICY="DROP"
# Which is the filter table chains policy during firewall activation?
FIREHOL_INPUT_ACTIVATION_POLICY="ACCEPT"
FIREHOL_OUTPUT_ACTIVATION_POLICY="ACCEPT"
FIREHOL_FORWARD_ACTIVATION_POLICY="ACCEPT"
# Should we drop all INVALID packets always?
FIREHOL_DROP_INVALID=0
# What to do with unmatched packets?
# To change these, simply define them the configuration file.
UNMATCHED_INPUT_POLICY="DROP"
UNMATCHED_OUTPUT_POLICY="DROP"
UNMATCHED_ROUTER_POLICY="DROP"
# Options for iptables LOG action.
# These options will be added to all LOG actions FireHOL will generate.
# To change them, type such a line in the configuration file.
# FIREHOL_LOG_OPTIONS="--log-tcp-sequence --log-tcp-options --log-ip-options"
FIREHOL_LOG_OPTIONS=""
FIREHOL_LOG_LEVEL="warning"
FIREHOL_LOG_MODE="LOG"
FIREHOL_LOG_FREQUENCY="1/second"
FIREHOL_LOG_BURST="5"
# The client ports to be used for "default" client ports when the
# client specified is a foreign host.
# We give all ports above 1000 because a few systems (like Solaris)
# use this range.
# Note that FireHOL will ask the kernel for default client ports of
# the local host. This only applies to client ports of remote hosts.
DEFAULT_CLIENT_PORTS="1024:65535"
# Get the default client ports from the kernel configuration.
# This is formed to a range of ports to be used for all "default"
# client ports when the client specified is the localhost.
LOCAL_CLIENT_PORTS_LOW=`${SYSCTL_CMD} net.ipv4.ip_local_port_range | ${CUT_CMD} -d '=' -f 2 | ${CUT_CMD} -f 1`
LOCAL_CLIENT_PORTS_HIGH=`${SYSCTL_CMD} net.ipv4.ip_local_port_range | ${CUT_CMD} -d '=' -f 2 | ${CUT_CMD} -f 2`
LOCAL_CLIENT_PORTS="${LOCAL_CLIENT_PORTS_LOW}:${LOCAL_CLIENT_PORTS_HIGH}"
# ----------------------------------------------------------------------
# This is our version number. It is increased when the configuration
# file commands and arguments change their meaning and usage, so that
# the user will have to review it more precisely.
FIREHOL_VERSION=5
# ----------------------------------------------------------------------
# The initial line number of the configuration file.
FIREHOL_LINEID="INIT"
# Variable kernel module requirements.
# Suggested by Fco.Felix Belmonte <ffelix@gescosoft.com>
# Note that each of the complex services
# may add to this variable the kernel modules it requires.
# See rules_ftp() bellow for an example.
FIREHOL_KERNEL_MODULES=""
#
# In the configuration file you can write:
#
# require_kernel_module <module_name>
#
# to have FireHOL require a specific module for the configurarion.
# Set this to 1 in the configuration file to have FireHOL complex
# services' rules load NAT kernel modules too.
FIREHOL_NAT=0
# Set this to 1 in the configuration file if routing should be enabled
# in the kernel.
FIREHOL_ROUTING=0
# Services may add themeselves to this variable so that the service "all" will
# also call them.
# By default it is empty - only rules programmers should change this.
ALL_SHOULD_ALSO_RUN=
# ------------------------------------------------------------------------------
# Various Defaults
# If set to 1, we are just going to present the resulting firewall instead of
# installing it.
# It can be changed on the command line
FIREHOL_DEBUG=0
# If set to 1, the firewall will be saved for normal iptables processing.
# It can be changed on the command line
FIREHOL_SAVE=0
# If set to 1, the firewall will be restored if you don't commit it.
# It can be changed on the command line
FIREHOL_TRY=1
# If set to 1, FireHOL enters interactive mode to answer questions.
# It can be changed on the command line
FIREHOL_EXPLAIN=0
# If set to 1, FireHOL enters a wizard mode to help the user build a firewall.
# It can be changed on the command line
FIREHOL_WIZARD=0
# If set to 0, FireHOL will not try to load the required kernel modules.
# It can be set in the configuration file.
FIREHOL_LOAD_KERNEL_MODULES=1
# If set to 1, FireHOL will output the commands of the configuration file
# with variables expanded.
FIREHOL_CONF_SHOW=1
# ------------------------------------------------------------------------------
# Keep information about the current primary command
# Primary commands are: interface, router
work_counter=0
work_cmd=
work_realcmd=("(unset)")
work_name=
work_inface=
work_outface=
work_policy="${DEFAULT_INTERFACE_POLICY}"
work_error=0
work_function="Initializing"
# ------------------------------------------------------------------------------
# Keep status information
# 0 = no errors, 1 = there were errors in the script
work_final_status=0
# This variable is used for generating dynamic chains when needed for
# combined negative statements (AND) implied by the "not" parameter
# to many FireHOL directives.
# What FireHOL is doing to accomplish this, is to produce dynamically
# a linked list of iptables chains with just one condition each, making
# the packets to traverse from chain to chain when matched, to reach
# their final destination.
FIREHOL_DYNAMIC_CHAIN_COUNTER=1
# If set to 0, FireHOL will not trust interface lo for all traffic.
# This means the admin could setup a firewall on lo.
FIREHOL_TRUST_LOOPBACK=1
# Services API version
FIREHOL_SERVICES_API="1"
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# SIMPLE SERVICES DEFINITIONS
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# The following are definitions for simple services.
# We define as "simple" the services that are implemented using a single socket,
# initiated by the client and used by the server.
#
# The following list is sorted by service name.
server_AH_ports="51/any"
client_AH_ports="any"
# Debian package proxy
server_aptproxy_ports="tcp/9999"
client_aptproxy_ports="default"
# APC UPS Server (these ports have to be accessible on all machines NOT
# directly connected to the UPS (e.g. the slaves)
server_apcupsd_ports="tcp/6544"
client_apcupsd_ports="default"
server_apcupsdnis_ports="tcp/3551"
client_apcupsdnis_ports="default"
server_asterisk_ports="tcp/5038"
client_asterisk_ports="default"
server_cups_ports="tcp/631 udp/631"
client_cups_ports="default 631"
server_cvspserver_ports="tcp/2401"
client_cvspserver_ports="default"
server_darkstat_ports="tcp/666"
client_darkstat_ports="default"
server_daytime_ports="tcp/13"
client_daytime_ports="default"
server_dcc_ports="udp/6277"
client_dcc_ports="default"
server_dcpp_ports="tcp/1412 udp/1412"
client_dcpp_ports="default"
server_dns_ports="udp/53 tcp/53"
client_dns_ports="any"
# DHCP Relaying (server is the relay server which behaves like a client
# towards the real DHCP Server); I'm not sure about this one...
server_dhcprelay_ports="udp/67"
client_dhcprelay_ports="67"
server_dict_ports="tcp/2628"
client_dict_ports="default"
# DISTCC is the distributed gcc for Gentoo
server_distcc_ports="tcp/3632"
client_distcc_ports="default"
server_eserver_ports="tcp/4661 udp/4661 udp/4665"
client_eserver_ports="any"
server_ESP_ports="50/any"
client_ESP_ports="any"
server_echo_ports="tcp/7"
client_echo_ports="default"
server_finger_ports="tcp/79"
client_finger_ports="default"
# giFT modules' ports
# Gnutella = tcp/4302
# FastTrack = tcp/1214
# OpenFT = tcp/2182 tcp/2472
server_gift_ports="tcp/4302 tcp/1214 tcp/2182 tcp/2472"
client_gift_ports="any"
# giFT User Interface connections
server_giftui_ports="tcp/1213"
client_giftui_ports="default"
# gkrellmd (from gkrellm.net)
server_gkrellmd_ports="tcp/19150"
client_gkrellmd_ports="default"
server_GRE_ports="47/any"
client_GRE_ports="any"
server_h323_ports="tcp/1720 tcp/1731"
client_h323_ports="default"
# We assume heartbeat uses ports in the range 690 to 699
server_heartbeat_ports="udp/690:699"
client_heartbeat_ports="default"
server_http_ports="tcp/80"
client_http_ports="default"
server_https_ports="tcp/443"
client_https_ports="default"
server_iax_ports="udp/5036"
client_iax_ports="default"
server_iax2_ports="udp/5469 udp/4569"
client_iax2_ports="default"
server_ICMP_ports="icmp/any"
client_ICMP_ports="any"
server_icmp_ports="icmp/any"
client_icmp_ports="any"
# ALL_SHOULD_ALSO_RUN="${ALL_SHOULD_ALSO_RUN} icmp"
# Squid' ICP port
server_icp_ports="udp/3130"
client_icp_ports="3130"
server_ident_ports="tcp/113"
client_ident_ports="default"
server_imap_ports="tcp/143"
client_imap_ports="default"
server_imaps_ports="tcp/993"
client_imaps_ports="default"
server_irc_ports="tcp/6667"
client_irc_ports="default"
require_irc_modules="ip_conntrack_irc"
require_irc_nat_modules="ip_nat_irc"
ALL_SHOULD_ALSO_RUN="${ALL_SHOULD_ALSO_RUN} irc"
# for IPSec Key negotiation
server_isakmp_ports="udp/500"
client_isakmp_ports="500"
server_jabber_ports="tcp/5222 tcp/5223"
client_jabber_ports="default"
server_jabberd_ports="tcp/5222 tcp/5223 tcp/5269"
client_jabberd_ports="default"
server_ldap_ports="tcp/389"
client_ldap_ports="default"
server_ldaps_ports="tcp/636"
client_ldaps_ports="default"
server_lpd_ports="tcp/515"
client_lpd_ports="721:731 default"
server_microsoft_ds_ports="tcp/445"
client_microsoft_ds_ports="default"
server_mms_ports="tcp/1755 udp/1755"
client_mms_ports="default"
require_mms_modules="ip_conntrack_mms"
require_mms_nat_modules="ip_nat_mms"
# this will produce warnings on most distribution
# because the mms module is not there:
# ALL_SHOULD_ALSO_RUN="${ALL_SHOULD_ALSO_RUN} mms"
server_msn_ports="tcp/6891"
client_msn_ports="default"
server_mysql_ports="tcp/3306"
client_mysql_ports="default"
# Veritas NetBackup
server_netbackup_ports="tcp/13701 tcp/13711 tcp/13720 tcp/13721 tcp/13724 tcp/13782 tcp/13783"
client_netbackup_ports="any"
server_netbios_ns_ports="udp/137"
client_netbios_ns_ports="default 137"
server_netbios_dgm_ports="udp/138"
client_netbios_dgm_ports="default 138"
server_netbios_ssn_ports="tcp/139"
client_netbios_ssn_ports="default"
server_nntp_ports="tcp/119"
client_nntp_ports="default"
server_nntps_ports="tcp/563"
client_nntps_ports="default"
server_ntp_ports="udp/123 tcp/123"
client_ntp_ports="123 default"
# Network UPS Tools
server_nut_ports="tcp/3493 udp/3493"
client_nut_ports="default"
# NoMachine's NX server
server_nxserver_ports="tcp/5000:5200"
client_nxserver_ports="default"
# Oracle database
server_oracle_ports="tcp/1521"
client_oracle_ports="default"
server_pop3_ports="tcp/110"
client_pop3_ports="default"
server_pop3s_ports="tcp/995"
client_pop3s_ports="default"
# Portmap clients appear to use ports bellow 1024
server_portmap_ports="udp/111 tcp/111"
client_portmap_ports="500:65535"
server_postgres_ports="tcp/5432"
client_postgres_ports="default"
# Privacy Proxy
server_privoxy_ports="tcp/8118"
client_privoxy_ports="default"
server_radius_ports="udp/1812 udp/1813"
client_radius_ports="default"
server_radiusproxy_ports="udp/1814"
client_radiusproxy_ports="default"
server_radiusold_ports="udp/1645 udp/1646"
client_radiusold_ports="default"
server_radiusoldproxy_ports="udp/1647"
client_radiusoldproxy_ports="default"
server_rdp_ports="tcp/3389"
client_rdp_ports="default"
server_rndc_ports="tcp/953"
client_rndc_ports="default"
server_rsync_ports="tcp/873 udp/873"
client_rsync_ports="default"
server_rtp_ports="udp/10000:20000"
client_rtp_ports="any"
server_sip_ports="udp/5060"
client_sip_ports="default"
server_socks_ports="tcp/1080 udp/1080"
client_socks_ports="default"
server_squid_ports="tcp/3128"
client_squid_ports="default"
server_smtp_ports="tcp/25"
client_smtp_ports="default"
server_smtps_ports="tcp/465"
client_smtps_ports="default"
server_snmp_ports="udp/161"
client_snmp_ports="default"
server_snmptrap_ports="udp/162"
client_snmptrap_ports="any"
server_ssh_ports="tcp/22"
client_ssh_ports="default"
server_stun_ports="udp/3478 udp/3479"
client_stun_ports="any"
# SMTP over SSL/TLS submission
server_submission_ports="tcp/587"
client_submission_ports="default"
# Sun RCP is an alias for service portmap
server_sunrpc_ports="${server_portmap_ports}"
client_sunrpc_ports="${client_portmap_ports}"
server_swat_ports="tcp/901"
client_swat_ports="default"
server_syslog_ports="udp/514"
client_syslog_ports="syslog default"
server_telnet_ports="tcp/23"
client_telnet_ports="default"
server_time_ports="tcp/37 udp/37"
client_time_ports="default"
server_upnp_ports="udp/1900 tcp/2869"
client_upnp_ports="default"
server_uucp_ports="tcp/540"
client_uucp_ports="default"
server_whois_ports="tcp/43"
client_whois_ports="default"
server_vmware_ports="tcp/902"
client_vmware_ports="default"
server_vmwareauth_ports="tcp/903"
client_vmwareauth_ports="default"
server_vmwareweb_ports="tcp/8222"
client_vmwareweb_ports="default"
server_vnc_ports="tcp/5900:5903"
client_vnc_ports="default"
server_webcache_ports="tcp/8080"
client_webcache_ports="default"
server_webmin_ports="tcp/10000"
client_webmin_ports="default"
server_xdmcp_ports="udp/177"
client_xdmcp_ports="default"
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# COMPLEX SERVICES DEFINITIONS
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# The following are definitions for complex services.
# We define as "complex" the services that are implemented using multiple sockets.
# Each function bellow is organized in three parts:
# 1) A Header, common to each and every function
# 2) The rules required for the INPUT of the server
# 3) The rules required for the OUTPUT of the server
#
# The Header part, together with the "reverse" keyword can reverse the rules so
# that if we are implementing a client the INPUT will become OUTPUT and vice versa.
#
# In most the cases the input and output rules are the same with the following
# differences:
#
# a) The output rules begin with the "reverse" keyword, which reverses:
# inface/outface, src/dst, sport/dport
# b) The output rules use ${out}_${mychain} instead of ${in}_${mychain}
# c) The state rules match the client operation, not the server.
# --- DHCP --------------------------------------------------------------------
rules_dhcp() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
set_work_function "Setting up rules for DHCP (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" sport "68" dport "67" || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport "68" dport "67" || return 1
return 0
}
# --- EMULE --------------------------------------------------------------------
rules_emule() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# allow incomming to server tcp/4662
set_work_function "Setting up rules for EMULE/client-to-server tcp/4662 (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp" sport any dport 4662 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp" sport any dport 4662 state ESTABLISHED || return 1
# allow outgoing to client tcp/4662
set_work_function "Setting up rules for EMULE/server-to-client tcp/4662 (${type})"
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp" dport any sport 4662 state NEW,ESTABLISHED || return 1
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp" dport any sport 4662 state ESTABLISHED || return 1
# allow incomming to server udp/4672
set_work_function "Setting up rules for EMULE/client-to-server udp/4672 (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" sport any dport 4672 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport any dport 4672 state ESTABLISHED || return 1
# allow outgoing to client udp/4672
set_work_function "Setting up rules for EMULE/server-to-client udp/4672 (${type})"
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" dport any sport 4672 state NEW,ESTABLISHED || return 1
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" dport any sport 4672 state ESTABLISHED || return 1
# allow incomming to server tcp/4661
set_work_function "Setting up rules for EMULE/client-to-server tcp/4661 (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp" sport any dport 4661 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp" sport any dport 4661 state ESTABLISHED || return 1
# allow incomming to server udp/4665
set_work_function "Setting up rules for EMULE/client-to-server udp/4665 (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" sport any dport 4665 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport any dport 4665 state ESTABLISHED || return 1
return 0
}
# --- HYLAFAX ------------------------------------------------------------------
# Written by: Franscisco Javier Felix <ffelix@gescosoft.com>
rules_hylafax() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# allow incomming to server tcp/4559
set_work_function "Setting up rules for HYLAFAX/client-to-server tcp/4559 (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp" sport any dport 4559 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp" sport any dport 4559 state ESTABLISHED || return 1
# allow outgoing to client from server tcp/4558
set_work_function "Setting up rules for HYLAFAX/server-to-client from server tcp/4558 (${type})"
rule ${out} action "$@" chain "${out}_${mychain}" proto "tcp" sport 4558 dport any state NEW,ESTABLISHED || return 1
rule ${in} reverse action "$@" chain "${in}_${mychain}" proto "tcp" sport 4558 dport any state ESTABLISHED || return 1
return 0
}
# --- SAMBA --------------------------------------------------------------------
rules_samba() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
set_work_function "Setting up rules for SAMBA/NETBIOS-NS (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" sport "137 ${client_ports}" dport 137 state NEW,ESTABLISHED || return 1
# NETBIOS initiates based on the broadcast address of an interface
# (request goes to broadcast address) but the server responds from
# its own IP address. This makes the server samba accept statement
# drop the server reply.
# Bellow is a hack, that allows a linux samba server to respond
# correctly, as it allows new outgoing connections from the well
# known netbios-ns port to the clients high ports.
# For clients and routers this hack is not applied because it
# would be a huge security hole.
if [ "${type}" = "server" -a "${work_cmd}" = "interface" ]
then
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport "137 ${client_ports}" dport 137 state NEW,ESTABLISHED || return 1
else
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport "137 ${client_ports}" dport 137 state ESTABLISHED || return 1
fi
set_work_function "Setting up rules for SAMBA/NETBIOS-DGM (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" sport "138 ${client_ports}" dport 138 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport "138 ${client_ports}" dport 138 state ESTABLISHED || return 1
set_work_function "Setting up rules for SAMBA/NETBIOS-SSN (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp" sport "${client_ports}" dport 139 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp" sport "${client_ports}" dport 139 state ESTABLISHED || return 1
set_work_function "Setting up rules for SAMBA/MICROSOFT_DS (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp" sport "${client_ports}" dport 445 state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp" sport "${client_ports}" dport 445 state ESTABLISHED || return 1
return 0
}
# --- PPTP --------------------------------------------------------------------
rules_pptp() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
set_work_function "Setting up rules for PPTP/initial connection (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp" sport "${client_ports}" dport "1723" state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp" sport "${client_ports}" dport "1723" state ESTABLISHED || return 1
set_work_function "Setting up rules for PPTP/tunnel GRE traffic (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "47" || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "47" || return 1
return 0
}
# --- NFS ----------------------------------------------------------------------
rules_nfs() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# This command requires in the client or route subcommands,
# the first argument after the policy/action is a dst.
local action="${1}"; shift
local servers="localhost"
if [ "${type}" = "client" -o ! "${work_cmd}" = "interface" ]
then
case "${1}" in
dst|DST|destination|DESTINATION)
shift
local servers="${1}"
shift
;;
*)
error "Please re-phrase to: ${type} nfs ${action} dst <NFS_SERVER> [other rules]"
return 1
;;
esac
fi
local x=
for x in ${servers}
do
local tmp="${FIREHOL_DIR}/firehol.rpcinfo.$$.${RANDOM}"
set_work_function "Getting RPC information from server '${x}'"
rpcinfo -p ${x} >"${tmp}"
if [ $? -gt 0 -o ! -s "${tmp}" ]
then
error "Cannot get rpcinfo from host '${x}' (using the previous firewall rules)"
${RM_CMD} -f "${tmp}"
return 1
fi
local server_rquotad_ports="`${CAT_CMD} "${tmp}" | ${GREP_CMD} " rquotad$" | ( while read a b proto port s; do echo "$proto/$port"; done ) | ${SORT_CMD} | ${UNIQ_CMD}`"
local server_mountd_ports="`${CAT_CMD} "${tmp}" | ${GREP_CMD} " mountd$" | ( while read a b proto port s; do echo "$proto/$port"; done ) | ${SORT_CMD} | ${UNIQ_CMD}`"
local server_lockd_ports="`${CAT_CMD} "${tmp}" | ${GREP_CMD} " nlockmgr$" | ( while read a b proto port s; do echo "$proto/$port"; done ) | ${SORT_CMD} | ${UNIQ_CMD}`"
local server_nfsd_ports="`${CAT_CMD} "${tmp}" | ${GREP_CMD} " nfs$" | ( while read a b proto port s; do echo "$proto/$port"; done ) | ${SORT_CMD} | ${UNIQ_CMD}`"
test -z "${server_mountd_ports}" && error "Cannot find mountd ports for nfs server '${x}'" && return 1
test -z "${server_lockd_ports}" && error "Cannot find lockd ports for nfs server '${x}'" && return 1
test -z "${server_nfsd_ports}" && error "Cannot find nfsd ports for nfs server '${x}'" && return 1
local dst=
if [ ! "${x}" = "localhost" ]
then
dst="dst ${x}"
fi
if [ ! -z "${server_rquotad_ports}" ]
then
set_work_function "Processing rquotad rules for server '${x}'"
rules_custom "${mychain}" "${type}" nfs-rquotad "${server_rquotad_ports}" "500:65535" "${action}" $dst "$@"
fi
set_work_function "Processing mountd rules for server '${x}'"
rules_custom "${mychain}" "${type}" nfs-mountd "${server_mountd_ports}" "500:65535" "${action}" $dst "$@"
set_work_function "Processing lockd rules for server '${x}'"
rules_custom "${mychain}" "${type}" nfs-lockd "${server_lockd_ports}" "500:65535" "${action}" $dst "$@"
set_work_function "Processing nfsd rules for server '${x}'"
rules_custom "${mychain}" "${type}" nfs-nfsd "${server_nfsd_ports}" "500:65535" "${action}" $dst "$@"
${RM_CMD} -f "${tmp}"
echo >&2 ""
echo >&2 "WARNING:"
echo >&2 "This firewall must be restarted if NFS server ${x} is restarted!"
echo >&2 ""
done
return 0
}
# --- NIS ----------------------------------------------------------------------
# These rules work for client access only!
#
# Pushing changes to slave servers won't work if these rules are active
# somewhere between the master and its slaves, because it is impossible to
# predict the ports where "yppush" will be listening on each push.
#
# Pulling changes directly on the slaves will work, and could be improved
# performance-wise if these rules are modified to open "fypxfrd". This wasn't
# done because it doesn't make that much sense since pushing changes on the
# master server is the most common, and recommended, way to replicate maps.
#
# Created by Carlos Rodrigues <crlf@users.sourceforge.net>
# Feature Requests item #1050951 <https://sourceforge.net/tracker/?func=detail&atid=487695&aid=1050951&group_id=58425>
rules_nis() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# This command requires in the client or route subcommands,
# the first argument after the policy/action is a dst.
local action="${1}"; shift
local servers="localhost"
if [ "${type}" = "client" -o ! "${work_cmd}" = "interface" ]
then
case "${1}" in
dst|DST|destination|DESTINATION)
shift
local servers="${1}"
shift
;;
*)
error "Please re-phrase to: ${type} nis ${action} dst <NIS_SERVER> [other rules]"
return 1
;;
esac
fi
local x=
for x in ${servers}
do
local tmp="${FIREHOL_DIR}/firehol.rpcinfo.$$.${RANDOM}"
set_work_function "Getting RPC information from server '${x}'"
rpcinfo -p ${x} >"${tmp}"
if [ $? -gt 0 -o ! -s "${tmp}" ]
then
error "Cannot get rpcinfo from host '${x}' (using the previous firewall rules)"
${RM_CMD} -f "${tmp}"
return 1
fi
local server_ypserv_ports="`${CAT_CMD} "${tmp}" | ${GREP_CMD} " ypserv$" | ( while read a b proto port s; do echo "$proto/$port"; done ) | ${SORT_CMD} | ${UNIQ_CMD}`"
local server_yppasswdd_ports="`${CAT_CMD} "${tmp}" | ${GREP_CMD} " yppasswdd$" | ( while read a b proto port s; do echo "$proto/$port"; done ) | ${SORT_CMD} | ${UNIQ_CMD}`"
test -z "${server_ypserv_ports}" && error "Cannot find ypserv ports for nis server '${x}'" && return 1
local dst=
if [ ! "${x}" = "localhost" ]
then
dst="dst ${x}"
fi
if [ ! -z "${server_yppasswd_ports}" ]
then
set_work_function "Processing yppasswd rules for server '${x}'"
rules_custom "${mychain}" "${type}" nis-yppasswd "${server_yppasswdd_ports}" "500:65535" "${action}" $dst "$@"
fi
set_work_function "Processing ypserv rules for server '${x}'"
rules_custom "${mychain}" "${type}" nis-ypserv "${server_ypserv_ports}" "500:65535" "${action}" $dst "$@"
${RM_CMD} -f "${tmp}"
echo >&2 ""
echo >&2 "WARNING:"
echo >&2 "This firewall must be restarted if NIS server ${x} is restarted!"
echo >&2 ""
done
return 0
}
# --- AMANDA -------------------------------------------------------------------
FIREHOL_AMANDA_PORTS="850:859"
rules_amanda() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
set_work_function "*** AMANDA: See http://amanda.sourceforge.net/fom-serve/cache/139.html"
set_work_function "Setting up rules for initial amanda server-to-client connection"
rule ${out} action "$@" chain "${out}_${mychain}" proto "udp" dport 10080 state NEW,ESTABLISHED || return 1
rule ${in} reverse action "$@" chain "${in}_${mychain}" proto "udp" dport 10080 state ESTABLISHED || return 1
set_work_function "Setting up rules for amanda data exchange client-to-server"
rule ${in} action "$@" chain "${in}_${mychain}" proto "tcp udp" dport "${FIREHOL_AMANDA_PORTS}" state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "tcp udp" dport "${FIREHOL_AMANDA_PORTS}" state ESTABLISHED || return 1
return 0
}
# --- FTP ----------------------------------------------------------------------
ALL_SHOULD_ALSO_RUN="${ALL_SHOULD_ALSO_RUN} ftp"
rules_ftp() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# For an explanation of how FTP connections work, see
# http://slacksite.com/other/ftp.html
# ----------------------------------------------------------------------
# allow new and established incoming, and established outgoing
# accept port ftp new connections
set_work_function "Setting up rules for initial FTP connection ${type}"
rule ${in} action "$@" chain "${in}_${mychain}" proto tcp sport "${client_ports}" dport ftp state NEW,ESTABLISHED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto tcp sport "${client_ports}" dport ftp state ESTABLISHED || return 1
# Active FTP
# send port ftp-data related connections
set_work_function "Setting up rules for Active FTP ${type}"
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto tcp sport "${client_ports}" dport ftp-data state ESTABLISHED,RELATED || return 1
rule ${in} action "$@" chain "${in}_${mychain}" proto tcp sport "${client_ports}" dport ftp-data state ESTABLISHED || return 1
# ----------------------------------------------------------------------
# A hack for Passive FTP only
local s_client_ports="${DEFAULT_CLIENT_PORTS}"
local c_client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
c_client_ports="${LOCAL_CLIENT_PORTS}"
elif [ "${type}" = "server" -a "${work_cmd}" = "interface" ]
then
s_client_ports="${LOCAL_CLIENT_PORTS}"
fi
# Passive FTP
# accept high-ports related connections
set_work_function "Setting up rules for Passive FTP ${type}"
rule ${in} action "$@" chain "${in}_${mychain}" proto tcp sport "${c_client_ports}" dport "${s_client_ports}" state ESTABLISHED,RELATED || return 1
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto tcp sport "${c_client_ports}" dport "${s_client_ports}" state ESTABLISHED || return 1
require_kernel_module ip_conntrack_ftp
test ${FIREHOL_NAT} -eq 1 && require_kernel_module ip_nat_ftp
return 0
}
# --- TFTP ---------------------------------------------------------------------
# Written by: Goetz Bock <bock@blacknet.de>
rules_tftp() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ---------------------------------------------------------------------
# TFTP is a broken protokol. It works like this:
#
# 1. The client sends from a high port (a) to the server's tftp port an
# udp packet with "give me file 'bla'".
#
# 2. The server replies from a high port (b) to the highport the client
# used (a) with "this is part 0 if your file"
#
# 3. The client now has to send a reply (from his highport a) to the
# servers high port (b): "got part 0, send next part 1".
#
# 4. repeat 2. and 3. till file transmitted
# allow the initial TFTP connection
set_work_function "Setting up rules for initial TFTP connection (${type})"
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" sport "${client_ports}" dport tftp state NEW,ESTABLISHED || return 1
# rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport "${client_ports}" dport tftp state ESTABLISHED || return 1
# We now need both server and client port ranges
local s_client_ports="${DEFAULT_CLIENT_PORTS}"
local c_client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
c_client_ports="${LOCAL_CLIENT_PORTS}"
elif [ "${type}" = "server" -a "${work_cmd}" = "interface" ]
then
s_client_ports="${LOCAL_CLIENT_PORTS}"
fi
# allow the TFTP server to establish a new connection to the client
set_work_function "Setting up rules for server-to-client TFTP connection (${type})"
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "udp" sport "${c_client_ports}" dport "${s_client_ports}" state RELATED,ESTABLISHED || return 1
rule ${in} action "$@" chain "${in}_${mychain}" proto "udp" sport "${c_client_ports}" dport "${s_client_ports}" state ESTABLISHED || return 1
require_kernel_module ip_conntrack_tftp
test ${FIREHOL_NAT} -eq 1 && require_kernel_module ip_nat_tftp
return 0
}
# --- PING ---------------------------------------------------------------------
rules_ping() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# allow incoming new and established PING packets
rule ${in} action "$@" chain "${in}_${mychain}" proto icmp custom "--icmp-type echo-request" state NEW,ESTABLISHED || return 1
# allow outgoing established packets
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto icmp custom "--icmp-type echo-reply" state ESTABLISHED || return 1
return 0
}
# --- TIMESTAMP ----------------------------------------------------------------
rules_timestamp() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# allow incoming new and established PING packets
rule ${in} action "$@" chain "${in}_${mychain}" proto icmp custom "--icmp-type timestamp-request" state NEW,ESTABLISHED || return 1
# allow outgoing established packets
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto icmp custom "--icmp-type timestamp-reply" state ESTABLISHED || return 1
return 0
}
# --- P2P ----------------------------------------------------------------------
rules_p2p() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# Remove the action from the arguments.
shift
do_in() {
# allow new and established incoming packets
rule ${in} action "$@" chain "${in}_${mychain}" state NEW,ESTABLISHED || return 1
}
do_out() {
# allow outgoing established packets
rule ${out} reverse action "$@" chain "${out}_${mychain}" state NEW,ESTABLISHED || return 1
}
# Kazaa
# Check: http://www.derkeiler.com/Mailing-Lists/Firewall-Wizards/2003-06/0152.html
# New clients will try to use port 80 - use a proxy to filter this too.
set_work_function "Setting up rules for Kazaa (${type})"
do_in drop "$@" proto "tcp udp" sport 1214
do_in drop "$@" proto "tcp udp" dport 1214
do_out drop "$@" proto "tcp udp" dport 1214
do_out drop "$@" proto "tcp udp" sport 1214
# Gnutella
# Mldonkey
# Emule
# audiogalaxy
# hotline
return 0
}
# --- ALL ----------------------------------------------------------------------
rules_all() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# allow new and established incoming packets
rule ${in} action "$@" chain "${in}_${mychain}" state NEW,ESTABLISHED || return 1
# allow outgoing established packets
rule ${out} reverse action "$@" chain "${out}_${mychain}" state ESTABLISHED || return 1
local ser=
for ser in ${ALL_SHOULD_ALSO_RUN}
do
"${type}" ${ser} "$@" || return 1
done
return 0
}
# --- ANY ----------------------------------------------------------------------
rules_any() {
local mychain="${1}"; shift
local type="${1}"; shift
local name="${1}"; shift # a special case: service any gets a name
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# allow new and established incoming packets
rule ${in} action "$@" chain "${in}_${mychain}" state NEW,ESTABLISHED || return 1
# allow outgoing established packets
rule ${out} reverse action "$@" chain "${out}_${mychain}" state ESTABLISHED || return 1
return 0
}
# --- ANYSTATELESS -------------------------------------------------------------
rules_anystateless() {
local mychain="${1}"; shift
local type="${1}"; shift
local name="${1}"; shift # a special case: service any gets a name
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# allow new and established incoming packets
rule ${in} action "$@" chain "${in}_${mychain}" || return 1
# allow outgoing established packets
rule ${out} reverse action "$@" chain "${out}_${mychain}" || return 1
return 0
}
# --- MULTICAST ----------------------------------------------------------------
rules_multicast() {
local mychain="${1}"; shift
local type="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
# match multicast packets in both directions
rule ${out} action "$@" chain "${out}_${mychain}" dst "224.0.0.0/8" proto 2 || return 1
rule ${in} reverse action "$@" chain "${in}_${mychain}" src "224.0.0.0/8" proto 2 || return 1
return 0
}
# --- CUSTOM -------------------------------------------------------------------
rules_custom() {
local mychain="${1}"; shift
local type="${1}"; shift
local server="${1}"; shift
local my_server_ports="${1}"; shift
local my_client_ports="${1}"; shift
local in=in
local out=out
if [ "${type}" = "client" ]
then
in=out
out=in
fi
local client_ports="${DEFAULT_CLIENT_PORTS}"
if [ "${type}" = "client" -a "${work_cmd}" = "interface" ]
then
client_ports="${LOCAL_CLIENT_PORTS}"
fi
# ----------------------------------------------------------------------
local sp=
for sp in ${my_server_ports}
do
local proto=
local sport=
IFS="/" read proto sport <<EOF
$sp
EOF
local cp=
for cp in ${my_client_ports}
do
local cport=
case ${cp} in
default)
cport="${client_ports}"
;;
*) cport="${cp}"
;;
esac
# allow new and established incoming packets
rule ${in} action "$@" chain "${in}_${mychain}" proto "${proto}" sport "${cport}" dport "${sport}" state NEW,ESTABLISHED || return 1
# allow outgoing established packets
rule ${out} reverse action "$@" chain "${out}_${mychain}" proto "${proto}" sport "${cport}" dport "${sport}" state ESTABLISHED || return 1
done
done
return 0
}
# ------------------------------------------------------------------------------
# The caller may need just our services definitions
if [ "$1" = "gimme-the-services-defs" ]
then
return 0
exit 1
fi
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# SUPPORT FOR EXTERNAL DEFINITIONS OF SERVICES
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# Load all the services.
# All these files should start with: #FHVER: 1
cd "${FIREHOL_CONFIG_DIR}/services" || exit 1
for f in `ls *.conf 2>/dev/null`
do
cd "${FIREHOL_CONFIG_DIR}/services" || exit 1
if [ ! -O "${f}" ]
then
echo >&2 " >>> Ignoring service in '${FIREHOL_CONFIG_DIR}/services/${f}' because it is not owned by root."
continue
fi
n=`"${HEAD_CMD}" -n 1 "${f}" | "${CUT_CMD}" -d ':' -f 2`
"${EXPR_CMD}" ${n} + 0 >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo >&2 " >>> Ignoring service in '${FIREHOL_CONFIG_DIR}/services/${f}' due to malformed header."
elif [ ${n} -ne ${FIREHOL_SERVICES_API} ]
then
echo >&2 " >>> Ignoring service '${FIREHOL_CONFIG_DIR}/services/${f}' due to incompatible API version."
else
n=`"${HEAD_CMD}" -n 1 "${f}" | "${CUT_CMD}" -d ':' -f 3`
"${EXPR_CMD}" ${n} + 0 >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo >&2 " >>> Ignoring service in '${FIREHOL_CONFIG_DIR}/services/${f}' due to malformed API minor number."
else
if [ ${n} -gt ${FIREHOL_MINOR_VERSION} ]
then
echo >&2 " >>> Ignoring service in '${FIREHOL_CONFIG_DIR}/services/${f}' because the required MINOR version (${n}) is higher than the one provided by FireHOL (${FIREHOL_MINOR_VERSION})."
else
source ${f}
ret=$?
if [ ${ret} -ne 0 ]
then
echo >&2 " >>> Service in '${FIREHOL_CONFIG_DIR}/services/${f}' returned code ${ret}."
continue
fi
fi
fi
fi
done
cd "${FIREHOL_DEFAULT_WORKING_DIRECTORY}" || exit 1
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# HELPER FUNCTIONS BELLOW THIS POINT
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# Fetch a URL and output it to the standard output.
firehol_wget() {
local url="${1}"
require_cmd wget curl
if [ ! -z "${WGET_CMD}" ]
then
${WGET_CMD} -O - "${url}" 2>/dev/null
return $?
elif [ ! -z "${CURL_CMD}" ]
then
${CURL_CMD} -s "${url}"
return $?
fi
error "Cannot use either 'wget' or 'curl' to fetch '${url}'."
return 1
}
FIREHOL_ECN_SHAME_URL="http://urchin.earth.li/cgi-bin/ecn.pl?output=ip"
ecn_shame() {
work_realcmd_helper ${FUNCNAME} "$@"
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition."; return 1 )
if [ `${CAT_CMD} /proc/sys/net/ipv4/tcp_ecn` -eq 1 ]
then
set_work_function "Fetching '${FIREHOL_ECN_SHAME_URL}'."
# Reads in list of ip address and makes iptables rules to drop ecn
# from packets destined for those hosts.
# http://urchin.earth.li/ecn/
local tmp="${FIREHOL_DIR}/ecn_shame.ips"
firehol_wget "${FIREHOL_ECN_SHAME_URL}" | ${SORT_CMD} | ${UNIQ_CMD} >"${tmp}"
if [ $? -ne 0 -o ! -s "${tmp}" ]
then
softwarning "Cannot fetch '${FIREHOL_ECN_SHAME_URL}'."
else
${MV_CMD} -f "${tmp}" "${FIREHOL_SPOOL_DIR}/ecn_shame.ips"
fi
set_work_function "Removing ECN for all communication from/to SHAME ECN list."
local count=0
for ip in `${CAT_CMD} "${FIREHOL_SPOOL_DIR}/ecn_shame.ips"`
do
local count=$[count + 1]
iptables -t mangle -A POSTROUTING -p tcp -d ${ip} -j ECN --ecn-tcp-remove
done
test ${count} -eq 0 && softwarning "No ECN SHAME IPs found." && return 1
else
softwarning "TCP_ECN is not enabled in the kernel. ECN_SHAME helper is ignored."
return 0
fi
return 0
}
masquerade() {
work_realcmd_helper ${FUNCNAME} "$@"
set_work_function -ne "Initializing $FUNCNAME"
local f="${work_outface}"
test "${1}" = "reverse" && f="${work_inface}" && shift
test -z "${f}" && local f="${1}" && shift
test -z "${f}" && error "masquerade requires an interface set or as argument" && return 1
set_work_function "Initializing masquerade on interface '${f}'"
rule noowner table nat chain POSTROUTING "$@" inface any outface "${f}" action MASQUERADE || return 1
FIREHOL_NAT=1
FIREHOL_ROUTING=1
return 0
}
transparent_proxy_count=0
transparent_proxy() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition."; return 1 )
local ports="${1}"; shift
local redirect="${1}"; shift
local user="${1}"; shift
test -z "${redirect}" && error "Proxy listening port is empty" && return 1
transparent_proxy_count=$[transparent_proxy_count + 1]
set_work_function "Setting up rules for catching routed tcp/${ports} traffic"
create_chain nat "in_trproxy.${transparent_proxy_count}" PREROUTING noowner "$@" outface any proto tcp sport "${DEFAULT_CLIENT_PORTS}" dport "${ports}" || return 1
# rule table nat chain "in_trproxy.${transparent_proxy_count}" proto tcp dport "${ports}" action REDIRECT to-port ${redirect} || return 1
rule table nat chain "in_trproxy.${transparent_proxy_count}" proto tcp action REDIRECT to-port ${redirect} || return 1
if [ ! -z "${user}" ]
then
set_work_function "Setting up rules for catching outgoing tcp/${ports} traffic"
create_chain nat "out_trproxy.${transparent_proxy_count}" OUTPUT "$@" uid not "${user}" nosoftwarnings inface any outface any src any proto tcp sport "${LOCAL_CLIENT_PORTS}" dport "${ports}" || return 1
# do not catch traffic for localhost servers
rule table nat chain "out_trproxy.${transparent_proxy_count}" dst "127.0.0.1" action RETURN || return 1
# rule table nat chain "out_trproxy.${transparent_proxy_count}" proto tcp dport "${ports}" action REDIRECT to-port ${redirect} || return 1
rule table nat chain "out_trproxy.${transparent_proxy_count}" proto tcp action REDIRECT to-port ${redirect} || return 1
fi
FIREHOL_NAT=1
FIREHOL_ROUTING=1
return 0
}
transparent_squid() {
transparent_proxy 80 "$@"
}
nat_count=0
nat_helper() {
# work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "NAT cannot be used in '${work_cmd}'. Put all NAT related commands before any '${work_cmd}' definition."; return 1 )
local type="${1}"; shift
local to="${1}"; shift
nat_count=$[nat_count + 1]
set_work_function -ne "Setting up rules for NAT type: '${type}'"
case ${type} in
to-source)
create_chain nat "nat.${nat_count}" POSTROUTING nolog "$@" inface any || return 1
local action=snat
;;
to-destination)
create_chain nat "nat.${nat_count}" PREROUTING noowner nolog "$@" outface any || return 1
local action=dnat
;;
redirect-to)
create_chain nat "nat.${nat_count}" PREROUTING noowner nolog "$@" outface any || return 1
local action=redirect
;;
*)
error "$FUNCNAME requires a type (i.e. to-source, to-destination, redirect-to, etc) as its first argument. '${type}' is not understood."
return 1
;;
esac
set_work_function "Taking the NAT action: '${action}'"
# we now need to keep the protocol
rule table nat chain "nat.${nat_count}" noowner "$@" action "${action}" to "${to}" nosoftwarnings src any dst any inface any outface any sport any dport any || return 1
FIREHOL_NAT=1
FIREHOL_ROUTING=1
return 0
}
nat() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
nat_helper "$@"
}
snat() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
local to="${1}"; shift
test "${to}" = "to" && local to="${1}" && shift
nat_helper "to-source" "${to}" "$@"
}
dnat() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
local to="${1}"; shift
test "${to}" = "to" && local to="${1}" && shift
nat_helper "to-destination" "${to}" "$@"
}
redirect() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
local to="${1}"; shift
test "${to}" = "to" -o "${to}" = "to-port" && local to="${1}" && shift
nat_helper "redirect-to" "${to}" "$@"
}
wrongmac_chain=0
mac() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition."; return 1 )
if [ ${wrongmac_chain} -eq 0 ]
then
set_work_function "Creating the MAC-MISSMATCH chain (only once)"
iptables -t filter -N WRONGMAC
rule table filter chain WRONGMAC loglimit "MAC MISSMATCH" action DROP || return 1
wrongmac_chain=1
fi
set_work_function "If the source IP ${1} does not match MAC ${2}, drop the packet"
iptables -t filter -A INPUT -s "${1}" -m mac --mac-source ! "${2}" -j WRONGMAC
iptables -t filter -A FORWARD -s "${1}" -m mac --mac-source ! "${2}" -j WRONGMAC
return 0
}
# blacklist creates two types of blacklists: unidirectional or bidirectional
blacklist_chain=0
blacklist() {
work_realcmd_helper ${FUNCNAME} "$@"
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition."; return 1 )
local full=1
if [ "${1}" = "them" -o "${1}" = "him" -o "${1}" = "her" -o "${1}" = "it" -o "${1}" = "this" -o "${1}" = "these" -o "${1}" = "input" ]
then
shift
full=0
elif [ "${1}" = "all" -o "${1}" = "full" ]
then
shift
full=1
fi
if [ ${blacklist_chain} -eq 0 ]
then
set_work_function "Generating blacklist chains"
# Blacklist INPUT unidirectional
iptables -t filter -N BL_IN_UNI # INPUT
iptables -A BL_IN_UNI -m state --state NEW -j DROP
# No need for OUTPUT/FORWARD unidirectional
# Blacklist INPUT bidirectional
iptables -t filter -N BL_IN_BI # INPUT
iptables -A BL_IN_BI -j DROP
# Blacklist OUTPUT/FORWARD bidirectional
iptables -t filter -N BL_OUT_BI # OUTPUT and FORWARD
iptables -A BL_OUT_BI -p tcp -j REJECT --reject-with tcp-reset
iptables -A BL_OUT_BI -j REJECT --reject-with icmp-host-unreachable
blacklist_chain=1
fi
set_work_function "Generating blacklist rules"
local z=
for z in $@
do
local x=
for x in ${z}
do
set_work_function "Blacklisting '${x}'"
if [ ${full} -eq 1 ]
then
iptables -I INPUT -s ${x} -j BL_IN_BI
iptables -I FORWARD -s ${x} -j BL_IN_BI
iptables -I OUTPUT -d ${x} -j BL_OUT_BI
iptables -I FORWARD -d ${x} -j BL_OUT_BI
else
iptables -I INPUT -s ${x} -j BL_IN_UNI
iptables -I FORWARD -s ${x} -j BL_IN_UNI
fi
done
done
return 0
}
mark_count=0
mark() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition."; return 1 )
local num="${1}"; shift
local where="${1}"; shift
test -z "${where}" && where=OUTPUT
mark_count=$[mark_count + 1]
set_work_function "Setting up rules for MARK"
create_chain mangle "mark.${mark_count}" "${where}" "$@" || return 1
iptables -t mangle -A "mark.${mark_count}" -j MARK --set-mark ${num}
return 0
}
tos_count=0
tos() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition."; return 1 )
local num="${1}"; shift
local where="${1}"; shift
test -z "${where}" && where=OUTPUT
tos_count=$[tos_count + 1]
set_work_function "Setting up rules for TOS"
create_chain mangle "tos.${tos_count}" "${where}" "$@" || return 1
iptables -t mangle -A "tos.${tos_count}" -j TOS --set-tos ${num}
return 0
}
dscp_count=0
dscp() {
work_realcmd=($FUNCNAME "$@")
set_work_function -ne "Initializing $FUNCNAME"
require_work clear || ( error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition."; return 1 )
local value="${1}"; shift
local class=""
if [ "${value}" = "class" ]
then
local value=""
local class="${1}"; shift
fi
local where="${1}"; shift
test -z "${where}" && where=OUTPUT
dscp_count=$[dscp_count + 1]
set_work_function "Setting up rules for setting DSCP"
create_chain mangle "dscp.${dscp_count}" "${where}" "$@" || return 1
if [ ! -z "${class}" ]
then
iptables -t mangle -A "dscp.${dscp_count}" -j DSCP --set-dscp-class ${class}
else
iptables -t mangle -A "dscp.${dscp_count}" -j DSCP --set-dscp ${value}
fi
return 0
}
tcpmss() {
work_realcmd_helper $FUNCNAME "$@"
set_work_function -ne "Initializing $FUNCNAME"
# work only if this helper is called before any primary command
# or within routers.
if [ -z "${work_cmd}" -o "${work_cmd}" = "router" ]
then
local chains="FORWARD"
test ! -z "${work_cmd}" && chains="in_${work_name} out_${work_name}"
for tcmpmss_chain in ${chains}
do
case $1 in
auto)
iptables -A "${tcmpmss_chain}" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
;;
[0-9]*)
iptables -A "${tcmpmss_chain}" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss $1
;;
*)
error "$FUNCNAME requires either the word 'auto' or a numeric argument."
return 1
;;
esac
done
else
error "$FUNCNAME cannot be used in '${work_cmd}'. Put it before any '${work_cmd}' definition or in 'router' definitions."
return 1
fi
return 0
}
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# INTERNAL FUNCTIONS BELLOW THIS POINT - Primary commands
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Check the version required by the configuration file
# WHY:
# We have to make sure the configuration file has been written for this version
# of FireHOL. Note that the version command does not actually check the version
# of firehol.sh. It checks only its release number (R5 currently).
version() {
work_realcmd_helper ${FUNCNAME} "$@"
if [ ${1} -gt ${FIREHOL_VERSION} ]
then
error "Wrong version. FireHOL is v${FIREHOL_VERSION}, your script requires v${1}."
fi
}
# ------------------------------------------------------------------------------
# PRIMARY COMMAND: interface
# Setup rules specific to an interface (physical or logical)
interface() {
work_realcmd_primary ${FUNCNAME} "$@"
# --- close any open command ---
close_cmd || return 1
# --- test prerequisites ---
require_work clear || return 1
set_work_function -ne "Initializing $FUNCNAME"
# --- get paramaters and validate them ---
# Get the interface
local inface="${1}"; shift
test -z "${inface}" && error "real interface is not set" && return 1
# Get the name for this interface
local name="${1}"; shift
test -z "${name}" && error "$FUNCNAME name is not set" && return 1
# --- do the job ---
work_cmd="${FUNCNAME}"
work_name="${name}"
work_realcmd=("(unset)")
set_work_function -ne "Initializing $FUNCNAME '${work_name}'"
create_chain filter "in_${work_name}" INPUT in set_work_inface "$@" inface "${inface}" outface any || return 1
create_chain filter "out_${work_name}" OUTPUT out set_work_outface reverse "$@" inface "${inface}" outface any || return 1
return 0
}
router() {
work_realcmd_helper ${FUNCNAME} "$@"
# --- close any open command ---
close_cmd || return 1
# --- test prerequisites ---
require_work clear || return 1
set_work_function -ne "Initializing $FUNCNAME"
# --- get paramaters and validate them ---
# Get the name for this router
local name="${1}"; shift
test -z "${name}" && error "$FUNCNAME name is not set" && return 1
# --- do the job ---
work_cmd="${FUNCNAME}"
work_name="${name}"
work_realcmd=("(unset)")
set_work_function -ne "Initializing $FUNCNAME '${work_name}'"
create_chain filter "in_${work_name}" FORWARD in set_work_inface set_work_outface "$@" || return 1
create_chain filter "out_${work_name}" FORWARD out reverse "$@" || return 1
FIREHOL_ROUTING=1
return 0
}
postprocess() {
# work_realcmd_helper ${FUNCNAME} "$@"
local check="error"
test "A${1}" = "A-ne" && shift && local check="none"
test "A${1}" = "A-warn" && shift && local check="warn"
test ${FIREHOL_DEBUG} -eq 1 && local check="none"
test ${FIREHOL_EXPLAIN} -eq 1 && local check="none"
if [ ! ${check} = "none" ]
then
printf "runcmd '${check}' '${FIREHOL_LINEID}' " >>${FIREHOL_OUTPUT}
fi
printf "%q " "$@" >>${FIREHOL_OUTPUT}
printf "\n" >>${FIREHOL_OUTPUT}
if [ ${FIREHOL_EXPLAIN} -eq 1 ]
then
${CAT_CMD} ${FIREHOL_OUTPUT}
${RM_CMD} -f ${FIREHOL_OUTPUT}
fi
return 0
}
runcmd() {
local check="${1}"; shift
local line="${1}"; shift
local cmd="${1}"; shift
"${cmd}" "$@" >${FIREHOL_OUTPUT}.log 2>&1
local r=$?
test ${r} -gt 0 && runtime_error ${check} ${r} ${line} "${cmd}" "$@"
return 0
}
FIREHOL_COMMAND_COUNTER=0
iptables() {
# work_realcmd_helper ${FUNCNAME} "$@"
postprocess "${IPTABLES_CMD}" "$@"
FIREHOL_COMMAND_COUNTER=$[FIREHOL_COMMAND_COUNTER + 1]
return 0
}
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# INTERNAL FUNCTIONS BELLOW THIS POINT - Sub-commands
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Change the policy of an interface
# WHY:
# Not all interfaces have the same policy. The admin must have control over it.
# Here we just set what the admin wants. At the interface finalization we
# produce the iptables rules.
policy() {
work_realcmd_secondary ${FUNCNAME} "$@"
require_work set interface || return 1
set_work_function "Setting interface '${work_inface}' (${work_name}) policy to ${1}"
work_policy="$*"
return 0
}
server() {
work_realcmd_secondary ${FUNCNAME} "$@"
require_work set any || return 1
smart_function server "$@"
return $?
}
client() {
work_realcmd_secondary ${FUNCNAME} "$@"
require_work set any || return 1
smart_function client "$@"
return $?
}
route() {
work_realcmd_secondary ${FUNCNAME} "$@"
require_work set router || return 1
smart_function server "$@"
return $?
}
# --- protection ---------------------------------------------------------------
protection() {
work_realcmd_secondary ${FUNCNAME} "$@"
require_work set any || return 1
local in="in"
local prface="${work_inface}"
local pre="pr"
local reverse=
if [ "${1}" = "reverse" ]
then
local reverse="reverse" # needed to recursion
local pre="prr" # in case a router has protections
# both ways, the second needs to
# have different chain names
local in="out" # reverse the interface
prface="${work_outface}"
shift
fi
local type="${1}"
local rate="${2}"
local burst="${3}"
test -z "${rate}" && rate="100/s"
test -z "${burst}" && burst="50"
set_work_function -ne "Generating protections on '${prface}' for ${work_cmd} '${work_name}'"
local x=
for x in ${type}
do
case "${x}" in
none|NONE)
return 0
;;
strong|STRONG|full|FULL|all|ALL)
protection ${reverse} "invalid fragments new-tcp-w/o-syn icmp-floods syn-floods malformed-xmas malformed-null malformed-bad" "${rate}" "${burst}"
return $?
;;
invalid|INVALID)
iptables -A "${in}_${work_name}" -m state --state INVALID -j DROP || return 1
;;
fragments|FRAGMENTS)
local mychain="${pre}_${work_name}_fragments"
create_chain filter "${mychain}" "${in}_${work_name}" in custom "-f" || return 1
set_work_function "Generating rules to be protected from packet fragments on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${mychain}" loglimit "PACKET FRAGMENTS" action drop || return 1
;;
new-tcp-w/o-syn|NEW-TCP-W/O-SYN)
local mychain="${pre}_${work_name}_nosyn"
create_chain filter "${mychain}" "${in}_${work_name}" in proto tcp state NEW custom "! --syn" || return 1
set_work_function "Generating rules to be protected from new TCP connections without the SYN flag set on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${mychain}" loglimit "NEW TCP w/o SYN" action drop || return 1
;;
icmp-floods|ICMP-FLOODS)
local mychain="${pre}_${work_name}_icmpflood"
create_chain filter "${mychain}" "${in}_${work_name}" in proto icmp custom "--icmp-type echo-request" || return 1
set_work_function "Generating rules to be protected from ICMP floods on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${mychain}" limit "${rate}" "${burst}" action return || return 1
rule in chain "${mychain}" loglimit "ICMP FLOOD" action drop || return 1
;;
syn-floods|SYN-FLOODS)
local mychain="${pre}_${work_name}_synflood"
create_chain filter "${mychain}" "${in}_${work_name}" in proto tcp custom "--syn" || return 1
set_work_function "Generating rules to be protected from TCP SYN floods on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${mychain}" limit "${rate}" "${burst}" action return || return 1
rule in chain "${mychain}" loglimit "SYN FLOOD" action drop || return 1
;;
all-floods|ALL-FLOODS)
local mychain="${pre}_${work_name}_allflood"
create_chain filter "${mychain}" "${in}_${work_name}" in state NEW || return 1
set_work_function "Generating rules to be protected from ALL floods on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${mychain}" limit "${rate}" "${burst}" action return || return 1
rule in chain "${mychain}" loglimit "ALL FLOOD" action drop || return 1
;;
malformed-xmas|MALFORMED-XMAS)
local mychain="${pre}_${work_name}_malxmas"
create_chain filter "${mychain}" "${in}_${work_name}" in proto tcp custom "--tcp-flags ALL ALL" || return 1
set_work_function "Generating rules to be protected from packets with all TCP flags set on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${mychain}" loglimit "MALFORMED XMAS" action drop || return 1
;;
malformed-null|MALFORMED-NULL)
local mychain="${pre}_${work_name}_malnull"
create_chain filter "${mychain}" "${in}_${work_name}" in proto tcp custom "--tcp-flags ALL NONE" || return 1
set_work_function "Generating rules to be protected from packets with all TCP flags unset on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${mychain}" loglimit "MALFORMED NULL" action drop || return 1
;;
malformed-bad|MALFORMED-BAD)
local mychain="${pre}_${work_name}_malbad"
create_chain filter "${mychain}" "${in}_${work_name}" in proto tcp custom "--tcp-flags SYN,FIN SYN,FIN" || return 1
set_work_function "Generating rules to be protected from packets with illegal TCP flags on '${prface}' for ${work_cmd} '${work_name}'"
rule in chain "${in}_${work_name}" action "${mychain}" proto tcp custom "--tcp-flags SYN,RST SYN,RST" || return 1
rule in chain "${in}_${work_name}" action "${mychain}" proto tcp custom "--tcp-flags ALL SYN,RST,ACK,FIN,URG" || return 1
rule in chain "${in}_${work_name}" action "${mychain}" proto tcp custom "--tcp-flags ALL FIN,URG,PSH" || return 1
rule in chain "${mychain}" loglimit "MALFORMED BAD" action drop || return 1
;;
*)
error "Protection '${x}' does not exists."
return 1
;;
esac
done
return 0
}
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# KERNEL MODULE MANAGEMENT
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Manage kernel modules
# WHY:
# We need to load a set of kernel modules during postprocessing, and after the
# new firewall has been activated. Here we just keep a list of the required
# kernel modules.
# optionaly require command gzcat
require_cmd -n gzcat
KERNEL_CONFIG=
if [ -f "/proc/config" ]
then
KERNEL_CONFIG="/proc/config"
${CAT_CMD} /proc/config >${FIREHOL_DIR}/kcfg
source ${FIREHOL_DIR}/kcfg
${RM_CMD} -f ${FIREHOL_DIR}/kcfg
elif [ -f "/proc/config.gz" -a ! -z "${GZCAT_CMD}" ]
then
KERNEL_CONFIG="/proc/config.gz"
${GZCAT_CMD} /proc/config.gz >${FIREHOL_DIR}/kcfg
source ${FIREHOL_DIR}/kcfg
${RM_CMD} -f ${FIREHOL_DIR}/kcfg
elif [ -f "/lib/modules/`${UNAME_CMD} -r`/build/.config" ]
then
KERNEL_CONFIG="/lib/modules/`${UNAME_CMD} -r`/build/.config"
. "${KERNEL_CONFIG}"
elif [ -f "/boot/config-`${UNAME_CMD} -r`" ]
then
KERNEL_CONFIG="/boot/config-`${UNAME_CMD} -r`"
. "${KERNEL_CONFIG}"
elif [ -f "/usr/src/linux/.config" ]
then
KERNEL_CONFIG="/usr/src/linux/.config"
. "${KERNEL_CONFIG}"
else
echo >&2 " "
echo >&2 " IMPORTANT WARNING:"
echo >&2 " ------------------"
echo >&2 " FireHOL cannot find your current kernel configuration."
echo >&2 " Please, either compile your kernel with /proc/config,"
echo >&2 " or make sure there is a valid kernel config in:"
echo >&2 " /usr/src/linux/.config"
echo >&2 " "
echo >&2 " Because of this, FireHOL will simply attempt to load"
echo >&2 " all kernel modules for the services used, without"
echo >&2 " being able to detect failures."
echo >&2 " "
sleep 2
fi
# activation-phase command to check for the existance of
# a kernel configuration directive. It returns:
# 0 = module is already in the kernel
# 1 = module can be loaded with modprobe
# 2 = no info about this module in the kernel
check_kernel_config() {
eval local kcfg="\$${1}"
case ${kcfg} in
y) return 0
;;
m) return 1
;;
*) return 2
;;
esac
return 2
}
# activation-phase command to check for the existance of
# a kernel module. It returns:
# 0 = module is already in the kernel
# 1 = module can be loaded with modprobe
# 2 = no info about this module in the kernel
check_kernel_module() {
local mod="${1}"
case ${mod} in
ip_tables)
test -f /proc/net/ip_tables_names && return 0
check_kernel_config CONFIG_IP_NF_IPTABLES
return $?
;;
ip_conntrack)
test -f /proc/net/ip_conntrack && return 0
check_kernel_config CONFIG_IP_NF_CONNTRACK
return $?
;;
ip_conntrack_*)
local mnam="CONFIG_IP_NF_`echo ${mod} | ${CUT_CMD} -d '_' -f 3- | ${TR_CMD} a-z A-Z`"
check_kernel_config ${mnam}
return $?
;;
ip_nat_*)
local mnam="CONFIG_IP_NF_NAT_`echo ${mod} | ${CUT_CMD} -d '_' -f 3- | ${TR_CMD} a-z A-Z`"
check_kernel_config ${mnam}
return $?
;;
*)
return 2
;;
esac
return 2
}
# activation-phase command to load a kernel module.
load_kernel_module() {
local mod="${1}"
if [ ! ${FIREHOL_LOAD_KERNEL_MODULES} -eq 0 ]
then
check_kernel_module ${mod}
if [ $? -gt 0 ]
then
runcmd warn ${FIREHOL_LINEID} ${MODPROBE_CMD} ${mod} -q
fi
fi
return 0
}
# Processing-phase command to tell FireHOL to find one or more
# kernel modules to load, during activation-phase.
require_kernel_module() {
local new="${1}"
local m=
for m in ${FIREHOL_KERNEL_MODULES}
do
test "${m}" = "${new}" && return 0
done
FIREHOL_KERNEL_MODULES="${FIREHOL_KERNEL_MODULES} ${new}"
return 0
}
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# INTERNAL FUNCTIONS BELLOW THIS POINT - FireHOL internals
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
set_work_function() {
local show_explain=1
test "$1" = "-ne" && shift && local show_explain=0
work_function="$*"
if [ ${FIREHOL_EXPLAIN} -eq 1 ]
then
test ${show_explain} -eq 1 && printf "\n# %s\n" "$*"
elif [ ${FIREHOL_CONF_SHOW} -eq 1 ]
then
test ${show_explain} -eq 1 && printf "\n# INFO>>> %s\n" "$*" >>${FIREHOL_OUTPUT}
fi
}
# ------------------------------------------------------------------------------
# Check the status of the current primary command.
# WHY:
# Some sanity check for the order of commands in the configuration file.
# Each function has a "require_work type command" in order to check that it is
# placed in a valid point. This means that if you place a "route" command in an
# interface section (and many other combinations) it will fail.
require_work() {
local type="${1}"
local cmd="${2}"
case "${type}" in
clear)
test ! -z "${work_cmd}" && error "Previous work was not applied." && return 1
;;
set)
test -z "${work_cmd}" && error "The command used requires that a primary command is set." && return 1
test ! "${work_cmd}" = "${cmd}" -a ! "${cmd}" = "any" && error "Primary command is '${work_cmd}' but '${cmd}' is required." && return 1
;;
*)
error "Unknown work status '${type}'."
return 1
;;
esac
return 0
}
# ------------------------------------------------------------------------------
# Finalizes the rules of the last primary command.
# WHY:
# At the end of an interface or router we need to add some code to apply its
# policy, accept all related packets, etc.
# Finalization occures automatically when a new primary command is executed and
# when the configuration file finishes.
close_cmd() {
set_work_function -ne "Closing last open primary command (${work_cmd}/${work_name})"
case "${work_cmd}" in
interface)
close_interface || return 1
;;
router)
close_router || return 1
;;
'')
;;
*)
error "Unknown work '${work_cmd}'."
return 1
;;
esac
# Reset the current status variables to empty/default
work_counter=0
work_cmd=
work_realcmd=("(unset)")
work_name=
work_inface=
work_outface=
work_policy="${DEFAULT_INTERFACE_POLICY}"
return 0
}
# ------------------------------------------------------------------------------
# close_interface
# WHY:
# Finalizes the rules for the last interface().
close_interface() {
require_work set interface || return 1
close_all_groups
set_work_function "Finilizing interface '${work_name}'"
case "${work_policy}" in
return|RETURN)
return 0
;;
accept|ACCEPT)
;;
*)
local -a inlog=(loglimit "'IN-${work_name}'")
local -a outlog=(loglimit "'OUT-${work_name}'")
;;
esac
# Accept all related traffic to the established connections
rule chain "in_${work_name}" state RELATED action ACCEPT || return 1
rule chain "out_${work_name}" state RELATED action ACCEPT || return 1
rule chain "in_${work_name}" "${inlog[@]}" action ${work_policy} || return 1
rule reverse chain "out_${work_name}" "${outlog[@]}" action ${work_policy} || return 1
return 0
}
# ------------------------------------------------------------------------------
# close_router
# WHY:
# Finalizes the rules for the last router().
close_router() {
require_work set router || return 1
close_all_groups
set_work_function "Finilizing router '${work_name}'"
# Accept all related traffic to the established connections
rule chain "in_${work_name}" state RELATED action ACCEPT || return 1
rule chain "out_${work_name}" state RELATED action ACCEPT || return 1
return 0
}
# ------------------------------------------------------------------------------
# close_master
# WHY:
# Finalizes the rules for the whole firewall.
# It assummes there is not primary command open.
close_master() {
set_work_function "Finilizing firewall policies"
# Accept all related traffic to the established connections
rule chain INPUT state RELATED action ACCEPT || return 1
rule chain OUTPUT state RELATED action ACCEPT || return 1
rule chain FORWARD state RELATED action ACCEPT || return 1
rule chain INPUT loglimit "IN-unknown" action ${UNMATCHED_INPUT_POLICY} || return 1
rule chain OUTPUT loglimit "OUT-unknown" action ${UNMATCHED_OUTPUT_POLICY} || return 1
rule chain FORWARD loglimit "PASS-unknown" action ${UNMATCHED_ROUTER_POLICY} || return 1
return 0
}
FIREHOL_GROUP_COUNTER=0
FIREHOL_GROUP_DEPTH=0
FIREHOL_GROUP_STACK=()
group() {
work_realcmd_primary ${FUNCNAME} "$@"
require_work set any || return 1
local type="${1}"; shift
case $type in
with|start|begin)
# increase the counter
FIREHOL_GROUP_COUNTER=$[FIREHOL_GROUP_COUNTER + 1]
set_work_function "Starting new group No ${FIREHOL_GROUP_COUNTER}, under '${work_name}'"
# put the current name in the stack
FIREHOL_GROUP_STACK[$FIREHOL_GROUP_DEPTH]=${work_name}
FIREHOL_GROUP_DEPTH=$[FIREHOL_GROUP_DEPTH + 1]
# name for the new chain
mychain="group${FIREHOL_GROUP_COUNTER}"
# create the new chain
create_chain filter "in_${mychain}" "in_${work_name}" in "$@" || return 1
create_chain filter "out_${mychain}" "out_${work_name}" out reverse "$@" || return 1
# set a new name for new rules
work_name=${mychain}
;;
end|stop|close)
if [ ${FIREHOL_GROUP_DEPTH} -eq 0 ]
then
error "There is no group open to close."
return 1
fi
# pop one name from the stack
FIREHOL_GROUP_DEPTH=$[FIREHOL_GROUP_DEPTH - 1]
set_work_function "Closing group '${work_name}'. Now working under '${FIREHOL_GROUP_STACK[$FIREHOL_GROUP_DEPTH]}'"
work_name=${FIREHOL_GROUP_STACK[$FIREHOL_GROUP_DEPTH]}
;;
*)
error "Statement 'group' requires the first argument to be one of with, start, begin, end, stop, close."
return 1
;;
esac
return 0
}
close_all_groups() {
while [ ${FIREHOL_GROUP_DEPTH} -gt 0 ]
do
group close || return 1
done
return 0
}
# ------------------------------------------------------------------------------
# rule - the heart of FireHOL - iptables commands generation
# WHY:
# This is the function that gives all the magic to FireHOL. Actually it is a
# wrapper for iptables, producing multiple iptables commands based on its
# arguments. The rest of FireHOL is simply a "driver" for this function.
# rule_action_param() is a function - part of rule() - to create the final iptables cmd
# taking into account the "action_param" parameter of the action.
# rule_action_param() should only be used within rule() - no other place
FIREHOL_ACCEPT_CHAIN_COUNT=0
rule_action_param() {
local action="${1}"; shift
local protocol="${1}"; shift
local statenot="${1}"; shift
local state="${1}"; shift
local table="${1}"; shift
local -a action_param=()
# All arguments until the separator are the parameters of the action
local count=0
while [ ! -z "${1}" -a ! "A${1}" = "A--" ]
do
action_param[$count]="${1}"
shift
count=$[count + 1]
done
# If we don't have a seperator, generate an error
local sep="${1}"; shift
if [ ! "A${sep}" = "A--" ]
then
error "Internal Error, in parsing action_param parameters ($FUNCNAME '${action}' '${protocol}' '${statenot}' '${state}' '${table}' '${action_param[@]}' ${sep} '$@')."
return 1
fi
# Do the rule
case "${action}" in
NONE)
return 0
;;
ACCEPT)
# do we have any options for this accept?
if [ ! -z "${action_param[0]}" ]
then
# find the options we have
case "${action_param[0]}" in
"limit")
# limit NEW connections to the specified rate
local freq="${action_param[1]}"
local burst="${action_param[2]}"
local overflow="REJECT"
# if we have a custom overflow action, parse it.
test "${action_param[3]}" = "overflow" && local overflow="`echo "${action_param[4]}" | tr "a-z" "A-Z"`"
# unset the action_param, so that if this rule does not include NEW connections,
# we will not append anything to the generated iptables statements.
local -a action_param=()
# find is this rule matches NEW connections
local has_new=`echo "${state}" | grep -i NEW`
local do_accept_limit=0
if [ -z "${statenot}" ]
then
test ! -z "${has_new}" && local do_accept_limit=1
else
test -z "${has_new}" && local do_accept_limit=1
fi
# we have a match for NEW connections.
# redirect the traffic to a new chain, which will control
# the NEW connections while allowing all the other traffic
# to pass.
if [ "${do_accept_limit}" = "1" ]
then
local accept_limit_chain="`echo "ACCEPT ${freq} ${burst} ${overflow}" | tr " /." "___"`"
# does the chain we need already exist?
if [ ! -f "${FIREHOL_CHAINS_DIR}/${accept_limit_chain}" ]
then
# the chain does not exist. create it.
iptables ${table} -N "${accept_limit_chain}"
touch "${FIREHOL_CHAINS_DIR}/${accept_limit_chain}"
# first, if the traffic is not a NEW connection, allow it.
# doing this first will speed up normal traffic.
iptables ${table} -A "${accept_limit_chain}" -m state ! --state NEW -j ACCEPT
# accept NEW connections within the given limits.
iptables ${table} -A "${accept_limit_chain}" -m limit --limit "${freq}" --limit-burst "${burst}" -j ACCEPT
# log the overflow NEW connections reaching this step within the new chain
local -a logopts_arg=()
if [ "${FIREHOL_LOG_MODE}" = "ULOG" ]
then
local -a logopts_arg=("--ulog-prefix='OVERFLOW:'")
else
local -a logopts_arg=("--log-level" "${FIREHOL_LOG_LEVEL}" "--log-prefix='OVERFLOW:'")
fi
iptables ${table} -A "${accept_limit_chain}" -m limit --limit "${FIREHOL_LOG_FREQUENCY}" --limit-burst "${FIREHOL_LOG_BURST}" -j ${FIREHOL_LOG_MODE} ${FIREHOL_LOG_OPTIONS} "${logopts_arg[@]}"
# if the overflow is to be rejected is tcp, reject it with TCP-RESET
if [ "${overflow}" = "REJECT" ]
then
iptables ${table} -A "${accept_limit_chain}" -p tcp -j REJECT --reject-with tcp-reset
fi
# do the specified action on the overflow
iptables ${table} -A "${accept_limit_chain}" -j ${overflow}
fi
# send the rule to be generated to this chain
local action=${accept_limit_chain}
fi
;;
'knock')
# the name of the knock
local name="knock_${action_param[1]}"
# unset the action_param, so that if this rule does not include NEW connections,
# we will not append anything to the generated iptables statements.
local -a action_param=()
# does the knock chain exists?
if [ ! -f "${FIREHOL_CHAINS_DIR}/${name}" ]
then
# the chain does not exist. create it.
iptables ${table} -N "${name}"
touch "${FIREHOL_CHAINS_DIR}/${name}"
iptables -A "${name}" -m state --state ESTABLISHED -j ACCEPT
# knockd (http://www.zeroflux.org/knock/)
# will create more rules inside this chain to match NEW packets.
fi
# send the rule to be generated to this knock chain
local action=${name}
;;
*)
error "Internal error. Cannot understand action ${action} with parameter '${action_param[0]}'."
return 1
;;
esac
fi
;;
REJECT)
if [ "${action_param[1]}" = "auto" ]
then
if [ "${protocol}" = "tcp" -o "${protocol}" = "TCP" ]
then
action_param=("--reject-with" "tcp-reset")
else
action_param=()
fi
fi
;;
esac
iptables "$@" -j "${action}" "${action_param[@]}"
local ret=$?
test $ret -gt 0 && failed=$[failed + 1]
return $ret
}
rule() {
local table=
local chain=
local inface=any
local infacenot=
local outface=any
local outfacenot=
local physin=any
local physinnot=
local physout=any
local physoutnot=
local mac=any
local macnot=
local src=any
local srcnot=
local dst=any
local dstnot=
local sport=any
local sportnot=
local dport=any
local dportnot=
local proto=any
local protonot=
local uid=any
local uidnot=
local gid=any
local gidnot=
local pid=any
local pidnot=
local sid=any
local sidnot=
local cmd=any
local cmdnot=
local mark=any
local marknot=
local dscp=any
local dscptype=
local despnot=
local tos=any
local tosnot=
local log=
local logtxt=
local loglevel=
local limit=
local burst=
local iplimit=
local iplimit_mask=
local action=
local state=
local statenot=
local failed=0
local reverse=0
local swi=0
local swo=0
local custom=
# if set to 1, all owner module options will be ignored
local noowner=0
# if set to 1, all mac options will be ignored
local nomac=0
# if set to 1, MIRROR will be converted to REJECT
local nomirror=0
# if set to 1, log and loglimit are ignored.
local nolog=0
# if set to 1, detection algorithm about overwritting optional rule
# parameters will take place.
local softwarnings=1
# set it, in order to be local
local -a action_param=()
while [ ! -z "${1}" ]
do
case "${1}" in
reverse|REVERSE)
reverse=1
shift
;;
table|TABLE)
test ${softwarnings} -eq 1 -a ! -z "${table}" && softwarning "Overwritting param: ${1} '${chain}' becomes '${2}'"
table="-t ${2}"
shift 2
;;
chain|CHAIN)
test ${softwarnings} -eq 1 -a ! -z "${chain}" && softwarning "Overwritting param: ${1} '${chain}' becomes '${2}'"
chain="${2}"
shift 2
;;
inface|INFACE)
shift
if [ ${reverse} -eq 0 ]
then
infacenot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
infacenot="!"
else
if [ $swi -eq 1 ]
then
work_inface="${1}"
fi
fi
test ${softwarnings} -eq 1 -a ! "${inface}" = "any" && softwarning "Overwritting param: inface '${inface}' becomes '${1}'"
inface="${1}"
else
outfacenot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
outfacenot="!"
else
if [ ${swo} -eq 1 ]
then
work_outface="$1"
fi
fi
test ${softwarnings} -eq 1 -a ! "${outface}" = "any" && softwarning "Overwritting param: outface '${outface}' becomes '${1}'"
outface="${1}"
fi
shift
;;
outface|OUTFACE)
shift
if [ ${reverse} -eq 0 ]
then
outfacenot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
outfacenot="!"
else
if [ ${swo} -eq 1 ]
then
work_outface="${1}"
fi
fi
test ${softwarnings} -eq 1 -a ! "${outface}" = "any" && softwarning "Overwritting param: outface '${outface}' becomes '${1}'"
outface="${1}"
else
infacenot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
infacenot="!"
else
if [ ${swi} -eq 1 ]
then
work_inface="${1}"
fi
fi
test ${softwarnings} -eq 1 -a ! "${inface}" = "any" && softwarning "Overwritting param: inface '${inface}' becomes '${1}'"
inface="${1}"
fi
shift
;;
physin|PHYSIN)
shift
if [ ${reverse} -eq 0 ]
then
physinnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
physinnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${physin}" = "any" && softwarning "Overwritting param: physin '${physin}' becomes '${1}'"
physin="${1}"
else
physoutnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
physoutnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${physout}" = "any" && softwarning "Overwritting param: physout '${physout}' becomes '${1}'"
physout="${1}"
fi
shift
;;
physout|PHYSOUT)
shift
if [ ${reverse} -eq 0 ]
then
physoutnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
physoutnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${physout}" = "any" && softwarning "Overwritting param: physout '${physout}' becomes '${1}'"
physout="${1}"
else
physinnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
physinnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${physin}" = "any" && softwarning "Overwritting param: physin '${physin}' becomes '${1}'"
physin="${1}"
fi
shift
;;
mac|MAC)
shift
macnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
macnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${mac}" = "any" && softwarning "Overwritting param: mac '${mac}' becomes '${1}'"
test ${nomac} -eq 0 && mac="${1}"
shift
;;
src|SRC|source|SOURCE)
shift
if [ ${reverse} -eq 0 ]
then
srcnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
srcnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${src}" = "any" && softwarning "Overwritting param: src '${src}' becomes '${1}'"
src="${1}"
else
dstnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
dstnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${dst}" = "any" && softwarning "Overwritting param: dst '${dst}' becomes '${1}'"
dst="${1}"
fi
shift
;;
dst|DST|destination|DESTINATION)
shift
if [ ${reverse} -eq 0 ]
then
dstnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
dstnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${dst}" = "any" && softwarning "Overwritting param: dst '${dst}' becomes '${1}'"
dst="${1}"
else
srcnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
srcnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${src}" = "any" && softwarning "Overwritting param: src '${src}' becomes '${1}'"
src="${1}"
fi
shift
;;
sport|SPORT|sourceport|SOURCEPORT)
shift
if [ ${reverse} -eq 0 ]
then
sportnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
sportnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${sport}" = "any" && softwarning "Overwritting param: sport '${sport}' becomes '${1}'"
sport="${1}"
else
dportnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
dportnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${dport}" = "any" && softwarning "Overwritting param: dport '${dport}' becomes '${1}'"
dport="${1}"
fi
shift
;;
dport|DPORT|destinationport|DESTINATIONPORT)
shift
if [ ${reverse} -eq 0 ]
then
dportnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
dportnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${dport}" = "any" && softwarning "Overwritting param: dport '${dport}' becomes '${1}'"
dport="${1}"
else
sportnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
sportnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${sport}" = "any" && softwarning "Overwritting param: sport '${sport}' becomes '${1}'"
sport="${1}"
fi
shift
;;
proto|PROTO|protocol|PROTOCOL)
shift
protonot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
protonot="!"
fi
test ${softwarnings} -eq 1 -a ! "${proto}" = "any" && softwarning "Overwritting param: proto '${proto}' becomes '${1}'"
proto="${1}"
shift
;;
mark|MARK)
shift
marknot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
marknot="!"
fi
test ${softwarnings} -eq 1 -a ! "${mark}" = "any" && softwarning "Overwritting param: mark '${mark}' becomes '${1}'"
mark="${1}"
shift
;;
tos|TOS)
shift
tosnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
tosnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${tos}" = "any" && softwarning "Overwritting param: tos '${tos}' becomes '${1}'"
tos="${1}"
shift
;;
dscp|DSCP)
shift
dscpnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
dscpnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${dscp}" = "any" && softwarning "Overwritting param: dscp '${dscp}' becomes '${1}'"
dscp="${1}"
shift
if [ "${dscp}" = "class" ]
then
dscptype="-class"
dscp="${1}"
shift
fi
;;
action|ACTION)
test ${softwarnings} -eq 1 -a ! -z "${action}" && softwarning "Overwritting param: action '${action}' becomes '${2}'"
action="${2}"
shift 2
local -a action_param=()
local action_is_chain=0
case "${action}" in
accept|ACCEPT)
action="ACCEPT"
if [ "${1}" = "with" ]
then
shift
case "${1}" in
limit|LIMIT)
local -a action_param=("limit" "${2}" "${3}")
shift 3
if [ "${1}" = "overflow" ]
then
action_param[3]="overflow"
action_param[4]="${2}"
shift 2
fi
;;
knock|KNOCK)
local -a action_param=("knock" "${2}")
shift 2
;;
*)
error "Cannot understand action's '${action}' directive '${1}'"
return 1
;;
esac
fi
;;
deny|DENY|drop|DROP)
action="DROP"
;;
reject|REJECT)
action="REJECT"
if [ "${1}" = "with" ]
then
local -a action_param=("--reject-with" "${2}")
shift 2
else
local -a action_param=("--reject-with" "auto")
fi
;;
return|RETURN)
action="RETURN"
;;
mirror|MIRROR)
action="MIRROR"
test $nomirror -eq 1 && action="REJECT"
;;
none|NONE)
action="NONE"
;;
snat|SNAT)
action="SNAT"
if [ "${1}" = "to" ]
then
local -a action_param=()
local x=
for x in ${2}
do
action_param=(${action_param[@]} "--to-source" "${x}")
done
shift 2
else
error "${action} requires a 'to' argument."
return 1
fi
if [ ! "A${table}" = "A-t nat" ]
then
error "${action} must on a the 'nat' table."
return 1
fi
;;
dnat|DNAT)
action="DNAT"
if [ "${1}" = "to" ]
then
local -a action_param=()
local x=
for x in ${2}
do
action_param=(${action_param[@]} "--to-destination" "${x}")
done
shift 2
else
error "${action} requires a 'to' argument"
return 1
fi
if [ ! "A${table}" = "A-t nat" ]
then
error "${action} must on a the 'nat' table."
return 1
fi
;;
redirect|REDIRECT)
action="REDIRECT"
if [ "${1}" = "to-port" -o "${1}" = "to" ]
then
local -a action_param=("--to-ports" "${2}")
shift 2
else
error "${action} requires a 'to-port' or 'to' argument."
return 1
fi
if [ ! "A${table}" = "A-t nat" ]
then
error "${action} must on a the 'nat' table."
return 1
fi
;;
tos|TOS)
action="TOS"
if [ "${1}" = "to" ]
then
local -a action_param=("--set-tos" "${2}")
shift 2
else
error "${action} requires a 'to' argument"
return 1
fi
if [ ! "A${table}" = "A-t mangle" ]
then
error "${action} must on a the 'mangle' table."
return 1
fi
;;
mark|MARK)
action="MARK"
if [ "${1}" = "to" ]
then
local -a action_param=("--set-mark" "${2}")
shift 2
else
error "${action} requires a 'to' argument"
return 1
fi
if [ ! "A${table}" = "A-t mangle" ]
then
error "${action} must on a the 'mangle' table."
return 1
fi
;;
dscp|DSCP)
action="DSCP"
if [ "${1}" = "to" ]
then
if [ "${2}" = "class" ]
then
local -a action_param=("--set-dscp-class" "${2}")
shift
else
local -a action_param=("--set-dscp" "${2}")
fi
shift 2
else
error "${action} requires a 'to' argument"
return 1
fi
if [ ! "A${table}" = "A-t mangle" ]
then
error "${action} must on a the 'mangle' table."
return 1
fi
;;
*)
chain_exists "${action}"
local action_is_chain=$?
;;
esac
;;
state|STATE)
shift
statenot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
statenot="!"
fi
test ${softwarnings} -eq 1 -a ! -z "${state}" && softwarning "Overwritting param: state '${state}' becomes '${1}'"
state="${1}"
shift
;;
user|USER|uid|UID)
shift
uidnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
test ${noowner} -eq 0 && uidnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${uid}" = "any" && softwarning "Overwritting param: uid '${uid}' becomes '${1}'"
test ${noowner} -eq 0 && uid="${1}"
shift
;;
group|GROUP|gid|GID)
shift
gidnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
test ${noowner} -eq 0 && gidnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${gid}" = "any" && softwarning "Overwritting param: gid '${gid}' becomes '${1}'"
test ${noowner} -eq 0 && gid="${1}"
shift
;;
process|PROCESS|pid|PID)
shift
pidnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
test ${noowner} -eq 0 && pidnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${pid}" = "any" && softwarning "Overwritting param: pid '${pid}' becomes '${1}'"
test ${noowner} -eq 0 && pid="${1}"
shift
;;
session|SESSION|sid|SID)
shift
sidnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
test ${noowner} -eq 0 && sidnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${sid}" = "any" && softwarning "Overwritting param: sid '${sid}' becomes '${1}'"
test ${noowner} -eq 0 && sid="${1}"
shift
;;
command|COMMAND|cmd|CMD)
shift
cmdnot=
if [ "${1}" = "not" -o "${1}" = "NOT" ]
then
shift
test ${noowner} -eq 0 && cmdnot="!"
fi
test ${softwarnings} -eq 1 -a ! "${cmd}" = "any" && softwarning "Overwritting param: cmd '${cmd}' becomes '${1}'"
test ${noowner} -eq 0 && cmd="${1}"
shift
;;
custom|CUSTOM)
test ${softwarnings} -eq 1 -a ! -z "${custom}" && softwarning "Overwritting param: custom '${custom}' becomes '${2}'"
custom="${2}"
shift 2
;;
log|LOG)
if [ ${nolog} -eq 0 ]
then
test ${softwarnings} -eq 1 -a ! -z "${log}" && softwarning "Overwritting param: log '${log}/${logtxt}' becomes 'normal/${2}'"
log=normal
logtxt="${2}"
fi
shift 2
if [ "${1}" = "level" ]
then
loglevel="${2}"
shift 2
else
loglevel="${FIREHOL_LOG_LEVEL}"
fi
;;
loglimit|LOGLIMIT)
if [ ${nolog} -eq 0 ]
then
test ${softwarnings} -eq 1 -a ! -z "${log}" && softwarning "Overwritting param: log '${log}/${logtxt}' becomes 'limit/${2}'"
log=limit
logtxt="${2}"
fi
shift 2
if [ "${1}" = "level" ]
then
loglevel="${2}"
shift 2
else
loglevel="${FIREHOL_LOG_LEVEL}"
fi
;;
limit|LIMIT)
test ${softwarnings} -eq 1 -a ! -z "${limit}" && softwarning "Overwritting param: limit '${limit}' becomes '${2}'"
limit="${2}"
burst="${3}"
shift 3
;;
iplimit|IPLIMIT)
test ${softwarnings} -eq 1 -a ! -z "${iplimit}" && softwarning "Overwritting param: iplimit '${iplimit}' becomes '${2}'"
iplimit="${2}"
iplimit_mask="${3}"
shift 3
;;
in) # this is incoming traffic - ignore packet ownership
local noowner=1
local nomirror=0
local nomac=0
shift
;;
out) # this is outgoing traffic - ignore packet ownership if not in an interface
if [ ! "${work_cmd}" = "interface" ]
then
local noowner=1
else
local nomirror=1
fi
local nomac=1
shift
;;
nolog)
local nolog=1
shift
;;
noowner)
local noowner=1
shift
;;
softwarnings)
local softwarnings=1
shift
;;
nosoftwarnings)
local softwarnings=0
shift
;;
set_work_inface|SET_WORK_INFACE)
swi=1
shift
;;
set_work_outface|SET_WORK_OUTFACE)
swo=1
shift
;;
*)
error "Cannot understand directive '${1}'."
return 1
;;
esac
done
test -z "${table}" && table="-t filter"
# If the user did not specified a rejection message,
# we have to be smart and produce a tcp-reset if the protocol
# is TCP and an ICMP port unreachable in all other cases.
# The special case here is the protocol "any".
# To accomplish the differentiation based on protocol we have
# to change the protocol "any" to "tcp any"
test "${action}" = "REJECT" -a "${action_param[1]}" = "auto" -a "${proto}" = "any" && proto="tcp any"
# we cannot accept empty strings to a few parameters, since this
# will prevent us from generating a rule (due to nested BASH loops).
test -z "${inface}" && error "Cannot accept an empty 'inface'." && return 1
test -z "${outface}" && error "Cannot accept an empty 'outface'." && return 1
test -z "${physin}" && error "Cannot accept an empty 'physin'." && return 1
test -z "${physout}" && error "Cannot accept an empty 'physout'." && return 1
test -z "${mac}" && error "Cannot accept an empty 'mac'." && return 1
test -z "${src}" && error "Cannot accept an empty 'src'." && return 1
test -z "${dst}" && error "Cannot accept an empty 'dst'." && return 1
test -z "${sport}" && error "Cannot accept an empty 'sport'." && return 1
test -z "${dport}" && error "Cannot accept an empty 'dport'." && return 1
test -z "${proto}" && error "Cannot accept an empty 'proto'." && return 1
test -z "${uid}" && error "Cannot accept an empty 'uid'." && return 1
test -z "${gid}" && error "Cannot accept an empty 'gid'." && return 1
test -z "${pid}" && error "Cannot accept an empty 'pid'." && return 1
test -z "${sid}" && error "Cannot accept an empty 'sid'." && return 1
test -z "${cmd}" && error "Cannot accept an empty 'cmd'." && return 1
# ----------------------------------------------------------------------------------
# Do we have negative contitions?
# If yes, we have to:
#
# case 1: If the action is a chain.
# Add to this chain positive RETURN statements matching all the negatives.
# The positive rules will be added bellow to the same chain and will be
# matched only if all RETURNs have not been matched.
#
# case 2: If the action is not a chain.
# Create a temporary chain, then add to this chain positive RETURN rules
# matching the negatives, and append at its end the final action (which is
# not a chain), then change the action of the positive rules to jump to
# this temporary chain.
# ignore 'statenot' since it is negated in the positive rules
if [ ! -z "${infacenot}${outfacenot}${physinnot}${physoutnot}${macnot}${srcnot}${dstnot}${sportnot}${dportnot}${protonot}${uidnot}${gidnot}${pidnot}${sidnot}${cmdnot}${marknot}${tosnot}${dscpnot}" ]
then
if [ ${action_is_chain} -eq 1 ]
then
# if the action is a chain name, then just add the negative
# expressions to this chain. Nothing more.
local negative_chain="${action}"
local negative_action=
else
# if the action is a native iptables action, then create
# an intermidiate chain to store the negative expression,
# and change the action of the rule to point to this action.
# In this case, bellow we add after all negatives, the original
# action of the rule.
local negative_chain="${chain}.${FIREHOL_DYNAMIC_CHAIN_COUNTER}"
FIREHOL_DYNAMIC_CHAIN_COUNTER="$[FIREHOL_DYNAMIC_CHAIN_COUNTER + 1]"
iptables ${table} -N "${negative_chain}"
local negative_action="${action}"
local action="${negative_chain}"
fi
if [ ! -z "${infacenot}" ]
then
local inf=
for inf in ${inface}
do
iptables ${table} -A "${negative_chain}" -i "${inf}" -j RETURN
done
infacenot=
inface=any
fi
if [ ! -z "${outfacenot}" ]
then
local outf=
for outf in ${outface}
do
iptables ${table} -A "${negative_chain}" -o "${outf}" -j RETURN
done
outfacenot=
outface=any
fi
if [ ! -z "${physinnot}" ]
then
local inph=
for inph in ${physin}
do
iptables ${table} -A "${negative_chain}" -m physdev --physdev-in "${inph}" -j RETURN
done
physinnot=
physin=any
fi
if [ ! -z "${physoutnot}" ]
then
local outph=
for outph in ${physout}
do
iptables ${table} -A "${negative_chain}" -m physdev --physdev-out "${outph}" -j RETURN
done
physoutnot=
physout=any
fi
if [ ! -z "${macnot}" ]
then
local m=
for m in ${mac}
do
iptables ${table} -A "${negative_chain}" -m mac --mac-source "${m}" -j RETURN
done
macnot=
mac=any
fi
if [ ! -z "${srcnot}" ]
then
local s=
for s in ${src}
do
iptables ${table} -A "${negative_chain}" -s "${s}" -j RETURN
done
srcnot=
src=any
fi
if [ ! -z "${dstnot}" ]
then
local d=
for d in ${dst}
do
iptables ${table} -A "${negative_chain}" -d "${d}" -j RETURN
done
dstnot=
dst=any
fi
if [ ! -z "${protonot}" ]
then
if [ ! -z "${sportnot}" -o ! -z "${dportnot}" ]
then
error "Cannot have negative protocol(s) and source/destination port(s)."
return 1
fi
local pr=
for pr in ${proto}
do
iptables ${table} -A "${negative_chain}" -p "${pr}" -j RETURN
done
protonot=
proto=any
fi
if [ ! -z "${sportnot}" ]
then
if [ "${proto}" = "any" ]
then
error "Cannot have negative source port specification without protocol."
return 1
fi
local sp=
for sp in ${sport}
do
local pr=
for pr in ${proto}
do
iptables ${table} -A "${negative_chain}" -p "${pr}" --sport "${sp}" -j RETURN
done
done
sportnot=
sport=any
fi
if [ ! -z "${dportnot}" ]
then
if [ "${proto}" = "any" ]
then
error "Cannot have negative destination port specification without protocol."
return 1
fi
local dp=
for dp in ${dport}
do
local pr=
for pr in ${proto}
do
iptables ${table} -A "${negative_chain}" -p "${pr}" --dport "${dp}" -j RETURN
done
done
dportnot=
dport=any
fi
if [ ! -z "${uidnot}" ]
then
local tuid=
for tuid in ${uid}
do
iptables ${table} -A "${negative_chain}" -m owner --uid-owner "${tuid}" -j RETURN
done
uidnot=
uid=any
fi
if [ ! -z "${gidnot}" ]
then
local tgid=
for tgid in ${gid}
do
iptables ${table} -A "${negative_chain}" -m owner --gid-owner "${tgid}" -j RETURN
done
gidnot=
gid=any
fi
if [ ! -z "${pidnot}" ]
then
local tpid=
for tpid in ${pid}
do
iptables ${table} -A "${negative_chain}" -m owner --pid-owner "${tpid}" -j RETURN
done
pidnot=
pid=any
fi
if [ ! -z "${sidnot}" ]
then
local tsid=
for tsid in ${sid}
do
iptables ${table} -A "${negative_chain}" -m owner --sid-owner "${tsid}" -j RETURN
done
sidnot=
sid=any
fi
if [ ! -z "${cmdnot}" ]
then
local tcmd=
for tcmd in ${cmd}
do
iptables ${table} -A "${negative_chain}" -m owner --cmd-owner "${tcmd}" -j RETURN
done
cmdnot=
cmd=any
fi
if [ ! -z "${marknot}" ]
then
local tmark=
for tmark in ${mark}
do
iptables ${table} -A "${negative_chain}" -m mark --mark "${tmark}" -j RETURN
done
marknot=
mark=any
fi
if [ ! -z "${tosnot}" ]
then
local ttos=
for ttos in ${tos}
do
iptables ${table} -A "${negative_chain}" -m tos --tos "${ttos}" -j RETURN
done
tosnot=
tos=any
fi
if [ ! -z "${dscpnot}" ]
then
local tdscp=
for tdscp in ${dscp}
do
iptables ${table} -A "${negative_chain}" -m dscp --dscp${dscptype} "${tdscp}" -j RETURN
done
dscp=any
dscpnot=
fi
# in case this is temporary chain we created for the negative expression,
# just make it have the final action of the rule.
if [ ! -z "${negative_action}" ]
then
local pr=
for pr in ${proto}
do
local -a proto_arg=()
case ${pr} in
any|ANY)
;;
*)
local -a proto_arg=("-p" "${pr}")
;;
esac
rule_action_param "${negative_action}" "${pr}" "" "" "${table}" "${action_param[@]}" -- ${table} -A "${negative_chain}" "${proto_arg[@]}"
local -a action_param=()
done
fi
fi
# ----------------------------------------------------------------------------------
# Process the positive rules
# uid
local tuid=
for tuid in ${uid}
do
local -a uid_arg=()
local -a owner_arg=()
case ${tuid} in
any|ANY)
;;
*)
local -a owner_arg=("-m" "owner")
local -a uid_arg=("--uid-owner" "${tuid}")
;;
esac
# gid
local tgid=
for tgid in ${gid}
do
local -a gid_arg=()
case ${tgid} in
any|ANY)
;;
*)
local -a owner_arg=("-m" "owner")
local -a gid_arg=("--gid-owner" "${tgid}")
;;
esac
# pid
local tpid=
for tpid in ${pid}
do
local -a pid_arg=()
case ${tpid} in
any|ANY)
;;
*)
local -a owner_arg=("-m" "owner")
local -a pid_arg=("--pid-owner" "${tpid}")
;;
esac
# sid
local tsid=
for tsid in ${sid}
do
local -a sid_arg=()
case ${tsid} in
any|ANY)
;;
*)
local -a owner_arg=("-m" "owner")
local -a sid_arg=("--sid-owner" "${tsid}")
;;
esac
# cmd
local tcmd=
for tcmd in ${cmd}
do
local -a cmd_arg=()
case ${tcmd} in
any|ANY)
;;
*)
local -a owner_arg=("-m" "owner")
local -a cmd_arg=("--cmd-owner" "${tcmd}")
;;
esac
# mark
local tmark=
for tmark in ${mark}
do
local -a mark_arg=()
case ${tmark} in
any|ANY)
;;
*)
local -a mark_arg=("-m" "mark" "--mark" "${tmark}")
;;
esac
# tos
local ttos=
for ttos in ${tos}
do
local -a tos_arg=()
case ${ttos} in
any|ANY)
;;
*)
local -a tos_arg=("-m" "tos" "--tos" "${ttos}")
;;
esac
# dscp
local tdscp=
for tdscp in ${dscp}
do
local -a dscp_arg=()
case ${tdscp} in
any|ANY)
;;
*)
local -a dscp_arg=("-m" "dscp" "--dscp${dscptype}" "${tdscp}")
;;
esac
# proto
local pr=
for pr in ${proto}
do
local -a proto_arg=()
case ${pr} in
any|ANY)
;;
*)
local -a proto_arg=("-p" "${pr}")
;;
esac
# inface
local inf=
for inf in ${inface}
do
local -a inf_arg=()
case ${inf} in
any|ANY)
;;
*)
local -a inf_arg=("-i" "${inf}")
;;
esac
# outface
local outf=
for outf in ${outface}
do
local -a outf_arg=()
case ${outf} in
any|ANY)
;;
*)
local -a outf_arg=("-o" "${outf}")
;;
esac
# physin
local inph=
for inph in ${physin}
do
local -a inph_arg=()
case ${inph} in
any|ANY)
;;
*)
local -a physdev_arg=("-m" "physdev")
local -a inph_arg=("--physdev-in" "${inph}")
;;
esac
# physout
local outph=
for outph in ${physout}
do
local -a outph_arg=()
case ${outph} in
any|ANY)
;;
*)
local -a physdev_arg=("-m" "physdev")
local -a outph_arg=("--physdev-out" "${outph}")
;;
esac
# sport
local sp=
for sp in ${sport}
do
local -a sp_arg=()
case ${sp} in
any|ANY)
;;
*)
local -a sp_arg=("--sport" "${sp}")
;;
esac
# dport
local dp=
for dp in ${dport}
do
local -a dp_arg=()
case ${dp} in
any|ANY)
;;
*)
local -a dp_arg=("--dport" "${dp}")
;;
esac
# mac
local mc=
for mc in ${mac}
do
local -a mc_arg=()
case ${mc} in
any|ANY)
;;
*)
local -a mc_arg=("-m" "mac" "--mac-source" "${mc}")
;;
esac
# src
local s=
for s in ${src}
do
local -a s_arg=()
case ${s} in
any|ANY)
;;
*)
local -a s_arg=("-s" "${s}")
;;
esac
# dst
local d=
for d in ${dst}
do
local -a d_arg=()
case ${d} in
any|ANY)
;;
*)
local -a d_arg=("-d" "${d}")
;;
esac
# state
local -a state_arg=()
if [ ! -z "${state}" ]
then
local -a state_arg=("-m" "state" "${statenot}" "--state" "${state}")
fi
# limit
local -a limit_arg=()
if [ ! -z "${limit}" ]
then
local -a limit_arg=("-m" "limit" "--limit" "${limit}" "--limit-burst" "${burst}")
fi
# iplimit
local -a iplimit_arg=()
if [ ! -z "${iplimit}" ]
then
local -a iplimit_arg=("-m" "iplimit" "--iplimit-above" "${iplimit}" "--iplimit-mask" "${iplimit_mask}")
fi
# build the command
declare -a basecmd=("${inf_arg[@]}" "${outf_arg[@]}" "${physdev_arg[@]}" "${inph_arg[@]}" "${outph_arg[@]}" "${limit_arg[@]}" "${iplimit_arg[@]}" "${proto_arg[@]}" "${s_arg[@]}" "${sp_arg[@]}" "${d_arg[@]}" "${dp_arg[@]}" "${owner_arg[@]}" "${uid_arg[@]}" "${gid_arg[@]}" "${pid_arg[@]}" "${sid_arg[@]}" "${cmd_arg[@]}" "${state_arg[@]}" "${mc_arg[@]}" "${mark_arg[@]}" "${tos_arg[@]}" "${dscp_arg[@]}")
# log mode selection
local -a logopts_arg=()
if [ "${FIREHOL_LOG_MODE}" = "ULOG" ]
then
local -a logopts_arg=("--ulog-prefix='${logtxt}:'")
else
local -a logopts_arg=("--log-level" "${loglevel}" "--log-prefix='${logtxt}:'")
fi
# log / loglimit
case "${log}" in
'')
;;
limit)
iptables ${table} -A "${chain}" "${basecmd[@]}" ${custom} -m limit --limit "${FIREHOL_LOG_FREQUENCY}" --limit-burst "${FIREHOL_LOG_BURST}" -j ${FIREHOL_LOG_MODE} ${FIREHOL_LOG_OPTIONS} "${logopts_arg[@]}"
;;
normal)
iptables ${table} -A "${chain}" "${basecmd[@]}" ${custom} -j ${FIREHOL_LOG_MODE} ${FIREHOL_LOG_OPTIONS} "${logopts_arg[@]}"
;;
*)
error "Unknown log value '${log}'."
;;
esac
# do it!
rule_action_param "${action}" "${pr}" "${statenot}" "${state}" "${table}" "${action_param[@]}" -- ${table} -A "${chain}" "${basecmd[@]}" ${custom}
done # dst
done # src
done # mac
done # dport
done # sport
done # physout
done # physin
done # outface
done # inface
done # proto
done # dscp
done # tos
done # mark
done # cmd
done # sid
done # pid
done # gid
done # uid
test ${failed} -gt 0 && error "There are ${failed} failed commands." && return 1
return 0
}
softwarning() {
echo >&2
echo >&2 "--------------------------------------------------------------------------------"
echo >&2 "WARNING"
echo >&2 "WHAT : ${work_function}"
echo >&2 "WHY :" "$@"
printf >&2 "COMMAND: "; printf >&2 "%q " "${work_realcmd[@]}"; echo >&2
echo >&2 "SOURCE : line ${FIREHOL_LINEID} of ${FIREHOL_CONFIG}"
echo >&2
return 0
}
# ------------------------------------------------------------------------------
# error - error reporting while still parsing the configuration file
# WHY:
# This is the error handler that presents to the user detected errors during
# processing FireHOL's configuration file.
# This command is directly called by other functions of FireHOL.
error() {
work_error=$[work_error + 1]
echo >&2
echo >&2 "--------------------------------------------------------------------------------"
echo >&2 "ERROR #: ${work_error}"
echo >&2 "WHAT : ${work_function}"
echo >&2 "WHY :" "$@"
printf >&2 "COMMAND: "; printf >&2 "%q " "${work_realcmd[@]}"; echo >&2
echo >&2 "SOURCE : line ${FIREHOL_LINEID} of ${FIREHOL_CONFIG}"
echo >&2
return 0
}
# ------------------------------------------------------------------------------
# runtime_error - postprocessing evaluation of commands run
# WHY:
# The generated iptables commands must be checked for errors in case they fail.
# This command is executed after every postprocessing command to find out
# if it has been successfull or failed.
runtime_error() {
local type="ERROR"
local id=
case "${1}" in
error)
local type="ERROR "
work_final_status=$[work_final_status + 1]
local id="# ${work_final_status}."
;;
warn)
local type="WARNING"
local id="This might or might not affect the operation of your firewall."
;;
*)
work_final_status=$[work_final_status + 1]
local id="# ${work_final_status}."
echo >&2
echo >&2
echo >&2 "*** unsupported final status type '${1}'. Assuming it is 'ERROR'"
echo >&2
echo >&2
;;
esac
shift
local ret="${1}"; shift
local line="${1}"; shift
echo >&2
echo >&2
echo >&2 "--------------------------------------------------------------------------------"
echo >&2 "${type} : ${id}"
echo >&2 "WHAT : A runtime command failed to execute (returned error ${ret})."
echo >&2 "SOURCE : line ${line} of ${FIREHOL_CONFIG}"
printf >&2 "COMMAND : "
printf >&2 "%q " "$@"
printf >&2 "\n"
echo >&2 "OUTPUT : "
echo >&2
${CAT_CMD} ${FIREHOL_OUTPUT}.log
echo >&2
return 0
}
# ------------------------------------------------------------------------------
# chain_exists - find if chain name has already being specified
# WHY:
# We have to make sure each service gets its own chain.
# Although FireHOL chain naming makes chains with unique names, this is just
# an extra sanity check.
chain_exists() {
local chain="${1}"
test -f "${FIREHOL_CHAINS_DIR}/${chain}" && return 1
return 0
}
# ------------------------------------------------------------------------------
# create_chain - create a chain and link it to the firewall
# WHY:
# When a chain is created it must somehow to be linked to the rest of the
# firewall apropriately. This function first creates the chain and then
# it links it to its final position within the generated firewall.
create_chain() {
local table="${1}"
local newchain="${2}"
local oldchain="${3}"
shift 3
set_work_function "Creating chain '${newchain}' under '${oldchain}' in table '${table}'"
chain_exists "${newchain}"
test $? -eq 1 && error "Chain '${newchain}' already exists." && return 1
iptables -t ${table} -N "${newchain}" || return 1
${TOUCH_CMD} "${FIREHOL_CHAINS_DIR}/${newchain}"
rule table ${table} chain "${oldchain}" action "${newchain}" "$@" || return 1
return 0
}
# ------------------------------------------------------------------------------
# smart_function - find the valid service definition for a service
# WHY:
# FireHOL supports simple and complex services. This function first tries to
# detect if there are the proper variables set for a simple service, and if
# they do not exist, it then tries to find the complex function definition for
# the service.
#
# Additionally, it creates a chain for the subcommand.
smart_function() {
local type="${1}" # The current subcommand: server/client/route
local services="${2}" # The services to implement
shift 2
local service=
for service in $services
do
local servname="${service}"
test "${service}" = "custom" && local servname="${1}"
set_work_function "Preparing for service '${service}' of type '${type}' under interface '${work_name}'"
# Increase the command counter, to make all chains within a primary
# command, unique.
work_counter=$[work_counter + 1]
local suffix="u${work_counter}"
case "${type}" in
client)
suffix="c${work_counter}"
;;
server)
suffix="s${work_counter}"
;;
route)
suffix="r${work_counter}"
;;
*) error "Cannot understand type '${type}'."
return 1
;;
esac
local mychain="${work_name}_${servname}_${suffix}"
create_chain filter "in_${mychain}" "in_${work_name}" || return 1
create_chain filter "out_${mychain}" "out_${work_name}" || return 1
# Try the simple services first
simple_service "${mychain}" "${type}" "${service}" "$@"
local ret=$?
# simple service completed succesfully.
test $ret -eq 0 && continue
# simple service exists but failed.
if [ $ret -ne 127 ]
then
error "Simple service '${service}' returned an error ($ret)."
return 1
fi
# Try the custom services
local fn="rules_${service}"
set_work_function "Running complex rules function ${fn}() for ${type} '${service}'"
"${fn}" "${mychain}" "${type}" "$@"
local ret=$?
test $ret -eq 0 && continue
if [ $ret -eq 127 ]
then
error "There is no service '${service}' defined."
else
error "Complex service '${service}' returned an error ($ret)."
fi
return 1
done
return 0
}
# ------------------------------------------------------------------------------
# simple_service - convert a service definition to an inline service definition
# WHY:
# When a simple service is detected, there must be someone to call
# rules_custom() with the appropriate service definition parameters.
simple_service() {
local mychain="${1}"; shift
local type="${1}"; shift
local server="${1}"; shift
local server_varname="server_${server}_ports"
eval local server_ports="\$${server_varname}"
local client_varname="client_${server}_ports"
eval local client_ports="\$${client_varname}"
test -z "${server_ports}" -o -z "${client_ports}" && return 127
local x=
local varname="require_${server}_modules"
eval local value="\$${varname}"
for x in ${value}
do
require_kernel_module $x || return 1
done
if [ ${FIREHOL_NAT} -eq 1 ]
then
local varname="require_${server}_nat_modules"
eval local value="\$${varname}"
for x in ${value}
do
require_kernel_module $x || return 1
done
fi
set_work_function "Running simple rules for ${type} '${service}'"
rules_custom "${mychain}" "${type}" "${server}" "${server_ports}" "${client_ports}" "$@"
return $?
}
show_work_realcmd() {
test ${FIREHOL_EXPLAIN} -eq 1 && return 0
(
printf "\n\n"
printf "# === CONFIGURATION STATEMENT =================================================\n"
printf "# CONF:%3s>>> " ${FIREHOL_LINEID}
case $1 in
2) printf " "
;;
*) ;;
esac
printf "%q " "${work_realcmd[@]}"
printf "\n\n"
) >>${FIREHOL_OUTPUT}
}
work_realcmd_primary() {
work_realcmd=("$@")
test ${FIREHOL_CONF_SHOW} -eq 1 && show_work_realcmd 1
}
work_realcmd_secondary() {
work_realcmd=("$@")
test ${FIREHOL_CONF_SHOW} -eq 1 && show_work_realcmd 2
}
work_realcmd_helper() {
work_realcmd=("$@")
test ${FIREHOL_CONF_SHOW} -eq 1 && show_work_realcmd 3
}
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# START UP SCRIPT PROCESSING
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# On non RedHat machines we need success() and failure()
success() {
printf " OK"
}
failure() {
echo " FAILED"
}
# ------------------------------------------------------------------------------
# A small part bellow is copied from /etc/init.d/iptables
# On RedHat systems this will define success() and failure()
test -f /etc/init.d/functions && . /etc/init.d/functions
if [ -z "${IPTABLES_CMD}" -o ! -x "${IPTABLES_CMD}" ]; then
echo >&2 "Cannot find an executables iptables command."
exit 0
fi
KERNELMAJ=`${UNAME_CMD} -r | ${SED_CMD} -e 's,\..*,,'`
KERNELMIN=`${UNAME_CMD} -r | ${SED_CMD} -e 's,[^\.]*\.,,' -e 's,\..*,,'`
if [ "$KERNELMAJ" -lt 2 ] ; then
echo >&2 "FireHOL requires a kernel version higher than 2.3."
exit 0
fi
if [ "$KERNELMAJ" -eq 2 -a "$KERNELMIN" -lt 3 ] ; then
echo >&2 "FireHOL requires a kernel version higher than 2.3."
exit 0
fi
if ${LSMOD_CMD} 2>/dev/null | ${GREP_CMD} -q ipchains ; then
# Don't do both
echo >&2 "ipchains is loaded in the kernel. Please remove ipchains to run iptables."
exit 0
fi
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# COMMAND LINE ARGUMENTS PROCESSING
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
me="${0}"
arg="${1}"
shift
case "${arg}" in
explain)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_EXPLAIN=1
;;
helpme|wizard)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_WIZARD=1
;;
try)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_TRY=1
;;
start)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_TRY=0
;;
stop)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
test -f /var/lock/subsys/firehol && ${RM_CMD} -f /var/lock/subsys/firehol
test -f /var/lock/subsys/iptables && ${RM_CMD} -f /var/lock/subsys/iptables
echo -n $"FireHOL: Clearing Firewall:"
load_kernel_module ip_tables
tables=`${CAT_CMD} /proc/net/ip_tables_names`
for t in ${tables}
do
${IPTABLES_CMD} -t "${t}" -F
${IPTABLES_CMD} -t "${t}" -X
${IPTABLES_CMD} -t "${t}" -Z
# Find all default chains in this table.
chains=`${IPTABLES_CMD} -t "${t}" -nL | ${GREP_CMD} "^Chain " | ${CUT_CMD} -d ' ' -f 2`
for c in ${chains}
do
${IPTABLES_CMD} -t "${t}" -P "${c}" ACCEPT
done
done
success $"FireHOL: Clearing Firewall:"
echo
exit 0
;;
restart|force-reload)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_TRY=0
;;
condrestart)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_TRY=0
if [ -f /var/lock/subsys/firehol ]
then
exit 0
fi
;;
status)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
(
echo
echo "--- MANGLE ---------------------------------------------------------------------"
echo
${IPTABLES_CMD} -t mangle -nxvL
echo
echo
echo "--- NAT ------------------------------------------------------------------------"
echo
${IPTABLES_CMD} -t nat -nxvL
echo
echo
echo "--- FILTER ---------------------------------------------------------------------"
echo
${IPTABLES_CMD} -nxvL
) | ${LESS_CMD}
exit $?
;;
panic)
ssh_src=
ssh_sport="0:65535"
ssh_dport="0:65535"
if [ ! -z "${SSH_CLIENT}" ]
then
set -- ${SSH_CLIENT}
ssh_src="${1}"
ssh_sport="${2}"
ssh_dport="${3}"
elif [ ! -z "${1}" ]
then
ssh_src="${1}"
fi
echo -n $"FireHOL: Blocking all communications:"
load_kernel_module ip_tables
tables=`${CAT_CMD} /proc/net/ip_tables_names`
for t in ${tables}
do
${IPTABLES_CMD} -t "${t}" -F
${IPTABLES_CMD} -t "${t}" -X
${IPTABLES_CMD} -t "${t}" -Z
# Find all default chains in this table.
chains=`${IPTABLES_CMD} -t "${t}" -nL | ${GREP_CMD} "^Chain " | ${CUT_CMD} -d ' ' -f 2`
for c in ${chains}
do
${IPTABLES_CMD} -t "${t}" -P "${c}" ACCEPT
if [ ! -z "${ssh_src}" ]
then
${IPTABLES_CMD} -t "${t}" -A "${c}" -p tcp -s "${ssh_src}" --sport "${ssh_sport}" --dport "${ssh_dport}" -m state --state ESTABLISHED -j ACCEPT
${IPTABLES_CMD} -t "${t}" -A "${c}" -p tcp -d "${ssh_src}" --dport "${ssh_sport}" --sport "${ssh_dport}" -m state --state ESTABLISHED -j ACCEPT
fi
${IPTABLES_CMD} -t "${t}" -A "${c}" -j DROP
done
done
success $"FireHOL: Blocking all communications:"
echo
exit 0
;;
save)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_TRY=0
FIREHOL_SAVE=1
;;
debug)
test ! -z "${1}" && softwarning "Arguments after parameter '${arg}' are ignored."
FIREHOL_TRY=0
FIREHOL_DEBUG=1
;;
*) if [ ! -z "${arg}" -a -f "${arg}" ]
then
FIREHOL_CONFIG="${arg}"
arg="${1}"
test "${arg}" = "--" && arg="" && shift
test -z "${arg}" && arg="try"
case "${arg}" in
start)
FIREHOL_TRY=0
FIREHOL_DEBUG=0
;;
try)
FIREHOL_TRY=1
FIREHOL_DEBUG=0
;;
debug)
FIREHOL_TRY=0
FIREHOL_DEBUG=1
;;
*)
echo "Cannot accept command line argument '${arg}' here."
exit 1
;;
esac
else
${CAT_CMD} <<EOF
$Id: firehol.sh,v 1.231 2005/04/03 21:48:04 ktsaou Exp $
(C) Copyright 2003, Costa Tsaousis <costa@tsaousis.gr>
FireHOL is distributed under GPL.
EOF
${CAT_CMD} <<EOF
FireHOL supports the following command line arguments (only one of them):
start to activate the firewall configuration.
The configuration is expected to be found in
${FIREHOL_CONFIG_DIR}/firehol.conf
try to activate the firewall, but wait until
the user types the word "commit". If this word
is not typed within 30 seconds, the previous
firewall is restored.
stop to stop a running iptables firewall.
This will allow all traffic to pass unchecked.
restart this is an alias for start and is given for
compatibility with /etc/init.d/iptables.
condrestart will start the firewall only if it is not
already active. It does not detect a modified
configuration file.
status will show the running firewall, as in:
${IPTABLES_CMD} -nxvL | ${LESS_CMD}
panic will block all IP communication.
save to start the firewall and then save it to the
place where /etc/init.d/iptables looks for it.
Note that not all firewalls will work if
restored with:
/etc/init.d/iptables start
debug to parse the configuration file but instead of
activating it, to show the generated iptables
statements.
explain to enter interactive mode and accept configuration
directives. It also gives the iptables commands
for each directive together with reasoning.
helpme or to enter a wizard mode where FireHOL will try
wizard to figure out the configuration you need.
You can redirect the standard output of FireHOL to
a file to get the config to this file.
<a filename> a different configuration file.
If not other argument is given, the configuration
will be "tried" (default = try).
Otherwise the argument next to the filename can
be one of 'start', 'debug' and 'try'.
-------------------------------------------------------------------------
FireHOL supports the following services (sorted by name):
EOF
(
# The simple services
${CAT_CMD} "${me}" |\
${GREP_CMD} -e "^server_.*_ports=" |\
${CUT_CMD} -d '=' -f 1 |\
${SED_CMD} "s/^server_//" |\
${SED_CMD} "s/_ports\$//"
# The complex services
${CAT_CMD} "${me}" |\
${GREP_CMD} -e "^rules_.*()" |\
${CUT_CMD} -d '(' -f 1 |\
${SED_CMD} "s/^rules_/(*) /"
) | ${SORT_CMD} | ${UNIQ_CMD} |\
(
x=0
while read
do
x=$[x + 1]
if [ $x -gt 4 ]
then
printf "\n"
x=1
fi
printf "% 16s |" "$REPLY"
done
printf "\n\n"
)
${CAT_CMD} <<EOF
Services marked with (*) are "smart" or complex services.
All the others are simple single socket services.
Please note that the service:
all matches all packets, all protocols, all of everything,
while ensuring that required kernel modules are loaded.
any allows the matching of packets with unusual rules, like
only protocol but no ports. If service any is used
without other parameters, it does what service all does
but it does not handle kernel modules.
For example, to match GRE traffic use:
server any mygre accept proto 47
Service any does not handle kernel modules.
custom allows the definition of a custom service.
The template is:
server custom name protocol/sport cport accept
where name is just a name, protocol is the protocol the
service uses (tcp, udp, etc), sport is server port,
cport is the client port. For example, IMAP4 is:
server custom imap tcp/143 default accept
For more information about FireHOL, please refer to:
http://firehol.sourceforge.net
-------------------------------------------------------------------------
FireHOL controls your firewall. You should want to get updates quickly.
Subscribe (at the home page) to get notified of new releases.
-------------------------------------------------------------------------
YOU DO NOT KNOW WHAT TO DO? FireHOL can help you! Just run it with the
argument 'helpme' and it will generate its configuration file for this
machine. Your running firewall will not be altered or stopped, and no
systems settings will be modified. Just run:
${FIREHOL_FILE} helpme >/tmp/firehol.conf
and you will get the configuration written to /tmp/firehol.conf
EOF
exit 1
fi
;;
esac
# Remove the next arg if it is --
test "${1}" = "--" && shift
if [ ${FIREHOL_EXPLAIN} -eq 0 -a ${FIREHOL_WIZARD} -eq 0 -a ! -f "${FIREHOL_CONFIG}" ]
then
echo -n $"FireHOL config ${FIREHOL_CONFIG} not found:"
failure $"FireHOL config ${FIREHOL_CONFIG} not found:"
echo
exit 1
fi
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# MAIN PROCESSING - Interactive mode
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
if [ ${FIREHOL_EXPLAIN} -eq 1 ]
then
FIREHOL_CONFIG="Interactive User Input"
FIREHOL_LINEID="1"
FIREHOL_TEMP_CONFIG="${FIREHOL_DIR}/firehol.conf"
echo "version ${FIREHOL_VERSION}" >"${FIREHOL_TEMP_CONFIG}"
version ${FIREHOL_VERSION}
${CAT_CMD} <<EOF
$Id: firehol.sh,v 1.231 2005/04/03 21:48:04 ktsaou Exp $
(C) Copyright 2003, Costa Tsaousis <costa@tsaousis.gr>
FireHOL is distributed under GPL.
Home Page: http://firehol.sourceforge.net
--------------------------------------------------------------------------------
FireHOL controls your firewall. You should want to get updates quickly.
Subscribe (at the home page) to get notified of new releases.
--------------------------------------------------------------------------------
You can now start typing FireHOL configuration directives.
Special interactive commands: help, show, quit
EOF
while [ 1 = 1 ]
do
read -p "# FireHOL [${work_cmd}:${work_name}] > " -e -r
test -z "${REPLY}" && continue
set_work_function -ne "Executing user input"
while [ 1 = 1 ]
do
set -- ${REPLY}
case "${1}" in
help)
${CAT_CMD} <<EOF
You can use anything a FireHOL configuration file accepts, including variables,
loops, etc. Take only care to write loops in one row.
Additionaly, you can use the following commands:
help to print this text on your screen.
show to show all the successfull commands so far.
quit to show the interactively given configuration file
and quit.
in same as typing: interface eth0 internet
This is used as a shortcut to get into the server/client
mode in which you can test the rules for certain
services.
EOF
break
;;
show)
echo
${CAT_CMD} "${FIREHOL_TEMP_CONFIG}"
echo
break
;;
quit)
echo
${CAT_CMD} "${FIREHOL_TEMP_CONFIG}"
echo
exit 1
;;
in)
REPLY="interface eth0 internet"
continue
;;
*)
${CAT_CMD} <<EOF
# \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
# Cmd Line : ${FIREHOL_LINEID}
# Command : ${REPLY}
EOF
eval "$@"
if [ $? -gt 0 ]
then
printf "\n# > FAILED <\n"
else
if [ "${1}" = "interface" -o "${1}" = "router" ]
then
echo >>"${FIREHOL_TEMP_CONFIG}"
else
printf " " >>"${FIREHOL_TEMP_CONFIG}"
fi
printf "%s\n" "${REPLY}" >>"${FIREHOL_TEMP_CONFIG}"
FIREHOL_LINEID=$[FIREHOL_LINEID + 1]
printf "\n# > OK <\n"
fi
break
;;
esac
break
done
done
exit 0
fi
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# MAIN PROCESSING - help wizard
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
if [ ${FIREHOL_WIZARD} -eq 1 ]
then
# require commands for wizard mode
require_cmd ip
require_cmd netstat
require_cmd egrep
require_cmd date
require_cmd hostname
wizard_ask() {
local prompt="${1}"; shift
local def="${1}"; shift
echo
while [ 1 = 1 ]
do
printf >&2 "%s [%s] > " "${prompt}" "${def}"
read
local ans="${REPLY}"
test -z "${ans}" && ans="${def}"
local c=0
while [ $c -le $# ]
do
eval local t="\${${c}}"
test "${ans}" = "${t}" && break
c=$[c + 1]
done
test $c -le $# && return $c
printf >&2 "*** '${ans}' is not a valid answer. Pick one of "
printf >&2 "%s " "$@"
echo >&2
echo >&2
done
return 0
}
ip_in_net() {
local ip="${1}"; shift
local net="${1}"; shift
if [ -z "${ip}" -o -z "${net}" ]
then
return 1
fi
test "${net}" = "default" && net="0.0.0.0/0"
set -- `echo ${ip} | ${TR_CMD} './' ' '`
local i1=${1}
local i2=${2}
local i3=${3}
local i4=${4}
set -- `echo ${net} | ${TR_CMD} './' ' '`
local n1=${1}
local n2=${2}
local n3=${3}
local n4=${4}
local n5=${5:-32}
local i=$[i1*256*256*256 + i2*256*256 + i3*256 + i4]
local n=$[n1*256*256*256 + n2*256*256 + n3*256 + n4]
# echo "IP : '${i1}' . '${i2}' . '${i3}' . '${i4}'"
# echo "NET: '${n1}' . '${n2}' . '${n3}' . '${n4}' / '${n5}'"
local d=1
local c=${n5}
while [ $c -lt 32 ]
do
c=$[c + 1]
d=$[d * 2]
done
local nm=$[n + d - 1]
printf "### DEBUG: Is ${ip} part of network ${net}? "
if [ ${i} -ge ${n} -a ${i} -le ${nm} ]
then
echo "yes"
return 0
else
echo "no"
return 1
fi
}
ip_is_net() {
local ip="${1}"; shift
local net="${1}"; shift
if [ -z "${ip}" -o -z "${net}" ]
then
return 1
fi
test "${net}" = "default" && net="0.0.0.0/0"
set -- `echo ${ip} | ${TR_CMD} './' ' '`
local i1=${1}
local i2=${2}
local i3=${3}
local i4=${4}
local i5=${5:-32}
set -- `echo ${net} | ${TR_CMD} './' ' '`
local n1=${1}
local n2=${2}
local n3=${3}
local n4=${4}
local n5=${5:-32}
local i=$[i1*256*256*256 + i2*256*256 + i3*256 + i4]
local n=$[n1*256*256*256 + n2*256*256 + n3*256 + n4]
if [ ${i} -eq ${n} -a ${i5} -eq ${n5} ]
then
return 0
else
return 1
fi
}
ip2net() {
local ip="${1}"; shift
if [ -z "${ip}" ]
then
return 0
fi
if [ "${ip}" = "default" ]
then
echo "default"
return 0
fi
set -- `echo ${ip} | ${TR_CMD} './' ' '`
local i1=${1}
local i2=${2}
local i3=${3}
local i4=${4}
local i5=${5:-32}
echo ${i1}.${i2}.${i3}.${i4}/${i5}
}
ips2net() {
(
if [ "A${1}" = "A-" ]
then
while read ip
do
ip2net ${ip}
done
else
while [ ! -z "${1}" ]
do
ip2net ${1}
shift
done
fi
) | ${SORT_CMD} | ${UNIQ_CMD} | ${TR_CMD} "\n" " "
}
cd "${FIREHOL_DIR}"
"${MKDIR_CMD}" ports
"${MKDIR_CMD}" keys
cd ports
"${MKDIR_CMD}" tcp
"${MKDIR_CMD}" udp
"${CAT_CMD}" >&2 <<EOF
$Id: firehol.sh,v 1.231 2005/04/03 21:48:04 ktsaou Exp $
(C) Copyright 2003, Costa Tsaousis <costa@tsaousis.gr>
FireHOL is distributed under GPL.
Home Page: http://firehol.sourceforge.net
--------------------------------------------------------------------------------
FireHOL controls your firewall. You should want to get updates quickly.
Subscribe (at the home page) to get notified of new releases.
--------------------------------------------------------------------------------
FireHOL will now try to figure out its configuration file on this system.
Please have all the services and network interfaces on this system running.
Your running firewall will not be stopped or altered.
You can re-run the same command with output redirection to get the config
to a file. Example:
EOF
echo >&2 "${FIREHOL_FILE} helpme >/tmp/firehol.conf"
echo >&2
echo >&2
echo >&2
echo >&2 "Building list of known services."
echo >&2 "Please wait..."
${CAT_CMD} /etc/services |\
${TR_CMD} '\t' ' ' |\
${SED_CMD} "s/ \+/ /g" >services
for c in `echo ${!server_*} | ${TR_CMD} ' ' '\n' | ${GREP_CMD} "_ports$"`
do
serv=`echo $c | ${SED_CMD} "s/server_//" | ${SED_CMD} "s/_ports//"`
eval "ret=\${$c}"
for x in ${ret}
do
proto=`echo $x | ${CUT_CMD} -d '/' -f 1`
port=`echo $x | ${CUT_CMD} -d '/' -f 2`
test ! -d "${proto}" && continue
nport=`${EGREP_CMD} "^${port}[[:space:]][0-9]+/${proto}" services | ${CUT_CMD} -d ' ' -f 2 | ${CUT_CMD} -d '/' -f 1`
test -z "${nport}" && nport="${port}"
echo "server ${serv}" >"${proto}/${nport}"
done
done
echo "server ftp" >tcp/21
echo "server nfs" >udp/2049
echo "client amanda" >udp/10080
echo "server dhcp" >udp/67
echo "server dhcp" >tcp/67
echo "client dhcp" >udp/68
echo "client dhcp" >tcp/68
echo "server emule" >tcp/4662
echo "server pptp" >tcp/1723
echo "server samba" >udp/137
echo "server samba" >udp/138
echo "server samba" >tcp/139
wizard_ask "Press RETURN to start." "continue" "continue"
echo >&2
echo >&2 "--- snip --- snip --- snip --- snip ---"
echo >&2
echo "#!${FIREHOL_FILE}"
echo "# ------------------------------------------------------------------------------"
echo "# This feature is under construction -- use it with care."
echo "# *** NEVER USE THIS CONFIG AS-IS ***"
echo "# "
${CAT_CMD} <<EOF
# $Id: firehol.sh,v 1.231 2005/04/03 21:48:04 ktsaou Exp $
# (C) Copyright 2003, Costa Tsaousis <costa@tsaousis.gr>
# FireHOL is distributed under GPL.
# Home Page: http://firehol.sourceforge.net
#
# ------------------------------------------------------------------------------
# FireHOL controls your firewall. You should want to get updates quickly.
# Subscribe (at the home page) to get notified of new releases.
# ------------------------------------------------------------------------------
#
EOF
echo "# This config will have the same effect as NO PROTECTION!"
echo "# Everything that found to be running, is allowed."
echo "# "
echo "# Date: `${DATE_CMD}` on host `${HOSTNAME_CMD}`"
echo "# "
echo "# The TODOs bellow, are YOUR to-dos!"
echo
# globals for routing
set -a found_interfaces=
set -a found_ips=
set -a found_nets=
set -a found_excludes=
helpme_iface() {
local route="${1}"; shift
local i="${1}"; shift
local iface="${1}"; shift
local ifip="${1}"; shift
local ifnets="${1}"; shift
local ifreason="${1}"; shift
# one argument left: ifnets_excluded
if [ "${route}" = "route" ]
then
found_interfaces[$i]="${iface}"
found_ips[$i]="${ifip}"
found_nets[$i]="${ifnets}"
found_excludes[$i]="${1}"
fi
if [ "${ifnets}" = "default" ]
then
ifnets="not \"\${UNROUTABLE_IPS} ${1}\""
else
ifnets="\"${ifnets}\""
fi
# output the interface
echo
echo "# Interface No $i."
echo "# The purpose of this interface is to control the traffic"
if [ ! -z "${ifreason}" ]
then
echo "# ${ifreason}."
else
echo "# on the ${iface} interface with IP ${ifip} (net: ${ifnets})."
fi
echo "# TODO: Change \"interface${i}\" to something with meaning to you."
echo "# TODO: Check the optional rule parameters (src/dst)."
echo "# TODO: Remove 'dst ${ifip}' if this is dynamically assigned."
echo "interface ${iface} interface${i} src ${ifnets} dst ${ifip}"
echo
echo " # The default policy is DROP. You can be more polite with REJECT."
echo " # Prefer to be polite on your own clients to prevent timeouts."
echo " policy drop"
echo
echo " # If you don't trust the clients behind ${iface} (net ${ifnets}),"
echo " # add something like this."
echo " # > protection strong"
echo
echo " # Here are the services listening on ${iface}."
echo " # TODO: Normally, you will have to remove those not needed."
(
local x=
local ports=
for x in `${NETSTAT_CMD} -an | ${EGREP_CMD} "^tcp" | ${GREP_CMD} "0.0.0.0:*" | ${EGREP_CMD} " (${ifip}|0.0.0.0):[0-9]+" | ${CUT_CMD} -d ':' -f 2 | ${CUT_CMD} -d ' ' -f 1 | ${SORT_CMD} -n | ${UNIQ_CMD}`
do
if [ -f "tcp/${x}" ]
then
echo " `${CAT_CMD} tcp/${x}` accept"
else
ports="${ports} tcp/${x}"
fi
done
for x in `${NETSTAT_CMD} -an | ${EGREP_CMD} "^udp" | ${GREP_CMD} "0.0.0.0:*" | ${EGREP_CMD} " (${ifip}|0.0.0.0):[0-9]+" | ${CUT_CMD} -d ':' -f 2 | ${CUT_CMD} -d ' ' -f 1 | ${SORT_CMD} -n | ${UNIQ_CMD}`
do
if [ -f "udp/${x}" ]
then
echo " `${CAT_CMD} udp/${x}` accept"
else
ports="${ports} udp/${x}"
fi
done
echo " server ICMP accept"
echo "${ports}" | ${TR_CMD} " " "\n" | ${SORT_CMD} -n | ${UNIQ_CMD} | ${TR_CMD} "\n" " " >unknown.ports
) | ${SORT_CMD} | ${UNIQ_CMD}
echo
echo " # The following ${iface} server ports are not known by FireHOL:"
echo " # `${CAT_CMD} unknown.ports`"
echo " # TODO: If you need any of them, you should define new services."
echo " # (see Adding Services at the web site - http://firehol.sf.net)."
echo
echo " # The following means that this machine can REQUEST anything via ${iface}."
echo " # TODO: On production servers, avoid this and allow only the"
echo " # client services you really need."
echo " client all accept"
echo
}
interfaces=`${IP_CMD} link show | ${EGREP_CMD} "^[0-9A-Za-z]+:" | ${CUT_CMD} -d ':' -f 2 | ${SED_CMD} "s/^ //" | ${GREP_CMD} -v "^lo$" | ${SORT_CMD} | ${UNIQ_CMD} | ${TR_CMD} "\n" " "`
gw_if=`${IP_CMD} route show | ${GREP_CMD} "^default" | ${SED_CMD} "s/dev /dev:/g" | ${TR_CMD} " " "\n" | ${GREP_CMD} "^dev:" | ${CUT_CMD} -d ':' -f 2`
gw_ip=`${IP_CMD} route show | ${GREP_CMD} "^default" | ${SED_CMD} "s/via /via:/g" | ${TR_CMD} " " "\n" | ${GREP_CMD} "^via:" | ${CUT_CMD} -d ':' -f 2 | ips2net -`
i=0
for iface in ${interfaces}
do
echo "### DEBUG: Processing interface '${iface}'"
ips=`${IP_CMD} addr show dev ${iface} | ${SED_CMD} "s/ / /g" | ${SED_CMD} "s/ / /g" | ${SED_CMD} "s/ / /g" | ${GREP_CMD} "^ inet " | ${CUT_CMD} -d ' ' -f 3 | ${CUT_CMD} -d '/' -f 1 | ips2net -`
peer=`${IP_CMD} addr show dev ${iface} | ${SED_CMD} "s/ / /g" | ${SED_CMD} "s/ / /g" | ${SED_CMD} "s/ / /g" | ${SED_CMD} "s/peer /peer:/g" | ${TR_CMD} " " "\n" | ${GREP_CMD} "^peer:" | ${CUT_CMD} -d ':' -f 2 | ips2net -`
nets=`${IP_CMD} route show dev ${iface} | ${CUT_CMD} -d ' ' -f 1 | ips2net -`
if [ -z "${ips}" -o -z "${nets}" ]
then
echo
echo "# Ignoring interface '${iface}' because does not have an IP or route."
echo
continue
fi
for ip in ${ips}
do
echo "### DEBUG: Processing IP ${ip} of interface '${iface}'"
ifreason=""
# find all the networks this IP can access directly
# or through its peer
netcount=0
ifnets=
ofnets=
for net in ${nets}
do
test "${net}" = "default" && continue
found=1
ip_in_net ${ip} ${net}
found=$?
if [ ${found} -gt 0 -a ! -z "${peer}" ]
then
ip_in_net ${peer} ${net}
found=$?
fi
if [ ${found} -eq 0 ]
then
# Add it to ifnets
f=0; ff=0
while [ $f -lt $netcount ]
do
if ip_in_net ${net} ${ifnets[$f]}
then
# Already satisfied
ff=1
elif ip_in_net ${ifnets[$f]} ${net}
then
# New one is superset of old
ff=1
ifnets[$f]=${net}
fi
f=$[f + 1]
done
if [ $ff -eq 0 ]
then
# Add it
netcount=$[netcount + 1]
ifnets=(${net} ${ifnets[@]})
fi
else
ofnets=(${net} ${ofnets[@]})
fi
done
# find all the networks this IP can access through gateways
if [ ! -z "${ofnets[*]}" ]
then
for net in ${ofnets[@]}
do
test "${net}" = "default" && continue
nn=`echo "${net}" | ${CUT_CMD} -d "/" -f 1`
gw=`${IP_CMD} route show ${nn} dev ${iface} | ${EGREP_CMD} "^${nn}[[:space:]]+via[[:space:]][0-9\.]+" | ${CUT_CMD} -d ' ' -f 3 | ips2net -`
test -z "${gw}" && continue
for nn in ${ifnets[@]}
do
test "${nn}" = "default" && continue
if ip_in_net ${gw} ${nn}
then
echo "### DEBUG: Route ${net} is accessed through ${gw}"
# Add it to ifnets
f=0; ff=0
while [ $f -lt $netcount ]
do
if ip_in_net ${net} ${ifnets[$f]}
then
# Already satisfied
ff=1
elif ip_in_net ${ifnets[$f]} ${net}
then
# New one is superset of old
ff=1
ifnets[$f]=${net}
fi
f=$[f + 1]
done
if [ $ff -eq 0 ]
then
# Add it
netcount=$[netcount + 1]
ifnets=(${net} ${ifnets[@]})
fi
break
fi
done
done
fi
# Don't produce an interface if this is just a peer that is also the default gw
def_ignore_ifnets=0
if (test ${netcount} -eq 1 -a "${gw_if}" = "${iface}" && ip_is_net "${peer}" "${ifnets[*]}" && ip_is_net "${gw_ip}" "${peer}")
then
echo "### DEBUG: Skipping ${iface} peer ${ifnets[*]} only interface (default gateway)."
echo
def_ignore_ifnets=1
else
i=$[i + 1]
helpme_iface route $i "${iface}" "${ip}" "${ifnets[*]}" "${ifreason}"
fi
# Is this interface the default gateway too?
if [ "${gw_if}" = "${iface}" ]
then
for nn in ${ifnets[@]}
do
if ip_in_net "${gw_ip}" ${nn}
then
echo "### DEBUG: Default gateway ${gw_ip} is part of network ${nn}"
i=$[i + 1]
helpme_iface route $i "${iface}" "${ip}" "default" "from/to unknown networks behind the default gateway ${gw_ip}" "`test ${def_ignore_ifnets} -eq 0 && echo "${ifnets[*]}"`"
break
fi
done
fi
done
done
echo
echo "# The above $i interfaces were found active at this moment."
echo "# Add more interfaces that can potentially be activated in the future."
echo "# FireHOL will not complain if you setup a firewall on an interface that is"
echo "# not active when you activate the firewall."
echo "# If you don't setup an interface, FireHOL will drop all traffic from or to"
echo "# this interface, if and when it becomes available."
echo "# Also, if an interface name dynamically changes (i.e. ppp0 may become ppp1)"
echo "# you can use the plus (+) character to match all of them (i.e. ppp+)."
echo
if [ "1" = "`${CAT_CMD} /proc/sys/net/ipv4/ip_forward`" ]
then
x=0
i=0
while [ $i -lt ${#found_interfaces[*]} ]
do
i=$[i + 1]
inface="${found_interfaces[$i]}"
src="${found_nets[$i]}"
case "${src}" in
"default")
src="not \"\${UNROUTABLE_IPS} ${found_excludes[$i]}\""
;;
*)
src="\"${src}\""
;;
esac
j=0
while [ $j -lt ${#found_interfaces[*]} ]
do
j=$[j + 1]
test $j -eq $i && continue
outface="${found_interfaces[$j]}"
dst="${found_nets[$j]}"
dst_ip="${found_ips[$j]}"
case "${dst}" in
"default")
dst="not \"\${UNROUTABLE_IPS} ${found_excludes[$j]}\""
;;
*)
dst="\"${dst}\""
;;
esac
# Make sure we are not routing to the same subnet
test "${inface}" = "${outface}" -a "${src}" = "${dst}" && continue
# Make sure this is not a duplicate router
key="`echo ${inface}/${src}-${outface}/${dst} | ${TR_CMD} "/ \\\$\\\"{}" "______"`"
test -f "${FIREHOL_DIR}/keys/${key}" && continue
${TOUCH_CMD} "${FIREHOL_DIR}/keys/${key}"
x=$[x + 1]
echo
echo "# Router No ${x}."
echo "# Clients on ${inface} (from ${src}) accessing servers on ${outface} (to ${dst})."
echo "# TODO: Change \"router${x}\" to something with meaning to you."
echo "# TODO: Check the optional rule parameters (src/dst)."
echo "router router${x} inface ${inface} outface ${outface} src ${src} dst ${dst}"
echo
echo " # If you don't trust the clients on ${inface} (from ${src}), or"
echo " # if you want to protect the servers on ${outface} (to ${dst}),"
echo " # uncomment the following line."
echo " # > protection strong"
echo
echo " # To NAT client requests on the output of ${outface}, add this."
echo " # > masquerade"
echo " # Alternatively, you can SNAT them by placing this at the top of this config:"
echo " # > snat to ${dst_ip} outface ${outface} src ${src} dst ${dst}"
echo " # SNAT commands can be enhanced using 'proto', 'sport', 'dport', etc in order to"
echo " # NAT only some specific traffic."
echo
echo " # TODO: This will allow all traffic to pass."
echo " # If you remove it, no REQUEST will pass matching this traffic."
echo " route all accept"
echo
done
done
if [ ${x} -eq 0 ]
then
echo
echo
echo "# No router statements have been produced, because your server"
echo "# does not seem to need any."
echo
fi
else
echo
echo
echo "# No router statements have been produced, because your server"
echo "# is not configured for forwarding traffic."
echo
fi
exit 0
fi
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
#
# MAIN PROCESSING
#
# ------------------------------------------------------------------------------
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ------------------------------------------------------------------------------
# --- Initialization -----------------------------------------------------------
fixed_iptables_save() {
local tmp="${FIREHOL_DIR}/iptables-save-$$"
local err=
load_kernel_module ip_tables
${IPTABLES_SAVE_CMD} -c >$tmp
err=$?
if [ ! $err -eq 0 ]
then
${RM_CMD} -f $tmp >/dev/null 2>&1
return $err
fi
${CAT_CMD} ${tmp} |\
${SED_CMD} "s/--uid-owner !/! --uid-owner /g" |\
${SED_CMD} "s/--gid-owner !/! --gid-owner /g" |\
${SED_CMD} "s/--pid-owner !/! --pid-owner /g" |\
${SED_CMD} "s/--sid-owner !/! --sid-owner /g" |\
${SED_CMD} "s/--cmd-owner !/! --cmd-owner /g"
err=$?
${RM_CMD} -f $tmp >/dev/null 2>&1
return $err
}
echo -n $"FireHOL: Saving your old firewall to a temporary file:"
fixed_iptables_save >${FIREHOL_SAVED}
if [ $? -eq 0 ]
then
success $"FireHOL: Saving your old firewall to a temporary file:"
echo
else
test -f "${FIREHOL_SAVED}" && ${RM_CMD} -f "${FIREHOL_SAVED}"
failure $"FireHOL: Saving your old firewall to a temporary file:"
echo
exit 1
fi
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# Place all the statements bellow to the beginning of the final firewall script.
${CAT_CMD} >"${FIREHOL_OUTPUT}" <<EOF
#!/bin/sh
load_kernel_module ip_tables
load_kernel_module ip_conntrack
# Find all tables supported
tables=\`${CAT_CMD} /proc/net/ip_tables_names\`
for t in \${tables}
do
# Reset/empty this table.
${IPTABLES_CMD} -t "\${t}" -F >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t "\${t}" -F
${IPTABLES_CMD} -t "\${t}" -X >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t "\${t}" -X
${IPTABLES_CMD} -t "\${t}" -Z >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t "\${t}" -Z
# Find all default chains in this table.
chains=\`${IPTABLES_CMD} -t "\${t}" -nL | ${GREP_CMD} "^Chain " | ${CUT_CMD} -d ' ' -f 2\`
# If this is the 'filter' table, remember the default chains.
# This will be used at the end to make it DROP all packets.
test "\${t}" = "filter" && firehol_filter_chains="\${chains}"
# Set the policy to ACCEPT on all default chains.
for c in \${chains}
do
${IPTABLES_CMD} -t "\${t}" -P "\${c}" ACCEPT >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t "\${t}" -P "\${c}" ACCEPT
done
done
${IPTABLES_CMD} -t filter -P INPUT "\${FIREHOL_INPUT_ACTIVATION_POLICY}" >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t filter -P INPUT "\${FIREHOL_INPUT_ACTIVATION_POLICY}"
${IPTABLES_CMD} -t filter -P OUTPUT "\${FIREHOL_OUTPUT_ACTIVATION_POLICY}" >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t filter -P OUTPUT "\${FIREHOL_OUTPUT_ACTIVATION_POLICY}"
${IPTABLES_CMD} -t filter -P FORWARD "\${FIREHOL_FORWARD_ACTIVATION_POLICY}" >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t filter -P FORWARD "\${FIREHOL_FORWARD_ACTIVATION_POLICY}"
# Accept everything in/out the loopback device.
if [ "\${FIREHOL_TRUST_LOOPBACK}" = "1" ]
then
${IPTABLES_CMD} -A INPUT -i lo -j ACCEPT
${IPTABLES_CMD} -A OUTPUT -o lo -j ACCEPT
fi
# Drop all invalid packets.
# Netfilter HOWTO suggests to DROP all INVALID packets.
if [ "\${FIREHOL_DROP_INVALID}" = "1" ]
then
${IPTABLES_CMD} -A INPUT -m state --state INVALID -j DROP
${IPTABLES_CMD} -A OUTPUT -m state --state INVALID -j DROP
${IPTABLES_CMD} -A FORWARD -m state --state INVALID -j DROP
fi
EOF
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
echo -n $"FireHOL: Processing file ${FIREHOL_CONFIG}:"
ret=0
# ------------------------------------------------------------------------------
# Create a small awk script that inserts line numbers in the configuration file
# just before each known directive.
# These line numbers will be used for debugging the configuration script.
${CAT_CMD} >"${FIREHOL_TMP}.awk" <<"EOF"
/^[[:space:]]*blacklist[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*client[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*dnat[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*dscp[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*interface[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*iptables[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*mac[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*mark[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*masquerade[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*nat[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*policy[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*postprocess[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*protection[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*redirect[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*router[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*route[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*server[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*snat[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*tcpmss[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*tos[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*transparent_squid[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
/^[[:space:]]*transparent_proxy[[:space:]]/ { printf "FIREHOL_LINEID=${LINENO} " }
{ print }
EOF
${CAT_CMD} ${FIREHOL_CONFIG} | ${GAWK_CMD} -f "${FIREHOL_TMP}.awk" >${FIREHOL_TMP}
${RM_CMD} -f "${FIREHOL_TMP}.awk"
# ------------------------------------------------------------------------------
# Run the configuration file.
enable -n trap # Disable the trap buildin shell command.
enable -n exit # Disable the exit buildin shell command.
source ${FIREHOL_TMP} "$@" # Run the configuration as a normal script.
FIREHOL_LINEID="FIN"
enable trap # Enable the trap buildin shell command.
enable exit # Enable the exit buildin shell command.
close_cmd || ret=$[ret + 1]
close_master || ret=$[ret + 1]
${CAT_CMD} >>"${FIREHOL_OUTPUT}" <<EOF
# Make it drop everything on table 'filter'.
for c in \${firehol_filter_chains}
do
${IPTABLES_CMD} -t filter -P "\${c}" DROP >${FIREHOL_OUTPUT}.log 2>&1
r=\$?; test ! \${r} -eq 0 && runtime_error error \${r} INIT ${IPTABLES_CMD} -t filter -P "\${c}" DROP
done
EOF
if [ ${work_error} -gt 0 -o $ret -gt 0 ]
then
echo >&2
echo >&2 "NOTICE: No changes made to your firewall."
failure $"FireHOL: Processing file ${FIREHOL_CONFIG}:"
echo
exit 1
fi
success $"FireHOL: Processing file ${FIREHOL_CONFIG}:"
echo
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
for m in ${FIREHOL_KERNEL_MODULES}
do
postprocess -ne load_kernel_module $m
done
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if [ $FIREHOL_ROUTING -eq 1 ]
then
postprocess ${SYSCTL_CMD} -w "net.ipv4.ip_forward=1"
fi
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if [ ${FIREHOL_DEBUG} -eq 1 ]
then
${CAT_CMD} ${FIREHOL_OUTPUT}
exit 1
fi
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
echo -n $"FireHOL: Activating new firewall (${FIREHOL_COMMAND_COUNTER} rules):"
source ${FIREHOL_OUTPUT} "$@"
if [ ${work_final_status} -gt 0 ]
then
failure $"FireHOL: Activating new firewall:"
echo
# The trap will restore the firewall.
exit 1
fi
success $"FireHOL: Activating new firewall (${FIREHOL_COMMAND_COUNTER} rules):"
echo
if [ ${FIREHOL_TRY} -eq 1 ]
then
read -p "Keep the firewall? (type 'commit' to accept - 30 seconds timeout) : " -t 30 -e
ret=$?
echo
if [ ! $ret -eq 0 -o ! "${REPLY}" = "commit" ]
then
# The trap will restore the firewall.
exit 1
else
echo "Successfull activation of FireHOL firewall."
fi
fi
# Remove the saved firewall, so that the trap will not restore it.
${RM_CMD} -f "${FIREHOL_SAVED}"
# RedHat startup service locking.
if [ -d /var/lock/subsys ]
then
${TOUCH_CMD} /var/lock/subsys/iptables
${TOUCH_CMD} /var/lock/subsys/firehol
fi
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if [ ${FIREHOL_SAVE} -eq 1 ]
then
if [ -z "${FIREHOL_AUTOSAVE}" ]
then
if [ -d "/etc/sysconfig" ]
then
# RedHat
FIREHOL_AUTOSAVE="/etc/sysconfig/iptables"
elif [ -d "/var/lib/iptables" ]
then
if [ -f /etc/conf.d/iptables ]
then
# Gentoo
IPTABLES_SAVE=
. /etc/conf.d/iptables
FIREHOL_AUTOSAVE="${IPTABLES_SAVE}"
fi
if [ -z "${FIREHOL_AUTOSAVE}" ]
then
# Debian
FIREHOL_AUTOSAVE="/var/lib/iptables/autosave"
fi
else
error "Cannot find where to save iptables file. Please set FIREHOL_AUTOSAVE."
echo
exit 1
fi
fi
echo -n $"FireHOL: Saving firewall to ${FIREHOL_AUTOSAVE}:"
fixed_iptables_save >"${FIREHOL_AUTOSAVE}"
if [ ! $? -eq 0 ]
then
failure $"FireHOL: Saving firewall to ${FIREHOL_AUTOSAVE}:"
echo
exit 1
fi
success $"FireHOL: Saving firewall to ${FIREHOL_AUTOSAVE}:"
echo
exit 0
fi
|