Imported Upstream version 2.5.1
[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_connection = "connection";
38     char *xml_remote_lip = "local_ip";
39
40     logr = (remoted *)d1;
41
42     /* Getting allowed-ips */
43     if(logr->allowips)
44     {
45         while(logr->allowips[allow_size -1])
46             allow_size++;
47     }
48
49     /* Getting denied-ips */
50     if(logr->denyips)
51     {
52         while(logr->denyips[deny_size -1])
53             deny_size++;
54     }
55                                             
56     
57     /* conn and port must not be null */
58     if(!logr->conn)
59     {
60         os_calloc(1, sizeof(int), logr->conn);
61         logr->conn[0] = 0;
62     }
63     if(!logr->port)
64     {
65         os_calloc(1, sizeof(int), logr->port);
66         logr->port[0] = 0;
67     }
68     if(!logr->proto)
69     {
70         os_calloc(1, sizeof(int), logr->proto);
71         logr->proto[0] = 0;
72     }
73     if(!logr->lip)
74     {
75         os_calloc(1, sizeof(char *), logr->lip);
76         logr->lip[0] = NULL;
77     }
78     
79     
80     /* Cleaning */
81     while(logr->conn[pl] != 0)
82         pl++;
83
84
85     /* Adding space for the last null connection/port */
86     logr->port = realloc(logr->port, sizeof(int)*(pl +2));
87     logr->conn = realloc(logr->conn, sizeof(int)*(pl +2));
88     logr->proto = realloc(logr->proto, sizeof(int)*(pl +2));
89     logr->lip = realloc(logr->lip, sizeof(char *)*(pl +2));
90     if(!logr->port || !logr->conn || !logr->proto || !logr->lip)
91     {
92         merror(MEM_ERROR, ARGV0);
93     }
94     
95     logr->port[pl] = 0;
96     logr->conn[pl] = 0;
97     logr->proto[pl] = 0;
98     logr->lip[pl] = NULL;
99     
100     logr->port[pl +1] = 0;
101     logr->conn[pl +1] = 0;
102     logr->proto[pl +1] = 0;
103     logr->lip[pl +1] = NULL;
104     
105     while(node[i])
106     {
107         if(!node[i]->element)
108         {
109             merror(XML_ELEMNULL, ARGV0);
110             return(OS_INVALID);
111         }
112         else if(!node[i]->content)
113         {
114             merror(XML_VALUENULL, ARGV0, node[i]->element);
115             return(OS_INVALID);
116         }
117         else if(strcasecmp(node[i]->element,xml_remote_connection) == 0)
118         {
119             if(strcmp(node[i]->content, "syslog") == 0)
120             {
121                 logr->conn[pl] = SYSLOG_CONN;
122             }
123             else if(strcmp(node[i]->content, "secure") == 0)
124             {
125                 logr->conn[pl] = SECURE_CONN;
126             }
127             else
128             {
129                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
130                 return(OS_INVALID);
131             }
132         }
133         else if(strcasecmp(node[i]->element,xml_remote_port) == 0)
134         {
135             if(!OS_StrIsNum(node[i]->content))
136             {
137                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
138                 return(OS_INVALID);
139             }
140             logr->port[pl] = atoi(node[i]->content);
141
142             if(logr->port[pl] <= 0 || logr->port[pl] > 65535)
143             {
144                 merror(PORT_ERROR, ARGV0, logr->port[pl]);
145                 return(OS_INVALID);
146             }
147         }
148         else if(strcasecmp(node[i]->element,xml_remote_proto) == 0)
149         {
150             if(strcasecmp(node[i]->content, "tcp") == 0)
151             {
152                 logr->proto[pl] = TCP_PROTO;
153             }
154             else if(strcasecmp(node[i]->content, "udp") == 0)
155             {
156                 logr->proto[pl] = UDP_PROTO;
157             }
158             else
159             {
160                 merror(XML_VALUEERR,ARGV0,node[i]->element,
161                        node[i]->content);
162                 return(OS_INVALID);
163             }
164         }
165         else if(strcasecmp(node[i]->element,xml_remote_lip) == 0)
166         {
167             os_strdup(node[i]->content,logr->lip[pl]);
168             if(OS_IsValidIP(logr->lip[pl], NULL) != 1)
169             {
170                 merror(INVALID_IP, ARGV0, node[i]->content);
171                 return(OS_INVALID);
172             }
173         }
174         else if(strcmp(node[i]->element, xml_allowips) == 0)
175         {
176             allow_size++;
177             logr->allowips =realloc(logr->allowips,sizeof(os_ip *)*allow_size);
178             if(!logr->allowips)
179             {
180                 merror(MEM_ERROR, ARGV0);
181                 return(OS_INVALID);
182             }
183
184             os_calloc(1, sizeof(os_ip), logr->allowips[allow_size -2]);
185             logr->allowips[allow_size -1] = NULL;
186             
187             if(!OS_IsValidIP(node[i]->content,logr->allowips[allow_size -2]))
188             {
189                 merror(INVALID_IP, ARGV0, node[i]->content);
190                 return(OS_INVALID);
191             }
192         }
193         else if(strcmp(node[i]->element, xml_denyips) == 0)
194         {
195             deny_size++;
196             logr->denyips = realloc(logr->denyips,sizeof(os_ip *)*deny_size);
197             if(!logr->denyips)             
198             {
199                 merror(MEM_ERROR, ARGV0);
200                 return(OS_INVALID);
201             }
202
203             os_calloc(1, sizeof(os_ip), logr->denyips[deny_size -2]);
204             logr->denyips[deny_size -1] = NULL;
205             if(!OS_IsValidIP(node[i]->content, logr->denyips[deny_size -2]))
206             {
207                 merror(INVALID_IP, ARGV0, node[i]->content);
208                 return(OS_INVALID);
209             }
210         }
211         else
212         {
213             merror(XML_INVELEM, ARGV0, node[i]->element);
214             return(OS_INVALID);
215         }
216         i++;
217     }
218
219     /* conn must be set */
220     if(logr->conn[pl] == 0)
221     {
222         merror(CONN_ERROR, ARGV0);
223         return(OS_INVALID);
224     }
225     
226     /* Set port in here */
227     if(logr->port[pl] == 0)
228     {
229         if(logr->conn[pl] == SECURE_CONN)
230             logr->port[pl] = DEFAULT_SECURE;
231         else
232             logr->port[pl] = DEFAULT_SYSLOG;    
233     }
234
235     /* set default protocol */
236     if(logr->proto[pl] == 0)
237     {
238         logr->proto[pl] = UDP_PROTO;
239     }
240
241     /* Secure connections only run on UDP */
242     if((logr->conn[pl] == SECURE_CONN) && (logr->proto[pl] == TCP_PROTO))
243     {
244         logr->proto[pl] = UDP_PROTO;
245     }
246     
247     return(0);
248 }
249
250
251 /* EOF */