b4dd39bffdc9219034b472883025b1ee41d9a21f
[darkstat] / darkstat.c
1 /* darkstat 3
2 * copyright (c) 2001-2010 Emil Mikulic.
3 *
4 * darkstat.c: signals, cmdline parsing, program body.
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include "darkstat.h"
11 #include "acct.h"
12 #include "cap.h"
13 #include "config.h"
14 #include "conv.h"
15 #include "daylog.h"
16 #include "db.h"
17 #include "dns.h"
18 #include "http.h"
19 #include "hosts_db.h"
20 #include "localip.h"
21 #include "ncache.h"
22 #include "pidfile.h"
23
24 #include "err.h"
25 #include <arpa/inet.h>
26 #include <sys/socket.h>
27 #include <netdb.h>
28 #include <assert.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <unistd.h>
36 #include <pcap.h>
37
38 #include "now.h"
39 time_t now;
40
41 #ifndef INADDR_NONE
42 # define INADDR_NONE (-1) /* Solaris */
43 #endif
44
45 /* --- Signal handling --- */
46 static volatile int running = 1;
47 static void sig_shutdown(int signum _unused_) { running = 0; }
48
49 static volatile int reset_pending = 0, export_pending = 0;
50 static void sig_reset(int signum _unused_)
51 {
52 reset_pending = 1;
53 export_pending = 1;
54 }
55
56 static void sig_export(int signum _unused_) { export_pending = 1; }
57
58 /* --- Commandline parsing --- */
59 static unsigned long
60 parsenum(const char *str, unsigned long max /* 0 for no max */)
61 {
62 unsigned long n;
63 char *end;
64
65 errno = 0;
66 n = strtoul(str, &end, 10);
67 if (*end != '\0')
68 errx(1, "\"%s\" is not a valid number", str);
69 if (errno == ERANGE)
70 errx(1, "\"%s\" is out of range", str);
71 if ((max != 0) && (n > max))
72 errx(1, "\"%s\" is out of range (max %lu)", str, max);
73 return n;
74 }
75
76 const char *opt_interface = NULL;
77 static void cb_interface(const char *arg) { opt_interface = arg; }
78
79 const char *opt_capfile = NULL;
80 static void cb_capfile(const char *arg) { opt_capfile = arg; }
81
82 int opt_want_snaplen = -1;
83 static void cb_snaplen(const char *arg)
84 { opt_want_snaplen = (int)parsenum(arg, 0); }
85
86 int opt_want_pppoe = 0;
87 static void cb_pppoe(const char *arg _unused_) { opt_want_pppoe = 1; }
88
89 int opt_want_syslog = 0;
90 static void cb_syslog(const char *arg _unused_) { opt_want_syslog = 1; }
91
92 int opt_want_verbose = 0;
93 static void cb_verbose(const char *arg _unused_) { opt_want_verbose = 1; }
94
95 int opt_want_daemonize = 1;
96 static void cb_no_daemon(const char *arg _unused_) { opt_want_daemonize = 0; }
97
98 int opt_want_promisc = 1;
99 static void cb_no_promisc(const char *arg _unused_) { opt_want_promisc = 0; }
100
101 int opt_want_dns = 1;
102 static void cb_no_dns(const char *arg _unused_) { opt_want_dns = 0; }
103
104 int opt_want_macs = 1;
105 static void cb_no_macs(const char *arg _unused_) { opt_want_macs = 0; }
106
107 int opt_want_lastseen = 1;
108 static void cb_no_lastseen(const char *arg _unused_) { opt_want_lastseen = 0; }
109
110 unsigned short opt_bindport = 667;
111 static void cb_port(const char *arg)
112 { opt_bindport = (unsigned short)parsenum(arg, 65536); }
113
114 const char *opt_bindaddr = NULL;
115 static void cb_bindaddr(const char *arg)
116 {
117 struct addrinfo hints, *ai;
118
119 memset(&hints, 0, sizeof(hints));
120 hints.ai_flags = AI_PASSIVE;
121 #ifdef AI_ADDRCONFIG
122 hints.ai_flags |= AI_ADDRCONFIG;
123 #endif
124 hints.ai_family = AF_UNSPEC;
125 hints.ai_socktype = SOCK_STREAM;
126
127 if (getaddrinfo(arg, NULL, &hints, &ai))
128 errx(1, "malformed address \"%s\"", arg);
129
130 freeaddrinfo(ai);
131 opt_bindaddr = arg;
132 }
133
134 const char *opt_filter = NULL;
135 static void cb_filter(const char *arg) { opt_filter = arg; }
136
137 static void cb_local(const char *arg) { acct_init_localnet(arg); }
138
139 const char *opt_chroot_dir = NULL;
140 static void cb_chroot(const char *arg) { opt_chroot_dir = arg; }
141
142 const char *opt_privdrop_user = NULL;
143 static void cb_user(const char *arg) { opt_privdrop_user = arg; }
144
145 const char *daylog_fn = NULL;
146 static void cb_daylog(const char *arg)
147 {
148 if (opt_chroot_dir == NULL)
149 errx(1, "the daylog file is relative to the chroot.\n"
150 "You must specify a --chroot dir before you can use --daylog.");
151 else
152 daylog_fn = arg;
153 }
154
155 const char *import_fn = NULL;
156 static void cb_import(const char *arg)
157 {
158 if (opt_chroot_dir == NULL)
159 errx(1, "the import file is relative to the chroot.\n"
160 "You must specify a --chroot dir before you can use --import.");
161 else
162 import_fn = arg;
163 }
164
165 const char *export_fn = NULL;
166 static void cb_export(const char *arg)
167 {
168 if ((opt_chroot_dir == NULL) && (opt_capfile == NULL))
169 errx(1, "the export file is relative to the chroot.\n"
170 "You must specify a --chroot dir before you can use --export.");
171 else
172 export_fn = arg;
173 }
174
175 static const char *pid_fn = NULL;
176 static void cb_pidfile(const char *arg)
177 {
178 if (opt_chroot_dir == NULL)
179 errx(1, "the pidfile is relative to the chroot.\n"
180 "You must specify a --chroot dir before you can use --pidfile.");
181 else
182 pid_fn = arg;
183 }
184
185 unsigned int opt_hosts_max = 1000;
186 static void cb_hosts_max(const char *arg)
187 { opt_hosts_max = parsenum(arg, 0); }
188
189 unsigned int opt_hosts_keep = 500;
190 static void cb_hosts_keep(const char *arg)
191 { opt_hosts_keep = parsenum(arg, 0); }
192
193 unsigned int opt_ports_max = 200;
194 static void cb_ports_max(const char *arg)
195 { opt_ports_max = parsenum(arg, 65536); }
196
197 unsigned int opt_ports_keep = 30;
198 static void cb_ports_keep(const char *arg)
199 { opt_ports_keep = parsenum(arg, 65536); }
200
201 unsigned int opt_highest_port = 65535;
202 static void cb_highest_port(const char *arg)
203 { opt_highest_port = parsenum(arg, 65535); }
204
205 int opt_wait_secs = -1;
206 static void cb_wait_secs(const char *arg)
207 { opt_wait_secs = (int)parsenum(arg, 0); }
208
209 int opt_want_hexdump = 0;
210 static void cb_hexdump(const char *arg _unused_) { opt_want_hexdump = 1; }
211
212 /* --- */
213
214 struct cmdline_arg {
215 const char *name, *arg_name; /* NULL arg_name means unary */
216 void (*callback)(const char *arg);
217 int num_seen;
218 };
219
220 static struct cmdline_arg cmdline_args[] = {
221 {"-i", "interface", cb_interface, 0},
222 {"-r", "file", cb_capfile, 0},
223 {"--snaplen", "bytes", cb_snaplen, 0},
224 {"--pppoe", NULL, cb_pppoe, 0},
225 {"--syslog", NULL, cb_syslog, 0},
226 {"--verbose", NULL, cb_verbose, 0},
227 {"--no-daemon", NULL, cb_no_daemon, 0},
228 {"--no-promisc", NULL, cb_no_promisc, 0},
229 {"--no-dns", NULL, cb_no_dns, 0},
230 {"--no-macs", NULL, cb_no_macs, 0},
231 {"--no-lastseen", NULL, cb_no_lastseen, 0},
232 {"-p", "port", cb_port, 0},
233 {"-b", "bindaddr", cb_bindaddr, 0},
234 {"-f", "filter", cb_filter, 0},
235 {"-l", "network/netmask", cb_local, 0},
236 {"--chroot", "dir", cb_chroot, 0},
237 {"--user", "username", cb_user, 0},
238 {"--daylog", "filename", cb_daylog, 0},
239 {"--import", "filename", cb_import, 0},
240 {"--export", "filename", cb_export, 0},
241 {"--pidfile", "filename", cb_pidfile, 0},
242 {"--hosts-max", "count", cb_hosts_max, 0},
243 {"--hosts-keep", "count", cb_hosts_keep, 0},
244 {"--ports-max", "count", cb_ports_max, 0},
245 {"--ports-keep", "count", cb_ports_keep, 0},
246 {"--highest-port", "port", cb_highest_port, 0},
247 {"--wait", "secs", cb_wait_secs, 0},
248 {"--hexdump", NULL, cb_hexdump, 0},
249 {NULL, NULL, NULL, 0}
250 };
251
252 static void
253 pad(const int width)
254 {
255 int i;
256 for (i=0; i<width; i++) printf(" ");
257 }
258
259 /*
260 * We autogenerate the usage statement from the cmdline_args data structure.
261 */
262 static void
263 usage(void)
264 {
265 int width, first;
266 struct cmdline_arg *arg;
267
268 printf(PACKAGE_STRING " (built with libpcap %d.%d)\n\n",
269 PCAP_VERSION_MAJOR, PCAP_VERSION_MINOR);
270
271 width = printf("usage: darkstat ");
272 first = 1;
273
274 for (arg = cmdline_args; arg->name != NULL; arg++) {
275 if (first) first = 0; else pad(width);
276 printf("[ %s", arg->name);
277 if (arg->arg_name != NULL) printf(" %s", arg->arg_name);
278 printf(" ]\n");
279 }
280 printf("\n"
281 "Please refer to the darkstat(8) manual page for further\n"
282 "documentation and usage examples.\n");
283 }
284
285 static void
286 parse_sub_cmdline(const int argc, char * const *argv)
287 {
288 struct cmdline_arg *arg;
289
290 if (argc == 0) return;
291 for (arg = cmdline_args; arg->name != NULL; arg++)
292 if (strcmp(argv[0], arg->name) == 0) {
293 if ((arg->arg_name != NULL) && (argc == 1)) {
294 fprintf(stderr,
295 "error: argument \"%s\" requires parameter \"%s\"\n",
296 arg->name, arg->arg_name);
297 usage();
298 exit(EXIT_FAILURE);
299 }
300 if (arg->num_seen > 0) {
301 fprintf(stderr,
302 "error: already specified argument \"%s\"\n",
303 arg->name);
304 usage();
305 exit(EXIT_FAILURE);
306 }
307
308 arg->num_seen++;
309 if (arg->arg_name == NULL) {
310 arg->callback(NULL);
311 parse_sub_cmdline(argc-1, argv+1);
312 } else {
313 arg->callback(argv[1]);
314 parse_sub_cmdline(argc-2, argv+2);
315 }
316 return;
317 }
318
319 fprintf(stderr, "error: illegal argument: \"%s\"\n", argv[0]);
320 usage();
321 exit(EXIT_FAILURE);
322 }
323
324 static void
325 parse_cmdline(const int argc, char * const *argv)
326 {
327 if (argc < 1) {
328 /* Not enough args. */
329 usage();
330 exit(EXIT_FAILURE);
331 }
332
333 parse_sub_cmdline(argc, argv);
334
335 /* start syslogging as early as possible */
336 if (opt_want_syslog) openlog("darkstat", LOG_NDELAY | LOG_PID, LOG_DAEMON);
337
338 /* some default values */
339 if (opt_chroot_dir == NULL) opt_chroot_dir = CHROOT_DIR;
340 if (opt_privdrop_user == NULL) opt_privdrop_user = PRIVDROP_USER;
341
342 /* sanity check args */
343 if ((opt_interface == NULL) && (opt_capfile == NULL))
344 errx(1, "must specify either interface (-i) or capture file (-r)");
345
346 if ((opt_interface != NULL) && (opt_capfile != NULL))
347 errx(1, "can't specify both interface (-i) and capture file (-r)");
348
349 if ((opt_hosts_max != 0) && (opt_hosts_keep >= opt_hosts_max)) {
350 opt_hosts_keep = opt_hosts_max / 2;
351 warnx("reducing --hosts-keep to %u, to be under --hosts-max (%u)",
352 opt_hosts_keep, opt_hosts_max);
353 }
354 verbosef("max %u hosts, cutting down to %u when exceeded",
355 opt_hosts_max, opt_hosts_keep);
356
357 if ((opt_ports_max != 0) && (opt_ports_keep >= opt_ports_max)) {
358 opt_ports_keep = opt_ports_max / 2;
359 warnx("reducing --ports-keep to %u, to be under --ports-max (%u)",
360 opt_ports_keep, opt_ports_max);
361 }
362 verbosef("max %u ports per host, cutting down to %u when exceeded",
363 opt_ports_max, opt_ports_keep);
364
365 if (opt_want_hexdump && !opt_want_verbose) {
366 opt_want_verbose = 1;
367 verbosef("--hexdump implies --verbose");
368 }
369
370 if (opt_want_hexdump && opt_want_daemonize) {
371 opt_want_daemonize = 0;
372 verbosef("--hexdump implies --no-daemon");
373 }
374 }
375
376 static void
377 run_from_capfile(void)
378 {
379 graph_init();
380 hosts_db_init();
381 cap_from_file(opt_capfile, opt_filter);
382 cap_stop();
383 if (export_fn != NULL) db_export(export_fn);
384 hosts_db_free();
385 graph_free();
386 verbosef("Total packets: %qu, bytes: %qu",
387 acct_total_packets, acct_total_bytes);
388 }
389
390 /* --- Program body --- */
391 int
392 main(int argc, char **argv)
393 {
394 test_64order();
395 parse_cmdline(argc-1, argv+1);
396
397 if (opt_capfile) {
398 /*
399 * This is very different from a regular run against a network
400 * interface.
401 */
402 run_from_capfile();
403 return 0;
404 }
405
406 /* must verbosef() before first fork to init lock */
407 verbosef("starting up");
408 if (pid_fn) pidfile_create(opt_chroot_dir, pid_fn, opt_privdrop_user);
409
410 if (opt_want_daemonize) {
411 verbosef("daemonizing to run in the background!");
412 daemonize_start();
413 verbosef("I am the main process");
414 }
415 if (pid_fn) pidfile_write_close();
416
417 /* do this first as it forks - minimize memory use */
418 if (opt_want_dns) dns_init(opt_privdrop_user);
419 cap_init(opt_interface, opt_filter, opt_want_promisc); /* needs root */
420 http_init(opt_bindaddr, opt_bindport, /*maxconn=*/-1);
421 ncache_init(); /* must do before chroot() */
422
423 privdrop(opt_chroot_dir, opt_privdrop_user);
424
425 /* Don't need root privs for these: */
426 now = time(NULL);
427 if (daylog_fn != NULL) daylog_init(daylog_fn);
428 graph_init();
429 hosts_db_init();
430 if (import_fn != NULL) db_import(import_fn);
431 localip_init(opt_interface);
432
433 if (signal(SIGTERM, sig_shutdown) == SIG_ERR)
434 errx(1, "signal(SIGTERM) failed");
435 if (signal(SIGINT, sig_shutdown) == SIG_ERR)
436 errx(1, "signal(SIGINT) failed");
437 if (signal(SIGUSR1, sig_reset) == SIG_ERR)
438 errx(1, "signal(SIGUSR1) failed");
439 if (signal(SIGUSR2, sig_export) == SIG_ERR)
440 errx(1, "signal(SIGUSR2) failed");
441
442 verbosef("entering main loop");
443 daemonize_finish();
444
445 while (running) {
446 int select_ret, max_fd = -1, use_timeout = 0;
447 struct timeval timeout;
448 fd_set rs, ws;
449
450 now = time(NULL);
451
452 if (export_pending) {
453 if (export_fn != NULL)
454 db_export(export_fn);
455 export_pending = 0;
456 }
457
458 if (reset_pending) {
459 hosts_db_reset();
460 graph_reset();
461 reset_pending = 0;
462 }
463
464 FD_ZERO(&rs);
465 FD_ZERO(&ws);
466
467 cap_fd_set(&rs, &max_fd, &timeout, &use_timeout);
468 http_fd_set(&rs, &ws, &max_fd, &timeout, &use_timeout);
469
470 select_ret = select(max_fd+1, &rs, &ws, NULL,
471 (use_timeout) ? &timeout : NULL);
472
473 if ((select_ret == 0) && (!use_timeout))
474 errx(1, "select() erroneously timed out");
475
476 if (select_ret == -1) {
477 if (errno == EINTR)
478 continue;
479 else
480 err(1, "select()");
481 }
482 else {
483 graph_rotate();
484 cap_poll(&rs);
485 dns_poll();
486 http_poll(&rs, &ws);
487 }
488 }
489
490 verbosef("shutting down");
491 verbosef("pcap stats: %u packets received, %u packets dropped",
492 cap_pkts_recv, cap_pkts_drop);
493 http_stop();
494 cap_stop();
495 dns_stop();
496 if (export_fn != NULL) db_export(export_fn);
497 hosts_db_free();
498 graph_free();
499 if (daylog_fn != NULL) daylog_free();
500 ncache_free();
501 if (pid_fn) pidfile_unlink();
502 verbosef("shut down");
503 return (EXIT_SUCCESS);
504 }
505
506 /* vim:set ts=3 sw=3 tw=78 expandtab: */