File: prelude-manager.c

package info (click to toggle)
prelude-manager 1.0.1-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,304 kB
  • sloc: ansic: 31,338; sh: 11,290; sql: 1,156; makefile: 274
file content (328 lines) | stat: -rw-r--r-- 9,297 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*****
*
* Copyright (C) 1998-2007,2008 PreludeIDS Technologies. All Rights Reserved.
* Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
*
* This file is part of the Prelude-Manager program.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING.  If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h>

#include <libprelude/prelude.h>
#include <libprelude/prelude-log.h>

#include "prelude-manager.h"
#include "server-generic.h"
#include "sensor-server.h"
#include "manager-options.h"
#include "decode-plugins.h"
#include "report-plugins.h"
#include "filter-plugins.h"
#include "idmef-message-scheduler.h"
#include "reverse-relaying.h"
#include "manager-auth.h"

#define MANAGER_MODEL "Prelude Manager"
#define MANAGER_CLASS "Concentrator"
#define MANAGER_MANUFACTURER "http://www.prelude-ids.com"
#define DEFAULT_ANALYZER_NAME "prelude-manager"

extern manager_config_t config;

prelude_client_t *manager_client;
struct ev_loop *manager_event_loop;

static char **global_argv;
static volatile sig_atomic_t got_signal = 0;



/*
 * all function called here should be signal safe.
 */
static RETSIGTYPE handle_signal(int sig)
{
        size_t i;

        /*
         * stop the sensor server.
         */
        for ( i = 0; i < config.nserver; i++ )
                sensor_server_stop(config.server[i]);

        got_signal = sig;
}



#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
static void restart_manager(void)
{
        int ret;

        prelude_log(PRELUDE_LOG_INFO, "Restarting Prelude Manager (%s).\n", global_argv[0]);

        ret = execvp(global_argv[0], global_argv);
        if ( ret < 0 )
                prelude_log(PRELUDE_LOG_ERR, "Error restarting Prelude Manager (%s).\n", global_argv[0]);
}
#endif



static int fill_analyzer_infos(void)
{
        int ret;
        prelude_string_t *str;
        idmef_analyzer_t *local = NULL;

        local = prelude_client_get_analyzer(manager_client);
        assert(local);

        ret = prelude_string_new_constant(&str, VERSION);
        if ( ret < 0 )
                return ret;
        idmef_analyzer_set_version(local, str);

        ret = prelude_string_new_constant(&str, MANAGER_MODEL);
        if ( ret < 0 )
                return ret;
        idmef_analyzer_set_model(local, str);

        ret = prelude_string_new_constant(&str, MANAGER_CLASS);
        if ( ret < 0 )
                return ret;
        idmef_analyzer_set_class(local, str);

        ret = prelude_string_new_constant(&str, MANAGER_MANUFACTURER);
        if ( ret < 0 )
                return ret;
        idmef_analyzer_set_manufacturer(local, str);

        return 0;
}



static void heartbeat_cb(prelude_client_t *client, idmef_message_t *idmef)
{
        idmef_message_process(idmef);
}



static void sig_cb(struct ev_loop *loop, struct ev_signal *s, int revent)
{
#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        if ( s->signum == SIGHUP )
                signal(SIGHUP, SIG_IGN);
#endif

        handle_signal(s->signum);
        ev_unloop(manager_event_loop, EVUNLOOP_ALL);

        return;
}


static void add_signal(int signo, struct sigaction *action)
{
        ev_signal *s = malloc(sizeof(*s));
        ev_signal_init(s, sig_cb, signo);
        ev_signal_start(manager_event_loop, s);
}


static const char *get_restart_string(void)
{
#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        if ( got_signal == SIGHUP )
                return "will restart";
#endif

        return "terminating";
}


