This file is indexed.

/usr/share/shorewall/lib.cli is in shorewall-core 5.1.12.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#
# Shorewall 5.1 -- /usr/share/shorewall/lib.cli.
#
#     (c) 1999-2018 - Tom Eastep (teastep@shorewall.net)
#
#	Complete documentation is available at http://shorewall.net
#
#       This program is part of Shorewall.
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by the
#       Free Software Foundation, either version 2 of the license or, at your
#       option, any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# This library contains the command processing code common to /sbin/shorewall[6] and
# /sbin/shorewall[6]-lite. In Shorewall and Shorewall6, the lib.cli-std library is 
# loaded after this one and replaces some of the functions declared here.
#

SHOREWALL_CAPVERSION=50112

if [ -z "$g_basedir" ]; then
    #
    # This is modified by the installer when ${SHAREDIR} <> /usr/share
    #
    . /usr/share/shorewall/shorewallrc

    g_basedir=${SHAREDIR}/shorewall
fi

. ${g_basedir}/lib.core

#
# Issue an error message and die
#
startup_error() {
    echo "   ERROR: $@" >&2
    kill $$
    exit 1
}

only_root() {
    [ "$(id -u)" != 0 ] && fatal_error "The '$COMMAND' command may only be run by root"
}

#
# Display a chain if it exists
#

showfirstchain() # $1 = name of chain
{
    awk \
    'BEGIN	 {prnt=0; rslt=1; }; \
    /^$/	 { next; };\
    /^Chain/	 {if ( prnt == 1 ) { rslt=0; exit 0; }; };\
    /Chain '$1'/ { prnt=1; }; \
		 { if (prnt == 1)  print; };\
    END		 { exit rslt; }' $TMPFILE
}

showchain() # $1 = name of chain
{
    if [ "$firstchain" = "Yes" ]; then
	if showfirstchain $1; then
	    firstchain=
	fi
    else
	awk \
	'BEGIN	     {prnt=0;};\
	/^$|^ pkts/  { next; };\
	/^Chain/     {if ( prnt == 1 ) exit; };\
	/Chain '$1'/ { prnt=1; };\
		     { if (prnt == 1)  print; }' $TMPFILE
    fi
}

#
# Validate the value of RESTOREFILE
#
validate_restorefile() # $* = label
{
    case $RESTOREFILE in
	*/*)
	    error_message "ERROR: $@ must specify a simple file name: $RESTOREFILE"
	    exit 2
	    ;;
	.safe|.try)
	    ;;
	.*|NONE)
	    error_message "ERROR: Reserved File Name: $RESTOREFILE"
	    exit 2
	    ;;
    esac
}

#
# Clear descriptor 1 if it is a terminal
#
clear_term() {
    [ -t 1 ] && clear
}

#
# Delay $timeout seconds -- if we're running on a recent bash2 then allow
# <enter> to terminate the delay
#
timed_read ()
{
    read -t $timeout foo 2> /dev/null

    test $? -eq 2 && sleep $timeout
}

#
# Determine if 'syslogd -C' or logd -S is running
#
syslog_circular_buffer() {
    local pid
    local tty
    local flags
    local time
    local path
    local args
    local arg

    ps w 2> /dev/null | (
	while read pid tty stat time path args; do
	    case $path in
		syslogd|*/syslogd)
		    for arg in $args; do
			case $arg in
			    -C*)
				return 0
				;;
			esac
		    done
		    ;;
		logd|*/logd)
		    for arg in $args; do
			case $arg in
			    -S*)
				return 0
				;;
			esac
		    done
		    ;;
	    esac
	done

	return 1 )
}

setup_logread() {
    [ -z "$LOGFILE" ] && LOGFILE=/var/log/messages

    if syslog_circular_buffer; then
	LOGFILE=logread
	if qt mywhich tac; then
	    g_logread="logread | tac"
	else
	    g_logread="logread"
	fi
    elif [ "$LOGFILE" = "systemd" ]; then
	g_logread="journalctl -r"
    elif [ -r $LOGFILE ]; then
	if qt mywhich tac; then
	    g_logread="tac $LOGFILE"
	else
	    g_logread="cat $LOGFILE"
	fi
    else
	fatal_error "LOGFILE ($LOGFILE) does not exist or is not readable!"
    fi
}

#
# Display the last $1 packets logged
#
packet_log() # $1 = number of messages
{
    if qt mywhich tac; then
	if [ -n "$g_showmacs" -o $VERBOSITY -gt 2 ]; then
	    if [ $g_family -eq 4 ]; then
		$g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' | head -n$1 | tac | sed 's/ kernel://; s/\[.*\] //' | sed s/" $host $LOGFORMAT"/" "/
	    else
		$g_logread | grep 'IN=.* OUT=.*SRC=.*:.*DST=' | head -n$1 | tac | sed -r 's/ kernel://; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	    fi
	elif [ $g_family -eq 4 ]; then
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' |  head -n$1 | tac | sed 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] '// | sed s/" $host $LOGFORMAT"/" "/
	else
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*:.*DST=' |  head -n$1 | tac | sed -r 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	fi
    else
	if [ -n "$g_showmacs" -o $VERBOSITY -gt 2 ]; then
	    if [ $g_family -eq 4 ]; then
		$g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' | tail -n$1 | sed 's/ kernel://; s/\[.*\] //' | sed s/" $host $LOGFORMAT"/" "/
	    else
		$g_logread | grep 'IN=.* OUT=.*SRC=.*:.*DST=' | tail -n$1 | sed -r 's/ kernel://; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	    fi
	elif [ $g_family -eq 4 ]; then
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' |  tail -n$1 | sed 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] '// | sed s/" $host $LOGFORMAT"/" "/
	else
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*:.*DST=' |  tail -n$1 | sed -r 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	fi
    fi	
}

search_log() # $1 = IP address to search for
{
    if qt mywhich tac; then
	if [ -n "$g_showmacs" -o $VERBOSITY -gt 2 ]; then
            if [ $g_family -eq 4 ]; then
		$g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' | grep "$1" | tac | sed 's/ kernel://; s/\[.*\] //' | sed s/" $host $LOGFORMAT"/" "/
	    else
		$g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' | grep "$1" | tac | sed -r 's/ kernel://; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	    fi
	elif [ $g_family -eq 4 ]; then
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' |  grep "$1" | tac | sed 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] '// | sed s/" $host $LOGFORMAT"/" "/
	else
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' |  grep "$1" | tac | sed -r 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	fi
    else
	if [ -n "$g_showmacs" -o $VERBOSITY -gt 2 ]; then
            if [ $g_family -eq 4 ]; then
		$g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' | grep "$1" | sed 's/ kernel://; s/\[.*\] //' | sed s/" $host $LOGFORMAT"/" "/
	    else
		$g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' | grep "$1" | sed -r 's/ kernel://; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	    fi
	elif [ $g_family -eq 4 ]; then
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' |  grep "$1" | sed 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] '// | sed s/" $host $LOGFORMAT"/" "/
	else
	    $g_logread | grep 'IN=.* OUT=.*SRC=.*\..*DST=' |  grep "$1" | sed -r 's/ kernel://; s/MAC=.* SRC=/SRC=/; s/\[.*\] //; s/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | sed s/" $host $LOGFORMAT"/" "/
	fi
    fi
}

#
# Show traffic control information
#
show_tc1() {

    show_one_tc() {
	local device
	device=${1%@*}
	qdisc=$(tc qdisc list dev $device)

	if [ -n "$qdisc" ]; then
	    echo Device $device:
	    tc -s -d qdisc show dev $device
	    echo
	    tc -s -d class show dev $device
	    echo
	fi
    }

    if [ $# -gt 0 ]; then
	show_one_tc $1
    else
	ip -o link list | while read inx interface details; do
	    show_one_tc ${interface%:}
	done
    fi

}

show_tc() {
    echo "$g_product $SHOREWALL_VERSION Traffic Control at $g_hostname - $(date)"
    echo
    shift

    if [ -z "$1" ]; then
	$g_tool -t mangle -L -n -v | $output_filter
	echo
    fi

    show_tc1 $1
}

#
# Show classifier information
#
show_classifiers() {

    show_one_classifier() {
	local device
	device=${1%@*}
	qdisc=$(tc qdisc list dev $device)

	if [ -n "$qdisc" ]; then
	    echo Device $device:
	    qt tc -s filter ls root dev $device && tc -s filter ls root dev $device | grep -v '^$'
	    tc filter show dev $device
	    tc class show dev $device | fgrep 'leaf ' | fgrep -v ' hfsc' | sed 's/^.*leaf //;s/ .*//' | while read class; do
		if [ -n "$class" ]; then
		    echo
		    echo Node $class
		    tc filter show dev $device parent $class
		fi
	    done
	    echo
	fi
    }

    ip -o link list | while read inx interface details; do
	show_one_classifier ${interface%:}
    done

}

#
# Display blacklist chains
#
blacklist_filter() {
    awk \
	'BEGIN          { prnt=0; }; \
        /^Members:/     { print "Dynamic:"; prnt=1; next; }; \
                        { if (prnt == 1) print; };'
}

show_bl() {
    [ -n "$g_blacklistipset" ] && ipset -L $g_blacklistipset | blacklist_filter && echo
    $g_tool -L $g_ipt_options | \
	awk 'BEGIN           {prnt=0; };
            /^$/             {if (prnt == 1) print ""; prnt=0; };
            /Chain .*~ /     {prnt=1; };
            /Chain dynamic / {prnt=1; };
            {if (prnt == 1)  print; };
            END {if (prnt == 1 ) print "" };'
}

#
# Watch the Firewall Log
#
logwatch() # $1 = timeout -- if negative, prompt each time that
	   #		     an 'interesting' packet count changes
{
    setup_logread

    host=$(echo $g_hostname | sed 's/\..*$//')
    oldrejects=$($g_tool -L -v -n | grep 'LOG')

    if [ $1 -lt 0 ]; then
	timeout=$((- $1))
	pause="Yes"
    else
	pause="No"
	timeout=$1
    fi

    qt mywhich awk && haveawk=Yes || haveawk=

    while true; do
	clear_term
	echo "$banner $(date)"
	echo

	echo "Dropped/Rejected Packet Log ($LOGFILE)"
	echo

	show_reset

	rejects=$($g_tool -L -v -n | grep 'LOG')

	if [ "$rejects" != "$oldrejects" ]; then
	    oldrejects="$rejects"

	    printf '\a'

	    packet_log 40

	    if [ "$pause" = "Yes" ]; then
		echo
		printf 'Enter any character to continue: '
		read foo
	    else
		timed_read
	    fi
	else
	    echo
	    packet_log 40
	    timed_read
	fi
    done
}


#
# Try to find the arptables binary -- sets the variable 'arptables'
#
resolve_arptables() {
    arptables="$ARPTABLES"

    [ -n "${arptables:=arptables}" ]

    case $arptables in
	*/*)
	    ;;
	*)
	    arptables=$(mywhich "$arptables")
	    ;;
    esac
}

#
# Try to run the 'savesets' command
#
savesets() {
    local supported

    supported=$(run_it ${VARDIR}/firewall help | fgrep savesets )

    [ -n "$supported" ] && run_it ${VARDIR}/firewall savesets ${g_restorepath}-ipsets 
}

#
# Proactive save of the current ipset contents
#
savesets1() {
    local supported

    supported=$(run_it ${VARDIR}/firewall help | fgrep savesets )

    [ -n "$supported" ] && run_it ${VARDIR}/firewall savesets ${VARDIR}/ipsets.save && progress_message3 "The ipsets have been saved to ${VARDIR}/ipsets.save"
}

#
# Save currently running configuration
#
do_save() {
    local status
    local arptables
    status=0

    if [ -f ${VARDIR}/firewall ]; then
	if $iptables_save | grep -v -- '-A dynamic.* -j ACCEPT' > ${VARDIR}/restore-$$; then
	    cp -f ${VARDIR}/firewall $g_restorepath
	    mv -f ${VARDIR}/restore-$$ ${g_restorepath}-iptables
	    chmod 700 $g_restorepath
	    chmod 600 ${g_restorepath}-iptables
	    echo "   Currently-running Configuration Saved to $g_restorepath"
	    run_user_exit save
	else
	    rm -f ${VARDIR}/restore-$$
	    echo "   ERROR: Currently-running Configuration Not Saved" >&2
	    status=1
	fi
    else
	echo "   ERROR: ${VARDIR}/firewall does not exist" >&2
	status=1
    fi

    case ${SAVE_ARPTABLES:=No} in
	[Yy]es)
	    resolve_arptables

	    if [ -n "$arptables" ]; then
		if ${arptables}-save > ${VARDIR}/restore-$$; then
		    if grep -q '^-A' ${VARDIR}/restore-$$; then
			mv -f ${VARDIR}/restore-$$ ${g_restorepath}-arptables
			chmod 600 ${g_restorepath}-arptables
		    else
			rm -f ${VARDIR}/restore-$$
		    fi
		fi
	    else
		case "$ARPTABLES" in
		    */*)
			error_message "ERROR: ARPTABLES=$ARPTABLES does not exist or is not executable - arptables not saved"
			;;
		    *)
			error_message "ERROR: The arptables utility cannot be located - arptables not saved"
		    ;;
		esac

		rm -f ${g_restorepath}-arptables
	    fi
	    ;;
	[Nn]o)
	    rm -f ${g_restorepath}-arptables
	    ;;
	*)
	    error_message "WARNING: Invalid value ($SAVE_ARPTABLES) for SAVE_ARPTABLES"
	    ;;
    esac

    if ! savesets;  then
	case ${SAVE_IPSETS:=No} in
	    [Yy]es)
		case ${IPSET:=ipset} in
		    */*)
			if [ ! -x "$IPSET" ]; then
			    error_message "ERROR: IPSET=$IPSET does not exist or is not executable - ipsets are not saved"
			    IPSET=
			fi
			;;
		    *)
			IPSET="$(mywhich $IPSET)"
			[ -n "$IPSET" ] || error_message "ERROR: The ipset utility cannot be located - ipsets are not saved"
			;;
		esac

		if [ -n "$IPSET" ]; then
		    if eval $IPSET -S > ${VARDIR}/ipsets.tmp; then
			#
			# Don't save an 'empty' file
			#
			grep -qE -- '^(-N|create )' ${VARDIR}/ipsets.tmp && mv -f ${VARDIR}/ipsets.tmp ${g_restorepath}-ipsets && chmod 600 ${g_restorepath}-ipsets
		    fi
		fi
		;;
	    [Nn]o|ipv4|ipv6)
		;;
	    *)
		error_message "WARNING: Invalid value ($SAVE_IPSETS) for SAVE_IPSETS"
		;;
	esac
    fi

    return $status
}

save_config() {

    local result
    result=1

    iptables_save=${g_tool}-save

    [ -x $iptables_save ] || echo "$iptables-save does not exist or is not executable" >&2

    [ -n "$g_counters" ] && iptables_save="$iptables_save --counters"

    if product_is_started ; then
	[ -d ${VARDIR} ] || mkdir -p ${VARDIR}

	if [ -f $g_restorepath -a ! -x $g_restorepath ]; then
	    echo "   ERROR: $g_restorepath exists and is not a saved $g_product configuration" >&2
	else
	    case $RESTOREFILE in
		capabilities|chains|default_route|firewall|firewall.conf|nat|proxyarp|restarted|rt_tables|save|state|undo_routing|zones)
		    echo "   ERROR: Reserved file name: $RESTOREFILE" >&2
		    ;;
		*)
		    validate_restorefile RESTOREFILE
		    if do_save; then
			rm -f ${VARDIR}/save
			result=0
		    fi
		    ;;
	    esac
	fi
    else
	echo "$g_product isn't started" >&2
    fi

    return $result

}

#
# Recent Linux systems seem to like to print a randomly-ordered
# view of routing tables. This hack sorts the output into the
# order we all know and love
#
sort_routes() {
    local dest
    local second
    local rest
    local vlsm
    local maxvlsm
    local rule

    if [ $g_family -eq 4 ]; then
	maxvlsm=032
    else
	maxvlsm=128
    fi

    while read dest second rest; do
	if [ -n "$dest" ]; then
	    rule="$dest $second $rest"
	    case "$dest" in
		default)
		    echo "000 $rule"
		    ;;
		blackhole|local)
		    case "$second" in
			*/*)
			    vlsm=${second#*/}
			    printf "%03d %s\n" $vlsm "$rule"
			    ;;
			*)
			    echo "$maxvlsm $rule"
			    ;;
		    esac
		    ;;
		*/*)
		    vlsm=${dest#*/}
		    printf "%03d %s\n" $vlsm "$rule"
		    ;;
		*)
		    echo "$maxvlsm $rule"
		    ;;
	    esac
	fi
    done | sort -r | while read dest rest; do echo $rest; done
}

