1 /* @(#) $Id: ./src/remoted/syslogtcp.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
16 #include "os_net/os_net.h"
22 /* OS_IPNotAllowed, v0.1, 2005/02/11
23 * Checks if an IP is not allowed.
25 static int OS_IPNotAllowed(char *srcip)
27 if(logr.denyips != NULL)
29 if(OS_IPFoundList(srcip, logr.denyips))
34 if(logr.allowips != NULL)
36 if(OS_IPFoundList(srcip, logr.allowips))
42 /* If the ip is not allowed, it will be denied */
47 /** void HandleClient() v0,1
50 static void HandleClient(int client_socket, char *srcip)
52 int sb_size = OS_MAXSTR;
55 char buffer[OS_MAXSTR +2];
56 char storage_buffer[OS_MAXSTR +2];
57 char tmp_buffer[OS_MAXSTR +2];
59 char *buffer_pt = NULL;
62 /* Initializing some variables */
63 memset(buffer, '\0', OS_MAXSTR +2);
64 memset(storage_buffer, '\0', OS_MAXSTR +2);
65 memset(tmp_buffer, '\0', OS_MAXSTR +2);
70 /* If we fail, we need to return and close the socket */
71 if((r_sz = OS_RecvTCPBuffer(client_socket, buffer, OS_MAXSTR -2)) < 0)
77 /* We must have a new line at the end */
78 buffer_pt = strchr(buffer, '\n');
82 if((sb_size - r_sz) <= 2)
84 merror("%s: Full buffer receiving from: '%s'", ARGV0, srcip);
86 storage_buffer[0] = '\0';
90 strncat(storage_buffer, buffer, sb_size);
95 /* Seeing if we received more then just one message */
96 if(*(buffer_pt +1) != '\0')
100 strncpy(tmp_buffer, buffer_pt, OS_MAXSTR);
103 /* Storing everything on the storage_buffer */
104 /* Checking if buffer will be full */
105 if((sb_size - r_sz) <= 2)
107 merror("%s: Full buffer receiving from: '%s'.", ARGV0, srcip);
109 storage_buffer[0] = '\0';
110 tmp_buffer[0] = '\0';
114 strncat(storage_buffer, buffer, sb_size);
117 /* Removing carriage returns too */
118 buffer_pt = strchr(storage_buffer, '\r');
123 /* Removing syslog header */
124 if(storage_buffer[0] == '<')
126 buffer_pt = strchr(storage_buffer+1, '>');
133 buffer_pt = storage_buffer;
138 buffer_pt = storage_buffer;
142 /* Sending to the queue */
143 if(SendMSG(logr.m_queue, buffer_pt, srcip,SYSLOG_MQ) < 0)
145 merror(QUEUE_ERROR,ARGV0,DEFAULTQUEUE, strerror(errno));
146 if((logr.m_queue = StartMQ(DEFAULTQUEUE,READ)) < 0)
148 ErrorExit(QUEUE_FATAL,ARGV0,DEFAULTQUEUE);
152 /* Cleaning up the buffers */
153 if(tmp_buffer[0] != '\0')
155 strncpy(storage_buffer, tmp_buffer, OS_MAXSTR);
156 sb_size = OS_MAXSTR - (strlen(storage_buffer) +1);
157 tmp_buffer[0] = '\0';
161 storage_buffer[0] = '\0';
168 /** void HandleSyslogTCP() v0.2
169 * Handle syslog tcp connections
171 void HandleSyslogTCP()
173 int client_socket = 0;
177 char srcip[IPSIZE +1];
179 /* Initializing some variables */
180 memset(srcip, '\0', IPSIZE + 1);
183 /* Connecting to the message queue
186 if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
188 ErrorExit(QUEUE_FATAL,ARGV0, DEFAULTQUEUE);
192 /* Infinit loop in here */
195 /* Waiting for the childs .. */
199 wp = waitpid((pid_t) -1, NULL, WNOHANG);
201 merror(WAITPID_ERROR, ARGV0);
203 /* if = 0, we still need to wait for the child process */
211 /* Accepting new connections */
212 client_socket = OS_AcceptTCP(logr.sock, srcip, IPSIZE);
213 if(client_socket < 0)
218 /* Checking if IP is allowed here */
219 if(OS_IPNotAllowed(srcip))
221 merror(DENYIP_WARN,ARGV0,srcip);
222 close(client_socket);
226 /* Forking to deal with new client */
229 HandleClient(client_socket, srcip);
236 /* Closing client socket, since the child is handling it */
237 close(client_socket);
241 /* The parent process should not reach here */