new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / config / remote-config.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "remote-config.h"
12 #include "config.h"
13
14
15 /* Reads remote config */
16 int Read_Remote(XML_NODE node, void *d1, __attribute__((unused)) void *d2)
17 {
18     int i = 0;
19     unsigned int pl = 0;
20     unsigned int allow_size = 1;
21     unsigned int deny_size = 1;
22     int portnum;
23     remoted *logr;
24
25     /*** XML Definitions ***/
26
27     /* Allowed and denied IPS */
28     const char *xml_allowips = "allowed-ips";
29     const char *xml_denyips = "denied-ips";
30
31     /* Remote options */
32     const char *xml_remote_port = "port";
33     const char *xml_remote_proto = "protocol";
34     const char *xml_remote_ipv6 = "ipv6";
35     const char *xml_remote_connection = "connection";
36     const char *xml_remote_lip = "local_ip";
37
38     logr = (remoted *)d1;
39
40     /* Getting allowed-ips */
41     if (logr->allowips) {
42         while (logr->allowips[allow_size - 1]) {
43             allow_size++;
44         }
45     }
46
47     /* Getting denied-ips */
48     if (logr->denyips) {
49         while (logr->denyips[deny_size - 1]) {
50             deny_size++;
51         }
52     }
53
54     /* conn and port must not be null */
55     if (!logr->conn) {
56         os_calloc(1, sizeof(int), logr->conn);
57         logr->conn[0] = 0;
58     }
59     if(!logr->port) {
60         os_calloc(1, sizeof(char *), logr->port);
61         logr->port[0] = NULL;
62     }
63     if (!logr->proto) {
64         os_calloc(1, sizeof(int), logr->proto);
65         logr->proto[0] = 0;
66     }
67     if (!logr->ipv6) {
68         os_calloc(1, sizeof(int), logr->ipv6);
69         logr->ipv6[0] = 0;
70     }
71     if (!logr->lip) {
72         os_calloc(1, sizeof(char *), logr->lip);
73         logr->lip[0] = NULL;
74     }
75
76     /* Clean */
77     while (logr->conn[pl] != 0) {
78         pl++;
79     }
80
81     /* Add space for the last null connection/port */
82     logr->port = (char **) realloc(logr->port, sizeof(char *) * (pl + 2));
83     logr->conn = (int *) realloc(logr->conn, sizeof(int) * (pl + 2));
84     logr->proto = (int *) realloc(logr->proto, sizeof(int) * (pl + 2));
85     logr->ipv6 = (int *) realloc(logr->ipv6, sizeof(int) * (pl + 2));
86     logr->lip = (char **) realloc(logr->lip, sizeof(char *) * (pl + 2));
87     if (!logr->port || !logr->conn || !logr->proto || !logr->ipv6 || !logr->lip) {
88         ErrorExit(MEM_ERROR, __local_name, errno, strerror(errno));
89     }
90
91     logr->port[pl] = NULL;
92     logr->conn[pl] = 0;
93     logr->proto[pl] = 0;
94     logr->ipv6[pl] = 0;
95     logr->lip[pl] = NULL;
96
97     logr->port[pl + 1] = NULL;
98     logr->conn[pl + 1] = 0;
99     logr->proto[pl + 1] = 0;
100     logr->ipv6[pl + 1] = 0;
101     logr->lip[pl + 1] = NULL;
102
103     while (node[i]) {
104         if (!node[i]->element) {
105             merror(XML_ELEMNULL, __local_name);
106             return (OS_INVALID);
107         } else if (!node[i]->content) {
108             merror(XML_VALUENULL, __local_name, node[i]->element);
109             return (OS_INVALID);
110         } else if (strcasecmp(node[i]->element, xml_remote_connection) == 0) {
111             if (strcmp(node[i]->content, "syslog") == 0) {
112                 logr->conn[pl] = SYSLOG_CONN;
113             } else if (strcmp(node[i]->content, "secure") == 0) {
114                 logr->conn[pl] = SECURE_CONN;
115             } else {
116                 merror(XML_VALUEERR, __local_name, node[i]->element, node[i]->content);
117                 return (OS_INVALID);
118             }
119         } else if (strcasecmp(node[i]->element, xml_remote_port) == 0) {
120             if (!OS_StrIsNum(node[i]->content)) {
121                 merror(XML_VALUEERR, __local_name, node[i]->element, node[i]->content);
122                 return (OS_INVALID);
123             }
124             os_strdup(node[i]->content,logr->port[pl]);
125             portnum = atoi(node[i]->content);
126
127             if (portnum <= 0 || portnum > 65535) {
128                 merror(PORT_ERROR, __local_name, portnum);
129                 return (OS_INVALID);
130             }
131         } else if (strcasecmp(node[i]->element, xml_remote_proto) == 0) {
132             if (strcasecmp(node[i]->content, "tcp") == 0) {
133                 logr->proto[pl] = IPPROTO_TCP;
134             } else if (strcasecmp(node[i]->content, "udp") == 0) {
135                 logr->proto[pl] = IPPROTO_UDP;
136             } else {
137                 merror(XML_VALUEERR, __local_name, node[i]->element,
138                        node[i]->content);
139                 return (OS_INVALID);
140             }
141         } else if (strcasecmp(node[i]->element, xml_remote_ipv6) == 0) {
142             if (strcasecmp(node[i]->content, "yes") == 0) {
143                 logr->ipv6[pl] = 1;
144             }
145         } else if (strcasecmp(node[i]->element, xml_remote_lip) == 0) {
146             os_strdup(node[i]->content, logr->lip[pl]);
147             if (OS_IsValidIP(logr->lip[pl], NULL) != 1) {
148                 merror(INVALID_IP, __local_name, node[i]->content);
149                 return (OS_INVALID);
150             }
151         } else if (strcmp(node[i]->element, xml_allowips) == 0) {
152             allow_size++;
153             logr->allowips = (os_ip **) realloc(logr->allowips, sizeof(os_ip *)*allow_size);
154             if (!logr->allowips) {
155                 merror(MEM_ERROR, __local_name, errno, strerror(errno));
156                 return (OS_INVALID);
157             }
158
159             os_calloc(1, sizeof(os_ip), logr->allowips[allow_size - 2]);
160             logr->allowips[allow_size - 1] = NULL;
161
162             if (!OS_IsValidIP(node[i]->content, logr->allowips[allow_size - 2])) {
163                 merror(INVALID_IP, __local_name, node[i]->content);
164                 return (OS_INVALID);
165             }
166         } else if (strcmp(node[i]->element, xml_denyips) == 0) {
167             deny_size++;
168             logr->denyips = (os_ip **) realloc(logr->denyips, sizeof(os_ip *)*deny_size);
169             if (!logr->denyips) {
170                 merror(MEM_ERROR, __local_name, errno, strerror(errno));
171                 return (OS_INVALID);
172             }
173
174             os_calloc(1, sizeof(os_ip), logr->denyips[deny_size - 2]);
175             logr->denyips[deny_size - 1] = NULL;
176             if (!OS_IsValidIP(node[i]->content, logr->denyips[deny_size - 2])) {
177                 merror(INVALID_IP, __local_name, node[i]->content);
178                 return (OS_INVALID);
179             }
180         } else {
181             merror(XML_INVELEM, __local_name, node[i]->element);
182             return (OS_INVALID);
183         }
184         i++;
185     }
186
187     /* conn must be set */
188     if (logr->conn[pl] == 0) {
189         merror(CONN_ERROR, __local_name);
190         return (OS_INVALID);
191     }
192
193     /* Set port in here */
194     if (logr->port[pl] == NULL) {
195         if (logr->conn[pl] == SECURE_CONN) {
196             logr->port[pl] = DEFAULT_SECURE;
197         } else {
198             logr->port[pl] = DEFAULT_SYSLOG;
199         }
200     }
201
202     /* Set default protocol */
203     if (logr->proto[pl] == 0) {
204         logr->proto[pl] = IPPROTO_UDP;
205     }
206
207     /* Secure connections only run on UDP */
208     if ((logr->conn[pl] == SECURE_CONN) && (logr->proto[pl] == IPPROTO_TCP)) {
209         logr->proto[pl] = IPPROTO_UDP;
210     }
211
212     return (0);
213 }
214