1 /* $OSSEC, remote-config.c, v0.3, 2005/11/09, Daniel B. Cid$ */
3 /* Copyright (C) 2009 Trend Micro Inc.
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
14 #include "remote-config.h"
17 /* Read_Remote: Reads remote config
19 int Read_Remote(XML_NODE node, void *d1, void *d2)
28 /*** XML Definitions ***/
30 /* Allowed and denied IPS */
31 char *xml_allowips = "allowed-ips";
32 char *xml_denyips = "denied-ips";
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";
43 /* Getting allowed-ips */
46 while(logr->allowips[allow_size -1])
50 /* Getting denied-ips */
53 while(logr->denyips[deny_size -1])
58 /* conn and port must not be null */
61 os_calloc(1, sizeof(int), logr->conn);
66 os_calloc(1, sizeof(int), logr->port);
71 os_calloc(1, sizeof(int), logr->proto);
76 os_calloc(1, sizeof(int), logr->ipv6);
81 os_calloc(1, sizeof(char *), logr->lip);
87 while(logr->conn[pl] != 0)
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)
99 merror(MEM_ERROR, ARGV0);
106 logr->lip[pl] = NULL;
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;
116 if(!node[i]->element)
118 merror(XML_ELEMNULL, ARGV0);
121 else if(!node[i]->content)
123 merror(XML_VALUENULL, ARGV0, node[i]->element);
126 else if(strcasecmp(node[i]->element,xml_remote_connection) == 0)
128 if(strcmp(node[i]->content, "syslog") == 0)
130 logr->conn[pl] = SYSLOG_CONN;
132 else if(strcmp(node[i]->content, "secure") == 0)
134 logr->conn[pl] = SECURE_CONN;
138 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
142 else if(strcasecmp(node[i]->element,xml_remote_port) == 0)
144 if(!OS_StrIsNum(node[i]->content))
146 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
149 logr->port[pl] = atoi(node[i]->content);
151 if(logr->port[pl] <= 0 || logr->port[pl] > 65535)
153 merror(PORT_ERROR, ARGV0, logr->port[pl]);
157 else if(strcasecmp(node[i]->element,xml_remote_proto) == 0)
159 if(strcasecmp(node[i]->content, "tcp") == 0)
161 logr->proto[pl] = TCP_PROTO;
163 else if(strcasecmp(node[i]->content, "udp") == 0)
165 logr->proto[pl] = UDP_PROTO;
169 merror(XML_VALUEERR,ARGV0,node[i]->element,
174 else if(strcasecmp(node[i]->element,xml_remote_ipv6) == 0)
176 if(strcasecmp(node[i]->content, "yes") == 0)
181 else if(strcasecmp(node[i]->element,xml_remote_lip) == 0)
183 os_strdup(node[i]->content,logr->lip[pl]);
184 if(OS_IsValidIP(logr->lip[pl], NULL) != 1)
186 merror(INVALID_IP, ARGV0, node[i]->content);
190 else if(strcmp(node[i]->element, xml_allowips) == 0)
193 logr->allowips =realloc(logr->allowips,sizeof(os_ip *)*allow_size);
196 merror(MEM_ERROR, ARGV0);
200 os_calloc(1, sizeof(os_ip), logr->allowips[allow_size -2]);
201 logr->allowips[allow_size -1] = NULL;
203 if(!OS_IsValidIP(node[i]->content,logr->allowips[allow_size -2]))
205 merror(INVALID_IP, ARGV0, node[i]->content);
209 else if(strcmp(node[i]->element, xml_denyips) == 0)
212 logr->denyips = realloc(logr->denyips,sizeof(os_ip *)*deny_size);
215 merror(MEM_ERROR, ARGV0);
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]))
223 merror(INVALID_IP, ARGV0, node[i]->content);
229 merror(XML_INVELEM, ARGV0, node[i]->element);
235 /* conn must be set */
236 if(logr->conn[pl] == 0)
238 merror(CONN_ERROR, ARGV0);
242 /* Set port in here */
243 if(logr->port[pl] == 0)
245 if(logr->conn[pl] == SECURE_CONN)
246 logr->port[pl] = DEFAULT_SECURE;
248 logr->port[pl] = DEFAULT_SYSLOG;
251 /* set default protocol */
252 if(logr->proto[pl] == 0)
254 logr->proto[pl] = UDP_PROTO;
257 /* Secure connections only run on UDP */
258 if((logr->conn[pl] == SECURE_CONN) && (logr->proto[pl] == TCP_PROTO))
260 logr->proto[pl] = UDP_PROTO;