Imported Upstream version 2.5.1
[ossec-hids.git] / src / os_execd / win_execd.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
13 #ifdef WIN32
14
15 #include "shared.h"
16 #include "list_op.h"
17 #include "os_regex/os_regex.h"
18 #include "os_net/os_net.h"
19
20 #include "execd.h"
21
22
23 #ifdef ARGV0
24    #undef ARGV0
25 #endif
26       
27 #define ARGV0 "ossec-execd"
28    
29
30
31
32 /* Timeout data structure */
33 typedef struct _timeout_data
34 {
35     time_t time_of_addition;
36     int time_to_block;
37     char **command;
38 }timeout_data;
39
40
41 /* Timeout list */
42 OSList *timeout_list;
43 OSListNode *timeout_node;
44             
45
46
47
48 /** int main(int argc, char **argv) v0.1
49  */
50 int WinExecd_Start()
51 {
52     int c;
53     int test_config = 0;
54
55     char *xmlcfg = DEFAULTCPATH;
56
57
58
59     /* Reading config */
60     if((c = ExecdConfig(xmlcfg)) < 0)
61     {
62         ErrorExit(CONFIG_ERROR, ARGV0, xmlcfg);
63     }
64
65
66     /* Exit if test_config */
67     if(test_config)
68         return(0);
69         
70         
71     /* Active response disabled */
72     if(c == 1)
73     {
74         verbose(EXEC_DISABLED, ARGV0);
75         return(0);
76     }
77     
78
79     /* Creating list for timeout */
80     timeout_list = OSList_Create();
81     if(!timeout_list)
82     {
83         ErrorExit(LIST_ERROR, ARGV0);
84     }
85                                     
86     
87
88     /* Start up message */
89     verbose(STARTUP_MSG, ARGV0, getpid());
90         
91
92     return(1);
93 }
94
95
96
97 void WinTimeoutRun(int curr_time)
98 {
99     /* Checking if there is any timeouted command to execute. */
100     timeout_node = OSList_GetFirstNode(timeout_list);
101     while(timeout_node)
102     {
103         timeout_data *list_entry;
104
105         list_entry = (timeout_data *)timeout_node->data;
106
107         /* Timeouted */
108         if((curr_time - list_entry->time_of_addition) > 
109             list_entry->time_to_block)
110         {
111             ExecCmd_Win32(list_entry->command[0]);
112
113             /* Deletecurrently node already sets the pointer to next */
114             OSList_DeleteCurrentlyNode(timeout_list);
115             timeout_node = OSList_GetCurrentlyNode(timeout_list);
116
117             /* Clearing the memory */
118             FreeTimeoutEntry(list_entry);
119         }
120
121         else
122         {
123             timeout_node = OSList_GetNextNode(timeout_list);
124         }
125     }
126 }
127
128
129
130 /** void WinExecdRun(char *exec_msg)
131  */
132 void WinExecdRun(char *exec_msg)
133 {
134     time_t curr_time;
135
136     int i,j;
137     int timeout_value;
138     int added_before = 0;
139
140     char **timeout_args;
141
142
143     char *tmp_msg = NULL;
144     char *name;
145     char *command;
146     char *cmd_user;
147     char *cmd_ip;
148     char buffer[OS_MAXSTR + 1];
149     
150
151     timeout_data *timeout_entry;
152
153
154
155
156     /* Currently time */
157     curr_time = time(0);
158
159
160     /* Getting application name */
161     name = exec_msg;
162
163
164     /* Zeroing the name */
165     tmp_msg = strchr(exec_msg, ' ');
166     if(!tmp_msg)
167     {
168         merror(EXECD_INV_MSG, ARGV0, exec_msg);
169         return;
170     }
171     *tmp_msg = '\0';
172     tmp_msg++;
173
174
175     /* Getting user. */
176     cmd_user = tmp_msg;
177     tmp_msg = strchr(tmp_msg, ' ');
178     if(!tmp_msg)
179     {
180         merror(EXECD_INV_MSG, ARGV0, cmd_user);
181         return;
182     }
183     *tmp_msg = '\0';
184     tmp_msg++;
185
186
187     /* Getting ip. */
188     cmd_ip = tmp_msg;
189     tmp_msg = strchr(tmp_msg, ' ');
190     if(!tmp_msg)
191     {
192         merror(EXECD_INV_MSG, ARGV0, cmd_ip);
193         return;
194     }
195     *tmp_msg = '\0';
196     tmp_msg++;
197     
198
199     /* Getting the command to execute (valid name) */
200     command = GetCommandbyName(name, &timeout_value);
201     if(!command)
202     {
203         ReadExecConfig();
204         command = GetCommandbyName(name, &timeout_value);
205         if(!command)
206         {
207             merror(EXEC_INV_NAME, ARGV0, name);
208             return;
209         }
210     }
211
212
213     /* Command not present. */
214     if(command[0] == '\0')
215         return;
216
217
218     /* Allocating memory for the timeout argument */
219     os_calloc(MAX_ARGS+2, sizeof(char *), timeout_args);
220
221
222     /* Adding initial variables to the timeout cmd */
223     snprintf(buffer, OS_MAXSTR, "\"%s\" %s \"%s\" \"%s\" \"%s\"", 
224              command, DELETE_ENTRY, cmd_user, cmd_ip, tmp_msg); 
225     os_strdup(buffer, timeout_args[0]);
226     timeout_args[1] = NULL;
227     
228
229
230     /* Getting size for the strncmp */
231     i = 0, j = 0;
232     while(buffer[i] != '\0')
233     {
234         if(buffer[i] == ' ')
235             j++;
236         
237         i++;
238         if(j == 4)
239             break;
240     }
241     
242
243     /* Check this command was already executed. */
244     timeout_node = OSList_GetFirstNode(timeout_list);
245     added_before = 0;
246
247
248     while(timeout_node)
249     {
250         timeout_data *list_entry;
251
252         list_entry = (timeout_data *)timeout_node->data;
253         if(strncmp(list_entry->command[0], timeout_args[0], i) == 0)
254         {
255             /* Means we executed this command before
256              * and we don't need to add it again.
257              */
258             added_before = 1;
259
260
261             /* updating the timeout */
262             list_entry->time_of_addition = curr_time;
263             break;
264         }
265
266         /* Continue with the next entry in timeout list*/
267         timeout_node = OSList_GetNextNode(timeout_list);
268     }
269
270
271     /* If it wasn't added before, do it now */
272     if(!added_before)
273     {
274         snprintf(buffer, OS_MAXSTR, "\"%s\" %s \"%s\" \"%s\" \"%s\"", command, 
275                                     ADD_ENTRY, cmd_user, cmd_ip, tmp_msg);
276         /* executing command */
277
278         ExecCmd_Win32(buffer);
279
280         /* We don't need to add to the list if the timeout_value == 0 */
281         if(timeout_value)
282         {
283             /* Creating the timeout entry */
284             os_calloc(1, sizeof(timeout_data), timeout_entry);
285             timeout_entry->command = timeout_args;
286             timeout_entry->time_of_addition = curr_time;
287             timeout_entry->time_to_block = timeout_value;
288
289
290             /* Adding command to the timeout list */
291             if(!OSList_AddData(timeout_list, timeout_entry))
292             {
293                 merror(LIST_ADD_ERROR, ARGV0);
294                 FreeTimeoutEntry(timeout_entry);
295             } 
296         }
297
298         /* If no timeout, we still need to free it in here */
299         else
300         {
301             char **ss_ta = timeout_args;
302             while(*timeout_args)
303             {
304                 os_free(*timeout_args);
305                 *timeout_args = NULL;
306                 timeout_args++;
307             }
308             os_free(ss_ta);
309         }
310     }
311
312     /* We didn't add it to the timeout list */
313     else
314     {
315         char **ss_ta = timeout_args;
316
317         /* Clear the timeout arguments */
318         while(*timeout_args)
319         {
320             os_free(*timeout_args);
321             *timeout_args = NULL;
322             timeout_args++;
323         }
324
325         os_free(ss_ta);
326     }
327 }
328
329 #endif
330
331 /* EOF */