1 /* @(#) $Id: ./src/addagent/manage_agents.c, 2012/02/07 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
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
18 * Add/extract and remove agents from a server.
22 #include "manage_agents.h"
23 #include "os_crypto/md5/md5_op.h"
27 /* Global internal variables */
31 /* chomp: remove spaces, new lines, etc from a string */
32 char *chomp(char *str)
37 /* Removing spaces from the beginning */
38 while(*str == ' ' || *str == '\t')
42 /* Removing any trailing new lines or \r */
45 tmp_str = strchr(str, '\n');
52 tmp_str = strchr(str, '\r');
57 }while(tmp_str != NULL);
60 /* Removing spaces at the end of the string */
64 while((size >= 0) && (tmp_str[size] == ' ' || tmp_str[size] == '\t'))
80 char str1[STR_SIZE +1];
81 char str2[STR_SIZE +1];
91 char name[FILE_SIZE +1];
92 char id[FILE_SIZE +1];
93 char ip[FILE_SIZE +1];
97 /* Checking if we can open the auth_file */
98 fp = fopen(AUTH_FILE,"a");
101 ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
106 /* Allocating for c_ip */
107 os_calloc(1, sizeof(os_ip), c_ip);
111 chmod(AUTH_FILE, 0440);
118 /* Source is time1+ time2 +pid + ppid */
123 srandom(time2 + time1 + getpid() + getppid());
126 srandom(time2 + time1 + getpid());
132 /* Zeroing strings */
133 memset(str1,'\0', STR_SIZE +1);
134 memset(str2,'\0', STR_SIZE +1);
140 /* Getting the name */
141 memset(name, '\0', FILE_SIZE +1);
147 /* Read the agent's name from user environment. If it is invalid
148 * we should force user to provide a name from input device. */
149 _name = getenv("OSSEC_AGENT_NAME");
150 if (_name == NULL || NameExist(_name) || !OS_IsValidName(_name))
151 _name = read_from_user();
153 if(strcmp(_name, QUIT) == 0)
156 strncpy(name, _name, FILE_SIZE -1);
159 if(!OS_IsValidName(name))
160 printf(INVALID_NAME,name);
162 /* Search for name -- no duplicates */
164 printf(ADD_ERROR_NAME, name);
166 } while(NameExist(name) || !OS_IsValidName(name));
170 memset(ip, '\0', FILE_SIZE +1);
177 /* Read IP address from user's environment. If that IP is invalid,
178 * force user to provide IP from input device */
179 _ip = getenv("OSSEC_AGENT_IP");
180 if (_ip == NULL || !OS_IsValidIP(_ip, c_ip))
181 _ip = read_from_user();
184 if(strcmp(_ip, QUIT) == 0)
187 strncpy(ip, _ip, FILE_SIZE -1);
189 if(!OS_IsValidIP(ip, c_ip))
191 printf(IP_ERROR, ip);
201 i = MAX_AGENTS + 768;
202 snprintf(id, 8, "%03d", i);
206 snprintf(id, 8, "%03d", i);
208 /* No key present, use id 0 */
215 snprintf(id, 8, "%03d", i+1);
221 /* Get Agent id from environment. If 0, use default ID. If null,
222 * get from user input. If value from environment is invalid,
223 * we force user to specify an ID from the terminal. Otherwise,
224 * our program goes to infinite loop. */
225 _id = getenv("OSSEC_AGENT_ID");
226 if (_id == NULL || IDExist(_id) || !OS_IsValidID(_id)) {
227 _id = read_from_user();
230 /* If user specified 0 as Agent ID, he meant use default value.
231 * NOTE: a bad condistion can cause infinite loop. */
232 if (strcmp(_id,"0") == 0) {
233 strncpy(_id, id, FILE_SIZE -1);
237 if(strcmp(_id, QUIT) == 0)
243 strncpy(id, _id, FILE_SIZE -1);
246 if(!OS_IsValidID(id))
247 printf(INVALID_ID, id);
249 /* Search for ID KEY -- no duplicates */
251 printf(ADD_ERROR_ID, id);
253 } while(IDExist(id) || !OS_IsValidID(id));
257 printf(AGENT_INFO, id, name, ip);
263 /* Confirmation by an environment variable. The valid value is y/Y.
264 * If the user provide anything other string, it is considered as
265 * n/N; please note that the old code only accepts y/Y/n/N. So if
266 * the variable OSSEC_ACTION_CONFIRMED is 'foobar', the program will
267 * go into an infinite loop. */
268 user_input = getenv("OSSEC_ACTION_CONFIRMED");
269 if (user_input == NULL) user_input = read_from_user();
271 /* If user accepts to add */
272 if(user_input[0] == 'y' || user_input[0] == 'Y')
277 fp = fopen(AUTH_FILE,"a");
280 ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE);
283 chmod(AUTH_FILE, 0440);
287 /* Random 1: Time took to write the agent information.
288 * Random 2: Time took to choose the action.
289 * Random 3: All of this + time + pid
290 * Random 4: Md5 all of this + the name, key and ip
291 * Random 5: Final key
294 snprintf(str1, STR_SIZE, "%d%s%d",time3-time2, name, rand1);
295 snprintf(str2, STR_SIZE, "%d%s%s%d", time2-time1, ip, id, rand2);
297 OS_MD5_Str(str1, md1);
298 OS_MD5_Str(str2, md2);
300 snprintf(str1, STR_SIZE, "%s%d%d%d",md1,(int)getpid(), (int)random(),
302 OS_MD5_Str(str1, md1);
304 fprintf(fp,"%s %s %s %s%s\n",id, name, c_ip->ip, md1,md2);
309 restart_necessary = 1;
312 else /* if(user_input[0] == 'n' || user_input[0] == 'N') */
324 /* remove an agent */
329 char u_id[FILE_SIZE +1];
331 u_id[FILE_SIZE] = '\0';
333 if(!print_agents(0, 0, 0))
344 user_input = getenv("OSSEC_AGENT_ID");
345 if (user_input == NULL || !IDExist(user_input)) {
346 user_input = read_from_user();
349 if(strcmp(user_input, QUIT) == 0)
352 strncpy(u_id, user_input, FILE_SIZE);
354 if(!IDExist(user_input))
356 printf(NO_ID, user_input);
358 } while(!IDExist(user_input));
362 printf(REMOVE_CONFIRM);
365 user_input = getenv("OSSEC_ACTION_CONFIRMED");
366 if (user_input == NULL) {
367 user_input = read_from_user();
369 /* If user confirm */
370 if(user_input[0] == 'y' || user_input[0] == 'Y')
372 /* Getting full agent name */
373 char *full_name = getFullnameById(u_id);
376 ErrorExit(MEM_ERROR, ARGV0);
379 fp = fopen(AUTH_FILE, "r+");
383 ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
386 chmod(AUTH_FILE, 0440);
390 /* Removing the agent, but keeping the id. */
391 fsetpos(fp, &fp_pos);
392 fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id);
397 /* Remove counter for id */
398 delete_agentinfo(full_name);
399 OS_RemoveCounter(u_id);
404 printf(REMOVE_DONE, u_id);
405 restart_necessary = 1;
408 else /* if(user_input[0] == 'n' || user_input[0] == 'N') */
420 int list_agents(int cmdlist)
422 if(!print_agents(0, 0, 0))