new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / logcollector / read_syslog.c
1 /* Copyright (C) 2009 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 /* Read the syslog */
11
12 #include "shared.h"
13 #include "logcollector.h"
14
15
16 /* Read syslog files */
17 void *read_syslog(int pos, int *rc, int drop_it)
18 {
19     int __ms = 0;
20     char *p;
21     char str[OS_MAXSTR + 1];
22     fpos_t fp_pos;
23
24     str[OS_MAXSTR] = '\0';
25     *rc = 0;
26
27     /* Get initial file location */
28     fgetpos(logff[pos].fp, &fp_pos);
29
30     while (fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL) {
31         /* Get the last occurrence of \n */
32         if ((p = strrchr(str, '\n')) != NULL) {
33             *p = '\0';
34          /* From issue #913 @ybonnamy */
35          } else if((p = strchr(str, '\0')) != NULL) {
36              /* Replace NUL with a space */
37              *p = ' ';
38          }
39
40         /* If we didn't get the new line, because the
41          * size is large, send what we got so far.
42          */
43         else if (strlen(str) >= (OS_MAXSTR - OS_LOG_HEADER - 2)) {
44             /* Message size > maximum allowed */
45             __ms = 1;
46         } else {
47             /* Message not complete. Return. */
48             debug1("%s: Message not complete. Trying again: '%s'", ARGV0, str);
49             fsetpos(logff[pos].fp, &fp_pos);
50             break;
51         }
52
53 #ifdef WIN32
54         if ((p = strrchr(str, '\r')) != NULL) {
55             *p = '\0';
56         }
57
58         /* Look for empty string (only on Windows) */
59         if (strlen(str) <= 2) {
60             fgetpos(logff[pos].fp, &fp_pos);
61             continue;
62         }
63
64         /* Windows can have comment on their logs */
65         if (str[0] == '#') {
66             fgetpos(logff[pos].fp, &fp_pos);
67             continue;
68         }
69 #endif
70
71         debug2("%s: DEBUG: Reading syslog message: '%s'", ARGV0, str);
72
73         /* Send message to queue */
74         if (drop_it == 0) {
75             if (SendMSG(logr_queue, str, logff[pos].file,
76                         LOCALFILE_MQ) < 0) {
77                 merror(QUEUE_SEND, ARGV0);
78                 if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
79                     ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
80                 }
81             }
82         }
83
84         /* Incorrect message size */
85         if (__ms) {
86             // strlen(str) >= (OS_MAXSTR - OS_LOG_HEADER - 2)
87             // truncate str before logging to ossec.log
88 #define OUTSIZE 4096
89             char buf[OUTSIZE + 1];
90             buf[OUTSIZE] = '\0';
91             snprintf(buf, OUTSIZE, "%s", str);
92             merror("%s: Large message size(length=%d): '%s...'", ARGV0, (int)strlen(str), buf);
93             while (fgets(str, OS_MAXSTR - 2, logff[pos].fp) != NULL) {
94                 /* Get the last occurrence of \n */
95                 if (strrchr(str, '\n') != NULL) {
96                     break;
97                 }
98             }
99             __ms = 0;
100         }
101
102         fgetpos(logff[pos].fp, &fp_pos);
103         continue;
104     }
105
106     return (NULL);
107 }
108