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 }
|
|
354 }
|
3
|
355 }
|
|
356
|
|
357
|
|
358 bool SYSLOGCONFIG::read(CONFIG &con) {
|
4
|
359 if (failed()) {
|
|
360 open(false);
|
|
361 if (failed()) return false;
|
|
362 }
|
3
|
363 int n = ::read(fd, buf+len, buflen-len);
|
|
364 bool have = (n > 0);
|
|
365 if (have) {
|
2
|
366 len += n;
|
|
367 while (true) {
|
|
368 char *p = (char*)memchr(buf, '\n', len);
|
|
369 if (!p) break;
|
|
370 n = p-buf;
|
|
371 *p = '\0';
|
3
|
372 process(con); // process null terminated string
|
2
|
373 len -= n+1;
|
|
374 memmove(buf, p+1, len);
|
|
375 }
|
|
376 // no <lf> in a full buffer
|
|
377 if (len == buflen) len = 0;
|
|
378 }
|
4
|
379 else {
|
|
380 // check for file close
|
|
381 struct stat filenamest;
|
|
382 if (0 == stat(file_name, &filenamest)) {
|
|
383 if ((filenamest.st_dev != openfdstat.st_dev) ||
|
|
384 (filenamest.st_ino != openfdstat.st_ino)) {
|
|
385 close();
|
|
386 }
|
|
387 }
|
|
388 else {
|
|
389 // filename no longer exists
|
|
390 close();
|
|
391 }
|
|
392 }
|
3
|
393 return have;
|
2
|
394 }
|
|
395
|
|
396
|
4
|
397 void SYSLOGCONFIG::close() {
|
5
|
398 if (debug_syslog > 1) {
|
|
399 snprintf(buf, sizeof(buf), "syslog file %s closed", file_name);
|
|
400 my_syslog(buf);
|
|
401 }
|
4
|
402 if (fd != -1) ::close(fd);
|
|
403 fd = -1;
|
|
404 }
|
|
405
|
|
406
|
|
407 void SYSLOGCONFIG::add_pattern(PATTERNP pat) {
|
|
408 patterns.push_back(pat);
|
|
409 }
|
|
410
|
|
411
|
3
|
412 void SYSLOGCONFIG::process(CONFIG &con) {
|
20
|
413 int pi=0;
|
3
|
414 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
|
|
415 PATTERN *p = *i;
|
20
|
416 if (p->process(buf, con, file_name, pi)) break;
|
|
417 pi++;
|
3
|
418 }
|
1
|
419 }
|
|
420
|
|
421
|
|
422 void SYSLOGCONFIG::dump(int level) {
|
|
423 char indent[maxlen];
|
|
424 int i = min(maxlen-1, level*4);
|
|
425 memset(indent, ' ', i);
|
|
426 indent[i] = '\0';
|
|
427 char buf[maxlen];
|
|
428 printf("%s file \"%s\" {\n", indent, file_name);
|
3
|
429 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
|
|
430 PATTERN *p = *i;
|
4
|
431 p->dump(level+1);
|
3
|
432 }
|
1
|
433 printf("%s }; \n", indent);
|
|
434 }
|
|
435
|
|
436
|
|
437 ////////////////////////////////////////////////
|
|
438 // helper to discard the strings held by a string_set
|
|
439 //
|
|
440 void discard(string_set &s) {
|
|
441 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
442 free(*i);
|
|
443 }
|
|
444 s.clear();
|
|
445 }
|
|
446
|
|
447
|
|
448 ////////////////////////////////////////////////
|
|
449 // helper to register a string in a string set
|
|
450 //
|
|
451 char* register_string(string_set &s, char *name) {
|
|
452 string_set::iterator i = s.find(name);
|
|
453 if (i != s.end()) return *i;
|
|
454 char *x = strdup(name);
|
|
455 s.insert(x);
|
|
456 return x;
|
|
457 }
|
|
458
|
|
459
|
|
460 ////////////////////////////////////////////////
|
|
461 // register a global string
|
|
462 //
|
|
463 char* register_string(char *name) {
|
|
464 return register_string(all_strings, name);
|
|
465 }
|
|
466
|
|
467
|
|
468 ////////////////////////////////////////////////
|
|
469 //
|
|
470 bool tsa(TOKEN &tok, char *token);
|
|
471 bool tsa(TOKEN &tok, char *token) {
|
|
472 char *have = tok.next();
|
|
473 if (have == token) return true;
|
|
474 tok.token_error(token, have);
|
|
475 return false;
|
|
476 }
|
|
477
|
|
478
|
|
479 ////////////////////////////////////////////////
|
|
480 //
|
3
|
481 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con);
|
|
482 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con) {
|
|
483 char *pat = tok.next();
|
|
484 int ind, buc;
|
1
|
485 if (!tsa(tok, token_lbrace)) return false;
|
|
486 while (true) {
|
|
487 char *have = tok.next();
|
3
|
488 if (!have) break;
|
|
489 if (have == token_rbrace) break;
|
|
490 if (have == token_index) {
|
1
|
491 have = tok.next();
|
3
|
492 ind = atoi(have);
|
1
|
493 if (!tsa(tok, token_semi)) return false;
|
|
494 }
|
3
|
495 else if (have == token_bucket) {
|
|
496 have = tok.next();
|
|
497 buc = atoi(have);
|
|
498 if (!tsa(tok, token_semi)) return false;
|
1
|
499 }
|
|
500 else {
|
3
|
501 tok.token_error("index/bucket", have);
|
1
|
502 return false;
|
|
503 }
|
|
504 }
|
|
505 if (!tsa(tok, token_semi)) return false;
|
3
|
506 PATTERNP patt = new PATTERN(tok, pat, ind, buc);
|
|
507 con.add_pattern(patt);
|
|
508 return true;
|
|
509 }
|
|
510
|
|
511
|
|
512 ////////////////////////////////////////////////
|
|
513 //
|
|
514 bool parse_ignore(TOKEN &tok, CONFIG &dc);
|
|
515 bool parse_ignore(TOKEN &tok, CONFIG &dc) {
|
|
516 if (!tsa(tok, token_lbrace)) return false;
|
|
517 while (true) {
|
|
518 char *have = tok.next();
|
|
519 if (!have) break;
|
|
520 if (have == token_rbrace) break;
|
|
521 int ipaddr = ip_address(have);
|
|
522 if (ipaddr == 0) {
|
|
523 tok.token_error("ip address", have);
|
|
524 return false;
|
|
525 }
|
|
526 if (!tsa(tok, token_slash)) return false;
|
|
527 have = tok.next();
|
|
528 int mask = atoi(have);
|
|
529 if ((mask < 8) || (mask > 32)) {
|
|
530 tok.token_error("cidr 8..32 value", have);
|
|
531 return false;
|
|
532 }
|
|
533 if (!tsa(tok, token_semi)) return false;
|
|
534 IPPAIR pair;
|
|
535 const int masks[33] = {0xffffffff, // 0
|
|
536 0x7fffffff, // 1
|
|
537 0x3fffffff, // 2
|
|
538 0x1fffffff, // 3
|
|
539 0x0fffffff, // 4
|
|
540 0x07ffffff, // 5
|
|
541 0x03ffffff, // 6
|
|
542 0x01ffffff, // 7
|
|
543 0x00ffffff, // 8
|
|
544 0x007fffff, // 9
|
|
545 0x003fffff, // 10
|
|
546 0x001fffff, // 11
|
|
547 0x000fffff, // 12
|
|
548 0x0007ffff, // 13
|
|
549 0x0003ffff, // 14
|
|
550 0x0001ffff, // 15
|
|
551 0x0000ffff, // 16
|
|
552 0x00007fff, // 17
|
|
553 0x00003fff, // 18
|
|
554 0x00001fff, // 19
|
|
555 0x00000fff, // 20
|
|
556 0x000007ff, // 21
|
|
557 0x000003ff, // 22
|
|
558 0x000001ff, // 23
|
|
559 0x000000ff, // 24
|
|
560 0x0000007f, // 25
|
|
561 0x0000003f, // 26
|
|
562 0x0000001f, // 27
|
|
563 0x0000000f, // 28
|
|
564 0x00000007, // 29
|
|
565 0x00000003, // 30
|
|
566 0x00000001, // 31
|
|
567 0x00000000}; // 32
|
|
568 pair.first = ipaddr;
|
|
569 pair.last = ipaddr | masks[mask];
|
|
570 pair.cidr = mask;
|
|
571 dc.add_pair(pair);
|
|
572 }
|
|
573 if (!tsa(tok, token_semi)) return false;
|
|
574 return true;
|
|
575 }
|
|
576
|
|
577
|
|
578 ////////////////////////////////////////////////
|
|
579 //
|
|
580 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc);
|
|
581 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc) {
|
|
582 char *name = tok.next();
|
|
583 if (!tsa(tok, token_lbrace)) return false;
|
|
584 SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name);
|
2
|
585 if (con->failed()) {
|
|
586 delete con;
|
|
587 return false;
|
|
588 }
|
1
|
589 dc.add_syslogconfig(con);
|
3
|
590 while (true) {
|
|
591 char *have = tok.next();
|
|
592 if (!have) break;
|
|
593 if (have == token_rbrace) break;
|
|
594 if (have == token_pattern) {
|
|
595 if (!parse_pattern(tok, *con)) return false;
|
|
596 }
|
|
597 else {
|
|
598 tok.token_error("pattern", have);
|
|
599 return false;
|
|
600 }
|
|
601 }
|
|
602 if (!tsa(tok, token_semi)) return false;
|
1
|
603 return true;
|
|
604 }
|
|
605
|
|
606
|
|
607 ////////////////////////////////////////////////
|
|
608 // parse a config file
|
|
609 //
|
|
610 bool load_conf(CONFIG &dc, char *fn) {
|
|
611 int count = 0;
|
|
612 TOKEN tok(fn, &dc.config_files);
|
|
613 while (true) {
|
|
614 char *have = tok.next();
|
|
615 if (!have) break;
|
3
|
616 if (have == token_threshold) {
|
|
617 have = tok.next();
|
|
618 dc.set_threshold(atoi(have));
|
|
619 if (!tsa(tok, token_semi)) return false;
|
|
620 }
|
|
621 else if (have == token_ignore) {
|
|
622 if (!parse_ignore(tok, dc)) return false;
|
|
623 }
|
27
|
624 else if (have == token_add) {
|
|
625 have = tok.next();
|
|
626 dc.set_add(have);
|
|
627 if (!tsa(tok, token_semi)) return false;
|
|
628 }
|
|
629 else if (have == token_remove) {
|
|
630 have = tok.next();
|
|
631 dc.set_remove(have);
|
|
632 if (!tsa(tok, token_semi)) return false;
|
|
633 }
|
3
|
634 else if (have == token_file) {
|
|
635 if (!parse_syslogconfig(tok, dc)) return false;
|
|
636 count++;
|
1
|
637 }
|
|
638 else {
|
28
|
639 tok.token_error("threshold/ignore/add_command/remove_command/file", have);
|
1
|
640 return false;
|
|
641 }
|
|
642 }
|
|
643 tok.token_error("load_conf() found %d syslog files in %s", count, fn);
|
|
644 return (!dc.syslogconfigs.empty());
|
|
645 }
|
|
646
|
|
647
|
|
648 ////////////////////////////////////////////////
|
|
649 // init the tokens
|
|
650 //
|
|
651 void token_init() {
|
27
|
652 token_add = register_string("add_command");
|
3
|
653 token_bucket = register_string("bucket");
|
1
|
654 token_file = register_string("file");
|
3
|
655 token_ignore = register_string("ignore");
|
1
|
656 token_include = register_string("include");
|
3
|
657 token_index = register_string("index");
|
1
|
658 token_lbrace = register_string("{");
|
3
|
659 token_pattern = register_string("pattern");
|
1
|
660 token_rbrace = register_string("}");
|
27
|
661 token_remove = register_string("remove_command");
|
1
|
662 token_semi = register_string(";");
|
3
|
663 token_slash = register_string("/");
|
|
664 token_threshold = register_string("threshold");
|
1
|
665 }
|