#
# Isolate the table in the routing rules being read from stdin.
# Piping through sed to remove trailing whitespace works around
# recent 'features' in dash and ip.
#
find_tables() {
    sed -r 's/[[:space:]]+$//' | while read rule; do
	echo ${rule##* }
    done
}

#
# Show routing configuration
#
show_routing() {
    local rule
    local table

    if [ -n "$(ip -$g_family rule list)" ]; then
	heading "Routing Rules"
	ip -$g_family rule list
	ip -$g_family rule list | find_tables | sort -u | while read table; do
	    heading "Table $table:"
	    if [ $g_family -eq 6 ]; then
		ip -6 -o route list table $table | grep -vF cache | sort_routes
	    else
		ip -4 -o route list table $table | sort_routes
	    fi
	done

	if [ -n "$g_routecache" ]; then
	    heading "Route Cache"
	    ip -$g_family route list cache
	fi
    else
	heading "Routing Table"
	if [ $g_family -eq 6 ]; then
	    ip -6 -o route list | grep -vF cache | sort_routes
	else
	    ip -4 -o route list table $table | sort_routes
	fi
    fi
}

determine_ipset_version() {
    local setname

    if [ -z "$IPSET" -o "$IPSET" = "ipset" ]; then
	IPSET=$(mywhich ipset)
	[ -n "$IPSET" ] || fatal_error "The ipset utility cannot be located"
    fi

    setname=fooX$$

    qt ipset -X $setname # Just in case something went wrong the last time

    if qt ipset -N $setname hash:ip family inet; then
	qt ipset -X $setname
	IPSETN="$IPSET"
    else
	IPSETN="$IPSET -n"
    fi
}

#
# 'list dynamic' command executor
#
find_sets() {
    local junk
    local setname

    $IPSETN -L | egrep "^Name: ${1}(_.+)?$" | while read junk setname; do echo $setname; done
}

list_zone() {

    local sets
    local setname

    determine_ipset_version

    if [ $g_family -eq 4 ]; then
	sets=$($IPSETN -L | egrep "^$1(_.+)?");
     else
	sets=$($IPSETN -L | egrep "^6_$1(_.+)?")
    fi

    [ -n "$sets" ] || sets=$(find_sets $1)

    for setname in $sets; do
	echo "${setname#${1}_}:"
	$IPSETN -L $setname | awk 'BEGIN        {prnt=0;}; \
                                   /^Members:/  {prnt=1; next; }; \
                                   /^Bindings:/ {prnt=0; }; \
                                                { if (prnt == 1) print "   ", $1; };'
    done
}

option_error() {
    fatal_error "The $COMMAND command does not accept this option: -$1"
}

too_many_arguments() {
    fatal_error "Too many arguments: $1"
}

missing_argument() {
    fatal_error "Missing argument"
}

missing_option_value() {
    fatal_error "The $1 option requires a value"
}

version_command() {
    local finished
    finished=0
    local all
    all=
    local product
    local compiletime

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			a*)
			    all=Yes
			    option=${option#a}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    [ $# -gt 0 ] && too_many_arguments

    if [ -n "$all" ]; then
	echo "shorewall-core: $(cat ${SHAREDIR}/shorewall/coreversion)"

	for product in shorewall shorewall6 shorewall-lite shorewall6-lite shorewall-init; do
	    if [ -f ${SHAREDIR}/$product/version ]; then
		echo "$product: $(cat ${SHAREDIR}/$product/version)"
	    fi
	done

	if [ "$(id -u)" -eq 0 -a -f $g_firewall ]; then
	    compiletime=$(run_it $g_firewall info 2>/dev/null)

	    case $compiletime in
		compiled\ *)
		    echo "$g_firewall was $compiletime"
		    ;;
		*)
		    echo "$g_firewall was compiled by Shorewall version $(run_it $g_firewall version))"
		    ;;
	    esac
	fi
    else
	echo $SHOREWALL_VERSION
    fi
}

#
# Show Filter - For Shorewall[6]-lite, if there was an scfilter file at compile-time,
#               then the compiler generated another version of this function and
#               embedded it in the firewall.conf file. That version supersedes this
#               one.
#
show_connections_filter() {
    local filter
    local command
    local first

    command=${SHOREWALL_SHELL}

    filter=$(find_file scfilter)

    if [ -f $filter ]; then
	first=$(head -n1 $filter)

	case $first in
	    \#!*)
		command=${first#\#!}
		;;
	esac

	$command $filter
    else
	cat -
    fi
}

show_nfacct() {
    if [ -n "$NFACCT" -a ! -x "$NFACCT" ]; then
	error_message "WARNING: NFACCT=$NFACCT does not exist or is not executable"
	NFACCT=
    else
	NFACCT=$(mywhich nfacct)
	[ -n "$NFACCT" ] || echo "No NF Accounting defined (nfacct not found)"
    fi

    if [ -n "$NFACCT" ]; then
	$NFACCT list
	echo
    fi
}

show_event() {
    local address
    local ttl_label
    local ttl
    local last_seen
    local last
    local oldest_pkt
    local oldest
    local intimes
    local outtimes1
    local outtimes2
    local time
    local count

    while read address ttl_label ttl last_seen last oldest_pkt oldest intimes; do
	case $address in
	    *.*)
		[ $g_family -eq 4 ] || continue
		;;
	    *:*)
		[ $g_family -eq 6 ] || continue
		;;
	    *)
		continue
		;;
	esac

	outtimes1=''
	outtimes2=''
	count=0
	last=$((($currenttime - $last)/1000))
	for time in $intimes; do
	    time=${time%,}
	    time=$(($currenttime - $time))
	    if [ $time -lt 10 ]; then
		time="000$time"
	    elif [ $time -lt 100 ]; then
		time="00$time"
	    elif [ $time -lt 1000 ]; then
		time="0$time"
	    fi

	    if [ $count -lt $oldest ]; then
		outtimes2="$outtimes2 $time"
	    else
		outtimes1="$outtimes1 $time"
	    fi

	    count=$(($count + 1))
	done

	outtimes1="${outtimes1}${outtimes2}"

	[ -n "$outtimes1" ] && outtimes1=$(echo "$outtimes1 " | sed -r 's/([[:digit:]]{3}) /\.\1, /g') && outtimes1=${outtimes1%, }

	echo "   $address : ${outtimes1}"
    done < /proc/net/xt_recent/$1
}

