1 /* @(#) $Id: win_execd.c,v 1.4 2009/06/24 17:06:30 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
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 3) as published by the FSF - Free Software
17 #include "os_regex/os_regex.h"
18 #include "os_net/os_net.h"
27 #define ARGV0 "ossec-execd"
32 /* Timeout data structure */
33 typedef struct _timeout_data
35 time_t time_of_addition;
43 OSListNode *timeout_node;
48 /** int main(int argc, char **argv) v0.1
55 char *xmlcfg = DEFAULTCPATH;
60 if((c = ExecdConfig(xmlcfg)) < 0)
62 ErrorExit(CONFIG_ERROR, ARGV0, xmlcfg);
66 /* Exit if test_config */
71 /* Active response disabled */
74 verbose(EXEC_DISABLED, ARGV0);
79 /* Creating list for timeout */
80 timeout_list = OSList_Create();
83 ErrorExit(LIST_ERROR, ARGV0);
88 /* Start up message */
89 verbose(STARTUP_MSG, ARGV0, getpid());
97 void WinTimeoutRun(int curr_time)
99 /* Checking if there is any timeouted command to execute. */
100 timeout_node = OSList_GetFirstNode(timeout_list);
103 timeout_data *list_entry;
105 list_entry = (timeout_data *)timeout_node->data;
108 if((curr_time - list_entry->time_of_addition) >
109 list_entry->time_to_block)
111 ExecCmd_Win32(list_entry->command[0]);
113 /* Deletecurrently node already sets the pointer to next */
114 OSList_DeleteCurrentlyNode(timeout_list);
115 timeout_node = OSList_GetCurrentlyNode(timeout_list);
117 /* Clearing the memory */
118 FreeTimeoutEntry(list_entry);
123 timeout_node = OSList_GetNextNode(timeout_list);
130 /** void WinExecdRun(char *exec_msg)
132 void WinExecdRun(char *exec_msg)
138 int added_before = 0;
143 char *tmp_msg = NULL;
148 char buffer[OS_MAXSTR + 1];
151 timeout_data *timeout_entry;
160 /* Getting application name */
164 /* Zeroing the name */
165 tmp_msg = strchr(exec_msg, ' ');
168 merror(EXECD_INV_MSG, ARGV0, exec_msg);
177 tmp_msg = strchr(tmp_msg, ' ');
180 merror(EXECD_INV_MSG, ARGV0, cmd_user);
189 tmp_msg = strchr(tmp_msg, ' ');
192 merror(EXECD_INV_MSG, ARGV0, cmd_ip);
199 /* Getting the command to execute (valid name) */
200 command = GetCommandbyName(name, &timeout_value);
204 command = GetCommandbyName(name, &timeout_value);
207 merror(EXEC_INV_NAME, ARGV0, name);
213 /* Command not present. */
214 if(command[0] == '\0')
218 /* Allocating memory for the timeout argument */
219 os_calloc(MAX_ARGS+2, sizeof(char *), timeout_args);
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;
230 /* Getting size for the strncmp */
232 while(buffer[i] != '\0')
243 /* Check this command was already executed. */
244 timeout_node = OSList_GetFirstNode(timeout_list);
250 timeout_data *list_entry;
252 list_entry = (timeout_data *)timeout_node->data;
253 if(strncmp(list_entry->command[0], timeout_args[0], i) == 0)
255 /* Means we executed this command before
256 * and we don't need to add it again.
261 /* updating the timeout */
262 list_entry->time_of_addition = curr_time;
266 /* Continue with the next entry in timeout list*/
267 timeout_node = OSList_GetNextNode(timeout_list);
271 /* If it wasn't added before, do it now */
274 snprintf(buffer, OS_MAXSTR, "\"%s\" %s \"%s\" \"%s\" \"%s\"", command,
275 ADD_ENTRY, cmd_user, cmd_ip, tmp_msg);
276 /* executing command */
278 ExecCmd_Win32(buffer);
280 /* We don't need to add to the list if the timeout_value == 0 */
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;
290 /* Adding command to the timeout list */
291 if(!OSList_AddData(timeout_list, timeout_entry))
293 merror(LIST_ADD_ERROR, ARGV0);
294 FreeTimeoutEntry(timeout_entry);
298 /* If no timeout, we still need to free it in here */
301 char **ss_ta = timeout_args;
304 os_free(*timeout_args);
305 *timeout_args = NULL;
312 /* We didn't add it to the timeout list */
315 char **ss_ta = timeout_args;
317 /* Clear the timeout arguments */
320 os_free(*timeout_args);
321 *timeout_args = NULL;