new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / reportd / report.c
1 /* Copyright (C) 2010 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11
12 /* Prototypes */
13 static void help_reportd(void) __attribute__((noreturn));
14
15
16 /* Print help statement */
17 static void help_reportd()
18 {
19     print_header();
20     print_out("  Generate reports (via stdin)");
21     print_out("  %s: -[Vhdtns] [-u user] [-g group] [-D dir] [-f filter value] [-r filter value]", ARGV0);
22     print_out("    -V          Version and license message");
23     print_out("    -h          This help message");
24     print_out("    -d          Execute in debug mode. This parameter");
25     print_out("                can be specified multiple times");
26     print_out("                to increase the debug level.");
27     print_out("    -t          Test configuration");
28     print_out("    -n          Create description for the report");
29     print_out("    -s          Show the alert dump");
30     print_out("    -u <user>   User to run as (default: %s)", USER);
31     print_out("    -g <group>  Group to run as (default: %s)", GROUPGLOBAL);
32     print_out("    -D <dir>    Directory to chroot into (default: %s)", DEFAULTDIR);
33     print_out("    -f <filter> <value> Filter the results");
34     print_out("    -r <filter> <value> Show related entries");
35     print_out("    Filters allowed: group, rule, level, location,");
36     print_out("                     user, srcip, filename");
37     print_out("  Examples:");
38     print_out("     -f group authentication_success (to filter on login success)");
39     print_out("     -f level 10 (to filter on level >= 10)");
40     print_out("     -f group authentication -r user srcip (to show srcip for all users)");
41     print_out(" ");
42     exit(1);
43 }
44
45 int main(int argc, char **argv)
46 {
47     int c, test_config = 0;
48     uid_t uid;
49     gid_t gid;
50     const char *dir  = DEFAULTDIR;
51     const char *user = USER;
52     const char *group = GROUPGLOBAL;
53
54     const char *filter_by = NULL;
55     const char *filter_value = NULL;
56
57     const char *related_of = NULL;
58     const char *related_values = NULL;
59     report_filter r_filter;
60
61     /* Set the name */
62     OS_SetName(ARGV0);
63
64     r_filter.group = NULL;
65     r_filter.rule = NULL;
66     r_filter.level = NULL;
67     r_filter.location = NULL;
68     r_filter.srcip = NULL;
69     r_filter.user = NULL;
70     r_filter.files = NULL;
71     r_filter.show_alerts = 0;
72
73     r_filter.related_group = 0;
74     r_filter.related_rule = 0;
75     r_filter.related_level = 0;
76     r_filter.related_location = 0;
77     r_filter.related_srcip = 0;
78     r_filter.related_user = 0;
79     r_filter.related_file = 0;
80
81     r_filter.report_name = NULL;
82
83     while ((c = getopt(argc, argv, "Vdhstu:g:D:f:v:n:r:")) != -1) {
84         switch (c) {
85             case 'V':
86                 print_version();
87                 break;
88             case 'h':
89                 help_reportd();
90                 break;
91             case 'd':
92                 nowDebug();
93                 break;
94             case 'n':
95                 if (!optarg) {
96                     ErrorExit("%s: -n needs an argument", ARGV0);
97                 }
98                 r_filter.report_name = optarg;
99                 break;
100             case 'r':
101                 if (!optarg || !argv[optind]) {
102                     ErrorExit("%s: -r needs two argument", ARGV0);
103                 }
104                 related_of = optarg;
105                 related_values = argv[optind];
106
107                 if (os_report_configfilter(related_of, related_values,
108                                            &r_filter, REPORT_RELATED) < 0) {
109                     ErrorExit(CONFIG_ERROR, ARGV0, "user argument");
110                 }
111                 optind++;
112                 break;
113             case 'f':
114                 if (!optarg) {
115                     ErrorExit("%s: -f needs two argument", ARGV0);
116                 }
117                 filter_by = optarg;
118                 filter_value = argv[optind];
119
120                 if (os_report_configfilter(filter_by, filter_value,
121                                            &r_filter, REPORT_FILTER) < 0) {
122                     ErrorExit(CONFIG_ERROR, ARGV0, "user argument");
123                 }
124                 optind++;
125                 break;
126             case 'u':
127                 if (!optarg) {
128                     ErrorExit("%s: -u needs an argument", ARGV0);
129                 }
130                 user = optarg;
131                 break;
132             case 'g':
133                 if (!optarg) {
134                     ErrorExit("%s: -g needs an argument", ARGV0);
135                 }
136                 group = optarg;
137                 break;
138             case 'D':
139                 if (!optarg) {
140                     ErrorExit("%s: -D needs an argument", ARGV0);
141                 }
142                 dir = 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                 help_reportd();
152                 break;
153         }
154
155     }
156
157     /* Start 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 == (uid_t) - 1 || gid == (gid_t) - 1) {
164         ErrorExit(USER_ERROR, ARGV0, user, group);
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, errno, strerror(errno));
175     }
176
177     /* chroot */
178     if (Privsep_Chroot(dir) < 0) {
179         ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
180     }
181     nowChroot();
182
183     /* Change user */
184     if (Privsep_SetUser(uid) < 0) {
185         ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
186     }
187
188     debug1(CHROOT_MSG, ARGV0, dir);
189     debug1(PRIVSEP_MSG, ARGV0, user);
190
191     /* Signal manipulation */
192     StartSIG(ARGV0);
193
194     /* Create PID files */
195     if (CreatePID(ARGV0, getpid()) < 0) {
196         ErrorExit(PID_ERROR, ARGV0);
197     }
198
199     /* Start up message */
200     verbose(STARTUP_MSG, ARGV0, (int)getpid());
201
202     /* The real stuff now */
203     os_ReportdStart(&r_filter);
204
205     exit(0);
206 }
207