/* * Routine to parse default.W * * Uses insert_rules() to add rules to linked list * * This routine finds the appropriate section of each rule, and sends it off * to the appropriate function to be inserted into the linked list. */ #include "rules.h" void find_rules(char *rulesC) { char buf[LNSIZE]; /* temp buffer space */ char action[LNSIZE]; /* array of action pointers */ char install[LNSIZE]; /* array of gw install pointers */ char time[LNSIZE]; /* array of time allowed pointers */ char *source[LNSIZE]; /* array of src address pointers */ char *dest[LNSIZE]; /* array of dst address pointers */ char *service[LNSIZE]; /* array of svc pointers */ char *rulenum; /* Current rule number */ char *lineptr; /* Marks current position on line */ /* File pointers */ FILE *fp; /* number of rules processed */ int rulecount=0; /* allocate space for rule pointer */ if((rulenum = (char *)malloc(LNSIZE * sizeof(char))) == NULL) { fprintf(stderr,"malloc failed\n"); exit(1); } /* Read default.W from command line */ if((fp = fopen(rulesC,"r")) == NULL) { fprintf(stderr,"Unable to open %s\n",rulesC); exit(1); } /* Main loop that continues until EOF */ while((fgets(buf,LNSIZE,fp)) != NULL) { /* zero the strings, so we can check later for null */ source[rulecount-1] = '\0'; dest[rulecount-1] = '\0'; service[rulecount-1] = '\0'; lineptr = strchr(buf,':'); if(lineptr != NULL) { if(strstr(lineptr,"rule ")) { rulecount++; } /* process source hostnames */ if(strstr(lineptr,"src ")) { get_source(fp,&source[rulecount-1],rulecount); } /* process destination hostnames */ if(strstr(lineptr,"dst ")) { get_dest(fp,&dest[rulecount-1],rulecount); } /* process services for this rule */ if(strstr(lineptr,"services ")) { get_service(fp,&service[rulecount-1],rulecount); } /* action is usually 'accept' */ if(strstr(lineptr,"action ")) { get_action(fp,action); } /* determine install gateway hostname */ if(strstr(lineptr,"install ")) { get_install(fp,install); } /* time rule is in effect */ if(strstr(lineptr,"time ")) { get_time(fp,time); } /* if all three aren't null, find the ones that need to be * inserted */ if((time[0] != '\0') && (install[0] != '\0') && (action[0] != '\0')) { insert_three(time,install,action,rulecount); } } } } /* * NAME: get_source * * Find the source hostnames in the rule, and insert into the linked list */ void get_source(FILE *fp, char *source[], int rule) { /* Find source address field */ int srccount=0; int j,l; char *lineptr; char buf[LNSIZE]; /* one rule may have many src addresses. Continue until next * field delimiter is found */ while((fgets(buf,LNSIZE,fp)) != NULL) { lineptr = strchr(buf,':'); j=l=0; if(lineptr == NULL) return; if((source[srccount] = (char *)malloc(LNSIZE)) == NULL) { fprintf(stderr,"get_source: malloc failed\n"); exit(1); } /* get rid of beginning ':' and ' ' */ if(lineptr[1] == ' ') lineptr += 2; /* parse string to remove colons found on line */ while(j < (strlen(lineptr)-1)) { *(source[srccount] + l) = '\0'; /* strip off some of the characters we're not interested in */ if((lineptr[j] != ':') && ((lineptr[j] != '"') && (lineptr[j] != '(') && (lineptr[j] != ')'))) { *(source[srccount] + l) = *(lineptr + j); l++; } j++; } if(source[srccount] != NULL) { update_source(source[srccount],rule,srccount); } /* increment number of src addresses for this rule */ srccount++; source[srccount] = '\0'; } } void get_dest(FILE *fp, char *dest[],int rule) { /* Find destination address field */ int dstcount=0; int j,l; char *lineptr; char buf[LNSIZE]; /* one rule may have many dst addresses. Continue until next * field delimiter is found */ while((fgets(buf,LNSIZE,fp)) != NULL) { lineptr = strchr(buf,':'); j=l=0; if(lineptr == NULL) return; if((dest[dstcount] = (char *)malloc(LNSIZE)) == NULL) fprintf(stderr,"malloc failed\n"); /* get rid of beginning ':' and ' ' */ if(lineptr[1] == ' ') lineptr += 2; /* parse string removing colons from line */ while(j < (strlen(lineptr)-1)) { /* strip off some of the characters we're not interested in */ if((lineptr[j] != ':') && ((lineptr[j] != '"') && (lineptr[j] != '(') && (lineptr[j] != ')'))) { *(dest[dstcount] + l) = *(lineptr + j); l++; } j++; } if(dest[dstcount] != NULL) update_dest(dest[dstcount],rule,dstcount); /* increment number of destination addresses found */ dstcount++; dest[dstcount] = '\0'; } } void get_service(FILE *fp, char *service[],int rule) { char *lineptr; char buf[LNSIZE]; int j,l; int svccount=0; service[svccount] = '\0'; /* Find services fields */ svccount=0; while((fgets(buf,LNSIZE,fp)) != NULL) { lineptr = strchr(buf,':'); j=l=0; if(lineptr == NULL) return; /* get rid of beginning ':' and ' ' */ if(lineptr[1] == ' ') lineptr += 2; if((service[svccount] = (char *)malloc(LNSIZE)) == NULL) { fprintf(stderr,"malloc failed\n"); exit(1); } while(j < (strlen(lineptr)-1)) { /* strip off some of the characters we're not interested in */ if((lineptr[j] != ':') && ((lineptr[j] != '"') && (lineptr[j] != '(') && (lineptr[j] != ')'))) { *(service[svccount] + l) = *(lineptr + j); l++; } j++; } update_service(service[svccount],rule,svccount); svccount++; } } void get_action(FILE *fp, char action[]) { /* Find action fields. This one is a little more involved, as it * has multiple combinations of valid entries. We are searching * for 'drop' 'accept' 'reject' and 'User' entries for now. */ char *lineptr; char buf[LNSIZE]; action[0] = '\0'; while((fgets(buf,LNSIZE,fp)) != NULL) { lineptr = strchr(buf,':'); if((lineptr == NULL) && (action[0] != '\0')) { return; } if(strstr(lineptr,"drop")) { strcpy(action,"drop"); return; } if(strstr(lineptr,"accept")) { strcpy(action,"accept"); return; } if(strstr(lineptr,"User Auth")) { strcpy(action,"User Auth"); return; } if(strstr(lineptr,"reject")) { strcpy(action,"reject"); return; } } } void get_install(FILE *fp, char install[]) { /* Find install fields. This field also has multiple possible * valid combations. Accept 'gateways' in place of 'Gateways' and * assume for now all else is valid data */ char *lineptr; int j,l; char buf[LNSIZE]; while((fgets(buf,LNSIZE,fp)) != NULL) { lineptr = strchr(buf,':'); j=l=0; if((lineptr == NULL) && (install[0] != '\0')) return; /* need a cheap way to compensate for 'gateways' being one too many chars */ if(strstr(lineptr,"Gateways")) { strcpy(install,"gatewys"); return; } while(j < (strlen(lineptr)-1)) { /* strip off some of the characters we're not interested in */ if((lineptr[j] != ':') && ((lineptr[j] != '"') && (lineptr[j] != '(') && (lineptr[j] != ')') && (lineptr[j] != ' '))) { install[l] = *(lineptr + j); l++; install[l] = '\0'; } j++; } } } void get_time(FILE *fp, char time[]) { char *lineptr; int j,l; char buf[LNSIZE]; while((fgets(buf,LNSIZE,fp)) != NULL) { lineptr = strchr(buf,':'); j=l=0; if((lineptr == NULL) && (time[0] != '\0')) { return; } while(j < (strlen(lineptr)-1)) { if((lineptr[j] != ':') && (lineptr[j] != ' ')) { time[l] = *(lineptr + j); l++; time[l] = '\0'; } j++; } } }