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