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