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