int main(int argc, char **argv)
{
        int ret;
        struct sigaction action;
        prelude_option_t *manager_root_optlist;

        prelude_init(&argc, argv);

        manager_event_loop = ev_default_loop_init(EVFLAG_AUTO);
        if ( ! manager_event_loop ) {
                prelude_log(PRELUDE_LOG_ERR, "error initializing libev.\n");
                return -1;
        }


        global_argv = argv;
        prelude_option_new_root(&manager_root_optlist);

        /*
         * make sure we ignore sighup until acceptable.
         */
#ifdef SA_INTERRUPT
        action.sa_flags = SA_INTERRUPT;
#else
        action.sa_flags = 0;
#endif

#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        action.sa_handler = SIG_IGN;
        sigemptyset(&action.sa_mask);
        sigaction(SIGHUP, &action, NULL);
#endif

        /*
         * Initialize plugin first.
         */
        PRELUDE_PLUGIN_SET_PRELOADED_SYMBOLS();

        ret = report_plugins_init(REPORT_PLUGIN_DIR, manager_root_optlist);
        if ( ret < 0 )
                return -1;
        prelude_log(PRELUDE_LOG_DEBUG, "Initialized %d reporting plugins.\n", ret);

        ret = decode_plugins_init(DECODE_PLUGIN_DIR, manager_root_optlist);
        if ( ret < 0 )
                return -1;
        prelude_log(PRELUDE_LOG_DEBUG, "Initialized %d decoding plugins.\n", ret);

        ret = filter_plugins_init(FILTER_PLUGIN_DIR, manager_root_optlist);
        if ( ret < 0 )
                return -1;
        prelude_log(PRELUDE_LOG_DEBUG, "Initialized %d filtering plugins.\n", ret);


        ret = manager_options_init(manager_root_optlist, &argc, argv);
        if ( ret < 0 )
                return -1;

        ret = prelude_client_new(&manager_client, DEFAULT_ANALYZER_NAME);
        if ( ret < 0 ) {
                prelude_perror(ret, "error creating prelude-client object");
                return -1;
        }

        fill_analyzer_infos();
        prelude_client_set_heartbeat_cb(manager_client, heartbeat_cb);
        prelude_client_set_flags(manager_client, prelude_client_get_flags(manager_client) & ~PRELUDE_CLIENT_FLAGS_CONNECT);
        prelude_client_set_config_filename(manager_client, config.config_file);

        ret = prelude_client_init(manager_client);
        if ( ret < 0 ) {
                prelude_perror(ret, "error initializing prelude-client");
                return ret;
        }

        ret = manager_options_read(manager_root_optlist, &argc, argv);
        if ( ret < 0 )
                return -1;

        ret = prelude_client_start(manager_client);
        if ( ret < 0 ) {
                prelude_perror(ret, "error starting prelude-client");
                return -1;
        }

        ret = reverse_relay_init();
        if ( ret < 0 )
                return -1;

        /*
         * start server
         */
        ret = manager_auth_init(manager_client, config.tls_options, config.dh_bits, config.dh_regenerate);
        if ( ret < 0 ) {
                if ( ret != -2 )
                        prelude_log(PRELUDE_LOG_WARN, "%s\n", prelude_client_get_setup_error(manager_client));

                return -1;
        }

        /*
         * prelude_client_start() should send it's initial heartbeat
         * before the scheduler start handling IDMEF messages, so that we don't refcount
         * the shared manager_client analyzer object from two different thread.
         */
        ret = idmef_message_scheduler_init();
        if ( ret < 0 ) {
                prelude_log(PRELUDE_LOG_ERR, "couldn't initialize alert scheduler.\n");
                return -1;
        }

        /*
         * setup signal handling
         */
#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        sigaction(SIGPIPE, &action, NULL);
#endif

        action.sa_handler = handle_signal;
        add_signal(SIGINT, &action);
        add_signal(SIGTERM, &action);
        add_signal(SIGABRT, &action);

#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        add_signal(SIGQUIT, &action);
        add_signal(SIGHUP, &action);
#endif

        server_generic_start(config.server, config.nserver);

        /*
         * we won't get there unless a signal is caught.
         */
        if ( got_signal )
                prelude_log(PRELUDE_LOG_WARN, "signal %d received, %s prelude-manager.\n",
                            got_signal, get_restart_string());

        idmef_message_scheduler_exit();
        prelude_client_destroy(manager_client, PRELUDE_CLIENT_EXIT_STATUS_FAILURE);

        report_plugins_close();

        /*
         * De-Initialize the Prelude library. This has the side effect of flushing
         * the Prelude asynchronous stack.
         */
        prelude_deinit();

#if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        if ( got_signal == SIGHUP )
                restart_manager();
#endif

        if ( config.pidfile )
                unlink(config.pidfile);

        exit(0);
}