Imported Upstream version 2.5.1
[ossec-hids.git] / src / addagent / manage_agents.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  * License details at the LICENSE file included with OSSEC or 
12  * online at: http://www.ossec.net/en/licensing.html
13  */
14
15
16 /* Manage agents tool
17  * Add/extract and remove agents from a server.
18  */
19
20
21 #include "manage_agents.h"
22 #include "os_crypto/md5/md5_op.h"
23
24
25
26 /* Global internal variables */
27
28
29
30 /* chomp: remove spaces, new lines, etc from a string */
31 char *chomp(char *str)
32 {
33     char *tmp_str;
34     int size = 0;
35
36     /* Removing spaces from the beginning */
37     while(*str == ' ' || *str == '\t')
38         str++;
39     
40     
41     /* Removing any trailing new lines or \r */
42     do
43     {
44         tmp_str = strchr(str, '\n');
45         if(tmp_str)
46         {
47             *tmp_str = '\0';
48             continue;
49         }
50
51         tmp_str = strchr(str, '\r');
52         if(tmp_str)
53         {
54             *tmp_str = '\0';
55         }
56     }while(tmp_str != NULL);
57
58     
59     /* Removing spaces at the end of the string */
60     tmp_str = str;
61     size = strlen(str)-1;
62     
63     while((size >= 0) && (tmp_str[size] == ' ' || tmp_str[size] == '\t'))
64     {
65         tmp_str[size] = '\0';
66         size--;
67     }
68     
69     return(str);
70 }
71
72
73
74 /* Add an agent */
75 int add_agent()
76 {
77     int i = 1;
78     FILE *fp;
79     char str1[STR_SIZE +1];
80     char str2[STR_SIZE +1];
81     
82     os_md5 md1;
83     os_md5 md2;
84     
85     char *user_input;
86     char *_name;
87     char *_id;
88     char *_ip;
89
90     char name[FILE_SIZE +1];
91     char id[FILE_SIZE +1];
92     char ip[FILE_SIZE +1];
93     os_ip *c_ip;
94
95
96     /* Checking if we can open the auth_file */
97     fp = fopen(AUTH_FILE,"a");
98     if(!fp)
99     {
100         ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
101     }
102     fclose(fp);
103
104
105     /* Allocating for c_ip */
106     os_calloc(1, sizeof(os_ip), c_ip);
107     
108     
109     #ifndef WIN32
110     chmod(AUTH_FILE, 0440);
111     #endif
112     
113     /* Setting time 2 */
114     time2 = time(0);
115
116     
117     /* Source is time1+ time2 +pid + ppid */
118     #ifndef WIN32
119         #ifdef __OpenBSD__
120         srandomdev();
121         #else
122         srandom(time2 + time1 + getpid() + getppid());
123         #endif
124     #else
125     srandom(time2 + time1 + getpid());
126     #endif
127
128     rand1 = random();
129
130     
131     /* Zeroing strings */
132     memset(str1,'\0', STR_SIZE +1);
133     memset(str2,'\0', STR_SIZE +1);
134
135
136     printf(ADD_NEW);
137
138     
139     /* Getting the name */
140     memset(name, '\0', FILE_SIZE +1);
141
142     do
143     {
144         printf(ADD_NAME);
145         fflush(stdout);
146         _name = read_from_user();
147
148         if(strcmp(_name, QUIT) == 0)
149             return(0);
150
151         strncpy(name, _name, FILE_SIZE -1);
152
153         /* check the name */
154         if(!OS_IsValidName(name))
155             printf(INVALID_NAME,name);
156
157         /* Search for name  -- no duplicates */
158         if(NameExist(name))
159             printf(ADD_ERROR_NAME, name);
160
161     } while(NameExist(name) || !OS_IsValidName(name));
162
163
164     /* Getting IP */
165     memset(ip, '\0', FILE_SIZE +1);
166
167     do
168     {
169       printf(ADD_IP);
170       fflush(stdout);
171     
172       _ip = read_from_user();
173       
174       /* quit */
175       if(strcmp(_ip, QUIT) == 0)
176           return(0);
177                               
178       strncpy(ip, _ip, FILE_SIZE -1);
179       
180       if(!OS_IsValidIP(ip, c_ip))
181       {
182           printf(IP_ERROR, ip);
183           _ip = NULL;
184       }
185
186     } while(!_ip);
187    
188     
189     do
190     {
191         /* Default ID */
192         i = 1024;
193         snprintf(id, 8, "%03d", i);
194         while(!IDExist(id))
195         {
196             i--;
197             snprintf(id, 8, "%03d", i);
198
199             /* No key present, use id 0 */
200             if(i <= 0)
201             {
202                 i = 0;
203                 break;
204             }
205         }
206         snprintf(id, 8, "%03d", i+1);
207
208         /* Getting ID */
209         printf(ADD_ID, id);
210         fflush(stdout);
211
212         _id = read_from_user();
213
214
215
216         /* quit */
217         if(strcmp(_id, QUIT) == 0)
218             return(0);
219
220
221         if(_id[0] != '\0')
222         {
223             strncpy(id, _id, FILE_SIZE -1);
224         }
225
226         if(!OS_IsValidID(id))
227             printf(INVALID_ID, id);
228
229         /* Search for ID KEY  -- no duplicates */
230         if(IDExist(id))
231             printf(ADD_ERROR_ID, id);
232
233     } while(IDExist(id) || !OS_IsValidID(id));
234     
235     
236
237     printf(AGENT_INFO, id, name, ip);
238     fflush(stdout);
239
240     do
241     {
242       printf(ADD_CONFIRM);
243       user_input = read_from_user();
244    
245       /* If user accepts to add */ 
246       if(user_input[0] == 'y' || user_input[0] == 'Y')
247       {
248         time3 = time(0);
249         rand2 = random();
250
251         fp = fopen(AUTH_FILE,"a");
252         if(!fp)
253         {
254             ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE);
255         }
256         #ifndef WIN32
257         chmod(AUTH_FILE, 0440);
258         #endif
259                 
260         
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
266          */
267         
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);
270
271         OS_MD5_Str(str1, md1);
272         OS_MD5_Str(str2, md2);
273
274         snprintf(str1, STR_SIZE, "%s%d%d%d",md1,(int)getpid(), (int)random(), 
275                                             time3);
276         OS_MD5_Str(str1, md1);
277
278         fprintf(fp,"%s %s %s %s%s\n",id, name, c_ip->ip, md1,md2);
279
280         fclose(fp);
281
282         printf(AGENT_ADD);
283         restart_necessary = 1;
284         break;
285       }
286       else if(user_input[0] == 'n' || user_input[0] == 'N')
287       {
288         printf(ADD_NOT);
289         break;
290       }
291
292     } while(1);
293
294     return(0);
295 }
296
297
298 /* remove an agent */
299 int remove_agent()
300 {
301     FILE *fp;
302     char *user_input;
303     char u_id[FILE_SIZE +1];
304     
305     u_id[FILE_SIZE] = '\0';
306
307     if(!print_agents(0, 0, 0))
308     {
309         printf(NO_AGENT);
310         return(0);
311     }
312
313     do
314     {
315       printf(REMOVE_ID);
316       fflush(stdout);
317
318       user_input = read_from_user();
319
320       if(strcmp(user_input, QUIT) == 0)
321           return(0);
322
323       strncpy(u_id, user_input, FILE_SIZE);
324
325       if(!IDExist(user_input))
326       {
327         printf(NO_ID, user_input);
328       }
329     } while(!IDExist(user_input));
330     
331     do
332     {
333         printf(REMOVE_CONFIRM);
334         fflush(stdout);
335
336         user_input = read_from_user();
337
338         /* If user confirm */
339         if(user_input[0] == 'y' || user_input[0] == 'Y')
340         {
341             /* Getting full agent name */
342             char *full_name = getFullnameById(u_id);
343             if(!full_name)
344             {
345                 ErrorExit(MEM_ERROR, ARGV0);
346             }
347             
348             fp = fopen(AUTH_FILE, "r+");
349             if(!fp)
350             {
351                 free(full_name);
352                 ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
353             }
354             #ifndef WIN32
355             chmod(AUTH_FILE, 0440);
356             #endif
357
358
359             /* Removing the agent, but keeping the id. */
360             fsetpos(fp, &fp_pos);
361             fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id);
362
363             fclose(fp);
364
365
366             /* Remove counter for id */
367             delete_agentinfo(full_name); 
368             OS_RemoveCounter(u_id);
369             free(full_name);
370             full_name = NULL;
371
372
373             printf(REMOVE_DONE, u_id);
374             restart_necessary = 1;
375             break;
376         }
377         else if(user_input[0] == 'n' || user_input[0] == 'N')
378         {
379             printf(REMOVE_NOT);
380             break;
381         }
382
383     } while(1);
384
385     return(0);
386 }
387
388
389 int list_agents(int cmdlist)
390 {
391     if(!print_agents(0, 0, 0))
392         printf(NO_AGENT);
393
394     printf("\n");
395     if(!cmdlist)
396     {
397         printf(PRESS_ENTER);
398         read_from_user();
399     }
400
401     return(0);
402
403 }
404
405 /* EOF */