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