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