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_connection = "connection";
38 char *xml_remote_lip = "local_ip";
42 /* Getting allowed-ips */
45 while(logr->allowips[allow_size -1])
49 /* Getting denied-ips */
52 while(logr->denyips[deny_size -1])
57 /* conn and port must not be null */
60 os_calloc(1, sizeof(int), logr->conn);
65 os_calloc(1, sizeof(int), logr->port);
70 os_calloc(1, sizeof(int), logr->proto);
75 os_calloc(1, sizeof(char *), logr->lip);
81 while(logr->conn[pl] != 0)
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)
92 merror(MEM_ERROR, ARGV0);
100 logr->port[pl +1] = 0;
101 logr->conn[pl +1] = 0;
102 logr->proto[pl +1] = 0;
103 logr->lip[pl +1] = NULL;
107 if(!node[i]->element)
109 merror(XML_ELEMNULL, ARGV0);
112 else if(!node[i]->content)
114 merror(XML_VALUENULL, ARGV0, node[i]->element);
117 else if(strcasecmp(node[i]->element,xml_remote_connection) == 0)
119 if(strcmp(node[i]->content, "syslog") == 0)
121 logr->conn[pl] = SYSLOG_CONN;
123 else if(strcmp(node[i]->content, "secure") == 0)
125 logr->conn[pl] = SECURE_CONN;
129 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
133 else if(strcasecmp(node[i]->element,xml_remote_port) == 0)
135 if(!OS_StrIsNum(node[i]->content))
137 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
140 logr->port[pl] = atoi(node[i]->content);
142 if(logr->port[pl] <= 0 || logr->port[pl] > 65535)
144 merror(PORT_ERROR, ARGV0, logr->port[pl]);
148 else if(strcasecmp(node[i]->element,xml_remote_proto) == 0)
150 if(strcasecmp(node[i]->content, "tcp") == 0)
152 logr->proto[pl] = TCP_PROTO;
154 else if(strcasecmp(node[i]->content, "udp") == 0)
156 logr->proto[pl] = UDP_PROTO;
160 merror(XML_VALUEERR,ARGV0,node[i]->element,
165 else if(strcasecmp(node[i]->element,xml_remote_lip) == 0)
167 os_strdup(node[i]->content,logr->lip[pl]);
168 if(OS_IsValidIP(logr->lip[pl], NULL) != 1)
170 merror(INVALID_IP, ARGV0, node[i]->content);
174 else if(strcmp(node[i]->element, xml_allowips) == 0)
177 logr->allowips =realloc(logr->allowips,sizeof(os_ip *)*allow_size);
180 merror(MEM_ERROR, ARGV0);
184 os_calloc(1, sizeof(os_ip), logr->allowips[allow_size -2]);
185 logr->allowips[allow_size -1] = NULL;
187 if(!OS_IsValidIP(node[i]->content,logr->allowips[allow_size -2]))
189 merror(INVALID_IP, ARGV0, node[i]->content);
193 else if(strcmp(node[i]->element, xml_denyips) == 0)
196 logr->denyips = realloc(logr->denyips,sizeof(os_ip *)*deny_size);
199 merror(MEM_ERROR, ARGV0);
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]))
207 merror(INVALID_IP, ARGV0, node[i]->content);
213 merror(XML_INVELEM, ARGV0, node[i]->element);
219 /* conn must be set */
220 if(logr->conn[pl] == 0)
222 merror(CONN_ERROR, ARGV0);
226 /* Set port in here */
227 if(logr->port[pl] == 0)
229 if(logr->conn[pl] == SECURE_CONN)
230 logr->port[pl] = DEFAULT_SECURE;
232 logr->port[pl] = DEFAULT_SYSLOG;
235 /* set default protocol */
236 if(logr->proto[pl] == 0)
238 logr->proto[pl] = UDP_PROTO;
241 /* Secure connections only run on UDP */
242 if((logr->conn[pl] == SECURE_CONN) && (logr->proto[pl] == TCP_PROTO))
244 logr->proto[pl] = UDP_PROTO;