new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / logcollector / read_multiline.c
1 /* Copyright (C) 2010 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "logcollector.h"
12
13
14 /* Read multiline logs */
15 void *read_multiline(int pos, int *rc, int drop_it)
16 {
17     int __ms = 0;
18     int linecount;
19     int linesgot = 0;
20     size_t buffer_size = 0;
21     char *p;
22     char str[OS_MAXSTR + 1];
23     char buffer[OS_MAXSTR + 1];
24     fpos_t fp_pos;
25
26     buffer[0] = '\0';
27     buffer[OS_MAXSTR] = '\0';
28     str[OS_MAXSTR] = '\0';
29     *rc = 0;
30
31     linecount = atoi(logff[pos].logformat);
32
33     /* Get initial file location */
34     fgetpos(logff[pos].fp, &fp_pos);
35
36     while (fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL) {
37         linesgot++;
38
39         /* Get the last occurrence of \n */
40         if ((p = strrchr(str, '\n')) != NULL) {
41             *p = '\0';
42         }
43
44         /* If we didn't get the new line, because the
45          * size is large, send what we got so far.
46          */
47         else if (strlen(str) >= (OS_MAXSTR - OS_LOG_HEADER - 2)) {
48             /* Message size > maximum allowed */
49             __ms = 1;
50         } else {
51             /* Message not complete. Return. */
52             debug1("%s: Message not complete. Trying again: '%s'", ARGV0, str);
53             fsetpos(logff[pos].fp, &fp_pos);
54             break;
55         }
56
57 #ifdef WIN32
58         if ((p = strrchr(str, '\r')) != NULL) {
59             *p = '\0';
60         }
61 #endif
62
63         debug2("%s: DEBUG: Reading message: '%s'", ARGV0, str);
64
65         /* Add to buffer */
66         buffer_size = strlen(buffer);
67         if (buffer[0] != '\0') {
68             buffer[buffer_size] = ' ';
69             buffer_size++;
70         }
71
72         strncpy(buffer + buffer_size, str, OS_MAXSTR - buffer_size - 2);
73
74         if (linesgot < linecount) {
75             continue;
76         }
77
78         /* Send message to queue */
79         if (drop_it == 0) {
80             if (SendMSG(logr_queue, buffer, logff[pos].file,
81                         LOCALFILE_MQ) < 0) {
82                 merror(QUEUE_SEND, ARGV0);
83                 if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
84                     ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
85                 }
86             }
87         }
88
89         buffer[0] = '\0';
90
91
92         /* Incorrect message size */
93         if (__ms) {
94             merror("%s: Large message size: '%s'", ARGV0, str);
95             while (fgets(str, OS_MAXSTR - 2, logff[pos].fp) != NULL) {
96                 /* Get the last occurrence of \n */
97                 if ((p = strrchr(str, '\n')) != NULL) {
98                     break;
99                 }
100             }
101             __ms = 0;
102         }
103
104         fgetpos(logff[pos].fp, &fp_pos);
105         continue;
106     }
107
108     return (NULL);
109 }
110