new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / remoted / syslog.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
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
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "os_net/os_net.h"
12 #include "remoted.h"
13
14 /* Prototypes */
15 static int OS_IPNotAllowed(const char *srcip);
16
17
18 /* Check if an IP is not allowed */
19 static int OS_IPNotAllowed(const char *srcip)
20 {
21     if (logr.denyips != NULL) {
22         if (OS_IPFoundList(srcip, logr.denyips)) {
23             return (1);
24         }
25     }
26     if (logr.allowips != NULL) {
27         if (OS_IPFoundList(srcip, logr.allowips)) {
28             return (0);
29         }
30     }
31
32     /* If the IP is not allowed, it will be denied */
33     return (1);
34 }
35
36 /* Handle syslog connections */
37 void HandleSyslog()
38 {
39     char buffer[OS_SIZE_1024 + 2];
40     char srcip[IPSIZE + 1];
41     char *buffer_pt = NULL;
42     ssize_t recv_b;
43     struct sockaddr_storage peer_info;
44     socklen_t peer_size;
45     fd_set fdsave, fdwork;                      /* select() work areas */
46     int fdmax;                                  /* max socket number + 1 */
47     int sock;                                   /* active socket */
48
49     /* Set peer size */
50     peer_size = sizeof(peer_info);
51
52     /* Initialize some variables */
53     memset(buffer, '\0', OS_SIZE_1024 + 2);
54
55     /* initialize select() save area */
56     fdsave = logr.netinfo->fdset;
57     fdmax  = logr.netinfo->fdmax;        /* value preset to max fd + 1 */
58
59     /* Connect to the message queue
60      * Exit if it fails.
61      */
62     if ((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
63         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
64     }
65
66     /* Infinite loop */
67     while (1) {
68         /* process connections through select() for multiple sockets */
69         fdwork = fdsave;
70         if (select (fdmax, &fdwork, NULL, NULL, NULL) < 0) {
71             ErrorExit("ERROR: Call to syslog select() failed, errno %d - %s",
72                       errno, strerror (errno));
73         }
74
75         /* read through socket list for active socket */
76         for (sock = 0; sock <= fdmax; sock++) {
77             if (FD_ISSET (sock, &fdwork)) {
78
79                 /* Receive message */
80                 recv_b = recvfrom(sock, buffer, OS_SIZE_1024, 0,
81                                   (struct sockaddr *)&peer_info, &peer_size);
82
83                 /* Nothing received */
84                 if (recv_b <= 0) {
85                     continue;
86                 }
87
88                 /* Null-terminate the message */
89                 buffer[recv_b] = '\0';
90
91                 /* Remove newline */
92                 if (buffer[recv_b - 1] == '\n') {
93                     buffer[recv_b - 1] = '\0';
94                 }
95
96                 /* Set the source IP */
97                 satop((struct sockaddr *) &peer_info, srcip, IPSIZE);
98                 srcip[IPSIZE] = '\0';
99
100                 /* Remove syslog header */
101                 if (buffer[0] == '<') {
102                     buffer_pt = strchr(buffer + 1, '>');
103                     if (buffer_pt) {
104                         buffer_pt++;
105                     } else {
106                         buffer_pt = buffer;
107                     }
108                 } else {
109                     buffer_pt = buffer;
110                 }
111
112                 /* Check if IP is allowed here */
113                 if (OS_IPNotAllowed(srcip)) {
114                     merror(DENYIP_WARN, ARGV0, srcip);
115                     continue;
116                 }
117
118                 if (SendMSG(logr.m_queue, buffer_pt, srcip, SYSLOG_MQ) < 0) {
119                     merror(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
120
121                     if ((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
122                         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
123                     }
124                 }
125             } /* if socket active */
126         } /* for() loop on sockets */
127     } /* while(1) loop for messages */
128 }
129