1 /* @(#) $Id: exec.c,v 1.20 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
14 #include "os_regex/os_regex.h"
17 char exec_names[MAX_AR +1][OS_FLSIZE +1];
18 char exec_cmd[MAX_AR +1][OS_FLSIZE +1];
19 int exec_timeout[MAX_AR +1];
21 int f_time_reading = 1;
24 /** int ReadExecConfig() v0.1:
25 * Reads the shared exec config.
26 * Returns 1 on success or 0 on failure.
27 * Format of the file is 'name - command - timeout'
31 int i = 0, j = 0, dup_entry = 0;
34 char buffer[OS_MAXSTR +1];
37 for(i = 0;i <= exec_size+1; i++)
39 memset(exec_names[i], '\0', OS_FLSIZE +1);
40 memset(exec_cmd[i], '\0', OS_FLSIZE +1);
47 fp = fopen(DEFAULTARPATH, "r");
50 merror(FOPEN_ERROR, ARGV0, DEFAULTARPATH);
56 while(fgets(buffer, OS_MAXSTR, fp) != NULL)
63 /* Cleaning up the buffer */
64 tmp_str = strchr(buffer, ' ');
67 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
74 /* Searching for ' ' and - */
81 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
87 /* Setting the name */
88 strncpy(exec_names[exec_size], str_pt, OS_FLSIZE);
89 exec_names[exec_size][OS_FLSIZE] = '\0';
94 tmp_str = strchr(tmp_str, ' ');
97 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
103 /* Writting the full command path */
104 snprintf(exec_cmd[exec_size], OS_FLSIZE,
108 process_file = fopen(exec_cmd[exec_size], "r");
113 verbose("%s: INFO: Active response command not present: '%s'. "
114 "Not using it on this system.",
115 ARGV0, exec_cmd[exec_size]);
118 exec_cmd[exec_size][0] = '\0';
122 fclose(process_file);
126 /* Searching for ' ' and - */
134 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
140 tmp_str = strchr(tmp_str, '\n');
145 /* Getting the exec timeout */
146 exec_timeout[exec_size] = atoi(str_pt);
149 /* Checking if name is duplicated. */
151 for(j = 0; j< exec_size; j++)
153 if(strcmp(exec_names[j], exec_names[exec_size]) == 0)
155 if(exec_cmd[j][0] == '\0')
157 strncpy(exec_cmd[j], exec_cmd[exec_size], OS_FLSIZE);
158 exec_cmd[j][OS_FLSIZE] = '\0';
162 else if(exec_cmd[exec_size][0] == '\0')
171 exec_cmd[exec_size][0] = '\0';
172 exec_names[exec_size][0] = '\0';
173 exec_timeout[exec_size] = 0;
189 /** char *GetCommandbyName(char *name, int *timeout) v0.2
190 * Returns a pointer to the command name (full path)
191 * Returns NULL if name cannot be found
192 * If timeout is not NULL, write the timeout for that
195 char *GetCommandbyName(char *name, int *timeout)
199 for(;i < exec_size; i++)
201 if(strcmp(name, exec_names[i]) == 0)
203 *timeout = exec_timeout[i];
212 /** void ExecCmd(char **cmd, char *extra_data) v0.1
213 * Execute command given. Must be a argv** NULL terminated.
214 * Void. Prints error to log message in case of problems.
216 void ExecCmd(char **cmd)
222 /* Forking and leaving it running */
226 if(execv(*cmd, cmd) < 0)
228 merror(EXEC_CMDERROR, ARGV0, *cmd, strerror(errno));
241 void ExecCmd_Win32(char *cmd)
243 /* Windows code now. */
247 PROCESS_INFORMATION pi;
249 ZeroMemory( &si, sizeof(si) );
251 ZeroMemory( &pi, sizeof(pi) );
253 if(!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL,
256 merror("%s: ERROR: Unable to create active response process. ", ARGV0);
261 /* Wait until process exits. */
262 WaitForSingleObject(pi.hProcess, INFINITE );
264 /* Close process and thread */
265 CloseHandle( pi.hProcess );
266 CloseHandle( pi.hThread );