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