Imported Upstream version 2.7
[ossec-hids.git] / src / config / client-config.c
1 /*   $OSSEC, client-config.c, v0.1, 2005/04/01, Daniel B. Cid$   */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right 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
14 #include "shared.h"
15 #include "client-config.h"
16 #include "os_net/os_net.h"
17
18
19 int Read_Client(XML_NODE node, void *d1, void *d2)
20 {
21     int i = 0;
22
23     /* XML definitions */
24     char *xml_client_ip = "server-ip";
25     char *xml_client_hostname = "server-hostname";
26     char *xml_local_ip = "local_ip";
27     char *xml_client_port = "port";
28     char *xml_ar_disabled = "disable-active-response";
29     /* cmoraes */
30     char *xml_profile_name = "config-profile";
31
32     agent *logr;
33
34     logr = (agent *)d1;
35
36
37     while(node[i])
38     {
39         if(!node[i]->element)
40         {
41             merror(XML_ELEMNULL, ARGV0);
42             return(OS_INVALID);
43         }
44         else if(!node[i]->content)
45         {
46             merror(XML_VALUENULL, ARGV0, node[i]->element);
47             return(OS_INVALID);
48         }
49         /* Getting local ip. */
50         else if(strcmp(node[i]->element, xml_local_ip) == 0)
51         {
52             os_strdup(node[i]->content, logr->lip);
53             if(OS_IsValidIP(logr->lip, NULL) != 1)
54             {
55                 merror(INVALID_IP, ARGV0, logr->lip);
56                 return(OS_INVALID);
57             }
58         }
59         /* Getting server ip */
60         else if(strcmp(node[i]->element,xml_client_ip) == 0)
61         {
62             int ip_id = 0;
63
64             /* Getting last ip */
65             if(logr->rip)
66             {
67                 while(logr->rip[ip_id])
68                 {
69                     ip_id++;
70                 }
71             }
72             os_realloc(logr->rip, (ip_id + 2) * sizeof(char*), logr->rip);
73             logr->rip[ip_id] = NULL;
74             logr->rip[ip_id +1] = NULL;
75
76             os_strdup(node[i]->content, logr->rip[ip_id]);
77             if(OS_IsValidIP(logr->rip[ip_id], NULL) != 1)
78             {
79                 merror(INVALID_IP, ARGV0, logr->rip[ip_id]);
80                 return(OS_INVALID);
81             }
82             logr->rip_id++;
83         }
84         else if(strcmp(node[i]->element,xml_client_hostname) == 0)
85         {
86             int ip_id = 0;
87             char *s_ip;
88             char f_ip[128];
89
90
91             /* Getting last ip. */
92             if(logr->rip)
93             {
94                 while(logr->rip[ip_id])
95                 {
96                     ip_id++;
97                 }
98             }
99
100             os_realloc(logr->rip, (ip_id + 2) * sizeof(char*),
101                        logr->rip);
102
103
104             s_ip = OS_GetHost(node[i]->content, 5);
105             if(!s_ip)
106             {
107                 merror("%s: WARN: Unable to get hostname for '%s'.",
108                        ARGV0, node[i]->content);
109                 merror(AG_INV_HOST, ARGV0, node[i]->content);
110
111                 os_strdup("invalid_ip", s_ip);
112             }
113
114
115             f_ip[127] = '\0';
116             snprintf(f_ip, 127, "%s/%s", node[i]->content, s_ip);
117
118             os_strdup(f_ip, logr->rip[ip_id]);
119             logr->rip[ip_id +1] = NULL;
120
121             free(s_ip);
122
123             logr->rip_id++;
124         }
125         else if(strcmp(node[i]->element,xml_client_port) == 0)
126         {
127             if(!OS_StrIsNum(node[i]->content))
128             {
129                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
130                 return(OS_INVALID);
131             }
132             logr->port = atoi(node[i]->content);
133
134             if(logr->port <= 0 || logr->port > 65535)
135             {
136                 merror(PORT_ERROR, ARGV0, logr->port);
137                 return(OS_INVALID);
138             }
139         }
140         else if(strcmp(node[i]->element,xml_ar_disabled) == 0)
141         {
142             if(strcmp(node[i]->content, "yes") == 0)
143                 logr->execdq = -1;
144             else if(strcmp(node[i]->content, "no") == 0)
145                 logr->execdq = 0;
146             else
147             {
148                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
149                 return(OS_INVALID);
150             }
151         }
152         /* cmoraes */
153         else if(strcmp(node[i]->element,xml_profile_name) == 0)
154         {
155             /* profile name can be anything hence no validation */
156             os_strdup(node[i]->content, logr->profile);
157         }
158         else
159         {
160             merror(XML_INVELEM, ARGV0, node[i]->element);
161             return(OS_INVALID);
162         }
163         i++;
164     }
165
166     if(!logr->rip)
167     {
168         return(OS_INVALID);
169     }
170
171     return(0);
172 }
173
174
175 /* EOF */