1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "os_net/os_net.h"
15 /* Checks if an IP is not allowed */
16 static int OS_IPNotAllowed(char *srcip)
18 if (logr.denyips != NULL) {
19 if (OS_IPFoundList(srcip, logr.denyips)) {
23 if (logr.allowips != NULL) {
24 if (OS_IPFoundList(srcip, logr.allowips)) {
29 /* If the IP is not allowed, it will be denied */
33 /* Handle each client */
34 static void HandleClient(int client_socket, char *srcip)
36 int sb_size = OS_MAXSTR;
39 char buffer[OS_MAXSTR + 2];
40 char storage_buffer[OS_MAXSTR + 2];
41 char tmp_buffer[OS_MAXSTR + 2];
43 char *buffer_pt = NULL;
46 if (CreatePID(ARGV0, getpid()) < 0) {
47 ErrorExit(PID_ERROR, ARGV0);
50 /* Initialize some variables */
51 memset(buffer, '\0', OS_MAXSTR + 2);
52 memset(storage_buffer, '\0', OS_MAXSTR + 2);
53 memset(tmp_buffer, '\0', OS_MAXSTR + 2);
57 /* If we fail, we need to return and close the socket */
58 if ((r_sz = OS_RecvTCPBuffer(client_socket, buffer, OS_MAXSTR - 2)) < 0) {
64 /* We must have a new line at the end */
65 buffer_pt = strchr(buffer, '\n');
68 if ((sb_size - r_sz) <= 2) {
69 merror("%s: Full buffer receiving from: '%s'", ARGV0, srcip);
71 storage_buffer[0] = '\0';
75 strncat(storage_buffer, buffer, sb_size);
80 /* See if we received more than just one message */
81 if (*(buffer_pt + 1) != '\0') {
84 strncpy(tmp_buffer, buffer_pt, OS_MAXSTR);
87 /* Store everything in the storage_buffer
88 * Check if buffer will be full
90 if ((sb_size - r_sz) <= 2) {
91 merror("%s: Full buffer receiving from: '%s'.", ARGV0, srcip);
93 storage_buffer[0] = '\0';
98 strncat(storage_buffer, buffer, sb_size);
100 /* Remove carriage returns too */
101 buffer_pt = strchr(storage_buffer, '\r');
106 /* Remove syslog header */
107 if (storage_buffer[0] == '<') {
108 buffer_pt = strchr(storage_buffer + 1, '>');
112 buffer_pt = storage_buffer;
115 buffer_pt = storage_buffer;
118 /* Send to the queue */
119 if (SendMSG(logr.m_queue, buffer_pt, srcip, SYSLOG_MQ) < 0) {
120 merror(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
122 if ((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
123 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
127 /* Clean up the buffers */
128 if (tmp_buffer[0] != '\0') {
129 strncpy(storage_buffer, tmp_buffer, OS_MAXSTR);
130 sb_size = OS_MAXSTR - (strlen(storage_buffer) + 1);
131 tmp_buffer[0] = '\0';
133 storage_buffer[0] = '\0';
139 /* Handle syslog TCP connections */
140 void HandleSyslogTCP()
143 char srcip[IPSIZE + 1];
144 fd_set fdsave, fdwork; /* select() work areas */
145 int fdmax; /* max socket number + 1 */
146 int sock; /* active socket */
148 /* Initialize some variables */
149 memset(srcip, '\0', IPSIZE + 1);
151 /* initialize select() save area */
152 fdsave = logr.netinfo->fdset;
153 fdmax = logr.netinfo->fdmax; /* value preset to max fd + 1 */
155 /* Connecting to the message queue
158 if ((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
159 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
163 /* Wait for the children */
166 wp = waitpid((pid_t) - 1, NULL, WNOHANG);
168 merror(WAITPID_ERROR, ARGV0, errno, strerror(errno));
171 /* if = 0, we still need to wait for the child process */
179 /* process connections through select() for multiple sockets */
181 if (select (fdmax, &fdwork, NULL, NULL, NULL) < 0) {
182 ErrorExit("ERROR: Call to syslogtcp select() failed, errno %d - %s",
183 errno, strerror (errno));
186 /* read through socket list for active socket */
187 for (sock = 0; sock <= fdmax; sock++) {
188 if (FD_ISSET (sock, &fdwork)) {
190 /* Accept new connections */
191 int client_socket = OS_AcceptTCP(sock, srcip, IPSIZE);
192 if (client_socket < 0) {
193 merror("%s: WARN: Accepting tcp connection from client failed.", ARGV0);
197 /* Check if IP is allowed here */
198 if (OS_IPNotAllowed(srcip)) {
199 merror(DENYIP_WARN, ARGV0, srcip);
200 close(client_socket);
204 /* Fork to deal with new client */
206 HandleClient(client_socket, srcip);
211 /* Close client socket, since the child is handling it */
212 close(client_socket);
215 } /* if socket active */
216 } /* for() loop on available sockets */
217 } /* while(1) loop for messages */