File: rapolicy.c

package info (click to toggle)
argus-clients 1%3A3.0.8.2-6.1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 22,948 kB
  • sloc: ansic: 130,931; sh: 3,809; perl: 3,199; makefile: 1,782; lex: 459; yacc: 372
file content (1775 lines) | stat: -rw-r--r-- 55,134 bytes parent folder | download | duplicates (3)
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
/* 
 * Argus Software
 * Copyright (c) 2000-2016 QoSient, LLC
 * All rights reserved.
 * 
 * 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, 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 * 
 * $Id: //depot/argus/clients/examples/rapolicy/rapolicy.c#17 $
 * $DateTime: 2016/06/01 15:17:28 $
 * $Change: 3148 $
 */

/*
 * rapolicy.c  - match input argus records against
 *    a Cisco access control policy.
 *       
 * written by Carter Bullard and Dave Edelman
 * QoSient, LLC
 *       
 */

#ifdef HAVE_CONFIG_H
#include "argus_config.h"
#endif

#if defined(CYGWIN)
#define USE_IPV6
#endif

#define RA_POLICY_C

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <ctype.h>
#include <stdio.h>
#include <arpa/inet.h>

#include <argus_compat.h>

#include <argus_util.h>
#include <argus_client.h>
#include <argus_main.h>
#include <argus_filter.h>


#include <rapolicy.h>

struct RaPolicyPolicyStruct *RaPolicy = NULL;
struct RaPolicyPolicyStruct *RaGlobalPolicy = NULL;

int RaPolicyParseResourceFile (struct ArgusParserStruct *, char *, struct RaPolicyPolicyStruct **);
int RaReadPolicy (struct ArgusParserStruct *, struct RaPolicyPolicyStruct **, char *);
int RaParsePolicy (struct ArgusParserStruct *, struct RaPolicyPolicyStruct **, char *);
int RaCheckPolicy (struct ArgusParserStruct *, struct ArgusRecordStruct *, struct RaPolicyPolicyStruct *);
int RaMeetsPolicyCriteria (struct ArgusParserStruct *, struct ArgusRecordStruct *, struct RaPolicyPolicyStruct *);
void RaDumpPolicy (struct ArgusParserStruct *, struct RaPolicyPolicyStruct *);
void RaDumpCounters (struct RaPolicyPolicyStruct *);
int RaDoNotification (struct ArgusRecordStruct *, struct RaPolicyPolicyStruct *);


void
ArgusClientInit (struct ArgusParserStruct *parser)
{
   parser->RaWriteOut = 1;

   if (!(parser->RaInitialized)) {
      (void) signal (SIGHUP,  (void (*)(int)) RaParseComplete);

      parser->RaInitialized++;
      parser->RaWriteOut = 0;

      if (parser->ArgusFlowModelFile != NULL) {
         RaPolicyParseResourceFile (parser, parser->ArgusFlowModelFile, &RaPolicy);
      } else {
         if (!(parser->Xflag)) {
            RaPolicyParseResourceFile (parser, "/etc/rapolicy.conf", &RaPolicy);
         }
      }
   }
}

void RaArgusInputComplete (struct ArgusInput *input) { return; }


void
RaParseComplete (int sig)
{
   if (sig >= 0) {
      if (!ArgusParser->RaParseCompleting++) {
#ifdef ARGUSDEBUG
         ArgusDebug (2, "RaParseComplete(caught signal %d)\n", sig);
#endif
         switch (sig) {
            case SIGHUP: 
            case SIGINT:
            case SIGTERM:
            case SIGQUIT: {
               struct ArgusWfileStruct *wfile = NULL;

               if (ArgusParser->Aflag) {
                      RaDumpCounters(RaPolicy);
                  }
               
               ArgusShutDown(sig);

               if (ArgusParser->ArgusWfileList != NULL) {
                  struct ArgusListObjectStruct *lobj = NULL;
                  int i, count = ArgusParser->ArgusWfileList->count;

                  if ((lobj = ArgusParser->ArgusWfileList->start) != NULL) {
                     for (i = 0; i < count; i++) {
                        if ((wfile = (struct ArgusWfileStruct *) lobj) != NULL) {
                           if (wfile->fd != NULL) {
#ifdef ARGUSDEBUG
                              ArgusDebug (2, "RaParseComplete: closing %s\n", wfile->filename);
#endif
                              fflush (wfile->fd);
                              fclose (wfile->fd);
                              wfile->fd = NULL;
                           }
                        }
                        lobj = lobj->nxt;
                     }
                  }
               }
               exit(0);
               break;
            }
         }
      }
   }
}


void
ArgusClientTimeout ()
{
#ifdef ARGUSDEBUG
   ArgusDebug (6, "ArgusClientTimeout()\n");
#endif
}

void
parse_arg (int argc, char**argv)
{}

void
usage ()
{
   extern char version[];

   fprintf (stdout, "Rapolicy Version %s\n", version);
   fprintf (stdout, "usage: %s -f rapolicy.conf [ra-options]\n", ArgusParser->ArgusProgramName);

   fprintf (stdout, "options: -f rapolicy.conf file.\n");
   fflush (stdout);

   exit(1);
}

void
RaDumpCounters (struct RaPolicyPolicyStruct *policy) 
{
       printf("\nHit Rates [flows  packets   bytes]\n");
       while (policy) {
	if(policy->hitCount != 0) {
         printf("[%10lld %15lld %20lld]\tACL %s Line %ld: %s\n",
           policy->hitCount, policy->hitPkts, policy->hitBytes,
           policy->policyID, policy->line,  policy->str);
	}
         policy = policy->nxt;
       }
}
 
void
RaProcessRecord (struct ArgusParserStruct *parser, struct ArgusRecordStruct *argus)
{
   struct ArgusFlow *flow = (struct ArgusFlow *) argus->dsrs[ARGUS_FLOW_INDEX];
   int process= 0;

   switch (argus->hdr.type & 0xF0) {
      case ARGUS_MAR:
      case ARGUS_EVENT: {
         break;
      }

      case ARGUS_NETFLOW:
      case ARGUS_FAR: {
         if (flow) {
            switch (flow->hdr.subtype & 0x3F) {
               case ARGUS_FLOW_CLASSIC5TUPLE: {
                  switch (flow->hdr.argus_dsrvl8.qual & 0x1F) {
                     case ARGUS_TYPE_IPV4:
                        switch (flow->ip_flow.ip_p) {
                           case IPPROTO_TCP:
                           default:
                           case IPPROTO_UDP: {
                              process++;
                              break;
                           }
                        }
                        break;

                     case ARGUS_TYPE_IPV6: {
                        switch (flow->ipv6_flow.ip_p) {
                           case IPPROTO_TCP:
                           case IPPROTO_UDP: {
// not quite ready for IPv6                              process++;
                              break;
                           }
                        }
                        break;
                     }
                  }
                  break;
               }
            }

            if (process){
               struct ArgusRecordStruct *ns = ArgusCopyRecordStruct(argus);
               if ((RaCheckPolicy (parser, ns, RaPolicy) || (parser->RaPolicyStatus & ARGUS_POLICY_JUST_LABEL)))
                  RaSendArgusRecord (ns);
               
               ArgusDeleteRecordStruct(parser, ns);
            }

            if ( (!process) && (parser->RaPolicyStatus & ARGUS_POLICY_PERMIT_OTHERS))
               RaSendArgusRecord (argus);
         }
      }
   }

#ifdef ARGUSDEBUG
   ArgusDebug (6, "RaProcessRecord () returning\n");
#endif
}

