Imported Upstream version 2.7
[ossec-hids.git] / src / addagent / validate.c
1 /* @(#) $Id: ./src/addagent/validate.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation
11  */
12
13
14
15 #include "manage_agents.h"
16 #include "os_crypto/md5/md5_op.h"
17
18 char *OS_AddNewAgent(char *name, char *ip, char *id, char *key)
19 {
20     int i = 0;
21     FILE *fp;
22     int rand1;
23     os_md5 md1;
24     os_md5 md2;
25     char str1[STR_SIZE +1];
26     char str2[STR_SIZE +1];
27     char *muname = NULL;
28     char *finals = NULL;
29
30     char nid[9];
31
32
33     #ifndef WIN32
34         #ifdef __OpenBSD__
35         srandomdev();
36         #else
37         srandom(time(0) + getpid() + getppid());
38         #endif
39     #else
40         srandom(time(0) + getpid());
41     #endif
42
43     rand1 = random();
44     muname = getuname();
45
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);
50
51
52     nid[8] = '\0';
53     if(id == NULL)
54     {
55         i = 1024;
56         snprintf(nid, 6, "%d", i);
57         while(IDExist(nid))
58         {
59             i++;
60             snprintf(nid, 6, "%d", i);
61             if(i >= 4000)
62             {
63                 return(NULL);
64             }
65         }
66         id = nid;
67     }
68
69     fp = fopen(KEYSFILE_PATH,"a");
70     if(!fp)
71     {
72         return(NULL);
73     }
74
75     os_calloc(2048, sizeof(char), finals);
76     if (ip == NULL){
77         snprintf(finals, 2048, "%s %s any %s%s",id, name, md1,md2);
78     } else {
79         snprintf(finals, 2048, "%s %s %s %s%s",id, name, ip, md1,md2);
80     }
81     fprintf(fp, "%s\n",finals);
82
83     fclose(fp);
84     return(finals);
85 }
86
87
88 int OS_IsValidID(char *id)
89 {
90     int id_len = 0;
91     int i = 0;
92
93     /* ID must not be null */
94     if(!id)
95       return(0);
96
97     id_len = strlen(id);
98
99     /* Check ID length, it should contain max. 8 characters */
100     if (id_len > 8)
101       return(0);
102
103     /* Check ID if it contains only numeric characters [0-9] */
104     for(i = 0; i < id_len; i++)
105     {
106       if(!(isdigit((int)id[i])))
107         return(0);
108     }
109
110     return(1);
111 }
112
113
114 /* Get full agent name (name + ip) of ID.
115  */
116 char *getFullnameById(char *id)
117 {
118     FILE *fp;
119     char line_read[FILE_SIZE +1];
120     line_read[FILE_SIZE] = '\0';
121
122     /* ID must not be null */
123     if(!id)
124         return(NULL);
125
126     fp = fopen(AUTH_FILE, "r");
127     if(!fp)
128         return(NULL);
129
130
131     while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
132     {
133         char *name;
134         char *ip;
135         char *tmp_str;
136
137         if(line_read[0] == '#')
138         {
139             continue;
140         }
141
142         name = strchr(line_read, ' ');
143         if(name)
144         {
145             *name = '\0';
146             /* Didn't match */
147             if(strcmp(line_read,id) != 0)
148             {
149                 continue;
150             }
151
152             name++;
153
154             /* Removed entry */
155             if(*name == '#')
156             {
157                 continue;
158             }
159
160             ip = strchr(name, ' ');
161             if(ip)
162             {
163                 *ip = '\0';
164                 ip++;
165
166                 /* Cleaning up ip */
167                 tmp_str = strchr(ip, ' ');
168                 if(tmp_str)
169                 {
170                     char *final_str;
171                     *tmp_str = '\0';
172                     tmp_str = strchr(ip, '/');
173                     if(tmp_str)
174                         *tmp_str = '\0';
175
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);
179
180                     fclose(fp);
181                     return(final_str);
182                 }
183             }
184         }
185     }
186
187     fclose(fp);
188     return(NULL);
189 }
190
191
192 /* ID Search (is valid ID) */
193 int IDExist(char *id)
194 {
195     FILE *fp;
196     char line_read[FILE_SIZE +1];
197     line_read[FILE_SIZE] = '\0';
198
199     /* ID must not be null */
200     if(!id)
201         return(0);
202
203     if(isChroot())
204       fp = fopen(AUTH_FILE, "r");
205     else
206       fp = fopen(KEYSFILE_PATH, "r");
207
208     if(!fp)
209         return(0);
210
211     fseek(fp, 0, SEEK_SET);
212     fgetpos(fp, &fp_pos);
213
214     while(fgets(line_read,FILE_SIZE -1, fp) != NULL)
215     {
216         char *name;
217
218         if(line_read[0] == '#')
219         {
220             fgetpos(fp, &fp_pos);
221             continue;
222         }
223
224         name = strchr(line_read, ' ');
225         if(name)
226         {
227             *name = '\0';
228             name++;
229
230             if(strcmp(line_read,id) == 0)
231             {
232                 fclose(fp);
233                 return (1); /*(fp_pos);*/
234             }
235         }
236
237         fgetpos(fp, &fp_pos);
238     }
239
240     fclose(fp);
241     return(0);
242 }
243
244
245 /* Validate agent name.
246  */
247 int OS_IsValidName(char *u_name)
248 {
249     int i = 0;
250
251     /* We must have something in the name */
252     if(strlen(u_name) < 2 || strlen(u_name) > 128)
253       return(0);
254
255     /* check if it contains any non-alphanumeric characters */
256     for(i = 0; i < strlen(u_name); i++)
257     {
258       if(!isalnum((int)u_name[i]) && (u_name[i] != '-') &&
259          (u_name[i] != '_') && (u_name[i] != '.'))
260         return(0);
261     }
262
263     return(1);
264 }
265
266
267 /* Is_Name (is valid name) */
268 int NameExist(char *u_name)
269 {
270     FILE *fp;
271     char line_read[FILE_SIZE +1];
272     line_read[FILE_SIZE] = '\0';
273
274     if((!u_name)||
275        (*u_name == '\0')||
276        (*u_name == '\r')||
277        (*u_name == '\n'))
278         return(0);
279
280     if(isChroot())
281       fp = fopen(AUTH_FILE, "r");
282     else
283       fp = fopen(KEYSFILE_PATH, "r");
284
285     if(!fp)
286         return(0);
287
288
289     fseek(fp, 0, SEEK_SET);
290     fgetpos(fp, &fp_pos);
291
292
293     while(fgets(line_read, FILE_SIZE-1, fp) != NULL)
294     {
295         char *name;
296
297         if(line_read[0] == '#')
298             continue;
299
300         name = strchr(line_read, ' ');
301         if(name)
302         {
303             char *ip;
304             name++;
305
306             if(*name == '#')
307             {
308                 continue;
309             }
310
311             ip = strchr(name, ' ');
312             if(ip)
313             {
314                 *ip = '\0';
315                 if(strcmp(u_name, name) == 0)
316                 {
317                     fclose(fp);
318                     return(1);
319                 }
320             }
321         }
322         fgetpos(fp, &fp_pos);
323     }
324
325     fclose(fp);
326     return(0);
327 }
328
329
330 /* print available agents */
331 int print_agents(int print_status, int active_only, int csv_output)
332 {
333     int total = 0;
334     FILE *fp;
335     char line_read[FILE_SIZE +1];
336     line_read[FILE_SIZE] = '\0';
337
338     fp = fopen(AUTH_FILE, "r");
339     if(!fp)
340         return(0);
341
342     fseek(fp, 0, SEEK_SET);
343
344     memset(line_read,'\0',FILE_SIZE);
345
346     while(fgets(line_read, FILE_SIZE -1, fp) != NULL)
347     {
348         char *name;
349
350         if(line_read[0] == '#')
351             continue;
352
353         name = strchr(line_read, ' ');
354         if(name)
355         {
356             char *ip;
357             *name = '\0';
358             name++;
359
360
361             /* Removed agent. */
362             if(*name == '#')
363             {
364                 continue;
365             }
366
367             ip = strchr(name, ' ');
368             if(ip)
369             {
370                 char *key;
371                 *ip = '\0';
372                 ip++;
373                 key = strchr(ip, ' ');
374                 if(key)
375                 {
376                     *key = '\0';
377                     if(!total && !print_status)
378                         printf(PRINT_AVAILABLE);
379                     total++;
380
381
382                     if(print_status)
383                     {
384                         int agt_status = get_agent_status(name, ip);
385                         if(active_only && (agt_status != GA_STATUS_ACTIVE))
386                         {
387                             continue;
388                         }
389
390                         if(csv_output)
391                         {
392                             printf("%s,%s,%s,%s,\n", line_read, name, ip,
393                                                   print_agent_status(agt_status));
394                         }
395                         else
396                         {
397                             printf(PRINT_AGENT_STATUS, line_read, name, ip,
398                                    print_agent_status(agt_status));
399                         }
400                     }
401                     else
402                     {
403                         printf(PRINT_AGENT, line_read, name, ip);
404                     }
405                 }
406
407             }
408         }
409     }
410
411
412     /* Only print agentless for non-active only searches */
413     if(!active_only && print_status)
414     {
415         char *aip = NULL;
416         DIR *dirp;
417         struct dirent *dp;
418
419         if(!csv_output)
420         {
421             printf("\nList of agentless devices:\n");
422         }
423
424         dirp = opendir(AGENTLESS_ENTRYDIR);
425         if(dirp)
426         {
427             while ((dp = readdir(dirp)) != NULL)
428             {
429                 if(strncmp(dp->d_name, ".", 1) == 0)
430                 {
431                     continue;
432                 }
433
434                 aip = strchr(dp->d_name, '@');
435                 if(aip)
436                 {
437                     aip++;
438                 }
439                 else
440                 {
441                     aip = "<na>";
442                 }
443
444                 if(csv_output)
445                 {
446                     printf("na,%s,%s,agentless,\n", dp->d_name, aip);
447                 }
448                 else
449                 {
450                     printf("   ID: na, Name: %s, IP: %s, agentless\n",
451                            dp->d_name, aip);
452                 }
453             }
454             closedir(dirp);
455         }
456     }
457
458     fclose(fp);
459     if(total)
460         return(1);
461
462     return(0);
463 }
464
465
466 /* EOF */