new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / logcollector / read_snortfull.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 #include "logcollector.h"
12
13
14 /* Read snort_full files */
15 void *read_snortfull(int pos, int *rc, int drop_it)
16 {
17     int f_msg_size = OS_MAXSTR;
18     const char *one = "one";
19     const char *two = "two";
20     const char *p = NULL;
21     char *q;
22     char str[OS_MAXSTR + 1];
23     char f_msg[OS_MAXSTR + 1];
24
25     *rc = 0;
26     str[OS_MAXSTR] = '\0';
27     f_msg[OS_MAXSTR] = '\0';
28
29     while (fgets(str, OS_MAXSTR, logff[pos].fp) != NULL) {
30         /* Remove \n at the end of the string */
31         if ((q = strrchr(str, '\n')) != NULL) {
32             *q = '\0';
33         } else {
34             goto file_error;
35         }
36
37         /* First part of the message */
38         if (p == NULL) {
39             if (strncmp(str, "[**] [", 6) == 0) {
40                 strncpy(f_msg, str, OS_MAXSTR);
41                 f_msg_size -= strlen(str) + 1;
42                 p = one;
43             }
44         } else {
45             if (p == one) {
46                 /* Second line has the [Classification: */
47                 if (strncmp(str, "[Classification: ", 16) == 0) {
48                     strncat(f_msg, str, f_msg_size);
49                     f_msg_size -= strlen(str) + 1;
50                     p = two;
51                 } else if (strncmp(str, "[Priority: ", 10) == 0) {
52                     strncat(f_msg, "[Classification: Preprocessor] "
53                             "[Priority: 3] ", f_msg_size);
54                     f_msg_size -= strlen(str) + 1;
55                     p = two;
56                 }
57
58                 /* If it is a preprocessor message, it will not have
59                  * the classification.
60                  */
61                 else if ((str[2] == '/') && (str[5] == '-') && (q = strchr(str, ' '))) {
62                     strncat(f_msg, "[Classification: Preprocessor] "
63                             "[Priority: 3] ", f_msg_size);
64                     strncat(f_msg, ++q, f_msg_size - 40);
65
66                     /* Clean for next event */
67                     p = NULL;
68
69                     /* Send the message */
70                     if (drop_it == 0) {
71                         if (SendMSG(logr_queue, f_msg, logff[pos].file,
72                                     LOCALFILE_MQ) < 0) {
73                             merror(QUEUE_SEND, ARGV0);
74                             if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
75                                 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
76                             }
77                         }
78                     }
79
80                     f_msg[0] = '\0';
81                     f_msg_size = OS_MAXSTR;
82                     str[0] = '\0';
83                 } else {
84                     goto file_error;
85                 }
86             } else if (p == two) {
87                 /* Third line has the 01/13-15 (date) */
88                 if ((str[2] == '/') && (str[5] == '-') && (q = strchr(str, ' '))) {
89                     strncat(f_msg, ++q, f_msg_size);
90                     f_msg_size -= strlen(q) + 1;
91                     p = NULL;
92
93                     /* Send the message */
94                     if (drop_it == 0) {
95                         if (SendMSG(logr_queue, f_msg, logff[pos].file,
96                                     LOCALFILE_MQ) < 0) {
97                             merror(QUEUE_SEND, ARGV0);
98                             if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
99                                 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
100                             }
101                         }
102                     }
103
104                     f_msg[0] = '\0';
105                     f_msg_size = OS_MAXSTR;
106                     str[0] = '\0';
107                 } else {
108                     goto file_error;
109                 }
110
111             }
112         }
113
114         continue;
115
116 file_error:
117
118         merror("%s: Bad formatted snort full file.", ARGV0);
119         *rc = -1;
120         return (NULL);
121
122     }
123
124     return (NULL);
125 }
126