File: filter.cc

package info (click to toggle)
wflogs 0.9.8-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,220 kB
  • ctags: 3,750
  • sloc: ansic: 12,166; sh: 8,047; cpp: 6,412; lex: 1,525; yacc: 1,430; makefile: 731; sed: 16
file content (546 lines) | stat: -rw-r--r-- 11,671 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 * WallFire -- a comprehensive firewall administration tool.
 * 
 * Copyright (C) 2001 Herv Eychenne <rv@wallfire.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

using namespace std;

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h> /* for regex.h */
#include <regex.h>

extern "C" time_t get_date(const char *p, const time_t *now);

#include "filter.h"
#include "defs.h"


/* Indexes must be the same than enum filter_op_unary */
const char* ops_unary[] = {
  "-",
  "!",
  "+"
};

/* Indexes must be the same than enum filter_op_binary */
const char* ops_binary[] = {
  "||",
  "&&",
  "==",
  "!=",
  "=~",  /* regular expression */
  "<",
  ">",
  "<=",
  ">=",
  "^",
  "|",
  "&",
  "+",
  "-"
};


exprnode_unary::exprnode_unary(enum filter_op_unary op, exprnode* node) :
  _op(op),
  _child(node)
{
}

exprnode_unary::~exprnode_unary() {
  delete _child;
}

exprnode_unary::exprnode_unary(const exprnode_unary& unary) :
  _op(unary._op),
  _child(unary._child)
{
}


bool
exprnode_unary::check() const {
  /* unary operator only applies to terminals of numerical type */
  if (_child->ret_type() != FOP_VAR_TYPE_INT) {
    cerr << _("error: unary operator can only apply to numeric value") << endl;
    return false;
  }
  return _child->check();
}

int
exprnode_unary::eval() const {
  int result;
  switch (_op) {
  case FOP_NEG:
    result = - _child->eval(); break;
  case FOP_NOT:
    result = ! _child->eval(); break;
  case FOP_POS:
    result = _child->eval(); break;
  default:
    result = 0; // RV@@9 this shouldn't happen
  }
  return result;
}

ostream&
exprnode_unary::print(ostream& os) const {
  os << ops_unary[_op] << '(';
  _child->print(os);
  return os << ')';
}




exprnode_binary::exprnode_binary(enum filter_op_binary op,
				 exprnode* lft, exprnode* rt) :
  _op(op),
  _left(lft),
  _right(rt)
{
}

exprnode_binary::~exprnode_binary() {
  delete _left;
  delete _right;
}

exprnode_binary::exprnode_binary(const exprnode_binary& binary) :
  _op(binary._op),
  _left(binary._left),
  _right(binary._right)
{
}

bool
exprnode_binary::check() const {
  enum filter_op_var_type left_type = _left->ret_type(),
    right_type = _right->ret_type();

  if (_op == FOP_REG) {
    if (left_type != FOP_VAR_TYPE_STR) {
      cerr << _("error: regular expression should apply to a string") << endl;
      return false;
    }
    if (right_type == FOP_VAR_TYPE_REGEXP)
      return true;
    if (right_type == FOP_VAR_TYPE_STR)
      cerr << _("error: regular expression should be /.../, not \"...\"") << endl;
    else
      cerr << _("error: invalid use of regexp operator") << endl;
    return false;
  }

  if (left_type != right_type) {
    ostream_printf(cerr, _("error: operator `%s' used with different types\n"),
		   ops_binary[_op]);
    return false;
  }
  /* From now on, left_type == right_type. */
  if (left_type == FOP_VAR_TYPE_STR) { /* types are terminal and string */
    if (_op != FOP_EQ && _op != FOP_NE) {
      ostream_printf(cerr, _("error: invalid operator `%s' used with string\n"),
		       ops_binary[_op]);
      return false;
    }
  }
  return _left->check() && _right->check();
}