show_events() {
    local file
    local base
    local currenttime

    if [ -f /proc/net/xt_recent/%CURRENTTIME ]; then
	echo -127.0.0.1 > /proc/net/xt_recent/%CURRENTTIME
	echo +127.0.0.1 > /proc/net/xt_recent/%CURRENTTIME
	currenttime=$(cat /proc/net/xt_recent/%CURRENTTIME | cut -d ' ' -f 5 -)
	# echo Current time: $currenttime
	# echo
    else
	currenttime=0
    fi

    if [ $# -gt 0 ]; then
	for event in $@ ; do
	    if [ -f /proc/net/xt_recent/$event ]; then
		echo $event:
		show_event $event
		echo
	    else
		error_message "WARNING: Event $event not found"
	    fi
	done
    else
	for file in /proc/net/xt_recent/*; do
	    base=$(basename $file)

	    if [ "$base" != %CURRENTTIME -a "$base" != "*" ]; then
		echo $base
		show_event $base
		echo
	    fi
	done
    fi
}

show_actions() {
    if [ -f ${g_confdir}/actions ]; then
	cat ${g_sharedir}/actions.std ${g_confdir}/actions | grep -Ev '^[#?[:space:]]|^$'
    else
	grep -Ev '^[#?[:space:]]|^$' ${g_sharedir}/actions.std
    fi
}

show_chain() {
    echo "$g_product $SHOREWALL_VERSION $([ $# -gt 1 ] && echo "Chains " || [ $# -gt 0 ] && echo "Chain " || echo $table Table)$* at $g_hostname - $(date)"
    echo
    show_reset
    if [ $# -gt 0 ]; then
	for chain in $*; do
	    $g_tool -t $table -L $chain $g_ipt_options | $output_filter
	    echo
	done
    else
	$g_tool -t $table -L $g_ipt_options | $output_filter
    fi
}

show_chains() {
    echo "$g_product $SHOREWALL_VERSION $([ $# -gt 1 ] && echo "Chains " || echo "Chain ")$* at $g_hostname - $(date)"
    echo
    show_reset
    for chain in $*; do
	$g_tool -t $table -L $chain $g_ipt_options | $output_filter
	echo
    done
}

show_table() {
    echo "$g_product $SHOREWALL_VERSION $table Table at $g_hostname - $(date)"
    echo
    show_reset
    $g_tool -t $table -L $g_ipt_options | $output_filter
}

show_nat() {
    echo "$g_product $SHOREWALL_VERSION NAT Table at $g_hostname - $(date)"
    echo
    show_reset
    $g_tool -t nat -L $g_ipt_options | $output_filter
}

show_raw() {
    echo "$g_product $SHOREWALL_VERSION RAW Table at $g_hostname - $(date)"
    echo
    show_reset
    $g_tool -t raw -L $g_ipt_options | $output_filter
}

show_mangle() {
    echo "$g_product $SHOREWALL_VERSION Mangle Table at $g_hostname - $(date)"
    echo
    show_reset
    $g_tool -t mangle -L $g_ipt_options | $output_filter
}

show_classifiers_command() {
    echo "$g_product $SHOREWALL_VERSION Classifiers at $g_hostname - $(date)"
    echo
    show_classifiers
}

show_ip_addresses() {
    echo "$g_product $SHOREWALL_VERSION IP at $g_hostname - $(date)"
    echo
    ip -$g_family addr list
}

show_routing_command() {
    echo "$g_product $SHOREWALL_VERSION Routing at $g_hostname - $(date)"
    echo
    show_routing
}

show_policies() {
    echo "$g_product $SHOREWALL_VERSION Policies at $g_hostname - $(date)"
    echo
    [ -f ${VARDIR}/policies ] && cat ${VARDIR}/policies
}

show_ipa() {
    echo "$g_product $SHOREWALL_VERSION per-IP Accounting at $g_hostname - $(date)"
    echo
    perip_accounting
}

show_arptables() {
    echo "$g_product $SHOREWALL_VERSION arptables at $g_hostname - $(date)"
    echo
    $arptables -L -n -v
}

show_log() {
    echo "$g_product $SHOREWALL_VERSION Log ($LOGFILE) at $g_hostname - $(date)"
    echo
    show_reset
    host=$(echo $g_hostname | sed 's/\..*$//')

    if [ $# -eq 2 ]; then
	eval search_log $2
    elif [ -n "$g_pager" ]; then
	packet_log 100
    else
	packet_log 20
    fi
}

show_connections() {
    if [ $g_family -eq 4 ]; then
	if [ -d /proc/sys/net/netfilter/ ]; then
	    local count
	    local max
	    count=$(cat /proc/sys/net/netfilter/nf_conntrack_count)
	    max=$(cat /proc/sys/net/netfilter/nf_conntrack_max)
	    echo "$g_product $SHOREWALL_VERSION Connections ($count out of $max) at $g_hostname - $(date)"
	else
	    echo "$g_product $SHOREWALL_VERSION Connections at $g_hostname - $(date)"
	fi

	echo

	if qt mywhich conntrack ; then
	    shift
	    conntrack -f ipv4 -L $@ | show_connections_filter
	else
	    [ $# -gt 1 ] && too_many_arguments
	    if [ -f /proc/net/ip_conntrack ]; then
		cat /proc/net/ip_conntrack | show_connections_filter
	    else
		grep -v '^ipv6' /proc/net/nf_conntrack | show_connections_filter
	    fi
	fi
    elif qt mywhich conntrack ; then
	shift
	echo "$g_product $SHOREWALL_VERSION Connections at $g_hostname - $(date)"
	echo
	conntrack -f ipv6 -L $@ | show_connections_filter
    else
	[ $# -gt 1 ] && too_many_arguments
	if [ -f /proc/sys/net/netfilter/nf_conntrack_count -a -f /proc/sys/net/nf_conntrack ]; then
	    local count=$(cat /proc/sys/net/netfilter/nf_conntrack_count)
	    local max=$(cat /proc/sys/net/netfilter/nf_conntrack_max)
	    echo "$g_product $SHOREWALL_VERSION Connections ($count of $max) at $g_hostname - $(date)"
	    echo
	    grep '^ipv6' /proc/net/nf_conntrack | sed -r 's/0000:/:/g; s/:::+/::/g; s/:0+/:/g' | show_connections_filter
	fi
    fi
}

show_nfacct_command() {
    echo "$g_product $SHOREWALL_VERSION NF Accounting at $g_hostname - $(date)"
    echo
    show_nfacct
}

show_events_command() {
    echo "$g_product $SHOREWALL_VERSION events at $g_hostname - $(date)"
    echo
    show_events
}

show_blacklists() {
    echo "$g_product $SHOREWALL_VERSION blacklist chains at $g_hostname - $(date)"
    echo
    show_bl;
}

show_actions_sorted() {
    show_actions | sort
}

show_macros() {
    for directory in $(split $CONFIG_PATH); do
	temp=
	for macro in ${directory}/macro.*; do
	    case $macro in
		*\*)
                ;;
		*)
		    if [ -z "$temp" ]; then
			echo
			echo "Macros in $directory:"
			echo
			temp=Yes
		    fi
		    show_macro
		    ;;
	    esac
	done
    done
}

show_an_action() {
    echo "Shorewall $SHOREWALL_VERSION Action $1 at $g_hostname - $(date)"
    cat ${directory}/action.$1
}

show_a_macro() {
    echo "Shorewall $SHOREWALL_VERSION Macro $1 at $g_hostname - $(date)"
    cat ${directory}/macro.$1
}
#
# Don't dump empty SPD entries or entries from the other address family
#
spd_filter() {
    #
    # af   = Address Family (4 or 6)
    # afok = Address Family of entry matches af
    # p    = print the contents of A (entry is not empty)
    # i    = Number of lines stored in A
    #
    awk -v af=$g_family \
	'function prnt(A,i,    j) { while ( j < i ) print A[j++]; };\
\
	 /^src / { if (p) prnt( A, i );\
		   afok = 1;\
		   p	= 0;\
		   i	= 0;\
		   if ( af == 4 )\
		       { if ( /:/ )  afok = 0; }\
		   else\
		       { if ( /\./ ) afok = 0; }\
		 };\
		 { if ( afok ) A[i++] = $0; };\
	 /tmpl/	 { p = afok; };\
\
	 END	 { if (p) prnt( A, i ); }'
}
#
# Print a heading with leading and trailing black lines
#
heading() {
    echo
    echo "$@"
    echo
}

show_ipsec() {
    heading "PFKEY SPD"
    $IP -s -$g_family xfrm policy | spd_filter

    heading "PFKEY SAD"
    $IP -s -$g_family xfrm state | egrep -v '[[:space:]]+(auth-trunc|enc )' # Don't divulge the keys
}

show_ipsec_command() {
    echo "$g_product $SHOREWALL_VERSION IPSEC at $g_hostname - $(date)"
    show_ipsec
}

#
# Show Command Executor
#
show_command() {
    local finished
    finished=0
    local table
    table=filter
    local table_given
    table_given=
    local output_filter
    output_filter=cat
    local arptables
    local macro

    show_macro() {
	foo=`grep 'This macro' $macro | sed 's/This macro //'`
	if [ -n "$foo" ]; then
	    macro=$(basename $macro)
	    macro=${macro#*.}
	    foo=${foo%.*}
	    if [ ${#macro} -gt 5 ]; then
		printf "  $macro\t${foo#\#}\n"
	    else
		printf "  $macro\t\t${foo#\#}\n"
	    fi
	fi
    }

    # eliminates rules which have not been used from ip*tables' output
    brief_output() {
	awk \
	'/^Chain /  { heading1 = $0; getline heading2; printed = 0; next; };
         /^ +0 +0 / { next; };
         /^$/       { if ( printed == 1 ) { print $0; }; next; };
                    { if ( printed == 0 ) { print heading1; print heading2; printed = 1 }; };
	            { print; }';
    }

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			v*)
			    VERBOSITY=$(($VERBOSITY + 1 ))
			    option=${option#v}
			    ;;
			x*)
			    g_ipt_options="-xnv"
			    option=${option#x}
			    ;;
			m*)
			    g_showmacs=Yes
			    option=${option#m}
			    ;;
			f*)
			    g_filemode=Yes
			    option=${option#f}
			    ;;
			t)
			    [ $# -eq 1 ] && missing_option_value -t

			    case $2 in
				mangle|nat|filter|raw)
				    table=$2
				    table_given=Yes
				    ;;
				*)
				    fatal_error "Invalid table name ($s)"
				    ;;
			    esac

			    option=
			    shift
			    ;;
			l*)
			    g_ipt_options1="--line-numbers"
			    option=${option#l}
			    ;;
			c*)
			    g_routecache=Yes
			    option=${option#c}
			    ;;
			b*)
			    output_filter=brief_output
			    option=${option#b}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    g_ipt_options="$g_ipt_options $g_ipt_options1"


    [ -n "$g_debugging" ] && set -x

    COMMAND="$COMMAND $1"

    case "$1" in
	connections)
	    only_root
	    eval show_connections $@ $g_pager
	    ;;
	nat)
	    only_root
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_nat $g_pager
	    ;;
	raw)
	    only_root
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_raw $g_pager
	    ;;
	tos|mangle)
	    only_root
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_mangle $g_pager
	    ;;
	log)
	    [ $# -gt 2 ] && too_many_arguments $2

	    only_root
	    setup_logread
	    eval show_log $g_pager
	    ;;
	tc)
	    only_root
	    [ $# -gt 2 ] && too_many_arguments $2
	    eval show_tc $@ $g_pager
	    ;;
	classifiers|filters)
	    only_root
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_classifiers_command $g_pager
	    ;;
	zones)
	    only_root
	    [ $# -gt 1 ] && too_many_arguments $2
	    if [ -f ${VARDIR}/zones ]; then
		echo "$g_product $SHOREWALL_VERSION Zones at $g_hostname - $(date)"
		echo
		while read zone type hosts; do
		    echo "$zone ($type)"
		    for host in $hosts; do
			case $host in
			    exclude)
				echo "  exclude:"
				;;
			    *)
				echo "   $host"
				;;
			esac
		    done
		done < ${VARDIR}/zones
		echo
	    else
		fatal_error "${VARDIR}/zones does not exist"
	    fi
	    ;;
	capabilities)
	    only_root
	    [ $# -gt 1 ] && too_many_arguments $2
	    determine_capabilities
	    VERBOSITY=2
	    if [ -n "$g_filemode" ]; then
		eval report_capabilities1 $g_pager
	    else
		eval report_capabilities $g_pager
	    fi
	    ;;
	ip)
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_ip_addresses $g_pager
	    ;;
	routing)
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_routing_command $g_pager
	    ;;
	config)
	    . ${g_sharedir}/configpath
	    if [ -n "$g_filemode" ]; then
		echo "CONFIG_PATH=$CONFIG_PATH"
		echo "VARDIR=$VARDIR"
		echo "LIBEXEC=${LIBEXECDIR}"
		echo "SBINDIR=${SBINDIR}"
		echo "CONFDIR=${CONFDIR}"
		[ -n "$g_lite" ] && [ ${VARDIR} != /var/lib/$PRODUCT ] && echo "LITEDIR=${VARDIR}"
	    else
		echo "Default CONFIG_PATH is $CONFIG_PATH"
		echo "Default VARDIR is /var/lib/$PRODUCT"
		echo "LIBEXEC is ${LIBEXECDIR}"
		echo "SBINDIR is ${SBINDIR}"
		echo "CONFDIR is ${CONFDIR}"
		[ -n "$g_lite" ] && [ ${VARDIR} != /var/lib/$PRODUCT ] && echo "LITEDIR is ${VARDIR}"
	    fi
	    ;;
	chain)
	    only_root
	    shift
	    eval show_chain $@ $g_pager
	    ;;
	vardir)
	    echo $VARDIR;
	    ;;
	policies)
	    only_root
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_policies $g_pager
	    ;;
	ipa)
	    only_root
	    [ $g_family -eq 4 ] || fatal_error "'show ipa' is now available in $g_product"
	    [ $# -gt 1 ] && too_many_arguments $2
	    eval show_ipa $g_pager
	    ;;
	marks)
	    [ $# -gt 1 ] && too_many_arguments $2
	    only_root
	    echo "$g_product $SHOREWALL_VERSION Mark Layout at $g_hostname - $(date)"
	    echo
	    [ -f ${VARDIR}/marks ] && cat ${VARDIR}/marks;
	    ;;
	nfacct)
	    [ $# -gt 1 ] && too_many_arguments $2
	    only_root
	    eval show_nfacct_command $g_pager
	    ;;
	arptables)
	    [ $# -gt 1 ] && too_many_arguments $2
	    only_root
	    resolve_arptables
	    if [ -n "$arptables" -a -x $arptables ]; then
		eval show_arptables $g_pager
	    else
		error_message "Cannot locate the arptables executable"
	    fi
	    ;;
	event)
	    [ $# -gt 1 ] || too_many_arguments $2
	    only_root
	    echo "$g_product $SHOREWALL_VERSION events at $g_hostname - $(date)"
	    echo
	    shift
	    show_events $@
	    ;;
	events)
	    [ $# -gt 1 ] && too_many_arguments $2
	    only_root
	    eval show_events_command $g_pager
	    ;;
	bl|blacklists)
	    [ $# -gt 1 ] && too_many_arguments $2
	    only_root
	    setup_dbl
	    eval show_blacklists $g_pager
	    ;;
	opens)
	    [ $# -gt 1 ] && too_many_arguments $2
	    only_root
	    echo "$g_product $SHOREWALL_VERSION Temporarily opened connections at $g_hostname - $(date)"

	    if chain_exists dynamic; then
		g_ipt_options="$g_ipt_options --line-numbers"
		$g_tool -t filter -L dynamic $g_ipt_options  | head -n2
		$g_tool -t filter -L dynamic $g_ipt_options  | fgrep ACCEPT | $output_filter
	    fi
	    ;;
	ipsec)
	    [ $# -gt 1 ] && too_many_arguments $2
	    only_root
	    eval show_ipsec_command $g_pager
	    ;;
	*)
	    case "$PRODUCT" in
		*-lite)
		    ;;
	    	*)
		    case $1 in
			action)
			    [ $# -lt 2 ] && fatal_error 'Missing <action>'
			    [ $# -gt 2 ] && too_many_arguments $2

			    for directory in $(split $CONFIG_PATH); do
				if [ -f ${directory}/action.$2 ]; then
				    eval show_an_action $2 $g_pager
				    return
				fi
			    done

			    echo "   WARNING: Action $2 not found" >&2
			    return
			    ;;
			actions)
			    [ $# -gt 1 ] && too_many_arguments $2
			    eval show_actions_sorted $g_pager
			    return
			    ;;
			macro)
			    [ $# -lt 2 ] && fatal_error 'Missing <macro>'
			    [ $# -ne 2 ] && too_many_arguments $2
			    for directory in $(split $CONFIG_PATH); do
				if [ -f ${directory}/macro.$2 ]; then
				    eval show_a_macro $2 $g_pager
				    return
				fi
			    done
			    echo "   WARNING: Macro $2 not found" >&2
			    return
			    ;;
			macros)
			    [ $# -gt 1 ] && too_many_arguments $2
			    eval show_macros $g_pager
			    return
			    ;;
		    esac
		    ;;
	    esac

	    only_root

	    if [ $# -gt 0 ]; then
		if [ $1 = dynamic -a $# -gt 1 ]; then
		    shift
		    [ $# -eq 1 ] || too_many_arguments $2
		    list_zone $1
		    return;
                fi

		[ -n "$table_given" ] || for chain in $*; do
		    if ! qt $g_tool -t $table -L $chain $g_ipt_options; then
			error_message "ERROR: Chain '$chain' is not recognized by $g_tool."
			exit 1
		    fi
					 done

		eval show_chains $@ $g_pager
	    else
		eval show_table $g_pager
	    fi
	    ;;
    esac
}

perip_accounting() {
    if qt mywhich iptaccount; then
	local hnames
	local hname

	hnames=$(iptaccount -a | grep '^Found table:' | cut -d ' ' -f 3)

	if [ -n "$hnames" ]; then
	    for hname in $hnames; do
		iptaccount -l $hname | egrep '^IP:|^Show'
		echo
	    done
	else
	    echo "   No IP Accounting Tables Defined"
	    echo
	fi
    else
	echo "   iptaccount is not installed"
    fi
}

#
# Dump Filter - For Shorewall-lite, if there was a dumpfilter file at compile-time,
#               then the compiler generated another version of this function and
#               embedded it in the firewall.conf file. That version supersedes this
#               one.
#
dump_filter() {
    local filter
    local command
    local first

    command=${SHOREWALL_SHELL}

    filter=$(find_file dumpfilter)

    if [ -f $filter ]; then
	first=$(head -n1 $filter)

	case $first in
	    \#!*)
		command=${first#\#!}
		;;
	esac

	eval $command $filter $g_pager
    else
	cat -
    fi
}

dump_filter_wrapper() {
    eval dump_filter $g_pager
}

show_status() {
    local compiletime
    local state

    if product_is_started ; then
	[ $VERBOSITY -ge 1 ] && echo "$g_product is running"
	status=0
    else
	[ $VERBOSITY -ge 1 ] && echo "$g_product is stopped"
	status=4
    fi

    if [ -f ${VARDIR}/state ]; then
	state="$(cat ${VARDIR}/state)"
	case $state in
	    Stopped*|Closed*|Clear*)
		status=3
		;;
	esac
    else
	state=Unknown
    fi

    if [ $VERBOSITY -ge 1 ]; then 
	if [ -f $g_firewall ]; then
	    compiletime=$(run_it $g_firewall info 2>/dev/null)

	    case $compiletime in
		compiled\ *)
		    state="$state ($g_firewall $compiletime)"
		    ;;
		*)
		    state="$state ($g_firewall compiled by Shorewall version $(run_it $g_firewall version))"
		    ;;
	    esac
	fi

	echo "State:$state"
	echo
    fi

}

#
# Dump Command Executor
#
do_dump_command() {
    local finished
    finished=0
    local arptables

    resolve_arptables

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			x*)
			    g_ipt_options="-xnv"
			    option=${option#x}
			    ;;
			m*)
			    g_showmacs=Yes
			    option=${option#m}
			    ;;
			l*)
			    g_ipt_options1="--line-numbers"
			    option=${option#l}
			    ;;
			c*)
			    g_routecache=Yes
			    option=${option#c}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    setup_logread

    g_ipt_options="$g_ipt_options $g_ipt_options1"

    [ $VERBOSITY -lt 2 ] && VERBOSITY=2

    [ -n "$g_debugging" ] && set -x
    [ $# -eq 0 ] || too_many_arguments $1
    clear_term
    echo "$g_product $SHOREWALL_VERSION Dump at $g_hostname - $(date)"
    echo

    if [ $g_family -eq 6 ] && [ -f ${SHAREDIR}/shorewall/version ]; then
	echo "   Shorewall $(cat ${SHAREDIR}/shorewall/version)"
	echo
    fi
    show_status
    show_reset
    host=$(echo $g_hostname | sed 's/\..*$//')
    $g_tool -L $g_ipt_options

    if [ -n "$arptables" -a -x "$arptables" ]; then
	heading "ARP rules"
	$arptables -L -n -v
    fi

    heading "Log ($LOGFILE)"
    packet_log 20

    if qt $g_tool -t nat -L -n; then
	heading "NAT Table"
	$g_tool -t nat -L $g_ipt_options
    fi

    if qt $g_tool -t mangle -L -n; then
	heading "Mangle Table"
	$g_tool -t mangle -L $g_ipt_options
    fi

    if qt $g_tool -t raw -L -n; then
	heading "Raw Table"
	$g_tool -t raw -L $g_ipt_options
    fi

    local count
    local max

    if [ -f /proc/sys/net/netfilter/nf_conntrack_count ]; then
	count=$(cat /proc/sys/net/netfilter/nf_conntrack_count)
	max=$(cat /proc/sys/net/netfilter/nf_conntrack_max)

	heading "Conntrack Table ($count out of $max)"
    elif [ -f /proc/sys/net/ipv4/netfilter/ip_conntrack_count ]; then
	count=$(cat /proc/sys/net/ipv4/netfilter/ip_conntrack_count)
	max=$(cat /proc/sys/net/ipv4/netfilter/ip_conntrack_max)

	heading "Conntrack Table ($count out of $max)"
    else
	heading "Conntrack Table"
    fi

    if qt mywhich conntrack; then
	conntrack -f ipv${g_family} -L 2> /dev/null
    elif [ $g_family -eq 4 ]; then
    	[ -f /proc/net/ip_conntrack ] && cat /proc/net/ip_conntrack || grep -v '^ipv6' /proc/net/nf_conntrack
    else
	grep '^ipv6' /proc/net/nf_conntrack
    fi

    heading "IP Configuration"
    ip -$g_family addr list

    heading "IP Stats"
    ip -stat link list

    if qt mywhich brctl; then
	heading "Bridges"
	brctl show
    fi

    show_routing

    if [ $g_family -eq 4 ]; then
	heading "Per-IP Counters"

	perip_accounting
    fi

    heading "NF Accounting"
    show_nfacct

    heading "Events"
    show_events

    show_ipsec

    heading "/proc"
    show_proc /proc/version

    if [ $g_family -eq 4 ]; then
	show_proc /proc/sys/net/ipv4/ip_forward
	show_proc /proc/sys/net/ipv4/icmp_echo_ignore_all

	for directory in /proc/sys/net/ipv4/conf/*; do
	    for file in proxy_arp arp_filter arp_ignore rp_filter log_martians; do
		show_proc $directory/$file
	    done
	done
    else
	for directory in /proc/sys/net/ipv6/conf/*; do
	    for file in forwarding proxy_ra proxy_ndp; do
		show_proc $directory/$file
	    done
	done
    fi

    if [ $g_family -eq 4 ]; then
	heading "ARP"
	if qt mywhich arp; then
	    arp -na
	else
	    ip -4 neigh ls
	    ip -4 neigh ls proxy
	fi
    else
	heading "Neighbors"
	ip -6 neigh ls
    fi

    if qt mywhich lsmod; then
	heading "Modules"

	if [ $g_family -eq 4 ]; then
	    lsmod | grep -E '^(ip_|ipt_|iptable_|nf_|xt_)' | sort
	else
	    lsmod | grep -E '^(x_|ip6|nf_|xt_)' | sort
	fi
    fi

    determine_capabilities
    echo
    report_capabilities

    echo

    qt mywhich ss && ss -${g_family}tunap || { qt mywhich netstat && netstat -tunap; }

    if [ -n "$TC_ENABLED" ]; then
	heading "Traffic Control"
	show_tc1
	heading "TC Filters"
	show_classifiers
	fi
}

dump_command() {
    do_dump_command $@ | dump_filter_wrapper
}

#
# Restore Comand Executor
#
restore_command() {
    local finished
    finished=0
    local result

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			n*)
			    g_noroutes=Yes
			    option=${option#n}
			    ;;
			p*)
			    [ -n "$(which conntrack)" ] || fatal_error "The '-p' option requires the conntrack utility which does not appear to be installed on this system"
			    g_purge=Yes
			    option=${option%p}
			    ;;
			C*)
			    g_counters=Yes
			    option=${option#C}
			    ;;
			*)
			    option_error
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    case $# in
    0)
	;;
    1)
	RESTOREFILE="$1"
	validate_restorefile '<restore file>'
	;;
    *)
	too_many_arguments $2
	;;
    esac

    if [ -z "$STARTUP_ENABLED" ]; then
	error_message "ERROR: Startup is disabled"
	exit 6
    fi

    g_restorepath=${VARDIR}/$RESTOREFILE

    [ -n "$g_nolock" ] || mutex_on

    if [ -x $g_restorepath ]; then
	progress_message3 "Restoring $g_product..."

	run_it $g_restorepath restore && progress_message3 "$g_product restored from ${VARDIR}/$RESTOREFILE"
	result=$?

	[ -n "$g_nolock" ] || mutex_off

	exit $result
    else
	echo "File $g_restorepath: file not found"
	[ -n "$g_nolock" ] || mutex_off
	exit 2
    fi
}

#
# Display the time that the counters were last reset
#
show_reset() {
    [ -f ${VARDIR}/restarted ] && \
	echo "Counters reset $(cat ${VARDIR}/restarted)" && \
	echo
}

#
# Display's the passed file name followed by "=" and the file's contents.
#
show_proc() # $1 = name of a file
{
    [ -f $1 ] && echo "   $1 = $(cat $1)"
}

#
# Create the appropriate -q option to pass onward
#
make_verbose() {
    local v
    v=$g_verbose_offset
    local option
    option=-

    if [ -n "$g_use_verbosity" ]; then
	echo "-v$g_use_verbosity"
    elif [ $g_verbose_offset -gt 0 ]; then
	while [ $v -gt 0 ]; do
	    option="${option}v"
	    v=$(($v - 1))
	done

	echo $option
    elif [ $g_verbose_offset -lt 0 ]; then
	while [ $v -lt 0 ]; do
	    option="${option}q"
	    v=$(($v + 1))
	done

	echo $option
    fi
}

#
# Executor for drop,reject,... commands
#
block() # $1 = command, $2 = Finished, $3 - $n addresses
{
    local chain
    chain=$1
    local finished
    finished=$2
    local which
    which='-s'
    local range
    range='--src-range'

    if ! chain_exists dynamic; then
	echo "Dynamic blacklisting is not enabled in the current $g_product configuration" >&2
	[ -n "$g_nolock" ] || mutex_off
	exit 2
    fi

    shift 3

    while [ $# -gt 0 ]; do
	case $1 in
	    from)
		which='-s'
		range='--src-range'
		shift
		continue
		;;
	    to)
		which='-d'
		range='--dst-range'
		shift
		continue
		;;
	    *-*)
		qt $g_tool -D dynamic -m iprange $range $1 -j reject
		qt $g_tool -D dynamic -m iprange $range  $1 -j DROP
		qt $g_tool -D dynamic -m iprange $range $1 -j logreject
		qt $g_tool -D dynamic -m iprange $range $1 -j logdrop
		$g_tool -A dynamic -m iprange $range $1 -j $chain || break 1
		;;
	    *)
		qt $g_tool -D dynamic $which $1 -j reject
		qt $g_tool -D dynamic $which $1 -j DROP
		qt $g_tool -D dynamic $which $1 -j logreject
		qt $g_tool -D dynamic $which $1 -j logdrop
		$g_tool -A dynamic $which $1 -j $chain || break 1
		;;
	esac

	echo "$1 $finished"
	shift
    done
}

#
# Replace commas with spaces and echo the result
#
separate_list() {
    local list
    list="$@"
    local part
    local newlist
    local firstpart
    local lastpart
    local enclosure

    case "$list" in
	*,|,*|*,,*|*[[:space:]]*)
	    #
	    # There's been whining about us not catching embedded white space in
	    # comma-separated lists. This is an attempt to snag some of the cases.
	    #
            echo "WARNING -- invalid comma-separated list \"$@\"" >&2
	    ;;
	*\[*\]*)
	    #
	    # Where we need to embed comma-separated lists within lists, we enclose them
	    # within square brackets.
	    #
	    firstpart=${list%%\[*}
	    lastpart=${list#*\[}
	    enclosure=${lastpart%%\]*}
	    lastpart=${lastpart#*\]}
	    case $lastpart in
		\,*)
		    case $firstpart in
			*\,)
			    echo "$(separate_list ${firstpart%,}) [$enclosure] $(separate_list ${lastpart#,})"
			    ;;
			*)
			    echo "$(separate_list $firstpart)[$enclosure] $(separate_list ${lastpart#,})"
			    ;;
		    esac
		    ;;
		*)
		    case $firstpart in
			*\,)
			    echo "$(separate_list ${firstpart%,}) [$enclosure]$(separate_list $lastpart)"
			    ;;
			*)
			    echo "$(separate_list $firstpart)[$enclosure]$(separate_list $lastpart)"
			    ;;
		    esac
		    ;;
	    esac
	    return
	    ;;
    esac

    list="$@"
    part="${list%%,*}"
    newlist="$part"

    while [ "x$part" != "x$list" ]; do
	list="${list#*,}";
	part="${list%%,*}";
	newlist="$newlist $part";
    done

    echo "$newlist"
}

#
# add command executor
#
add_command() {
    local interface host hostlist zone ipset
    if ! product_is_started ; then
	fatal_error "$g_product Not Started"
    fi

    determine_ipset_version

    case $1 in
	*:*)
	    while [ $# -gt 1 ]; do
		if [ $g_family -eq 4 ]; then
		    interface=${1%%:*}
		    host=${1#*:}
		else
		    interface=${1%%|*}
		    host=${1#*|}
		fi

		[ "$host" = "$1" ] && host=

		if [ -z "$host" ]; then
		    if [ $g_family -eq 4 ]; then
			hostlist="$hostlist $interface:0.0.0.0/0"
		    else
			hostlist="$hostlist $interface:::/0"
		    fi
		else
		    for h in $(separate_list $host); do
			hostlist="$hostlist $interface:$h"
		    done
		fi

		shift
	    done
	    ;;
	*)
	    ipset=$1
	    shift
	    while [ $# -gt 0 ]; do
		for h in $(separate_list $1); do
		    hostlist="$hostlist $h"
		done
		shift
	    done
	    ;;
    esac

    zone=$1

    if [ -n "$zone" ]; then
	for host in $hostlist; do
	    if [ $g_family -eq 4 ]; then
		interface=${host%:*}
		ipset=${zone}_${interface};
	    else
		interface=${host%%:*}
		ipset=6_${zone}_${interface};
	    fi

	    ipset=$(echo $ipset | sed 's/\./_/g');

	    if ! qt $IPSET -L $ipset; then
		fatal_error "Zone $zone, interface $interface does not have a dynamic host list"
	    fi

	    host=${host#*:}

	    if $IPSET -A $ipset $host; then
		echo "Host $interface:$host added to zone $zone"
	    else
		fatal_error "Unable to add $interface:$host to zone $zone"
	    fi
	done
    else
	qt $IPSET -L $ipset || fatal_error "Zone $ipset is not dynamic"

	for host in $hostlist; do
	    if $IPSET -A $ipset $host; then
		echo "Host $host added to zone $ipset"
	    else
		fatal_error "Unable to add $host to zone $ipset"
	    fi
	done
    fi
}

#
# delete command executor
#
delete_command() {
    local interface host hostent hostlist zone ipset
    if ! product_is_started ; then
	fatal_error "$g_product Not Started"
    fi

    determine_ipset_version

    case $1 in
	*:*)
	    while [ $# -gt 1 ]; do
		if [ $g_family -eq 4 ]; then
		    interface=${1%%:*}
		    host=${1#*:}
		else
		    interface=${1%%|*}
		    host=${1#*|}
		fi

		[ "$host" = "$1" ] && host=

		if [ -z "$host" ]; then
		    if [ $g_family -eq 4 ]; then
			hostlist="$hostlist $interface:0.0.0.0/0"
		    else
			hostlist="$hostlist $interface:::/0"
		    fi
		else
		    for h in $(separate_list $host); do
			hostlist="$hostlist $interface:$h"
		    done
		fi

		shift
	    done
	    ;;
	*)
	    ipset=$1
	    shift
	    while [ $# -gt 0 ]; do
		for h in $(separate_list $1); do
		    hostlist="$hostlist $h"
		done
		shift
	    done
	    ;;
    esac

    zone=$1

    if [ -n "$zone" ]; then
	for host in $hostlist; do
	    if [ $g_family -eq 4 ]; then
		interface=${host%:*}
		ipset=${zone}_${interface};
	    else
		interface=${host%%:*}
		ipset=6_${zone}_${interface};
	    fi

	    ipset=$(echo $ipset | sed 's/./_/g');

	    if ! qt $IPSET -L $ipset -n; then
		fatal_error "Zone $zone, interface $interface does not have a dynamic host list"
	    fi

	    host=${host#*:}

	    if $IPSET -D $ipset $host; then
		echo "Host $host deleted from zone $zone"
	    else
		echo "   WARNING: Unable to delete host $hostent to zone $zone" >&2
	    fi
	done
    else
	qt $IPSET -L $ipset -n || fatal_error "Zone $ipset is not dynamic"

	for host in $hostlist; do
	    if $IPSET -D $ipset $host; then
		echo "Host $host deleted from to zone $ipset"
	    else
		echo "   WARNING: Unable to delete host $host from zone $zone" >&2
	    fi
	done
    fi
}

open_close_command() {
    local command
    local desc
    local proto
    local icmptype

    open_close_setup() {
	[ -n "$g_nolock" ] || mutex_on

	if ! product_is_started ; then
	    [ -n "$g_nolock" ] || mutex_off
	    fatal_error "The $COMMAND command requires the firewall to be running"
	fi

	if ! chain_exists dynamic; then
	    [ -n "$g_nolock" ] || mutex_off
	    fatal_error "The $COMMAND command requires DYNAMIC_BLACKLIST=Yes in the running configuration"
	fi
    }

    [ $# -le 4 ] || fatal_error "Too many parameters"

    if [ $COMMAND = open ]; then
	[ $# -ge 2 ] || fatal_error "Too few parameters"
    else
	[ $# -ge 1 ] || fatal_error "Too few parameters"
    fi

    if [ $# -eq 1 ]; then
	#
	# close <rule number>
	#
	case $1 in
	    [1-9]|[1-9][0-9]|[1-9][0-9][0-9]*)
		;;
	    *)
		fatal_error "$1 is not a valid temporary open number"
		;;
	esac

	open_close_setup #Conditionally acquires mutex

	if $g_tool -L dynamic --line-numbers | grep -q "^$1 .* ACCEPT "; then
	    if $g_tool -D dynamic $1; then
		[ -n "$g_nolock" ] || mutex_off
		echo "Temporary open #$1 closed"
		return 0
	    fi
	    [ -n "$g_nolock" ] || mutex_off
	    return 2
	else
	    [ -n "$g_nolock" ] || mutex_off
	    fatal_error "$1 is not a valid temporary open number"
	fi
    else
	if [ $1 = all ]; then
	    command=dynamic
	else
	    command="dynamic -s $1"
	fi

	if [ $2 != all ]; then
	    command="$command -d $2"
	fi

	desc="from $1 to $2"

	if [ $# -ge 3 ]; then
	    proto=$3

	    [ $proto = icmp -a $g_family -eq 6 ] && proto=58

	    command="$command -p $proto"

	    case $3 in
		[0-9]*)
		    desc="$desc protocol $3"
		    ;;
		*)
		    desc="$desc $3"
		    ;;
	    esac

	    if [ $g_family -eq 4 ]; then
		if [ $proto = 6 -o $proto = icmp ]; then
		    proto=icmp
		    icmptype='--icmp-type'
		fi
	    else
		if [ $proto = 58 -o $proto = ipv6-icmp ]; then
		    proto=icmp
		    icmptype='--icmpv6-type'
		fi
	    fi
	fi

	if [ $# -eq 4 ]; then
	    if [ $proto = icmp ]; then
		case $4 in
		    *,*)
			fatal_error "Only a single ICMP type may be specified"
			;;
		    [0-9]*)
			desc="$desc type $4"
			;;
		    *)
			desc="$desc $4"
			;;
		esac

		command="$command $icmptype $4"
	    else
		case $4 in
		*,*)
		    command="$command -m multiport --dports $4"
		    ;;
		*)
		    command="$command --dport $4"
		    ;;
		esac

		case $4 in
		    [0-9]*,)
			desc="$desc ports $4"
			;;
		    [0-9]*)
			desc="$desc port $4"
			;;
		    *)
			desc="$desc $4"
			;;
		esac
	    fi
	fi

	command="$command -j ACCEPT"

	open_close_setup #Conditionally acquires mutex

	if [ $COMMAND = open ]; then
	    if $g_tool -I $command ; then
		[ -n "$g_nolock" ] || mutex_off
		echo "Firewall dynamically opened for connections $desc"
		return 0
	    fi
	    [ -n "$g_nolock" ] || mutex_off
	    return 2
	fi

	if $g_tool -D $command 2> /dev/null; then
	    [ -n "$g_nolock" ] || mutex_off
	    echo "Firewall dynamically closed for connections $desc (may still be permitted by rules/policies)"
	    return 0
	fi

	[ -n "$g_nolock" ] || mutex_off
	fatal_error "Connections $desc are not currently opened"
    fi
}

#
# 'hits' commmand executor
#
hits_command() {
    local finished
    finished=0
    local today
    today=

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			t*)
			    today=$(date +'^%b %_d.*')
			    option=${option#t}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    [ $# -eq 0 ] || too_many_arguments $1

    clear_term
    echo "$g_product $SHOREWALL_VERSION Hits at $g_hostname - $(date)"
    echo

    timeout=30

    if $g_logread | grep -q "${today}IN=.* OUT=" ; then
	echo "   HITS IP	        DATE"
	echo "   ---- --------------- ------"
	$g_logread | grep "${today}IN=.* OUT=" | sed 's/\(.\{6\}\)\(.*SRC=\)\(.*\)\( DST=.*\)/\3	\1/' | sort | uniq -c | sort -rn | while read count address month day; do
	    printf '%7d %-15s %3s %2d\n' $count $address $month $day
	done

	echo ""

	echo "   HITS IP	        PORT"
	echo "   ---- --------------- -----"
	$g_logread | grep  "${today}IN=.* OUT=" | sed 's/\(.*SRC=\)\(.*\)\( DST=.*DPT=\)\([0-9]\{1,5\}\)\(.*\)/\2	\4/
						t
						s/\(.*SRC=\)\(.*\)\( DST=.*\)/\2/' | sort | uniq -c | sort -rn | while read count address port; do
	    [ -z "$port" ] && port=0
	    printf '%7d %-15s %d\n' $count $address $port
	done

	echo ""

	echo "   HITS DATE"
	echo "   ---- ------"
	$g_logread | grep  "${today}IN=.* OUT=" | sed 's/\(.\{6\}\)\(.*\)/\1/' | sort | uniq -c | sort -rn | while read count month day; do
	    printf '%7d %3s %2d\n' $count $month $day
	done

	echo ""

	echo "   HITS  PORT SERVICE(S)"
	echo "   ---- ----- ----------"
	$g_logread | grep "${today}IN=.* OUT=.*DPT" | sed 's/\(.*DPT=\)\([0-9]\{1,5\}\)\(.*\)/\2/' | sort | uniq -c | sort -rn | while read count port ; do
	    # List all services defined for the given port
	    srv=$(grep "^[^#].*\\b$port/" /etc/services | cut -f 1 | cut -f 1 -d' ' | sort -u)
	    srv=$(echo $srv | sed 's/ /,/g')

	    if [ -n "$srv" ] ; then
		printf '%7d %5d %s\n' $count $port $srv
	    else
		printf '%7d %5d\n' $count $port
	    fi
	done
    fi
}

#
# Issue an error message and terminate if the firewall isn't started
#
require_started() {
    if ! product_is_started; then
	error_message "ERROR: $g_product is not started"
	exit 2
    fi
}

#
# 'allow' command executor
#
allow_command() {

    local allowed
    local which
    which='-s'
    local range
    range='--src-range'
    local dynexists

    [ -n "$g_debugging" ] && set -x
    [ $# -eq 1 ] && missing_argument

    if [ -n "$g_blacklistipset" ]; then
	case ${IPSET:=ipset} in
	    */*)
		if [ ! -x "$IPSET" ]; then
		    fatal_error "IPSET=$IPSET does not exist or is not executable"
		fi
		;;
	    *)
		IPSET="$(mywhich $IPSET)"
		[ -n "$IPSET" ] || fatal_error "The ipset utility cannot be located"
		;;
	esac
    fi

    if chain_exists dynamic; then
	dynexists=Yes
    elif [ -z "$g_blacklistipset" ]; then
	require_started
	fatal_error "Dynamic blacklisting is not enabled in the current $g_product configuration"
    fi

    [ -n "$g_nolock" ] || mutex_on

    while [ $# -gt 1 ]; do
	shift

	allowed=''

	case $1 in
	    from)
		which='-s'
		range='--src-range'
		continue
		;;
	    to)
		which='-d'
		range='--dst-range'
		continue
		;;
	    *-*)
		if [ -n "$g_blacklistipset" ]; then
		    if qt $IPSET -D $g_blacklistipset $1; then
			allowed=Yes
		    fi
		fi

		if [ -n "$dynexists" ]; then
		    if  qt $g_tool -D dynamic -m iprange $range $1 -j reject    ||\
			qt $g_tool -D dynamic -m iprange $range $1 -j DROP      ||\
			qt $g_tool -D dynamic -m iprange $range $1 -j logdrop   ||\
			qt $g_tool -D dynamic -m iprange $range $1 -j logreject
		    then
			allowed=Yes
		    fi
		fi
		;;
	    *)
		if [ -n "$g_blacklistipset" ]; then
		    if qt $IPSET -D $g_blacklistipset $1; then
			allowed=Yes
		    fi
		fi

		if [ -n "$dynexists" ]; then
		    if  qt $g_tool -D dynamic $which $1 -j reject    ||\
		        qt $g_tool -D dynamic $which $1 -j DROP      ||\
			qt $g_tool -D dynamic $which $1 -j logdrop   ||\
			qt $g_tool -D dynamic $which $1 -j logreject
		    then
			allowed=Yes
		    fi
		fi
		;;
	esac

	if [ -n "$allowed" ]; then
	    progress_message2 "$1 Allowed"
	else
	    error_message "WARNING: $1 already allowed (not dynamically blacklisted)"
	fi
    done

    [ -n "$g_nolock" ] || mutex_off
}

