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