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