Imported Upstream version 2.7
[ossec-hids.git] / src / logcollector / read_syslog.c
1 /* @(#) $Id: ./src/logcollector/read_syslog.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right 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
13 /* Read the syslog */
14
15
16 #include "shared.h"
17 #include "logcollector.h"
18
19
20
21 /* v0.3 (2005/08/24): Using fgets instead of fgetc
22  * v0.2 (2005/04/04)
23  */
24
25 /* Read syslog files/snort fast/apache files */
26 void *read_syslog(int pos, int *rc, int drop_it)
27 {
28     int __ms = 0;
29     char *p;
30     char str[OS_MAXSTR+1];
31
32     fpos_t fp_pos;
33
34     str[OS_MAXSTR]= '\0';
35     *rc = 0;
36
37     /* Getting initial file location */
38     fgetpos(logff[pos].fp, &fp_pos);
39
40     while(fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL)
41     {
42         /* Getting the last occurence of \n */
43         if ((p = strrchr(str, '\n')) != NULL)
44         {
45             *p = '\0';
46         }
47
48         /* If we didn't get the new line, because the
49          * size is large, send what we got so far.
50          */
51         else if(strlen(str) >= (OS_MAXSTR - OS_LOG_HEADER - 2))
52         {
53             /* Message size > maximum allowed */
54             __ms = 1;
55         }
56         else
57         {
58             /* Message not complete. Return. */
59             debug1("%s: Message not complete. Trying again: '%s'", ARGV0,str);
60             fsetpos(logff[pos].fp, &fp_pos);
61             break;
62         }
63
64         #ifdef WIN32
65         if ((p = strrchr(str, '\r')) != NULL)
66         {
67             *p = '\0';
68         }
69
70         /* Looking for empty string (only on windows) */
71         if(strlen(str) <= 2)
72         {
73             fgetpos(logff[pos].fp, &fp_pos);
74             continue;
75         }
76
77         /* Windows can have comment on their logs */
78         if(str[0] == '#')
79         {
80             fgetpos(logff[pos].fp, &fp_pos);
81             continue;
82         }
83         #endif
84
85         debug2("%s: DEBUG: Reading syslog message: '%s'", ARGV0, str);
86
87
88         /* Sending message to queue */
89         if(drop_it == 0)
90         {
91             if(SendMSG(logr_queue,str,logff[pos].file,
92                         LOCALFILE_MQ) < 0)
93             {
94                 merror(QUEUE_SEND, ARGV0);
95                 if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
96                 {
97                     ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
98                 }
99             }
100         }
101
102         /* Incorrectly message size */
103         if(__ms)
104         {
105             // strlen(str) >= (OS_MAXSTR - OS_LOG_HEADER - 2)
106             // truncate str before logging to ossec.log
107 #define OUTSIZE 4096
108             char buf[OUTSIZE + 1];
109             buf[OUTSIZE] = '\0';
110             snprintf(buf, OUTSIZE, "%s", str);
111             merror("%s: Large message size(length=%d): '%s...'", ARGV0, (int)strlen(str), buf);
112             while(fgets(str, OS_MAXSTR - 2, logff[pos].fp) != NULL)
113             {
114                 /* Getting the last occurence of \n */
115                 if ((p = strrchr(str, '\n')) != NULL)
116                 {
117                     break;
118                 }
119             }
120             __ms = 0;
121         }
122
123         fgetpos(logff[pos].fp, &fp_pos);
124         continue;
125     }
126
127     return(NULL);
128 }
129
130 /* EOF */