source: trunk/src/sh_log_evalrule.c@ 356

Last change on this file since 356 was 337, checked in by katerina, 14 years ago

Fix segfault on reload in sh_log_evalrule.c

File size: 29.2 KB
Line 
1
2#include "config_xor.h"
3
4#include <stdio.h>
5#include <stdarg.h>
6#include <string.h>
7#include <ctype.h>
8#include <time.h>
9#include <limits.h>
10#include <sys/types.h>
11
12#ifdef USE_LOGFILE_MONITOR
13
14#undef FIL__
15#define FIL__ _("sh_log_evalrule.c")
16
17/* Debian/Ubuntu: libpcre3-dev */
18#ifdef HAVE_PCRE_PCRE_H
19#include <pcre/pcre.h>
20#else
21#include <pcre.h>
22#endif
23
24#ifndef PCRE_NO_AUTO_CAPTURE
25#define PCRE_NO_AUTO_CAPTURE 0
26#endif
27
28#include "samhain.h"
29#include "sh_pthread.h"
30#include "sh_utils.h"
31#include "sh_string.h"
32#include "sh_log_check.h"
33#include "sh_log_evalrule.h"
34#include "sh_log_correlate.h"
35#include "sh_log_mark.h"
36#include "sh_log_repeat.h"
37#include "zAVLTree.h"
38
39extern int flag_err_debug;
40
41/* #define DEBUG_EVALRULES */
42
43#ifdef DEBUG_EVALRULES
44static void DEBUG(const char *fmt, ...)
45{
46 va_list ap;
47 va_start(ap, fmt);
48 vfprintf(stderr, fmt, ap); /* flawfinder: ignore *//* we control fmt string */
49 va_end(ap);
50 return;
51}
52#else
53static void DEBUG(const char *fmt, ...)
54{
55 (void) fmt;
56 return;
57}
58#endif
59
60struct sh_ceval /* Counter for summarizing */
61{
62 sh_string * hostname;
63 sh_string * counted_str;
64 sh_string * filename;
65 unsigned long count;
66 time_t start;
67 time_t interval;
68};
69
70void sh_ceval_free(void * item)
71{
72 struct sh_ceval * counter = (struct sh_ceval *) item;
73 if (!counter)
74 return;
75 sh_string_destroy(&(counter->hostname));
76 sh_string_destroy(&(counter->counted_str));
77 sh_string_destroy(&(counter->filename));
78 SH_FREE(counter);
79}
80
81enum {
82 RFL_ISRULE = 1 << 0,
83 RFL_ISGROUP = 1 << 1,
84 RFL_KEEP = 1 << 2,
85 RFL_MARK = 1 << 3
86};
87
88
89/*--------------------------------------------------------------
90 *
91 * Adding rules/groups/hosts
92 *
93 *--------------------------------------------------------------*/
94
95struct sh_geval /* Group of rules (may be a single rule) */
96{
97 sh_string * label; /* label for this group */
98 pcre * rule; /* compiled regex for rule */
99 pcre_extra * rule_extra;
100 int * ovector; /* captured substrings */
101 int ovecnum; /* how many captured */
102 int captures; /* (captures+1)*3 required */
103 int flags; /* bit flags */
104 unsigned long delay; /* delay for keep rules */
105 zAVLTree * counterlist; /* counters if EVAL_SUM */
106 struct sh_qeval * queue; /* queue for this rule */
107 struct sh_geval * nextrule; /* next rule in this group */
108 struct sh_geval * next; /* next group of rules */
109 struct sh_geval * gnext; /* grouplist next */
110};
111
112struct sh_heval /* host-specific rules */
113{
114 pcre * hostname; /* compiled regex for hostname */
115 pcre_extra * hostname_extra;
116 struct sh_geval * rulegroups; /* list of group of rules */
117 struct sh_heval * next;
118};
119
120static struct sh_heval * hostlist = NULL;
121static struct sh_qeval * queuelist = NULL;
122static struct sh_geval * grouplist = NULL;
123
124/* These flags are set if we are within
125 * the define of a host/rule group.
126 */
127static struct sh_heval * host_open = NULL;
128static struct sh_geval * group_open = NULL;
129
130int sh_eval_gend (const char * str)
131{
132 (void) str;
133 if (group_open) {
134 group_open = NULL;
135 return 0;
136 }
137 return -1;
138}
139
140int sh_eval_gadd (const char * str)
141{
142 struct sh_geval * ng;
143 struct sh_geval * tmp;
144 pcre * group;
145 pcre_extra * group_extra;
146 const char * error;
147 int erroffset;
148 unsigned int nfields = 2;
149 size_t lengths[2];
150 char * new = sh_util_strdup(str);
151 char ** splits = split_array(new, &nfields, ':', lengths);
152
153 /* group is label:regex
154 */
155
156 if (group_open)
157 group_open = NULL;
158
159 if (nfields != 2)
160 {
161 SH_FREE(splits);
162 SH_FREE(new);
163 return -1;
164 }
165
166 group = pcre_compile(splits[1], PCRE_NO_AUTO_CAPTURE,
167 &error, &erroffset, NULL);
168 if (!group)
169 {
170 sh_string * msg = sh_string_new(0);
171 sh_string_add_from_char(msg, _("Bad regex: "));
172 sh_string_add_from_char(msg, splits[1]);
173
174 SH_MUTEX_LOCK(mutex_thread_nolog);
175 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
176 sh_string_str(msg),
177 _("sh_eval_gadd"));
178 SH_MUTEX_UNLOCK(mutex_thread_nolog);
179 sh_string_destroy(&msg);
180
181 SH_FREE(splits);
182 SH_FREE(new);
183 return -1;
184 }
185 group_extra = NULL; /* pcre_study(group, 0, &error); */
186
187 ng = SH_ALLOC(sizeof(struct sh_geval));
188 memset(ng, '\0', sizeof(struct sh_geval));
189
190 ng->label = sh_string_new_from_lchar(splits[0], lengths[0]);
191 ng->flags = RFL_ISGROUP;
192
193 ng->rule = group;
194 ng->rule_extra = group_extra;
195 ng->ovector = NULL;
196 ng->ovecnum = 0;
197 ng->captures = 0;
198 ng->counterlist = NULL;
199 ng->queue = NULL;
200 ng->nextrule = NULL;
201 ng->next = NULL;
202 ng->gnext = NULL;
203
204 if (!host_open)
205 {
206 if (0 != sh_eval_hadd("^.*"))
207 {
208 pcre_free(group);
209 sh_string_destroy(&(ng->label));
210 SH_FREE(splits);
211 SH_FREE(new);
212 SH_FREE(ng);
213 return -1;
214 }
215 }
216
217 /*
218 * Insert at end, to keep user-defined order
219 */
220
221 if (host_open)
222 {
223 if (grouplist)
224 {
225 tmp = grouplist;
226 while (tmp->gnext != NULL) { tmp = tmp->gnext; }
227 tmp->gnext = ng;
228 } else {
229 grouplist = ng;
230 }
231
232
233 /*
234 * If there is an open host group, add it to its
235 * rulegroups
236 */
237
238 if (host_open->rulegroups)
239 {
240 tmp = host_open->rulegroups;
241 while (tmp->next != NULL) { tmp = tmp->next; }
242 tmp->next = ng;
243 } else {
244 host_open->rulegroups = ng;
245 }
246 }
247
248 group_open = ng;
249 SH_FREE(splits);
250 SH_FREE(new);
251 return 0;
252}
253
254int sh_eval_hend (const char * str)
255{
256 (void) str;
257 if (host_open) {
258 host_open = NULL;
259 return 0;
260 }
261 return -1;
262}
263
264int sh_eval_hadd (const char * str)
265{
266 struct sh_heval * nh;
267 struct sh_heval * tmp;
268 pcre * host;
269 pcre_extra * host_extra;
270 const char * error;
271 int erroffset;
272
273 if (host_open)
274 host_open = NULL;
275
276 host = pcre_compile(str, PCRE_NO_AUTO_CAPTURE,
277 &error, &erroffset, NULL);
278 if (!host)
279 {
280 sh_string * msg = sh_string_new(0);
281 sh_string_add_from_char(msg, _("Bad regex: "));
282 sh_string_add_from_char(msg, str);
283
284 SH_MUTEX_LOCK(mutex_thread_nolog);
285 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
286 sh_string_str(msg),
287 _("sh_eval_hadd"));
288 SH_MUTEX_UNLOCK(mutex_thread_nolog);
289 sh_string_destroy(&msg);
290
291 return -1;
292 }
293 host_extra = NULL; /* pcre_study(host, 0, &error); */
294
295 nh = SH_ALLOC(sizeof(struct sh_heval));
296 memset(nh, '\0', sizeof(struct sh_heval));
297
298 nh->hostname = host;
299 nh->hostname_extra = host_extra;
300 nh->rulegroups = NULL;
301
302 /*
303 * Insert at end, to keep user-defined order
304 */
305 nh->next = NULL;
306 if (hostlist) {
307 tmp = hostlist;
308 while (tmp->next != NULL) { tmp = tmp->next; }
309 tmp->next = nh;
310 } else {
311 hostlist = nh;
312 }
313 host_open = nh;
314
315 return 0;
316}
317
318int sh_eval_qadd (const char * str)
319{
320 struct sh_qeval * nq;
321 int severity;
322 unsigned int nfields = 5; /* label:interval:(report|sum):severity[:alias] */
323 size_t lengths[5];
324 char * new = sh_util_strdup(str);
325 char ** splits = split_array(new, &nfields, ':', lengths);
326
327 if (nfields < 4)
328 {
329 SH_FREE(splits);
330 SH_FREE(new);
331 return -1;
332 }
333
334 if (strcmp(splits[2], _("sum")) && strcmp(splits[2], _("report")))
335 {
336 SH_FREE(splits);
337 SH_FREE(new);
338 return -1;
339 }
340
341 if (!strcmp(splits[2], _("sum")) && atoi(splits[1]) < 0)
342 {
343 SH_FREE(splits);
344 SH_FREE(new);
345 return -1;
346 }
347
348 if (!strcmp(splits[1], _("trash"))) /* predefined, reserved */
349 {
350 SH_FREE(splits);
351 SH_FREE(new);
352 return -1;
353 }
354
355 severity = sh_error_convert_level (splits[3]);
356 if (severity < 0)
357 {
358 SH_FREE(splits);
359 SH_FREE(new);
360 return -1;
361 }
362
363 nq = SH_ALLOC(sizeof(struct sh_qeval));
364 memset(nq, '\0', sizeof(struct sh_qeval));
365
366 nq->label = sh_string_new_from_lchar(splits[0], lengths[0]);
367 nq->alias = NULL;
368
369 DEBUG("debug: splits[2] = %s, policy = %d\n",splits[2],nq->policy);
370 if (0 == strcmp(splits[2], _("report"))) {
371 nq->policy = EVAL_REPORT;
372 nq->interval = 0;
373 }
374 else {
375 nq->policy = EVAL_SUM;
376 nq->interval = (time_t) atoi(splits[1]);
377 }
378
379 nq->severity = severity;
380
381 if (nfields == 5)
382 {
383 nq->alias = sh_string_new_from_lchar(splits[4], lengths[4]);
384 }
385
386 nq->next = queuelist;
387 queuelist = nq;
388
389 SH_FREE(splits);
390 SH_FREE(new);
391 return 0;
392}
393
394struct sh_qeval * sh_log_find_queue(const char * str)
395{
396 struct sh_qeval * retval = queuelist;
397
398 if (!str)
399 return NULL;
400
401 while (retval)
402 {
403 if (0 == strcmp(str, sh_string_str(retval->label)))
404 break;
405 retval = retval->next;
406 }
407 return retval;
408}
409
410int sh_log_lookup_severity(const char * str)
411{
412 struct sh_qeval * queue;
413
414 if (str)
415 {
416 if (0 != strcmp(str, _("trash")))
417 {
418 queue = sh_log_find_queue(str);
419
420 if (queue)
421 return queue->severity;
422 }
423 }
424 return SH_ERR_SEVERE;
425}
426
427sh_string * sh_log_lookup_alias(const char * str)
428{
429 struct sh_qeval * queue;
430
431 if (str)
432 {
433 if (0 != strcmp(str, _("trash")))
434 {
435 queue = sh_log_find_queue(str);
436
437 if (queue)
438 return queue->alias;
439 }
440 }
441 return NULL;
442}
443
444
445static char * get_label_and_time(const char * inprefix, char * str,
446 unsigned long * seconds)
447{
448 char * res = NULL;
449 char * endptr = NULL;
450
451 unsigned int nfields = 2; /* seconds:label */
452 size_t lengths[2];
453 char * prefix = sh_util_strdup(inprefix);
454 char * new = sh_util_strdup(str);
455 char ** splits = split_array_braced(new, prefix, &nfields, lengths);
456
457 if (splits && nfields == 2 && lengths[0] > 0 && lengths[1] > 0)
458 {
459 *seconds = strtoul(splits[0], &endptr, 10);
460 if ((endptr == '\0' || endptr != splits[0]) && (*seconds != ULONG_MAX))
461 {
462 res = sh_util_strdup(splits[1]);
463 }
464 }
465 if (splits)
466 SH_FREE(splits);
467 SH_FREE(new);
468 SH_FREE(prefix);
469 return res;
470}
471
472static struct sh_qeval ** dummy_queue;
473static char ** dummy_dstr;
474
475int sh_eval_radd (const char * str)
476{
477 struct sh_geval * nr;
478 struct sh_geval * tmp;
479 struct sh_qeval * queue = NULL;
480 pcre * rule;
481 pcre_extra * rule_extra;
482 const char * error;
483 int erroffset;
484 int captures = 0;
485 unsigned int nfields = 2; /* queue:regex */
486 size_t lengths[3];
487 char * new = sh_util_strdup(str);
488 char ** splits;
489
490 int qpos = 0;
491 volatile int rpos = 1;
492 unsigned long dsec = 0;
493 char * dstr = NULL;
494 char * s = new;
495 volatile char pflag = '-';
496
497 while ( *s && isspace((int)*s) ) ++s;
498 if (0 == strncmp(s, _("KEEP"), 4) ||
499 0 == strncmp(s, _("CORRELATE"), 9) ||
500 0 == strncmp(s, _("MARK"), 4))
501 {
502 pflag = s[0];
503 nfields = 3;
504 }
505
506 splits = split_array(new, &nfields, ':', lengths);
507
508 dummy_queue = &queue;
509 dummy_dstr = &dstr;
510
511 if (nfields < 2 || nfields > 3)
512 {
513 SH_FREE(splits);
514 SH_FREE(new);
515 return -1;
516 }
517
518 if (nfields == 3)
519 {
520 if (pflag == 'K')
521 {
522 /* KEEP(nsec,label):queue:regex
523 */
524 dstr = get_label_and_time(_("KEEP"), splits[0], &dsec);
525 if (!dstr)
526 {
527 SH_FREE(splits);
528 SH_FREE(new);
529 return -1;
530 }
531 }
532 else if (pflag == 'C')
533 {
534 /* CORRELATE(description):queue:regex
535 */
536 int retval = sh_keep_match_add(splits[0], splits[1], splits[2]);
537 SH_FREE(splits);
538 SH_FREE(new);
539 return retval;
540 }
541 else if (pflag == 'M')
542 {
543 /* MARK(description, interval):queue:regex
544 */
545 int retval = -1;
546
547 dstr = get_label_and_time(_("MARK"), splits[0], &dsec);
548 if (dstr)
549 {
550 retval = sh_log_mark_add(dstr, dsec, splits[1]);
551 }
552 if (retval != 0)
553 {
554 SH_FREE(splits);
555 SH_FREE(new);
556 return retval;
557 }
558 }
559 ++qpos; ++rpos;
560 }
561
562 if (0 != strcmp(splits[qpos], _("trash")))
563 {
564 queue = sh_log_find_queue(splits[qpos]);
565 if (!queue)
566 {
567 SH_FREE(splits);
568 SH_FREE(new);
569 return -1;
570 }
571 }
572
573 rule = pcre_compile(splits[rpos], 0,
574 &error, &erroffset, NULL);
575 if (!rule)
576 {
577 sh_string * msg = sh_string_new(0);
578 sh_string_add_from_char(msg, _("Bad regex: "));
579 sh_string_add_from_char(msg, splits[rpos]);
580
581 SH_MUTEX_LOCK(mutex_thread_nolog);
582 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
583 sh_string_str(msg),
584 _("sh_eval_radd"));
585 SH_MUTEX_UNLOCK(mutex_thread_nolog);
586 sh_string_destroy(&msg);
587
588 SH_FREE(splits);
589 SH_FREE(new);
590 return -1;
591 }
592 rule_extra = NULL; /* pcre_study(rule, 0, &error); */
593 pcre_fullinfo(rule, rule_extra, PCRE_INFO_CAPTURECOUNT, &captures);
594
595 if (flag_err_debug == SL_TRUE)
596 {
597 char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
598 if (dstr)
599 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Adding rule: |%s| with %d captures, keep(%lu,%s)"),
600 splits[rpos], captures, dsec, dstr);
601 else
602 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Adding rule: |%s| with %d captures"),
603 splits[rpos], captures);
604 SH_MUTEX_LOCK(mutex_thread_nolog);
605 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
606 emsg, _("sh_eval_radd"));
607 SH_MUTEX_UNLOCK(mutex_thread_nolog);
608 SH_FREE(emsg);
609 }
610
611 DEBUG("adding rule: |%s| with %d captures\n", splits[rpos], captures);
612
613 SH_FREE(splits);
614 SH_FREE(new);
615
616 nr = SH_ALLOC(sizeof(struct sh_geval));
617 memset(nr, '\0', sizeof(struct sh_geval));
618
619 nr->label = NULL;
620 nr->flags = RFL_ISRULE;
621 nr->delay = 0;
622
623 nr->rule = rule;
624 nr->rule_extra = rule_extra;
625 nr->captures = captures;
626 nr->ovector = SH_ALLOC(sizeof(int) * (captures+1) * 3);
627 nr->ovecnum = 0;
628 nr->counterlist = NULL;
629 nr->queue = queue;
630 nr->nextrule = NULL;
631 nr->next = NULL;
632 nr->gnext = NULL;
633
634
635 if (pflag == 'K')
636 {
637 nr->label = sh_string_new_from_lchar(dstr, strlen(dstr));
638 nr->flags |= RFL_KEEP;
639 nr->delay = dsec;
640 SH_FREE(dstr);
641 }
642 else if (pflag == 'M')
643 {
644 nr->label = sh_string_new_from_lchar(dstr, strlen(dstr));
645 nr->flags |= RFL_MARK;
646 nr->delay = dsec;
647 SH_FREE(dstr);
648 }
649
650 /*
651 * If there is an open group, add it to its
652 * rules
653 */
654 if (group_open)
655 {
656 if (flag_err_debug == SL_TRUE)
657 {
658 char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
659 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Adding rule to group |%s|"),
660 sh_string_str(group_open->label));
661 SH_MUTEX_LOCK(mutex_thread_nolog);
662 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
663 emsg, _("sh_eval_radd"));
664 SH_MUTEX_UNLOCK(mutex_thread_nolog);
665 SH_FREE(emsg);
666 }
667
668 DEBUG("adding rule to group |%s|\n", sh_string_str(group_open->label));
669
670 if (group_open->nextrule)
671 {
672 tmp = group_open->nextrule;
673 while (tmp->nextrule != NULL) { tmp = tmp->nextrule; } /* next -> nextrule */
674 tmp->nextrule = nr; /* next -> nextrule */
675 } else {
676 group_open->nextrule = nr;
677 }
678 }
679
680 /*
681 * ..else, add it to the currently open host (open the
682 * default host, if there is no open one)
683 */
684 else
685 {
686 if (!host_open)
687 {
688 if (0 != sh_eval_hadd("^.*"))
689 {
690 if (nr->label)
691 sh_string_destroy(&(nr->label));
692 SH_FREE(nr->ovector);
693 SH_FREE(nr);
694 return -1;
695 }
696 }
697
698 if (host_open)
699 {
700 /*
701 * Add rule as member to grouplist, to facilitate cleanup
702 */
703
704 DEBUG("adding solitary rule to grouplist\n");
705
706 if (grouplist)
707 {
708 tmp = grouplist;
709 while (tmp->gnext != NULL) { tmp = tmp->gnext; }
710 tmp->gnext = nr;
711 } else {
712 grouplist = nr;
713 }
714
715
716 /*
717 * Add rule to host rulegroups
718 */
719 DEBUG("adding solitary rule to host rulegroups\n");
720
721 if (host_open->rulegroups)
722 {
723 /* Second, third, ... rule go to host_open->rulegroups->next,
724 * since test_grules() iterates over nextrules
725 */
726 tmp = host_open->rulegroups;
727 while (tmp->next != NULL) { tmp = tmp->next; }
728 tmp->next = nr;
729 }
730 else
731 {
732 /* First rule goes to host_open->rulegroups */
733 host_open->rulegroups = nr;
734 }
735 }
736 else
737 {
738 if (nr->label)
739 sh_string_destroy(&(nr->label));
740 SH_FREE(nr->ovector);
741 SH_FREE(nr);
742 return -1;
743 }
744 }
745
746 return 0;
747}
748
749void sh_eval_cleanup()
750{
751 struct sh_geval * gtmp;
752 struct sh_qeval * qtmp;
753 struct sh_heval * htmp;
754
755 while (grouplist)
756 {
757 gtmp = grouplist;
758 grouplist = gtmp->gnext;
759
760 if (gtmp->label) sh_string_destroy(&(gtmp->label));
761 if (gtmp->rule_extra) (*pcre_free)(gtmp->rule_extra);
762 if (gtmp->rule) (*pcre_free)(gtmp->rule);
763 if (gtmp->counterlist)
764 zAVLFreeTree(gtmp->counterlist, sh_ceval_free);
765 if (gtmp->ovector)
766 SH_FREE(gtmp->ovector);
767#if 0
768 while (gtmp->nextrule)
769 {
770 tmp = gtmp->nextrule;
771 gtmp->nextrule = tmp->nextrule;
772
773 if (tmp->rule_extra) (*pcre_free)(tmp->rule_extra);
774 if (tmp->rule) (*pcre_free)(tmp->rule);
775 if (tmp->counterlist)
776 zAVLFreeTree(tmp->counterlist, sh_ceval_free);
777 if (tmp->ovector)
778 SH_FREE(tmp->ovector);
779 SH_FREE(tmp);
780 }
781#endif
782 SH_FREE(gtmp);
783 }
784
785 qtmp = queuelist;
786 while (qtmp)
787 {
788 if (qtmp->label) sh_string_destroy(&(qtmp->label));
789 queuelist = qtmp->next;
790 SH_FREE(qtmp);
791 qtmp = queuelist;
792 }
793
794 htmp = hostlist;
795 while (htmp)
796 {
797 if (htmp->hostname_extra) (*pcre_free)(htmp->hostname_extra);
798 if (htmp->hostname) (*pcre_free)(htmp->hostname);
799 if (htmp->rulegroups) htmp->rulegroups = NULL;
800 hostlist = htmp->next;
801 htmp->next = NULL;
802 SH_FREE(htmp);
803 htmp = hostlist;
804 }
805
806 hostlist = NULL;
807 queuelist = NULL;
808 grouplist = NULL;
809
810 host_open = NULL;
811 group_open = NULL;
812
813 sh_keep_destroy();
814 sh_keep_match_del();
815
816 return;
817}
818
819/**********************************************************************
820 *
821 * Actual rule processing
822 *
823 **********************************************************************/
824
825/* Test a list of rules against msg; return matched rule, with ovector
826 * filled in
827 */
828static struct sh_geval ** dummy1;
829
830static struct sh_geval * test_rule (struct sh_geval * rule, sh_string *msg, time_t tstamp)
831{
832 int res;
833 volatile int count;
834 volatile time_t timestamp = tstamp;
835
836 dummy1 = &rule;
837
838 if (!rule)
839 DEBUG("debug: (NULL) rule\n");
840
841 if (rule && sh_string_len(msg) < (size_t)INT_MAX)
842 {
843 count = 1;
844 do {
845
846 if (flag_err_debug == SL_TRUE)
847 {
848 char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
849 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Check rule %d for |%s|"),
850 count, sh_string_str(msg));
851 SH_MUTEX_LOCK(mutex_thread_nolog);
852 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
853 emsg, _("test_rule"));
854 SH_MUTEX_UNLOCK(mutex_thread_nolog);
855 SH_FREE(emsg);
856 }
857
858 DEBUG("debug: check rule %d for <%s>\n", count, msg->str);
859 res = pcre_exec(rule->rule, rule->rule_extra,
860 sh_string_str(msg), (int)sh_string_len(msg), 0,
861 0, rule->ovector, (3*(1+rule->captures)));
862 if (res >= 0)
863 {
864 rule->ovecnum = res;
865
866 if (flag_err_debug == SL_TRUE)
867 {
868 char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
869 if ( rule->flags & RFL_KEEP )
870 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Rule %d matches, result = %d (keep)"),
871 count, res);
872 else if ( rule->flags & RFL_MARK )
873 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Rule %d matches, result = %d (mark)"),
874 count, res);
875 else
876 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Rule %d matches, result = %d"),
877 count, res);
878 SH_MUTEX_LOCK(mutex_thread_nolog);
879 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
880 emsg, _("test_rule"));
881 SH_MUTEX_UNLOCK(mutex_thread_nolog);
882 SH_FREE(emsg);
883 }
884
885 if ( rule->flags & RFL_KEEP )
886 {
887 DEBUG("debug: rule %d matches (keep)\n", count);
888 sh_keep_add(rule->label, rule->delay,
889 timestamp == 0 ? time(NULL) : timestamp);
890 }
891
892 else if ( rule->flags & RFL_MARK )
893 {
894 DEBUG("debug: rule %d matches (mark)\n", count);
895 sh_log_mark_update(rule->label,
896 timestamp == 0 ? time(NULL) : timestamp);
897 }
898
899 break; /* return the matching rule; ovector is filled in */
900 }
901
902 if (flag_err_debug == SL_TRUE)
903 {
904 char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
905 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Rule %d did not match"),
906 count);
907 SH_MUTEX_LOCK(mutex_thread_nolog);
908 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
909 emsg, _("test_rule"));
910 SH_MUTEX_UNLOCK(mutex_thread_nolog);
911 SH_FREE(emsg);
912 }
913 DEBUG("debug: rule %d did not match\n", count);
914
915 rule = rule->nextrule; ++count;
916 } while (rule);
917 }
918 if (!rule)
919 DEBUG("debug: no match found\n");
920 /* If there was no match, this is NULL */
921 return rule;
922}
923
924/* Test a (struct sh_geval *), which may be single rule or a group of rules,
925 * against msg
926 */
927static struct sh_geval ** dummy2;
928static struct sh_geval ** dummy3;
929
930static struct sh_geval * test_grules (struct sh_heval * host,
931 sh_string * msg,
932 time_t timestamp)
933{
934 struct sh_geval * result = NULL;
935 struct sh_geval * group = host->rulegroups;
936
937 dummy2 = &result;
938 dummy3 = &group;
939
940 if (group && sh_string_len(msg) < (size_t)INT_MAX)
941 {
942 DEBUG("debug: if group\n");
943 do {
944 if( (group->label != NULL) && (0 != (group->flags & RFL_ISGROUP)))
945 {
946 /* this is a rule group */
947
948 if (flag_err_debug == SL_TRUE)
949 {
950 char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
951 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Checking group |%s| of rules against |%s|"),
952 sh_string_str(group->label), sh_string_str(msg));
953 SH_MUTEX_LOCK(mutex_thread_nolog);
954 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
955 emsg, _("test_rule"));
956 SH_MUTEX_UNLOCK(mutex_thread_nolog);
957 SH_FREE(emsg);
958 }
959
960 DEBUG("debug: if group->label %s\n", sh_string_str(group->label));
961 if (pcre_exec(group->rule, group->rule_extra,
962 sh_string_str(msg), (int) sh_string_len(msg),
963 0, 0, NULL, 0) >= 0)
964 {
965 result = test_rule(group->nextrule, msg, timestamp);
966 if (result)
967 break;
968 }
969 }
970 else
971 {
972 /* If there is no group label, the 'group' is actually a solitary
973 * rule (not within any group).
974 */
975
976 if (flag_err_debug == SL_TRUE)
977 {
978 char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
979 sl_snprintf(emsg, SH_ERRBUF_SIZE, _("Checking solitary rules"));
980 SH_MUTEX_LOCK(mutex_thread_nolog);
981 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
982 emsg, _("test_rule"));
983 SH_MUTEX_UNLOCK(mutex_thread_nolog);
984 SH_FREE(emsg);
985 }
986
987 DEBUG("debug: else (single rule)\n");
988 result = test_rule(group, msg, timestamp);
989 if (result)
990 break;
991 }
992 group = group->next; /* next group of rules */
993 } while (group);
994 }
995 return result;
996}
997
998/* Top-level find_rule() function
999 */
1000static struct sh_geval * find_rule (sh_string *host,
1001 sh_string *msg,
1002 time_t timestamp)
1003{
1004 struct sh_geval * result = NULL;
1005 struct sh_heval * hlist = hostlist;
1006
1007 if (hlist && sh_string_len(host) < (size_t)INT_MAX)
1008 {
1009 do {
1010 if (pcre_exec(hlist->hostname, hlist->hostname_extra,
1011 sh_string_str(host), (int) sh_string_len(host),
1012 0, 0, NULL, 0) >= 0)
1013 {
1014 /* matching host, check rules/groups of rules */
1015 result = test_grules(hlist, msg, timestamp);
1016 if (result)
1017 break;
1018 }
1019 hlist = hlist->next;
1020 } while (hlist);
1021 }
1022 return result;
1023}
1024
1025/* copy the message and replace captured substrings with '___'
1026 */
1027static sh_string * replace_captures(const sh_string * message,
1028 int * ovector, int ovecnum)
1029{
1030 sh_string * retval = sh_string_new_from_lchar(sh_string_str(message),
1031 sh_string_len(message));
1032
1033 if (ovecnum > 1)
1034 {
1035 retval = sh_string_replace(retval, &(ovector[2]), (ovecnum-1), "___", 3);
1036 }
1037 return retval;
1038}
1039
1040static void msg_report(int severity, const sh_string * alias,
1041 struct sh_geval * rule, struct sh_logrecord * record)
1042{
1043 char * tmp;
1044 char * msg;
1045 sh_string * mmm = NULL;
1046 char * ttt;
1047
1048
1049 SH_MUTEX_LOCK(mutex_thread_nolog);
1050 if (rule) {
1051 mmm = replace_captures(record->message, rule->ovector,
1052 rule->ovecnum);
1053 rule->ovecnum = 0;
1054 msg = sh_util_safe_name_keepspace (sh_string_str(mmm));
1055 }
1056 else {
1057 msg = sh_util_safe_name_keepspace (sh_string_str(record->message));
1058 }
1059 tmp = sh_util_safe_name_keepspace (record->filename);
1060 ttt = sh_util_safe_name_keepspace (sh_string_str(record->timestr));
1061 sh_error_handle (severity, FIL__, __LINE__, 0, MSG_LOGMON_REP,
1062 msg,
1063 ttt,
1064 sh_string_str(record->host),
1065 tmp);
1066 if (alias)
1067 {
1068 sh_error_mail (sh_string_str(alias),
1069 severity, FIL__, __LINE__, 0, MSG_LOGMON_REP,
1070 msg,
1071 ttt,
1072 sh_string_str(record->host),
1073 tmp);
1074 }
1075 SH_FREE(ttt);
1076 SH_FREE(msg);
1077 SH_FREE(tmp);
1078 if (mmm)
1079 sh_string_destroy(&mmm);
1080 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1081}
1082
1083static void sum_report(int severity, const sh_string * alias,
1084 sh_string * host, sh_string * message, sh_string * path)
1085{
1086 char * tmp;
1087 char * msg;
1088
1089 SH_MUTEX_LOCK(mutex_thread_nolog);
1090 tmp = sh_util_safe_name_keepspace (sh_string_str(path));
1091 msg = sh_util_safe_name_keepspace (sh_string_str(message));
1092 sh_error_handle (severity, FIL__, __LINE__, 0, MSG_LOGMON_SUM,
1093 msg,
1094 sh_string_str(host),
1095 tmp);
1096 if (alias)
1097 {
1098 sh_error_mail (sh_string_str(alias),
1099 severity, FIL__, __LINE__, 0, MSG_LOGMON_SUM,
1100 msg,
1101 sh_string_str(host),
1102 tmp);
1103 }
1104 SH_FREE(msg);
1105 SH_FREE(tmp);
1106 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1107}
1108
1109static zAVLKey sh_eval_getkey(void const *item)
1110{
1111 return ((struct sh_ceval *)item)->hostname->str;
1112}
1113
1114/* Find the counter, or initialize one if there is none already
1115 */
1116static struct sh_ceval * find_counter(struct sh_geval * rule,
1117 sh_string * host, time_t interval)
1118{
1119 struct sh_ceval * counter;
1120
1121 if (!(rule->counterlist))
1122 {
1123 DEBUG("debug: allocate new counterlist AVL tree\n");
1124 rule->counterlist = zAVLAllocTree(sh_eval_getkey);
1125 }
1126
1127 counter = (struct sh_ceval *) zAVLSearch (rule->counterlist,
1128 sh_string_str(host));
1129
1130 if (!counter)
1131 {
1132 DEBUG("debug: no counter found\n");
1133
1134 counter = SH_ALLOC(sizeof(struct sh_ceval));
1135 memset(counter, '\0', sizeof(struct sh_ceval));
1136
1137 counter->hostname = sh_string_new_from_lchar(sh_string_str(host),
1138 sh_string_len(host));
1139 counter->counted_str = NULL;
1140 counter->filename = NULL;
1141 counter->count = 0;
1142 counter->start = time(NULL);
1143 counter->interval = interval;
1144
1145 zAVLInsert(rule->counterlist, counter);
1146 }
1147 return counter;
1148
1149}
1150
1151
1152/* process the counter for a SUM rule
1153 */
1154static int process_counter(struct sh_ceval * counter,
1155 struct sh_geval * rule,
1156 struct sh_logrecord * record)
1157{
1158 int retval = -1;
1159 time_t now;
1160
1161 if (!(counter->counted_str))
1162 {
1163 counter->counted_str = replace_captures(record->message, rule->ovector,
1164 rule->ovecnum);
1165 rule->ovecnum = 0;
1166 counter->filename = sh_string_new_from_lchar(record->filename,
1167 strlen(record->filename));
1168 DEBUG("debug: counted_str after replace: %s\n",
1169 sh_string_str(counter->counted_str));
1170 }
1171
1172 ++(counter->count);
1173 now = time(NULL); now -= counter->start;
1174 DEBUG("debug: count %lu, interval %lu, time %lu\n",
1175 counter->count, counter->interval, now);
1176 if (now >= counter->interval)
1177 {
1178 DEBUG("debug: report count\n");
1179 sum_report(rule->queue->severity, rule->queue->alias,
1180 counter->hostname, counter->counted_str, counter->filename);
1181 counter->start = time(NULL);
1182 counter->count = 0;
1183 }
1184 return retval;
1185}
1186
1187/* Process a rule
1188 */
1189static int process_rule(struct sh_geval * rule, struct sh_logrecord * record)
1190{
1191 int retval = -1;
1192 struct sh_qeval * queue = rule->queue;
1193
1194 if (queue)
1195 {
1196 DEBUG("debug: queue policy = %d found\n", queue->policy);
1197 if (queue->policy == EVAL_REPORT)
1198 {
1199 DEBUG("debug: EVAL_REPORT host: %s, message: %s\n",
1200 sh_string_str(record->host),
1201 sh_string_str(record->message));
1202 msg_report(queue->severity, queue->alias, rule, record);
1203 retval = 0;
1204 }
1205 else if (queue->policy == EVAL_SUM)
1206 {
1207
1208 struct sh_ceval * counter =
1209 find_counter(rule, record->host, queue->interval);
1210 DEBUG("debug: EVAL_SUM host: %s, message: %s\n",
1211 sh_string_str(record->host),
1212 sh_string_str(record->message));
1213 if (counter)
1214 {
1215 DEBUG("debug: counter found\n");
1216 retval = process_counter(counter, rule, record);
1217 }
1218 }
1219 }
1220 else
1221 {
1222 DEBUG("debug: no queue found -- trash\n");
1223 /* No queue means 'trash' */
1224 retval = 0;
1225 }
1226 return retval;
1227}
1228
1229#define DEFAULT_SEVERITY (-1)
1230
1231int sh_eval_process_msg(struct sh_logrecord * record)
1232{
1233 static unsigned long i = 0;
1234 if (record)
1235 {
1236 struct sh_geval * rule = find_rule (record->host,
1237 record->message,
1238 record->timestamp);
1239
1240 if (rule)
1241 {
1242 DEBUG("debug: (%lu) rule found\n", i); ++i;
1243 return process_rule(rule, record);
1244 }
1245 else
1246 {
1247 DEBUG("debug: (%lu) no rule found\n", i); ++i;
1248 msg_report(DEFAULT_SEVERITY, NULL, NULL, record);
1249 }
1250
1251 sh_repeat_message_check(record->host,
1252 record->message,
1253 record->timestamp);
1254
1255 return 0;
1256 }
1257 return -1;
1258}
1259
1260#endif
Note: See TracBrowser for help on using the repository browser.