1 /* @(#) $Id: manage_agents.c,v 1.33 2009/08/19 18:35:57 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
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
17 * Add/extract and remove agents from a server.
21 #include "manage_agents.h"
22 #include "os_crypto/md5/md5_op.h"
26 /* Global internal variables */
30 /* chomp: remove spaces, new lines, etc from a string */
31 char *chomp(char *str)
36 /* Removing spaces from the beginning */
37 while(*str == ' ' || *str == '\t')
41 /* Removing any trailing new lines or \r */
44 tmp_str = strchr(str, '\n');
51 tmp_str = strchr(str, '\r');
56 }while(tmp_str != NULL);
59 /* Removing spaces at the end of the string */
63 while((size >= 0) && (tmp_str[size] == ' ' || tmp_str[size] == '\t'))
79 char str1[STR_SIZE +1];
80 char str2[STR_SIZE +1];
90 char name[FILE_SIZE +1];
91 char id[FILE_SIZE +1];
92 char ip[FILE_SIZE +1];
96 /* Checking if we can open the auth_file */
97 fp = fopen(AUTH_FILE,"a");
100 ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
105 /* Allocating for c_ip */
106 os_calloc(1, sizeof(os_ip), c_ip);
110 chmod(AUTH_FILE, 0440);
117 /* Source is time1+ time2 +pid + ppid */
122 srandom(time2 + time1 + getpid() + getppid());
125 srandom(time2 + time1 + getpid());
131 /* Zeroing strings */
132 memset(str1,'\0', STR_SIZE +1);
133 memset(str2,'\0', STR_SIZE +1);
139 /* Getting the name */
140 memset(name, '\0', FILE_SIZE +1);
146 _name = read_from_user();
148 if(strcmp(_name, QUIT) == 0)
151 strncpy(name, _name, FILE_SIZE -1);
154 if(!OS_IsValidName(name))
155 printf(INVALID_NAME,name);
157 /* Search for name -- no duplicates */
159 printf(ADD_ERROR_NAME, name);
161 } while(NameExist(name) || !OS_IsValidName(name));
165 memset(ip, '\0', FILE_SIZE +1);
172 _ip = read_from_user();
175 if(strcmp(_ip, QUIT) == 0)
178 strncpy(ip, _ip, FILE_SIZE -1);
180 if(!OS_IsValidIP(ip, c_ip))
182 printf(IP_ERROR, ip);
193 snprintf(id, 8, "%03d", i);
197 snprintf(id, 8, "%03d", i);
199 /* No key present, use id 0 */
206 snprintf(id, 8, "%03d", i+1);
212 _id = read_from_user();
217 if(strcmp(_id, QUIT) == 0)
223 strncpy(id, _id, FILE_SIZE -1);
226 if(!OS_IsValidID(id))
227 printf(INVALID_ID, id);
229 /* Search for ID KEY -- no duplicates */
231 printf(ADD_ERROR_ID, id);
233 } while(IDExist(id) || !OS_IsValidID(id));
237 printf(AGENT_INFO, id, name, ip);
243 user_input = read_from_user();
245 /* If user accepts to add */
246 if(user_input[0] == 'y' || user_input[0] == 'Y')
251 fp = fopen(AUTH_FILE,"a");
254 ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE);
257 chmod(AUTH_FILE, 0440);
261 /* Random 1: Time took to write the agent information.
262 * Random 2: Time took to choose the action.
263 * Random 3: All of this + time + pid
264 * Random 4: Md5 all of this + the name, key and ip
265 * Random 5: Final key
268 snprintf(str1, STR_SIZE, "%d%s%d",time3-time2, name, rand1);
269 snprintf(str2, STR_SIZE, "%d%s%s%d", time2-time1, ip, id, rand2);
271 OS_MD5_Str(str1, md1);
272 OS_MD5_Str(str2, md2);
274 snprintf(str1, STR_SIZE, "%s%d%d%d",md1,(int)getpid(), (int)random(),
276 OS_MD5_Str(str1, md1);
278 fprintf(fp,"%s %s %s %s%s\n",id, name, c_ip->ip, md1,md2);
283 restart_necessary = 1;
286 else if(user_input[0] == 'n' || user_input[0] == 'N')
298 /* remove an agent */
303 char u_id[FILE_SIZE +1];
305 u_id[FILE_SIZE] = '\0';
307 if(!print_agents(0, 0, 0))
318 user_input = read_from_user();
320 if(strcmp(user_input, QUIT) == 0)
323 strncpy(u_id, user_input, FILE_SIZE);
325 if(!IDExist(user_input))
327 printf(NO_ID, user_input);
329 } while(!IDExist(user_input));
333 printf(REMOVE_CONFIRM);
336 user_input = read_from_user();
338 /* If user confirm */
339 if(user_input[0] == 'y' || user_input[0] == 'Y')
341 /* Getting full agent name */
342 char *full_name = getFullnameById(u_id);
345 ErrorExit(MEM_ERROR, ARGV0);
348 fp = fopen(AUTH_FILE, "r+");
352 ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
355 chmod(AUTH_FILE, 0440);
359 /* Removing the agent, but keeping the id. */
360 fsetpos(fp, &fp_pos);
361 fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id);
366 /* Remove counter for id */
367 delete_agentinfo(full_name);
368 OS_RemoveCounter(u_id);
373 printf(REMOVE_DONE, u_id);
374 restart_necessary = 1;
377 else if(user_input[0] == 'n' || user_input[0] == 'N')
389 int list_agents(int cmdlist)
391 if(!print_agents(0, 0, 0))