1 /* @(#) $Id: validate.c,v 1.19 2009/06/24 17:06:20 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 "manage_agents.h"
17 int OS_IsValidID(char *id)
22 /* ID must not be null */
28 /* Check ID length, it should contain max. 5 characters */
32 /* Check ID if it contains only numeric characters [0-9] */
33 for(i = 0; i < id_len; i++)
35 if(!(isdigit((int)id[i])))
43 /* Get full agent name (name + ip) of ID.
45 char *getFullnameById(char *id)
48 char line_read[FILE_SIZE +1];
49 line_read[FILE_SIZE] = '\0';
51 /* ID must not be null */
55 fp = fopen(AUTH_FILE, "r");
60 while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
66 if(line_read[0] == '#')
71 name = strchr(line_read, ' ');
76 if(strcmp(line_read,id) != 0)
89 ip = strchr(name, ' ');
96 tmp_str = strchr(ip, ' ');
101 tmp_str = strchr(ip, '/');
105 /* If we reached here, we found the IP and name */
106 os_calloc(1, FILE_SIZE, final_str);
107 snprintf(final_str, FILE_SIZE -1, "%s-%s", name, ip);
121 /* ID Search (is valid ID) */
122 int IDExist(char *id)
125 char line_read[FILE_SIZE +1];
126 line_read[FILE_SIZE] = '\0';
128 /* ID must not be null */
132 fp = fopen(AUTH_FILE, "r");
136 fseek(fp, 0, SEEK_SET);
137 fgetpos(fp, &fp_pos);
139 while(fgets(line_read,FILE_SIZE -1, fp) != NULL)
143 if(line_read[0] == '#')
145 fgetpos(fp, &fp_pos);
149 name = strchr(line_read, ' ');
155 if(strcmp(line_read,id) == 0)
158 return (1); /*(fp_pos);*/
162 fgetpos(fp, &fp_pos);
170 /* Validate agent name.
172 int OS_IsValidName(char *u_name)
176 /* We must have something in the name */
177 if(strlen(u_name) < 2 || strlen(u_name) > 128)
180 /* check if it contains any non-alphanumeric characters */
181 for(i = 0; i < strlen(u_name); i++)
183 if(!isalnum((int)u_name[i]) && (u_name[i] != '-') &&
184 (u_name[i] != '_') && (u_name[i] != '.'))
192 /* Is_Name (is valid name) */
193 int NameExist(char *u_name)
196 char line_read[FILE_SIZE +1];
197 line_read[FILE_SIZE] = '\0';
205 fp = fopen(AUTH_FILE, "r");
210 fseek(fp, 0, SEEK_SET);
211 fgetpos(fp, &fp_pos);
214 while(fgets(line_read, FILE_SIZE-1, fp) != NULL)
218 if(line_read[0] == '#')
221 name = strchr(line_read, ' ');
232 ip = strchr(name, ' ');
236 if(strcmp(u_name, name) == 0)
243 fgetpos(fp, &fp_pos);
251 /* print available agents */
252 int print_agents(int print_status, int active_only, int csv_output)
256 char line_read[FILE_SIZE +1];
257 line_read[FILE_SIZE] = '\0';
259 fp = fopen(AUTH_FILE, "r");
263 fseek(fp, 0, SEEK_SET);
265 memset(line_read,'\0',FILE_SIZE);
267 while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
271 if(line_read[0] == '#')
274 name = strchr(line_read, ' ');
288 ip = strchr(name, ' ');
294 key = strchr(ip, ' ');
298 if(!total && !print_status)
299 printf(PRINT_AVAILABLE);
305 int agt_status = get_agent_status(name, ip);
306 if(active_only && (agt_status != GA_STATUS_ACTIVE))
313 printf("%s,%s,%s,%s,\n", line_read, name, ip,
314 print_agent_status(agt_status));
318 printf(PRINT_AGENT_STATUS, line_read, name, ip,
319 print_agent_status(agt_status));
324 printf(PRINT_AGENT, line_read, name, ip);
333 /* Only print agentless for non-active only searches */
334 if(!active_only && print_status)
342 printf("\nList of agentless devices:\n");
345 dirp = opendir(AGENTLESS_ENTRYDIR);
348 while ((dp = readdir(dirp)) != NULL)
350 if(strncmp(dp->d_name, ".", 1) == 0)
355 aip = strchr(dp->d_name, '@');
367 printf("na,%s,%s,agentless,\n", dp->d_name, aip);
371 printf(" ID: na, Name: %s, IP: %s, agentless\n",