Imported Upstream version 2.7
[ossec-hids.git] / src / monitord / report.c
1 /* @(#) $Id: ./src/monitord/report.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2010 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 #include "shared.h"
15
16
17
18 /* ossec-reportd - Runs manual reports. */
19 void report_help()
20 {
21     printf("\nOSSEC HIDS %s: Generate reports (via stdin).\n", ARGV0);
22     printf("Available options:\n");
23     printf("\t-h                  This help message.\n");
24     printf("\t-f <filter> <value> Filter the results.\n");
25     printf("\t-r <filter> <value> Show related entries.\n");
26     printf("\t-n                  Creates a description for the report.\n");
27     printf("\t-s                  Show the alert dump.\n");
28     printf("\n");
29     printf("\tFilters allowed: group, rule, level, location,\n");
30     printf("\t                 user, srcip, filename\n");
31     printf("\n");
32     printf("Examples:\n");
33     printf("\t-f group authentication_success (to filter on login success).\n");
34     printf("\t-f level 10  (to filter on level >= 10).\n");
35     printf("\t-f group authentication -r user srcip (to show the srcip for all users).\n");
36     exit(1);
37 }
38
39
40
41 int main(int argc, char **argv)
42 {
43     int c, test_config = 0;
44     int uid=0,gid=0;
45     char *dir  = DEFAULTDIR;
46     char *user = USER;
47     char *group = GROUPGLOBAL;
48     char *cfg = DEFAULTCPATH;
49
50     char *filter_by = NULL;
51     char *filter_value = NULL;
52
53     char *related_of = NULL;
54     char *related_values = NULL;
55     report_filter r_filter;
56
57
58     /* Setting the name */
59     OS_SetName(ARGV0);
60
61     r_filter.group = NULL;
62     r_filter.rule = NULL;
63     r_filter.level = NULL;
64     r_filter.location = NULL;
65     r_filter.srcip = NULL;
66     r_filter.user = NULL;
67     r_filter.files = NULL;
68     r_filter.show_alerts = 0;
69
70     r_filter.related_group = 0;
71     r_filter.related_rule = 0;
72     r_filter.related_level = 0;
73     r_filter.related_location = 0;
74     r_filter.related_srcip = 0;
75     r_filter.related_user = 0;
76     r_filter.related_file = 0;
77
78     r_filter.report_name = NULL;
79
80     while((c = getopt(argc, argv, "Vdhstu:g:D:c:f:v:n:r:")) != -1)
81     {
82         switch(c){
83             case 'V':
84                 print_version();
85                 break;
86             case 'h':
87                 report_help();
88                 break;
89             case 'd':
90                 nowDebug();
91                 break;
92             case 'n':
93                 if(!optarg)
94                     ErrorExit("%s: -n needs an argument",ARGV0);
95                 r_filter.report_name = optarg;
96                 break;
97             case 'r':
98                 if(!optarg || !argv[optind])
99                     ErrorExit("%s: -r needs two argument",ARGV0);
100                 related_of = optarg;
101                 related_values = argv[optind];
102
103                 if(os_report_configfilter(related_of, related_values,
104                                           &r_filter, REPORT_RELATED) < 0)
105                 {
106                     ErrorExit(CONFIG_ERROR, ARGV0, "user argument");
107                 }
108                 optind++;
109                 break;
110             case 'f':
111                 if(!optarg)
112                     ErrorExit("%s: -f needs two argument",ARGV0);
113                 filter_by = optarg;
114                 filter_value = argv[optind];
115
116                 if(os_report_configfilter(filter_by, filter_value,
117                                           &r_filter, REPORT_FILTER) < 0)
118                 {
119                     ErrorExit(CONFIG_ERROR, ARGV0, "user argument");
120                 }
121                 optind++;
122                 break;
123             case 'u':
124                 if(!optarg)
125                     ErrorExit("%s: -u needs an argument",ARGV0);
126                 user=optarg;
127                 break;
128             case 'g':
129                 if(!optarg)
130                     ErrorExit("%s: -g needs an argument",ARGV0);
131                 group=optarg;
132                 break;
133             case 'D':
134                 if(!optarg)
135                     ErrorExit("%s: -D needs an argument",ARGV0);
136                 dir=optarg;
137                 break;
138             case 'c':
139                 if(!optarg)
140                     ErrorExit("%s: -c needs an argument",ARGV0);
141                 cfg = optarg;
142                 break;
143             case 't':
144                 test_config = 1;
145                 break;
146             case 's':
147                 r_filter.show_alerts = 1;
148                 break;
149             default:
150                 report_help();
151                 break;
152         }
153
154     }
155
156     /* Starting daemon */
157     debug1(STARTED_MSG,ARGV0);
158
159     /* Check if the user/group given are valid */
160     uid = Privsep_GetUser(user);
161     gid = Privsep_GetGroup(group);
162     if((uid < 0)||(gid < 0))
163         ErrorExit(USER_ERROR,ARGV0,user,group);
164
165
166
167     /* Exit here if test config is set */
168     if(test_config)
169         exit(0);
170
171
172     /* Privilege separation */  
173     if(Privsep_SetGroup(gid) < 0)
174         ErrorExit(SETGID_ERROR,ARGV0,group);
175
176
177     /* chrooting */
178     if(Privsep_Chroot(dir) < 0)
179         ErrorExit(CHROOT_ERROR,ARGV0,dir);
180
181     nowChroot();
182
183
184
185     /* Changing user */
186     if(Privsep_SetUser(uid) < 0)
187         ErrorExit(SETUID_ERROR,ARGV0,user);
188
189
190     debug1(PRIVSEP_MSG,ARGV0,dir,user);
191
192
193
194     /* Signal manipulation */
195     StartSIG(ARGV0);
196
197
198
199     /* Creating PID files */
200     if(CreatePID(ARGV0, getpid()) < 0)
201         ErrorExit(PID_ERROR,ARGV0);
202
203
204     /* Start up message */
205     verbose(STARTUP_MSG, ARGV0, (int)getpid());
206
207     /* the real stuff now */    
208     os_ReportdStart(&r_filter);
209     exit(0);
210 }
211
212
213 /* EOF */