#
# 'logwatch' command executor
#
logwatch_command() {
    shift

    finished=0

    while [ $finished -eq 0 -a $# -ne 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			v*)
			    VERBOSITY=$(($VERBOSITY + 1 ))
			    option=${option#v}
			    ;;
			q*)
			    VERBOSITY=$(($VERBOSITY - 1 ))
			    option=${option#q}
			    ;;
			m*)
			    g_showmacs=Yes
			    option=${option#m}
			    ;;
			-)
			    finished=1
			    option=
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    [ -n "$g_debugging" ] && set -x

    if [ $# -eq 1 ]; then
	logwatch $1
    elif [ $# -eq 0 ]; then
	logwatch 30
    else
	too_many_arguments $2
    fi
}

#
# Determine which optional facilities are supported by iptables/netfilter
#
determine_capabilities() {

    local tool
    local chain
    local chain1
    local arptables
    local helper

    if [ -z "$g_tool" ]; then
	[ $g_family -eq 4 ] && tool=iptables || tool=ip6tables

	g_tool=$(mywhich $tool)

	if [ -z "$g_tool" ]; then
	    fatal-error "No executable $tool binary can be found on your PATH"
	fi
    fi

    qt $g_tool -t nat    -L -n && NAT_ENABLED=Yes    || NAT_ENABLED=
    qt $g_tool -t mangle -L -n && MANGLE_ENABLED=Yes || MANGLE_ENABLED=

    [ "$IP" = ip -o -z "$IP" ] && IP=$(which ip)

    [ -n "$IP" -a -x "$IP" ] || IP=

    [ "$TC" = tc -o -z "$TC" ] && TC=$(which tc)

    [ -n "$TC" -a -x "$TC" ] || TC=

    CONNTRACK_MATCH=
    NEW_CONNTRACK_MATCH=
    OLD_CONNTRACK_MATCH=
    MULTIPORT=
    XMULTIPORT=
    EMULTIPORT=
    POLICY_MATCH=
    PHYSDEV_MATCH=
    PHYSDEV_BRIDGE=
    IPRANGE_MATCH=
    RECENT_MATCH=
    REAP_OPTION=
    OWNER_MATCH=
    OWNER_NAME_MATCH=
    IPSET_MATCH=
    OLD_IPSET_MATCH=
    IPSET_MATCH_NOMATCH=
    IPSET_MATCH_COUNTERS=
    IPSET_V5=
    CONNMARK=
    XCONNMARK=
    CONNMARK_MATCH=
    XCONNMARK_MATCH=
    RAW_TABLE=
    IPP2P_MATCH=
    OLD_IPP2P_MATCH=
    LENGTH_MATCH=
    CLASSIFY_TARGET=
    ENHANCED_REJECT=
    USEPKTTYPE=
    KLUDGEFREE=
    MARK=
    XMARK=
    EXMARK=
    TPROXY_TARGET=
    MANGLE_FORWARD=
    COMMENTS=
    ADDRTYPE=
    TCPMSS_MATCH=
    HASHLIMIT_MATCH=
    NFQUEUE_TARGET=
    REALM_MATCH=
    HELPER_MATCH=
    CONNLIMIT_MATCH=
    TIME_MATCH=
    GOTO_TARGET=
    LOGMARK_TARGET=
    IPMARK_TARGET=
    LOG_TARGET=
    ULOG_TARGET=
    NFLOG_TARGET=
    PERSISTENT_SNAT=
    FLOW_FILTER=
    FWMARK_RT_MASK=
    MARK_ANYWHERE=
    HEADER_MATCH=
    ACCOUNT_TARGET=
    AUDIT_TARGET=
    CONDITION_MATCH=
    IPTABLES_S=
    BASIC_FILTER=
    BASIC_EMATCH=
    CT_TARGET=
    STATISTIC_MATCH=
    IMQ_TARGET=
    DSCP_MATCH=
    DSCP_TARGET=
    GEOIP_MATCH=
    RPFILTER_MATCH=
    NFACCT_MATCH=
    CHECKSUM_TARGET=
    ARPTABLESJF=
    MASQUERADE_TGT=
    UDPLITEREDIRECT=
    NEW_TOS_MATCH=
    TARPIT_TARGET=
    IFACE_MATCH=
    TCPMSS_TARGET=
    WAIT_OPTION=
    CPU_FANOUT=
    NETMAP_TARGET=
    NFLOG_SIZE=
    RESTORE_WAIT_OPTION=

    AMANDA_HELPER=
    FTP_HELPER=
    FTP0_HELPER=
    IRC_HELPER=
    IRC0_HELPER=
    NETBIOS_NS_HELPER=
    H323_HELPER=
    PPTP_HELPER=
    SANE_HELPER=
    SANE0_HELPER=
    SIP_HELPER=
    SIP0_HELPER=
    SNMP_HELPER=
    TFTP_HELPER=
    TFTP0_HELPER=

    resolve_arptables

    if [ -n "$arptables" -a -x "$arptables" ]; then
	qt $arptables -L OUT && ARPTABLESJF=Yes
    fi

    [ -z "$(${g_tool}-restore --wait < /dev/null 2>&1)" ] && RESTORE_WAIT_OPTION=Yes

    if qt $g_tool --wait -t filter -L INPUT -n -v; then
	WAIT_OPTION=Yes
	g_tool="$g_tool --wait"
    fi

    chain=fooX$$

    if [ -n "$NAT_ENABLED" ]; then
	if qt $g_tool -t nat -N $chain; then
	    if [ $g_family -eq 4 ]; then
		qt $g_tool -t nat -A $chain -j SNAT --to-source 1.2.3.4 --persistent && PERSISTENT_SNAT=Yes
		qt $g_tool -t nat -A $chain -j NETMAP --to 1.2.3.0/24 && NETMAP_TARGET=Yes
	    else
		qt $g_tool -t nat -A $chain -j SNAT --to-source 2001::1 --persistent && PERSISTENT_SNAT=Yes
		qt $g_tool -t nat -A $chain -j NETMAP --to 2001:470:B:227::/64 && NETMAP_TARGET=Yes
	    fi
	    qt $g_tool -t nat -A $chain -j MASQUERADE && MASQUERADE_TGT=Yes
	    qt $g_tool -t nat -L INPUT -n && NAT_INPUT_CHAIN=Yes
	    qt $g_tool -t nat -A $chain -p udplite -m multiport --dport 33 -j REDIRECT --to-port 22 && UDPREDIRECT=Yes
	    qt $g_tool -t nat -F $chain
	    qt $g_tool -t nat -X $chain
	fi
    fi

    qt $g_tool -F $chain
    qt $g_tool -X $chain
    if ! $g_tool -N $chain; then
	fatal_error "The command \"$g_tool -N $chain\" failed"
    fi

    chain1=${chain}1

    qt $g_tool -F $chain1
    qt $g_tool -X $chain1
    if ! $g_tool -N $chain1; then
	qt $g_tool -X $CHAIN
	fatal_error "The command \"$g_tool -N $chain1\" failed"
    fi

    if ! qt $g_tool -A $chain -m state --state ESTABLISHED,RELATED -j ACCEPT &&
       ! qt $g_tool -A $chain -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT; then
	qt $g_tool -x $chain
	qt $g_tool -x $chain1
	fatal_error "Your kernel lacks connection tracking and/or state matching -- $g_product will not run on this system"
    fi

    if [ $g_family -eq 4 ]; then
	qt $g_tool -A $chain -m conntrack --ctorigdst 192.168.1.1 -j ACCEPT && CONNTRACK_MATCH=Yes
    else
	qt $g_tool -A $chain -m conntrack --ctorigdst ::1 -j ACCEPT && CONNTRACK_MATCH=Yes
    fi

    if [ -n "$CONNTRACK_MATCH" ]; then
	qt $g_tool -A $chain -m conntrack -p tcp --ctorigdstport 22 -j ACCEPT && NEW_CONNTRACK_MATCH=Yes

	if [ $g_family -eq 4 ]; then
	    qt $g_tool -A $chain -m conntrack ! --ctorigdst 1.2.3.4 || OLD_CONNTRACK_MATCH=Yes
	else
	    qt $g_tool -A $chain -m conntrack ! --ctorigdst ::1 || OLD_CONNTRACK_MATCH=Yes
	fi
    fi

    if qt $g_tool -A $chain -p tcp -m multiport --dports 21,22 -j ACCEPT; then
	MULTIPORT=Yes
	qt $g_tool -A $chain -p tcp -m multiport --sports 60 -m multiport --dports 99 -j ACCEPT && KLUDEFREE=Yes
    fi

    qt $g_tool -A $chain -p tcp  -m multiport --dports 21:22 -j ACCEPT           && XMULTIPORT=Yes
    qt $g_tool -A $chain -p sctp -m multiport --dports 21,22 -j ACCEPT           && EMULTIPORT=Yes
    qt $g_tool -A $chain -m policy --pol ipsec --mode tunnel --dir in -j ACCEPT && POLICY_MATCH=Yes

    if qt $g_tool -A $chain -m physdev --physdev-out eth0 -j ACCEPT; then
	PHYSDEV_MATCH=Yes
	qt $g_tool -A $chain -m physdev --physdev-is-bridged --physdev-in eth0 --physdev-out eth0 -j ACCEPT && PHYSDEV_BRIDGE=Yes
	if [ -z "${KLUDGEFREE}" ]; then
	    qt $g_tool -A $chain -m physdev --physdev-in eth0 -m physdev --physdev-out eth0 -j ACCEPT && KLUDGEFREE=Yes
	fi
    fi

    if [ $g_family -eq 4 ]; then
	if qt $g_tool -A $chain -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT; then
	    IPRANGE_MATCH=Yes
	    if [ -z "${KLUDGEFREE}" ]; then
		qt $g_tool -A $chain -m iprange --src-range 192.168.1.5-192.168.1.124 -m iprange --dst-range 192.168.1.5-192.168.1.124 -j ACCEPT && KLUDGEFREE=Yes
	    fi
	fi
    elif qt $g_tool -A $chain -m iprange --src-range ::1-::2 -j ACCEPT; then
	IPRANGE_MATCH=Yes
	if [ -z "${KLUDGEFREE}" ]; then
	    qt $g_tool -A $chain -m iprange --src-range ::1-::2 -m iprange --dst-range ::1-::2 -j ACCEPT && KLUDGEFREE=Yes
	fi
    fi

    if qt $g_tool -A $chain -m recent --update -j ACCEPT; then
	RECENT_MATCH=Yes
	qt $g_tool -A $chain -m recent --rcheck --seconds 10 --reap && REAP_OPTION=Yes
    fi

    qt $g_tool -A $chain -m owner --uid-owner 0 -j ACCEPT && OWNER_MATCH=Yes

    local name
    name=$(id -un 2> /dev/null)

    [ -n "$name" ] && qt $g_tool -A $chain -m owner --uid-owner $name -j ACCEPT && OWNER_NAME_MATCH=Yes

    if qt $g_tool -A $chain -m connmark --mark 2  -j ACCEPT; then
	CONNMARK_MATCH=Yes
	qt $g_tool -A $chain -m connmark --mark 2/0xFF -j ACCEPT && XCONNMARK_MATCH=Yes
    fi

    qt $g_tool -A $chain -p tcp -m ipp2p --edk -j ACCEPT       && IPP2P_MATCH=Yes
    if [ -n "$IPP2P_MATCH" ]; then
	qt $g_tool -A $chain -p tcp -m ipp2p --ipp2p -j ACCEPT && OLD_IPP2P_MATCH=Yes
    fi

    qt $g_tool -A $chain -m length --length 10:20 -j ACCEPT    && LENGTH_MATCH=Yes

    if [ $g_family -eq 4 ]; then
        qt $g_tool -A $chain -j REJECT --reject-with icmp-host-prohibited && ENHANCED_REJECT=Yes
    else
	qt $g_tool -A $chain -j REJECT --reject-with icmp6-adm-prohibited && ENHANCED_REJECT=Yes
    fi

    qt $g_tool -A $chain -j ACCEPT -m comment --comment "This is a comment" && COMMENTS=Yes

    if [ -n "$NFACCT" -a ! -x "$NFACCT" ]; then
	error_message "WARNING: NFACCT=$NFACCT does not exist or is not executable"
	NFACCT=
    else
	NFACCT=$(mywhich nfacct)
    fi

    if [ -n "$NFACCT" ] && qt $NFACCT add $chain; then
	qt $g_tool -A $chain -m nfacct --nfacct-name $chain && NFACCT_MATCH=Yes
	qt $g_tool -D $chain -m nfacct --nfacct-name $chain
	qt $NFACCT del $chain
    fi

    qt $g_tool -A $chain -p tcp -j TARPIT && TARPIT_TARGET=Yes

    qt $g_tool -A $chain -m iface --iface lo --loopback && IFACE_MATCH=Yes

    qt $g_tool -A $chain -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu && TCPMSS_TARGET=Yes

    if [ -n "$MANGLE_ENABLED" ]; then
	qt $g_tool -t mangle -N $chain

	if qt $g_tool -t mangle -A $chain -j MARK --set-mark 1; then
	    MARK=Yes
	    qt $g_tool -t mangle -A $chain -j MARK --and-mark 0xFF && XMARK=Yes
	    qt $g_tool -t mangle -A $chain -j MARK --set-mark 1/0xFF && EXMARK=Yes
	fi

	if qt $g_tool -t mangle -A $chain -j CONNMARK --save-mark; then
	    CONNMARK=Yes
	    qt $g_tool -t mangle -A $chain -j CONNMARK --save-mark --mask 0xFF && XCONNMARK=Yes
	fi

	qt $g_tool -t mangle -A $chain -j CLASSIFY --set-class 1:1 && CLASSIFY_TARGET=Yes
	qt $g_tool -t mangle -A $chain -j IPMARK --addr src && IPMARK_TARGET=Yes
	qt $g_tool -t mangle -A $chain -p tcp -j TPROXY --on-port 0 --tproxy-mark 1 && TPROXY_TARGET=Yes
	qt $g_tool -t mangle -A $chain -j IMQ --todev 0 && IMQ_TARGET=Yes
	qt $g_tool -t mangle -A $chain -m dscp --dscp 0 && DSCP_MATCH=Yes
	qt $g_tool -t mangle -A $chain -j DSCP --set-dscp 0 && DSCP_TARGET=Yes
	qt $g_tool -t mangle -A $chain -m rpfilter && RPFILTER_MATCH=Yes
	qt $g_tool -t mangle -A $chain -j CHECKSUM --checksum-fill && CHECKSUM_TARGET=Yes
	qt $g_tool -t mangle -A $chain -m tos --tos 0x10/0xff && NEW_TOS_MATCH=Yes

	qt $g_tool -t mangle -F $chain
	qt $g_tool -t mangle -X $chain

	qt $g_tool -t mangle -L FORWARD -n && MANGLE_FORWARD=Yes
    fi

    qt $g_tool -t raw	  -L -n && RAW_TABLE=Yes

    if [ -n "$RAW_TABLE" ]; then
	qt $g_tool -t raw -F $chain
	qt $g_tool -t raw -X $chain
	qt $g_tool -t raw -N $chain
	
	if qt $g_tool -t raw -A $chain -j CT --notrack; then
	    CT_TARGET=Yes;

	    for helper in amanda ftp ftp0 h323 irc irc0 netbios_ns pptp sane sane0 sip sip0 snmp tftp tftp0; do
		eval ${helper}_ENABLED=''
	    done

	    if [ -n "$HELPERS" ]; then
		for helper in $(split_list "$HELPERS"); do
		    case $helper in
			none)
			    ;;
			amanda|ftp|ftp0|h323|irc|irc0|netbios_ns|pptp|sane|sane0|sip|sip0|snmp|tftp|tftp0)
			    eval ${helper}_ENABLED=Yes
			    ;;
			*)
			    error_message "WARNING: Invalid helper ($helper) ignored"
			    ;;
		    esac
		done
	    else
		for helper in amanda ftp ftp0 h323 irc irc0 netbios_ns pptp sane sane0 sip sip0 snmp tftp tftp0; do
		    eval ${helper}_ENABLED=Yes
		done
	    fi

	    [ -n "$amanda_ENABLED" ]     && qt $g_tool -t raw -A $chain -p udp --dport 10080 -j CT --helper amanda     && AMANDA_HELPER=Yes
	    [ -n "$ftp_ENABLED" ]        && qt $g_tool -t raw -A $chain -p tcp --dport 21    -j CT --helper ftp        && FTP_HELPER=Yes
	    [ -n "$ftp0_ENABLED" ]       && qt $g_tool -t raw -A $chain -p tcp --dport 21    -j CT --helper ftp-0      && FTP0_HELPER=Yes
	    [ -n "$h323_ENABLED" ]       && qt $g_tool -t raw -A $chain -p udp --dport 1719  -j CT --helper RAS        && H323_HELPER=Yes
	    [ -n "$irc_ENABLED" ]        && qt $g_tool -t raw -A $chain -p tcp --dport 6667  -j CT --helper irc        && IRC_HELPER=Yes
	    [ -n "$irc0_ENABLED" ]       && qt $g_tool -t raw -A $chain -p tcp --dport 6667  -j CT --helper irc-0      && IRC0_HELPER=Yes
	    [ -n "$netbios_ns_ENABLED" ] && qt $g_tool -t raw -A $chain -p udp --dport 137   -j CT --helper netbios-ns && NETBIOS_NS_HELPER=Yes
	    [ -n "$pptp_ENABLED" ]       && qt $g_tool -t raw -A $chain -p tcp --dport 1729  -j CT --helper pptp       && PPTP_HELPER=Yes
	    [ -n "$sane_ENABLED" ]       && qt $g_tool -t raw -A $chain -p tcp --dport 6566  -j CT --helper sane       && SANE_HELPER=Yes
	    [ -n "$sane0_ENABLED" ]      && qt $g_tool -t raw -A $chain -p tcp --dport 6566  -j CT --helper sane-0     && SANE0_HELPER=Yes
	    [ -n "$sip_ENABLED" ]        && qt $g_tool -t raw -A $chain -p udp --dport 5060  -j CT --helper sip        && SIP_HELPER=Yes
	    [ -n "$sip0_ENABLED" ]       && qt $g_tool -t raw -A $chain -p udp --dport 5060  -j CT --helper sip-0      && SIP0_HELPER=Yes
	    [ -n "$snmp_ENABLED" ]       && qt $g_tool -t raw -A $chain -p udp --dport 161   -j CT --helper snmp       && SNMP_HELPER=Yes
	    [ -n "$tftp_ENABLED" ]       && qt $g_tool -t raw -A $chain -p udp --dport 69    -j CT --helper tftp       && TFTP_HELPER=Yes
	    [ -n "$tftp0_ENABLED" ]      && qt $g_tool -t raw -A $chain -p udp --dport 69    -j CT --helper tftp-0     && TFTP0_HELPER=Yes
	fi

	qt $g_tool -t raw -F $chain
	qt $g_tool -t raw -X $chain	   
    fi

    if qt mywhich ipset; then
	qt ipset -X $chain # Just in case something went wrong the last time

	local have_ipset

	if [ $g_family -eq 4 ]; then
	    if qt ipset -N $chain hash:ip family inet; then
		IPSET_V5=Yes
		have_ipset=Yes
	    elif qt ipset -N $chain iphash ; then
		have_ipset=Yes
	    fi

	    if [ -n "$have_ipset" ]; then
		if qt $g_tool -A $chain -m set --match-set $chain src -j ACCEPT; then
		    qt $g_tool -A $chain -m set --match-set $chain src --return-nomatch  -j ACCEPT && IPSET_MATCH_NOMATCH=Yes
		    qt $g_tool -A $chain -m set --match-set $chain src --packets-lt 100  -j ACCEPT && IPSET_MATCH_COUNTERS=Yes
		    qt $g_tool -F $chain
		    IPSET_MATCH=Yes
		elif qt $g_tool -A $chain -m set --set $chain src -j ACCEPT; then
		    qt $g_tool -F $chain
		    IPSET_MATCH=Yes
		    OLD_IPSET_MATCH=Yes
		fi
		qt ipset -X $chain
	    fi
	elif qt ipset -N $chain hash:ip family inet6; then
	    IPSET_V5=Yes
	    if qt $g_tool -A $chain -m set --match-set $chain src -j ACCEPT; then
		qt $g_tool -F $chain
		IPSET_MATCH=Yes
	    elif qt $g_tool -A $chain -m set --set $chain src -j ACCEPT; then
		qt $g_tool -F $chain
		IPSET_MATCH=Yes
		OLD_IPSET_MATCH=Yes
	    fi
	    qt ipset -X $chain
	fi
    fi

    qt $g_tool -A $chain -m pkttype --pkt-type broadcast -j ACCEPT && USEPKTTYPE=Yes
    qt $g_tool -A $chain -m addrtype --src-type BROADCAST -j ACCEPT && ADDRTYPE=Yes
    qt $g_tool -A $chain -p tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1000:1500 -j ACCEPT && TCPMSS_MATCH=Yes
    qt $g_tool -A $chain -m hashlimit --hashlimit-upto 4 --hashlimit-burst 5 --hashlimit-name $chain --hashlimit-mode dstip -j ACCEPT && HASHLIMIT_MATCH=Yes
    if [ -z "$HASHLIMIT_MATCH" ]; then
	qt $g_tool -A $chain -m hashlimit --hashlimit 4 --hashlimit-burst 5 --hashlimit-name $chain --hashlimit-mode dstip -j ACCEPT && OLD_HL_MATCH=Yes
	HASHLIMIT_MATCH=$OLD_HL_MATCH
    fi

    if qt $g_tool -A $chain -j NFQUEUE --queue-num 4; then
	NFQUEUE_TARGET=Yes
	qt $g_tool -A $chain -j NFQUEUE --queue-balance 0:3 --queue-cpu-fanout && CPU_FANOUT=Yes
    fi

    qt $g_tool -A $chain -m realm --realm 4 && REALM_MATCH=Yes

    #
    # -m helper doesn't verify the existence of the specified helper :-(
    #
    if qt $g_tool -A $chain -p tcp --dport 21 -m helper --helper ftp; then
	HELPER_MATCH=Yes

	if [ -z "$CT_TARGET" ]; then
	    AMANDA_HELPER=Yes
	    FTP_HELPER=Yes
	    FTP_HELPER=Yes
	    H323_HELPER=Yes
	    IRC_HELPER=Yes
	    NS_HELPER=Yes
	    PPTP_HELPER=Yes
	    SANE_HELPER=Yes
	    SIP_HELPER=Yes
	    SNMP_HELPER=Yes
	    TFTP_HELPER=Yes
	fi
    fi

    qt $g_tool -A $chain -m connlimit --connlimit-above 8 -j DROP && CONNLIMIT_MATCH=Yes
    qt $g_tool -A $chain -m time --timestart 23:00 -j DROP && TIME_MATCH=Yes
    qt $g_tool -A $chain -g $chain1 && GOTO_TARGET=Yes
    qt $g_tool -A $chain -j LOGMARK && LOGMARK_TARGET=Yes
    qt $g_tool -A $chain -j LOG && LOG_TARGET=Yes
    qt $g_tool -A $chain -j ULOG && ULOG_TARGET=Yes
    qt $g_tool -A $chain -j MARK --set-mark 5 && MARK_ANYWHERE=Yes
    qt $g_tool -A $chain -m statistic --mode nth --every 2 --packet 1 && STATISTIC_MATCH=Yes
    qt $g_tool -A $chain -m geoip --src-cc US && GEOIP_MATCH=Yes
    if qt $g_tool -A $chain -j NFLOG; then
	NFLOG_TARGET=Yes
	qt $g_tool -A $chain -j NFLOG --nflog-size 64 && NFLOG_SIZE=Yes
    fi

    if [ $g_family -eq 4 ]; then
	qt $g_tool -A $chain -j ACCOUNT --addr 192.168.1.0/29 --tname $chain && ACCOUNT_TARGET=Yes
    else
	qt $g_tool -A $chain -m ipv6header --header 255 && HEADER_MATCH=Yes
	qt $g_tool -A $chain -j ACCOUNT --addr ::1/122 --tname $chain && ACCOUNT_TARGET=Yes
    fi

    qt $g_tool -A $chain -j AUDIT --type drop && AUDIT_TARGET=Yes

    qt $g_tool -A $chain -m condition --condition foo && CONDITION_MATCH=Yes

    qt $g_tool -S INPUT && IPTABLES_S=Yes
    qt $g_tool -F $chain
    qt $g_tool -X $chain
    qt $g_tool -F $chain1
    qt $g_tool -X $chain1

    if [ -n "$TC" ]; then
	$TC filter add flow help 2>&1 | grep -q ^Usage && FLOW_FILTER=Yes

	if $TC filter add basic help 2>&1 | grep -q ^Usage; then
	    BASIC_FILTER=Yes
	    $TC filter add basic help 2>&1 | egrep -q match && BASIC_EMATCH=Yes
	fi
    fi

    [ -n "$IP" ] && $IP rule add help 2>&1 | grep -q /MASK && FWMARK_RT_MASK=Yes

    CAPVERSION=$SHOREWALL_CAPVERSION

    KERNELVERSION=$(uname -r 2> /dev/null | sed -e 's/-.*//')

    case "$KERNELVERSION" in
	*.*.*)
	    KERNELVERSION=$(printf "%d%02d%02d" $(echo $KERNELVERSION | sed -e 's/^\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\).*$/\1 \2 \3/g'))
	    ;;
	*)
	    KERNELVERSION=$(printf "%d%02d00" $(echo $KERNELVERSION | sed -e 's/^\([0-9][0-9]*\)\.\([0-9][0-9]*\).*$/\1 \2/g'))
	    ;;
    esac
}

