344e4ee79cf799b269944a022b08b95bf0b4753a
[ossec-hids.git] / src / addagent / validate.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
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 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13
14 #include "manage_agents.h"
15
16
17 int OS_IsValidID(char *id)
18 {
19     int id_len = 0;
20     int i = 0;
21     
22     /* ID must not be null */ 
23     if(!id)
24       return(0);
25
26     id_len = strlen(id);
27
28     /* Check ID length, it should contain max. 5 characters */
29     if (id_len > 8)
30       return(0);
31
32     /* Check ID if it contains only numeric characters [0-9] */
33     for(i = 0; i < id_len; i++)
34     {
35       if(!(isdigit((int)id[i])))
36         return(0);
37     }
38     
39     return(1);
40 }
41
42
43 /* Get full agent name (name + ip) of ID.
44  */
45 char *getFullnameById(char *id)
46 {
47     FILE *fp;
48     char line_read[FILE_SIZE +1];
49     line_read[FILE_SIZE] = '\0';
50
51     /* ID must not be null */
52     if(!id)
53         return(NULL);
54
55     fp = fopen(AUTH_FILE, "r");
56     if(!fp)
57         return(NULL);
58
59
60     while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
61     {
62         char *name;
63         char *ip;
64         char *tmp_str;
65
66         if(line_read[0] == '#')
67         {
68             continue;
69         }
70
71         name = strchr(line_read, ' ');
72         if(name)
73         {
74             *name = '\0';
75             /* Didn't match */
76             if(strcmp(line_read,id) != 0)
77             {
78                 continue;
79             }
80
81             name++;
82
83             /* Removed entry */
84             if(*name == '#')
85             {
86                 continue;
87             }
88             
89             ip = strchr(name, ' ');
90             if(ip)
91             {
92                 *ip = '\0';
93                 ip++;
94
95                 /* Cleaning up ip */
96                 tmp_str = strchr(ip, ' ');
97                 if(tmp_str)
98                 {
99                     char *final_str;
100                     *tmp_str = '\0';
101                     tmp_str = strchr(ip, '/');
102                     if(tmp_str)
103                         *tmp_str = '\0';
104
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);
108
109                     fclose(fp);
110                     return(final_str);        
111                 }
112             }
113         }
114     }
115
116     fclose(fp);
117     return(NULL);
118 }
119
120
121 /* ID Search (is valid ID) */
122 int IDExist(char *id)
123 {
124     FILE *fp;
125     char line_read[FILE_SIZE +1];
126     line_read[FILE_SIZE] = '\0';
127    
128     /* ID must not be null */ 
129     if(!id)
130         return(0);
131
132     fp = fopen(AUTH_FILE, "r");
133     if(!fp)
134         return(0);
135         
136     fseek(fp, 0, SEEK_SET);
137     fgetpos(fp, &fp_pos);
138     
139     while(fgets(line_read,FILE_SIZE -1, fp) != NULL)
140     {
141         char *name;
142
143         if(line_read[0] == '#')
144         {
145             fgetpos(fp, &fp_pos);
146             continue;
147         }
148         
149         name = strchr(line_read, ' ');
150         if(name)
151         {
152             *name = '\0';
153             name++;
154
155             if(strcmp(line_read,id) == 0)
156             {
157                 fclose(fp);
158                 return (1); /*(fp_pos);*/
159             }
160         }
161
162         fgetpos(fp, &fp_pos);
163     }
164
165     fclose(fp);
166     return(0);
167 }
168
169
170 /* Validate agent name.
171  */
172 int OS_IsValidName(char *u_name)
173 {
174     int i = 0;
175
176     /* We must have something in the name */
177     if(strlen(u_name) < 2 || strlen(u_name) > 128)
178       return(0);
179
180     /* check if it contains any non-alphanumeric characters */
181     for(i = 0; i < strlen(u_name); i++)
182     {
183       if(!isalnum((int)u_name[i]) && (u_name[i] != '-') && 
184          (u_name[i] != '_') && (u_name[i] != '.'))
185         return(0);
186     }
187
188     return(1);
189 }
190
191
192 /* Is_Name (is valid name) */
193 int NameExist(char *u_name)
194 {
195     FILE *fp;
196     char line_read[FILE_SIZE +1];
197     line_read[FILE_SIZE] = '\0';
198
199     if((!u_name)||
200        (*u_name == '\0')||
201        (*u_name == '\r')||
202        (*u_name == '\n'))
203         return(0);
204
205     fp = fopen(AUTH_FILE, "r");
206     if(!fp)
207         return(0);
208
209
210     fseek(fp, 0, SEEK_SET);
211     fgetpos(fp, &fp_pos);
212
213
214     while(fgets(line_read, FILE_SIZE-1, fp) != NULL)
215     {
216         char *name;
217
218         if(line_read[0] == '#')
219             continue;
220
221         name = strchr(line_read, ' ');
222         if(name)
223         {
224             char *ip;
225             name++;
226
227             if(*name == '#')
228             {
229                 continue;
230             }
231             
232             ip = strchr(name, ' ');
233             if(ip)
234             {
235                 *ip = '\0';
236                 if(strcmp(u_name, name) == 0)
237                 {
238                     fclose(fp);
239                     return(1);
240                 }
241             }
242         }
243         fgetpos(fp, &fp_pos);
244     }
245
246     fclose(fp);
247     return(0);
248 }
249
250
251 /* print available agents */
252 int print_agents(int print_status, int active_only, int csv_output)
253 {
254     int total = 0;
255     FILE *fp;
256     char line_read[FILE_SIZE +1];
257     line_read[FILE_SIZE] = '\0';
258
259     fp = fopen(AUTH_FILE, "r");
260     if(!fp)
261         return(0);
262
263     fseek(fp, 0, SEEK_SET);
264     
265     memset(line_read,'\0',FILE_SIZE);
266     
267     while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
268     {
269         char *name;
270
271         if(line_read[0] == '#')
272             continue;
273             
274         name = strchr(line_read, ' ');
275         if(name)
276         {
277             char *ip;
278             *name = '\0';
279             name++;
280
281
282             /* Removed agent. */
283             if(*name == '#')
284             {
285                 continue;
286             }
287             
288             ip = strchr(name, ' ');
289             if(ip)
290             {
291                 char *key;
292                 *ip = '\0';
293                 ip++;
294                 key = strchr(ip, ' ');
295                 if(key)
296                 {
297                     *key = '\0';
298                     if(!total && !print_status)
299                         printf(PRINT_AVAILABLE);
300                     total++;
301
302                     
303                     if(print_status)
304                     {
305                         int agt_status = get_agent_status(name, ip);
306                         if(active_only && (agt_status != GA_STATUS_ACTIVE))
307                         {
308                             continue;
309                         }
310             
311                         if(csv_output)
312                         {
313                             printf("%s,%s,%s,%s,\n", line_read, name, ip, 
314                                                   print_agent_status(agt_status));  
315                         }
316                         else
317                         {
318                             printf(PRINT_AGENT_STATUS, line_read, name, ip, 
319                                    print_agent_status(agt_status));
320                         }
321                     }
322                     else
323                     {
324                         printf(PRINT_AGENT, line_read, name, ip);
325                     }
326                 }
327                 
328             }
329         }
330     }
331
332
333     /* Only print agentless for non-active only searches */
334     if(!active_only && print_status)
335     {
336         char *aip = NULL;
337         DIR *dirp;
338         struct dirent *dp;
339         
340         if(!csv_output)
341         {
342             printf("\nList of agentless devices:\n");
343         }
344
345         dirp = opendir(AGENTLESS_ENTRYDIR);
346         if(dirp)
347         {
348             while ((dp = readdir(dirp)) != NULL)
349             {
350                 if(strncmp(dp->d_name, ".", 1) == 0)
351                 {
352                     continue;
353                 }
354
355                 aip = strchr(dp->d_name, '@');
356                 if(aip)
357                 {
358                     aip++;
359                 }
360                 else
361                 {
362                     aip = "<na>";
363                 }
364
365                 if(csv_output)
366                 {
367                     printf("na,%s,%s,agentless,\n", dp->d_name, aip);
368                 }
369                 else
370                 {
371                     printf("   ID: na, Name: %s, IP: %s, agentless\n",
372                            dp->d_name, aip);
373                 }
374             }
375             closedir(dirp);
376         }
377     }
378
379     fclose(fp);
380     if(total)
381         return(1);
382     
383     return(0);    
384 }
385
386
387 /* EOF */