d4944c01b2a953f257a9ce953a1ede2908d0d026
[ossec-hids.git] / src / remoted / syslogtcp.c
1 /* @(#) $Id: ./src/remoted/syslogtcp.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 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
14
15 #include "shared.h"
16 #include "os_net/os_net.h"
17
18 #include "remoted.h"
19
20
21
22 /* OS_IPNotAllowed, v0.1, 2005/02/11
23  * Checks if an IP is not allowed.
24  */
25 static int OS_IPNotAllowed(char *srcip)
26 {
27     if(logr.denyips != NULL)
28     {
29         if(OS_IPFoundList(srcip, logr.denyips))
30         {
31             return(1);
32         }
33     }
34     if(logr.allowips != NULL)
35     {
36         if(OS_IPFoundList(srcip, logr.allowips))
37         {
38             return(0);
39         }
40     }
41
42     /* If the ip is not allowed, it will be denied */
43     return(1);
44 }
45
46
47 /** void HandleClient() v0,1
48  * Handle each client
49  */
50 static void HandleClient(int client_socket, char *srcip)
51 {
52     int sb_size = OS_MAXSTR;
53     int r_sz = 0;
54
55     char buffer[OS_MAXSTR +2];
56     char storage_buffer[OS_MAXSTR +2];
57     char tmp_buffer[OS_MAXSTR +2];
58
59     char *buffer_pt = NULL;
60
61     /* Create PID file */
62     if(CreatePID(ARGV0, getpid()) < 0)
63     {
64         ErrorExit(PID_ERROR,ARGV0);
65     }
66
67     /* Initializing some variables */
68     memset(buffer, '\0', OS_MAXSTR +2);
69     memset(storage_buffer, '\0', OS_MAXSTR +2);
70     memset(tmp_buffer, '\0', OS_MAXSTR +2);
71
72
73     while(1)
74     {
75         /* If we fail, we need to return and close the socket */
76         if((r_sz = OS_RecvTCPBuffer(client_socket, buffer, OS_MAXSTR -2)) < 0)
77         {
78             close(client_socket);
79             DeletePID(ARGV0);
80             return;
81         }
82
83         /* We must have a new line at the end */
84         buffer_pt = strchr(buffer, '\n');
85         if(!buffer_pt)
86         {
87             /* Buffer is full */
88             if((sb_size - r_sz) <= 2)
89             {
90                 merror("%s: Full buffer receiving from: '%s'", ARGV0, srcip);
91                 sb_size = OS_MAXSTR;
92                 storage_buffer[0] = '\0';
93                 continue;
94             }
95
96             strncat(storage_buffer, buffer, sb_size);
97             sb_size -= r_sz;
98             continue;
99         }
100
101         /* Seeing if we received more then just one message */
102         if(*(buffer_pt +1) != '\0')
103         {
104             *buffer_pt = '\0';
105             buffer_pt++;
106             strncpy(tmp_buffer, buffer_pt, OS_MAXSTR);
107         }
108
109         /* Storing everything on the storage_buffer */
110         /* Checking if buffer will be  full */
111         if((sb_size - r_sz) <= 2)
112         {
113             merror("%s: Full buffer receiving from: '%s'.", ARGV0, srcip);
114             sb_size = OS_MAXSTR;
115             storage_buffer[0] = '\0';
116             tmp_buffer[0] = '\0';
117             continue;
118         }
119
120         strncat(storage_buffer, buffer, sb_size);
121
122
123         /* Removing carriage returns too */
124         buffer_pt = strchr(storage_buffer, '\r');
125         if(buffer_pt)
126             *buffer_pt = '\0';
127
128
129         /* Removing syslog header */
130         if(storage_buffer[0] == '<')
131         {
132             buffer_pt = strchr(storage_buffer+1, '>');
133             if(buffer_pt)
134             {
135                 buffer_pt++;
136             }
137             else
138             {
139                 buffer_pt = storage_buffer;
140             }
141         }
142         else
143         {
144             buffer_pt = storage_buffer;
145         }
146
147
148         /* Sending to the queue */
149         if(SendMSG(logr.m_queue, buffer_pt, srcip,SYSLOG_MQ) < 0)
150         {
151             merror(QUEUE_ERROR,ARGV0,DEFAULTQUEUE, strerror(errno));
152             if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
153             {
154                 ErrorExit(QUEUE_FATAL,ARGV0,DEFAULTQUEUE);
155             }
156         }
157
158         /* Cleaning up the buffers */
159         if(tmp_buffer[0] != '\0')
160         {
161             strncpy(storage_buffer, tmp_buffer, OS_MAXSTR);
162             sb_size = OS_MAXSTR - (strlen(storage_buffer) +1);
163             tmp_buffer[0] = '\0';
164         }
165         else
166         {
167             storage_buffer[0] = '\0';
168             sb_size = OS_MAXSTR;
169         }
170     }
171 }
172
173
174 /** void HandleSyslogTCP() v0.2
175  * Handle syslog tcp connections
176  */
177 void HandleSyslogTCP()
178 {
179     int client_socket = 0;
180     int st_errors = 0;
181     int childcount = 0;
182
183     char srcip[IPSIZE +1];
184
185     /* Initializing some variables */
186     memset(srcip, '\0', IPSIZE + 1);
187
188
189     /* Connecting to the message queue
190      * Exit if it fails.
191      */
192     if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
193     {
194         ErrorExit(QUEUE_FATAL,ARGV0, DEFAULTQUEUE);
195     }
196
197
198     /* Infinit loop in here */
199     while(1)
200     {
201         /* Waiting for the childs .. */
202         while (childcount)
203         {
204             int wp;
205             wp = waitpid((pid_t) -1, NULL, WNOHANG);
206             if (wp < 0)
207                 merror(WAITPID_ERROR, ARGV0);
208
209             /* if = 0, we still need to wait for the child process */
210             else if (wp == 0)
211                 break;
212             else
213                 childcount--;
214         }
215
216
217         /* Accepting new connections */
218         client_socket = OS_AcceptTCP(logr.sock, srcip, IPSIZE);
219         if(client_socket < 0)
220         {
221             st_errors++;
222         }
223
224         /* Checking if IP is allowed here */
225         if(OS_IPNotAllowed(srcip))
226         {
227             merror(DENYIP_WARN,ARGV0,srcip);
228             close(client_socket);
229         }
230
231
232         /* Forking to deal with new client */
233         if(fork() == 0)
234         {
235             HandleClient(client_socket, srcip);
236             exit(0);
237         }
238         else
239         {
240             childcount++;
241
242             /* Closing client socket, since the child is handling it */
243             close(client_socket);
244             continue;
245         }
246
247         /* The parent process should not reach here */
248         return;
249     }
250 }
251
252
253
254 /* EOF */