~wibblymat/ubuntu/oneiric/sagan/ftbfs-jam

« back to all changes in this revision

Viewing changes to src/sagan-signal.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2011-03-17 15:18:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110317151858-iqvfx0hsxlamxp6b
Tags: upstream-0.1.9~svn129
ImportĀ upstreamĀ versionĀ 0.1.9~svn129

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** Copyright (C) 2009-2011 Softwink, Inc. 
 
3
** Copyright (C) 2009-2011 Champ Clark III <champ@softwink.com>
 
4
**
 
5
** This program is free software; you can redistribute it and/or modify
 
6
** it under the terms of the GNU General Public License Version 2 as
 
7
** published by the Free Software Foundation.  You may not use, modify or
 
8
** distribute this program under any other version of the GNU General
 
9
** Public License.
 
10
**
 
11
** This program is distributed in the hope that it will be useful,
 
12
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
** GNU General Public License for more details.
 
15
**
 
16
** You should have received a copy of the GNU General Public License
 
17
** along with this program; if not, write to the Free Software
 
18
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
/* sagan-signal.c
 
22
 *
 
23
 * This runs as a thread for signal processing.
 
24
 *
 
25
 */
 
26
 
 
27
#ifdef HAVE_CONFIG_H
 
28
#include "config.h"             /* From autoconf */
 
29
#endif
 
30
 
 
31
#include <stdio.h>
 
32
#include <stdlib.h>
 
33
#include <signal.h>
 
34
#include <pthread.h>
 
35
#include <stdint.h>
 
36
 
 
37
#include "version.h"
 
38
#include "sagan.h"
 
39
 
 
40
#ifdef HAVE_LIBLOGNORM
 
41
#include <liblognorm.h>
 
42
#include <ptree.h>
 
43
#include <lognorm.h>
 
44
static ln_ctx ctx;
 
45
int liblognorm_count;
 
46
#endif
 
47
 
 
48
#ifdef HAVE_LIBPRELUDE
 
49
#include <libprelude/prelude.h>
 
50
char sagan_prelude_profile[255];
 
51
sbool sagan_prelude_flag;
 
52
prelude_client_t *preludeclient;
 
53
#endif
 
54
 
 
55
#ifdef HAVE_LIBDNET
 
56
#include "output-plugins/sagan-unified2.h"
 
57
sbool sagan_unified2_flag;
 
58
#endif
 
59
 
 
60
#if defined(HAVE_LIBMYSQLCLIENT_R) || defined(HAVE_LIBPQ)
 
61
#include "output-plugins/sagan-snort.h"
 
62
#include "output-plugins/sagan-logzilla.h"
 
63
#endif
 
64
 
 
65
 
 
66
struct _SaganConfig *config;
 
67
struct _SaganCounters *counters;
 
68
 
 
69
struct rule_struct *rulestruct;
 
70
struct class_struct *classstruct;
 
71
struct ref_struct *refstruct;
 
72
 
 
73
 
 
74
char sagan_extern[255];
 
75
char sagan_esmtp_server[255];
 
76
int logzilla_log;
 
77
 
 
78
sbool daemonize;
 
79
 
 
80
uint64_t sagantotal;
 
81
uint64_t saganfound;
 
82
uint64_t sagandrop;
 
83
uint64_t threshold_total;
 
84
 
 
85
pthread_mutex_t sig_mutex = PTHREAD_MUTEX_INITIALIZER;
 
86
 
 
87
void sig_handler(int sigargs ) {
 
88
 
 
89
        sigset_t signal_set;
 
90
        int sig;
 
91
 
 
92
        for(;;) {
 
93
                /* wait for any and all signals */
 
94
                sigfillset( &signal_set );
 
95
                sigwait( &signal_set, &sig );
 
96
 
 
97
 
 
98
                switch( sig )
 
99
                {
 
100
                  /* exit */
 
101
                  case SIGQUIT:
 
102
                  case SIGINT:
 
103
                  case SIGTERM:
 
104
                  case SIGSEGV:
 
105
                  case SIGABRT:
 
106
 
 
107
                  sagan_log(0, "\n\n[Received signal %d. Sagan version %s shutting down]-------\n", sig, VERSION);
 
108
                  sagan_statistics();
 
109
 
 
110
#if defined(HAVE_LIBMYSQLCLIENT_R) || defined(HAVE_LIBPQ)
 
111
                  if ( config->dbtype != 0 ) record_last_cid();
 
112
#endif
 
113
 
 
114
#ifdef HAVE_LIBPRELUDE
 
115
 
 
116
/* This comment is from the Snort source code. "Sensor reporting to Prelude
 
117
   shall never go offline,  which is why we use the 
 
118
   PRELUDE_CLIENT_EXIT_STATUS_FAILURE.  */
 
119
 
 
120
if ( sagan_prelude_flag != 0 ) { 
 
121
prelude_client_destroy(preludeclient, PRELUDE_CLIENT_EXIT_STATUS_FAILURE);
 
122
prelude_deinit();
 
123
}
 
124
 
 
125
#endif
 
126
 
 
127
#ifdef HAVE_LIBDNET
 
128
if ( sagan_unified2_flag ) Unified2CleanExit(); 
 
129
#endif
 
130
 
 
131
                fflush(config->sagan_alert_stream);
 
132
                fclose(config->sagan_alert_stream);             /* Close Sagan alert file */
 
133
 
 
134
                fflush(config->sagan_log_stream);               /* Close the sagan.log */
 
135
                fclose(config->sagan_log_stream);
 
136
 
 
137
                removelockfile();
 
138
                exit(0);
 
139
                break;
 
140
 
 
141
                 case SIGHUP:
 
142
                   pthread_mutex_lock(&sig_mutex);
 
143
                   sagan_log(0, "[Reloading Sagan version %s.]-------", VERSION);
 
144
 
 
145
                      /* Reset counters */
 
146
                   counters->refcount=0; counters->classcount=0; counters->rulecount=0; counters->ruletotal=0;
 
147
                   
 
148
                   /* Re-load everything */
 
149
 
 
150
                   free(config);
 
151
                   load_config();
 
152
 
 
153
                  pthread_mutex_unlock(&sig_mutex);
 
154
                  
 
155
                  sagan_log(0, "Configuration reloaded.");
 
156
                  break;
 
157
 
 
158
                /* Signals to ignore */
 
159
                case 17:                /* Child process has exited. */ 
 
160
                case 28:                /* Terminal 'resize'/alarm. */
 
161
                break;
 
162
 
 
163
                case SIGUSR1:
 
164
                sagan_statistics(); 
 
165
                break;
 
166
 
 
167
                default:
 
168
                sagan_log(0, "[Received signal %d. Sagan doesn't know how to deal with]", sig);
 
169
                }
 
170
        }
 
171
}
 