report_capabilities_unsorted() {
    report_capability() # $1 = Capability Description , $2 Capability Setting (if any)
    {
	local setting
	setting=

	[ "x$2" = "xYes" ] && setting="Available" || setting="Not available"

	echo "  " $1: $setting
    }

    report_capability "NAT (NAT_ENABLED)" $NAT_ENABLED
    report_capability "Packet Mangling (MANGLE_ENABLED)" $MANGLE_ENABLED
    report_capability "Multi-port Match (MULTIPORT)" $MULTIPORT
    [ -n "$MULTIPORT" ]  && report_capability "Extended Multi-port Match (XMULIPORT)" $XMULTIPORT
    [ -n "$EMULTIPORT" ] && report_capability "Enhanced Multi-port Match (EMULIPORT)" $EMULTIPORT
    report_capability "Connection Tracking Match (CONNTRACK_MATCH)" $CONNTRACK_MATCH
    if [ -n "$CONNTRACK_MATCH" ]; then
	report_capability "Extended Connection Tracking Match Support (NEW_CONNTRACK_MATCH)" $NEW_CONNTRACK_MATCH
	[ -n "$OLD_CONNTRACK_MATCH" ] && report_capability "Old Connection Tracking Match Syntax (OLD_CONNTRACK_MATCH)" $OLD_CONNTRACK_MATCH
    fi
    report_capability "Packet Type Match (USEPKTTYPE)" $USEPKTTYPE
    report_capability "Policy Match (POLICY_MATCH)" $POLICY_MATCH
    report_capability "Physdev Match (PHYSDEV_MATCH)" $PHYSDEV_MATCH
    report_capability "Physdev-is-bridged Support (PHYSDEV_BRIDGE)" $PHYSDEV_BRIDGE
    report_capability "Packet length Match (LENGTH_MATCH)" $LENGTH_MATCH
    report_capability "IP range Match(IPRANGE_MATCH)" $IPRANGE_MATCH
    report_capability "Recent Match (RECENT_MATCH)" $RECENT_MATCH
    [ -n "$RECENT_MATCH" ] && report_capability 'Recent Match "--reap" option (REAP_OPTION)' $REAP_OPTION
    report_capability "Owner Match (OWNER_MATCH)" $OWNER_MATCH
    report_capability "Owner Name Match (OWNER_NAME_MATCH)" $OWNER_NAME_MATCH
    report_capability "Ipset Match (IPSET_MATCH)" $IPSET_MATCH
    if [ -n "$IPSET_MATCH" ]; then
	[ -n "$OLD_IPSET_MATCH" ]     && report_capability "OLD_Ipset Match (OLD_IPSET_MATCH)"           $OLD_IPSET_MATCH
	[ -n "$IPSET_MATCH_NOMATCH" ] && report_capability "Ipset Match Nomatch (IPSET_MATCH_NOMATCH)"   $IPSET_MATCH_NOMATCH
	[ -n "$IPSET_MATCH_NOMATCH" ] && report_capability "Ipset Match Counters (IPSET_MATCH_COUNTERS)" $IPSET_MATCH_COUNTERS
    fi
    report_capability "CONNMARK Target (CONNMARK)" $CONNMARK
    [ -n "$CONNMARK" ] && report_capability "Extended CONNMARK Target (XCONNMARK)" $XCONNMARK
    report_capability "Connmark Match (CONNMARK_MATCH)" $CONNMARK_MATCH
    [ -n "$CONNMARK_MATCH" ] && report_capability "Extended Connmark Match (XCONNMARK_MATCH)" $XCONNMARK_MATCH
    report_capability "Raw Table (RAW_TABLE)" $RAW_TABLE
    report_capability "IPP2P Match (IPP2P_MATCH)" $IPP2P_MATCH
    [ -n "$OLD_IPP2P_MATCH" ] && report_capability "Old IPP2P Match Syntax (OLD_IPP2P_MATCH)" $OLD_IPP2P_MATCH
    report_capability "CLASSIFY Target (CLASSIFY_TARGET)" $CLASSIFY_TARGET
    report_capability "Extended REJECT (ENHANCED_REJECT)" $ENHANCED_REJECT
    report_capability "Repeat match (KLUDGEFREE)" $KLUDGEFREE
    report_capability "MARK Target (MARK)" $MARK
    [ -n "$MARK" ] && report_capability "Extended MARK Target (XMARK)" $XMARK
    [ -n "$XMARK" ] && report_capability "Extended MARK Target 2 (EXMARK)" $EXMARK
    report_capability "Mangle FORWARD Chain (MANGLE_FORWARD)" $MANGLE_FORWARD
    report_capability "Comments (COMMENTS)" $COMMENTS
    report_capability "Address Type Match (ADDRTYPE)" $ADDRTYPE
    report_capability "TCPMSS Match (TCPMSS_MATCH)" $TCPMSS_MATCH
    report_capability "Hashlimit Match (HASHLIMIT_MATCH)" $HASHLIMIT_MATCH
    [ -n "$OLD_HL_MATCH" ] && report_capability "Old Hashlimit Match (OLD_HL_MATCH)" $OLD_HL_MATCH
    report_capability "NFQUEUE Target (NFQUEUE_TARGET)" $NFQUEUE_TARGET
    report_capability "Realm Match (REALM_MATCH)" $REALM_MATCH
    report_capability "Helper Match (HELPER_MATCH)" $HELPER_MATCH
    report_capability "Connlimit Match (CONNLIMIT_MATCH)" $CONNLIMIT_MATCH
    report_capability "Time Match (TIME_MATCH)" $TIME_MATCH
    report_capability "Goto Support (GOTO_TARGET)" $GOTO_TARGET
    report_capability "LOGMARK Target (LOGMARK_TARGET)" $LOGMARK_TARGET
    report_capability "IPMARK Target (IPMARK_TARGET)" $IPMARK_TARGET
    report_capability "LOG Target (LOG_TARGET)" $LOG_TARGET
    report_capability "ULOG Target (ULOG_TARGET)" $ULOG_TARGET
    report_capability "NFLOG Target (NFLOG_TARGET)" $NFLOG_TARGET
    report_capability "Persistent SNAT (PERSISTENT_SNAT)" $PERSISTENT_SNAT
    report_capability "TPROXY Target (TPROXY_TARGET)" $TPROXY_TARGET
    report_capability "FLOW Classifier (FLOW_FILTER)" $FLOW_FILTER
    report_capability "fwmark route mask (FWMARK_RT_MASK)" $FWMARK_RT_MASK
    report_capability "Mark in the filter table (MARK_ANYWHERE)" $MARK_ANYWHERE
    report_capability "Header Match (HEADER_MATCH)" $HEADER_MATCH
    report_capability "ACCOUNT Target (ACCOUNT_TARGET)" $ACCOUNT_TARGET
    report_capability "AUDIT Target (AUDIT_TARGET)" $AUDIT_TARGET
    report_capability "ipset V5 (IPSET_V5)" $IPSET_V5
    report_capability "Condition Match (CONDITION_MATCH)" $CONDITION_MATCH
    report_capability "Statistic Match (STATISTIC_MATCH)" $STATISTIC_MATCH
    report_capability "IMQ Target (IMQ_TARGET)" $IMQ_TARGET
    report_capability "DSCP Match (DSCP_MATCH)" $DSCP_MATCH
    report_capability "DSCP Target (DSCP_TARGET)" $DSCP_TARGET
    report_capability "Geo IP Match (GEOIP_MATCH)" $GEOIP_MATCH
    report_capability "RPFilter Match (RPFILTER_MATCH)" $RPFILTER_MATCH
    report_capability "NFAcct Match" $NFACCT_MATCH
    report_capability "Checksum Target (CHECKSUM_TARGET)" $CHECKSUM_TARGET
    report_capability "Arptables JF (ARPTABLESJF)" $ARPTABLESJF
    report_capability "MASQUERADE Target (MASQUERADE_TGT)" $MASQUERADE_TGT
    report_capability "UDPLITE Port Redirection (UDPLITEREDIRECT)" $UDPLITEREDIRECT
    report_capability "New tos Match (NEW_TOS_MATCH)" $NEW_TOS_MATCH
    report_capability "TARPIT Target (TARPIT_TARGET)" $TARPIT_TARGET
    report_capability "Iface Match (IFACE_MATCH)" $IFACE_MATCH
    report_capability "TCPMSS Target (TCPMSS_TARGET)" $TCPMSS_TARGET

    report_capability "Amanda Helper" $AMANDA_HELPER
    report_capability "FTP Helper" $FTP_HELPER
    report_capability "FTP-0 Helper" $FTP0_HELPER
    report_capability "IRC Helper" $IRC_HELPER
    report_capability "IRC-0 Helper" $IRC0_HELPER
    report_capability "Netbios_ns Helper" $NETBIOS_NS_HELPER
    report_capability "H323 Helper" $H323_HELPER
    report_capability "PPTP Helper" $PPTP_HELPER
    report_capability "SANE Helper" $SANE_HELPER
    report_capability "SANE-0 Helper" $SANE0_HELPER
    report_capability "SIP Helper" $SIP_HELPER
    report_capability "SIP-0 Helper" $SIP0_HELPER
    report_capability "SNMP Helper" $SNMP_HELPER
    report_capability "TFTP Helper" $TFTP_HELPER
    report_capability "TFTP-0 Helper" $TFTP0_HELPER

    if [ $g_family -eq 4 ]; then
	report_capability "iptables -S (IPTABLES_S)" $IPTABLES_S
	report_capability "iptables --wait option (WAIT_OPTION)" $WAIT_OPTION
	report_capability "iptables-restore --wait option (RESTORE_WAIT_OPTION)" $RESTORE_WAIT_OPTION
    else
	report_capability "ip6tables -S (IPTABLES_S)" $IPTABLES_S
	report_capability "ip6tables --wait option (WAIT_OPTION)" $WAIT_OPTION
	report_capability "ip6tables-restore --wait option (RESTORE_WAIT_OPTION)" $RESTORE_WAIT_OPTION
    fi

    report_capability "Basic Filter (BASIC_FILTER)" $BASIC_FILTER
    report_capability "Basic Ematch (BASIC_EMATCH)" $BASIC_EMATCH
    report_capability "CT Target (CT_TARGET)" $CT_TARGET
    report_capability "NFQUEUE CPU Fanout (CPU_FANOUT)" $CPU_FANOUT
    report_capability "NETMAP Target (NETMAP_TARGET)" $NETMAP_TARGET
    report_capability "--nflog-size support (NFLOG_SIZE)" $NFLOG_SIZE
    report_capability "INPUT chain in nat table (NAT_INPUT_CHAIN)" $NAT_INPUT_CHAIN

    echo "   Kernel Version (KERNELVERSION): $KERNELVERSION"
    echo "   Capabilities Version (CAPVERSION): $CAPVERSION"
}