int
exprnode_binary::eval() const {
  int result = 0; /* initialized in order to avoid warnings */

  if (_op == FOP_REG)  /* types are supposed to be good (after check()) */
    return ((exprnode_var_regexp*)_right)->regmatch(((exprnode_var*)_left)->str());

  enum filter_op_var_type type = _left->ret_type();
  /* _left and _right are of the same type (check() has ensured that). */

  if (type == FOP_VAR_TYPE_INT) { /* types are numerical */
    switch (_op) {
    case FOP_LOR:
      result = _left->eval() || _right->eval(); break;
    case FOP_LAND:
      result = _left->eval() && _right->eval(); break;
    case FOP_EQ:
      result = _left->eval() == _right->eval(); break;
    case FOP_NE:
      result = _left->eval() != _right->eval(); break;
    case FOP_LT:
      result = _left->eval() < _right->eval(); break;
    case FOP_GT:
      result = _left->eval() > _right->eval(); break;
    case FOP_LE:
      result = _left->eval() <= _right->eval(); break;
    case FOP_GE:
      result = _left->eval() >= _right->eval(); break;
    case FOP_AND:
      result = _left->eval() & _right->eval(); break;
    case FOP_OR:
      result = _left->eval() | _right->eval(); break;
    case FOP_XOR:
      result = _left->eval() ^ _right->eval(); break;
    case FOP_PLUS:
      result = _left->eval() + _right->eval(); break;
    case FOP_MINUS:
      result = _left->eval() - _right->eval(); break;
    default:
      ; // RV@@9 this shouldn't happen
    }
  }
  else if (type == FOP_VAR_TYPE_STR) { /* types are terminal and string */
    string leftstr = ((exprnode_var*)_left)->str();
    string rightstr = ((exprnode_var*)_right)->str();
    switch (_op) {
    case FOP_EQ:
      result = leftstr == rightstr; break;
    case FOP_NE: 
      result = leftstr != rightstr; break;
    default:
      ; // RV@@9 this shouldn't happen
    }
  }
  else if (type == FOP_VAR_TYPE_IPNET) {
    wf_ipaddr ipaddr = ((exprnode_var*)_left)->ipaddr();
    wf_network network = ((exprnode_var_ipnet*)_right)->_network;
    switch (_op) {
    case FOP_EQ:
      result = network.belong(ipaddr); break;
    case FOP_NE: 
      result = !network.belong(ipaddr); break;
    default:
      ; // RV@@9 this shouldn't happen
    }
  }
  else if (type == FOP_VAR_TYPE_MACADDR) {
    wf_macaddr lmacaddr = ((exprnode_var*)_left)->macaddr();
    wf_macaddr rmacaddr = ((exprnode_var_macaddr*)_right)->_macaddr;
    switch (_op) {
    case FOP_EQ:
      result = lmacaddr == rmacaddr; break;
    case FOP_NE: 
      result = lmacaddr != rmacaddr; break;
    default:
      ; // RV@@9 this shouldn't happen
    }
  }
  
  return result;
}

ostream&
exprnode_binary::print(ostream& os) const {
  os << '(';
  _left->print(os);
  os << ' ' << ops_binary[_op] << ' ';
  _right->print(os);
  return os << ')';
}



/* dummy method:never called */
int
exprnode_var::eval() const {
  return 0;
}

/* dummy method: never called */
string
exprnode_var::str() const {
  return "";
}

/* dummy method: never called */
wf_ipaddr
exprnode_var::ipaddr() const {
  return wf_ipaddr();
}

/* dummy method: never called */
wf_macaddr
exprnode_var::macaddr() const {
  return wf_macaddr();
}


struct vars {
  const string name;
  enum filter_op_var_type type;
};

/* Warning: must have the same order than enum vars_num */
struct vars vararr[] = {
  { "format", FOP_VAR_TYPE_STR },
  { "count", FOP_VAR_TYPE_INT },
  { "start_time", FOP_VAR_TYPE_INT },
  { "end_time", FOP_VAR_TYPE_INT },
  { "hostname", FOP_VAR_TYPE_STR},
  { "chainlabel", FOP_VAR_TYPE_STR},
  { "branchname", FOP_VAR_TYPE_STR},
  { "input_iface", FOP_VAR_TYPE_STR},
  { "output_iface", FOP_VAR_TYPE_STR},
  { "protocol", FOP_VAR_TYPE_INT },
  { "datalen", FOP_VAR_TYPE_INT },
  { "sipaddr", FOP_VAR_TYPE_IPNET },
  { "sport", FOP_VAR_TYPE_INT },
  { "smacaddr", FOP_VAR_TYPE_MACADDR },
  { "dipaddr", FOP_VAR_TYPE_IPNET },
  { "dport", FOP_VAR_TYPE_INT },
  { "dmacaddr", FOP_VAR_TYPE_MACADDR },
  { "tcpflags", FOP_VAR_TYPE_INT },
};

exprnode_var_id::exprnode_var_id(const string& name) {
  unsigned int i;
  
  for (i = 0; i < sizeof(vararr) / sizeof(struct vars); i++)
    if (vararr[i].name == name) {
      _num = (enum vars_num)i;
      return;
    }

  _num = VARS_UNKNOWN;
  return;
}

