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 static int OS_IPNotAllowed(const char *srcip);
18 /* Check if an IP is not allowed */
19 static int OS_IPNotAllowed(const char *srcip)
21 if (logr.denyips != NULL) {
22 if (OS_IPFoundList(srcip, logr.denyips)) {
26 if (logr.allowips != NULL) {
27 if (OS_IPFoundList(srcip, logr.allowips)) {
32 /* If the IP is not allowed, it will be denied */
36 /* Handle syslog connections */
39 char buffer[OS_SIZE_1024 + 2];
40 char srcip[IPSIZE + 1];
41 char *buffer_pt = NULL;
43 struct sockaddr_storage peer_info;
45 fd_set fdsave, fdwork; /* select() work areas */
46 int fdmax; /* max socket number + 1 */
47 int sock; /* active socket */
50 peer_size = sizeof(peer_info);
52 /* Initialize some variables */
53 memset(buffer, '\0', OS_SIZE_1024 + 2);
55 /* initialize select() save area */
56 fdsave = logr.netinfo->fdset;
57 fdmax = logr.netinfo->fdmax; /* value preset to max fd + 1 */
59 /* Connect to the message queue
62 if ((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
63 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
68 /* process connections through select() for multiple sockets */
70 if (select (fdmax, &fdwork, NULL, NULL, NULL) < 0) {
71 ErrorExit("ERROR: Call to syslog select() failed, errno %d - %s",
72 errno, strerror (errno));
75 /* read through socket list for active socket */
76 for (sock = 0; sock <= fdmax; sock++) {
77 if (FD_ISSET (sock, &fdwork)) {
80 recv_b = recvfrom(sock, buffer, OS_SIZE_1024, 0,
81 (struct sockaddr *)&peer_info, &peer_size);
83 /* Nothing received */
88 /* Null-terminate the message */
89 buffer[recv_b] = '\0';
92 if (buffer[recv_b - 1] == '\n') {
93 buffer[recv_b - 1] = '\0';
96 /* Set the source IP */
97 satop((struct sockaddr *) &peer_info, srcip, IPSIZE);
100 /* Remove syslog header */
101 if (buffer[0] == '<') {
102 buffer_pt = strchr(buffer + 1, '>');
112 /* Check if IP is allowed here */
113 if (OS_IPNotAllowed(srcip)) {
114 merror(DENYIP_WARN, ARGV0, srcip);
118 if (SendMSG(logr.m_queue, buffer_pt, srcip, SYSLOG_MQ) < 0) {
119 merror(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
121 if ((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
122 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
125 } /* if socket active */
126 } /* for() loop on sockets */
127 } /* while(1) loop for messages */