report_capabilities() {

    if [ $VERBOSITY -gt 1 ]; then
	echo "$g_product has detected the following iptables/netfilter capabilities:"
	report_capabilities_unsorted | sort
    fi

    [ -n "$PKTTYPE" ] || USEPKTTYPE=

}

report_capabilities_unsorted1() {
    report_capability1() # $1 = Capability
    {
	eval echo $1=\$$1
    }

    report_capability1 NAT_ENABLED
    report_capability1 MANGLE_ENABLED
    report_capability1 MULTIPORT
    report_capability1 XMULTIPORT
    report_capability1 EMULTIPORT
    report_capability1 CONNTRACK_MATCH
    report_capability1 NEW_CONNTRACK_MATCH
    report_capability1 OLD_CONNTRACK_MATCH
    report_capability1 USEPKTTYPE
    report_capability1 POLICY_MATCH
    report_capability1 PHYSDEV_MATCH
    report_capability1 PHYSDEV_BRIDGE
    report_capability1 LENGTH_MATCH
    report_capability1 IPRANGE_MATCH
    report_capability1 RECENT_MATCH
    report_capability1 REAP_OPTION
    report_capability1 OWNER_MATCH
    report_capability1 OWNER_NAME_MATCH
    report_capability1 IPSET_MATCH
    report_capability1 OLD_IPSET_MATCH
    report_capability1 IPSET_MATCH_NOMATCH
    report_capability1 IPSET_MATCH_COUNTERS
    report_capability1 CONNMARK
    report_capability1 XCONNMARK
    report_capability1 CONNMARK_MATCH
    report_capability1 XCONNMARK_MATCH
    report_capability1 RAW_TABLE
    report_capability1 IPP2P_MATCH
    report_capability1 OLD_IPP2P_MATCH
    report_capability1 CLASSIFY_TARGET
    report_capability1 ENHANCED_REJECT
    report_capability1 KLUDGEFREE
    report_capability1 MARK
    report_capability1 XMARK
    report_capability1 EXMARK
    report_capability1 MANGLE_FORWARD
    report_capability1 COMMENTS
    report_capability1 ADDRTYPE
    report_capability1 TCPMSS_MATCH
    report_capability1 HASHLIMIT_MATCH
    report_capability1 OLD_HL_MATCH
    report_capability1 NFQUEUE_TARGET
    report_capability1 REALM_MATCH
    report_capability1 HELPER_MATCH
    report_capability1 CONNLIMIT_MATCH
    report_capability1 TIME_MATCH
    report_capability1 GOTO_TARGET
    report_capability1 LOGMARK_TARGET
    report_capability1 IPMARK_TARGET
    report_capability1 LOG_TARGET
    report_capability1 ULOG_TARGET
    report_capability1 NFLOG_TARGET
    report_capability1 PERSISTENT_SNAT
    report_capability1 TPROXY_TARGET
    report_capability1 FLOW_FILTER
    report_capability1 FWMARK_RT_MASK
    report_capability1 MARK_ANYWHERE
    report_capability1 HEADER_MATCH
    report_capability1 ACCOUNT_TARGET
    report_capability1 AUDIT_TARGET
    report_capability1 IPSET_V5
    report_capability1 CONDITION_MATCH
    report_capability1 IPTABLES_S
    report_capability1 BASIC_FILTER
    report_capability1 BASIC_EMATCH
    report_capability1 CT_TARGET
    report_capability1 STATISTIC_MATCH
    report_capability1 IMQ_TARGET
    report_capability1 DSCP_MATCH
    report_capability1 DSCP_TARGET
    report_capability1 GEOIP_MATCH
    report_capability1 RPFILTER_MATCH
    report_capability1 NFACCT_MATCH
    report_capability1 CHECKSUM_TARGET
    report_capability1 ARPTABLESJF
    report_capability1 MASQUERADE_TGT
    report_capability1 UDPLITEREDIRECT
    report_capability1 NEW_TOS_MATCH
    report_capability1 TARPIT_TARGET
    report_capability1 IFACE_MATCH
    report_capability1 TCPMSS_TARGET
    report_capability1 WAIT_OPTION
    report_capability1 CPU_FANOUT
    report_capability1 NETMAP_TARGET
    report_capability1 NFLOG_SIZE
    report_capability1 RESTORE_WAIT_OPTION
    report_capability1 NAT_INPUT_CHAIN

    report_capability1 AMANDA_HELPER
    report_capability1 FTP_HELPER
    report_capability1 FTP0_HELPER
    report_capability1 IRC_HELPER
    report_capability1 IRC0_HELPER
    report_capability1 NETBIOS_NS_HELPER
    report_capability1 H323_HELPER
    report_capability1 PPTP_HELPER
    report_capability1 SANE_HELPER
    report_capability1 SANE0_HELPER
    report_capability1 SIP_HELPER
    report_capability1 SIP0_HELPER
    report_capability1 SNMP_HELPER
    report_capability1 TFTP_HELPER
    report_capability1 TFTP0_HELPER

    echo CAPVERSION=$SHOREWALL_CAPVERSION
    echo KERNELVERSION=$KERNELVERSION
}

