Imported Upstream version 2.7
[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 /** help **/
19 void helpmsg()
20 {
21     printf("\nOSSEC HIDS %s: Manage agents.\n", ARGV0);
22     printf("Available options:\n");
23     printf("\t-h          This help message.\n");
24     printf("\t-V          Display OSSEC version.\n");
25     printf("\t-l          List available agents.\n");
26     printf("\t-e <id>     Extracts key for an agent (Manager only).\n");
27     printf("\t-i <id>     Import authentication key (Agent only).\n");
28     printf("\t-f <file>   Bulk generate client keys from file. (Manager only).\n\n");
29     exit(1);
30 }
31
32
33 /* print banner */
34 void print_banner()
35 {
36     printf("\n");
37     printf(BANNER, __name, __version);
38
39     #ifdef CLIENT
40     printf(BANNER_CLIENT);
41     #else
42     printf(BANNER_OPT);
43     #endif
44
45     return;
46 }
47
48
49 /* Clean shutdown on kill */
50 void manage_shutdown()
51 {
52     /* Checking if restart message is necessary */
53     if(restart_necessary)
54     {
55         printf(MUST_RESTART);
56     }
57     else
58     {
59         printf("\n");
60     }
61     printf(EXIT);
62
63     exit(0);
64 }
65
66
67 /** main **/
68 int main(int argc, char **argv)
69 {
70     char *user_msg;
71
72     int c = 0, cmdlist = 0;
73     char *cmdexport = NULL;
74     char *cmdimport = NULL;
75     char *cmdbulk = NULL;
76
77     #ifndef WIN32
78     char *dir = DEFAULTDIR;
79     char *group = GROUPGLOBAL;
80     int gid;
81     #endif
82
83
84     /* Setting the name */
85     OS_SetName(ARGV0);
86
87
88     while((c = getopt(argc, argv, "Vhle:i:f:")) != -1){
89         switch(c){
90                 case 'V':
91                         print_version();
92                         break;
93             case 'h':
94                 helpmsg();
95                 break;
96             case 'd':
97                 nowDebug();
98                 break;
99             case 'e':
100                 #ifdef CLIENT
101                 ErrorExit("%s: You can't export keys on an agent", ARGV0);
102                 #endif
103                 if(!optarg)
104                     ErrorExit("%s: -e needs an argument",ARGV0);
105                 cmdexport = optarg;
106                 break;
107             case 'i':
108                 #ifndef CLIENT
109                 ErrorExit("%s: You can't import keys on the manager.", ARGV0);
110                 #endif
111                 if(!optarg)
112                     ErrorExit("%s: -i needs an argument",ARGV0);
113                 cmdimport = optarg;
114                 break;
115             case 'f':
116                 #ifdef CLIENT
117                 ErrorExit("%s: You can't bulk generate keys on an agent.", ARGV0);
118                 #endif
119                 if(!optarg)
120                     ErrorExit("%s: -f needs an argument",ARGV0);
121                 cmdbulk = optarg;
122                 printf("Bulk load file: %s\n", cmdbulk);
123                 break;
124             case 'l':
125                 cmdlist = 1;
126                 break;
127             default:
128                 helpmsg();
129                 break;
130         }
131
132     }
133
134
135
136     /* Getting currently time */
137     time1 = time(0);
138     restart_necessary = 0;
139
140
141     #ifndef WIN32
142     /* Getting the group name */
143     gid = Privsep_GetGroup(group);
144     if(gid < 0)
145     {
146             ErrorExit(USER_ERROR, ARGV0, "", group);
147     }
148
149
150     /* Setting the group */
151     if(Privsep_SetGroup(gid) < 0)
152     {
153             ErrorExit(SETGID_ERROR, ARGV0, group);
154     }
155
156
157     /* Chrooting to the default directory */
158     if(Privsep_Chroot(dir) < 0)
159     {
160         ErrorExit(CHROOT_ERROR, ARGV0, dir);
161     }
162
163
164     /* Inside chroot now */
165     nowChroot();
166
167
168     /* Starting signal handler */
169     StartSIG2(ARGV0, manage_shutdown);
170     #endif
171
172
173     if(cmdlist == 1)
174     {
175         list_agents(cmdlist);
176         exit(0);
177     }
178     else if(cmdimport)
179     {
180         k_import(cmdimport);
181         exit(0);
182     }
183     else if(cmdexport)
184     {
185         k_extract(cmdexport);
186         exit(0);
187     }
188     else if(cmdbulk)
189     {
190         k_bulkload(cmdbulk);
191         exit(0);
192     }
193
194
195
196     /* Little shell */
197     while(1)
198     {
199         int leave_s = 0;
200         print_banner();
201
202         /* Get ACTION from the environment. If ACTION is specified,
203          * we must set leave_s = 1 to ensure that the loop will end */
204         user_msg = getenv("OSSEC_ACTION");
205         if (user_msg == NULL) {
206           user_msg = read_from_user();
207         }
208         else{
209           leave_s = 1;
210         }
211
212         /* All the allowed actions */
213         switch(user_msg[0])
214         {
215             case 'A':
216             case 'a':
217                 add_agent();
218                 break;
219             case 'e':
220             case 'E':
221                 k_extract(NULL);
222                 break;
223             case 'i':
224             case 'I':
225                 k_import(NULL);
226                 break;
227             case 'l':
228             case 'L':
229                 list_agents(0);
230                 break;
231             case 'r':
232             case 'R':
233                 remove_agent();
234                 break;
235             case 'q':
236             case 'Q':
237                 leave_s = 1;
238                 break;
239                 case 'V':
240                         print_version();
241                         break;
242             default:
243                 printf("\n ** Invalid Action ** \n\n");
244                 break;
245         }
246
247         if(leave_s)
248         {
249             break;
250         }
251
252         continue;
253
254     }
255
256     /* Checking if restart message is necessary */
257     if(restart_necessary)
258     {
259         printf(MUST_RESTART);
260     }
261     else
262     {
263         printf("\n");
264     }
265     printf(EXIT);
266
267     return(0);
268 }
269
270
271 /* EOF */