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