int
RaSendArgusRecord(struct ArgusRecordStruct *ns)
{
   char buf[0x10000];
   int retn = 1;

   if (ns->status & ARGUS_RECORD_WRITTEN)
      return (retn);
 
   if ((ArgusParser->ArgusWfileList != NULL) && (!(ArgusListEmpty(ArgusParser->ArgusWfileList)))) {
      struct ArgusWfileStruct *wfile = NULL;
      struct ArgusListObjectStruct *lobj = NULL;
      int i, count = ArgusParser->ArgusWfileList->count;

      if ((lobj = ArgusParser->ArgusWfileList->start) != NULL) {
         for (i = 0; i < count; i++) {
            if ((wfile = (struct ArgusWfileStruct *) lobj) != NULL) {
               int pass = 1;
               if (wfile->filterstr) {
                     struct nff_insn *wfcode = wfile->filter.bf_insns;
                     pass = ArgusFilterRecord (wfcode, ns);
               }

               if (pass != 0) {
                  if ((ArgusParser->exceptfile == NULL) || strcmp(wfile->filename, ArgusParser->exceptfile)) {
                     struct ArgusRecord *argusrec = NULL;
                     if ((argusrec = ArgusGenerateRecord (ns, 0L, buf)) != NULL) {
#ifdef _LITTLE_ENDIAN
                        ArgusHtoN(argusrec);
#endif
                        ArgusWriteNewLogfile (ArgusParser, ns->input, wfile, argusrec);
                     }
                  }
               }
            }
            lobj = lobj->nxt;
         }
      }

   } else {
      if (!ArgusParser->qflag) {
         if (ArgusParser->Lflag) {
            if (ArgusParser->RaLabel == NULL)
               ArgusParser->RaLabel = ArgusGenerateLabel(ArgusParser, ns);
 
            if (!(ArgusParser->RaLabelCounter++ % ArgusParser->Lflag))
               printf ("%s\n", ArgusParser->RaLabel);
 
            if (ArgusParser->Lflag < 0)
               ArgusParser->Lflag = 0;
         }

         *(int *)&buf = 0;
         ArgusPrintRecord(ArgusParser, buf, ns, MAXSTRLEN);
         if (fprintf (stdout, "%s\n", buf) < 0)
            RaParseComplete(SIGQUIT);
         fflush(stdout);
      }
   }

   ns->status |= ARGUS_RECORD_WRITTEN;
   return (retn);
}

void ArgusWindowClose(void);

void ArgusWindowClose(void) { 
#ifdef ARGUSDEBUG
   ArgusDebug (6, "ArgusWindowClose () returning\n"); 
#endif
}

#define RAPOLICY_RCITEMS			8

#define RA_POLICY_SHOW_WHICH			0
#define RA_POLICY_LABEL_ALL			1
#define RA_POLICY_LABEL_LOG			2
#define RA_POLICY_PERMIT_OTHERS			3
#define RA_POLICY_DUMP_POLICY			4
#define RA_POLICY_LABEL_IMPLICIT		5
#define RA_POLICY_JUST_LABEL			6
#define RA_POLICY_ACL_FILE                      7

char *RaPolicyResourceFileStr [] = {
   "RA_POLICY_SHOW_WHICH=",
   "RA_POLICY_LABEL_ALL=",
   "RA_POLICY_LABEL_LOG=",
   "RA_POLICY_PERMIT_OTHERS=",
   "RA_POLICY_DUMP_POLICY=",
   "RA_POLICY_LABEL_IMPLICIT=",
   "RA_POLICY_JUST_LABEL=",
   "RA_POLICY_ACL_FILE=",
};


int RaInitialState = 0;
int RaParseError = 0;

char *RaParseErrorStr [POLICYERRORNUM] = {
   "access-list identifier not found",
   "policy id number not found",
   "permit/deny indication not found",
   "protocol indentifier not found",
   "no source address defined",
   "no source address mask defined",
   "wrong source port operator",
   "wrong source port specification"
   "no destination address defined",
   "no destination address mask defined",
   "wrong destination port operator",
   "wrong destination port specification",
   "access violation notification not found",
};

