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