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