172
 
 
173
 
 
174
/****************************************************************************/
 
175
/* sig_handler_daemon,  for handling signals when the --daemon flag is used */
 
176
/* We don't spawn a sig_handler() thread in the event --daemon is used.     */
 
177
/* Signals must be handled differently.  This is really redundant code and  */
 
178
/* I don't like it,  but oh well.                                           */
 
179
/****************************************************************************/
 
180
 
 
181
void sig_handler_daemon( int sig ) {
 
182
 
 
183
switch( sig )
 
184
        {
 
185
        case SIGQUIT:
 
186
        case SIGINT:
 
187
        case SIGTERM:
 
188
        case SIGSEGV:
 
189
        case SIGABRT:
 
190
 
 
191
        sagan_log(0, "\n\n[Received signal %d. Sagan version %s shutting down]-------\n", sig, VERSION);
 
192
        sagan_statistics();
 
193
 
 
194
#if defined(HAVE_LIBMYSQLCLIENT_R) || defined(HAVE_LIBPQ)
 
195
                  if ( config->dbtype != 0 ) record_last_cid();
 
196
#endif
 
197
 
 
198
#ifdef HAVE_LIBPRELUDE
 
199
 
 
200
/* This comment is from the Snort source code. "Sensor reporting to Prelude
 
201
   shall never go offline,  which is why we use the 
 
202
   PRELUDE_CLIENT_EXIT_STATUS_FAILURE.  */
 
203
 
 
204
if ( sagan_prelude_flag != 0 ) {
 
205
prelude_client_destroy(preludeclient, PRELUDE_CLIENT_EXIT_STATUS_FAILURE);
 
206
prelude_deinit();
 
207
}
 
208
 
 
209
#endif
 
210
 
 
211
#ifdef HAVE_LIBDNET
 
212
if ( sagan_unified2_flag ) Unified2CleanExit();
 
213
#endif
 
214
 
 
215
 
 
216
        fflush(config->sagan_alert_stream); 
 
217
        fclose(config->sagan_alert_stream);             /* Close Sagan alert file */
 
218
 
 
219
        fflush(config->sagan_log_stream);               /* Close the sagan.log */
 
220
        fclose(config->sagan_log_stream);
 
221
        
 
222
        removelockfile();
 
223
        exit(0);
 
224
        break;
 
225
 
 
226
 
 
227
        case SIGHUP:
 
228
                pthread_mutex_lock(&sig_mutex);
 
229
                sagan_log(0, "[Reloading Sagan version %s.]-------", VERSION);
 
230
 
 
231
                /* Reset counters */
 
232
                counters->refcount=0; counters->classcount=0; counters->rulecount=0; counters->ruletotal=0;
 
233
 
 
234
                /* Re-load everything */
 
235
 
 
236
                free(config);
 
237
                load_config();
 
238
 
 
239
                pthread_mutex_unlock(&sig_mutex);
 
240
 
 
241
                sagan_log(0, "Configuration reloaded.");
 
242
                break;
 
243
 
 
244
        case 17:                /* Child process has exited. */
 
245
        case 28:                /* Terminal 'resize'/alarm. */
 
246
        break;
 
247
 
 
248
        case SIGUSR1:
 
249
        sagan_statistics();
 
250
        break;
 
251
 
 
252
        default:
 
253
        sagan_log(0, "[Received signal %d. Sagan doesn't know how to deal with]", sig);
 
254
        }
 
255
 
 
256
}