int
RaPolicyParseResourceFile (struct ArgusParserStruct *parser, char *file, struct RaPolicyPolicyStruct **policy)
{
   int retn = 0;
   int i, len, done = 0, linenum = 0;
   struct RaPolicyPolicyStruct *pol;
   char strbuf[MAXSTRLEN], *str = strbuf, *optarg;
   FILE *fd;

   if (file) {
      if ((fd = fopen (file, "r")) != NULL) {
         while ((fgets(str, MAXSTRLEN, fd)) != NULL)  {
            done = 0;  linenum++;
            while (*str && isspace((int)*str))
                str++;

            if (*str && (*str != '#') && (*str != '\n') && (*str != '!')) {
               for (i = 0; i < RAPOLICY_RCITEMS && !done; i++) {
                  len = strlen(RaPolicyResourceFileStr[i]);
                  if (!(strncmp (str, RaPolicyResourceFileStr[i], len))) {
                     optarg = &str[len];
                     if (*optarg == '\"') { optarg++; }
                     if (optarg[strlen(optarg) - 1] == '\n')
                        optarg[strlen(optarg) - 1] = '\0';
                     if (optarg[strlen(optarg) - 1] == '\"')
                        optarg[strlen(optarg) - 1] = '\0';

                     switch (i) {
                        case RA_POLICY_SHOW_WHICH: {
                           if (!(strncasecmp(optarg, "deny", 4)))
                              parser->RaPolicyStatus |= ARGUS_POLICY_SHOW_DENY;
                           else
                              parser->RaPolicyStatus &= ~ARGUS_POLICY_SHOW_DENY;
                           break;
                        }
                        case RA_POLICY_LABEL_ALL: {
                           if (!(strncasecmp(optarg, "yes", 3)))
                              parser->RaPolicyStatus |= ARGUS_POLICY_LABEL_ALL;
                           else
                              parser->RaPolicyStatus &= ~ARGUS_POLICY_LABEL_ALL;
                           break;
                        }
                        case RA_POLICY_LABEL_LOG: {
                           if (!(strncasecmp(optarg, "yes", 3)))
                              parser->RaPolicyStatus |= ARGUS_POLICY_LABEL_LOG;
                           else
                              parser->RaPolicyStatus &= ~ARGUS_POLICY_LABEL_LOG;
                           break;
                        }
                        case RA_POLICY_PERMIT_OTHERS: {
                           if (!(strncasecmp(optarg, "yes", 3)))
                              parser->RaPolicyStatus |= ARGUS_POLICY_PERMIT_OTHERS;
                           else
                              parser->RaPolicyStatus &= ~ARGUS_POLICY_PERMIT_OTHERS;
                           break;
                        }
                        case RA_POLICY_DUMP_POLICY: {
                           if (!(strncasecmp(optarg, "yes", 3)))
                              parser->RaPolicyStatus |= ARGUS_POLICY_DUMP_POLICY;
                           else
                              parser->RaPolicyStatus &= ~ARGUS_POLICY_DUMP_POLICY;
                           break;
                        }
                        case RA_POLICY_LABEL_IMPLICIT: {
                           if (!(strncasecmp(optarg, "yes", 3)))
                              parser->RaPolicyStatus |= ARGUS_POLICY_LABEL_IMPLICIT;
                           else
                              parser->RaPolicyStatus &= ~ARGUS_POLICY_LABEL_IMPLICIT;
                           break;
                        }
                        case RA_POLICY_JUST_LABEL: {
                           if (!(strncasecmp(optarg, "yes", 3)))
                              parser->RaPolicyStatus |= ARGUS_POLICY_JUST_LABEL;
                           else
                              parser->RaPolicyStatus &= ~ARGUS_POLICY_JUST_LABEL;
                           break;
                        }
                       case RA_POLICY_ACL_FILE: {
                          if (!(RaReadPolicy(parser, policy, optarg) > 0) ){
                             ArgusLog (LOG_ERR, "RaPolicy: RaReadPolicy Error");
                             exit(0);
                          }
                          if (parser->RaPolicyStatus & ARGUS_POLICY_DUMP_POLICY) {
                             pol = *policy; 
                             while (pol) {
                                RaDumpPolicy(parser, pol);
                                pol = pol->nxt;
                             }
                             exit(1);
                          }
                          break;
                       }
                     }
                  }
               }
            }
         }
         fclose(fd);
      }
   }

#ifdef ARGUSDEBUG
   ArgusDebug (2, "RaPolicyParseResourceFile (%p, %s, %p) returning %d\n", parser, file, policy, retn);
#endif

   return (retn);
}

int
RaReadPolicy (struct ArgusParserStruct *parser, struct RaPolicyPolicyStruct **policy, char *file)
{
   int retn = 1, linenum = 0;
   struct RaPolicyPolicyStruct *pol, *policyLast = NULL;
   char buffer [1024];
   FILE *fd;

   if (file) {
      if ((fd = fopen (file, "r")) != NULL) {
         while (fgets (buffer, 1024, fd)) {
            linenum++;
            pol = NULL;
            if ((*buffer != '#') && (*buffer != '\n') && (*buffer != '!')) {
               if ((retn = RaParsePolicy (parser, &pol, buffer)) > 0) {
                  if (policyLast)  {
                     policyLast->nxt = pol;
                     pol->prv = policyLast;
                     pol->line = linenum;
                     pol->policyID = policyLast->policyID;
                     policyLast = pol;
                  } else {
                     *policy = policyLast = pol;
                     pol->line = linenum;
                    }
                  if (retn < 0)
                     ArgusLog (LOG_ERR, "RaReadPolicy: line %d: %s\n", linenum, RaParseErrorStr [RaParseError]);
               }
                     sprintf (buffer, "ACL=%s_%s_Line_%4.4d",
                           pol->flags & RA_PERMIT ? "Permit" : "Deny",
                           pol->policyID,
                           (int) pol->line
                     );
                     pol->labelStr = strdup(buffer);
                     if(pol->str[strlen(pol->str)-1] == '\n') pol->str[strlen(pol->str)-1] = '\0';
            }
         }
         fclose (fd);
         retn = 1;

      } else {
         retn = 0;
         ArgusLog (LOG_ERR, "RaReadPolicy: fopen %s %s\n", file,  strerror(errno));
      }
   }

#if defined(ARGUSDEBUG)
   ArgusDebug (2, "RaReadPolicy (0x%x, %s) returning %d\n", policy, file, retn);
#endif
   return (retn);
}


