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
|
/*
** Copyright (C) 1998,1999,2000,2001 Martin Roesch <roesch@clark.net>
** Copyright (C) 2000,2001 Andrew R. Baker <andrewb@uab.edu>
**
** 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, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* $Id: parser.c,v 1.26 2001/10/29 02:23:02 roesch Exp $ */
#include "parser.h"
extern char *file_name;
extern char *file_line;
extern RuleListNode *RuleLists;
extern int rule_count;
extern ListHead *head_tmp;
/****************************************************************************
*
* Function: ProcessClassificationConfig(char *)
*
* Purpose: parses the classification configuration
*
* Arguments: filespec => the file specification
*
* Returns: void function
*
***************************************************************************/
void ProcessClassificationConfig(char *args)
{
char **ctoks;
int num_ctoks;
int i;
char *data;
int class_id = -1;
ClassTypes *newNode;
ClassTypes *current = pv.ct;
ctoks = mSplit(args, ",",3, &num_ctoks, '\\');
if(num_ctoks < 1)
{
ErrorMessage("WARNING %s(%d): You must supply at least ONE"
" classification arguement\n", file_name, file_line);
ErrorMessage("WARNING %s(%d): Ignoring configuration directive (%s: %d)"
"\"config classification: %s\"\n", file_name, file_line, args);
return;
}
data = ctoks[0];
while(isspace((int)*data)) data++;
while(current != NULL)
{
if(strlen(current->type) == strlen(data))
{
if(!strncasecmp(current->type, data, strlen(current->type)))
{
ErrorMessage("WARNING %s(%d): Duplicate classification \"%s\""
"found, ignoring this line\n", file_name, file_line, data);
return;
}
}
class_id = current->id;
current = current->next;
}
/* Create the new node */
if((newNode = (ClassTypes *) calloc(sizeof(ClassTypes), sizeof(char)))
== NULL)
{
FatalError("ERROR => Can't add a new class type, calloc failed\n");
}
newNode->type = strdup(ctoks[0]);
newNode->id = ++class_id;
if(num_ctoks == 2)
{
data = ctoks[1];
while (isspace((int)*data)) data++;
newNode->priority = atoi(data);
data = ctoks[0];
while (isspace((int)*data)) data++;
newNode->name = strdup(data);
}
else
{
data = ctoks[1];
while (isspace((int)*data)) data++;
newNode->name = strdup(data);
data = ctoks[2];
while (isspace((int)*data)) data++;
newNode->priority = atoi(data);
}
/* Add the node to the list */
if(pv.ct == NULL)
{
pv.ct = newNode;
}
else
{
current = pv.ct;
while(current->next != NULL)
current = current->next;
current->next = newNode;
}
for(i=0; i<num_ctoks; i++)
{
free(ctoks[i]);
}
#ifdef DEBUG
printf("classification list:\n");
current = pv.ct;
i = 0;
while(current != NULL)
{
printf("Node %d type: %s name: %s pri: %d\n", i, current->type,
current->name, current->priority);
i++;
current = current->next;
}
#endif
return;
}
/****************************************************************************
*
* Function: ProcessAlertFileOption(char *)
*
* Purpose: define the alert file
*
* Arguments: filespec => the file specification
*
* Returns: void function
*
***************************************************************************/
void ProcessAlertFileOption(char *filespec)
{
pv.alert_filename = ProcessFileOption(filespec);
#ifdef DEBUG
printf("alertfile set to: %s\n", pv.alert_filename);
#endif
return;
}
char *ProcessFileOption(char *filespec)
{
char *filename;
char buffer[STD_BUF];
if(filespec == NULL)
{
FatalError("ERROR: no arguement in this file option, remove extra ':' at the end of the alert option\n");
}
/* look for ".." in the string and complain and exit if it is found */
if(strstr(filespec, "..") != NULL)
{
FatalError("ERROR: file definition contains \"..\". Do not do that!\n");
}
if(filespec[0] == '/')
{
/* absolute filespecs are saved as is */
filename = strdup(filespec);
}
else
{
/* relative filespec is considered relative to the log directory */
/* or /var/log if the log directory has not been set */
if(pv.log_flag)
{
strlcpy(buffer, pv.log_dir, STD_BUF);
}
else
{
strlcpy(buffer, "/var/log/snort", STD_BUF);
}
strlcat(buffer, "/", STD_BUF - strlen(buffer));
strlcat(buffer, filespec, STD_BUF - strlen(buffer));
filename = strdup(buffer);
}
if(!pv.quiet_flag)
printf("ProcessFileOption: %s\n", filename);
return filename;
}
void ParseConfig(char *rule)
{
char **toks;
char **config_decl;
char *args = NULL;
char *config;
int num_toks;
toks = mSplit(rule, ":", 2, &num_toks, 0);
if(num_toks > 1)
{
args = toks[1];
}
config_decl = mSplit(toks[0], " ", 2, &num_toks, '\\');
if(num_toks != 2)
{
FatalError("Error parsing config: %s\n", rule);
}
config = config_decl[1];
#ifdef DEBUG
printf("Config: %s\n", config);
printf("Args: %s\n", args);
#endif
if(!strcasecmp(config, "order"))
{
if(!pv.rules_order_flag)
OrderRuleLists(args);
else
printf("Commandline option overiding rule file config\n");
return;
}
else if(!strcasecmp(config, "alertfile"))
{
toks = mSplit(args, " ", 1, &num_toks, 0);
ProcessAlertFileOption(toks[0]);
return;
}
else if(!strcasecmp(config, "classification"))
{
ProcessClassificationConfig(args);
return;
}
else if(!strcasecmp(config, "decode_arp"))
{
/* show ARP packets */
DebugMessage(DEBUG_INIT, "Show ARP is active\n");
pv.showarp_flag = 1;
return;
}
else if(!strcasecmp(config, "dump_chars_only"))
{
/* dump the application layer as text only */
DebugMessage(DEBUG_INIT, "Character payload dump set\n");
pv.char_data_flag = 1;
return;
}
else if(!strcasecmp(config, "dump_payload"))
{
/* dump the application layer */
DebugMessage(DEBUG_INIT, "Payload dump set\n");
pv.data_flag = 1;
return;
}
else if(!strcasecmp(config, "decode_data_link"))
{
/* dump the data link layer as text only */
DebugMessage(DEBUG_INIT, "Decode DLL set\n");
pv.show2hdr_flag = 1;
return;
}
else if(!strcasecmp(config, "bpf_file"))
{
/* Read BPF filters from a file */
DebugMessage(DEBUG_INIT, "BPF file set\n");
/* suck 'em in */
pv.pcap_cmd = read_infile(args);
return;
}
else if(!strcasecmp(config, "set_gid"))
{
#ifdef WIN32
FatalError("[!] ERROR: Setting the group id is not supported in the WIN32 port of snort!\n");
#else
if((groupname = calloc(strlen(args) + 1, 1)) == NULL)
FatalPrintError("calloc");
bcopy(args, groupname, strlen(args));
if((groupid = atoi(groupname)) == 0)
{
gr = getgrnam(groupname);
if(gr == NULL)
{
ErrorMessage("ERROR %s(%d) => Group \"%s\" unknown\n",
groupname);
}
groupid = gr->gr_gid;
}
#endif
return;
}
else if(!strcasecmp(config, "daemon"))
{
DebugMessage(DEBUG_INIT, "Daemon mode flag set\n");
pv.daemon_flag = 1;
pv.quiet_flag = 1;
}
else if(!strcasecmp(config, "ghetto_msg"))
{
if(!strncasecmp(args, "basic", 5))
{
pv.ghetto_msg_flag = GHETTO_BASIC;
}
else if(!strncasecmp(args, "url", 3))
{
pv.ghetto_msg_flag = GHETTO_URL;
}
else
{
ErrorMessage("ERROR %s(%d) => Unknown command line Ghetto "
"option: %s\n", file_name, file_line, args);
return;
}
return;
}
else if(!strcasecmp(config, "reference_net"))
{
GenHomenet(args);
return;
}
else if(!strcasecmp(config, "interface"))
{
if(ifr_count == MAX_INTERFACES)
{
ErrorMessage(
"\nMaximum number of interfaces (%i) exceeded."
"Please recompile to extend it (oops)\n",
MAX_INTERFACES);
return;
}
pv.interfaces[ifr_count] = (char *) malloc(strlen(args) + 1);
strlcpy(pv.interfaces[ifr_count], args, strlen(args)+1);
ifr_count++;
DebugMessage(DEBUG_INIT, "Interface = %s\n", PRINT_INTERFACE(pv.interfaces[ifr_count - 1]));
if(!ifr_count)
ifr_count++;
if(!pv.readmode_flag)
{
if(pd != NULL)
{
pcap_close(pd);
}
DebugMessage(DEBUG_INIT, "Opening interface: %s\n",
PRINT_INTERFACE(pv.interfaces[0]));
/* open up our libpcap packet capture interface */
InitializeInterfaces();
}
return;
}
else if(!strcasecmp(config, "alert_with_interface_name"))
{
pv.alert_interface_flag = 1;
return;
}
else if(!strcasecmp(config, "logdir"))
{
printf("Found logdir config directive (%s)\n", args);
strlcpy(pv.log_dir, args, STD_BUF);
DebugMessage(DEBUG_INIT, "Log directory = %s\n", pv.log_dir);
pv.log_flag = 1;
return;
}
else if(!strcasecmp(config, "umask"))
{
char *p;
long val = 0;
int umaskchange = 1;
int defumask = 0;
umaskchange = 0;
val = strtol(args, &p, 8);
if (*p != '\0' || val < 0 || (val & ~FILEACCESSBITS))
{
ErrorMessage("ERROR: bad umask %s\n", args);
return;
}
else
{
defumask = val;
}
/* if the umask arg happened, set umask */
if (umaskchange)
{
umask(077); /* set default to be sane */
}
else
{
umask(defumask);
}
return;
}
else if(!strcasecmp(config, "pkt_count"))
{
pv.pkt_cnt = atoi(args);
DebugMessage(DEBUG_INIT, "Exiting after %d packets\n", pv.pkt_cnt);
return;
}
else if(!strcasecmp(config, "nolog"))
{
pv.nolog_flag = 1;
pv.log_cmd_override = 1;
return;
}
else if(!strcasecmp(config, "obfuscate"))
{
pv.obfuscation_flag = 1;
return;
}
else if(!strcasecmp(config, "no_promisc"))
{
pv.promisc_flag = 0;
DebugMessage(DEBUG_INIT, "Promiscuous mode disabled!\n");
return;
}
else if(!strcasecmp(config, "snaplen"))
{
pv.pkt_snaplen = atoi(optarg);
DebugMessage(DEBUG_INIT, "Snaplength of Packets set to: %d\n",
pv.pkt_snaplen);
return;
}
else if(!strcasecmp(config, "quiet"))
{
pv.quiet_flag = 1;
return;
}
else if(!strcasecmp(config, "read_bin_file"))
{
strlcpy(pv.readfile, optarg, STD_BUF);
pv.readmode_flag = 1;
DebugMessage(DEBUG_INIT, "Opening file: %s\n", pv.readfile);
/* open the packet file for readback */
OpenPcap(pv.readfile, 0);
return;
}
else if(!strcasecmp(config, "chroot"))
{
#ifdef WIN32
FatalError("[!] ERROR: Setting the chroot directory is not supported in the WIN32 port of snort!\n");
#else
if((chrootdir = calloc(strlen(args) + 2, 1)) == NULL)
FatalPrintError("calloc");
/* make sure '/' is appended */
sprintf(chrootdir, "%s/", args);
if(chrootdir != NULL)
{
if(chdir(chrootdir) < 0)
FatalError("Can not chdir to \"%s\"\n", chrootdir);
if(chroot(chrootdir) < 0)
FatalError("Can not chroot to %s\n", chrootdir);
if(chdir("/") < 0)
FatalError("Can not chdir to \"/\"\n");
free(chrootdir);
chrootdir = NULL; /* we don't need chrootdir anymore so all
* other routines should use fullpath. */
}
#endif
return;
}
else if(!strcasecmp(config, "checksum_mode"))
{
if(!strncasecmp(args, "all", 3))
{
pv.checksums_mode = DO_IP_CHECKSUMS | DO_TCP_CHECKSUMS |
DO_UDP_CHECKSUMS | DO_ICMP_CHECKSUMS;
}
else if(!strncasecmp(args, "noip", 4))
{
pv.checksums_mode = DO_TCP_CHECKSUMS | DO_UDP_CHECKSUMS |
DO_ICMP_CHECKSUMS;
}
else if(!strncasecmp(args, "notcp", 5))
{
pv.checksums_mode = DO_IP_CHECKSUMS | DO_UDP_CHECKSUMS |
DO_ICMP_CHECKSUMS;
}
else if(!strncasecmp(args, "noudp", 5))
{
pv.checksums_mode = DO_TCP_CHECKSUMS | DO_TCP_CHECKSUMS |
DO_ICMP_CHECKSUMS;
}
else if(!strncasecmp(args, "noicmp", 6))
{
pv.checksums_mode = DO_IP_CHECKSUMS | DO_UDP_CHECKSUMS |
DO_TCP_CHECKSUMS;
}
if(!strncasecmp(args, "none", 4))
{
pv.checksums_mode = 0;
}
return;
}
else if(!strcasecmp(config, "set_uid"))
{
#ifdef WIN32
FatalError("[!] ERROR: Setting the user id is not supported in the WIN32 port of snort!\n");
#else
if((username = calloc(strlen(args) + 1, 1)) == NULL)
FatalPrintError("malloc");
bcopy(args, username, strlen(args));
if((userid = atoi(username)) == 0)
{
pw = getpwnam(username);
if(pw == NULL)
FatalError("User \"%s\" unknown\n", username);
userid = pw->pw_uid;
}
else
{
pw = getpwuid(userid);
if(pw == NULL)
FatalError(
"Can not obtain username for uid: %lu\n",
(u_long) userid);
}
if(groupname == NULL)
{
char name[256];
snprintf(name, 255, "%lu", (u_long) pw->pw_gid);
if((groupname = calloc(strlen(name) + 1, 1)) == NULL)
{
FatalPrintError("malloc");
}
groupid = pw->pw_gid;
}
DebugMessage(DEBUG_INIT, "UserID: %lu GroupID: %lu\n",
(unsigned long) userid, (unsigned long) groupid);
#endif
return;
}
else if(!strcasecmp(config, "utc"))
{
pv.use_utc = 1;
return;
}
else if(!strcasecmp(config, "verbose"))
{
pv.verbose_flag = 1;
DebugMessage(DEBUG_INIT, "Verbose Flag active\n");
return;
}
else if(!strcasecmp(config, "dump_payload_verbose"))
{
DebugMessage(DEBUG_INIT, "Verbose packet bytecode dumps enabled\n");
pv.verbose_bytedump_flag = 1;
return;
}
else if(!strcasecmp(config, "show_year"))
{
pv.include_year = 1;
DebugMessage(DEBUG_INIT, "Enabled year in timestamp\n");
return;
}
else if(!strcasecmp(config, "stateful")) /* this one's for Johnny! */
{
pv.assurance_mode = ASSURE_EST;
return;
}
FatalError("Error: Unknown config: %s\n", config);
return;
}
/* verify that we are not reusing some other keyword */
int checkKeyword(char *keyword)
{
RuleListNode *node = RuleLists;
if(RuleType(keyword) != RULE_UNKNOWN)
{
return 1;
}
/* check the declared ruletypes now */
while(node != NULL)
{
if(!strcasecmp(node->name, keyword))
{
return 1;
}
node = node->next;
}
return 0;
}
void ParseRuleTypeDeclaration(FILE* rule_file, char *rule)
{
char *input;
char *keyword;
char **toks;
int num_toks;
int type;
int rval = 1;
int i;
ListHead *listhead = NULL;
toks = mSplit(rule, " ", 10, &num_toks, 0);
keyword = strdup(toks[1]);
/* Verify keyword is unique */
if(checkKeyword(keyword))
{
FatalError("ERROR line %s (%d): Duplicate keyword: %s\n",
file_name, file_line, keyword);
}
#ifdef DEBUG
printf("Declaring new rule type: %s\n", keyword);
#endif
if(num_toks > 2)
{
if(strcasecmp("{", toks[2]) != 0)
{
FatalError("ERROR line %s (%d): Syntax error: %s\n",
file_name, file_line, rule);
}
}
else
{
input = ReadLine(rule_file);
for(i=0;i<num_toks;i++)
{
free(toks[i]);
}
toks = mSplit(input, " ", 2, &num_toks, 0);
free(input);
}
input = ReadLine(rule_file);
for(i=0;i<num_toks;i++)
{
free(toks[i]);
}
toks = mSplit(input, " ", 10, &num_toks, 0);
/* read the type field */
if(!strcasecmp("type", toks[0]))
{
type = RuleType(toks[1]);
/* verify it is a valid ruletype */
if((type != RULE_LOG) && (type != RULE_PASS) && (type != RULE_ALERT) &&
(type != RULE_ACTIVATE) && (type != RULE_DYNAMIC))
{
FatalError("ERROR line %s (%d): Invalid type for rule type declaration: %s\n", file_name, file_line, toks[1]);
}
#ifdef DEBUG
printf("\ttype(%i): %s\n", type, toks[1]);
#endif
if(type == RULE_PASS)
{
rval = 0;
}
listhead = CreateRuleType(keyword, type, rval, NULL);
}
else
{
FatalError("ERROR line %s (%d): Type not defined for rule file declaration: %s\n", file_name, file_line, keyword);
}
free(input);
input = ReadLine(rule_file);
for(i=0;i<num_toks;i++)
{
free(toks[i]);
}
toks = mSplit(input, " ", 2, &num_toks, 0);
while(strcasecmp("}", toks[0]) != 0)
{
if(RuleType(toks[0]) != RULE_OUTPUT)
{
FatalError("ERROR line %s (%d): Not an output plugin declaration: %s\n", file_name, file_line, keyword);
}
head_tmp = listhead;
ParseOutputPlugin(input);
head_tmp = NULL;
free(input);
input = ReadLine(rule_file);
for(i=0;i<num_toks;i++)
{
free(toks[i]);
}
toks = mSplit(input, " ", 2, &num_toks, 0);
}
return;
}
/* Adapted from ParseRule in rules.c */
void ParseDeclaredRuleType(char *rule)
{
char **toks;
int num_toks;
RuleListNode *node;
int protocol;
int i;
RuleTreeNode proto_node;
toks = mSplit(rule, " ", 10, &num_toks, 0);
node = RuleLists;
while(node != NULL)
{
if(!strcasecmp(node->name, toks[0]))
break;
node = node->next;
}
/* if we did not find a match, then there is no such ruletype */
if(node == NULL)
{
FatalError("ERROR line %s (%d) => Unknown rule type: %s\n",
file_name, file_line, toks[0]);
}
printf("[**] Rule start\n");
printf("Rule id: %s\n", toks[0]);
printf("Rule type: ");
switch(node->mode)
{
case RULE_PASS:
printf("Pass\n");
break;
case RULE_LOG:
printf("Log\n");
break;
case RULE_ALERT:
printf("Alert\n");
break;
default:
printf("Unknown\n");
}
/* the rest of this function is almost identical to code in ParseRule */
bzero((char *) &proto_node, sizeof(RuleTreeNode));
proto_node.type = node->mode;
/* set the rule protocol */
protocol = WhichProto(toks[1]);
/* Process the IP address and CIDR netmask */
/* changed version 1.2.1 */
/*
* "any" IP's are now set to addr 0, netmask 0, and the normal rules are
* applied instead of checking the flag
*/
/*
* if we see a "!<ip number>" we need to set a flag so that we can
* properly deal with it when we are processing packets
*/
/*
if( *toks[2] == '!' )
{
proto_node.flags |= EXCEPT_SRC_IP;
ParseIP(&toks[2][1], (u_long *) & proto_node.sip,
(u_long *) & proto_node.smask);
}
else
{
ParseIP(toks[2], (u_long *) & proto_node.sip,
(u_long *) & proto_node.smask);
}*/
ProcessIP(toks[2], &proto_node, SRC);
/* do the same for the port */
if(ParsePort(toks[3], (u_short *) & proto_node.hsp,
(u_short *) & proto_node.lsp, toks[1],
(int *) &proto_node.not_sp_flag))
{
proto_node.flags |= ANY_SRC_PORT;
}
if(proto_node.not_sp_flag)
proto_node.flags |= EXCEPT_SRC_PORT;
/* New in version 1.3: support for bidirectional rules */
/*
* this checks the rule "direction" token and sets the bidirectional flag
* if the token = '<>'
*/
if(!strncmp("<>", toks[4], 2))
{
#ifdef DEBUG
printf("Bidirectional rule!\n");
#endif
proto_node.flags |= BIDIRECTIONAL;
}
/* changed version 1.2.1 */
/*
* "any" IP's are now set to addr 0, netmask 0, and the normal rules are
* applied instead of checking the flag
*/
/*
* if we see a "!<ip number>" we need to set a flag so that we can
* properly deal with it when we are processing packets
*/
/*
if( *toks[5] == '!' )
{
#ifdef DEBUG
printf("setting exception flag for dest IP\n");
#endif
proto_node.flags |= EXCEPT_DST_IP;
ParseIP(&toks[5][1], (u_long *) & proto_node.dip,
(u_long *) & proto_node.dmask);
}
else
ParseIP(toks[5], (u_long *) & proto_node.dip,
(u_long *) & proto_node.dmask);
*/
ProcessIP(toks[5], &proto_node, DST);
if(ParsePort(toks[6], (u_short *) & proto_node.hdp,
(u_short *) & proto_node.ldp, toks[1],
(int *) &proto_node.not_dp_flag))
{
proto_node.flags |= ANY_DST_PORT;
}
if(proto_node.not_dp_flag)
proto_node.flags |= EXCEPT_DST_PORT;
#ifdef DEBUG
printf("proto_node.flags = 0x%X\n", proto_node.flags);
#endif
if(&proto_node == NULL)
printf("NULL proto_node\n");
ProcessHeadNode(&proto_node, node->RuleList, protocol);
rule_count++;
ParseRuleOptions(rule, node->mode, protocol);
for(i=0;i<num_toks;i++)
{
free(toks[i]);
}
return;
}
/* adapted from ParseRuleFule in rules.c */
char *ReadLine(FILE * file)
{
char buf[STD_BUF];
char *index;
bzero((char *) buf, STD_BUF);
/*
* Read a line from file and return it. Skip over lines beginning with #,
* ;, or a newline
*/
while((fgets(buf, STD_BUF, file)) != NULL)
{
file_line++;
index = buf;
#ifdef DEBUG2
printf("Got line %s (%d): %s", file_name, file_line, buf);
#endif
/* if it's not a comment or a <CR>, we return it */
if((*index != '#') && (*index != 0x0a) && (*index != ';')
&& (index != NULL))
{
/* advance through any whitespace at the beginning of ther line */
while(isspace((int) *index))
++index;
/* return a copy of the line */
return strdup(index);
}
}
return NULL;
}
|