Imported Upstream version 2.7
[ossec-hids.git] / src / logcollector / read_multiline.c
1 /* @(#) $Id: ./src/logcollector/read_multiline.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2010 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 /* Read multiline logs. */
22 void *read_multiline(int pos, int *rc, int drop_it)
23 {
24     int __ms = 0;
25     int linecount;
26     int linesgot = 0;
27     int buffer_size = 0;
28     char *p;
29     char str[OS_MAXSTR + 1];
30     char buffer[OS_MAXSTR +1];
31
32     fpos_t fp_pos;
33
34     buffer[0] = '\0';
35     buffer[OS_MAXSTR] = '\0';
36     str[OS_MAXSTR]= '\0';
37     *rc = 0;
38
39     linecount = atoi(logff[pos].logformat);
40
41     /* Getting initial file location */
42     fgetpos(logff[pos].fp, &fp_pos);
43
44     while(fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL)
45     {
46         linesgot++;
47
48         /* Getting the last occurence of \n */
49         if ((p = strrchr(str, '\n')) != NULL)
50         {
51             *p = '\0';
52         }
53
54         /* If we didn't get the new line, because the
55          * size is large, send what we got so far.
56          */
57         else if(strlen(str) >= (OS_MAXSTR - OS_LOG_HEADER - 2))
58         {
59             /* Message size > maximum allowed */
60             __ms = 1;
61         }
62         else
63         {
64             /* Message not complete. Return. */
65             debug1("%s: Message not complete. Trying again: '%s'", ARGV0,str);
66             fsetpos(logff[pos].fp, &fp_pos);
67             break;
68         }
69
70         #ifdef WIN32
71         if ((p = strrchr(str, '\r')) != NULL)
72         {
73             *p = '\0';
74         }
75         #endif
76
77         debug2("%s: DEBUG: Reading message: '%s'", ARGV0, str);
78
79
80         /* Adding to buffer. */
81         buffer_size = strlen(buffer);
82         if(buffer[0] != '\0')
83         {
84             buffer[buffer_size] = ' ';
85             buffer_size++;
86         }
87
88         strncpy(buffer + buffer_size, str, OS_MAXSTR - buffer_size -2);
89
90
91         if(linesgot < linecount)
92         {
93             continue;
94         }
95
96
97         /* Sending message to queue */
98         if(drop_it == 0)
99         {
100             if(SendMSG(logr_queue, buffer, logff[pos].file,
101                         LOCALFILE_MQ) < 0)
102             {
103                 merror(QUEUE_SEND, ARGV0);
104                 if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
105                 {
106                     ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
107                 }
108             }
109         }
110
111         buffer[0] = '\0';
112
113
114         /* Incorrectly message size */
115         if(__ms)
116         {
117             merror("%s: Large message size: '%s'", ARGV0, str);
118             while(fgets(str, OS_MAXSTR - 2, logff[pos].fp) != NULL)
119             {
120                 /* Getting the last occurence of \n */
121                 if ((p = strrchr(str, '\n')) != NULL)
122                 {
123                     break;
124                 }
125             }
126             __ms = 0;
127         }
128
129         fgetpos(logff[pos].fp, &fp_pos);
130         continue;
131     }
132
133     return(NULL);
134 }
135
136 /* EOF */