1 /* @(#) $Id: ./src/addagent/validate.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 "manage_agents.h"
16 #include "os_crypto/md5/md5_op.h"
18 char *OS_AddNewAgent(char *name, char *ip, char *id, char *key)
25 char str1[STR_SIZE +1];
26 char str2[STR_SIZE +1];
37 srandom(time(0) + getpid() + getppid());
40 srandom(time(0) + getpid());
46 snprintf(str1, STR_SIZE, "%d%s%d%s",(int)time(0), name, rand1, muname);
47 snprintf(str2, STR_SIZE, "%s%s%ld", ip, id, (long int)random());
48 OS_MD5_Str(str1, md1);
49 OS_MD5_Str(str2, md2);
56 snprintf(nid, 6, "%d", i);
60 snprintf(nid, 6, "%d", i);
69 fp = fopen(KEYSFILE_PATH,"a");
75 os_calloc(2048, sizeof(char), finals);
77 snprintf(finals, 2048, "%s %s any %s%s",id, name, md1,md2);
79 snprintf(finals, 2048, "%s %s %s %s%s",id, name, ip, md1,md2);
81 fprintf(fp, "%s\n",finals);
88 int OS_IsValidID(char *id)
93 /* ID must not be null */
99 /* Check ID length, it should contain max. 8 characters */
103 /* Check ID if it contains only numeric characters [0-9] */
104 for(i = 0; i < id_len; i++)
106 if(!(isdigit((int)id[i])))
114 /* Get full agent name (name + ip) of ID.
116 char *getFullnameById(char *id)
119 char line_read[FILE_SIZE +1];
120 line_read[FILE_SIZE] = '\0';
122 /* ID must not be null */
126 fp = fopen(AUTH_FILE, "r");
131 while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
137 if(line_read[0] == '#')
142 name = strchr(line_read, ' ');
147 if(strcmp(line_read,id) != 0)
160 ip = strchr(name, ' ');
167 tmp_str = strchr(ip, ' ');
172 tmp_str = strchr(ip, '/');
176 /* If we reached here, we found the IP and name */
177 os_calloc(1, FILE_SIZE, final_str);
178 snprintf(final_str, FILE_SIZE -1, "%s-%s", name, ip);
192 /* ID Search (is valid ID) */
193 int IDExist(char *id)
196 char line_read[FILE_SIZE +1];
197 line_read[FILE_SIZE] = '\0';
199 /* ID must not be null */
204 fp = fopen(AUTH_FILE, "r");
206 fp = fopen(KEYSFILE_PATH, "r");
211 fseek(fp, 0, SEEK_SET);
212 fgetpos(fp, &fp_pos);
214 while(fgets(line_read,FILE_SIZE -1, fp) != NULL)
218 if(line_read[0] == '#')
220 fgetpos(fp, &fp_pos);
224 name = strchr(line_read, ' ');
230 if(strcmp(line_read,id) == 0)
233 return (1); /*(fp_pos);*/
237 fgetpos(fp, &fp_pos);
245 /* Validate agent name.
247 int OS_IsValidName(char *u_name)
251 /* We must have something in the name */
252 if(strlen(u_name) < 2 || strlen(u_name) > 128)
255 /* check if it contains any non-alphanumeric characters */
256 for(i = 0; i < strlen(u_name); i++)
258 if(!isalnum((int)u_name[i]) && (u_name[i] != '-') &&
259 (u_name[i] != '_') && (u_name[i] != '.'))
267 /* Is_Name (is valid name) */
268 int NameExist(char *u_name)
271 char line_read[FILE_SIZE +1];
272 line_read[FILE_SIZE] = '\0';
281 fp = fopen(AUTH_FILE, "r");
283 fp = fopen(KEYSFILE_PATH, "r");
289 fseek(fp, 0, SEEK_SET);
290 fgetpos(fp, &fp_pos);
293 while(fgets(line_read, FILE_SIZE-1, fp) != NULL)
297 if(line_read[0] == '#')
300 name = strchr(line_read, ' ');
311 ip = strchr(name, ' ');
315 if(strcmp(u_name, name) == 0)
322 fgetpos(fp, &fp_pos);
330 /* print available agents */
331 int print_agents(int print_status, int active_only, int csv_output)
335 char line_read[FILE_SIZE +1];
336 line_read[FILE_SIZE] = '\0';
338 fp = fopen(AUTH_FILE, "r");
342 fseek(fp, 0, SEEK_SET);
344 memset(line_read,'\0',FILE_SIZE);
346 while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
350 if(line_read[0] == '#')
353 name = strchr(line_read, ' ');
367 ip = strchr(name, ' ');
373 key = strchr(ip, ' ');
377 if(!total && !print_status)
378 printf(PRINT_AVAILABLE);
384 int agt_status = get_agent_status(name, ip);
385 if(active_only && (agt_status != GA_STATUS_ACTIVE))
392 printf("%s,%s,%s,%s,\n", line_read, name, ip,
393 print_agent_status(agt_status));
397 printf(PRINT_AGENT_STATUS, line_read, name, ip,
398 print_agent_status(agt_status));
403 printf(PRINT_AGENT, line_read, name, ip);
412 /* Only print agentless for non-active only searches */
413 if(!active_only && print_status)
421 printf("\nList of agentless devices:\n");
424 dirp = opendir(AGENTLESS_ENTRYDIR);
427 while ((dp = readdir(dirp)) != NULL)
429 if(strncmp(dp->d_name, ".", 1) == 0)
434 aip = strchr(dp->d_name, '@');
446 printf("na,%s,%s,agentless,\n", dp->d_name, aip);
450 printf(" ID: na, Name: %s, IP: %s, agentless\n",