/* * default.W linked list routine */ #include "rules.h" #include "objects.h" /* * Name: initrules() * * Description: This routine allocates space for the head and tail of the * `rulenum' structure that is used to store information about the rules * that are contained in default.W */ void initrules(void) { /* allocate space for head placeholder */ head = (rulenum *) malloc(sizeof(rulenum)); /* allocate space for first entry in array */ (head->src)[0] = (char *) malloc(LNSIZE * sizeof(char)); (head->dst)[0] = (char *) malloc(LNSIZE * sizeof(char)); (head->svc)[0] = (char *) malloc(LNSIZE * sizeof(char)); /* initialize head with marker data */ strcpy((head->src)[0],"AAAA"); strcpy((head->dst)[0],"AAAA"); strcpy((head->svc)[0],"AAAA"); strcpy(head->action,"AAAA"); strcpy(head->install,"AAAA"); strcpy(head->time,"AAAA"); /* allocate space for tail placeholder */ tail = (rulenum *) malloc(sizeof(rulenum)); /* allocate space for last entry in array */ (tail->src)[0] = (char *) malloc(LNSIZE * sizeof(char)); (tail->dst)[0] = (char *) malloc(LNSIZE * sizeof(char)); (tail->svc)[0] = (char *) malloc(LNSIZE * sizeof(char)); /* initilize tail with marker data */ strcpy((tail->src)[0],"ZZZZ"); strcpy((tail->dst)[0],"ZZZZ"); strcpy((tail->svc)[0],"ZZZZ"); strcpy(tail->action,"ZZZZ"); strcpy(tail->install,"ZZZZ"); strcpy(tail->time,"ZZZZ"); /* tail has no links */ tail->src_link=NULL; tail->dst_link=NULL; tail->svc_link=NULL; tail->action_link=NULL; tail->install_link=NULL; tail->time_link=NULL; /* point head at tail */ head->src_link=tail; head->dst_link=tail; head->svc_link=tail; head->action_link=tail; head->install_link=tail; head->time_link=tail; /* intilize head and tail counters */ head->rulecount = -1; tail->rulecount = 10000; tail->count_link = NULL; head->count_link = tail; } /* Name: dumprules() * * Description: This routine is used to simply print out the list of rules in a * format that is more easily viewable. It is intended to be printed in * landscape format, instead of portrait mode * * int srcdst: * Use 0 for source * Use 1 for destination * Use 3 for both */ void dumprules(int verbose, int srcdst) { /* counter for rule number */ int subrule = 1; /* control boldface on every other rule */ /* print primary rule entry */ printf("--------------------------------------------------------------------------------------------------------------------\n"); printf("%03d",prev->rulecount); (prev->src[0] != '\0') ? printf("%29s",prev->src[0]) : printf("%29s"," "); (prev->dst[0] != '\0') ? printf("%29s",prev->dst[0]) : printf("%29s"," "); (prev->svc[0] != '\0') ? printf("%28s",prev->svc[0]) : printf("%28s"," "); (prev->action[0] != '\0') ? printf("%11s",prev->action) : printf("%11s"," "); (prev->install[0] != '\0') ? printf("%9s",prev->install) : printf("%9s"," "); (prev->time[0] != '\0') ? printf("%6s",prev->time) : printf("%6s"," "); printf("\n"); /* continue printing subrules until there are no more */ while((prev->dst[subrule] != '\0') || (prev->src[subrule] != '\0') || (prev->svc[subrule] != '\0')) { printf("%3c",' '); (prev->src[subrule] != '\0') ? printf("%29s",prev->src[subrule]) : printf("%29s"," "); (prev->dst[subrule] != '\0') ? printf("%29s",prev->dst[subrule]) : printf("%29s"," "); (prev->svc[subrule] != '\0') ? printf("%28s",prev->svc[subrule]) : printf("%28s"," "); printf("\n"); subrule++; } /* reset number of subrules for next iteration, * and update pointer to next record */ subrule=1; } /* * Name: dumpsource() * * Description: This routine is a wrapper for printing the basic ruleset. * Also, it can be used to match `netname' if it is not null. * * int srcdst: * Use 0 for source * Use 1 for destination * Use 2 for services * Anything else is siliently ignored */ void dumpsource(char *netname, int verbose, int srcdst) { /* used for searching object list for specific pattern */ rulenum *rule; /* if 'netname' is defined, search list until we find this one specifically. * Then restrict printing to just this specific object * Used with -n option */ /* print field description information */ printf("%-8s%24s%29s%28s%11s%9s%6s\n","Rule Num","Source Name", "Dest Name","Service","Action","Install","Time"); if(netname != NULL) { /* start both off at the top of the structure */ rule = head->count_link; /* print the individual object we are looking for */ while(rule->count_link != NULL) { /* srcdst == 0 for source */ if(srcdst == 0) { if((rule->src != NULL) && (strcasecmp(rule->src[0],netname) == 0)) { /* set pointer to correct spot in the object list */ prev = rule; /* print out this specific entry we just found */ dumpnet(netname,verbose); dumprules(verbose,srcdst); printf("\n"); } } /* srcdst == 1 for destination */ if(srcdst == 1) { if((rule->dst != NULL) && (strcasecmp(rule->dst[0],netname) == 0)) { /* set pointer to correct spot in the object list */ prev = rule; /* print out this specific entry we just found */ dumprules(verbose,srcdst); dumpnet(netname,verbose); printf("\n"); } } /* srcdst == 2 for services */ if(srcdst == 2) { if((rule->svc != NULL) && (strcasecmp(rule->svc[0],netname) == 0)) { /* set pointer to correct spot in the object list */ prev = rule; /* print out this specific entry we just found */ dumprules(verbose,srcdst); dumpnet(netname,verbose); printf("\n"); } } rule = rule->count_link; } return; } /* There is no specific object to search for -- print the entire list */ if(netname == NULL) { prev = head->count_link; /* continue printing until end of list */ while(prev->count_link != NULL) { dumprules(verbose,3); prev = prev->src_link; } } } /* Name: update_source( char *src, int rulecount, int indexnum) * * Description: This routine adds another rule to the `rulenum' linked list * structure. */ void update_source(char *src, int rulecount, int indexnum) { if(indexnum == 0) { prev = head; /* create another pointer to the head of the list as a start point */ present = head->count_link; next = (rulenum *) malloc(sizeof(rulenum)); next->src[0] = (char *) malloc(LNSIZE * sizeof(char)); next->dst[0] = (char *) malloc(LNSIZE * sizeof(char)); next->svc[0] = (char *) malloc(LNSIZE * sizeof(char)); strcpy(next->src[0],src); next->rulecount = rulecount; while(1) { if(rulecount < present->rulecount) { next->count_link = present; prev->count_link = next; break; } else { prev = present; present = present->count_link; } } next->src_link = present; prev->src_link = next; return; } prev = head->count_link; /* continue looking for proper entry until the end */ while(prev->count_link != NULL) { /* have we found the right record? */ if(rulecount == prev->rulecount) { /* allocate space for 'indexnum' line number of next rule */ prev->src[indexnum] = (char *) malloc(LNSIZE * sizeof(char)); strcpy(prev->src[indexnum],src); } /* advance pointer to next record */ prev = prev->count_link; } } void update_dest(char *dst, int rulecount, int indexnum) { /* create another pointer to the head of the list as a start point */ prev = head->count_link; /* continue looking for proper entry until the end */ while(prev->count_link != NULL) { /* have we found the right record? */ if(rulecount == prev->rulecount) { /* allocate space for 'indexnum' line number of next rule */ (prev->dst)[indexnum] = (char *) malloc(LNSIZE * sizeof(char)); strcpy(prev->dst[indexnum],dst); } /* advance pointer to next record */ prev = prev->count_link; } } void update_service(char *svc, int rulecount, int indexnum) { /* create another pointer to the head of the list as a start point */ prev = head->count_link; /* continue looking for proper entry until the end */ while(prev->count_link != NULL) { /* have we found the right record? */ if(rulecount == prev->rulecount) { /* allocate space for 'indexnum' line number of next rule */ (prev->svc)[indexnum] = (char *) malloc(LNSIZE * sizeof(char)); strcpy(prev->svc[indexnum],svc); } /* advance pointer to next record */ prev = prev->count_link; } } /* Name: insert_three(char *time, char *install, char *action, int rulecount) * * Description: This routine is responsible for inserting the time, install, and * action arrays into the rulenum structure */ void insert_three(char *time, char *install, char *action, int rulecount) { /* create another pointer to the head of the list as a start point */ prev = head->count_link; /* continue looking for proper entry until the end */ while(prev->count_link != NULL) { /* have we found the right record? */ if(rulecount == prev->rulecount) { if(time[0] != '\0') { strcpy(prev->time,time); } if(install[0] != '\0') { strcpy(prev->install,install); } if(action[0] != '\0') { strcpy(prev->action,action); } } /* advance pointer to next record */ prev = prev->count_link; } } /* while(((*(prev->src[subrule]) != '\0') || (*(prev->dst[subrule]) != '\0') || (*(prev->svc[subrule]) != '\0')) > 0) { printf("%3c",' '); (prev->src[subrule] != '\0') ? printf("%29s",prev->src[subrule]) : printf("%29s"," "); (prev->dst[subrule] != '\0') ? printf("%29s",prev->dst[subrule]) : printf("%29s"," "); (prev->svc[subrule] != '\0') ? printf("%28s",prev->svc[subrule]) : printf("%28s"," "); printf("\n"); subrule++; } */