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