report_capabilities1() {
    echo "#"
    echo "# $g_product $SHOREWALL_VERSION detected the following iptables/netfilter capabilities - $(date)"
    echo "#"
    report_capabilities_unsorted1 | sort
}

interface_status() {
    case $(cat $1) in
	0)
	    echo Enabled
	    ;;
	1)
	    echo Disabled
	    ;;
	*)
	    echo Unknown
	    ;;
    esac
}

show_interfaces() {
    local f
    local interface
    local printed

    for f in ${VARDIR}/*.status; do
	if [ -f $f ]; then
	    interface=$(basename $f)
	    echo "   Interface ${interface%.status} is $(interface_status $f)"
	    printed=Yes
	fi
    done

    [ -n "$printed" ] && echo
}

status_command() {
     local finished
    finished=0
    local option
    local interfaces

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			i*)
			    interfaces=Yes
			    option=${option#i}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    [ $# -eq 0 ] || missing_argument

    [ $VERBOSITY -ge 1 ] && echo "${g_product}-$SHOREWALL_VERSION Status at $g_hostname - $(date)" && echo
    show_status
    [ -n "$interfaces" ] && show_interfaces
    exit $status
}

drop_command() {
    if product_is_started ; then
	if ! chain_exists dynamic; then
	    echo "Dynamic blacklisting is not supported in the current $g_product configuration"
	    exit 2
	fi

	[ -n "$g_nolock" ] || mutex_on
	block DROP Dropped $*
	[ -n "$g_nolock" ] || mutex_off
    else
	fatal_error "$g_product is not started"
    fi
}

logdrop_command() {
    if product_is_started ; then
	if ! chain_exists dynamic; then
	    echo "Dynamic blacklisting is not supported in the current $g_product configuration"
	    exit 2
	fi

	[ -n "$g_nolock" ] || mutex_on
	block logdrop Dropped $*
	[ -n "$g_nolock" ] || mutex_off
    else
	fatal_error "$g_product is not started"
    fi
}

reject_command() {
    if product_is_started ; then
	[ -n "$g_nolock" ] || mutex_on
	block $1 Rejected $*
	[ -n "$g_nolock" ] || mutex_off
    else
	fatal_error "$g_product is not started"
    fi
}

blacklist_command() {
    local family

    [ $# -gt 0 ] || fatal_error "Missing address"

    [ -z "$g_blacklistipset" ] && fatal_error "The blacklist command is not supported in the current $g_product configuration"

    case ${IPSET:=ipset} in
	*/*)
	    if [ ! -x "$IPSET" ]; then
		fatal_error "IPSET=$IPSET does not exist or is not executable"
	    fi
	    ;;
	*)
	    IPSET="$(mywhich $IPSET)"
	    [ -n "$IPSET" ] || fatal_error "The ipset utility cannot be located"
	    ;;
    esac

    if $IPSET -A $g_blacklistipset $@ -exist; then
	local message

	progress_message2 "$1 Blacklisted"

	if [ -n "$g_disconnect" ]; then
	    message="$(conntrack -D -s $1 2>&1)"
	    if [ -n "$message" -a $VERBOSITY -gt 0 ]; then
		if [ $VERBOSITY -gt 1 ]; then
		    echo "$message" | awk '/have been deleted/ { sub( /^.*: /, "" ); sub( / /,  " src " ); }; { print; }'
		else
		    echo "$message" | head -n1 | sed 's/^.*: //; s/ / src /'
		fi
	    fi

	    if [ $g_disconnect = src-dst ]; then
		message="$(conntrack -D -d $1 2>&1)"
		if [ -n "$message" -a $VERBOSITY -gt 0 ]; then
		    if [ $VERBOSITY -gt 1 ]; then
			echo "$message" | awk '/have been deleted/ { sub( /^.*: /, "" ); sub( / /,  " dst " ); }; { print; }'
		    else
			echo "$message" | head -n1 | sed 's/^.*: //; s/ / dst /'
		    fi
		fi
	    fi
	fi
    else
	error_message "ERROR: Address $1 not blacklisted"
	return 1
    fi

    return 0
}

save_command() {
    local finished
    finished=0

    shift

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			C*)
			    g_counters=Yes
			    option=${option#C}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    case $# in
	0)
	    ;;
	1)
	    RESTOREFILE="$1"
	    validate_restorefile '<restore file>'
	    ;;
	*)
	    too_many_arguments $2
	    ;;
	esac

    g_restorepath=${VARDIR}/$RESTOREFILE

    [ -n "$g_nolock" ] || mutex_on

    save_config

    result=$?

    [ -n "$g_nolock" ] || mutex_off

    exit $result
}

forget_command() {
    case $# in
	0)
	    missing_argument
	    ;;
	1)
	    ;;
	2)
	    RESTOREFILE="$2"
	    validate_restorefile '<restore file>'
	    ;;
	*)
	    too_many_arguments $3
	    ;;
    esac

    g_restorepath=${VARDIR}/$RESTOREFILE

    if [ -x $g_restorepath ]; then
	rm -f $g_restorepath
	rm -f ${g_restorepath}-iptables
	rm -f ${g_restorepath}-ipsets
	rm -f ${g_restorepath}-arptables
	echo "    $g_restorepath removed"
    elif [ -f $g_restorepath ]; then
	echo "   $g_restorepath exists and is not a saved $g_product configuration"
    fi
    rm -f ${VARDIR}/save
}

ipcalc_command() {
    local address
    local vlsm

    [ $g_family -eq 6 ] && fatal_error "$g_product does not support the ipcalc command"

    if [ $# -eq 2 ]; then
	address=${2%/*}
	vlsm=${2#*/}
    elif [ $# -eq 3 ]; then
	address=$2
	vlsm=$(ip_vlsm $3)
    elif [ $# -eq 0 ]; then
	missing_argument
    else
	too_many_arguments $4
    fi

    valid_address $address || fatal_error "Invalid IP address: $address"
    [ -z "$vlsm" ] && fatal_error "Missing VLSM"
    [ "x$address" = "x$vlsm" ] && fatal_error "Invalid VLSM"
    [ $vlsm -gt 32 ] && fatal_error "Invalid VLSM: /$vlsm"

    address=$address/$vlsm

    echo "   CIDR=$address"
    temp=$(ip_netmask $address);       echo "   NETMASK=$(encodeaddr $temp)"
    temp=$(ip_network $address);       echo "   NETWORK=$temp"
    temp=$(broadcastaddress $address); echo "   BROADCAST=$temp"
}

iprange_command() {
    local range

    [ $g_family -eq 6 ] && fatal_error "$g_product does not support the iprange command"

    range=''

    while [ $# -gt 0 ]; do
	shift
	range="${range}${1}"
    done

    case $range in
	*.*.*.*-*.*.*.*)
	    for address in ${range%-*} ${range#*-}; do
		valid_address $address || fatal_error "Invalid IP address: $address"
	    done

	    ip_range $range
	    ;;
	*)
	    fatal_error "Invalid ip range: $range"
	    ;;
    esac
}

ipdecimal_command() {
    if [ $# eq 1 ]; then
	missing_argument
    else
	[ $# -eq 2 ] || too_many_arguments $3
    fi

    [ $g_family -eq 6 ] && fatal_error "$g_product does not support the iprange command"

    case $2 in
	*.*.*.*)
	    valid_address $2 || fatal_error "Invalid IP address: $2"
	    echo "   $(decodeaddr $2)"
	    ;;
	*)
	    echo "   $(encodeaddr $2)"
	    ;;
    esac
}

iptrace_command() {
    if product_is_started ; then
	$g_tool -t raw -A PREROUTING $@ -j TRACE
	$g_tool -t raw -A OUTPUT $@ -j TRACE
    else
	fatal_error "$g_product is not started"
    fi
}

noiptrace_command() {
    if product_is_started ; then
	$g_tool -t raw -D PREROUTING $@ -j TRACE
	$g_tool -t raw -D OUTPUT $@ -j TRACE
    else
	fatal_error "$g_product is not started"
    fi
}

#
# Verify that we have a compiled firewall script
#
verify_firewall_script() {
    if [ ! -f $g_firewall ]; then
	echo "   ERROR: $g_product is not properly installed" >&2
	if [ -L $g_firewall ]; then
	    echo "          $g_firewall is a symbolic link to a" >&2
	    echo "          non-existant file" >&2
	else
	    echo "          The file $g_firewall does not exist" >&2
	fi

	exit 2
    fi
}

setup_dbl() {
    local original

    original=$DYNAMIC_BLACKLIST

    case $DYNAMIC_BLACKLIST in
	*:*,)
	    fatal_error "Invalid value ($original) for DYNAMIC_BLACKLIST"
	    ;;
	ipset*,disconnect*)
	    if qt mywhich conntrack; then
		g_disconnect=src
		DYNAMIC_BLACKLIST=$(echo $DYNAMIC_BLACKLIST | sed 's/,disconnect//')
	    else
		fatal_error "The 'disconnect' option requires that the conntrack utility be installed"
	    fi
	    ;;
    esac

    case $DYNAMIC_BLACKLIST in
	ipset*,src-dst*)
	    #
	    # This utility doesn't need to know about 'src-dst'
	    #
	    DYNAMIC_BLACKLIST=$(echo $DYNAMIC_BLACKLIST | sed 's/,src-dst//')

	    [ -n "$g_disconnect" ] && g_disconnect=src-dst
	    ;;
    esac

    case $DYNAMIC_BLACKLIST in
	ipset*,timeout*)
	    #
	    # This utility doesn't need to know about 'timeout=nnn'
	    #
	    DYNAMIC_BLACKLIST=$(echo $DYNAMIC_BLACKLIST | sed -r 's/,timeout=[[:digit:]]+//')
	    ;;
    esac

    case $DYNAMIC_BLACKLIST in
	[Nn]o)
	    DYNAMIC_BLACKLIST='';
	    ;;
	[Yy]es)
	    ;;
	ipset|ipset::*|ipset-only|ipset-only::*)
	    g_blacklistipset=SW_DBL$g_family
	    ;;
	ipset:[a-zA-Z]*)
	    g_blacklistipset=${DYNAMIC_BLACKLIST#ipset:}
	    g_blacklistipset=${g_blacklistipset%%:*}
	    ;;
	ipset-only:[a-zA-Z]*)
	    g_blacklistipset=${DYNAMIC_BLACKLIST#ipset-only:}
	    g_blacklistipset=${g_blacklistipset%%:*}
	    ;;
	*)
	    fatal_error "Invalid value ($original) for DYNAMIC_BLACKLIST"
	    ;;
    esac
}

