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