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