Imported Upstream version 2.7
[ossec-hids.git] / src / config / remote-config.c
1 /*   $OSSEC, remote-config.c, v0.3, 2005/11/09, Daniel B. Cid$   */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights 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 #include "shared.h"
14 #include "remote-config.h"
15
16
17 /* Read_Remote: Reads remote config
18  */
19 int Read_Remote(XML_NODE node, void *d1, void *d2)
20 {
21     int i = 0;
22     int pl = 0;
23
24     int allow_size = 1;
25     int deny_size = 1;
26     remoted *logr;
27
28     /*** XML Definitions ***/
29
30     /* Allowed and denied IPS */
31     char *xml_allowips = "allowed-ips";
32     char *xml_denyips = "denied-ips";
33
34     /* Remote options */        
35     char *xml_remote_port = "port";
36     char *xml_remote_proto = "protocol";
37     char *xml_remote_ipv6 = "ipv6";
38     char *xml_remote_connection = "connection";
39     char *xml_remote_lip = "local_ip";
40
41     logr = (remoted *)d1;
42
43     /* Getting allowed-ips */
44     if(logr->allowips)
45     {
46         while(logr->allowips[allow_size -1])
47             allow_size++;
48     }
49
50     /* Getting denied-ips */
51     if(logr->denyips)
52     {
53         while(logr->denyips[deny_size -1])
54             deny_size++;
55     }
56
57
58     /* conn and port must not be null */
59     if(!logr->conn)
60     {
61         os_calloc(1, sizeof(int), logr->conn);
62         logr->conn[0] = 0;
63     }
64     if(!logr->port)
65     {
66         os_calloc(1, sizeof(int), logr->port);
67         logr->port[0] = 0;
68     }
69     if(!logr->proto)
70     {
71         os_calloc(1, sizeof(int), logr->proto);
72         logr->proto[0] = 0;
73     }
74     if(!logr->ipv6)
75     {
76         os_calloc(1, sizeof(int), logr->ipv6);
77         logr->ipv6[0] = 0;
78     }
79     if(!logr->lip)
80     {
81         os_calloc(1, sizeof(char *), logr->lip);
82         logr->lip[0] = NULL;
83     }
84
85
86     /* Cleaning */
87     while(logr->conn[pl] != 0)
88         pl++;
89
90
91     /* Adding space for the last null connection/port */
92     logr->port = realloc(logr->port, sizeof(int)*(pl +2));
93     logr->conn = realloc(logr->conn, sizeof(int)*(pl +2));
94     logr->proto = realloc(logr->proto, sizeof(int)*(pl +2));
95     logr->ipv6 = realloc(logr->ipv6, sizeof(int)*(pl +2));
96     logr->lip = realloc(logr->lip, sizeof(char *)*(pl +2));
97     if(!logr->port || !logr->conn || !logr->proto || !logr->lip)
98     {
99         merror(MEM_ERROR, ARGV0);
100     }
101
102     logr->port[pl] = 0;
103     logr->conn[pl] = 0;
104     logr->proto[pl] = 0;
105     logr->ipv6[pl] = 0;
106     logr->lip[pl] = NULL;
107
108     logr->port[pl +1] = 0;
109     logr->conn[pl +1] = 0;
110     logr->proto[pl +1] = 0;
111     logr->ipv6[pl +1] = 0;
112     logr->lip[pl +1] = NULL;
113
114     while(node[i])
115     {
116         if(!node[i]->element)
117         {
118             merror(XML_ELEMNULL, ARGV0);
119             return(OS_INVALID);
120         }
121         else if(!node[i]->content)
122         {
123             merror(XML_VALUENULL, ARGV0, node[i]->element);
124             return(OS_INVALID);
125         }
126         else if(strcasecmp(node[i]->element,xml_remote_connection) == 0)
127         {
128             if(strcmp(node[i]->content, "syslog") == 0)
129             {
130                 logr->conn[pl] = SYSLOG_CONN;
131             }
132             else if(strcmp(node[i]->content, "secure") == 0)
133             {
134                 logr->conn[pl] = SECURE_CONN;
135             }
136             else
137             {
138                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
139                 return(OS_INVALID);
140             }
141         }
142         else if(strcasecmp(node[i]->element,xml_remote_port) == 0)
143         {
144             if(!OS_StrIsNum(node[i]->content))
145             {
146                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
147                 return(OS_INVALID);
148             }
149             logr->port[pl] = atoi(node[i]->content);
150
151             if(logr->port[pl] <= 0 || logr->port[pl] > 65535)
152             {
153                 merror(PORT_ERROR, ARGV0, logr->port[pl]);
154                 return(OS_INVALID);
155             }
156         }
157         else if(strcasecmp(node[i]->element,xml_remote_proto) == 0)
158         {
159             if(strcasecmp(node[i]->content, "tcp") == 0)
160             {
161                 logr->proto[pl] = TCP_PROTO;
162             }
163             else if(strcasecmp(node[i]->content, "udp") == 0)
164             {
165                 logr->proto[pl] = UDP_PROTO;
166             }
167             else
168             {
169                 merror(XML_VALUEERR,ARGV0,node[i]->element,
170                        node[i]->content);
171                 return(OS_INVALID);
172             }
173         }
174         else if(strcasecmp(node[i]->element,xml_remote_ipv6) == 0)
175         {
176             if(strcasecmp(node[i]->content, "yes") == 0)
177             {
178                 logr->ipv6[pl] = 1;
179             }
180         }
181         else if(strcasecmp(node[i]->element,xml_remote_lip) == 0)
182         {
183             os_strdup(node[i]->content,logr->lip[pl]);
184             if(OS_IsValidIP(logr->lip[pl], NULL) != 1)
185             {
186                 merror(INVALID_IP, ARGV0, node[i]->content);
187                 return(OS_INVALID);
188             }
189         }
190         else if(strcmp(node[i]->element, xml_allowips) == 0)
191         {
192             allow_size++;
193             logr->allowips =realloc(logr->allowips,sizeof(os_ip *)*allow_size);
194             if(!logr->allowips)
195             {
196                 merror(MEM_ERROR, ARGV0);
197                 return(OS_INVALID);
198             }
199
200             os_calloc(1, sizeof(os_ip), logr->allowips[allow_size -2]);
201             logr->allowips[allow_size -1] = NULL;
202
203             if(!OS_IsValidIP(node[i]->content,logr->allowips[allow_size -2]))
204             {
205                 merror(INVALID_IP, ARGV0, node[i]->content);
206                 return(OS_INVALID);
207             }
208         }
209         else if(strcmp(node[i]->element, xml_denyips) == 0)
210         {
211             deny_size++;
212             logr->denyips = realloc(logr->denyips,sizeof(os_ip *)*deny_size);
213             if(!logr->denyips)
214             {
215                 merror(MEM_ERROR, ARGV0);
216                 return(OS_INVALID);
217             }
218
219             os_calloc(1, sizeof(os_ip), logr->denyips[deny_size -2]);
220             logr->denyips[deny_size -1] = NULL;
221             if(!OS_IsValidIP(node[i]->content, logr->denyips[deny_size -2]))
222             {
223                 merror(INVALID_IP, ARGV0, node[i]->content);
224                 return(OS_INVALID);
225             }
226         }
227         else
228         {
229             merror(XML_INVELEM, ARGV0, node[i]->element);
230             return(OS_INVALID);
231         }
232         i++;
233     }
234
235     /* conn must be set */
236     if(logr->conn[pl] == 0)
237     {
238         merror(CONN_ERROR, ARGV0);
239         return(OS_INVALID);
240     }
241
242     /* Set port in here */
243     if(logr->port[pl] == 0)
244     {
245         if(logr->conn[pl] == SECURE_CONN)
246             logr->port[pl] = DEFAULT_SECURE;
247         else
248             logr->port[pl] = DEFAULT_SYSLOG;
249     }
250
251     /* set default protocol */
252     if(logr->proto[pl] == 0)
253     {
254         logr->proto[pl] = UDP_PROTO;
255     }
256
257     /* Secure connections only run on UDP */
258     if((logr->conn[pl] == SECURE_CONN) && (logr->proto[pl] == TCP_PROTO))
259     {
260         logr->proto[pl] = UDP_PROTO;
261     }
262
263     return(0);
264 }
265
266
267 /* EOF */