new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / util / ossec-regex.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11
12 #undef ARGV0
13 #define ARGV0 "ossec-regex"
14
15 /* Prototypes */
16 static void helpmsg(void) __attribute__((noreturn));
17
18
19 static void helpmsg()
20 {
21     printf("\nOSSEC HIDS %s: ossec-regex pattern\n", ARGV0);
22     exit(1);
23 }
24
25 int main(int argc, char **argv)
26 {
27     const char *pattern;
28
29     char msg[OS_MAXSTR + 1];
30     memset(msg, '\0', OS_MAXSTR + 1);
31     OSRegex regex;
32     OSMatch matcher;
33
34     OS_SetName(ARGV0);
35
36     /* User arguments */
37     if (argc != 2) {
38         helpmsg();
39         return (-1);
40     }
41
42     /* User options */
43     if (strcmp(argv[1], "-h") == 0) {
44         helpmsg();
45         return (-1);
46     }
47
48     pattern = argv[1];
49
50     if (!OSRegex_Compile(pattern, &regex, 0)) {
51         printf("pattern does not compile with OSRegex_Compile\n");
52         return (-1);
53     }
54     if (!OSMatch_Compile(pattern, &matcher, 0)) {
55         printf("pattern does not compile with OSMatch_Compile\n");
56         return (-1);
57     }
58
59     while ((fgets(msg, OS_MAXSTR, stdin)) != NULL) {
60         /* Remove newline */
61         if (msg[strlen(msg) - 1] == '\n') {
62             msg[strlen(msg) - 1] = '\0';
63         }
64
65         /* Make sure we ignore blank lines */
66         if (strlen(msg) < 2) {
67             continue;
68         }
69
70         if (OSRegex_Execute(msg, &regex)) {
71             printf("+OSRegex_Execute: %s\n", msg);
72         }
73         /*
74         else
75             printf("-OSRegex_Execute: \n");
76          */
77
78         if (OS_Regex(pattern, msg)) {
79             printf("+OS_Regex       : %s\n", msg);
80         }
81         /*
82         else
83             printf("-OS_Regex: \n");
84          */
85
86         if (OSMatch_Execute(msg, strlen(msg), &matcher)) {
87             printf("+OSMatch_Compile: %s\n", msg);
88         }
89
90         if (OS_Match2(pattern, msg)) {
91             printf("+OS_Match2      : %s\n", msg);
92         }
93     }
94     return (0);
95 }
96