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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/******************************************************************************
* IP protocols logger *
******************************************************************************
* Written by Hugo Haas (hugo@debian.org) *
* Based on the idea of Mike Edulla (medulla@infosoc.com) *
* Copyright (C) 1998-1999 Hugo Haas *
* See the HISTORY file for the changes *
* *
* Use of getopt_long for argument parsing: Etienne Bernard 1999 *
******************************************************************************/
/*
* 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 of the License, 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; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <stdlib.h>
#include <getopt.h>
#include "defines.h"
#include "configuration.h"
#include "icmp.h"
#include "tcp.h"
#include "udp.h"
#include "log.h"
#include "netutils.h"
#include "filter.h"
#include "pidfile.h"
/* Logging mechanism */
struct loginfo log;
/* Do we have to run as a daemon? */
int run_as_daemon = TRUE;
/* Configuration file to use */
char *configuration_file = NULL;
/* Protocols that we log */
extern unsigned short log_protocols;
/* Expiration of DNS data */
extern unsigned int dns_expire;
/* Threads */
pthread_t icmp_t, tcp_t, udp_t;
/*
* do_help
*
* Display the command line options
*/
void do_help() {
printf("ippl version %s \n\n", VERSION);
printf("\t-n, --nodaemon:\tdo not run as a daemon and write information to stdout\n");
printf("\t-c, --config <file>:\tspecify an alternate configuration file\n");
printf("\t-h, --help:\tdisplay this help and exit\n");
printf("\nPlease report bugs to ippl@via.ecp.fr\n");
}
/*
* run_thread
*
* Run a thread. Certain signals are blocked.
*
*/
void run_thread(pthread_t *t, void *(*function)(void*), void *param) {
pthread_attr_t attr_t;
sigset_t newset, oldset ;
sigfillset(&newset);
pthread_sigmask(SIG_BLOCK, &newset, &oldset);
pthread_attr_init(&attr_t);
pthread_attr_setdetachstate(&attr_t, PTHREAD_CREATE_DETACHED);
pthread_create(t, &attr_t, function, param);
pthread_attr_destroy(&attr_t);
pthread_sigmask(SIG_UNBLOCK, &newset, &oldset);
}
/*
* start_all_threads
*
* Parses the config file and launches the threads
*/
void start_all_threads() {
struct passwd *account;
extern struct loginfo icmp_log, tcp_log, udp_log;
extern char *used_user;
init_loginfo(&icmp_log);
init_loginfo(&tcp_log);
init_loginfo(&udp_log);
parse_config_file(configuration_file);
account = getpwnam(used_user);
if (!account) {
log.log(log.level_or_fd, "WARNING: I cannot find the \"%s\" account. Not spawning threads.", used_user);
log_protocols = NONE;
return;
}
if (!strcmp(used_user, "root")) {
log.log(log.level_or_fd, "WARNING: Using root account to run threads!");
}
if (log_protocols == NONE) {
log.log(log.level_or_fd, "WARNING: Nothing to log.");
return;
}
initialize_dns_cache();
if (log_protocols & RUN_ICMP) {
icmp_log.open(&icmp_log.level_or_fd, icmp_log.file);
run_thread(&icmp_t, log_icmp, (void *)account);
}
if (log_protocols & RUN_TCP) {
tcp_log.open(&tcp_log.level_or_fd, tcp_log.file);
run_thread(&tcp_t, log_tcp, (void *)account);
}
if (log_protocols & RUN_UDP) {
udp_log.open(&udp_log.level_or_fd, udp_log.file);
run_thread(&udp_t, log_udp, (void *)account);
}
}
/*
* reload_configuration
*
* Stops the threads and reloads the configuration
*/
void reload_configuration() {
extern pthread_mutex_t log_mutex, service_mutex, dns_mutex, r_mux, w_mux;
extern pthread_cond_t w_cond;
extern int readers;
extern struct loginfo icmp_log, tcp_log, udp_log;
extern pthread_mutex_t log_mutex;
extern int icmp_socket, tcp_socket, udp_socket;
/* We get all the mutexes that could block the threads,
* because we must be sure that no thread is in a
* critical portion of the code. */
/* First, make sure that no packet is being logged
* Let's block acces to the filter */
pthread_mutex_lock(&w_mux);
pthread_mutex_lock(&r_mux);
while (readers > 0)
pthread_cond_wait(&w_cond, &r_mux);
/* Then, acquire all the others so that you are sure we are in a safe
* position */
/* dns cache mutex */
pthread_mutex_lock(&dns_mutex);
/* Service query mutex */
pthread_mutex_lock(&service_mutex);
/* Logging mutex */
pthread_mutex_lock(&log_mutex);
/* Now we can safely kill the threads. */
if (log_protocols & RUN_ICMP)
pthread_cancel(icmp_t);
if (log_protocols & RUN_TCP)
pthread_cancel(tcp_t);
if (log_protocols & RUN_UDP)
pthread_cancel(udp_t);
/* Close the sockets */
close(icmp_socket);
close(tcp_socket);
close(udp_socket);
/* Close log files */
icmp_log.close(&icmp_log.level_or_fd, icmp_log.file);
tcp_log.close(&tcp_log.level_or_fd, tcp_log.file);
udp_log.close(&udp_log.level_or_fd, udp_log.file);
/* We release the mutexes as soon as possible */
pthread_mutex_unlock(&log_mutex);
pthread_mutex_unlock(&service_mutex);
pthread_mutex_unlock(&dns_mutex);
pthread_mutex_unlock(&r_mux);
pthread_mutex_unlock(&w_mux);
/* We destroy previous configuration */
destroy_filter();
/* And we restart all the threads */
start_all_threads();
}
/*
* parse_arguments
*
* Parse the arguments given on the command line
*/
void parse_arguments(int argc, char **argv) {
char path[PATH_MAX];
static struct option long_options[] =
{
{ "help", 0, 0, 0 },
{ "nodaemon", 0, &run_as_daemon, FALSE },
{ "config", 1, 0, 0 },
{ 0, 0, 0, 0 }
};
int c, option_index = 0;
while ((c = getopt_long(argc, argv, "hnlrc:", long_options,
&option_index))!=-1) {
switch (c) {
case 0:
switch (option_index) {
case 0:
do_help();
exit(0);
case 2:
configuration_file = strdup(optarg);
break;
}
break;
case 'h':
do_help();
exit(0);
case 'n':
run_as_daemon = FALSE;
break;
case 'c':
configuration_file = strdup(optarg);
break;
case '?':
do_help();
exit(1);
}
}
/* Get absolute path for configuration file */
if (configuration_file) {
if (configuration_file[0] != '/') {
getcwd(path, PATH_MAX);
strcat(path, "/");
strcat(path, configuration_file);
free(configuration_file);
configuration_file = strdup(path);
}
}
}
/*
* go_background
*
* Run as a daemon
*
* Based on iplogger (C) Mike Edulla + modifications by Topi Miettinen
* PID file handling based on sysklogd (C) Martin Schulze
*/
void go_background() {
/* Check PID */
if (check_pid(PID_FILE)) {
log.log(log.level_or_fd, "Already running. Exiting...");
exit(1);
}
if (getppid() != 1) {
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
}
daemon(0, 0);
umask(027);
/* Write PID file */
if (!write_pid(PID_FILE)) {
log.log(log.level_or_fd, "Can't write pid.\n");
exit(1);
}
}
/*
* die
*
* Function executed when the daemon is ended
*/
void die(int sig) {
extern struct loginfo icmp_log, tcp_log, udp_log;
extern pthread_mutex_t log_mutex;
/* Stop threads and close log files */
pthread_mutex_lock(&log_mutex);
if (log_protocols & RUN_ICMP)
pthread_cancel(icmp_t);
if (log_protocols & RUN_TCP)
pthread_cancel(tcp_t);
if (log_protocols & RUN_UDP)
pthread_cancel(udp_t);
icmp_log.close(&icmp_log.level_or_fd, icmp_log.file);
tcp_log.close(&tcp_log.level_or_fd, tcp_log.file);
udp_log.close(&udp_log.level_or_fd, udp_log.file);
pthread_mutex_unlock(&log_mutex);
if (run_as_daemon) {
(void) remove_pid(PID_FILE);
}
log.log(log.level_or_fd, "IP Protocols Logger: stopped (signal %d).", sig);
exit(0);
}
/*
* sighup
*
* Function executed when we receive a SIHUP signal
*/
void sighup(int sig) {
reload_configuration();
log.log(log.level_or_fd, "IP Protocols Logger: reloaded configuration.");
signal(SIGHUP, sighup);
}
/*
* handle_signals
*
* Define how to handle the different signals
*/
void handle_signals(void) {
signal(SIGTERM, die);
if (!run_as_daemon) {
signal(SIGINT, die);
}
signal(SIGHUP, sighup);
}
/*
* MAIN
*/
int main(int argc, char **argv) {
parse_arguments(argc, argv);
setuid(0);
if (geteuid() != 0) {
printf("You must be root to run this program.\n");
exit(1);
}
handle_signals();
if (configuration_file == NULL)
configuration_file = strdup(CONFIGURATION_FILE);
init_loginfo(&log);
log.open(&log.level_or_fd, log.file);
if (run_as_daemon) {
go_background();
}
start_all_threads();
log.log(log.level_or_fd, "IP Protocols Logger: started.");
for(;;) {
sleep(dns_expire);
initialize_dns_cache();
refresh_filter();
}
/*@NOTREACHED@*/
exit(0);
}
|