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