1 /* @(#) $Id: ./src/os_execd/exec.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
15 #include "os_regex/os_regex.h"
18 char exec_names[MAX_AR +1][OS_FLSIZE +1];
19 char exec_cmd[MAX_AR +1][OS_FLSIZE +1];
20 int exec_timeout[MAX_AR +1];
22 int f_time_reading = 1;
25 /** int ReadExecConfig() v0.1:
26 * Reads the shared exec config.
27 * Returns 1 on success or 0 on failure.
28 * Format of the file is 'name - command - timeout'
32 int i = 0, j = 0, dup_entry = 0;
35 char buffer[OS_MAXSTR +1];
38 for(i = 0;i <= exec_size+1; i++)
40 memset(exec_names[i], '\0', OS_FLSIZE +1);
41 memset(exec_cmd[i], '\0', OS_FLSIZE +1);
48 fp = fopen(DEFAULTARPATH, "r");
51 merror(FOPEN_ERROR, ARGV0, DEFAULTARPATH);
57 while(fgets(buffer, OS_MAXSTR, fp) != NULL)
64 /* Cleaning up the buffer */
65 tmp_str = strchr(buffer, ' ');
68 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
75 /* Searching for ' ' and - */
82 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
88 /* Setting the name */
89 strncpy(exec_names[exec_size], str_pt, OS_FLSIZE);
90 exec_names[exec_size][OS_FLSIZE] = '\0';
95 tmp_str = strchr(tmp_str, ' ');
98 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
104 /* Writting the full command path */
105 snprintf(exec_cmd[exec_size], OS_FLSIZE,
109 process_file = fopen(exec_cmd[exec_size], "r");
114 verbose("%s: INFO: Active response command not present: '%s'. "
115 "Not using it on this system.",
116 ARGV0, exec_cmd[exec_size]);
119 exec_cmd[exec_size][0] = '\0';
123 fclose(process_file);
127 /* Searching for ' ' and - */
135 merror(EXEC_INV_CONF, ARGV0, DEFAULTARPATH);
141 tmp_str = strchr(tmp_str, '\n');
146 /* Getting the exec timeout */
147 exec_timeout[exec_size] = atoi(str_pt);
150 /* Checking if name is duplicated. */
152 for(j = 0; j< exec_size; j++)
154 if(strcmp(exec_names[j], exec_names[exec_size]) == 0)
156 if(exec_cmd[j][0] == '\0')
158 strncpy(exec_cmd[j], exec_cmd[exec_size], OS_FLSIZE);
159 exec_cmd[j][OS_FLSIZE] = '\0';
163 else if(exec_cmd[exec_size][0] == '\0')
172 exec_cmd[exec_size][0] = '\0';
173 exec_names[exec_size][0] = '\0';
174 exec_timeout[exec_size] = 0;
190 /** char *GetCommandbyName(char *name, int *timeout) v0.2
191 * Returns a pointer to the command name (full path)
192 * Returns NULL if name cannot be found
193 * If timeout is not NULL, write the timeout for that
196 char *GetCommandbyName(char *name, int *timeout)
200 for(;i < exec_size; i++)
202 if(strcmp(name, exec_names[i]) == 0)
204 *timeout = exec_timeout[i];
213 /** void ExecCmd(char **cmd, char *extra_data) v0.1
214 * Execute command given. Must be a argv** NULL terminated.
215 * Void. Prints error to log message in case of problems.
217 void ExecCmd(char **cmd)
223 /* Forking and leaving it running */
227 if(execv(*cmd, cmd) < 0)
229 merror(EXEC_CMDERROR, ARGV0, *cmd, strerror(errno));
242 void ExecCmd_Win32(char *cmd)
244 /* Windows code now. */
248 PROCESS_INFORMATION pi;
250 ZeroMemory( &si, sizeof(si) );
252 ZeroMemory( &pi, sizeof(pi) );
254 if(!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL,
257 merror("%s: ERROR: Unable to create active response process. ", ARGV0);
262 /* Wait until process exits. */
263 WaitForSingleObject(pi.hProcess, INFINITE );
265 /* Close process and thread */
266 CloseHandle( pi.hProcess );
267 CloseHandle( pi.hThread );