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 if(CreatePID(ARGV0, getpid()) < 0)
64 ErrorExit(PID_ERROR,ARGV0);
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);
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)
83 /* We must have a new line at the end */
84 buffer_pt = strchr(buffer, '\n');
88 if((sb_size - r_sz) <= 2)
90 merror("%s: Full buffer receiving from: '%s'", ARGV0, srcip);
92 storage_buffer[0] = '\0';
96 strncat(storage_buffer, buffer, sb_size);
101 /* Seeing if we received more then just one message */
102 if(*(buffer_pt +1) != '\0')
106 strncpy(tmp_buffer, buffer_pt, OS_MAXSTR);
109 /* Storing everything on the storage_buffer */
110 /* Checking if buffer will be full */
111 if((sb_size - r_sz) <= 2)
113 merror("%s: Full buffer receiving from: '%s'.", ARGV0, srcip);
115 storage_buffer[0] = '\0';
116 tmp_buffer[0] = '\0';
120 strncat(storage_buffer, buffer, sb_size);
123 /* Removing carriage returns too */
124 buffer_pt = strchr(storage_buffer, '\r');
129 /* Removing syslog header */
130 if(storage_buffer[0] == '<')
132 buffer_pt = strchr(storage_buffer+1, '>');
139 buffer_pt = storage_buffer;
144 buffer_pt = storage_buffer;
148 /* Sending to the queue */
149 if(SendMSG(logr.m_queue, buffer_pt, srcip,SYSLOG_MQ) < 0)
151 merror(QUEUE_ERROR,ARGV0,DEFAULTQUEUE, strerror(errno));
152 if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
154 ErrorExit(QUEUE_FATAL,ARGV0,DEFAULTQUEUE);
158 /* Cleaning up the buffers */
159 if(tmp_buffer[0] != '\0')
161 strncpy(storage_buffer, tmp_buffer, OS_MAXSTR);
162 sb_size = OS_MAXSTR - (strlen(storage_buffer) +1);
163 tmp_buffer[0] = '\0';
167 storage_buffer[0] = '\0';
174 /** void HandleSyslogTCP() v0.2
175 * Handle syslog tcp connections
177 void HandleSyslogTCP()
179 int client_socket = 0;
183 char srcip[IPSIZE +1];
185 /* Initializing some variables */
186 memset(srcip, '\0', IPSIZE + 1);
189 /* Connecting to the message queue
192 if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
194 ErrorExit(QUEUE_FATAL,ARGV0, DEFAULTQUEUE);
198 /* Infinit loop in here */
201 /* Waiting for the childs .. */
205 wp = waitpid((pid_t) -1, NULL, WNOHANG);
207 merror(WAITPID_ERROR, ARGV0);
209 /* if = 0, we still need to wait for the child process */
217 /* Accepting new connections */
218 client_socket = OS_AcceptTCP(logr.sock, srcip, IPSIZE);
219 if(client_socket < 0)
224 /* Checking if IP is allowed here */
225 if(OS_IPNotAllowed(srcip))
227 merror(DENYIP_WARN,ARGV0,srcip);
228 close(client_socket);
232 /* Forking to deal with new client */
235 HandleClient(client_socket, srcip);
242 /* Closing client socket, since the child is handling it */
243 close(client_socket);
247 /* The parent process should not reach here */