void
RaDumpPolicy (struct ArgusParserStruct *parser, struct RaPolicyPolicyStruct *policy) 
{
struct protoent *proto;
arg_uint32 host, any, low, high;

host = ntohl(inet_addr("0.0.0.0"));
any = ntohl(inet_addr("255.255.255.255"));

    printf("%s Line %4.4ld %s\n",
           policy->policyID, policy->line, policy->str);


    printf("\tThe pointer to the previous policy is %s, to the next policy is %s\n",
           policy->prv == NULL ? "empty (this is the first entry)" : "defined",
           policy->nxt == NULL ? "empty (this is the final entry)" : "defined" );

    if(!((policy->flags & RA_PERMIT) || (policy->flags & RA_DENY))){
       return ;
   }

    printf("\tIn the case of a match, the flow will be %s%s\n",
           (policy->flags & RA_PERMIT) ? "permitted" : "",
           (policy->flags & RA_DENY) ? "denied" : "");

    if (policy->proto == 0) {
       printf("\tThe policy will be applied to flows matching any IP based protocol\n");
    }  else {
        proto = getprotobynumber((int) policy->proto);
        printf("\tThe policy will be applied to flows matching the %s (%d) protocol\n",
           proto->p_name, policy->proto );
    }
    printf("\tThe policy flag value is %lx, which means that a match has these requirements:\n",
           policy->flags);

    if (policy->flags & RA_SRC_SET) {
        printf("\tThe source address will be evaluated:\n");
        printf("\t\tThe source address is %lx (%s) and the source wildcard is %lx (%s)\n",
           policy->src.addr, ArgusGetName(parser, (u_char *) &policy->src.addr), 
           policy->src.mask, ArgusGetName(parser, (u_char *) &policy->src.mask));
        if((policy->src.addr == host) && (policy->src.mask == any)) {
           printf("\t\tThe source may be any IP address\n");
        } else {
          if (policy->src.mask == any) {
              printf("\t\tThe specific host IP address much match\n");
          } else {
            low = policy->src.addr & ~policy->src.mask;
            high = low + policy->src.mask;
            printf("\t\tThe source may be any IP address in the range %s - %s (with possible gaps)\n", 
               ArgusGetName(parser, (u_char *)  &low),
               ArgusGetName(parser, (u_char *) &high));
            }
       } 
    } else {
        printf("\tThe source address will not be evaluated to determine a match\n");
      }


    if (policy->flags & RA_DST_SET) {
        printf("\tThe destination address will be evaluated:\n");
        printf("\t\tThe destination address is %lx (%s) and the destination wildcard is %lx (%s)\n",
           policy->dst.addr, ArgusGetName(parser, (u_char *) &policy->dst.addr), 
           policy->dst.mask, ArgusGetName(parser, (u_char *) &policy->dst.mask));
        if((policy->dst.addr == host) && (policy->dst.mask == any)) {
           printf("\t\tThe destination may be any IP address\n");
        } else {
          if (policy->dst.mask == any) {
              printf("\t\tThe specific host IP address much match\n");
          } else {
            low = policy->dst.addr & ~policy->dst.mask;
            high = low + policy->dst.mask;
            printf("\t\tThe destination may be any IP address in the range %s - %s (with possible gaps)\n", 
               ArgusGetName(parser, (u_char *) &low),
               ArgusGetName(parser, (u_char *) &high));
            }
       } 
    } else {
        printf("\tThe destination address will not be evaluated to determine a match\n");
      }

     if (policy->flags & RA_SRCPORT_SET) {
       printf("\tThe source port will be evaluated and must ");
       switch (policy->src_action){
          case (RA_EQ):
             printf("be equal to %d\n", policy->src_port_low);
             break;
          case (RA_LT):
             printf("be less than %d\n", policy->src_port_low);
             break;
          case (RA_GT):
             printf("be greater than %d\n", policy->src_port_low);
             break;
          case (RA_NEQ):
             printf("not be equal to %d\n", policy->src_port_low);
             break;
          case (RA_RANGE):
             printf("be between %d and  %d\n", policy->src_port_low, policy->src_port_hi);
             break;
      }
    } else {
       printf("\tThe source port will not be evaluated to determine a match\n");
    }
     if (policy->flags & RA_DSTPORT_SET) {
       printf("\tThe destination port will be evaluated and must ");
       switch (policy->dst_action){
          case (RA_EQ):
             printf("be equal to %d\n", policy->dst_port_low);
             break;
          case (RA_LT):
             printf("be less than %d\n", policy->dst_port_low);
             break;
          case (RA_GT):
             printf("be greater than %d\n", policy->dst_port_low);
             break;
          case (RA_NEQ):
             printf("not be equal to %d\n", policy->dst_port_low);
             break;
          case (RA_RANGE):
             printf("be between %d and  %d\n", policy->dst_port_low, policy->dst_port_hi);
             break;
      }
    } else {
       printf("\tThe destination port will not be evaluated to determine a match\n");
    }
   if (policy->flags & RA_TCPFLG_SET) {
      printf("\tThe set of TCP flags from the source will be evaluated and must include:\n\t");
      if (policy->TCPflags & RA_FIN) printf(" FIN ");
      if (policy->TCPflags & RA_SYN) printf(" SYN ");
      if (policy->TCPflags & RA_RST) printf(" RST ");
      if (policy->TCPflags & RA_PSH) printf(" PSH ");
      if (policy->TCPflags & RA_ACK) printf(" ACK ");
      if (policy->TCPflags & RA_URG) printf(" URG ");
      if (policy->TCPflags & RA_ECE) printf(" ECE ");
      if (policy->TCPflags & RA_CWR) printf(" CWR ");
      if (policy->TCPflags & RA_NS) printf(" NS ");
      if (policy->flags & RA_EST_SET) printf(" the TCP session must be established (have the ACK or RST flag set) ");
      printf("\n");
  }
  if (policy->flags & RA_TOS_SET) {
     printf("\tThe TOS must be equal to %d\n", policy->tos);
  }
  if (policy->flags & RA_ICMP_SET) {
     printf("\tThe ICMP message type must be %d ", policy->ICMPtype);
     if (policy->ICMPcode < ICMPCodeAny) {
        printf("and the ICMP message code must be %d\n", policy->ICMPcode);
     } else {
        printf("with any valid ICMP code value\n");
     }
  }
 return ;
}


#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <ctype.h>


// Terminating Error 

events_t
terror (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "terror word is %s [%d]\n", token, strlen(token));
#endif

   printf ("The ACL parser encountered a problem with \"%s\" set debug to 3 for more information\n", token);
   exit (1);
}

events_t 
initACL (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "initACL the word is %s\n", token);
#endif

   policy->type = RA_IPACCESSLIST;
   return E_NULL;
}

events_t 
initEXT (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "initEXT the word is %s\n", token);
#endif

   policy->type = RA_IPACCESSLIST;
   return E_NULL;
}

events_t 
procACLnum (struct RaPolicyPolicyStruct *policy, char *token)
{
   int acl = atoi(token);

#ifdef ARGUSDEBUG
   ArgusDebug (3, "procACLnum the word is %s\n", token);
#endif

   policy->policyID = strdup(token);

//
// The ACL numbers indicate the type of access control list
//
//    1 -   99   Standard IP access list
//  100 -  199   Extended IP access list
//  200 -  299   Ethernet Type Code access list
//  700 -  799   Ethernet Address access control list   
// 1300 - 1999   Standard IP access list
// 2000 - 2699   Extended IP access list
//
// There were other ranges defined but DECnet, IPX, XNS, Vines AppleTalk seem to have fallen out of favor
//
   if (acl > 0 && acl < 100)
      return E_STD;

   if (acl > 1299 && acl < 2000)
      return E_STD;

   if (acl > 99 && acl < 200)
      return E_EXT;

   if (acl > 1999 && acl < 2700)
      return E_EXT;

   if (acl > 199 && acl < 300)
      return E_IGNORE;

   if (acl > 699 && acl < 800)
      return E_IGNORE;

   return E_NULL;
}

events_t 
saveName (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "saveName the word is %s\n", token);
#endif

   policy->policyID = strdup(token);
   return E_NULL;
}

events_t 
notYet (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug  (3, "this capability is not yet available the word is %s\n", token);
#endif

   return E_NULL;
}

events_t 
setAction (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setAction the word is %s\n", token);
#endif

   if ( !strcasecmp("permit", token))
      policy->flags |= RA_PERMIT;
   if ( !strcasecmp("deny", token))
      policy->flags |= RA_DENY;
   return E_NULL;
}

// Set Source Address
events_t
setsAddr (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setsAddr the word is %s\n", token);
#endif

   policy->src.addr = ntohl(inet_addr(token));
   policy->flags |= RA_SRC_SET;
   return E_NULL;
}

// Set Source wildcard
events_t
setswc (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "3, setswc the word is %s\n", token);
#endif

   policy->src.mask = ntohl(inet_addr(token));
   policy->flags |= RA_SRC_SET;
   return E_NULL;
}

