1 /* @(#) $Id: ./src/os_execd/win_execd.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
18 #include "os_regex/os_regex.h"
19 #include "os_net/os_net.h"
28 #define ARGV0 "ossec-execd"
33 /* Timeout data structure */
34 typedef struct _timeout_data
36 time_t time_of_addition;
44 OSListNode *timeout_node;
49 /** int main(int argc, char **argv) v0.1
56 char *xmlcfg = DEFAULTCPATH;
61 if((c = ExecdConfig(xmlcfg)) < 0)
63 ErrorExit(CONFIG_ERROR, ARGV0, xmlcfg);
67 /* Exit if test_config */
72 /* Active response disabled */
75 verbose(EXEC_DISABLED, ARGV0);
80 /* Creating list for timeout */
81 timeout_list = OSList_Create();
84 ErrorExit(LIST_ERROR, ARGV0);
89 /* Start up message */
90 verbose(STARTUP_MSG, ARGV0, getpid());
98 void WinTimeoutRun(int curr_time)
100 /* Checking if there is any timeouted command to execute. */
101 timeout_node = OSList_GetFirstNode(timeout_list);
104 timeout_data *list_entry;
106 list_entry = (timeout_data *)timeout_node->data;
109 if((curr_time - list_entry->time_of_addition) >
110 list_entry->time_to_block)
112 ExecCmd_Win32(list_entry->command[0]);
114 /* Deletecurrently node already sets the pointer to next */
115 OSList_DeleteCurrentlyNode(timeout_list);
116 timeout_node = OSList_GetCurrentlyNode(timeout_list);
118 /* Clearing the memory */
119 FreeTimeoutEntry(list_entry);
124 timeout_node = OSList_GetNextNode(timeout_list);
131 /** void WinExecdRun(char *exec_msg)
133 void WinExecdRun(char *exec_msg)
139 int added_before = 0;
144 char *tmp_msg = NULL;
149 char buffer[OS_MAXSTR + 1];
152 timeout_data *timeout_entry;
161 /* Getting application name */
165 /* Zeroing the name */
166 tmp_msg = strchr(exec_msg, ' ');
169 merror(EXECD_INV_MSG, ARGV0, exec_msg);
178 tmp_msg = strchr(tmp_msg, ' ');
181 merror(EXECD_INV_MSG, ARGV0, cmd_user);
190 tmp_msg = strchr(tmp_msg, ' ');
193 merror(EXECD_INV_MSG, ARGV0, cmd_ip);
200 /* Getting the command to execute (valid name) */
201 command = GetCommandbyName(name, &timeout_value);
205 command = GetCommandbyName(name, &timeout_value);
208 merror(EXEC_INV_NAME, ARGV0, name);
214 /* Command not present. */
215 if(command[0] == '\0')
219 /* Allocating memory for the timeout argument */
220 os_calloc(MAX_ARGS+2, sizeof(char *), timeout_args);
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;
231 /* Getting size for the strncmp */
233 while(buffer[i] != '\0')
244 /* Check this command was already executed. */
245 timeout_node = OSList_GetFirstNode(timeout_list);
251 timeout_data *list_entry;
253 list_entry = (timeout_data *)timeout_node->data;
254 if(strncmp(list_entry->command[0], timeout_args[0], i) == 0)
256 /* Means we executed this command before
257 * and we don't need to add it again.
262 /* updating the timeout */
263 list_entry->time_of_addition = curr_time;
267 /* Continue with the next entry in timeout list*/
268 timeout_node = OSList_GetNextNode(timeout_list);
272 /* If it wasn't added before, do it now */
275 snprintf(buffer, OS_MAXSTR, "\"%s\" %s \"%s\" \"%s\" \"%s\"", command,
276 ADD_ENTRY, cmd_user, cmd_ip, tmp_msg);
277 /* executing command */
279 ExecCmd_Win32(buffer);
281 /* We don't need to add to the list if the timeout_value == 0 */
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;
291 /* Adding command to the timeout list */
292 if(!OSList_AddData(timeout_list, timeout_entry))
294 merror(LIST_ADD_ERROR, ARGV0);
295 FreeTimeoutEntry(timeout_entry);
299 /* If no timeout, we still need to free it in here */
302 char **ss_ta = timeout_args;
305 os_free(*timeout_args);
306 *timeout_args = NULL;
313 /* We didn't add it to the timeout list */
316 char **ss_ta = timeout_args;
318 /* Clear the timeout arguments */
321 os_free(*timeout_args);
322 *timeout_args = NULL;