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