// Set a source ANY address
events_t
setsany (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "3, setsany the word is %s\n", token);
#endif

   policy->src.addr = ntohl(inet_addr("0.0.0.0"));
   policy->src.mask = ntohl(inet_addr("255.255.255.255"));
   policy->flags |= RA_SRC_SET;
   return E_NULL;
}

events_t 
finished (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "finished the word is %s\n", token);
#endif

   return E_NULL;
}

events_t 
getSeq (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "getSeq the word is %s\n", token);
#endif

   policy->seq = atoi(token);
   return E_NULL;
}

events_t 
setdAddr (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setdAddr the word is %s\n", token);
#endif

   policy->dst.addr = ntohl(inet_addr(token));
   policy->flags |= RA_DST_SET;
   return E_NULL;
}

events_t 
setdwc (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setdwc the word is %s\n", token);
#endif

   policy->dst.mask = ntohl(inet_addr(token));
   policy->flags |= RA_DST_SET;
   return E_NULL;
}

events_t 
setdany (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setdany the word is %s\n", token);
#endif

   policy->dst.addr = ntohl(inet_addr("0.0.0.0"));
   policy->dst.mask = ntohl(inet_addr("255.255.255.255"));
   policy->flags |= RA_DST_SET;
   return E_NULL;
}

events_t 
setsrel (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setsrel the word is %s\n", token);
#endif

   if (!strcasecmp("eq", token))
      policy->src_action = RA_EQ;
   if (!strcasecmp("ne", token))
      policy->src_action = RA_NEQ;
   if (!strcasecmp("lt", token))
      policy->src_action = RA_LT;
   if (!strcasecmp("gt", token))
      policy->src_action = RA_GT;
   if (!strcasecmp("range", token))
      policy->src_action = RA_RANGE;
   return E_NULL;
}

events_t 
setdrel (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setdrel the word is %s\n", token);
#endif

   if (!strcasecmp("eq", token))
      policy->dst_action = RA_EQ;
   if (!strcasecmp("ne", token))
      policy->dst_action = RA_NEQ;
   if (!strcasecmp("lt", token))
      policy->dst_action = RA_LT;
   if (!strcasecmp("gt", token))
      policy->dst_action = RA_GT;
   if (!strcasecmp("range", token))
      policy->dst_action = RA_RANGE;
   return E_NULL;
}

events_t 
setProto(struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setProto the word is %s\n", token);
#endif

   if (isdigit(token[0])) {
      policy->proto = atoi(token);
   } else {
      struct protoent *proto;
      if ((proto = getprotobyname(token)) != NULL)
         policy->proto = proto->p_proto;
   }
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setProto %s is %d\n", token,  policy->proto);
#endif

   policy->flags |= RA_PROTO_SET;
   return E_NULL;
}

events_t 
setsport(struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setsport the word is %s\n", token);
#endif

   policy->src_port_low = (arg_uint16) atoi(token);
   policy->flags |= RA_SRCPORT_SET;
   return E_NULL;
}

events_t 
setdport(struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "sdport the word is %s\n", token);
#endif

   policy->dst_port_low = (arg_uint16) atoi(token);
   policy->flags |= RA_DSTPORT_SET;
   return E_NULL;
}

events_t 
setsport2(struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setsport2 the word is %s\n", token);
#endif

   policy->src_port_hi = (arg_uint16) atoi(token);
   policy->flags |= RA_SRCPORT_SET;
   return E_NULL;
}

events_t 
setdport2(struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setsport2 the word is %s\n", token);
#endif

   policy->dst_port_hi = (arg_uint16) atoi(token);
   policy->flags |= RA_DSTPORT_SET;
   return E_NULL;
}

events_t
setsportname(struct RaPolicyPolicyStruct *policy, char *token)
{
   int port, proto;


   proto = (policy->proto) ?  policy->proto : 17 ;
   argus_nametoport(token, &port, &proto);
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setsportname the word is %s the proto is %d  the port is %d\n", token, proto, port);
#endif
   policy->src_port_low = port;
   policy->flags |= RA_SRCPORT_SET;
   return E_NULL;
}

events_t
setdportname(struct RaPolicyPolicyStruct *policy, char *token)
{
   int port, proto;


   proto = (policy->proto) ?  policy->proto : 17 ; // Or your favorite manifest constant
   argus_nametoport(token, &port, &proto);
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setsportname the word is %s the proto is %d  the port is %d\n", token, proto, port);
#endif
   policy->dst_port_low = port;
   policy->flags |= RA_DSTPORT_SET;
   return E_NULL;
}

events_t 
flagLog(struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "flagLog the word is %s\n", token);
#endif

   policy->flags |= RA_LOG_SET;
   return E_NULL;
}

events_t 
setIGMP(struct RaPolicyPolicyStruct *policy, char *token)
{
   int i;

   for ( i = 0; igmpmap[i].len > 0; i++) {
      if (!strncasecmp(igmpmap[i].name, token, igmpmap[i].len)) {
         policy->IGMPtype = igmpmap[i].value;
#ifdef ARGUSDEBUG
         ArgusDebug (3, "setIGMP the word is %s\n", token);
#endif

         policy->flags |= RA_IGMP_SET;
         return E_NULL;
      }
   }

   // The IGMP type can be expressed as a number
   if (isdigit(token[0])) {
      i = atoi(token);
      if (( i > 0) && (i < 256) ) {
         policy->IGMPtype = i;
#ifdef ARGUSDEBUG
         ArgusDebug( 3, "setIGMP the IGMP type is %d\n", i);
#endif

         policy->flags |= RA_IGMP_SET;
         return E_NULL;
      }
   }
#ifdef ARGUSDEBUG
   ArgusDebug (3, "%s is not a valid IGMP type\n", token);
#endif

   return E_NULL;
}

events_t 
setICMPmsg(struct RaPolicyPolicyStruct *policy, char *token)
{
   int i;
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setICMPmsg the word is %s\n", token);
#endif


   for ( i = 0; icmpmap[i].len > 0; i++) {
      if (!strncasecmp(icmpmap[i].name, token, icmpmap[i].len)) {
         policy->ICMPtype = icmpmap[i].value1;
         policy->ICMPcode = icmpmap[i].value2;
         policy->flags |= RA_ICMP_SET;
         return E_NULL;
      }
   }

#ifdef ARGUSDEBUG
   ArgusDebug (3, "setICMPmsg no text match, checking for an integer\n");
#endif


   // The ICMP type can be expressed as an integer as well

   if ( isdigit (token[0])) {
      i = atoi(token);
      if (( i >= 0) && (i < 256) ) {
         policy->ICMPtype = i;
         policy->flags |= RA_ICMP_SET;
#ifdef ARGUSDEBUG
         ArgusDebug (3, "setICMPmsg found an integer %d\n", i);
#endif

         i = -1 * ( (int) E_ICMPCODE);
#ifdef ARGUSDEBUG
         ArgusDebug (3, "setICMPmsg injecting event  %d\n", i);
#endif

         return (events_t) i;
      }
   }
#ifdef ARGUSDEBUG
   ArgusDebug (3, "%s is not a valid value for ICMP\n", token);
#endif

   return E_NULL;
}

