1 /* @(#) $Id: syslogtcp.c,v 1.8 2009/06/24 18:53:07 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
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 3) as published by the FSF - Free Software
15 #include "os_net/os_net.h"
21 /* OS_IPNotAllowed, v0.1, 2005/02/11
22 * Checks if an IP is not allowed.
24 static int OS_IPNotAllowed(char *srcip)
26 if(logr.denyips != NULL)
28 if(OS_IPFoundList(srcip, logr.denyips))
33 if(logr.allowips != NULL)
35 if(OS_IPFoundList(srcip, logr.allowips))
41 /* If the ip is not allowed, it will be denied */
46 /** void HandleClient() v0,1
49 static void HandleClient(int client_socket, char *srcip)
51 int sb_size = OS_MAXSTR;
54 char buffer[OS_MAXSTR +2];
55 char storage_buffer[OS_MAXSTR +2];
56 char tmp_buffer[OS_MAXSTR +2];
58 char *buffer_pt = NULL;
61 /* Initializing some variables */
62 memset(buffer, '\0', OS_MAXSTR +2);
63 memset(storage_buffer, '\0', OS_MAXSTR +2);
64 memset(tmp_buffer, '\0', OS_MAXSTR +2);
69 /* If we fail, we need to return and close the socket */
70 if((r_sz = OS_RecvTCPBuffer(client_socket, buffer, OS_MAXSTR -2)) < 0)
76 /* We must have a new line at the end */
77 buffer_pt = strchr(buffer, '\n');
81 if((sb_size - r_sz) <= 2)
83 merror("%s: Full buffer receiving from: '%s'", ARGV0, srcip);
85 storage_buffer[0] = '\0';
89 strncat(storage_buffer, buffer, sb_size);
94 /* Seeing if we received more then just one message */
95 if(*(buffer_pt +1) != '\0')
99 strncpy(tmp_buffer, buffer_pt, OS_MAXSTR);
102 /* Storing everything on the storage_buffer */
103 /* Checking if buffer will be full */
104 if((sb_size - r_sz) <= 2)
106 merror("%s: Full buffer receiving from: '%s'.", ARGV0, srcip);
108 storage_buffer[0] = '\0';
109 tmp_buffer[0] = '\0';
113 strncat(storage_buffer, buffer, sb_size);
116 /* Removing carriage returns too */
117 buffer_pt = strchr(storage_buffer, '\r');
122 /* Removing syslog header */
123 if(storage_buffer[0] == '<')
125 buffer_pt = strchr(storage_buffer+1, '>');
132 buffer_pt = storage_buffer;
137 buffer_pt = storage_buffer;
141 /* Sending to the queue */
142 if(SendMSG(logr.m_queue, buffer_pt, srcip,SYSLOG_MQ) < 0)
144 merror(QUEUE_ERROR,ARGV0,DEFAULTQUEUE, strerror(errno));
145 if((logr.m_queue = StartMQ(DEFAULTQUEUE,READ)) < 0)
147 ErrorExit(QUEUE_FATAL,ARGV0,DEFAULTQUEUE);
151 /* Cleaning up the buffers */
152 if(tmp_buffer[0] != '\0')
154 strncpy(storage_buffer, tmp_buffer, OS_MAXSTR);
155 sb_size = OS_MAXSTR - (strlen(storage_buffer) +1);
156 tmp_buffer[0] = '\0';
160 storage_buffer[0] = '\0';
167 /** void HandleSyslogTCP() v0.2
168 * Handle syslog tcp connections
170 void HandleSyslogTCP()
172 int client_socket = 0;
176 char srcip[IPSIZE +1];
178 /* Initializing some variables */
179 memset(srcip, '\0', IPSIZE + 1);
182 /* Connecting to the message queue
185 if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
187 ErrorExit(QUEUE_FATAL,ARGV0, DEFAULTQUEUE);
191 /* Infinit loop in here */
194 /* Waiting for the childs .. */
198 wp = waitpid((pid_t) -1, NULL, WNOHANG);
200 merror(WAITPID_ERROR, ARGV0);
202 /* if = 0, we still need to wait for the child process */
210 /* Accepting new connections */
211 client_socket = OS_AcceptTCP(logr.sock, srcip, IPSIZE);
212 if(client_socket < 0)
217 /* Checking if IP is allowed here */
218 if(OS_IPNotAllowed(srcip))
220 merror(DENYIP_WARN,ARGV0,srcip);
221 close(client_socket);
225 /* Forking to deal with new client */
228 HandleClient(client_socket, srcip);
235 /* Closing client socket, since the child is handling it */
236 close(client_socket);
240 /* The parent process should not reach here */