1
|
1 /***************************************************************************
|
|
2 * Copyright (C) 2005 by 510 Software Group *
|
|
3 * *
|
|
4 * *
|
|
5 * This program is free software; you can redistribute it and/or modify *
|
|
6 * it under the terms of the GNU General Public License as published by *
|
|
7 * the Free Software Foundation; either version 2 of the License, or *
|
|
8 * (at your option) any later version. *
|
|
9 * *
|
|
10 * This program is distributed in the hope that it will be useful, *
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
13 * GNU General Public License for more details. *
|
|
14 * *
|
|
15 * You should have received a copy of the GNU General Public License *
|
|
16 * along with this program; if not, write to the *
|
|
17 * Free Software Foundation, Inc., *
|
|
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
19 ***************************************************************************/
|
|
20
|
|
21 #include "includes.h"
|
2
|
22 #include <fcntl.h>
|
3
|
23 #include <sys/socket.h>
|
|
24 #include <netinet/in.h>
|
|
25 #include <arpa/inet.h>
|
|
26 #include <netdb.h>
|
4
|
27 #include <limits.h>
|
1
|
28
|
4
|
29 static char* syslogconfig_version = "$Id$";
|
1
|
30
|
27
|
31 char *token_add;
|
3
|
32 char *token_bucket;
|
1
|
33 char *token_file;
|
3
|
34 char *token_ignore;
|
1
|
35 char *token_include;
|
3
|
36 char *token_index;
|
1
|
37 char *token_lbrace;
|
3
|
38 char *token_pattern;
|
1
|
39 char *token_rbrace;
|
27
|
40 char *token_remove;
|
1
|
41 char *token_semi;
|
3
|
42 char *token_slash;
|
|
43 char *token_threshold;
|
|
44
|
|
45 struct ltint
|
|
46 {
|
|
47 bool operator()(const int s1, const int s2) const
|
|
48 {
|
|
49 return (unsigned)s1 < (unsigned)s2;
|
|
50 }
|
|
51 };
|
1
|
52
|
4
|
53 struct bucket {
|
|
54 int count;
|
|
55 bool latch; // true iff ever count>threshold
|
|
56 };
|
|
57
|
1
|
58 string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
59 const int maxlen = 1000; // used for snprintf buffers
|
4
|
60 typedef map<int, bucket, ltint> ip_buckets;
|
3
|
61
|
|
62 class IPR {
|
|
63 ip_buckets violations;
|
|
64 public:
|
20
|
65 void add(int ip, int amount, CONFIG &con, char *file_name, int pattern_index);
|
3
|
66 void leak(int amount, CONFIG &con);
|
20
|
67 void update(int ip, bool added, char *file_name, int pattern_index);
|
|
68 void changed(CONFIG &con, int ip, bool added);
|
3
|
69 };
|
|
70
|
|
71 IPR recorder;
|
|
72
|
|
73
|
|
74 ////////////////////////////////////////////////
|
|
75 //
|
20
|
76 void IPR::add(int ip, int amount, CONFIG &con, char *file_name, int pattern_index) {
|
3
|
77 if (con.looking(ip)) {
|
|
78 ip_buckets::iterator i = violations.find(ip);
|
4
|
79 if (i == violations.end()) {
|
|
80 bucket b;
|
|
81 b.count = amount;
|
20
|
82 b.latch = (con.get_threshold() <= b.count);
|
4
|
83 violations[ip] = b;
|
20
|
84 if (b.latch) {
|
|
85 update(ip, true, file_name, pattern_index);
|
|
86 changed(con, ip, true);
|
|
87 }
|
4
|
88 }
|
3
|
89 else {
|
4
|
90 bucket &b = (*i).second;
|
|
91 if (b.count < (INT_MAX-amount)) {
|
|
92 int t = con.get_threshold();
|
|
93 int c = b.count;
|
|
94 b.count += amount;
|
|
95 if ((!b.latch) && (c < t) && (t <= b.count)) {
|
|
96 b.latch = true;
|
20
|
97 update(ip, true, file_name, pattern_index);
|
|
98 changed(con, ip, true);
|
4
|
99 }
|
|
100 }
|
3
|
101 }
|
|
102 }
|
|
103 }
|
|
104
|
|
105
|
|
106 void IPR::leak(int amount, CONFIG &con) {
|
|
107 for (ip_buckets::iterator i=violations.begin(); i!=violations.end(); ) {
|
4
|
108 int ip = (*i).first;
|
|
109 bucket &b = (*i).second;
|
|
110 if (b.count <= amount) {
|
20
|
111 if (b.latch) {
|
|
112 update(ip, false, NULL, 0);
|
24
|
113 changed(con, ip, false);
|
20
|
114 }
|
3
|
115 violations.erase(i++);
|
|
116 }
|
|
117 else {
|
4
|
118 b.count -= amount;
|
3
|
119 i++;
|
|
120 }
|
|
121 }
|
20
|
122 }
|
|
123
|
|
124
|
|
125 void IPR::update(int ip, bool added, char *file_name, int pattern_index) {
|
|
126 if (debug_syslog > 2) {
|
|
127 char buf[maxlen];
|
|
128 in_addr ad;
|
|
129 ad.s_addr = htonl(ip);
|
|
130 if (added) snprintf(buf, maxlen, "dropping traffic from/to %s based on pattern match %d in %s", inet_ntoa(ad), pattern_index, file_name);
|
|
131 else snprintf(buf, maxlen, "allowing traffic from/to %s", inet_ntoa(ad));
|
|
132 my_syslog(buf);
|
|
133 }
|
3
|
134 }
|
|
135
|
|
136
|
20
|
137 void IPR::changed(CONFIG &con, int ip, bool added) {
|
|
138 int t = con.get_threshold();
|
4
|
139 char buf[maxlen];
|
20
|
140 if (added) {
|
|
141 bucket &b = violations[ip];
|
|
142 if (con.looking(ip) && (b.count > t)) {
|
4
|
143 in_addr ad;
|
|
144 ad.s_addr = htonl(ip);
|
27
|
145 snprintf(buf, maxlen, con.add_command, inet_ntoa(ad));
|
5
|
146 system(buf);
|
4
|
147 }
|
3
|
148 }
|
20
|
149 else {
|
24
|
150 in_addr ad;
|
|
151 ad.s_addr = htonl(ip);
|
27
|
152 snprintf(buf, maxlen, con.remove_command, inet_ntoa(ad));
|
20
|
153 system(buf);
|
|
154 }
|
3
|
155 }
|
1
|
156
|
|
157
|
3
|
158 ////////////////////////////////////////////////
|
|
159 //
|
|
160 int ip_address(char *have);
|
|
161 int ip_address(char *have) {
|
|
162 int ipaddr = 0;
|
|
163 in_addr ip;
|
|
164 if (inet_aton(have, &ip)) ipaddr = ip.s_addr;
|
|
165 else {
|
|
166 struct hostent *host = gethostbyname(have);
|
|
167 if (host && host->h_addrtype == AF_INET) memcpy(&ipaddr, host->h_addr, sizeof(ipaddr));
|
|
168 }
|
|
169 return ntohl(ipaddr);
|
|
170 }
|
|
171
|
|
172
|
|
173 ////////////////////////////////////////////////
|
|
174 //
|
4
|
175 PATTERN::PATTERN(TOKEN &tok, char *pattern_, int index_, int amount_) {
|
3
|
176 pattern = pattern_;
|
|
177 index = index_;
|
4
|
178 amount = amount_;
|
3
|
179 if (pattern) {
|
|
180 int rc = regcomp(&re, pattern, REG_ICASE | REG_EXTENDED);
|
|
181 if (rc) {
|
|
182 char bu[maxlen];
|
|
183 regerror(rc, &re, bu, maxlen);
|
|
184 char buf[maxlen];
|
|
185 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", pattern, bu);
|
|
186 tok.token_error(buf);
|
|
187 pattern = NULL;
|
|
188 }
|
|
189 }
|
|
190 }
|
|
191
|
|
192
|
|
193 PATTERN::~PATTERN() {
|
|
194 regfree(&re);
|
|
195 }
|
|
196
|
|
197
|
20
|
198 bool PATTERN::process(char *buf, CONFIG &con, char *file_name, int pattern_index) {
|
3
|
199 if (pattern) {
|
|
200 const int nmatch = index+1;
|
|
201 regmatch_t match[nmatch];
|
|
202 if (0 == regexec(&re, buf, nmatch, match, 0)) {
|
|
203 int s = match[index].rm_so;
|
|
204 int e = match[index].rm_eo;
|
|
205 if (s != -1) {
|
5
|
206 if (debug_syslog > 3) {
|
|
207 my_syslog(buf); // show lines with matches
|
|
208 }
|
3
|
209 buf[e] = '\0';
|
|
210 int ip = ip_address(buf+s);
|
|
211 if (ip) {
|
20
|
212 recorder.add(ip, amount, con, file_name, pattern_index);
|
3
|
213 }
|
|
214 return true;
|
|
215 }
|
|
216 }
|
|
217 }
|
|
218 return false;
|
|
219 }
|
|
220
|
|
221
|
|
222 void PATTERN::dump(int level) {
|
|
223 char indent[maxlen];
|
|
224 int i = min(maxlen-1, level*4);
|
|
225 memset(indent, ' ', i);
|
|
226 indent[i] = '\0';
|
|
227 printf("%s pattern \"%s\" {; \n", indent, pattern);
|
|
228 printf("%s index %d; \n", indent, index);
|
4
|
229 printf("%s bucket %d; \n", indent, amount);
|
3
|
230 printf("%s }; \n", indent);
|
|
231 }
|
|
232
|
|
233
|
|
234 ////////////////////////////////////////////////
|
|
235 //
|
1
|
236 CONFIG::CONFIG() {
|
|
237 reference_count = 0;
|
|
238 generation = 0;
|
|
239 load_time = 0;
|
27
|
240 threshold = 500;
|
|
241 add_command = "/sbin/iptables -I INPUT --src %s --jump DROP";
|
|
242 remove_command = "/sbin/iptables -D INPUT --src %s --jump DROP";
|
1
|
243 }
|
|
244
|
|
245
|
|
246 CONFIG::~CONFIG() {
|
|
247 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
248 SYSLOGCONFIG *c = *i;
|
|
249 delete c;
|
|
250 }
|
3
|
251 ignore.clear();
|
1
|
252 }
|
|
253
|
|
254
|
|
255 void CONFIG::add_syslogconfig(SYSLOGCONFIGP con) {
|
|
256 syslogconfigs.push_back(con);
|
|
257 }
|
|
258
|
|
259
|
3
|
260 void CONFIG::add_pair(IPPAIR pair) {
|
|
261 ignore.push_back(pair);
|
|
262 }
|
|
263
|
|
264
|
1
|
265 void CONFIG::dump() {
|
3
|
266 printf(" threshold %d; \n\n", threshold);
|
|
267
|
27
|
268 printf(" add_command \"%s\"; \n", add_command);
|
|
269 printf(" remove_command \"%s\"; \n\n", remove_command);
|
|
270
|
3
|
271 printf(" ignore { \n");
|
|
272 for (ippair_list::iterator i=ignore.begin(); i!=ignore.end(); i++) {
|
|
273 IPPAIR &p = *i;
|
|
274 in_addr ip;
|
|
275 ip.s_addr = htonl(p.first);
|
|
276 printf(" %s/%d; \n", inet_ntoa(ip), p.cidr);
|
|
277 }
|
|
278 printf(" }; \n\n");
|
|
279
|
1
|
280 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
281 SYSLOGCONFIGP c = *i;
|
|
282 c->dump(0);
|
|
283 }
|
|
284 }
|
|
285
|
|
286
|
2
|
287 void CONFIG::read() {
|
3
|
288 while (true) {
|
|
289 bool have = false;
|
|
290 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
291 SYSLOGCONFIGP c = *i;
|
|
292 have |= c->read(*this);
|
|
293 }
|
|
294 if (!have) break;
|
2
|
295 }
|
|
296 }
|
|
297
|
|
298
|
4
|
299 void CONFIG::sleep(int duration, time_t &previous) {
|
3
|
300 ::sleep(duration);
|
4
|
301 time_t now = time(NULL);
|
|
302 recorder.leak(now-previous, *this);
|
|
303 previous = now;
|
3
|
304 }
|
|
305
|
|
306
|
|
307 bool CONFIG::looking(int ip) {
|
|
308 for (ippair_list::iterator i=ignore.begin(); i!=ignore.end(); i++) {
|
|
309 IPPAIR &p = *i;
|
|
310 if ((p.first <= ip) && (ip <= p.last)) return false;
|
|
311 }
|
|
312 return true;
|
|
313 }
|
|
314
|
|
315 ////////////////////////////////////////////////
|
|
316 //
|
|
317 SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, char *file_name_) {
|
4
|
318 tokp = &tok;
|
2
|
319 file_name = file_name_;
|
4
|
320 open(true);
|
1
|
321 }
|
|
322
|
|
323
|
|
324 SYSLOGCONFIG::~SYSLOGCONFIG() {
|
4
|
325 close();
|
3
|
326 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
|
|
327 PATTERN *p = *i;
|
|
328 delete p;
|
|
329 }
|
2
|
330 }
|
|
331
|
|
332
|
4
|
333 void SYSLOGCONFIG::open(bool msg) {
|
|
334 fd = ::open(file_name, O_RDONLY);
|
|
335 len = 0;
|
|
336 if (fd == -1) {
|
|
337 if (msg) {
|
|
338 char buf[maxlen];
|
|
339 snprintf(buf, sizeof(buf), "syslog file %s not readable", file_name);
|
|
340 tokp->token_error(buf);
|
|
341 }
|
|
342 }
|
|
343 else {
|
5
|
344 if (debug_syslog > 1) {
|
|
345 snprintf(buf, sizeof(buf), "syslog file %s opened", file_name);
|
|
346 my_syslog(buf);
|
|
347 }
|
4
|
348 lseek(fd, 0, SEEK_END);
|
|
349 if (fstat(fd, &openfdstat)) {
|
|
350 close();
|
|
351 snprintf(buf, sizeof(buf), "syslog file %s cannot stat after open", file_name);
|
|
352 tokp->token_error(buf);
|
|
353 }
|
31
|
354 // specify that this fd gets closed on exec, so that selinux
|
|
355 // won't complain about iptables trying to read log files.
|
|
356 int oldflags = fcntl(fd, F_GETFD, 0);
|
|
357 if (oldflags >= 0) {
|
|
358 fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC);
|
|
359 }
|
4
|
360 }
|
3
|
361 }
|
|
362
|
|
363
|
|
364 bool SYSLOGCONFIG::read(CONFIG &con) {
|
4
|
365 if (failed()) {
|
|
366 open(false);
|
|
367 if (failed()) return false;
|
|
368 }
|
3
|
369 int n = ::read(fd, buf+len, buflen-len);
|
|
370 bool have = (n > 0);
|
|
371 if (have) {
|
2
|
372 len += n;
|
|
373 while (true) {
|
|
374 char *p = (char*)memchr(buf, '\n', len);
|
|
375 if (!p) break;
|
|
376 n = p-buf;
|
|
377 *p = '\0';
|
3
|
378 process(con); // process null terminated string
|
2
|
379 len -= n+1;
|
|
380 memmove(buf, p+1, len);
|
|
381 }
|
|
382 // no <lf> in a full buffer
|
|
383 if (len == buflen) len = 0;
|
|
384 }
|
4
|
385 else {
|
|
386 // check for file close
|
|
387 struct stat filenamest;
|
|
388 if (0 == stat(file_name, &filenamest)) {
|
|
389 if ((filenamest.st_dev != openfdstat.st_dev) ||
|
|
390 (filenamest.st_ino != openfdstat.st_ino)) {
|
|
391 close();
|
|
392 }
|
|
393 }
|
|
394 else {
|
|
395 // filename no longer exists
|
|
396 close();
|
|
397 }
|
|
398 }
|
3
|
399 return have;
|
2
|
400 }
|
|
401
|
|
402
|
4
|
403 void SYSLOGCONFIG::close() {
|
5
|
404 if (debug_syslog > 1) {
|
|
405 snprintf(buf, sizeof(buf), "syslog file %s closed", file_name);
|
|
406 my_syslog(buf);
|
|
407 }
|
4
|
408 if (fd != -1) ::close(fd);
|
|
409 fd = -1;
|
|
410 }
|
|
411
|
|
412
|
|
413 void SYSLOGCONFIG::add_pattern(PATTERNP pat) {
|
|
414 patterns.push_back(pat);
|
|
415 }
|
|
416
|
|
417
|
3
|
418 void SYSLOGCONFIG::process(CONFIG &con) {
|
20
|
419 int pi=0;
|
3
|
420 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
|
|
421 PATTERN *p = *i;
|
20
|
422 if (p->process(buf, con, file_name, pi)) break;
|
|
423 pi++;
|
3
|
424 }
|
1
|
425 }
|
|
426
|
|
427
|
|
428 void SYSLOGCONFIG::dump(int level) {
|
|
429 char indent[maxlen];
|
|
430 int i = min(maxlen-1, level*4);
|
|
431 memset(indent, ' ', i);
|
|
432 indent[i] = '\0';
|
|
433 char buf[maxlen];
|
|
434 printf("%s file \"%s\" {\n", indent, file_name);
|
3
|
435 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
|
|
436 PATTERN *p = *i;
|
4
|
437 p->dump(level+1);
|
3
|
438 }
|
1
|
439 printf("%s }; \n", indent);
|
|
440 }
|
|
441
|
|
442
|
|
443 ////////////////////////////////////////////////
|
|
444 // helper to discard the strings held by a string_set
|
|
445 //
|
|
446 void discard(string_set &s) {
|
|
447 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
448 free(*i);
|
|
449 }
|
|
450 s.clear();
|
|
451 }
|
|
452
|
|
453
|
|
454 ////////////////////////////////////////////////
|
|
455 // helper to register a string in a string set
|
|
456 //
|
|
457 char* register_string(string_set &s, char *name) {
|
|
458 string_set::iterator i = s.find(name);
|
|
459 if (i != s.end()) return *i;
|
|
460 char *x = strdup(name);
|
|
461 s.insert(x);
|
|
462 return x;
|
|
463 }
|
|
464
|
|
465
|
|
466 ////////////////////////////////////////////////
|
|
467 // register a global string
|
|
468 //
|
|
469 char* register_string(char *name) {
|
|
470 return register_string(all_strings, name);
|
|
471 }
|
|
472
|
|
473
|
|
474 ////////////////////////////////////////////////
|
|
475 //
|
|
476 bool tsa(TOKEN &tok, char *token);
|
|
477 bool tsa(TOKEN &tok, char *token) {
|
|
478 char *have = tok.next();
|
|
479 if (have == token) return true;
|
|
480 tok.token_error(token, have);
|
|
481 return false;
|
|
482 }
|
|
483
|
|
484
|
|
485 ////////////////////////////////////////////////
|
|
486 //
|
3
|
487 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con);
|
|
488 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con) {
|
|
489 char *pat = tok.next();
|
|
490 int ind, buc;
|
1
|
491 if (!tsa(tok, token_lbrace)) return false;
|
|
492 while (true) {
|
|
493 char *have = tok.next();
|
3
|
494 if (!have) break;
|
|
495 if (have == token_rbrace) break;
|
|
496 if (have == token_index) {
|
1
|
497 have = tok.next();
|
3
|
498 ind = atoi(have);
|
1
|
499 if (!tsa(tok, token_semi)) return false;
|
|
500 }
|
3
|
501 else if (have == token_bucket) {
|
|
502 have = tok.next();
|
|
503 buc = atoi(have);
|
|
504 if (!tsa(tok, token_semi)) return false;
|
1
|
505 }
|
|
506 else {
|
3
|
507 tok.token_error("index/bucket", have);
|
1
|
508 return false;
|
|
509 }
|
|
510 }
|
|
511 if (!tsa(tok, token_semi)) return false;
|
3
|
512 PATTERNP patt = new PATTERN(tok, pat, ind, buc);
|
|
513 con.add_pattern(patt);
|
|
514 return true;
|
|
515 }
|
|
516
|
|
517
|
|
518 ////////////////////////////////////////////////
|
|
519 //
|
|
520 bool parse_ignore(TOKEN &tok, CONFIG &dc);
|
|
521 bool parse_ignore(TOKEN &tok, CONFIG &dc) {
|
|
522 if (!tsa(tok, token_lbrace)) return false;
|
|
523 while (true) {
|
|
524 char *have = tok.next();
|
|
525 if (!have) break;
|
|
526 if (have == token_rbrace) break;
|
|
527 int ipaddr = ip_address(have);
|
|
528 if (ipaddr == 0) {
|
|
529 tok.token_error("ip address", have);
|
|
530 return false;
|
|
531 }
|
|
532 if (!tsa(tok, token_slash)) return false;
|
|
533 have = tok.next();
|
|
534 int mask = atoi(have);
|
|
535 if ((mask < 8) || (mask > 32)) {
|
|
536 tok.token_error("cidr 8..32 value", have);
|
|
537 return false;
|
|
538 }
|
|
539 if (!tsa(tok, token_semi)) return false;
|
|
540 IPPAIR pair;
|
|
541 const int masks[33] = {0xffffffff, // 0
|
|
542 0x7fffffff, // 1
|
|
543 0x3fffffff, // 2
|
|
544 0x1fffffff, // 3
|
|
545 0x0fffffff, // 4
|
|
546 0x07ffffff, // 5
|
|
547 0x03ffffff, // 6
|
|
548 0x01ffffff, // 7
|
|
549 0x00ffffff, // 8
|
|
550 0x007fffff, // 9
|
|
551 0x003fffff, // 10
|
|
552 0x001fffff, // 11
|
|
553 0x000fffff, // 12
|
|
554 0x0007ffff, // 13
|
|
555 0x0003ffff, // 14
|
|
556 0x0001ffff, // 15
|
|
557 0x0000ffff, // 16
|
|
558 0x00007fff, // 17
|
|
559 0x00003fff, // 18
|
|
560 0x00001fff, // 19
|
|
561 0x00000fff, // 20
|
|
562 0x000007ff, // 21
|
|
563 0x000003ff, // 22
|
|
564 0x000001ff, // 23
|
|
565 0x000000ff, // 24
|
|
566 0x0000007f, // 25
|
|
567 0x0000003f, // 26
|
|
568 0x0000001f, // 27
|
|
569 0x0000000f, // 28
|
|
570 0x00000007, // 29
|
|
571 0x00000003, // 30
|
|
572 0x00000001, // 31
|
|
573 0x00000000}; // 32
|
|
574 pair.first = ipaddr;
|
|
575 pair.last = ipaddr | masks[mask];
|
|
576 pair.cidr = mask;
|
|
577 dc.add_pair(pair);
|
|
578 }
|
|
579 if (!tsa(tok, token_semi)) return false;
|
|
580 return true;
|
|
581 }
|
|
582
|
|
583
|
|
584 ////////////////////////////////////////////////
|
|
585 //
|
|
586 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc);
|
|
587 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc) {
|
|
588 char *name = tok.next();
|
|
589 if (!tsa(tok, token_lbrace)) return false;
|
|
590 SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name);
|
2
|
591 if (con->failed()) {
|
|
592 delete con;
|
|
593 return false;
|
|
594 }
|
1
|
595 dc.add_syslogconfig(con);
|
3
|
596 while (true) {
|
|
597 char *have = tok.next();
|
|
598 if (!have) break;
|
|
599 if (have == token_rbrace) break;
|
|
600 if (have == token_pattern) {
|
|
601 if (!parse_pattern(tok, *con)) return false;
|
|
602 }
|
|
603 else {
|
|
604 tok.token_error("pattern", have);
|
|
605 return false;
|
|
606 }
|
|
607 }
|
|
608 if (!tsa(tok, token_semi)) return false;
|
1
|
609 return true;
|
|
610 }
|
|
611
|
|
612
|
|
613 ////////////////////////////////////////////////
|
|
614 // parse a config file
|
|
615 //
|
|
616 bool load_conf(CONFIG &dc, char *fn) {
|
|
617 int count = 0;
|
|
618 TOKEN tok(fn, &dc.config_files);
|
|
619 while (true) {
|
|
620 char *have = tok.next();
|
|
621 if (!have) break;
|
3
|
622 if (have == token_threshold) {
|
|
623 have = tok.next();
|
|
624 dc.set_threshold(atoi(have));
|
|
625 if (!tsa(tok, token_semi)) return false;
|
|
626 }
|
|
627 else if (have == token_ignore) {
|
|
628 if (!parse_ignore(tok, dc)) return false;
|
|
629 }
|
27
|
630 else if (have == token_add) {
|
|
631 have = tok.next();
|
|
632 dc.set_add(have);
|
|
633 if (!tsa(tok, token_semi)) return false;
|
|
634 }
|
|
635 else if (have == token_remove) {
|
|
636 have = tok.next();
|
|
637 dc.set_remove(have);
|
|
638 if (!tsa(tok, token_semi)) return false;
|
|
639 }
|
3
|
640 else if (have == token_file) {
|
|
641 if (!parse_syslogconfig(tok, dc)) return false;
|
|
642 count++;
|
1
|
643 }
|
|
644 else {
|
28
|
645 tok.token_error("threshold/ignore/add_command/remove_command/file", have);
|
1
|
646 return false;
|
|
647 }
|
|
648 }
|
|
649 tok.token_error("load_conf() found %d syslog files in %s", count, fn);
|
|
650 return (!dc.syslogconfigs.empty());
|
|
651 }
|
|
652
|
|
653
|
|
654 ////////////////////////////////////////////////
|
|
655 // init the tokens
|
|
656 //
|
|
657 void token_init() {
|
27
|
658 token_add = register_string("add_command");
|
3
|
659 token_bucket = register_string("bucket");
|
1
|
660 token_file = register_string("file");
|
3
|
661 token_ignore = register_string("ignore");
|
1
|
662 token_include = register_string("include");
|
3
|
663 token_index = register_string("index");
|
1
|
664 token_lbrace = register_string("{");
|
3
|
665 token_pattern = register_string("pattern");
|
1
|
666 token_rbrace = register_string("}");
|
27
|
667 token_remove = register_string("remove_command");
|
1
|
668 token_semi = register_string(";");
|
3
|
669 token_slash = register_string("/");
|
|
670 token_threshold = register_string("threshold");
|
1
|
671 }
|