events_t 
setICMPcode(struct RaPolicyPolicyStruct *policy, char *token)
{
   int i;
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setICMPcode the word is %s\n", token);
#endif


   // this can only be an integer value for the ICMP code

   if ( isdigit (token[0])) {
      i = atoi(token);
      if (( i >= 0) && (i < 256) ) {
         policy->ICMPcode = i;
         return E_NULL;
      }
   }
   
#ifdef ARGUSDEBUG
   ArgusDebug (3, "%s is not a valid value for ICMP\n", token);
#endif

   return E_NULL;
}

events_t 
setEst (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setEst the word is %s\n", token);
#endif

   policy->flags |= RA_EST_SET;
   return E_NULL;
}

events_t 
setTCPflag (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setTCPflag the word is %s\n", token);
#endif

   policy->flags |= RA_TCPFLG_SET;
   if ( !strcasecmp("fin", token)) {
      policy->TCPflags |=  RA_FIN;
      policy->flags |= RA_TCPFLG_SET;
      return E_NULL;
   }
   if ( !strcasecmp("syn", token)) {
      policy->TCPflags |=  RA_SYN;
      policy->flags |= RA_TCPFLG_SET;
      return E_NULL;
   }
   if ( !strcasecmp("rst", token)) {
      policy->TCPflags |=  RA_RST;
      policy->flags |= RA_TCPFLG_SET;
      return E_NULL;
   }
   if ( !strcasecmp("psh", token)) {
      policy->TCPflags |=  RA_PSH  ;
       policy->flags |= RA_TCPFLG_SET;
       return E_NULL;
   }
   if ( !strcasecmp("ack", token)) {
      policy->TCPflags |=  RA_ACK  ;
       policy->flags |= RA_TCPFLG_SET;
       return E_NULL;
   }
   if ( !strcasecmp("urg", token)) {
      policy->TCPflags |=  RA_URG  ;
       policy->flags |= RA_TCPFLG_SET;
       return E_NULL;
   }
   if ( !strcasecmp("ece", token)) {
      policy->TCPflags |=  RA_ECE  ;
       policy->flags |= RA_TCPFLG_SET;
       return E_NULL;
   }
   if ( !strcasecmp("cwr", token)) {
     policy->TCPflags |=  RA_CWR  ;
     policy->flags |= RA_TCPFLG_SET;
     return E_NULL;
   }
   if ( !strcasecmp("ns",  token)) {
      policy->TCPflags |=  RA_NS;
      policy->flags |= RA_TCPFLG_SET;
      return E_NULL;
   }
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setTCPflag %s is not a valid TCP flag name\n", token);
#endif

   return E_NULL;
}

events_t 
idle (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "idle the word is %s\n", token);
#endif

   return E_NULL;
}

events_t 
getRemark (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "getRemark the word is %s\n", token);
#endif

   policy->flags = RA_COMMENT;
   return E_NULL;
}

events_t 
flagTOS(struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "flagTOS the word is %s\n", token);
#endif

   policy->flags |= RA_TOS_SET;

// The next token must contain the value for the TOS comparison
// force the token to advance and inject the E_TOSVAL event

   return (events_t) (-1 * (int) E_TOSVAL);
}

events_t 
flagPrecedence (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "flagPrecedence the word is %s\n", token);
#endif

   policy->flags |= RA_PREC_SET;
// The next token must contain the value for the precedence comparison
// force the token to advance and inject the E_PRECEDENCE event

   return (events_t) (-1 * (int) E_PRECEDENCE);
}

events_t 
setPrecValue(struct RaPolicyPolicyStruct *policy, char *token)
{
   int i;

#ifdef ARGUSDEBUG
   ArgusDebug (3, "setPrecValue the word is %s\n", token);
#endif

   for ( i = 0; precmap[i].len > 0; i++) {
      if (!strncasecmp(precmap[i].name, token, precmap[i].len)) {
         policy->precedence = precmap[i].value;
         return E_NULL;
      }
   }
#ifdef ARGUSDEBUG
   ArgusDebug (3, "%s is not a valid value for precedence\n", token);
#endif

   return E_NULL;
}

events_t 
setTOSvalue(struct RaPolicyPolicyStruct *policy, char *token)
{
   int i;

#ifdef ARGUSDEBUG
   ArgusDebug (3, "setTOSvalue the word is %s\n", token);
#endif

   for ( i = 0; tosmap[i].len > 0; i++) {
      if (!strncasecmp(tosmap[i].name, token, tosmap[i].len)) {
         policy->tos = tosmap[i].value;
         return E_NULL;
      }
   }
#ifdef ARGUSDEBUG
   ArgusDebug (3, "%s is not a valid value for tos\n", token);
#endif

   return E_NULL;
}

events_t 
flagDSCP (struct RaPolicyPolicyStruct *policy, char *token)
{
#ifdef ARGUSDEBUG
   ArgusDebug (3, "flagDSCP the word is %s\n", token);
#endif

   policy->flags |= RA_DSCP_SET;

// The next token must contain the value for the DSCP code point 
// force the token to advance and inject the E_DSCPVAL event

   return (events_t) (-1 * (int) E_DSCPVAL);
}

events_t 
setDSCPvalue(struct RaPolicyPolicyStruct *policy, char *token)
{
   int i;

#ifdef ARGUSDEBUG
   ArgusDebug (3, "setDSCPvalue the word is %s\n", token);
#endif

   for ( i = 0; DSCPmap[i].len > 0; i++) {
      if (!strncasecmp(DSCPmap[i].name, token, DSCPmap[i].len)) {
         policy->dscp = DSCPmap[i].value ;
         return E_NULL;
      }
   }
#ifdef ARGUSDEBUG
   ArgusDebug (3, "%s is not a valid DSCP code point name\n", token);
#endif

   return E_NULL;
}

events_t
setProtoParameter(struct RaPolicyPolicyStruct *policy, char *token)
{
   // There are two instances where naked integers show up at the end of an ACL entry
   // both the ICMP and IGMP protocols can have a type parameter expressed as an integer
   // at this point the token is an integer and we need to determine what it represents
   
#ifdef ARGUSDEBUG
   ArgusDebug (3, "setProtoParameters the word is %s the protocol is %d\n", token, policy->proto);
#endif

   switch (policy->proto) {
      case 1: return E_ICMPMSG;
      case 2: return E_IGMPTYPE;
      default: 
#ifdef ARGUSDEBUG
         ArgusDebug (3, "The raw value %s is not appropriate except for protocols ICMP and IGMP\n", token);
#endif


      return E_NULL;
   }
}

