Imported Upstream version 2.7
[ossec-hids.git] / src / logcollector / read_mssql_log.c
1 /* @(#) $Id: ./src/logcollector/read_mssql_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 MSSQL logs */
17
18
19 #include "shared.h"
20 #include "logcollector.h"
21
22
23
24 /* Send mssql message and check the return code.
25  */
26 void __send_mssql_msg(int pos, int drop_it, char *buffer)
27 {
28     debug2("%s: DEBUG: Reading MSSQL message: '%s'", ARGV0, buffer);
29     if(drop_it == 0)
30     {
31         if(SendMSG(logr_queue, buffer, logff[pos].file, LOCALFILE_MQ) < 0)
32         {
33             merror(QUEUE_SEND, ARGV0);
34             if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
35             {
36                 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
37             }
38         }
39     }
40 }
41
42
43
44 /* Read PostgreSQL log files */
45 void *read_mssql_log(int pos, int *rc, int drop_it)
46 {
47     int str_len = 0;
48     int need_clear = 0;
49     char *p;
50     char str[OS_MAXSTR + 1];
51     char buffer[OS_MAXSTR + 1];
52
53
54     /* Zeroing buffer and str */
55     buffer[0] = '\0';
56     buffer[OS_MAXSTR] = '\0';
57     str[OS_MAXSTR]= '\0';
58     *rc = 0;
59
60
61     /* Getting new entry */
62     while(fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL)
63     {
64
65         /* Getting buffer size */
66         str_len = strlen(str);
67
68
69         /* Checking str_len size. Very useless, but just to make sure.. */
70         if(str_len >= sizeof(buffer) -2)
71         {
72             str_len = sizeof(buffer) -10;
73         }
74
75
76         /* Getting the last occurence of \n */
77         if ((p = strrchr(str, '\n')) != NULL)
78         {
79             *p = '\0';
80
81             /* If need clear is set, we just get the line and ignore it. */
82             if(need_clear)
83             {
84                 need_clear = 0;
85                 continue;
86             }
87         }
88         else
89         {
90             need_clear = 1;
91         }
92
93
94         #ifdef WIN32
95         if ((p = strrchr(str, '\r')) != NULL)
96         {
97             *p = '\0';
98         }
99
100
101         /* Looking for empty string (only on windows) */
102         if(str_len <= 1)
103         {
104             continue;
105         }
106
107
108         /* Windows can have comment on their logs */
109         if(str[0] == '#')
110         {
111             continue;
112         }
113         #endif
114
115
116
117         /* MSSQL messages have the following formats:
118          * 2009-03-25 04:47:30.01 Server
119          * 2003-10-09 00:00:06.68 sys1
120          * 2009-02-06 11:48:59     Server
121          */
122         if((str_len > 19) &&
123            (str[4] == '-') &&
124            (str[7] == '-') &&
125            (str[10] == ' ') &&
126            (str[13] == ':') &&
127            (str[16] == ':') &&
128            isdigit((int)str[0]) &&
129            isdigit((int)str[1]) &&
130            isdigit((int)str[2]) &&
131            isdigit((int)str[3]))
132         {
133
134             /* If the saved message is empty, set it and continue. */
135             if(buffer[0] == '\0')
136             {
137                 strncpy(buffer, str, str_len + 2);
138                 continue;
139             }
140
141             /* If not, send the saved one and store the new one for later */
142             else
143             {
144                 __send_mssql_msg(pos, drop_it, buffer);
145
146
147                 /* Storing current one at the buffer */
148                 strncpy(buffer, str, str_len + 2);
149             }
150         }
151
152
153         /* Query logs can be in multiple lines.
154          * They always start with a tab in the additional ones.
155          */
156         else if((str_len > 2) && (buffer[0] != '\0'))
157         {
158             /* Size of the buffer */
159             int buffer_len = strlen(buffer);
160
161             p = str;
162
163             /* Removing extra spaces and tabs */
164             while(*p == ' ' || *p == '\t')
165             {
166                 p++;
167             }
168
169
170             /* Adding additional message to the saved buffer. */
171             if(sizeof(buffer) - buffer_len > str_len +256)
172             {
173                 /* Here we make sure that the size of the buffer
174                  * minus what was used (strlen) is greater than
175                  * the length of the received message.
176                  */
177                  buffer[buffer_len] = ' ';
178                  buffer[buffer_len +1] = '\0';
179                  strncat(buffer, str, str_len +3);
180             }
181         }
182
183         continue;
184     }
185
186
187     /* Send whatever is stored. */
188     if(buffer[0] != '\0')
189     {
190         __send_mssql_msg(pos, drop_it, buffer);
191     }
192
193     return(NULL);
194 }
195
196 /* EOF */