root/trunk/src/rules_mgmt.c @ 8

Revision 8, 15.0 KB (checked in by andreu, 17 years ago)

second RENETCOL CVS Integration

  • Property svn:eol-style set to native
RevLine 
[2]1/*
2 * File: rules_mgmt.c
3 *
4 * Authors: ANDREU François-Xavier
5 *
6 * Copyright (C) 2005 GIP RENATER
7 */
8
9/*  This file is part of renetcol.
10 *
11 *  renetcol is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  renetcol is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with renetcol; if not, write to the Free Software
23 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include <syslog.h>
27#include "rules_mgmt.h"
28
29void 
30printRuleDef(RuleDefPtr rdPtr)
31{
32  if (rdPtr != NULL){
33    fprintf(stderr, "  ft: %hu\n", rdPtr->fieldType);
34    fprintf(stderr, "  check: %hu\n", rdPtr->check);
35    fprintf(stderr, "  operator: %hu\n", rdPtr->operator);
36    if (rdPtr->value!=NULL) {
37      switch (rdPtr->value->valueLength){
38      case 1:
39        fprintf(stderr, "  char value: %hu\n", rdPtr->value->stor.cvalue);
40        break;
41      case 2:
42        fprintf(stderr, "  short value: %hu\n", rdPtr->value->stor.svalue);
43        break;
44      case 4:
45        fprintf(stderr, "  long value: %lu\n", rdPtr->value->stor.lvalue);
46        break;
47      case 16:
48        fprintf(stderr, "  16 bytes : ");
49        fprintf(stderr, "%hx:", ntohl(rdPtr->value->stor.tabAdd6[0])>>16);
50        fprintf(stderr, "%hx:", ntohl(rdPtr->value->stor.tabAdd6[0])<<16>>16);
51        fprintf(stderr, "%hx:", ntohl(rdPtr->value->stor.tabAdd6[1])>>16);
52        fprintf(stderr, "%hx:", ntohl(rdPtr->value->stor.tabAdd6[1])<<16>>16);
53        fprintf(stderr, "%hx:", ntohl(rdPtr->value->stor.tabAdd6[2])>>16);
54        fprintf(stderr, "%hx:", ntohl(rdPtr->value->stor.tabAdd6[2])<<16>>16);
55        fprintf(stderr, "%hx:", ntohl(rdPtr->value->stor.tabAdd6[3])>>16);
56        fprintf(stderr, "%hx\n", ntohl(rdPtr->value->stor.tabAdd6[3])<<16>>16);
57        break;
58      default:
59        fprintf(stderr,"  no check\n");
60        break;
61      }
62    } else {
63      fprintf(stderr,"  no check\n");
64    }
65    printRuleDef(rdPtr->next);
66  }
67}
68
69void 
70printRule(RulesPtr rPtr)
71{
72  if (rPtr!=NULL){
73    fprintf(stderr, "\nid: %hu\n", rPtr->id);
[8]74    if (rPtr->prev) { fprintf(stderr, "\nid prev: %hu\n", rPtr->prev->id);}
75    if (rPtr->next) { fprintf(stderr, "\nid next: %hu\n", rPtr->next->id);}
[2]76    fprintf(stderr, "Name: %s\n", rPtr->ruleName);   
77/*     puts (asctime (& rPtr->begin)); */
78/*     puts (asctime (& rPtr->end)); */
79    printRuleDef(rPtr->def);
80    if (rPtr->host){
81      printHostDef(rPtr->host);
82    }
83    invPrintFieldSet(rPtr->fieldToRecord);
84    printRule(rPtr->next);
85  }
86}
87
88/*
89 *
90 */
91unsigned short
92getNewId(RulesPtr rPtr){
93  RulesPtr tmp = rPtr;
94  unsigned short ctr = 0;
95
96  while (tmp){
97    if (tmp->id > ctr){
98      ctr = tmp->id;
99    }
100    tmp = tmp->next;
101  }
102  return ctr+1;
103}
104
105void 
106freeValue(ValuesPtr vPtr)
107{
108  free(vPtr);
109  vPtr = NULL;
110}
111
112void
113freeRuleDef(RuleDefPtr rdPtr)
114{
115  RuleDefPtr tmp;
116  if (rdPtr) {
117    tmp = rdPtr->next;
118    freeValue(rdPtr->value);
119    free(rdPtr);
120    rdPtr = NULL;
121    freeRuleDef(tmp);
122  }
123}
124
125void
126freeRule(RulesPtr rPtr)
127{
128  if (rPtr->def) {freeRuleDef(rPtr->def);}
129  if (rPtr->fieldToRecord) {freeField(rPtr->fieldToRecord);}
130  if (rPtr->host) {freeRemoteHost(rPtr->host);}
131  if (rPtr->fileName) {freeFile(rPtr->fileName);}
132  if (rPtr->db) {freeDb(rPtr->db);}
133  free(rPtr);
134  rPtr = NULL;
135}
136
137RulesPtr
138delRule(RulesPtr rPtr, RulesPtr firstrPtr)
139{
140  RulesPtr tmp = NULL;
[8]141  if (rPtr==firstrPtr) {
[2]142    if (rPtr->next) {
143      tmp = rPtr->next;
[8]144      tmp->prev = NULL;
145      freeRule(rPtr);
146      return tmp;
147    }else{
148      freeRule(rPtr);
149      return NULL;
[2]150    }
[8]151  } else {
152    if (rPtr->prev->next) { rPtr->prev->next = rPtr->next;}
153    if (rPtr->next) {rPtr->next->prev = rPtr->prev;}
[2]154    freeRule(rPtr);
[8]155    return firstrPtr;
[2]156  }
157}
158
[8]159RulesPtr
[2]160getRules(RulesPtr rPtr, char *filename)
161{
162  FILE *ruleFile;
163  RulesPtr tmp = NULL;
[8]164  RulesPtr tmp2 = NULL;
[2]165  time_t hour;
166  char line[512];
167  char lineCopy[512];
168  char str1[256];
169  char str2[256];
170  char strTok[1];
171  char strName[100];
172  char strOutputType[20];
173  int cptLine;
174 
175  if (!(ruleFile = fopen(filename, "r"))) {
[8]176    syslog(LOG_ERR, "error during %s opening\n", filename);
[2]177    exit(1);
178  }
179  cptLine=0;
180  time (& hour);
181  /*    tmp->begin = localtime (& hour); */
182  /*    tmp->end = localtime (& hour); */
183  /*     strptime( begin, "%Y%m%d%H%M%S", & tmp->begin); */
184  /*     strptime( end, "%Y%m%d%H%M%S", & tmp->end); */
185  while ( fgets(line, 512, ruleFile) != 0) {   
186    if ( strspn(line, "#") == 0 ) {
187      strcpy(lineCopy, line);
188      if (strncmp("N", lineCopy, 1) == 0){
189        if (sscanf(line, "%1s %s\n",
190                   strTok,       
191                   strName) == 0) {
[8]192          syslog(LOG_ERR, "Error in file %s, line %d\n",
[2]193                  filename, cptLine);
194          exit(1);
195        } else {
196          rPtr = tmp;
197          tmp = NULL;
198          tmp = (RulesPtr) malloc(sizeof(struct Rules));
199          if (tmp==NULL) {
[8]200            syslog(LOG_ERR, "ERROR in malloc in getRule function\n");
[2]201            exit(1);
202          } else {
203            tmp->id = getNewId(rPtr);
204            tmp->def = NULL;
205            tmp->fieldToRecord = NULL;
206            tmp->host = NULL;
207            tmp->fileName = NULL;
208            tmp->db = NULL;
209            tmp->next = rPtr;
[8]210            /* rPtr->prev = tmp; */
[2]211            tmp->prev = NULL;
212          }
213          tmp->ruleName = (char *) malloc((strlen(strName)+1)*sizeof(char));
214          strcpy(tmp->ruleName, strName);
215        }
216      }
217      if (strncmp("O", lineCopy, 1) == 0){
218        if (sscanf(line, "%1s %s %s %s\n",
219                   strTok,       
220                   strOutputType,
221                   str1,
222                   str2) == 0) {
[8]223          syslog(LOG_ERR, "Error in file %s, line %d\n",
[2]224                  filename, cptLine);
225          exit(1);
226        } else {
227          /* switch output type */
228          if (strcmp("file", strOutputType) == 0){
229            tmp->type = 1;
230            tmp->fileName = addFileDef(tmp->fileName, str1, str2);
231          }
232          if (strcmp("socket", strOutputType) == 0){
233            tmp->type = 2;
234            tmp->host = addRemoteHostDef(tmp->host, str1, str2);
235          }
236          if (strcmp("base", strOutputType) == 0){
237            tmp->type = 3;
238            tmp->db = addDataBase(tmp->db, str1, str2);
239          }
240        }
241      }
242      if (strncmp("C", lineCopy, 1) == 0){
243        tmp->def = addRuleDef(tmp->def, lineCopy);
244      }
245      if (strncmp("A", lineCopy, 1) == 0){
246        syslog(LOG_INFO,"Aggregation mode isn't yet implemented\n");
247      }
248      if (strncmp("R", lineCopy, 1) == 0){
249        tmp->fieldToRecord = addFields(tmp->fieldToRecord, lineCopy);
250      }
251      cptLine++;
252    }
253    /*     tmp->prev = NULL; */
254  }
[8]255  tmp2 = tmp;
256  for ( ; tmp2; tmp2=tmp2->next) {
257    if (tmp2->next) {
258      tmp2->next->prev = tmp2;
259    }
260  }
261  fclose(ruleFile);
262  return tmp;
[2]263}
264
265RulesPtr
266getLightRules(RulesPtr rPtr, char *filename)
267{
268  FILE *ruleFile;
269  RulesPtr tmp = NULL;
[8]270  RulesPtr tmp2 = NULL;
[2]271  time_t hour;
272  char line[512];
273  char lineCopy[512];
274  char str1[256];
275  char str2[256];
276  char strTok[1];
277  char strName[100];
278  char strOutputType[20];
279  int cptLine;
280 
281  if (!(ruleFile = fopen(filename, "r"))) {
[8]282    syslog(LOG_ERR, "error during %s opening\n", filename);
[2]283    exit(1);
284  }
285  cptLine=0;
286  time (& hour);
287  /*    tmp->begin = localtime (& hour); */
288  /*    tmp->end = localtime (& hour); */
289  /*     strptime( begin, "%Y%m%d%H%M%S", & tmp->begin); */
290  /*     strptime( end, "%Y%m%d%H%M%S", & tmp->end); */
291  while ( fgets(line, 512, ruleFile) != 0) {   
292    if ( strspn(line, "#") == 0 ) {
293      strcpy(lineCopy, line);
294      if (strncmp("N", lineCopy, 1) == 0){
295        if (sscanf(line, "%1s %s\n",
296                   strTok,       
297                   strName) == 0) {
[8]298          syslog(LOG_ERR, "Error in file %s, line %d\n",
[2]299                  filename, cptLine);
300          exit(1);
301        } else {
302          rPtr = tmp;
303          tmp = NULL;
304          tmp = (RulesPtr) malloc(sizeof(struct Rules));
305          if (tmp==NULL) {
[8]306            syslog(LOG_ERR, "ERROR in malloc in getRule function\n");
[2]307            exit(1);
308          } else {
309            tmp->id = getNewId(rPtr);
310            tmp->def = NULL;
311            tmp->fieldToRecord = NULL;
312            tmp->host = NULL;
313            tmp->fileName = NULL;
314            tmp->db = NULL;
315            tmp->next = rPtr;
316            tmp->prev = NULL;
317          }
318          tmp->ruleName = (char *) malloc((strlen(strName)+1)*sizeof(char));
319          strcpy(tmp->ruleName, strName);
320        }
321      }
322      if (strncmp("O", lineCopy, 1) == 0){
323        if (sscanf(line, "%1s %s %s %s\n",
324                   strTok,       
325                   strOutputType,
326                   str1,
327                   str2) == 0) {
[8]328          syslog(LOG_ERR, "Error in file %s, line %d\n",
[2]329                  filename, cptLine);
330          exit(1);
331        } else {
332          /* switch output type */
333          if (strcmp("file", strOutputType) == 0){
334            tmp->type = 1;
335          }
336          if (strcmp("socket", strOutputType) == 0){
337            tmp->type = 2;
338          }
339          if (strcmp("base", strOutputType) == 0){
340            tmp->type = 3;
341          }
342        }
343      }
344      if (strncmp("C", lineCopy, 1) == 0){
345        tmp->def = addRuleDef(tmp->def, lineCopy);
346      }
347      if (strncmp("A", lineCopy, 1) == 0){
[8]348        syslog(LOG_INFO,"Aggregation mode isn't yet implemented");
[2]349      }
350      if (strncmp("R", lineCopy, 1) == 0){
351        tmp->fieldToRecord = addFields(tmp->fieldToRecord, lineCopy);
352      }
353      cptLine++;
354    }
355  }
[8]356  tmp2 = tmp;
357  for ( ; tmp2; tmp2=tmp2->next) {
358    if (tmp2->next) {
359      tmp2->next->prev = tmp2;
360    }
361  }
362  fclose(ruleFile);
[2]363  return tmp;
364}
365
366FilePtr
367addFileDef(FilePtr fPtr, char *str1, char *str2)
368{
[8]369/*   fprintf(stderr,"add file def: %s %s \n", str1, str2); */
[2]370  return NULL;
371}
372
373RemoteHostPtr
374addRemoteHostDef(RemoteHostPtr rPtr, char *str1, char *str2)
375{
376  RemoteHostPtr tmp = NULL;
377  tmp = (RemoteHostPtr) malloc(sizeof(struct RemoteHost));
378  if (tmp==NULL) {
[8]379    syslog(LOG_ERR, "ERROR in malloc in addRemoteHostDef function");
[2]380    exit(1);
381  } else {
[8]382    tmp->hostAddressPtr = (struct sockaddr_storage*)
383      malloc(sizeof(struct sockaddr_storage));
[2]384    if (tmp->hostAddressPtr==NULL) {
[8]385      syslog(LOG_ERR, "ERROR in malloc in addRemoteHostDef function");
[2]386      exit(1);
387    } else {
[8]388      if ((tmp->sockId=create_socket( str1, str2, tmp->hostAddressPtr))==0)
389        return NULL;
[2]390      tmp->next = rPtr;
391    }
392  }
393  return tmp;
394}
395
396DbPtr
397addDataBase(DbPtr dPtr, char *str1, char *str2){
[8]398  syslog(LOG_INFO,"DB output not in this release");
[2]399  return NULL;
400}
401
402FieldPtr
403addFields (FieldPtr fp, char *ftr)
404{
405  /* for each token "R" in the tests string */
406  /* build a field structure */
407  /* return a list of field */
408  static char * fields;
409  fields = strtok(ftr, "R");
410  while (fields != NULL) {
411    unsigned short type;
412    unsigned short length;
413    if (sscanf(fields, " %hu %hu ", &type, &length) == 0) {
[8]414      syslog(LOG_ERR,
415              "Errors in definition of fields to record: wrong syntax");
[2]416      exit(1);
417    } else {
418      if (type == 0){
419        return NULL; /* all fields must be recorded */
420      } else {
421        fp = addField(fp, type, length);
422      }
423    }
424    fields = strtok(NULL, "R");   
425  }
426  return fp;
427}
428
429RuleDefPtr
430addRuleDef(RuleDefPtr rdPtr, char *tests)
431{
432  /* for each token "C" in the tests string */
433  /* build a rule_def struct */
434  /* return the list of rule_def */
435  RuleDefPtr tmp = NULL;
436  static char * fields;
437  unsigned short operatorToNumber = 0;
438
439  fields = strtok(tests, "C");
440  while (fields != NULL) {
441    static unsigned short ft;
442    char operator[2];
443    char value[30];
444    tmp = (RuleDefPtr) malloc(sizeof(struct RuleDef));
445    if (tmp==NULL) {
[8]446      syslog(LOG_ERR, "ERROR in malloc in addRuleDef function");
[2]447      exit(1);
448    } else {
449      if (sscanf(fields, " %hu %1s %s ", &ft, operator, value) == 0) {
[8]450        syslog(LOG_ERR, "Errors in Tests definition : wrong syntax");
[2]451        exit(1);
452      }
453      /* operator possibilities : > < = b t (belong, table) */
454      if (strcmp(operator,">")==0) {
455        operatorToNumber = 0; /* FIXME why operatorToNumber ? */
456        tmp->operator = 0;
457      }
458      if (strcmp(operator,"<")==0) {
459        operatorToNumber = 1;
460        tmp->operator = 1;
461      }
462      if (strcmp(operator,"=")==0) {
463        operatorToNumber = 2;
464        tmp->operator = 2;
465      }
466      tmp->fieldType = ft;
467      tmp->value = NULL;
468      if ((ft==1) || (ft==25)){
469          tmp->value = addValue(tmp->value, operatorToNumber, value);
470      } else if ((ft==8) || (ft==12) || (ft==15) || (ft==18) ){
471          tmp->value = addAddress(tmp->value, operatorToNumber, value);
472      } else if ((ft==27) || (ft==28) || (ft==62) || (ft==63) ){
473          tmp->value = addV6Address(tmp->value, operatorToNumber, value);
474      } else if ((ft==60)){
475          tmp->value = addCValue(tmp->value, operatorToNumber, value);
476      } else {
477        syslog(LOG_INFO,
[8]478                "You can't compare the field <%hu> in this release, sorry",
[2]479                ft);
480      }
481      tmp->check = 0;
482      tmp->next = rdPtr;
483      if (rdPtr!=NULL) { rdPtr->prev = tmp;}
484      tmp->prev = NULL;
485      rdPtr=tmp;
486    }
487    fields = strtok(NULL, "C");
488  }
489  return tmp;
490}
491
492ValuesPtr
493addAddress(ValuesPtr vPtr, unsigned short op, char *val)
494{
495  ValuesPtr tmp;
496  unsigned short v0, v1, v2, v3;
497  unsigned char buffer4[4];
498
499  tmp = (ValuesPtr) malloc(sizeof(struct Values));
500  if (tmp==NULL) {
[8]501    syslog(LOG_ERR, "ERROR in malloc in addAddress function");
[2]502    exit(1);
503  } else {
504    if (sscanf(val, "%hu.%hu.%hu.%hu", &v0, &v1, &v2, &v3) == 0) {
[8]505      syslog(LOG_ERR, "Errors in Tests definition : wrong IPv4 value");
[2]506      exit(1);
507    }
508    buffer4[3] = (unsigned char)v0;
509    buffer4[2] = (unsigned char)v1;
510    buffer4[1] = (unsigned char)v2;
511    buffer4[0] = (unsigned char)v3;     
512    tmp->valueLength = 4;
513    tmp->stor.lvalue = *((unsigned long*)(&buffer4));
514    tmp->next = vPtr;
515  }
516  return tmp;
517}
518
519ValuesPtr
520addV6Address(ValuesPtr vPtr, unsigned short op, char *val)
521{
522  ValuesPtr tmp;
523  struct in6_addr v6addr;
524  char netw_form[16];
525  int result;
526
527  tmp = (ValuesPtr) malloc(sizeof(struct Values));
528  if (tmp==NULL) {
[8]529    syslog(LOG_ERR, "ERROR in malloc in addV6Address function");
[2]530    exit(1);
531  } else {
532    result = inet_pton(AF_INET6, val, netw_form);
533    switch(result) {
534    case 0 :
[8]535      syslog(LOG_ERR," inet_pton : Invalid IPv6 Address");
[2]536      exit(1);
537      break;
538    case -1:
[8]539      syslog(LOG_ERR," inet_pton : AF unknown");
[2]540      exit(1);
541      break;
542    }
543    memcpy(&v6addr, netw_form, sizeof(netw_form));
544    tmp->valueLength = 16;
545    tmp->stor.tabAdd6[0] = v6addr.s6_addr32[0];
546    tmp->stor.tabAdd6[1] = v6addr.s6_addr32[1];
547    tmp->stor.tabAdd6[2] = v6addr.s6_addr32[2];
548    tmp->stor.tabAdd6[3] = v6addr.s6_addr32[3];
549    tmp->next = vPtr;
550  }
551  return tmp;
552}
553
554ValuesPtr
555addValue(ValuesPtr vPtr, unsigned short op, char *val)
556{
557  ValuesPtr tmp;
558  char value[10];
559
560  tmp = (ValuesPtr) malloc(sizeof(struct Values));
561  if (tmp==NULL) {
[8]562    syslog(LOG_ERR, "ERROR in malloc in addValue function");
[2]563    exit(1);
564  } else {
565    if (sscanf(val, "%s", value) == 0) {
[8]566      syslog(LOG_ERR, "Errors in Tests definition : wrong short value");
[2]567      exit(1);
568    }   
569    tmp->valueLength = 2;
570    tmp->stor.svalue = (unsigned short) atoi(value);
571    tmp->next = vPtr;
572  }
573  return tmp;
574}
575
576ValuesPtr
577addCValue(ValuesPtr vPtr, unsigned short op, char *val)
578{
579  ValuesPtr tmp;
580  char value[10];
581
582  tmp = (ValuesPtr) malloc(sizeof(struct Values));
583  if (tmp==NULL) {
[8]584    syslog(LOG_ERR, "ERROR in malloc in addValue function");
[2]585    exit(1);
586  } else {
587    if (sscanf(val, "%s", value) == 0) {
[8]588      syslog(LOG_ERR, "Errors in Tests definition : wrong char value");
[2]589      exit(1);
590    }   
591    tmp->valueLength = 1;
592    tmp->stor.cvalue = (unsigned short) atoi(value);
593    tmp->next = vPtr;
594  }
595  return tmp;
596}
Note: See TracBrowser for help on using the browser.