pflogd.c revision 1.1.1.1 1 1.1 itojun /* $OpenBSD: pflogd.c,v 1.27 2004/02/13 19:01:57 otto Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (c) 2001 Theo de Raadt
5 1.1 itojun * Copyright (c) 2001 Can Erkin Acar
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun *
12 1.1 itojun * - Redistributions of source code must retain the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer.
14 1.1 itojun * - Redistributions in binary form must reproduce the above
15 1.1 itojun * copyright notice, this list of conditions and the following
16 1.1 itojun * disclaimer in the documentation and/or other materials provided
17 1.1 itojun * with the distribution.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.1 itojun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.1 itojun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 1.1 itojun * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 1.1 itojun * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 1.1 itojun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1 itojun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 1.1 itojun * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 1.1 itojun * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 itojun * POSSIBILITY OF SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun #include <sys/types.h>
34 1.1 itojun #include <sys/ioctl.h>
35 1.1 itojun #include <sys/file.h>
36 1.1 itojun #include <sys/stat.h>
37 1.1 itojun #include <stdio.h>
38 1.1 itojun #include <stdlib.h>
39 1.1 itojun #include <string.h>
40 1.1 itojun #include <unistd.h>
41 1.1 itojun #include <pcap-int.h>
42 1.1 itojun #include <pcap.h>
43 1.1 itojun #include <syslog.h>
44 1.1 itojun #include <signal.h>
45 1.1 itojun #include <errno.h>
46 1.1 itojun #include <stdarg.h>
47 1.1 itojun #include <fcntl.h>
48 1.1 itojun #include <util.h>
49 1.1 itojun #include "pflogd.h"
50 1.1 itojun
51 1.1 itojun pcap_t *hpcap;
52 1.1 itojun static FILE *dpcap;
53 1.1 itojun
54 1.1 itojun int Debug = 0;
55 1.1 itojun static int snaplen = DEF_SNAPLEN;
56 1.1 itojun static int cur_snaplen = DEF_SNAPLEN;
57 1.1 itojun
58 1.1 itojun volatile sig_atomic_t gotsig_close, gotsig_alrm, gotsig_hup;
59 1.1 itojun
60 1.1 itojun char *filename = PFLOGD_LOG_FILE;
61 1.1 itojun char *interface = PFLOGD_DEFAULT_IF;
62 1.1 itojun char *filter = NULL;
63 1.1 itojun
64 1.1 itojun char errbuf[PCAP_ERRBUF_SIZE];
65 1.1 itojun
66 1.1 itojun int log_debug = 0;
67 1.1 itojun unsigned int delay = FLUSH_DELAY;
68 1.1 itojun
69 1.1 itojun char *copy_argv(char * const *);
70 1.1 itojun void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
71 1.1 itojun void dump_packet_nobuf(u_char *, const struct pcap_pkthdr *, const u_char *);
72 1.1 itojun int flush_buffer(FILE *);
73 1.1 itojun int init_pcap(void);
74 1.1 itojun void logmsg(int, const char *, ...);
75 1.1 itojun void purge_buffer(void);
76 1.1 itojun int reset_dump(void);
77 1.1 itojun int scan_dump(FILE *, off_t);
78 1.1 itojun int set_snaplen(int);
79 1.1 itojun void set_suspended(int);
80 1.1 itojun void sig_alrm(int);
81 1.1 itojun void sig_close(int);
82 1.1 itojun void sig_hup(int);
83 1.1 itojun void usage(void);
84 1.1 itojun
85 1.1 itojun /* buffer must always be greater than snaplen */
86 1.1 itojun static int bufpkt = 0; /* number of packets in buffer */
87 1.1 itojun static int buflen = 0; /* allocated size of buffer */
88 1.1 itojun static char *buffer = NULL; /* packet buffer */
89 1.1 itojun static char *bufpos = NULL; /* position in buffer */
90 1.1 itojun static int bufleft = 0; /* bytes left in buffer */
91 1.1 itojun
92 1.1 itojun /* if error, stop logging but count dropped packets */
93 1.1 itojun static int suspended = -1;
94 1.1 itojun static long packets_dropped = 0;
95 1.1 itojun
96 1.1 itojun void
97 1.1 itojun set_suspended(int s)
98 1.1 itojun {
99 1.1 itojun if (suspended == s)
100 1.1 itojun return;
101 1.1 itojun
102 1.1 itojun suspended = s;
103 1.1 itojun setproctitle("[%s] -s %d -f %s",
104 1.1 itojun suspended ? "suspended" : "running", cur_snaplen, filename);
105 1.1 itojun }
106 1.1 itojun
107 1.1 itojun char *
108 1.1 itojun copy_argv(char * const *argv)
109 1.1 itojun {
110 1.1 itojun size_t len = 0, n;
111 1.1 itojun char *buf;
112 1.1 itojun
113 1.1 itojun if (argv == NULL)
114 1.1 itojun return (NULL);
115 1.1 itojun
116 1.1 itojun for (n = 0; argv[n]; n++)
117 1.1 itojun len += strlen(argv[n])+1;
118 1.1 itojun if (len == 0)
119 1.1 itojun return (NULL);
120 1.1 itojun
121 1.1 itojun buf = malloc(len);
122 1.1 itojun if (buf == NULL)
123 1.1 itojun return (NULL);
124 1.1 itojun
125 1.1 itojun strlcpy(buf, argv[0], len);
126 1.1 itojun for (n = 1; argv[n]; n++) {
127 1.1 itojun strlcat(buf, " ", len);
128 1.1 itojun strlcat(buf, argv[n], len);
129 1.1 itojun }
130 1.1 itojun return (buf);
131 1.1 itojun }
132 1.1 itojun
133 1.1 itojun void
134 1.1 itojun logmsg(int pri, const char *message, ...)
135 1.1 itojun {
136 1.1 itojun va_list ap;
137 1.1 itojun va_start(ap, message);
138 1.1 itojun
139 1.1 itojun if (log_debug) {
140 1.1 itojun vfprintf(stderr, message, ap);
141 1.1 itojun fprintf(stderr, "\n");
142 1.1 itojun } else
143 1.1 itojun vsyslog(pri, message, ap);
144 1.1 itojun va_end(ap);
145 1.1 itojun }
146 1.1 itojun
147 1.1 itojun __dead void
148 1.1 itojun usage(void)
149 1.1 itojun {
150 1.1 itojun fprintf(stderr, "usage: pflogd [-Dx] [-d delay] [-f filename] ");
151 1.1 itojun fprintf(stderr, "[-s snaplen] [expression]\n");
152 1.1 itojun exit(1);
153 1.1 itojun }
154 1.1 itojun
155 1.1 itojun void
156 1.1 itojun sig_close(int sig)
157 1.1 itojun {
158 1.1 itojun gotsig_close = 1;
159 1.1 itojun }
160 1.1 itojun
161 1.1 itojun void
162 1.1 itojun sig_hup(int sig)
163 1.1 itojun {
164 1.1 itojun gotsig_hup = 1;
165 1.1 itojun }
166 1.1 itojun
167 1.1 itojun void
168 1.1 itojun sig_alrm(int sig)
169 1.1 itojun {
170 1.1 itojun gotsig_alrm = 1;
171 1.1 itojun }
172 1.1 itojun
173 1.1 itojun void
174 1.1 itojun set_pcap_filter(void)
175 1.1 itojun {
176 1.1 itojun struct bpf_program bprog;
177 1.1 itojun
178 1.1 itojun if (pcap_compile(hpcap, &bprog, filter, PCAP_OPT_FIL, 0) < 0)
179 1.1 itojun logmsg(LOG_WARNING, "%s", pcap_geterr(hpcap));
180 1.1 itojun else {
181 1.1 itojun if (pcap_setfilter(hpcap, &bprog) < 0)
182 1.1 itojun logmsg(LOG_WARNING, "%s", pcap_geterr(hpcap));
183 1.1 itojun pcap_freecode(&bprog);
184 1.1 itojun }
185 1.1 itojun }
186 1.1 itojun
187 1.1 itojun int
188 1.1 itojun init_pcap(void)
189 1.1 itojun {
190 1.1 itojun hpcap = pcap_open_live(interface, snaplen, 1, PCAP_TO_MS, errbuf);
191 1.1 itojun if (hpcap == NULL) {
192 1.1 itojun logmsg(LOG_ERR, "Failed to initialize: %s", errbuf);
193 1.1 itojun return (-1);
194 1.1 itojun }
195 1.1 itojun
196 1.1 itojun if (pcap_datalink(hpcap) != DLT_PFLOG) {
197 1.1 itojun logmsg(LOG_ERR, "Invalid datalink type");
198 1.1 itojun pcap_close(hpcap);
199 1.1 itojun hpcap = NULL;
200 1.1 itojun return (-1);
201 1.1 itojun }
202 1.1 itojun
203 1.1 itojun set_pcap_filter();
204 1.1 itojun
205 1.1 itojun cur_snaplen = snaplen = pcap_snapshot(hpcap);
206 1.1 itojun
207 1.1 itojun /* lock */
208 1.1 itojun if (ioctl(pcap_fileno(hpcap), BIOCLOCK) < 0) {
209 1.1 itojun logmsg(LOG_ERR, "BIOCLOCK: %s", strerror(errno));
210 1.1 itojun return (-1);
211 1.1 itojun }
212 1.1 itojun
213 1.1 itojun return (0);
214 1.1 itojun }
215 1.1 itojun
216 1.1 itojun int
217 1.1 itojun set_snaplen(int snap)
218 1.1 itojun {
219 1.1 itojun if (priv_set_snaplen(snap))
220 1.1 itojun return (1);
221 1.1 itojun
222 1.1 itojun if (cur_snaplen > snap)
223 1.1 itojun purge_buffer();
224 1.1 itojun
225 1.1 itojun cur_snaplen = snap;
226 1.1 itojun
227 1.1 itojun return (0);
228 1.1 itojun }
229 1.1 itojun
230 1.1 itojun int
231 1.1 itojun reset_dump(void)
232 1.1 itojun {
233 1.1 itojun struct pcap_file_header hdr;
234 1.1 itojun struct stat st;
235 1.1 itojun int fd;
236 1.1 itojun FILE *fp;
237 1.1 itojun
238 1.1 itojun if (hpcap == NULL)
239 1.1 itojun return (-1);
240 1.1 itojun
241 1.1 itojun if (dpcap) {
242 1.1 itojun flush_buffer(dpcap);
243 1.1 itojun fclose(dpcap);
244 1.1 itojun dpcap = NULL;
245 1.1 itojun }
246 1.1 itojun
247 1.1 itojun /*
248 1.1 itojun * Basically reimplement pcap_dump_open() because it truncates
249 1.1 itojun * files and duplicates headers and such.
250 1.1 itojun */
251 1.1 itojun fd = priv_open_log();
252 1.1 itojun if (fd < 0)
253 1.1 itojun return (1);
254 1.1 itojun
255 1.1 itojun fp = fdopen(fd, "a+");
256 1.1 itojun
257 1.1 itojun if (fp == NULL) {
258 1.1 itojun logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
259 1.1 itojun return (1);
260 1.1 itojun }
261 1.1 itojun if (fstat(fileno(fp), &st) == -1) {
262 1.1 itojun logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
263 1.1 itojun return (1);
264 1.1 itojun }
265 1.1 itojun
266 1.1 itojun /* set FILE unbuffered, we do our own buffering */
267 1.1 itojun if (setvbuf(fp, NULL, _IONBF, 0)) {
268 1.1 itojun logmsg(LOG_ERR, "Failed to set output buffers");
269 1.1 itojun return (1);
270 1.1 itojun }
271 1.1 itojun
272 1.1 itojun #define TCPDUMP_MAGIC 0xa1b2c3d4
273 1.1 itojun
274 1.1 itojun if (st.st_size == 0) {
275 1.1 itojun if (snaplen != cur_snaplen) {
276 1.1 itojun logmsg(LOG_NOTICE, "Using snaplen %d", snaplen);
277 1.1 itojun if (set_snaplen(snaplen)) {
278 1.1 itojun logmsg(LOG_WARNING,
279 1.1 itojun "Failed, using old settings");
280 1.1 itojun }
281 1.1 itojun }
282 1.1 itojun hdr.magic = TCPDUMP_MAGIC;
283 1.1 itojun hdr.version_major = PCAP_VERSION_MAJOR;
284 1.1 itojun hdr.version_minor = PCAP_VERSION_MINOR;
285 1.1 itojun hdr.thiszone = hpcap->tzoff;
286 1.1 itojun hdr.snaplen = hpcap->snapshot;
287 1.1 itojun hdr.sigfigs = 0;
288 1.1 itojun hdr.linktype = hpcap->linktype;
289 1.1 itojun
290 1.1 itojun if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
291 1.1 itojun fclose(fp);
292 1.1 itojun return (1);
293 1.1 itojun }
294 1.1 itojun } else if (scan_dump(fp, st.st_size)) {
295 1.1 itojun /* XXX move file and continue? */
296 1.1 itojun fclose(fp);
297 1.1 itojun return (1);
298 1.1 itojun }
299 1.1 itojun
300 1.1 itojun dpcap = fp;
301 1.1 itojun
302 1.1 itojun set_suspended(0);
303 1.1 itojun flush_buffer(fp);
304 1.1 itojun
305 1.1 itojun return (0);
306 1.1 itojun }
307 1.1 itojun
308 1.1 itojun int
309 1.1 itojun scan_dump(FILE *fp, off_t size)
310 1.1 itojun {
311 1.1 itojun struct pcap_file_header hdr;
312 1.1 itojun struct pcap_pkthdr ph;
313 1.1 itojun off_t pos;
314 1.1 itojun
315 1.1 itojun /*
316 1.1 itojun * Must read the file, compare the header against our new
317 1.1 itojun * options (in particular, snaplen) and adjust our options so
318 1.1 itojun * that we generate a correct file. Furthermore, check the file
319 1.1 itojun * for consistency so that we can append safely.
320 1.1 itojun *
321 1.1 itojun * XXX this may take a long time for large logs.
322 1.1 itojun */
323 1.1 itojun (void) fseek(fp, 0L, SEEK_SET);
324 1.1 itojun
325 1.1 itojun if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
326 1.1 itojun logmsg(LOG_ERR, "Short file header");
327 1.1 itojun return (1);
328 1.1 itojun }
329 1.1 itojun
330 1.1 itojun if (hdr.magic != TCPDUMP_MAGIC ||
331 1.1 itojun hdr.version_major != PCAP_VERSION_MAJOR ||
332 1.1 itojun hdr.version_minor != PCAP_VERSION_MINOR ||
333 1.1 itojun hdr.linktype != hpcap->linktype ||
334 1.1 itojun hdr.snaplen > PFLOGD_MAXSNAPLEN) {
335 1.1 itojun logmsg(LOG_ERR, "Invalid/incompatible log file, move it away");
336 1.1 itojun return (1);
337 1.1 itojun }
338 1.1 itojun
339 1.1 itojun pos = sizeof(hdr);
340 1.1 itojun
341 1.1 itojun while (!feof(fp)) {
342 1.1 itojun off_t len = fread((char *)&ph, 1, sizeof(ph), fp);
343 1.1 itojun if (len == 0)
344 1.1 itojun break;
345 1.1 itojun
346 1.1 itojun if (len != sizeof(ph))
347 1.1 itojun goto error;
348 1.1 itojun if (ph.caplen > hdr.snaplen || ph.caplen > PFLOGD_MAXSNAPLEN)
349 1.1 itojun goto error;
350 1.1 itojun pos += sizeof(ph) + ph.caplen;
351 1.1 itojun if (pos > size)
352 1.1 itojun goto error;
353 1.1 itojun fseek(fp, ph.caplen, SEEK_CUR);
354 1.1 itojun }
355 1.1 itojun
356 1.1 itojun if (pos != size)
357 1.1 itojun goto error;
358 1.1 itojun
359 1.1 itojun if (hdr.snaplen != cur_snaplen) {
360 1.1 itojun logmsg(LOG_WARNING,
361 1.1 itojun "Existing file has different snaplen %u, using it",
362 1.1 itojun hdr.snaplen);
363 1.1 itojun if (set_snaplen(hdr.snaplen)) {
364 1.1 itojun logmsg(LOG_WARNING,
365 1.1 itojun "Failed, using old settings, offset %llu",
366 1.1 itojun (unsigned long long) size);
367 1.1 itojun }
368 1.1 itojun }
369 1.1 itojun
370 1.1 itojun return (0);
371 1.1 itojun
372 1.1 itojun error:
373 1.1 itojun logmsg(LOG_ERR, "Corrupted log file.");
374 1.1 itojun return (1);
375 1.1 itojun }
376 1.1 itojun
377 1.1 itojun /* dump a packet directly to the stream, which is unbuffered */
378 1.1 itojun void
379 1.1 itojun dump_packet_nobuf(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
380 1.1 itojun {
381 1.1 itojun FILE *f = (FILE *)user;
382 1.1 itojun
383 1.1 itojun if (suspended) {
384 1.1 itojun packets_dropped++;
385 1.1 itojun return;
386 1.1 itojun }
387 1.1 itojun
388 1.1 itojun if (fwrite((char *)h, sizeof(*h), 1, f) != 1) {
389 1.1 itojun /* try to undo header to prevent corruption */
390 1.1 itojun off_t pos = ftello(f);
391 1.1 itojun if (pos < sizeof(*h) ||
392 1.1 itojun ftruncate(fileno(f), pos - sizeof(*h))) {
393 1.1 itojun logmsg(LOG_ERR, "Write failed, corrupted logfile!");
394 1.1 itojun set_suspended(1);
395 1.1 itojun gotsig_close = 1;
396 1.1 itojun return;
397 1.1 itojun }
398 1.1 itojun goto error;
399 1.1 itojun }
400 1.1 itojun
401 1.1 itojun if (fwrite((char *)sp, h->caplen, 1, f) != 1)
402 1.1 itojun goto error;
403 1.1 itojun
404 1.1 itojun return;
405 1.1 itojun
406 1.1 itojun error:
407 1.1 itojun set_suspended(1);
408 1.1 itojun packets_dropped ++;
409 1.1 itojun logmsg(LOG_ERR, "Logging suspended: fwrite: %s", strerror(errno));
410 1.1 itojun }
411 1.1 itojun
412 1.1 itojun int
413 1.1 itojun flush_buffer(FILE *f)
414 1.1 itojun {
415 1.1 itojun off_t offset;
416 1.1 itojun int len = bufpos - buffer;
417 1.1 itojun
418 1.1 itojun if (len <= 0)
419 1.1 itojun return (0);
420 1.1 itojun
421 1.1 itojun offset = ftello(f);
422 1.1 itojun if (offset == (off_t)-1) {
423 1.1 itojun set_suspended(1);
424 1.1 itojun logmsg(LOG_ERR, "Logging suspended: ftello: %s",
425 1.1 itojun strerror(errno));
426 1.1 itojun return (1);
427 1.1 itojun }
428 1.1 itojun
429 1.1 itojun if (fwrite(buffer, len, 1, f) != 1) {
430 1.1 itojun set_suspended(1);
431 1.1 itojun logmsg(LOG_ERR, "Logging suspended: fwrite: %s",
432 1.1 itojun strerror(errno));
433 1.1 itojun ftruncate(fileno(f), offset);
434 1.1 itojun return (1);
435 1.1 itojun }
436 1.1 itojun
437 1.1 itojun set_suspended(0);
438 1.1 itojun bufpos = buffer;
439 1.1 itojun bufleft = buflen;
440 1.1 itojun bufpkt = 0;
441 1.1 itojun
442 1.1 itojun return (0);
443 1.1 itojun }
444 1.1 itojun
445 1.1 itojun void
446 1.1 itojun purge_buffer(void)
447 1.1 itojun {
448 1.1 itojun packets_dropped += bufpkt;
449 1.1 itojun
450 1.1 itojun set_suspended(0);
451 1.1 itojun bufpos = buffer;
452 1.1 itojun bufleft = buflen;
453 1.1 itojun bufpkt = 0;
454 1.1 itojun }
455 1.1 itojun
456 1.1 itojun /* append packet to the buffer, flushing if necessary */
457 1.1 itojun void
458 1.1 itojun dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
459 1.1 itojun {
460 1.1 itojun FILE *f = (FILE *)user;
461 1.1 itojun size_t len = sizeof(*h) + h->caplen;
462 1.1 itojun
463 1.1 itojun if (len < sizeof(*h) || h->caplen > (size_t)cur_snaplen) {
464 1.1 itojun logmsg(LOG_NOTICE, "invalid size %u (%u/%u), packet dropped",
465 1.1 itojun len, cur_snaplen, snaplen);
466 1.1 itojun packets_dropped++;
467 1.1 itojun return;
468 1.1 itojun }
469 1.1 itojun
470 1.1 itojun if (len <= bufleft)
471 1.1 itojun goto append;
472 1.1 itojun
473 1.1 itojun if (suspended) {
474 1.1 itojun packets_dropped++;
475 1.1 itojun return;
476 1.1 itojun }
477 1.1 itojun
478 1.1 itojun if (flush_buffer(f)) {
479 1.1 itojun packets_dropped++;
480 1.1 itojun return;
481 1.1 itojun }
482 1.1 itojun
483 1.1 itojun if (len > bufleft) {
484 1.1 itojun dump_packet_nobuf(user, h, sp);
485 1.1 itojun return;
486 1.1 itojun }
487 1.1 itojun
488 1.1 itojun append:
489 1.1 itojun memcpy(bufpos, h, sizeof(*h));
490 1.1 itojun memcpy(bufpos + sizeof(*h), sp, h->caplen);
491 1.1 itojun
492 1.1 itojun bufpos += len;
493 1.1 itojun bufleft -= len;
494 1.1 itojun bufpkt++;
495 1.1 itojun
496 1.1 itojun return;
497 1.1 itojun }
498 1.1 itojun
499 1.1 itojun int
500 1.1 itojun main(int argc, char **argv)
501 1.1 itojun {
502 1.1 itojun struct pcap_stat pstat;
503 1.1 itojun int ch, np, Xflag = 0;
504 1.1 itojun pcap_handler phandler = dump_packet;
505 1.1 itojun
506 1.1 itojun closefrom(STDERR_FILENO + 1);
507 1.1 itojun
508 1.1 itojun while ((ch = getopt(argc, argv, "Dxd:s:f:")) != -1) {
509 1.1 itojun switch (ch) {
510 1.1 itojun case 'D':
511 1.1 itojun Debug = 1;
512 1.1 itojun break;
513 1.1 itojun case 'd':
514 1.1 itojun delay = atoi(optarg);
515 1.1 itojun if (delay < 5 || delay > 60*60)
516 1.1 itojun usage();
517 1.1 itojun break;
518 1.1 itojun case 'f':
519 1.1 itojun filename = optarg;
520 1.1 itojun break;
521 1.1 itojun case 's':
522 1.1 itojun snaplen = atoi(optarg);
523 1.1 itojun if (snaplen <= 0)
524 1.1 itojun snaplen = DEF_SNAPLEN;
525 1.1 itojun if (snaplen > PFLOGD_MAXSNAPLEN)
526 1.1 itojun snaplen = PFLOGD_MAXSNAPLEN;
527 1.1 itojun break;
528 1.1 itojun case 'x':
529 1.1 itojun Xflag++;
530 1.1 itojun break;
531 1.1 itojun default:
532 1.1 itojun usage();
533 1.1 itojun }
534 1.1 itojun
535 1.1 itojun }
536 1.1 itojun
537 1.1 itojun log_debug = Debug;
538 1.1 itojun argc -= optind;
539 1.1 itojun argv += optind;
540 1.1 itojun
541 1.1 itojun if (!Debug) {
542 1.1 itojun openlog("pflogd", LOG_PID | LOG_CONS, LOG_DAEMON);
543 1.1 itojun if (daemon(0, 0)) {
544 1.1 itojun logmsg(LOG_WARNING, "Failed to become daemon: %s",
545 1.1 itojun strerror(errno));
546 1.1 itojun }
547 1.1 itojun pidfile(NULL);
548 1.1 itojun }
549 1.1 itojun
550 1.1 itojun (void)umask(S_IRWXG | S_IRWXO);
551 1.1 itojun
552 1.1 itojun /* filter will be used by the privileged process */
553 1.1 itojun if (argc) {
554 1.1 itojun filter = copy_argv(argv);
555 1.1 itojun if (filter == NULL)
556 1.1 itojun logmsg(LOG_NOTICE, "Failed to form filter expression");
557 1.1 itojun }
558 1.1 itojun
559 1.1 itojun /* initialize pcap before dropping privileges */
560 1.1 itojun if (init_pcap()) {
561 1.1 itojun logmsg(LOG_ERR, "Exiting, init failure");
562 1.1 itojun exit(1);
563 1.1 itojun }
564 1.1 itojun
565 1.1 itojun /* Privilege separation begins here */
566 1.1 itojun if (priv_init()) {
567 1.1 itojun logmsg(LOG_ERR, "unable to privsep");
568 1.1 itojun exit(1);
569 1.1 itojun }
570 1.1 itojun
571 1.1 itojun setproctitle("[initializing]");
572 1.1 itojun /* Process is now unprivileged and inside a chroot */
573 1.1 itojun signal(SIGTERM, sig_close);
574 1.1 itojun signal(SIGINT, sig_close);
575 1.1 itojun signal(SIGQUIT, sig_close);
576 1.1 itojun signal(SIGALRM, sig_alrm);
577 1.1 itojun signal(SIGHUP, sig_hup);
578 1.1 itojun alarm(delay);
579 1.1 itojun
580 1.1 itojun buffer = malloc(PFLOGD_BUFSIZE);
581 1.1 itojun
582 1.1 itojun if (buffer == NULL) {
583 1.1 itojun logmsg(LOG_WARNING, "Failed to allocate output buffer");
584 1.1 itojun phandler = dump_packet_nobuf;
585 1.1 itojun } else {
586 1.1 itojun bufleft = buflen = PFLOGD_BUFSIZE;
587 1.1 itojun bufpos = buffer;
588 1.1 itojun bufpkt = 0;
589 1.1 itojun }
590 1.1 itojun
591 1.1 itojun if (reset_dump()) {
592 1.1 itojun if (Xflag)
593 1.1 itojun return (1);
594 1.1 itojun
595 1.1 itojun logmsg(LOG_ERR, "Logging suspended: open error");
596 1.1 itojun set_suspended(1);
597 1.1 itojun } else if (Xflag)
598 1.1 itojun return (0);
599 1.1 itojun
600 1.1 itojun while (1) {
601 1.1 itojun np = pcap_dispatch(hpcap, PCAP_NUM_PKTS,
602 1.1 itojun dump_packet, (u_char *)dpcap);
603 1.1 itojun if (np < 0)
604 1.1 itojun logmsg(LOG_NOTICE, "%s", pcap_geterr(hpcap));
605 1.1 itojun
606 1.1 itojun if (gotsig_close)
607 1.1 itojun break;
608 1.1 itojun if (gotsig_hup) {
609 1.1 itojun if (reset_dump()) {
610 1.1 itojun logmsg(LOG_ERR,
611 1.1 itojun "Logging suspended: open error");
612 1.1 itojun set_suspended(1);
613 1.1 itojun }
614 1.1 itojun gotsig_hup = 0;
615 1.1 itojun }
616 1.1 itojun
617 1.1 itojun if (gotsig_alrm) {
618 1.1 itojun if (dpcap)
619 1.1 itojun flush_buffer(dpcap);
620 1.1 itojun gotsig_alrm = 0;
621 1.1 itojun alarm(delay);
622 1.1 itojun }
623 1.1 itojun }
624 1.1 itojun
625 1.1 itojun logmsg(LOG_NOTICE, "Exiting");
626 1.1 itojun if (dpcap) {
627 1.1 itojun flush_buffer(dpcap);
628 1.1 itojun fclose(dpcap);
629 1.1 itojun }
630 1.1 itojun purge_buffer();
631 1.1 itojun
632 1.1 itojun if (pcap_stats(hpcap, &pstat) < 0)
633 1.1 itojun logmsg(LOG_WARNING, "Reading stats: %s", pcap_geterr(hpcap));
634 1.1 itojun else
635 1.1 itojun logmsg(LOG_NOTICE,
636 1.1 itojun "%u packets received, %u/%u dropped (kernel/pflogd)",
637 1.1 itojun pstat.ps_recv, pstat.ps_drop, packets_dropped);
638 1.1 itojun
639 1.1 itojun pcap_close(hpcap);
640 1.1 itojun if (!Debug)
641 1.1 itojun closelog();
642 1.1 itojun return (0);
643 1.1 itojun }
644