Imported Upstream version 2.7
[ossec-hids.git] / src / logcollector / read_mysql_log.c
1 /* @(#) $Id: ./src/logcollector/read_mysql_log.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights 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  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16 /* Read MySQL logs */
17
18
19 #include "shared.h"
20 #include "logcollector.h"
21
22
23 /* Starting last time */
24 char __mysql_last_time[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
25
26
27
28 /* Read syslog files/snort fast/apache files */
29 void *read_mysql_log(int pos, int *rc, int drop_it)
30 {
31     int str_len = 0;
32     int need_clear = 0;
33     char *p;
34     char str[OS_MAXSTR + 1];
35     char buffer[OS_MAXSTR + 1];
36
37     str[OS_MAXSTR]= '\0';
38     *rc = 0;
39
40
41     /* Getting new entry */
42     while(fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL)
43     {
44
45         /* Getting buffer size */
46         str_len = strlen(str);
47
48
49         /* Getting the last occurence of \n */
50         if ((p = strrchr(str, '\n')) != NULL)
51         {
52             *p = '\0';
53
54             /* If need clear is set, we just get the line and ignore it. */
55             if(need_clear)
56             {
57                 need_clear = 0;
58                 continue;
59             }
60         }
61         else
62         {
63             need_clear = 1;
64         }
65
66
67         #ifdef WIN32
68         if ((p = strrchr(str, '\r')) != NULL)
69         {
70             *p = '\0';
71         }
72
73
74         /* Looking for empty string (only on windows) */
75         if(str_len <= 2)
76         {
77             continue;
78         }
79
80
81         /* Windows can have comment on their logs */
82         if(str[0] == '#')
83         {
84             continue;
85         }
86         #endif
87
88
89         /* Mysql messages have the following format:
90          * 070823 21:01:30 xx
91          */
92         if((str_len > 18) &&
93            (str[6] == ' ') &&
94            (str[9] == ':') &&
95            (str[12] == ':') &&
96            isdigit((int)str[0]) &&
97            isdigit((int)str[1]) &&
98            isdigit((int)str[2]) &&
99            isdigit((int)str[3]) &&
100            isdigit((int)str[4]) &&
101            isdigit((int)str[5]) &&
102            isdigit((int)str[7]) &&
103            isdigit((int)str[8]))
104         {
105             /* Saving last time */
106             strncpy(__mysql_last_time, str, 16);
107             __mysql_last_time[15] = '\0';
108
109
110             /* Removing spaces and tabs */
111             p = str + 15;
112             while(*p == ' ' || *p == '\t')
113             {
114                 p++;
115             }
116
117
118             /* Valid MySQL message */
119             snprintf(buffer, OS_MAXSTR, "MySQL log: %s %s",
120                                         __mysql_last_time, p);
121         }
122
123
124         /* Multiple events at the same second share the same
125          * time stamp.
126          * 0909 2020 2020 2020 20
127          */
128         else if((str_len > 10) && (__mysql_last_time[0] != '\0') &&
129                 (str[0] == 0x09) &&
130                 (str[1] == 0x09) &&
131                 (str[2] == 0x20) &&
132                 (str[3] == 0x20) &&
133                 (str[4] == 0x20) &&
134                 (str[5] == 0x20) &&
135                 (str[6] == 0x20) &&
136                 (str[7] == 0x20))
137         {
138             p = str +2;
139
140
141             /* Removing extra spaces and tabs */
142             while(*p == ' ' || *p == '\t')
143             {
144                 p++;
145             }
146
147             /* Valid MySQL message */
148             snprintf(buffer, OS_MAXSTR, "MySQL log: %s %s",
149                                         __mysql_last_time, p);
150         }
151         else
152         {
153             continue;
154         }
155
156
157         debug2("%s: DEBUG: Reading mysql messages: '%s'", ARGV0, buffer);
158
159
160         /* Sending message to queue */
161         if(drop_it == 0)
162         {
163             if(SendMSG(logr_queue, buffer, logff[pos].file, MYSQL_MQ) < 0)
164             {
165                 merror(QUEUE_SEND, ARGV0);
166                 if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
167                 {
168                     ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
169                 }
170             }
171         }
172
173         continue;
174     }
175
176     return(NULL);
177 }
178
179 /* EOF */