#include "services.h" void parse_svc(FILE *fp, int svccount, char *svcname) { /* set stack counter */ int svcstack=1; /* loop counters */ int i=0, j=0; /* contains contents of a line */ char *lineptr; char *buf; int svcnum=0; /* allocate space for temporary storage */ if((buf = (char *)malloc(LNSIZE * sizeof(char))) == NULL) { fprintf(stderr,"parsenetobj: malloc failed\n"); exit(1); } /* set line counter to 0 */ j=0; /* Find the correct position in the linked list to insert our new entry */ sprev = shead; spresent = shead->nodelink; snext = (services *)malloc(sizeof(services)); while(1) { if(svccount < spresent->svccount) { snext->nodelink = spresent; sprev->nodelink = snext; break; } else { sprev = spresent; spresent = spresent->nodelink; } } /* We are now at the next available entry in the linked list */ /* Set the object name correctly */ sprintf(snext->svcname,"%s",svcname); snext->svccount = svccount; /* Beginning of netobjs section */ while(svcstack > 0) { fgets(buf,LNSIZE,fp); /* determine if we are working with a group of services, then * store the service in the proper structure */ if((strstr(buf,": ")) && (strcmp(snext->type,"group")) == 0) { if(((snext->group)[svcnum] = (char *)malloc(LNSIZE)) == NULL) { fprintf(stderr,"parse_svc: malloc failed\n"); exit(1); } /* read the service name and copy into snext->group */ if((lineptr = strchr(buf,' ')) != NULL) { lineptr++; lineptr[strlen(lineptr)-1] = '\0'; sprintf(snext->group[svcnum],"%s",lineptr); /* set svcnum=0 when we find another group, * and increment it here */ svcnum++; } continue; } /* * These next sections deal with the individual elements in the main * services section */ /* This appears to work in preventing sub-services of type 'refobj' from * being parsed, as we are not interested in that information */ if((strstr(buf,":type (")) && (strlen(snext->type) == 0)) { if((lineptr = strchr(buf,'(')) != NULL) { /* advance the pointer past the first '(' and chop off the * ')' and '\n' at the end as well. */ lineptr++; lineptr[strlen(lineptr)-2] = '\0'; i=0; /* continue copying data as all lower case */ while(i < strlen(lineptr)) { snext->type[i] = tolower(lineptr[i]); i++; } /* found another group -- reset counter */ if(strcmp(snext->type,"group") == 0) svcnum=0; } continue; } /* define a special case for ':exp (' here. Not sure what * it means, but may be signficant */ /* Seems to be the 'port' equivalent for services of type 'other' */ if((strstr(buf,":exp (")) && (strcmp(snext->type,"other") == 0)) { if((lineptr = strchr(buf,'(')) != NULL) { lineptr++; i=0; j=0; /* get rid of '(' at end of string */ lineptr[strlen(lineptr)-2] = '\0'; /* dump the double quotes from the line first */ while(i < strlen(lineptr)) { if(lineptr[i] != '"') { snext->port[j] = lineptr[i]; j++; } i++; } /* this information isn't all that helpful. Cut it down * to 15 characters */ if(strlen(snext->port) > 15) snext->port[15] = '\0'; } continue; } /* find ports for all services except 'other' */ if((snext->type != NULL) && (strstr(buf,":port (")) && (strcmp(snext->type,"other") != 0)) { j=0; /* get rid of ':port (' first */ if((lineptr = strchr(buf,'(')) != NULL) { lineptr++; for(i=0;iport[j] = lineptr[i]; j++; } } /* replace the \n at the end with NULL */ snext->port[j-1] = '\0'; } continue; } /* icmp seems to also be a special case. Cut down the * length of the string first */ /* check for NULL first */ if((snext->type != NULL) && (strstr(buf,":exp (")) && (strcmp(snext->type,"icmp") == 0)) { /* first find location of 'icmp_type=' */ if((lineptr = strchr(buf,'=')) != NULL) { /* locate '=ICMP_' in string. Dangerous to use lineptr as * argument to strchr here? */ if((lineptr = strchr(lineptr,'_')) != NULL) { lineptr++; /* chop of ending '")\n' from string */ lineptr[strlen(lineptr)-3] = '\0'; sprintf(snext->port,"%s",lineptr); } } else { /* must be a special type of ':exp (' situation. Just take * it anyway. Move to the opening paren, and copy the rest */ if((lineptr = strchr(buf,'(')) != NULL) { lineptr++; /* get rid of ending ')' and carriage return */ lineptr[strlen(lineptr)-2] = '\0'; sprintf(snext->port,"%s",lineptr); } } /* Trim the ICMP port messages */ if(strlen(snext->port) > 11) snext->port[11] = '\0'; continue; } /* process the string before reading another line to determine if * we are at the end of the list of service */ for(i=0;inodelink = spresent; sprev->nodelink = snext; }