events_t 
tokenize (char * word) 
{
   int i;

   if ( word[0] == '\n')
      return E_EOL;

   if (isdigit(word[0])) {
      if (strpbrk ( word, ".") == NULL)
         return E_INTEGER;    

      return E_QUAD;

   } else {
      for (i = 0; strings[i].token != E_NULL; i++) {
         if (!strncasecmp(strings[i].symbol, word, strings[i].len) && (strlen(word) == strings[i].len)) 
            return strings[i].token;
      }
   }

   return E_RAWTEXT;
}

int
RaParsePolicy (struct ArgusParserStruct *parser, struct RaPolicyPolicyStruct **pol, char *buf)
{
   char *str = strdup(buf);
   struct RaPolicyPolicyStruct tpolicy;
   char *word = NULL;
   states_t theState, opState;
   events_t event;

   bzero ((char *)&tpolicy, sizeof(tpolicy));

   char labelStr[1024];
#ifdef ARGUSDEBUG
   ArgusDebug (3, "\n Parsing ACL entry: %s\n", buf);   
#endif

   theState = S_START;
   word = strtok(str, " \t\n\r");
   while (word != NULL) {
      event = tokenize(word);
      while ( event != E_NULL) {
         opState = theState;
         // E_LOCAL indicates that the next state is the same as the current state
         theState = stateTable[opState][event].nextState == S_LOCAL ? opState : stateTable[opState][event].nextState;
         if (theState < S_FINAL) 
#ifdef ARGUSDEBUG
            ArgusDebug (3, "entering state machine state = %d [%s] next = %d [%s] event = %d [%s] word = %s (%d)\n",
               opState, stateNames[opState], theState, stateNames[theState], event, eventNames[event], word, strlen(word));
#endif
         event = (stateTable[opState][event].fn)(&tpolicy, word);      
         if (theState == S_NONE) exit (1);
         // the processing of the event may have resulted in a new event being injected 
         // if so, that event either expects the next token or expects the token to remain
         // at its current value. We will use the convention that an event number less than
         // zero indicates the need to advance the token and pass the absolute value of the
         // injected event into the event loop. An positive event number not equal to 
         // E_NULL will result in the event being injected without advancing the token. And
         // an event of E_NULL will tokenize the next token and put the results on the event loop
         // First we handle the simple case of no special action, just get the next token and
         // inject the next event based on the next token.
         if (event == E_NULL) {
            if((word = strtok(NULL, " \t\n\r")) != NULL)
               event = tokenize(word);
         }
         // Otherwise we need to deal with an injected event so check if we need to advance the token
         // without actually tokenizing the result and make the event a non negative value
         if ( (int) event < 0) {
#ifdef ARGUSDEBUG
            ArgusDebug (3, "Found an injected state requiring a new token %d\n", event);
#endif

            event  = (events_t) (-1 * (int) event);
            word = strtok(NULL, " \t\n\r");
         }
         // Otherwise leave the token where it is and just inject the event as is
#ifdef ARGUSDEBUG
            ArgusDebug (3, "Found an injected state NOT requiring a new token %d\n", event);
#endif

      } // the inner while loop handles functions that inject events aka initiates local actions
   }

   if (str) free (str);

   if ((*pol = (struct RaPolicyPolicyStruct *) ArgusCalloc (1, sizeof (**pol))) != NULL) {
      bcopy ((char *)&tpolicy, (char *)*pol, sizeof(**pol));
      (*pol)->str = strdup(buf);
      if (RaGlobalPolicy != NULL){
         (*pol)->policyID = strdup(RaGlobalPolicy->policyID);
         labelStr[0] = 0;
         sprintf (labelStr, "ACL=%s_%s_Line_%4.4d", RaPolicy->flags & RA_PERMIT ? "Permit" : "Deny",
              RaPolicy->policyID, (int) RaPolicy->line);

         (*pol)->labelStr = strdup(labelStr);   
      }
   }

   return 1;
}

int
RaCheckPolicy (struct ArgusParserStruct *parser, struct ArgusRecordStruct *argus, struct RaPolicyPolicyStruct *policy)
{
   int retn = 0;
   char buffer[1024];
   struct RaPolicyPolicyStruct *base = policy;

//   scan through a doubly linked list of RaPolicyPolicyStruct 
//   comparing the criteria to the contents of the current flow
//      if this is not a protocol controlled by this policy then send the flow
//      if there is a match on a permit entry, then send the flow
//      if there is a match on a deny entry, then drop the flow
//      if there is no match, advance to the next item in the list
//      if there is no next item, drop the flow because of the implicit deny rule
//      Make the appropriate adjustments based on what you are displaying (deny or permit)

   if (policy) {
      while (policy) {
         if ((retn = RaMeetsPolicyCriteria (parser, argus, policy))) {
            if (retn == 1) { return 1;}  //do the permit stuff
            if (retn == 2) { return 0;}  //do the deny stuff
         }
         policy = policy->nxt;
      }
   }

// There is an implicit deny for all unmatched flows, deal with it as if it were an explicit deny

   if (parser->RaPolicyStatus & ARGUS_POLICY_LABEL_IMPLICIT) {
      policy = base;
      sprintf (buffer, "ACL=ImplicitDeny_%s", policy->policyID);
      ArgusAddToRecordLabel (parser, argus, buffer);
   }

   return((parser->RaPolicyStatus &  ARGUS_POLICY_SHOW_DENY) ? 1 : 0);
}