exprnode_var_id::~exprnode_var_id() {
}

enum vars_num
exprnode_var_id::num() const {
  return _num;
}

exprnode_var_id_wflogs::exprnode_var_id_wflogs(const string& name) :
  exprnode_var_id(name)
{
}

exprnode_wflogs::exprnode_wflogs(const exprnode_wflogs& wflogs) :
  _entry(wflogs._entry)
{
}

bool
exprnode_var_id::check() const {
  if (_num != VARS_UNKNOWN)
    return true;

  cerr << _("error: unknown var") << endl;
  return false;
}

enum filter_op_var_type
exprnode_var_id::ret_type() const {
  return vararr[_num].type;
}

ostream&
exprnode_var_id::print(ostream& os) const {
  return os << '$' << vararr[_num].name;
}




exprnode_var_int::exprnode_var_int(int val) :
  _value(val)
{
}

exprnode_var_int::~exprnode_var_int() {
}


int
exprnode_var_int::eval() const {
  return _value;
}

ostream&
exprnode_var_int::print(ostream& os) const {
  return os << _value;
}


void
exprnode_var_int::value(int val) {
  _value = val;
}




exprnode_var_str::exprnode_var_str(const string& str) :
  _str(str)
{
}

exprnode_var_str::~exprnode_var_str() {
}


string
exprnode_var_str::str() const {
  return _str;
}

ostream&
exprnode_var_str::print(ostream& os) const {
  return os << '"' << _str << '"';
}



exprnode_var_regexp::exprnode_var_regexp() :
  _regexp(),
  _str()
{
}

exprnode_var_regexp::~exprnode_var_regexp() {
  regfree(&_regexp);
}


bool
exprnode_var_regexp::set(const string& str, int flags) {
  int ret = regcomp(&_regexp, str.c_str(), flags|REG_NOSUB|REG_EXTENDED);
  if (ret) {
    char errtext[1024];
    _str.erase();
    regerror(ret, &_regexp, errtext, sizeof(errtext));
    ostream_printf(cerr, _("error: cannot compile regexp `%s': "), str.c_str());
    cerr << errtext << endl;
    return false;
  }
  _str = str;
  return true;
}

bool
exprnode_var_regexp::regmatch(const string& str) const {
  int ret = regexec(&_regexp, str.c_str(), 0, NULL, 0);
  switch (ret) {
  case 0:
    return true;
  case REG_NOMATCH:
    break;
  default:
    char errtext[1024];
    regerror(ret, &_regexp, errtext, sizeof(errtext));
    ostream_printf(cerr, _("error: cannot apply regexp `%s': "), _str.c_str());
    cerr << errtext << endl;
  }
  return false;
}

ostream&
exprnode_var_regexp::print(ostream& os) const {
  return os << '/' << _str << '/';
}





exprnode_var_ipnet::exprnode_var_ipnet(const wf_ipaddr& ipaddr) {
  _network.set(ipaddr, 32);
}

exprnode_var_ipnet::exprnode_var_ipnet(const wf_network& ipnet) :
  _network(ipnet)
{
}


ostream&
exprnode_var_ipnet::print(ostream& os) const {
  return os << _network;
}

bool
exprnode_var_ipnet::belong(const wf_ipaddr& ipaddr) const {
  return _network.belong(ipaddr);
}




exprnode_var_macaddr::exprnode_var_macaddr(const wf_macaddr& macaddr) :
  _macaddr(macaddr)
{
}


ostream&
exprnode_var_macaddr::print(ostream& os) const {
  return os << _macaddr;
}



exprnode_var_date::exprnode_var_date() :
  _time(-1)
{
}

exprnode_var_date::~exprnode_var_date() {
}


bool
exprnode_var_date::set(const string& str) {
  _time = get_date(str.c_str(), NULL);
  return _time != -1;
}

int
exprnode_var_date::eval() const {
  return _time;
}

ostream&
exprnode_var_date::print(ostream& os) const {
  char* str = ctime(&_time);
  str[strlen(str) - 1] = '\0';
  return os << '[' << str << ']';
}


/* optimize 
   cerr << "OPERATOR" << endl;
   if ($2->isvar()) { // check int type RV@@9
   cerr << "COUCOU1 " << $1 << endl;
   if ($1 == '-') {
   cerr << "COUCOU" << endl;
   $2->value(-($2->value()));
   }
   else if ($1 == '!')
   $2->value(!($2->value()));
   }
   $$ = $2; // RV@@9 wrong: what about operator?
*/