dodan override za lintian
[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     // TODO: delete or implement
49     char *cfg __attribute__((unused)) = DEFAULTCPATH;
50
51     char *filter_by = NULL;
52     char *filter_value = NULL;
53
54     char *related_of = NULL;
55     char *related_values = NULL;
56     report_filter r_filter;
57
58
59     /* Setting the name */
60     OS_SetName(ARGV0);
61
62     r_filter.group = NULL;
63     r_filter.rule = NULL;
64     r_filter.level = NULL;
65     r_filter.location = NULL;
66     r_filter.srcip = NULL;
67     r_filter.user = NULL;
68     r_filter.files = NULL;
69     r_filter.show_alerts = 0;
70
71     r_filter.related_group = 0;
72     r_filter.related_rule = 0;
73     r_filter.related_level = 0;
74     r_filter.related_location = 0;
75     r_filter.related_srcip = 0;
76     r_filter.related_user = 0;
77     r_filter.related_file = 0;
78
79     r_filter.report_name = NULL;
80
81     while((c = getopt(argc, argv, "Vdhstu:g:D:c:f:v:n:r:")) != -1)
82     {
83         switch(c){
84             case 'V':
85                 print_version();
86                 break;
87             case 'h':
88                 report_help();
89                 break;
90             case 'd':
91                 nowDebug();
92                 break;
93             case 'n':
94                 if(!optarg)
95                     ErrorExit("%s: -n needs an argument",ARGV0);
96                 r_filter.report_name = optarg;
97                 break;
98             case 'r':
99                 if(!optarg || !argv[optind])
100                     ErrorExit("%s: -r needs two argument",ARGV0);
101                 related_of = optarg;
102                 related_values = argv[optind];
103
104                 if(os_report_configfilter(related_of, related_values,
105                                           &r_filter, REPORT_RELATED) < 0)
106                 {
107                     ErrorExit(CONFIG_ERROR, ARGV0, "user argument");
108                 }
109                 optind++;
110                 break;
111             case 'f':
112                 if(!optarg)
113                     ErrorExit("%s: -f needs two argument",ARGV0);
114                 filter_by = optarg;
115                 filter_value = argv[optind];
116
117                 if(os_report_configfilter(filter_by, filter_value,
118                                           &r_filter, REPORT_FILTER) < 0)
119                 {
120                     ErrorExit(CONFIG_ERROR, ARGV0, "user argument");
121                 }
122                 optind++;
123                 break;
124             case 'u':
125                 if(!optarg)
126                     ErrorExit("%s: -u needs an argument",ARGV0);
127                 user=optarg;
128                 break;
129             case 'g':
130                 if(!optarg)
131                     ErrorExit("%s: -g needs an argument",ARGV0);
132                 group=optarg;
133                 break;
134             case 'D':
135                 if(!optarg)
136                     ErrorExit("%s: -D needs an argument",ARGV0);
137                 dir=optarg;
138                 break;
139             case 'c':
140                 if(!optarg)
141                     ErrorExit("%s: -c needs an argument",ARGV0);
142                 cfg = optarg;
143                 break;
144             case 't':
145                 test_config = 1;
146                 break;
147             case 's':
148                 r_filter.show_alerts = 1;
149                 break;
150             default:
151                 report_help();
152                 break;
153         }
154
155     }
156
157     /* Starting daemon */
158     debug1(STARTED_MSG,ARGV0);
159
160     /* Check if the user/group given are valid */
161     uid = Privsep_GetUser(user);
162     gid = Privsep_GetGroup(group);
163     if((uid < 0)||(gid < 0))
164         ErrorExit(USER_ERROR,ARGV0,user,group);
165
166
167
168     /* Exit here if test config is set */
169     if(test_config)
170         exit(0);
171
172
173     /* Privilege separation */
174     if(Privsep_SetGroup(gid) < 0)
175         ErrorExit(SETGID_ERROR,ARGV0,group);
176
177
178     /* chrooting */
179     if(Privsep_Chroot(dir) < 0)
180         ErrorExit(CHROOT_ERROR,ARGV0,dir);
181
182     nowChroot();
183
184
185
186     /* Changing user */
187     if(Privsep_SetUser(uid) < 0)
188         ErrorExit(SETUID_ERROR,ARGV0,user);
189
190
191     debug1(PRIVSEP_MSG,ARGV0,dir,user);
192
193
194
195     /* Signal manipulation */
196     StartSIG(ARGV0);
197
198
199
200     /* Creating PID files */
201     if(CreatePID(ARGV0, getpid()) < 0)
202         ErrorExit(PID_ERROR,ARGV0);
203
204
205     /* Start up message */
206     verbose(STARTUP_MSG, ARGV0, (int)getpid());
207
208     /* the real stuff now */
209     os_ReportdStart(&r_filter);
210     exit(0);
211 }
212
213
214 /* EOF */