pattern.h File Reference

Go to the source code of this file.

Functions

int patternMatch (char *filename, char *str)
int loadPatterns (char *file, char *cebuf, size_t cesiz)


Function Documentation

int loadPatterns char *  file,
char *  ebuf,
size_t  ebsiz
 

loadPatterns loads the patterns into the pattern storage from the pattern file. On error -1 is returned and ebuf contains an error message. On success 0 is returned.

Definition at line 109 of file pattern.c.

References _pattern::logdef, _pattern::logrx, _pattern::match, _pattern::matchrx, patrx(), _pattern::pos, and trim().

Referenced by main().

00109                                                        {
00110      char eb2[10240];
00111      char buf[1024], name[1024], val[1024];
00112      char *p, *ep, *p1;
00113      int ipp, i, line = 0;
00114      struct cfgitem *c;
00115      pat_t x;
00116      FILE *f;
00117      
00118      if((f = fopen(file,"r")) == NULL) {
00119           snprintf(ebuf,ebsiz,"%s(%d) Cannot open config file \"%s\": %m",file,line);
00120           return -1;
00121      }
00122      while((p = fgets(buf,sizeof(buf)-1,f)) != NULL) {
00123           line ++; 
00124           if(*p == '#') continue;
00125           if((p = strchr(buf,'#')) != NULL)
00126                *p = '\0';
00127           p = trim(buf);
00128           if(*p == '\0' || strlen(p) == 0)
00129                continue;
00130           x.logdef = p;
00131           if((p = strchr(x.logdef,':')) == NULL)
00132                continue;
00133           *p++ = '\0';
00134           x.match[0] = p;
00135           if((p = strchr(x.match[0],':')) == NULL)
00136                continue;
00137           *p++ = '\0';
00138           x.match[1] = p;
00139           if((p = strchr(x.match[1],'@')) == NULL)
00140                continue;
00141           *p++ = '\0';
00142           if((x.pos = atoi(p)) <= 0) {
00143                snprintf(ebuf,ebsiz,"%s:(%d) Invalid position specified in rule.",file,line);
00144                fclose(f);
00145                return -1;
00146           }
00147           if(strncmp(x.logdef,"//",2) == 0) {
00148                if((x.logrx = patrx(x.logdef,eb2,sizeof(eb2))) == NULL)
00149                     snprintf(ebuf,ebsiz,"%s(%d): WARNING: pcre compile failed for logmatch: %s",file,line,eb2);
00150           }
00151           else
00152                x.logrx = NULL;
00153           for(i = 0; i < 2; i++) {
00154                x.matchrx[i] = NULL;
00155                if(x.match[i][0] == '/') {
00156                     if((x.matchrx[i] = patrx(x.match[i],eb2,sizeof(eb2))) == NULL) 
00157                          snprintf(ebuf,ebsiz,"%s(%d): WARNING: pcre compile failed for match %d: %s",file,line,i+1,eb2);
00158                }
00159           }
00160           //
00161           // realloc and catenation of pattern to pats
00162           // 
00163           ipp = _patcnt ++;
00164           if((_pats = realloc(_pats,(sizeof(pat_t *)*_patcnt))) == NULL){
00165                snprintf(ebuf,ebsiz,"%s(%d): Could not allocate space for pat_t: %m",file,line);
00166                fclose(f);
00167                return -1;
00168           }
00169           if((_pats[ipp] = calloc(sizeof(pat_t),1)) == NULL) {
00170                snprintf(ebuf,ebsiz,"%s(%d): Could not allocate space for pat_t: %m",file,line);
00171                fclose(f);
00172                return -1;
00173           }
00174           x.logdef = strdup(x.logdef);
00175           for(i = 0; i < 2; i++) 
00176                x.match[i] = strdup(x.match[i]);
00177           *_pats[ipp] = x;
00178      }
00179      fclose(f);
00180      return 0;
00181 }

Here is the call graph for this function:

int patternMatch char *  filename,
char *  str
 


Generated on Tue Oct 31 10:17:37 2006 for secwatch by  doxygen 1.4.6