e0c76da82f7bf0e8cc4e8bcc327cf82a0b0be174
[ossec-hids.git] / src / config / agentlessd-config.c
1 /* @(#) $Id$ */
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 /* Functions to handle the configuration files
13  */
14
15
16 #include "shared.h"
17 #include "agentlessd-config.h"
18
19
20 int Read_CAgentless(XML_NODE node, void *config, void *config2)
21 {
22     int i = 0,j = 0,s = 0;
23
24     /* XML definitions */
25     char *xml_lessd_server = "host";
26     char *xml_lessd_port = "port";
27     char *xml_lessd_type = "type";
28     char *xml_lessd_frequency = "frequency";
29     char *xml_lessd_state = "state";
30     char *xml_lessd_command = "run_command";
31     char *xml_lessd_options = "arguments";
32
33
34     agentlessd_config *lessd_config = (agentlessd_config *)config;
35
36      
37     /* Getting any configured entry. */
38     if(lessd_config->entries)
39     {
40         while(lessd_config->entries[s])
41             s++;
42     }
43
44     
45     /* Allocating the memory for the config. */
46     os_realloc(lessd_config->entries, (s + 2) * sizeof(agentlessd_entries *), 
47                lessd_config->entries);
48     os_calloc(1, sizeof(agentlessd_entries), lessd_config->entries[s]);
49     lessd_config->entries[s + 1] = NULL;
50
51
52     /* Zeroing the elements. */
53     lessd_config->entries[s]->server = NULL;
54     lessd_config->entries[s]->command = NULL;
55     lessd_config->entries[s]->options = "";
56     lessd_config->entries[s]->type = NULL;
57     lessd_config->entries[s]->frequency = 86400;
58     lessd_config->entries[s]->state = 0;
59     lessd_config->entries[s]->current_state = 0;
60     lessd_config->entries[s]->port = 0;
61     lessd_config->entries[s]->error_flag = 0;
62
63     
64     /* Reading the XML. */
65     while(node[i])
66     {
67         if(!node[i]->element)
68         {
69             merror(XML_ELEMNULL, ARGV0);
70             return(OS_INVALID);
71         }
72         else if(!node[i]->content)
73         {
74             merror(XML_VALUENULL, ARGV0, node[i]->element);
75             return(OS_INVALID);
76         }
77         else if(strcmp(node[i]->element, xml_lessd_frequency) == 0)
78         {
79             if(!OS_StrIsNum(node[i]->content))
80             {
81                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
82                 return(OS_INVALID);
83             }
84
85             lessd_config->entries[s]->frequency = atoi(node[i]->content);
86         }
87         else if(strcmp(node[i]->element, xml_lessd_port) == 0)
88         {
89             if(!OS_StrIsNum(node[i]->content))
90             {
91                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
92                 return(OS_INVALID);
93             }
94
95             lessd_config->entries[s]->port = atoi(node[i]->content);
96         }
97         else if(strcmp(node[i]->element, xml_lessd_server) == 0)
98         {
99             char s_content[1024 +1];
100             s_content[1024] = '\0';
101             
102             /* Getting any configured entry. */
103             j = 0;
104             if(lessd_config->entries[s]->server)
105             {
106                 while(lessd_config->entries[s]->server[j])
107                     j++;
108             }
109
110             os_realloc(lessd_config->entries[s]->server, (j + 2) * 
111                        sizeof(char *), 
112                        lessd_config->entries[s]->server);
113             if(strncmp(node[i]->content, "use_su ", 7) == 0)
114             {
115                 snprintf(s_content, 1024, "s%s", node[i]->content +7);
116             }
117             else if(strncmp(node[i]->content, "use_sudo ", 9) == 0)
118             {
119                 snprintf(s_content, 1024, "o%s", node[i]->content +9);
120             }
121             else
122             {
123                 snprintf(s_content, 1024, " %s", node[i]->content);
124             }
125             
126             os_strdup(s_content, 
127                       lessd_config->entries[s]->server[j]);
128             lessd_config->entries[s]->server[j + 1] = NULL;
129         }
130         else if(strcmp(node[i]->element, xml_lessd_type) == 0)
131         {
132             char script_path[1024 +1];
133
134             script_path[1024] = '\0';
135             snprintf(script_path, 1024, "%s/%s", AGENTLESSDIRPATH,
136                                          node[i]->content);
137             
138             if(File_DateofChange(script_path) <= 0)
139             {
140                 merror("%s: ERROR: Unable to find '%s' at '%s'.",
141                        ARGV0, node[i]->content, AGENTLESSDIRPATH); 
142                 merror(XML_VALUEERR,ARGV0, node[i]->element, node[i]->content);
143                 return(OS_INVALID);
144             }
145             os_strdup(node[i]->content, lessd_config->entries[s]->type);
146         }
147         else if(strcmp(node[i]->element, xml_lessd_command) == 0)
148         {
149             os_strdup(node[i]->content, lessd_config->entries[s]->command);
150         }
151         else if(strcmp(node[i]->element, xml_lessd_options) == 0)
152         {
153             os_strdup(node[i]->content, lessd_config->entries[s]->options);
154         }
155         else if(strcmp(node[i]->element, xml_lessd_state) == 0)
156         {
157             if(strcmp(node[i]->content, "periodic") == 0)
158             {
159                 lessd_config->entries[s]->state |= LESSD_STATE_PERIODIC;
160             }
161             else if(strcmp(node[i]->content, "stay_connected") == 0)
162             {
163                 lessd_config->entries[s]->state |= LESSD_STATE_CONNECTED;
164             }
165             else if(strcmp(node[i]->content, "periodic_diff") == 0)
166             {
167                 lessd_config->entries[s]->state |= LESSD_STATE_PERIODIC;
168                 lessd_config->entries[s]->state |= LESSD_STATE_DIFF;
169             }
170             else
171             {
172                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
173                 return(OS_INVALID);
174             }
175         }
176         else
177         {
178             merror(XML_INVELEM, ARGV0, node[i]->element);
179             return(OS_INVALID);
180         }
181         i++;
182     }
183
184
185     /* We must have at least one entry set */
186     if(!lessd_config->entries[s]->server ||
187        !lessd_config->entries[s]->state ||
188        !lessd_config->entries[s]->type)
189     {
190         merror(XML_INV_MISSOPTS, ARGV0);
191         return(OS_INVALID);
192     }
193        
194     
195     if((lessd_config->entries[s]->state == LESSD_STATE_PERIODIC) &&
196        !lessd_config->entries[s]->frequency)
197     {
198         merror(XML_INV_MISSFREQ, ARGV0);
199         return(OS_INVALID);
200     }
201
202     return(0);
203 }
204
205
206 /* EOF */