32a91eca3f854076413ed22fa7b49de0c42c99c8
[ossec-hids.git] / src / addagent / main.c
1 /* @(#) $Id: ./src/addagent/main.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 <stdlib.h>
17
18 #if defined(__MINGW32__)
19 static int setenv(const char * name, const char * val, int overwrite) {
20     int len = strlen(name) + strlen(val) + 2; 
21     char * str = (char *)malloc(len); 
22     snprintf(str, len, "%s=%s", name, val); 
23     putenv(str);
24     return 0; 
25 }
26 #endif 
27
28 /** help **/
29 void helpmsg()
30 {
31     printf("\nOSSEC HIDS %s: Manage agents.\n", ARGV0);
32     printf("Available options:\n");
33     printf("\t-h          This help message.\n");
34     printf("\t-V          Display OSSEC version.\n");
35     printf("\t-l          List available agents.\n");
36     printf("\t-e <id>     Extracts key for an agent (Manager only).\n");
37     printf("\t-r <id>     Remove an agent. (Manager only).\n");
38     printf("\t-i <id>     Import authentication key (Agent only).\n");
39     printf("\t-f <file>   Bulk generate client keys from file. (Manager only).\n");
40     printf("\t            <file> contains lines in IP,NAME format.\n\n");
41     exit(1);
42 }
43
44
45 /* print banner */
46 void print_banner()
47 {
48     printf("\n");
49     printf(BANNER, __ossec_name, __version);
50
51     #ifdef CLIENT
52     printf(BANNER_CLIENT);
53     #else
54     printf(BANNER_OPT);
55     #endif
56
57     return;
58 }
59
60
61 /* Clean shutdown on kill */
62 void manage_shutdown()
63 {
64     /* Checking if restart message is necessary */
65     if(restart_necessary)
66     {
67         printf(MUST_RESTART);
68     }
69     else
70     {
71         printf("\n");
72     }
73     printf(EXIT);
74
75     exit(0);
76 }
77
78
79 /** main **/
80 int main(int argc, char **argv)
81 {
82     char *user_msg;
83
84     int c = 0, cmdlist = 0;
85     char *cmdexport = NULL;
86     char *cmdimport = NULL;
87     char *cmdbulk = NULL;
88
89     #ifndef WIN32
90     char *dir = DEFAULTDIR;
91     char *group = GROUPGLOBAL;
92     int gid;
93     #else
94     FILE *fp;
95     TCHAR path[2048];
96     DWORD last_error;
97     int ret;
98     #endif
99
100
101     /* Setting the name */
102     OS_SetName(ARGV0);
103
104
105     while((c = getopt(argc, argv, "Vhle:r:i:f:")) != -1){
106         switch(c){
107                 case 'V':
108                         print_version();
109                         break;
110             case 'h':
111                 helpmsg();
112                 break;
113             case 'd':
114                 nowDebug();
115                 break;
116             case 'e':
117                 #ifdef CLIENT
118                 ErrorExit("%s: You can't export keys on an agent", ARGV0);
119                 #endif
120                 if(!optarg)
121                     ErrorExit("%s: -e needs an argument",ARGV0);
122                 cmdexport = optarg;
123                 break;
124             case 'r':
125                 #ifdef CLIENT
126                 ErrorExit("%s: You can't remove keys on an agent", ARGV0);
127                 #endif
128                 if(!optarg)
129                     ErrorExit("%s: -r needs an argument",ARGV0);
130                     
131                 /* Use environment variables already available to remove_agent() */             
132                 setenv("OSSEC_ACTION", "r", 1);
133                 setenv("OSSEC_AGENT_ID", optarg, 1);
134                 setenv("OSSEC_ACTION_CONFIRMED", "y", 1);
135                 break;
136             case 'i':
137                 #ifndef CLIENT
138                 ErrorExit("%s: You can't import keys on the manager.", ARGV0);
139                 #endif
140                 if(!optarg)
141                     ErrorExit("%s: -i needs an argument",ARGV0);
142                 cmdimport = optarg;
143                 break;
144             case 'f':
145                 #ifdef CLIENT
146                 ErrorExit("%s: You can't bulk generate keys on an agent.", ARGV0);
147                 #endif
148                 if(!optarg)
149                     ErrorExit("%s: -f needs an argument",ARGV0);
150                 cmdbulk = optarg;
151                 printf("Bulk load file: %s\n", cmdbulk);
152                 break;
153             case 'l':
154                 cmdlist = 1;
155                 break;
156             default:
157                 helpmsg();
158                 break;
159         }
160
161     }
162
163
164
165     /* Getting currently time */
166     time1 = time(0);
167     restart_necessary = 0;
168
169
170     #ifndef WIN32
171     /* Getting the group name */
172     gid = Privsep_GetGroup(group);
173     if(gid < 0)
174     {
175             ErrorExit(USER_ERROR, ARGV0, "", group);
176     }
177
178
179     /* Setting the group */
180     if(Privsep_SetGroup(gid) < 0)
181     {
182             ErrorExit(SETGID_ERROR, ARGV0, group);
183     }
184
185
186     /* Chrooting to the default directory */
187     if(Privsep_Chroot(dir) < 0)
188     {
189         ErrorExit(CHROOT_ERROR, ARGV0, dir);
190     }
191
192
193     /* Inside chroot now */
194     nowChroot();
195
196
197     /* Starting signal handler */
198     StartSIG2(ARGV0, manage_shutdown);
199
200     #else
201
202     /* Get full path to the directory this
203      * executable lives in
204      */
205     ret = GetModuleFileName(NULL, path, sizeof(path));
206
207     /* check for errors */
208     if(!ret)
209     {
210         ErrorExit(GMF_ERROR);
211     }
212
213     /* Get last error */
214     last_error = GetLastError();
215
216     /* Look for errors */
217     if(last_error != ERROR_SUCCESS)
218     {
219         if(last_error == ERROR_INSUFFICIENT_BUFFER)
220         {
221             ErrorExit(GMF_BUFF_ERROR, ret, sizeof(path));
222         }
223         else
224         {
225             ErrorExit(GMF_UNKN_ERROR, last_error);
226         }
227     }
228
229     /* Remove file name from path */
230     PathRemoveFileSpec(path);
231
232     /* Move to correct directory */
233     if(chdir(path))
234     {
235         ErrorExit(CHDIR_ERROR, path);
236     }
237
238     /* Check permissions */
239     fp = fopen(OSSECCONF, "r");
240     if(fp)
241     {
242         fclose(fp);
243     }
244     else
245     {
246         ErrorExit(CONF_ERROR, OSSECCONF);
247     }
248
249     #endif
250
251     if(cmdlist == 1)
252     {
253         list_agents(cmdlist);
254         exit(0);
255     }
256     else if(cmdimport)
257     {
258         k_import(cmdimport);
259         exit(0);
260     }
261     else if(cmdexport)
262     {
263         k_extract(cmdexport);
264         exit(0);
265     }
266     else if(cmdbulk)
267     {
268         k_bulkload(cmdbulk);
269         exit(0);
270     }
271
272
273
274     /* Little shell */
275     while(1)
276     {
277         int leave_s = 0;
278         print_banner();
279
280         /* Get ACTION from the environment. If ACTION is specified,
281          * we must set leave_s = 1 to ensure that the loop will end */
282         user_msg = getenv("OSSEC_ACTION");
283         if (user_msg == NULL) {
284           user_msg = read_from_user();
285         }
286         else{
287           leave_s = 1;
288         }
289
290         /* All the allowed actions */
291         switch(user_msg[0])
292         {
293             case 'A':
294             case 'a':
295                 add_agent();
296                 break;
297             case 'e':
298             case 'E':
299                 k_extract(NULL);
300                 break;
301             case 'i':
302             case 'I':
303                 k_import(NULL);
304                 break;
305             case 'l':
306             case 'L':
307                 list_agents(0);
308                 break;
309             case 'r':
310             case 'R':
311                 remove_agent();
312                 break;
313             case 'q':
314             case 'Q':
315                 leave_s = 1;
316                 break;
317                 case 'V':
318                         print_version();
319                         break;
320             default:
321                 printf("\n ** Invalid Action ** \n\n");
322                 break;
323         }
324
325         if(leave_s)
326         {
327             break;
328         }
329
330         continue;
331
332     }
333
334     /* Checking if restart message is necessary */
335     if(restart_necessary)
336     {
337         printf(MUST_RESTART);
338     }
339     else
340     {
341         printf("\n");
342     }
343     printf(EXIT);
344
345     return(0);
346 }
347
348
349 /* EOF */