int
RaMeetsPolicyCriteria (struct ArgusParserStruct *parser, struct ArgusRecordStruct *argus, struct RaPolicyPolicyStruct *policy)
{

//   There are three possible outcomes:
//      There is a match for a permit ACL entry - send the packet = 1
//      There is a match for a deny ACL entry - drop the packet = 2
//      There is no match for a permit or a deny ACL entry - check the next entry = 0
//

   struct ArgusFlow *flow = (void *)argus->dsrs[ARGUS_FLOW_INDEX];
 
   if (flow != NULL) {
      arg_uint32 saddr = 0, daddr = 0;
      arg_uint16 sport = 0, dport = 0;
      u_char proto = flow->ip_flow.ip_p;
      saddr = flow->ip_flow.ip_src;
      daddr = flow->ip_flow.ip_dst;
      sport = flow->ip_flow.sport;
      dport = flow->ip_flow.dport;

#ifdef ARGUSDEBUG
      ArgusDebug (3, "RaMeetsPolicyCriteria for %s line %d: %s\n", 
         policy->policyID, policy->line, policy->str);
#endif

      if (policy->flags & (RA_COMMENT)) 
         return 0;

      if (policy->flags & (RA_PROTO_SET)) {
         if (policy->proto)
            if (proto != (u_char) policy->proto)
               return 0;
      }

      if (policy->flags & (RA_SRC_SET)) {
         if ((saddr & ~policy->src.mask) != policy->src.addr)
            return 0;
      }

      if (policy->flags & (RA_DST_SET)) {
         if ((daddr & ~policy->dst.mask) != policy->dst.addr)
            return 0;
      }

      if (policy->flags & (RA_SRCPORT_SET)) {
         switch (policy->src_action) {
            case  RA_EQ:
               if (sport != policy->src_port_low)
                  return 0;
               break;

            case  RA_LT:
               if (!(sport < policy->src_port_low))
                  return 0;
               break;

            case  RA_GT:
               if (!(sport > policy->src_port_low))
                  return 0;
               break;

            case RA_NEQ:
               if (sport == policy->src_port_low) 
                  return 0;
               break;

            case RA_RANGE:
               if (((sport < policy->src_port_low) || (sport > policy->src_port_hi)))
                  return 0;
               break;
         }
      } // end of  if testing for source port

      if (policy->flags & (RA_DSTPORT_SET)) {
         switch (policy->dst_action) {
            case  RA_EQ:
               if (dport != policy->dst_port_low)
                  return 0;
               break;
            case  RA_LT:
               if (!(dport < policy->dst_port_low))
                  return 0;
               break;
            case  RA_GT:
               if (!(dport > policy->dst_port_low))
                  return 0;
               break;
            case RA_NEQ:
               if (dport == policy->dst_port_low) 
                  return 0;
               break;

            case RA_RANGE:
               if (((dport < policy->dst_port_low) || (dport > policy->dst_port_hi))) 
                  return 0;
               break;
         }
      } // end of if testing for destination port

      if (policy->flags & (RA_EST_SET)) {
         int status = 0;
         struct ArgusNetworkStruct *net = (void *)argus->dsrs[ARGUS_NETWORK_INDEX];

         if (net != NULL) {
            switch (net->hdr.subtype) {
               case ARGUS_TCP_STATUS: {
                  struct ArgusTCPStatus *tcp = (struct ArgusTCPStatus *)&net->net_union.tcpstatus;
                  status = tcp->status;
                  break;
               }
               case ARGUS_TCP_PERF: {
                  struct ArgusTCPObject *tcp = (struct ArgusTCPObject *)&net->net_union.tcp;
                  status = tcp->status;
                  break;
               }
            }

            if (!(status & ARGUS_SAW_SYN_SENT))
               return (0);
         }
      }

      if (policy->flags & (RA_TCPFLG_SET)) {
         struct ArgusNetworkStruct *net;
         unsigned char sflags;
 
//  Checking the state of the TCP header flags
//
//   Since we are looking at flow records rather than individual packets we need to make some accommodations
//   The Cicso ACL checks for the specific flag's status regardless of the state of any other flag so there is
//   no need to check the state of any of the other flags.There is a credible arguement that the combination of
//   the SYN and the ACK flags is a special case that requires them both to be set in the same packet
//   Since we have aggregated flow data we need to check that combination against the special status indicator
//   rather than the summary of flags seen in the flow
//
//   We check for ESTABLISHED at this time as well since that is simply a description of a set of
//   TCP header flags that meet a specific requirement
//
         if ((net = (struct ArgusNetworkStruct *)argus->dsrs[ARGUS_NETWORK_INDEX]) != NULL) {
            switch (net->hdr.subtype) {
               case ARGUS_TCP_INIT:
               case ARGUS_TCP_STATUS:
               case ARGUS_TCP_PERF: {
                  struct ArgusTCPObject *tcp = (struct ArgusTCPObject *)&net->net_union.tcp;
                  sflags = tcp->src.flags;
                  break;
               }
            }

#ifdef ARGUSDEBUG
            ArgusDebug (3, "Policy wants TCPflags to be %x they are %x\n", policy->TCPflags, sflags);
#endif

//   for the Isolationists looking for only the specified flags,  uncomment the next line               
//          if ((policy->TCPflags ^ sflags) != 0 ) return 0;
            if ((policy->TCPflags & sflags) != (policy->TCPflags))
               return 0;
         }
      }
      
      if (policy->flags & (RA_ICMP_SET)) {
         unsigned char type, code;
         type = flow->icmp_flow.type;
         code = flow->icmp_flow.code;

         if (policy->ICMPtype !=  type)
            return 0;

// Cisco has created meta values that translate to type x  with any code value, we
// indicate that as a code value of ICMPCodeAny which indicates that a match of the type is
// the only match requirement, othewise we compare the code value

         if ((code < ICMPCodeAny) && (policy->ICMPcode != code))
            return 0;
      }
//
//   The tos, precedence,  and DSCP code points are closely coupled so we check them against the same
//   Argus value depending on how the ACL specifies the match
//
    {
         struct ArgusIPAttrStruct *ip1 = (void *)argus->dsrs[ARGUS_IPATTR_INDEX];

         if (ip1) {
            if ((policy->flags & (RA_TOS_SET)) && (policy->tos != ((ip1->src.tos >> 1) & 0x0f)))
                return 0;
             
            if ((policy->flags & (RA_DSCP_SET)) && (policy->dscp != (ip1->src.tos >> 2))) 
               return 0;
                
            
                if ((policy->flags & (RA_PREC_SET)) && (policy->precedence != (ip1->src.tos >> 5))) 
                          return(0);
       }
   }


      if (policy->flags & (RA_IGMP_SET)) {
         unsigned char type;
         type = flow->igmp_flow.type;

         if (policy->IGMPtype != type)
            return 0;
      }
      
      if (policy->flags & (RA_PREC_SET)) {
         // TBD Precedence value comparison
      }
      
      if (policy->flags & (RA_DSCP_SET)) {
         // The DSCP Code Point occupies the 6 MSB of the byte. The value in policy->dscp is already
         // left shifted to accommodate this. 
         // TBD - code being tested now
      }

      // If we make it to here we have a good match. Update the counts for this entry

      {
         struct ArgusMetricStruct *metric = (void *)argus->dsrs[ARGUS_METRIC_INDEX];
         policy->hitCount++;

         if (metric != NULL) {
            policy->hitPkts  += metric->src.pkts;
            policy->hitBytes += metric->src.bytes;
         }

         if ((policy->flags & RA_PERMIT) || (policy->flags & RA_DENY)) {
            if ((((parser->RaPolicyStatus & ARGUS_POLICY_LABEL_LOG)) && (policy->flags & RA_LOG_SET)) || 
                         (parser->RaPolicyStatus & ARGUS_POLICY_LABEL_ALL))
            ArgusAddToRecordLabel ( parser, argus, policy->labelStr);
         }

         if (policy->flags & (RA_PERMIT))
            return((parser->RaPolicyStatus &  ARGUS_POLICY_SHOW_DENY) ? 2 : 1);

         if (policy->flags & (RA_DENY))
            return((parser->RaPolicyStatus &  ARGUS_POLICY_SHOW_DENY) ? 1 : 2);
      }
   }

   return 0;
}