Imported Upstream version 2.7
[ossec-hids.git] / src / remoted / syslog.c
1 /* @(#) $Id: ./src/remoted/syslog.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
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
10  * Foundation
11  */
12
13
14
15 #include "shared.h"
16 #include "os_net/os_net.h"
17
18 #include "remoted.h"
19
20
21
22 /* OS_IPNotAllowed, v0.1, 2005/02/11
23  * Checks if an IP is not allowed.
24  */
25 static int OS_IPNotAllowed(char *srcip)
26 {
27     if(logr.denyips != NULL)
28     {
29         if(OS_IPFoundList(srcip, logr.denyips))
30         {
31             return(1);
32         }
33     }
34     if(logr.allowips != NULL)
35     {
36         if(OS_IPFoundList(srcip, logr.allowips))
37         {
38             return(0);
39         }
40     }
41
42     /* If the ip is not allowed, it will be denied */
43     return(1);
44 }
45
46
47 /** void HandleSyslog() v0.2
48  * Handle syslog connections
49  */
50 void HandleSyslog()
51 {
52     char buffer[OS_SIZE_1024 +2];
53     char srcip[IPSIZE +1];
54
55     char *buffer_pt = NULL;
56
57     int recv_b;
58
59     struct sockaddr_in peer_info;
60     socklen_t peer_size;
61
62
63     /* setting peer size */
64     peer_size = sizeof(peer_info);
65
66
67     /* Initializing some variables */
68     memset(buffer, '\0', OS_SIZE_1024 +2);
69
70
71     /* Connecting to the message queue
72      * Exit if it fails.
73      */
74     if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
75     {
76         ErrorExit(QUEUE_FATAL,ARGV0, DEFAULTQUEUE);
77     }
78
79
80     /* Infinite loop in here */
81     while(1)
82     {
83         /* Receiving message  */
84         recv_b = recvfrom(logr.sock, buffer, OS_SIZE_1024, 0,
85                 (struct sockaddr *)&peer_info, &peer_size);
86
87         /* Nothing received */
88         if(recv_b <= 0)
89             continue;
90
91
92         /* null terminating the message */
93         buffer[recv_b] = '\0';
94
95
96         /* Removing new line */
97         if(buffer[recv_b -1] == '\n')
98         {
99             buffer[recv_b -1] = '\0';
100         }
101
102         /* Setting the source ip */
103         strncpy(srcip, inet_ntoa(peer_info.sin_addr), IPSIZE);
104         srcip[IPSIZE] = '\0';
105
106
107         /* Removing syslog header */
108         if(buffer[0] == '<')
109         {
110             buffer_pt = strchr(buffer+1, '>');
111             if(buffer_pt)
112             {
113                 buffer_pt++;
114             }
115             else
116             {
117                 buffer_pt = buffer;
118             }
119         }
120         else
121         {
122             buffer_pt = buffer;
123         }
124
125         /* Checking if IP is allowed here */
126         if(OS_IPNotAllowed(srcip))
127         {
128             merror(DENYIP_WARN,ARGV0,srcip);
129         }
130
131         else if(SendMSG(logr.m_queue, buffer_pt, srcip,
132                         SYSLOG_MQ) < 0)
133         {
134             merror(QUEUE_ERROR,ARGV0,DEFAULTQUEUE, strerror(errno));
135             if((logr.m_queue = StartMQ(DEFAULTQUEUE,READ)) < 0)
136             {
137                 ErrorExit(QUEUE_FATAL,ARGV0,DEFAULTQUEUE);
138             }
139         }
140     }
141 }
142
143
144
145 /* EOF */