fff7a557b67a555525ae0a05a011ab18dee258bd
[ossec-hids.git] / src / util / rootcheck_control.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right 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 #include "addagent/manage_agents.h"
14 #include "sec.h"
15
16
17 #undef ARGV0
18 #define ARGV0 "rootcheck_control"
19
20
21 /** help **/
22 void helpmsg()
23 {
24     printf("\nOSSEC HIDS %s: Manages the policy and auditing database.\n", 
25            ARGV0);
26     printf("Available options:\n");
27     printf("\t-h          This help message.\n");
28     printf("\t-l          List available (active or not) agents.\n");
29     printf("\t-lc         List only active agents.\n");
30     printf("\t-u <id>     Updates (clear) the database for the agent.\n");
31     printf("\t-u all      Updates (clear) the database for all agents.\n");
32     printf("\t-i <id>     Prints database for the agent.\n");
33     printf("\t-r          Used with -i, prints all the resolved issues.\n");
34     printf("\t-q          Used with -i, prints all the outstanding issues.\n");
35     printf("\t-L          Used with -i, prints the last scan.\n");
36     printf("\t-s          Changes the output to CSV (comma delimited).\n");
37     exit(1);
38 }
39
40
41 /** main **/
42 int main(int argc, char **argv)
43 {
44     char *dir = DEFAULTDIR;
45     char *group = GROUPGLOBAL;
46     char *user = USER;
47     char *agent_id = NULL;
48
49     int gid = 0;
50     int uid = 0;
51     int c = 0, info_agent = 0, update_rootcheck = 0,
52                list_agents = 0, show_last = 0,
53                resolved_only = 0;
54     int active_only = 0, csv_output = 0;
55
56     char shost[512];
57     
58     
59     
60     /* Setting the name */
61     OS_SetName(ARGV0);
62         
63     
64     /* user arguments */
65     if(argc < 2)
66     {
67         helpmsg();
68     }
69
70
71     while((c = getopt(argc, argv, "VhqrDdLlcsu:i:")) != -1)
72     {
73         switch(c){
74             case 'V':
75                 print_version();
76                 break;
77             case 'h':
78                 helpmsg();
79                 break;
80             case 'D':
81                 nowDebug();
82                 break;
83             case 'l':
84                 list_agents++;
85                 break;
86             case 's':
87                 csv_output = 1;    
88                 break;
89             case 'c':
90                 active_only++;
91                 break;    
92             case 'r':
93                 resolved_only = 1;
94                 break;    
95             case 'q':
96                 resolved_only = 2;
97                 break;    
98             case 'L':
99                 show_last = 1;
100                 break;
101             case 'i':
102                 info_agent++;
103                 if(!optarg)
104                 {
105                     merror("%s: -u needs an argument",ARGV0);
106                     helpmsg();
107                 }
108                 agent_id = optarg;
109                 break;
110             case 'u':
111                 if(!optarg)
112                 {
113                     merror("%s: -u needs an argument",ARGV0);
114                     helpmsg();
115                 }
116                 agent_id = optarg;
117                 update_rootcheck = 1;
118                 break;
119             default:
120                 helpmsg();
121                 break;
122         }
123
124     }
125     
126     
127     /* Getting the group name */
128     gid = Privsep_GetGroup(group);
129     uid = Privsep_GetUser(user);
130     if(gid < 0)
131     {
132             ErrorExit(USER_ERROR, ARGV0, user, group);
133     }
134         
135     
136     /* Setting the group */
137     if(Privsep_SetGroup(gid) < 0)
138     {
139             ErrorExit(SETGID_ERROR,ARGV0, group);
140     }
141     
142     
143     /* Chrooting to the default directory */
144     if(Privsep_Chroot(dir) < 0)
145     {
146         ErrorExit(CHROOT_ERROR, ARGV0, dir);
147     }
148
149
150     /* Inside chroot now */
151     nowChroot();
152  
153
154     /* Setting the user */
155     if(Privsep_SetUser(uid) < 0)
156     {
157         ErrorExit(SETUID_ERROR, ARGV0, user);
158     }
159
160
161
162     /* Getting servers hostname */
163     memset(shost, '\0', 512);
164     if(gethostname(shost, 512 -1) != 0)
165     {
166         strncpy(shost, "localhost", 32);
167         return(0);
168     }
169
170
171     
172     /* Listing available agents. */
173     if(list_agents)
174     {
175         if(!csv_output)
176         {
177             printf("\nOSSEC HIDS %s. List of available agents:", 
178                     ARGV0);
179             printf("\n   ID: 000, Name: %s (server), IP: 127.0.0.1, "
180                    "Active/Local\n", shost);
181         }
182         else
183         {
184             printf("000,%s (server),127.0.0.1,Active/Local,\n", shost);
185         }
186         print_agents(1, active_only, csv_output);
187         printf("\n");
188         exit(0);
189     }
190     
191
192
193     /* Update rootcheck database. */
194     if(update_rootcheck)
195     {
196         /* Cleaning all agents (and server) db. */
197         if(strcmp(agent_id, "all") == 0)
198         {
199             DIR *sys_dir;
200             struct dirent *entry;
201
202             sys_dir = opendir(ROOTCHECK_DIR);
203             if(!sys_dir)
204             {
205                 ErrorExit("%s: Unable to open: '%s'", ARGV0, ROOTCHECK_DIR);
206             }
207
208             while((entry = readdir(sys_dir)) != NULL)
209             {
210                 FILE *fp;
211                 char full_path[OS_MAXSTR +1];
212
213                 /* Do not even attempt to delete . and .. :) */
214                 if((strcmp(entry->d_name,".") == 0)||
215                    (strcmp(entry->d_name,"..") == 0))
216                 {
217                     continue;
218                 }
219
220                 snprintf(full_path, OS_MAXSTR,"%s/%s", ROOTCHECK_DIR, 
221                          entry->d_name);
222
223                 fp = fopen(full_path, "w");
224                 if(fp)
225                 {
226                     fclose(fp);
227                 }
228                 if(entry->d_name[0] == '.')
229                 {
230                     unlink(full_path);
231                 }
232             }
233
234             closedir(sys_dir);
235             printf("\n** Policy and auditing database updated.\n\n");
236             exit(0);
237         }
238
239         else if((strcmp(agent_id, "000") == 0) || 
240                 (strcmp(agent_id, "local") == 0))
241         {
242             char final_dir[1024];
243             FILE *fp;
244             snprintf(final_dir, 1020, "/%s/rootcheck", ROOTCHECK_DIR);
245
246             fp = fopen(final_dir, "w");
247             if(fp)
248             {
249                 fclose(fp);
250             }
251             unlink(final_dir);
252             printf("\n** Policy and auditing database updated.\n\n");
253             exit(0);
254         }
255
256         /* Database from remote agents. */
257         else
258         {
259             int i;
260             keystore keys;
261
262             OS_ReadKeys(&keys);
263
264             i = OS_IsAllowedID(&keys, agent_id);
265             if(i < 0)
266             {
267                 printf("\n** Invalid agent id '%s'.\n", agent_id);
268                 helpmsg();
269             }
270
271             /* Deleting syscheck */
272             delete_rootcheck(keys.keyentries[i]->name,
273                              keys.keyentries[i]->ip->ip, 0);
274
275             printf("\n** Policy and auditing database updated.\n\n");
276             exit(0);
277         }
278     }
279
280     
281     /* Printing information from an agent. */
282     if(info_agent)
283     {
284         int i;
285         char final_ip[128 +1];
286         char final_mask[128 +1];
287         keystore keys;
288
289
290         if((strcmp(agent_id, "000") == 0) ||
291            (strcmp(agent_id, "local") == 0))
292         {
293             if(!csv_output)
294             printf("\nPolicy and auditing events for local system '%s - %s':\n",
295                     shost, "127.0.0.1");
296             
297             print_rootcheck(NULL,
298                             NULL, NULL, resolved_only, csv_output, show_last); 
299         }
300         else
301         {
302
303             OS_ReadKeys(&keys);
304
305             i = OS_IsAllowedID(&keys, agent_id);
306             if(i < 0)
307             {
308                 printf("\n** Invalid agent id '%s'.\n", agent_id);
309                 helpmsg();
310             }
311
312             /* Getting netmask from ip. */
313             final_ip[128] = '\0';
314             final_mask[128] = '\0';
315             getNetmask(keys.keyentries[i]->ip->netmask, 
316                        final_mask, 128);
317             snprintf(final_ip, 128, "%s%s",keys.keyentries[i]->ip->ip,
318                      final_mask);
319
320             if(!csv_output)
321             printf("\nPolicy and auditing events for agent "
322                        "'%s (%s) - %s':\n",
323                        keys.keyentries[i]->name, keys.keyentries[i]->id, 
324                        final_ip);
325
326             print_rootcheck(keys.keyentries[i]->name,
327                             keys.keyentries[i]->ip->ip, NULL, 
328                             resolved_only, csv_output, show_last);
329
330         }
331         
332         exit(0);
333     }
334
335
336     
337     printf("\n** Invalid argument combination.\n");
338     helpmsg();
339
340
341     return(0);
342 }
343
344
345 /* EOF */