################################################################################
# The remaining functions are used by the Lite cli - they are overloaded by
# the Standard CLI by loading lib.cli-std
################################################################################
#
# Set the configuration variables from shorewall[6]-lite.conf.
#
get_config() {
    local config
    local lib

    ensure_config_path

    config=$(find_file ${PRODUCT}.conf)

    if [ -f $config ]; then
	if [ -r $config ]; then
	    . $config
	else
	    fatal_error "Cannot read $config! (Hint: Are you root?)"
	fi
    else
	fatal_error "$config does not exist!"
    fi

    ensure_config_path

    [ -f ${VARDIR}/firewall.conf ] && . ${VARDIR}/firewall.conf

    [ -n "$PATH" ] || PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin

    setup_logread
    #
    # See if we have a real version of "tail" -- use separate redirection so
    # that ash (aka /bin/sh on LRP) doesn't crap
    #
    if ( tail -n5 /dev/null > /dev/null 2> /dev/null ) ; then
	realtail="Yes"
    else
	realtail=""
    fi

    [ -n "$FW" ] || FW=fw

    if [ $g_family -eq 4 ]; then
	if [ -n "$IPTABLES" ]; then
	    if [ ! -x "$IPTABLES" ]; then
		fatal_error "The program specified in IPTABLES does not exist or is not executable"
	    fi
	else
	    IPTABLES=$(mywhich iptables 2> /dev/null)
	    if [ -z "$IPTABLES" ] ; then
		fatal_error "Can't find iptables executable"
	    fi
	fi

	g_tool=$IPTABLES
    else
	if [ -n "$IP6TABLES" ]; then
	    if [ ! -x "$IP6TABLES" ]; then
		fatal_error "The program specified in IP6TABLES does not exist or is not executable"
	    fi
	else
	    IP6TABLES=$(mywhich ip6tables 2> /dev/null)
	    if [ -z "$IP6TABLES" ] ; then
		fatal_error "Can't find ip6tables executable"
	    fi
	fi

	g_tool=$IP6TABLES
    fi

    if [ -n "$SHOREWALL_SHELL" ]; then
	if [ ! -x "$SHOREWALL_SHELL" ]; then
	    echo "   WARNING: The program specified in SHOREWALL_SHELL does not exist or is not executable; falling back to /bin/sh" >&2
	    SHOREWALL_SHELL=/bin/sh
	fi
    fi

    [ -n "$RESTOREFILE" ] || RESTOREFILE=restore

    validate_restorefile RESTOREFILE

    [ -n "${VERBOSITY:=2}" ]

    [ -n "$g_use_verbosity" ] && VERBOSITY=$g_use_verbosity || VERBOSITY=$(($g_verbose_offset + $VERBOSITY))

    if [ $VERBOSITY -lt -1 ]; then
	VERBOSITY=-1
    elif [ $VERBOSITY -gt 2 ]; then
	VERBOSITY=2
    fi

    if qt mywhich hostname; then
	g_hostname=$(hostname 2> /dev/null)
    elif qt mywhich uname; then
	g_hostname=$(uname -n 2> /dev/null)
    else
	g_hostname=localhost
    fi

    if [ -n "$IPSET" ]; then
	case "$IPSET" in
	    */*)
		if [ ! -x "$IPSET" ] ; then
		    fatal_error "The program specified in IPSET ($IPSET) does not exist or is not executable"
		fi
		;;
	    *)
		prog="$(mywhich $IPSET 2> /dev/null)"
		if [ -z "$prog" ] ; then
		    fatal_error "Can't find $IPSET executable"
		fi
		IPSET=$prog
		;;
	esac
    else
	IPSET=''
    fi

    if [ -n "$WORKAROUNDS" ]; then
	case $WORKAROUNDS in
	    [Yy]es)
	        ;;
	    [Nn]o)
		WORKAROUNDS=''
		;;
	    *)
		fatal_error "Invalid setting ($WORKAROUNDS) for WORKAROUNDS"
		;;
	esac
    fi

    TC=tc

    IP=$(mywhich ip 2> /dev/null)

    g_loopback=$(find_loopback_interfaces)

    if [ -z "$g_nopager" ]; then
	[ -n "$PAGER" ] || PAGER=$DEFAULT_PAGER

	if [ -n "$PAGER" -a -t 1 ]; then
	    case $PAGER in
		/*)
		    g_pager="$PAGER"
		    [ -f "$g_pager" ] || fatal_error "PAGER=$PAGER does not exist"
		    ;;
		*)
		    g_pager=$(mywhich $PAGER 2> /dev/null)
		    [ -n "$g_pager" ] || fatal_error "PAGER=$PAGER does not exist"
		    ;;
	    esac

	    [ -x "$g_pager" ] || fatal_error "PAGER $g_pager is not executable"

	    g_pager="| $g_pager"
	fi
    fi

    if [ -n "$DYNAMIC_BLACKLIST" ]; then
	setup_dbl
    fi

    lib=$(find_file lib.cli-user)

    [ -f $lib ] && . $lib

}
#
# Start Command Executor
#
start_command() {
    local finished
    finished=0

    do_it() {
	local rc
	rc=0
	[ -n "$g_nolock" ] || mutex_on

	if [ -x ${VARDIR}/firewall ]; then
	    if [ -n "$g_fast" -a -x ${VARDIR}/${RESTOREFILE} -a ! ${VARDIR}/firewall -nt ${VARDIR}/${RESTOREFILE} ]; then
		run_it ${VARDIR}/${RESTOREFILE} $g_debugging restore
	    else
		run_it ${VARDIR}/firewall $g_debugging start
	    fi
	    rc=$?
	else
	    error_message "${VARDIR}/firewall is missing or is not executable"
	    mylogger kern.err "ERROR:$g_product start failed"
	    rc=6
	fi

	[ -n "$g_nolock" ] || mutex_off
	exit $rc
    }

    verify_firewall_script

    if product_is_started; then
	if [ $g_family -eq 4 ]; then
	    error_message "Shorewall is already running"
	else
	    error_message "Shorewall6 is already running"
	fi

	exit 0
    fi

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			f*)
			    g_fast=Yes
			    option=${option#f}
			    ;;
			C*)
			    g_counters=Yes
			    option=${option#C}
			    ;;
			p*)
			    [ -n "$(which conntrack)" ] || fatal_error "The '-p' option requires the conntrack utility which does not appear to be installed on this system"
			    g_purge=Yes
			    option=${option%p}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    case $# in
	0)
	    ;;
	*)
	    too_many_arguments $1
	    ;;
    esac

    do_it
}

#
# Reload/Restart Command Executor
#
restart_command() {
    local finished
    finished=0
    local rc
    rc=0

    verify_firewall_script

    while [ $finished -eq 0 -a $# -gt 0 ]; do
	option=$1
	case $option in
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			-)
			    finished=1
			    option=
			    ;;
			n*)
			    g_noroutes=Yes
			    option=${option#n}
			    ;;
			p*)
			    [ -n "$(mywhich conntrack)" ] || fatal_error "The '-p' option requires the conntrack utility which does not appear to be installed on this system"
			    g_purge=Yes
			    option=${option%p}
			    ;;
			C*)
			    g_counters=Yes
			    option=${option#C}
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    case $# in
	0)
	    ;;
	*)
	    too_many_arguments $1
	    ;;
    esac

    [ -n "$g_nolock" ] || mutex_on

    if [ -x ${VARDIR}/firewall ]; then
	run_it ${VARDIR}/firewall $g_debugging $COMMAND
	rc=$?
    else
	error_message "${VARDIR}/firewall is missing or is not executable"
	mylogger kern.err "ERROR:$g_product $COMMAND failed"
	rc=6
    fi

    [ -n "$g_nolock" ] || mutex_off
    return $rc
}

run_command() {
    if [ -x ${VARDIR}/firewall ] ; then
	run_it ${VARDIR}/firewall $g_debugging $@
    else
	fatal_error "${VARDIR}/firewall does not exist or is not executable"
    fi
}

#
# Echo the parameters if product is Shorewall or Shorewall6
#
ecko() {
    [ -z "$g_lite" ] && echo "$@"
}
#
# Give Usage Information
#
usage() # $1 = exit status
{
    echo "Usage: $(basename $0) [debug|trace] [nolock] [ -q ] [ -v[-1|{0-2}] ] [ -t ] <command>"
    echo "where <command> is one of:"
    echo "   add <interface>[:<host-list>] ... <zone>"
    echo "   allow <address> ..."
    echo "   blacklist <address> [ <option> ... ]"
    ecko "   [ check | ck ] [ -e ] [ -r ] [ -p ] [ -r ] [ -T ] [ -i ] [ <directory> ]"
    echo "   clear"
    ecko "   [ compile | co ] [ -e ] [ -p ] [ -t ] [ -c ] [ -d ] [ -T ] [ -i ] [ <directory name> ] [ <path name> ]"
    echo "   close <source> <dest> [ <protocol> [ <port> ] ]" 
    echo "   delete <interface>[:<host-list>] ... <zone>"
    echo "   disable <interface>"
    echo "   drop <address> ..."
    echo "   dump [ -x ] [ -l ] [ -m ]"
    echo "   enable <interface>"
    ecko "   export [ <directory1> ] [<user>@]<system>[:<directory2>]"
    echo "   forget [ <file name> ]"
    echo "   help"

    if [ $g_family -eq 4 ]; then
	echo "   ipcalc { <address>/<vlsm> | <address> <netmask> }"
	echo "   ipdecimal { <address> | <integer> }"
	echo "   iprange <address>-<address>"
    fi

    if [ $g_family -eq 4 ]; then
	echo "   iptrace <iptables match expression>"
    else
	echo "   iptrace <ip6tables match expression>"
    fi

    ecko "   load [ -s ] [ -c ] [ -r <root user> ] [ -T ] [ -i ] [ <directory> ] <system>"
    echo "   logdrop <address> ..."
    echo "   logreject <address> ..."
    echo "   logwatch [<refresh interval>]"

    if [ $g_family -eq 4 ]; then
	echo "   noiptrace <iptables match expression>"
    else
	echo "   noiptrace <ip6tables match expression>"
    fi

    echo "   open <source> <dest> [ <protocol> [ <port> ] ]" 
    echo "   reenable <interface>"
    ecko "   refresh [ -d ] [ -n ] [ -T ] [ -D <directory> ] [ <chain>... ]"
    echo "   reject <address> ..."

    if [ -n "$g_lite" ]; then
	echo "   reload [ -n ] [ -p ] [ -f ] [ -C ] [ <directory> ]"
    else
	echo "   reload [ -n ] [ -p ] [-d] [ -f ] [ -c ] [ -T ] [ -i ] [ -C ] [ <directory> ]"
    fi

    if [ -z "$g_lite" ]; then
	echo "   remote-reload [ -n ] [ -s ] [ -c ] [ -r <root-name> ] [ -T ] [ -i ] [ <directory> ] <system>"
	echo "   remote-restart [ -n ] [ -s ] [ -c ] [ -r <root-name> ] [ -T ] [ -i ] [ <directory> ] <system>"
	echo "   remote-start [ -n ] [ -s ] [ -c ] [ -r <root-name> ] [ -T ] [ -i ] [ <directory> ] <system>"
    fi

    echo "   reset [ <chain> ... ]"

    if [ -n "$g_lite" ]; then
	echo "   restart [ -n ] [ -p ] [ -f ] [ -C ] [ <directory> ]"
    else
	echo "   restart [ -n ] [ -p ] [-d] [ -f ] [ -c ] [ -T ] [ -i ] [ -C ] [ <directory> ]"
    fi

    echo "   restore [ -n ] [ -p ] [ -C ] [ <file name> ]"
    echo "   run <command> [ <parameter> ... ]"
    ecko "   safe-restart [ -t <timeout> ] [ <directory> ]"
    ecko "   safe-start [ -t <timeout> ] [ <directory> ]"
    echo "   save [ -C ] [ <file name> ]"
    echo "   savesets"
    echo "   [ show | list | ls ] [ -b ] [ -x ] [ -t {filter|mangle|nat} ] [ {chain [<chain> [ <chain> ... ]"
    ecko "   [ show | list | ls ] actions"
    ecko "   [ show | list | ls ] action <action>"
    echo "   [ show | list | ls ] arptables"
    echo "   [ show | list | ls ] [ -f ] capabilities"
    echo "   [ show | list | ls ] [ -x ] {bl|blacklists}"
    echo "   [ show | list | ls ] classifiers"
    echo "   [ show | list | ls ] config"
    echo "   [ show | list | ls ] connections"
    echo "   [ show | list | ls ] event [ <event> ...]"
    echo "   [ show | list | ls ] events"
    echo "   [ show | list | ls ] filters"
    echo "   [ show | list | ls ] ip"

    if [ $g_family -eq 4 ]; then
	echo "   [ show | list | ls ] ipa"
    fi

    echo "   [ show | list | ls ] ipsec"
    echo "   [ show | list | ls ] [ -m ] log [<regex>]"
    echo "   [ show | list | ls ] [ -x ] mangle|nat|raw"
    ecko "   [ show | list | ls ] macro <macro>"
    ecko "   [ show | list | ls ] macros"
    echo "   [ show | list | ls ] nfacct"
    echo "   [ show | list | ls ] opens"
    echo "   [ show | list | ls ] policies"
    echo "   [ show | list | ls ] routing"
    echo "   [ show | list | ls ] tc [ device ]"
    echo "   [ show | list | ls ] vardir"
    echo "   [ show | list | ls ] zones"

    if [ -n "$g_lite" ]; then
	echo "   start [ -f ] [ -p ] [ -C ] [ <directory> ]"
    else
	echo "   start [ -f ] [ -n ] [ -p ] [ -c ] [ -T ] [ -i ] [ -C ] [ <directory> ]"
    fi

    echo "   status [ -i ]"
    echo "   stop"
    ecko "   try <directory> [ <timeout> ]"
    ecko "   update [ -a ] [ -b ] [ -r ] [ -T ]  [ -D ] [ -i ] [-t] [-s] [-n] [-A] [ <directory> ]"
    echo "   version [ -a ]"
    echo
    exit $1
}

#
# This is the main entry point into the CLI. It directly handles all commands supported
# by both the full and lite versions. Note, however, that functions such as start_command()
# appear in both this library and in lib.cli-std. The ones in cli-std overload the ones
# here if that lib is loaded below.
#
shorewall_cli() {
    g_debugging=

    if [ $# -gt 0 ] && [ "x$1" = "xdebug" -o "x$1" = "xtrace" ]; then
	g_debugging=$1
	shift
    fi

    g_nolock=

    if [ $# -gt 0 ] && [ "$1" = "nolock" ]; then
	g_nolock=nolock
	shift
    fi

    g_noroutes=
    g_purge=
    g_ipt_options="-nv"
    g_fast=
    g_verbose_offset=0
    g_use_verbosity=
    g_debug=
    g_export=
    g_refreshchains=:none:
    g_confess=
    g_update=
    g_annotate=
    g_recovering=
    g_timestamp=
    g_shorewalldir=
    g_haveconfig=
    g_conditional=
    g_file=
    g_doing="Compiling"
    g_inline=
    g_counters=
    g_loopback=
    g_compiled=
    g_pager=
    g_nopager=
    g_blacklistipset=
    g_disconnect=

    VERBOSE=
    VERBOSITY=1
    #
    # Set the default product based on the Shorewall packages installed
    #
    set_default_product

    finished=0

    while [ $finished -eq 0 ]; do
	if [ $# -eq 0 ]; then
	    setup_product_environment 1
	    usage 1
	fi
	option=$1
	case $option in
	    -)
		finished=1
		;;
	    -*)
		option=${option#-}

		while [ -n "$option" ]; do
		    case $option in
			c)
			    [ $# -eq 1 ]     && missing_option_value -c
			    [ -n "$g_lite" ] && fatal_error "$g_product does not support the -c option"

			    if [ ! -d $2 ]; then
				if [ -e $2 ]; then
				    fatal_error "$2 is not a directory"
				else
				    fatal_error "Directory $2 does not exist"
				fi
			    fi

			    g_shorewalldir=$(resolve_file $2)
			    option=
			    shift
			    ;;
			e*)
			    [ -n "$g_lite" ] && fatal_error "$g_product does not support the -e option"
			    g_export=Yes
			    option=${option#e}
			    ;;
			x*)
			    g_ipt_options="-xnv"
			    option=${option#x}
			    ;;
			q*)
			    g_verbose_offset=$(($g_verbose_offset - 1 ))
			    option=${option#q}
			    ;;
			f*)
			    g_fast=Yes
			    option=${option#f}
			    ;;
			[vV]*)
			    case $option in
				v*)
				    option=${option#v}
				    ;;
				*)
				    option=${option#V}
				    ;;
			    esac

			    case $option in
				-1*)
				    g_use_verbosity=-1
				    option=${option#-1}
				    ;;
				0*)
				    g_use_verbosity=0
				    option=${option#0}
				    ;;
				1*)
				    g_use_verbosity=1
				    option=${option#1}
				    ;;
				2*)
				    g_use_verbosity=2
				    option=${option#2}
				    ;;
				*)
				    g_verbose_offset=$(($g_verbose_offset + 1 ))
				    g_use_verbosity=
				    ;;
			    esac
			    ;;
			n*)
			    g_noroutes=Yes
			    option=${option#n}
			    ;;
			t*)
			    g_timestamp=Yes
			    option=${option#t}
			    ;;
			p*)
			    g_nopager=Yes
			    option=${option#p}
			    ;;
			6*)
			    if [ "$PRODUCT" = shorewall ]; then
				PRODUCT=shorewall6
			    elif [ "$PRODUCT" = shorewall-lite ]; then
				PRODUCT=shorewall6-lite
			    fi
			    option=${option#6}
			    ;;
			4*)
			    if [ "$PRODUCT" = shorewall6 ]; then
				PRODUCT=shorewall
			    elif [ "$PRODUCT" = shorewall6-lite ]; then
				PRODUCT=shorewall-lite
			    fi
			    option=${option#4}
			    ;;
			l*)
			    if [ "$PRODUCT" = shorewall ]; then
				PRODUCT=shorewall-lite
			    elif [ "$PRODUCT" = shorewall6 ]; then
				PRODUCT=shorewall6-lite
			    fi
			    option=${option#l}
			    ;;
			-)
			    finished=1
			    option=
			    ;;
			*)
			    option_error $option
			    ;;
		    esac
		done
		shift
		;;
	    *)
		finished=1
		;;
	esac
    done

    setup_product_environment 1

   [ -n "$g_lite" ] || . ${SHAREDIR}/shorewall/lib.cli-std

    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
    MUTEX_TIMEOUT=

    [ -f ${g_confdir}/vardir ] && . ${g_confdir}/vardir

    [ -n "${VARDIR:=/var/lib/$PRODUCT}" ]

    g_firewall=${VARDIR}/firewall

    version_file=${g_sharedir}/version
    if [ -f $version_file ]; then
	SHOREWALL_VERSION=$(cat $version_file)
    else
	echo "   ERROR: $g_product is not properly installed" >&2
	echo "	 The file $version_file does not exist"  >&2
	exit 1
    fi

    banner="${g_product}-${SHOREWALL_VERSION} Status at $g_hostname -"

    COMMAND=$1

    case "$COMMAND" in
	start)
	    only_root
	    get_config Yes Yes
	    shift
	    start_command $@
	    ;;
	stop|clear)
	    [ $# -ne 1 ] && too_many_arguments $2
	    only_root
	    get_config
	    [ -x $g_firewall ] || fatal_error "$g_product has never been started"
	    [ -n "$g_nolock" ] || mutex_on
	    run_it $g_firewall $g_debugging $COMMAND
	    [ -n "$g_nolock" ] || mutex_off
	    ;;
	reset)
	    only_root
	    get_config
	    shift
	    [ -n "$g_nolock" ] || mutex_on
	    [ -x $g_firewall ] || fatal_error "$g_product has never been started"
	    run_it $g_firewall $g_debugging reset $@
	    [ -n "$g_nolock" ] || mutex_off
	    ;;
	reload|restart)
	    only_root
	    get_config Yes Yes
	    shift
	    restart_command $@
	    ;;
	disable|enable|reenable)
	    only_root
	    get_config Yes
	    if product_is_started; then
		run_it ${VARDIR}/firewall $g_debugging $@
	    else
		fatal_error "$g_product is not running"
	    fi
	    ;;
	blacklist)
	    only_root
	    get_config Yes
	    shift
	    [ -n "$g_nolock" ] || mutex_on
	    blacklist_command $@
	    [ -n "$g_nolock" ] || mutex_off
	    ;;
	run)
	    [ $# -gt 1 ] || fatal_error "Missing function name"
	    only_root
	    get_config Yes
	    run_command $@
	    ;;
	show|list|ls)
	    get_config Yes No Yes
    	    shift
    	    show_command $@
	    ;;
	status)
	    only_root
	    get_config
	    shift
	    status_command $@
	    ;;
	dump)
	    only_root
	    get_config Yes No Yes
    	    shift
    	    dump_command $@
	    ;;
	hits)
	    [ $g_family -eq 6 ] && fatal_error "$g_product does not support the hits command"
	    only_root
	    get_config Yes No Yes
	    [ -n "$g_debugging" ] && set -x
	    shift
	    hits_command $@
	    ;;
	version)
	    shift
	    version_command $@
	    ;;
	logwatch)
	    only_root
	    get_config Yes Yes Yes
	    banner="${g_product}-$SHOREWALL_VERSION Logwatch at $g_hostname -"
	    logwatch_command $@
	    ;;
	drop)
	    only_root
	    get_config
	    [ -n "$g_debugging" ] && set -x
	    [ $# -eq 1 ] && missing_argument
	    drop_command $@
	    ;;
	logdrop)
	    only_root
	    get_config
	    [ -n "$g_debugging" ] && set -x
	    [ $# -eq 1 ] && missing_argument
	    logdrop_command $@
	    ;;
	reject|logreject)
	    only_root
	    get_config
	    [ -n "$g_debugging" ] && set -x
	    [ $# -eq 1 ] && missing_argument
	    reject_command $@
	    ;;
	open|close)
	    only_root
	    get_config
	    shift
	    open_close_command $@
	    ;;
	allow)
	    only_root
	    get_config
	    allow_command $@
	    ;;
	add)
	    only_root
	    get_config
	    shift
	    add_command $@
	    ;;
	delete)
	    only_root
	    get_config
	    shift
	    delete_command $@
	    ;;
	save)
	    only_root
	    get_config
	    [ -n "$g_debugging" ] && set -x
	    save_command $@
	    ;;
	forget)
	    only_root
	    get_config
	    forget_command $@
	    ;;
	ipcalc)
    	    [ -n "$g_debugging" ] && set -x
	    ipcalc_command $@
	    ;;
	iprange)
	    [ -n "$g_debugging" ] && set -x
	    iprange_command $@
	    ;;
	ipdecimal)
	    [ -n "$g_debugging" ] && set -x
	    ipdecimal_command $@
	    ;;
	restore)
	    only_root
	    get_config
	    shift
	    restore_command $@
            ;;
	call)
	    only_root
	    get_config
	    [ -n "$g_debugging" ] && set -x
	    #
	    # Way to call functions in the libraries directly
	    #
	    shift

	    if [ $# -gt 0 ]; then
		#
		# First look for it here
		#
		if type $1 2> /dev/null | fgrep -q 'is a function'; then
		    #
		    # It's a shell function -- call it
		    #
		    $@
		elif type $1 2> /dev/null | fgrep -q 'is a shell function'; then
		    #
		    # It's a shell function -- call it
		    #
		    $@
		else
		    #
		    # It isn't a function visible to this script -- try
		    # the compiled firewall
		    #
		    run_it $g_firewall $g_debugging call $@
		fi
	    else
		missing_argument
	    fi
	    ;;
	help)
	    shift
	    usage
	    ;;
	iptrace)
	    only_root
	    get_config
	    shift
	    iptrace_command $@
	    ;;
	noiptrace)
	    only_root
	    get_config
	    shift
	    noiptrace_command $@
	    ;;
	savesets)
	    [ $# -eq 1 ] || too_many_arguments $2
	    only_root
	    get_config
	    [ -n "$g_debugging" ] && set -x
	    savesets1
	    ;;
	*)
	    if [ -z "$g_lite" ]; then
		compiler_command $@
	    else
		fatal_error "Invalid command: $COMMAND"
	    fi
	    ;;
    esac
}