new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / logcollector / read_mysql_log.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights 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 /* Read MySQL logs */
11
12 #include "shared.h"
13 #include "logcollector.h"
14
15 /* Starting last time */
16 static char __mysql_last_time[18] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
17
18
19 void *read_mysql_log(int pos, int *rc, int drop_it)
20 {
21     size_t str_len = 0;
22     int need_clear = 0;
23     char *p;
24     char str[OS_MAXSTR + 1];
25     char buffer[OS_MAXSTR + 1];
26
27     str[OS_MAXSTR] = '\0';
28     *rc = 0;
29
30     /* Get new entry */
31     while (fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL) {
32         /* Get buffer size */
33         str_len = strlen(str);
34
35         /* Get the last occurrence of \n */
36         if ((p = strrchr(str, '\n')) != NULL) {
37             *p = '\0';
38
39             /* If need clear is set, we just get the line and ignore it */
40             if (need_clear) {
41                 need_clear = 0;
42                 continue;
43             }
44         } else {
45             need_clear = 1;
46         }
47
48 #ifdef WIN32
49         if ((p = strrchr(str, '\r')) != NULL) {
50             *p = '\0';
51         }
52
53         /* Look for empty string (only on windows) */
54         if (str_len <= 2) {
55             continue;
56         }
57
58
59         /* Windows can have comment on their logs */
60         if (str[0] == '#') {
61             continue;
62         }
63 #endif
64
65         /* MySQL messages have the following format:
66          * 070823 21:01:30 xx
67          */
68         if ((str_len > 18) &&
69                 (str[6] == ' ') &&
70                 (str[9] == ':') &&
71                 (str[12] == ':') &&
72                 isdigit((int)str[0]) &&
73                 isdigit((int)str[1]) &&
74                 isdigit((int)str[2]) &&
75                 isdigit((int)str[3]) &&
76                 isdigit((int)str[4]) &&
77                 isdigit((int)str[5]) &&
78                 isdigit((int)str[7]) &&
79                 isdigit((int)str[8])) {
80             /* Save last time */
81             strncpy(__mysql_last_time, str, 16);
82             __mysql_last_time[15] = '\0';
83
84
85             /* Remove spaces and tabs */
86             p = str + 15;
87             while (*p == ' ' || *p == '\t') {
88                 p++;
89             }
90
91             /* Valid MySQL message */
92             snprintf(buffer, OS_MAXSTR, "MySQL log: %s %s",
93                      __mysql_last_time, p);
94         }
95
96         /* Multiple events at the same second share the same timestamp:
97          * 0909 2020 2020 2020 20
98          */
99         else if ((str_len > 10) && (__mysql_last_time[0] != '\0') &&
100                  (str[0] == 0x09) &&
101                  (str[1] == 0x09) &&
102                  (str[2] == 0x20) &&
103                  (str[3] == 0x20) &&
104                  (str[4] == 0x20) &&
105                  (str[5] == 0x20) &&
106                  (str[6] == 0x20) &&
107                  (str[7] == 0x20)) {
108             p = str + 2;
109
110             /* Remove extra spaces and tabs */
111             while (*p == ' ' || *p == '\t') {
112                 p++;
113             }
114
115             /* Valid MySQL message */
116             snprintf(buffer, OS_MAXSTR, "MySQL log: %s %s",
117                      __mysql_last_time, p);
118         } else {
119             continue;
120         }
121
122         debug2("%s: DEBUG: Reading mysql messages: '%s'", ARGV0, buffer);
123
124         /* Send message to queue */
125         if (drop_it == 0) {
126             if (SendMSG(logr_queue, buffer, logff[pos].file, MYSQL_MQ) < 0) {
127                 merror(QUEUE_SEND, ARGV0);
128                 if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
129                     ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
130                 }
131             }
132         }
133
134         continue;
135     }
136
137     return (NULL);
138 }
139