Home | History | Annotate | Line # | Download | only in pflogd
pflogd.c revision 1.1.1.3
      1  1.1.1.3   peter /*	$OpenBSD: pflogd.c,v 1.33 2005/02/09 12:09:30 henning 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.1.2    yamt 		close(fd);
    259      1.1  itojun 		logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
    260      1.1  itojun 		return (1);
    261      1.1  itojun 	}
    262      1.1  itojun 	if (fstat(fileno(fp), &st) == -1) {
    263  1.1.1.2    yamt 		fclose(fp);
    264      1.1  itojun 		logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
    265      1.1  itojun 		return (1);
    266      1.1  itojun 	}
    267      1.1  itojun 
    268      1.1  itojun 	/* set FILE unbuffered, we do our own buffering */
    269      1.1  itojun 	if (setvbuf(fp, NULL, _IONBF, 0)) {
    270  1.1.1.2    yamt 		fclose(fp);
    271      1.1  itojun 		logmsg(LOG_ERR, "Failed to set output buffers");
    272      1.1  itojun 		return (1);
    273      1.1  itojun 	}
    274      1.1  itojun 
    275      1.1  itojun #define TCPDUMP_MAGIC 0xa1b2c3d4
    276      1.1  itojun 
    277      1.1  itojun 	if (st.st_size == 0) {
    278      1.1  itojun 		if (snaplen != cur_snaplen) {
    279      1.1  itojun 			logmsg(LOG_NOTICE, "Using snaplen %d", snaplen);
    280      1.1  itojun 			if (set_snaplen(snaplen)) {
    281  1.1.1.2    yamt 				fclose(fp);
    282      1.1  itojun 				logmsg(LOG_WARNING,
    283      1.1  itojun 				    "Failed, using old settings");
    284      1.1  itojun 			}
    285      1.1  itojun 		}
    286      1.1  itojun 		hdr.magic = TCPDUMP_MAGIC;
    287      1.1  itojun 		hdr.version_major = PCAP_VERSION_MAJOR;
    288      1.1  itojun 		hdr.version_minor = PCAP_VERSION_MINOR;
    289      1.1  itojun 		hdr.thiszone = hpcap->tzoff;
    290      1.1  itojun 		hdr.snaplen = hpcap->snapshot;
    291      1.1  itojun 		hdr.sigfigs = 0;
    292      1.1  itojun 		hdr.linktype = hpcap->linktype;
    293      1.1  itojun 
    294      1.1  itojun 		if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
    295      1.1  itojun 			fclose(fp);
    296      1.1  itojun 			return (1);
    297      1.1  itojun 		}
    298      1.1  itojun 	} else if (scan_dump(fp, st.st_size)) {
    299      1.1  itojun 		/* XXX move file and continue? */
    300      1.1  itojun 		fclose(fp);
    301      1.1  itojun 		return (1);
    302      1.1  itojun 	}
    303      1.1  itojun 
    304      1.1  itojun 	dpcap = fp;
    305      1.1  itojun 
    306      1.1  itojun 	set_suspended(0);
    307      1.1  itojun 	flush_buffer(fp);
    308      1.1  itojun 
    309      1.1  itojun 	return (0);
    310      1.1  itojun }
    311      1.1  itojun 
    312      1.1  itojun int
    313      1.1  itojun scan_dump(FILE *fp, off_t size)
    314      1.1  itojun {
    315      1.1  itojun 	struct pcap_file_header hdr;
    316      1.1  itojun 	struct pcap_pkthdr ph;
    317      1.1  itojun 	off_t pos;
    318      1.1  itojun 
    319      1.1  itojun 	/*
    320      1.1  itojun 	 * Must read the file, compare the header against our new
    321      1.1  itojun 	 * options (in particular, snaplen) and adjust our options so
    322      1.1  itojun 	 * that we generate a correct file. Furthermore, check the file
    323      1.1  itojun 	 * for consistency so that we can append safely.
    324      1.1  itojun 	 *
    325      1.1  itojun 	 * XXX this may take a long time for large logs.
    326      1.1  itojun 	 */
    327      1.1  itojun 	(void) fseek(fp, 0L, SEEK_SET);
    328      1.1  itojun 
    329      1.1  itojun 	if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
    330      1.1  itojun 		logmsg(LOG_ERR, "Short file header");
    331      1.1  itojun 		return (1);
    332      1.1  itojun 	}
    333      1.1  itojun 
    334      1.1  itojun 	if (hdr.magic != TCPDUMP_MAGIC ||
    335      1.1  itojun 	    hdr.version_major != PCAP_VERSION_MAJOR ||
    336      1.1  itojun 	    hdr.version_minor != PCAP_VERSION_MINOR ||
    337      1.1  itojun 	    hdr.linktype != hpcap->linktype ||
    338      1.1  itojun 	    hdr.snaplen > PFLOGD_MAXSNAPLEN) {
    339      1.1  itojun 		logmsg(LOG_ERR, "Invalid/incompatible log file, move it away");
    340      1.1  itojun 		return (1);
    341      1.1  itojun 	}
    342      1.1  itojun 
    343      1.1  itojun 	pos = sizeof(hdr);
    344      1.1  itojun 
    345      1.1  itojun 	while (!feof(fp)) {
    346      1.1  itojun 		off_t len = fread((char *)&ph, 1, sizeof(ph), fp);
    347      1.1  itojun 		if (len == 0)
    348      1.1  itojun 			break;
    349      1.1  itojun 
    350      1.1  itojun 		if (len != sizeof(ph))
    351      1.1  itojun 			goto error;
    352      1.1  itojun 		if (ph.caplen > hdr.snaplen || ph.caplen > PFLOGD_MAXSNAPLEN)
    353      1.1  itojun 			goto error;
    354      1.1  itojun 		pos += sizeof(ph) + ph.caplen;
    355      1.1  itojun 		if (pos > size)
    356      1.1  itojun 			goto error;
    357      1.1  itojun 		fseek(fp, ph.caplen, SEEK_CUR);
    358      1.1  itojun 	}
    359      1.1  itojun 
    360      1.1  itojun 	if (pos != size)
    361      1.1  itojun 		goto error;
    362      1.1  itojun 
    363      1.1  itojun 	if (hdr.snaplen != cur_snaplen) {
    364      1.1  itojun 		logmsg(LOG_WARNING,
    365      1.1  itojun 		       "Existing file has different snaplen %u, using it",
    366      1.1  itojun 		       hdr.snaplen);
    367      1.1  itojun 		if (set_snaplen(hdr.snaplen)) {
    368      1.1  itojun 			logmsg(LOG_WARNING,
    369      1.1  itojun 			       "Failed, using old settings, offset %llu",
    370      1.1  itojun 			       (unsigned long long) size);
    371      1.1  itojun 		}
    372      1.1  itojun 	}
    373      1.1  itojun 
    374      1.1  itojun 	return (0);
    375      1.1  itojun 
    376      1.1  itojun  error:
    377      1.1  itojun 	logmsg(LOG_ERR, "Corrupted log file.");
    378      1.1  itojun 	return (1);
    379      1.1  itojun }
    380      1.1  itojun 
    381      1.1  itojun /* dump a packet directly to the stream, which is unbuffered */
    382      1.1  itojun void
    383      1.1  itojun dump_packet_nobuf(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
    384      1.1  itojun {
    385      1.1  itojun 	FILE *f = (FILE *)user;
    386      1.1  itojun 
    387      1.1  itojun 	if (suspended) {
    388      1.1  itojun 		packets_dropped++;
    389      1.1  itojun 		return;
    390      1.1  itojun 	}
    391      1.1  itojun 
    392      1.1  itojun 	if (fwrite((char *)h, sizeof(*h), 1, f) != 1) {
    393      1.1  itojun 		off_t pos = ftello(f);
    394  1.1.1.3   peter 
    395  1.1.1.3   peter 		/* try to undo header to prevent corruption */
    396      1.1  itojun 		if (pos < sizeof(*h) ||
    397      1.1  itojun 		    ftruncate(fileno(f), pos - sizeof(*h))) {
    398      1.1  itojun 			logmsg(LOG_ERR, "Write failed, corrupted logfile!");
    399      1.1  itojun 			set_suspended(1);
    400      1.1  itojun 			gotsig_close = 1;
    401      1.1  itojun 			return;
    402      1.1  itojun 		}
    403      1.1  itojun 		goto error;
    404      1.1  itojun 	}
    405      1.1  itojun 
    406      1.1  itojun 	if (fwrite((char *)sp, h->caplen, 1, f) != 1)
    407      1.1  itojun 		goto error;
    408      1.1  itojun 
    409      1.1  itojun 	return;
    410      1.1  itojun 
    411      1.1  itojun error:
    412      1.1  itojun 	set_suspended(1);
    413      1.1  itojun 	packets_dropped ++;
    414      1.1  itojun 	logmsg(LOG_ERR, "Logging suspended: fwrite: %s", strerror(errno));
    415      1.1  itojun }
    416      1.1  itojun 
    417      1.1  itojun int
    418      1.1  itojun flush_buffer(FILE *f)
    419      1.1  itojun {
    420      1.1  itojun 	off_t offset;
    421      1.1  itojun 	int len = bufpos - buffer;
    422      1.1  itojun 
    423      1.1  itojun 	if (len <= 0)
    424      1.1  itojun 		return (0);
    425      1.1  itojun 
    426      1.1  itojun 	offset = ftello(f);
    427      1.1  itojun 	if (offset == (off_t)-1) {
    428      1.1  itojun 		set_suspended(1);
    429      1.1  itojun 		logmsg(LOG_ERR, "Logging suspended: ftello: %s",
    430      1.1  itojun 		    strerror(errno));
    431      1.1  itojun 		return (1);
    432      1.1  itojun 	}
    433      1.1  itojun 
    434      1.1  itojun 	if (fwrite(buffer, len, 1, f) != 1) {
    435      1.1  itojun 		set_suspended(1);
    436      1.1  itojun 		logmsg(LOG_ERR, "Logging suspended: fwrite: %s",
    437      1.1  itojun 		    strerror(errno));
    438      1.1  itojun 		ftruncate(fileno(f), offset);
    439      1.1  itojun 		return (1);
    440      1.1  itojun 	}
    441      1.1  itojun 
    442      1.1  itojun 	set_suspended(0);
    443      1.1  itojun 	bufpos = buffer;
    444      1.1  itojun 	bufleft = buflen;
    445      1.1  itojun 	bufpkt = 0;
    446      1.1  itojun 
    447      1.1  itojun 	return (0);
    448      1.1  itojun }
    449      1.1  itojun 
    450      1.1  itojun void
    451      1.1  itojun purge_buffer(void)
    452      1.1  itojun {
    453      1.1  itojun 	packets_dropped += bufpkt;
    454      1.1  itojun 
    455      1.1  itojun 	set_suspended(0);
    456      1.1  itojun 	bufpos = buffer;
    457      1.1  itojun 	bufleft = buflen;
    458      1.1  itojun 	bufpkt = 0;
    459      1.1  itojun }
    460      1.1  itojun 
    461      1.1  itojun /* append packet to the buffer, flushing if necessary */
    462      1.1  itojun void
    463      1.1  itojun dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
    464      1.1  itojun {
    465      1.1  itojun 	FILE *f = (FILE *)user;
    466      1.1  itojun 	size_t len = sizeof(*h) + h->caplen;
    467      1.1  itojun 
    468      1.1  itojun 	if (len < sizeof(*h) || h->caplen > (size_t)cur_snaplen) {
    469      1.1  itojun 		logmsg(LOG_NOTICE, "invalid size %u (%u/%u), packet dropped",
    470      1.1  itojun 		       len, cur_snaplen, snaplen);
    471      1.1  itojun 		packets_dropped++;
    472      1.1  itojun 		return;
    473      1.1  itojun 	}
    474      1.1  itojun 
    475      1.1  itojun 	if (len <= bufleft)
    476      1.1  itojun 		goto append;
    477      1.1  itojun 
    478      1.1  itojun 	if (suspended) {
    479      1.1  itojun 		packets_dropped++;
    480      1.1  itojun 		return;
    481      1.1  itojun 	}
    482      1.1  itojun 
    483      1.1  itojun 	if (flush_buffer(f)) {
    484      1.1  itojun 		packets_dropped++;
    485      1.1  itojun 		return;
    486      1.1  itojun 	}
    487      1.1  itojun 
    488      1.1  itojun 	if (len > bufleft) {
    489      1.1  itojun 		dump_packet_nobuf(user, h, sp);
    490      1.1  itojun 		return;
    491      1.1  itojun 	}
    492      1.1  itojun 
    493  1.1.1.2    yamt  append:
    494      1.1  itojun 	memcpy(bufpos, h, sizeof(*h));
    495      1.1  itojun 	memcpy(bufpos + sizeof(*h), sp, h->caplen);
    496      1.1  itojun 
    497      1.1  itojun 	bufpos += len;
    498      1.1  itojun 	bufleft -= len;
    499      1.1  itojun 	bufpkt++;
    500      1.1  itojun 
    501      1.1  itojun 	return;
    502      1.1  itojun }
    503      1.1  itojun 
    504      1.1  itojun int
    505      1.1  itojun main(int argc, char **argv)
    506      1.1  itojun {
    507      1.1  itojun 	struct pcap_stat pstat;
    508      1.1  itojun 	int ch, np, Xflag = 0;
    509      1.1  itojun 	pcap_handler phandler = dump_packet;
    510  1.1.1.2    yamt 	const char *errstr = NULL;
    511      1.1  itojun 
    512      1.1  itojun 	closefrom(STDERR_FILENO + 1);
    513      1.1  itojun 
    514      1.1  itojun 	while ((ch = getopt(argc, argv, "Dxd:s:f:")) != -1) {
    515      1.1  itojun 		switch (ch) {
    516      1.1  itojun 		case 'D':
    517      1.1  itojun 			Debug = 1;
    518      1.1  itojun 			break;
    519      1.1  itojun 		case 'd':
    520  1.1.1.2    yamt 			delay = strtonum(optarg, 5, 60*60, &errstr);
    521  1.1.1.2    yamt 			if (errstr)
    522      1.1  itojun 				usage();
    523      1.1  itojun 			break;
    524      1.1  itojun 		case 'f':
    525      1.1  itojun 			filename = optarg;
    526      1.1  itojun 			break;
    527      1.1  itojun 		case 's':
    528  1.1.1.2    yamt 			snaplen = strtonum(optarg, 0, PFLOGD_MAXSNAPLEN,
    529  1.1.1.2    yamt 			    &errstr);
    530      1.1  itojun 			if (snaplen <= 0)
    531      1.1  itojun 				snaplen = DEF_SNAPLEN;
    532  1.1.1.2    yamt 			if (errstr)
    533      1.1  itojun 				snaplen = PFLOGD_MAXSNAPLEN;
    534      1.1  itojun 			break;
    535      1.1  itojun 		case 'x':
    536      1.1  itojun 			Xflag++;
    537      1.1  itojun 			break;
    538      1.1  itojun 		default:
    539      1.1  itojun 			usage();
    540      1.1  itojun 		}
    541      1.1  itojun 
    542      1.1  itojun 	}
    543      1.1  itojun 
    544      1.1  itojun 	log_debug = Debug;
    545      1.1  itojun 	argc -= optind;
    546      1.1  itojun 	argv += optind;
    547      1.1  itojun 
    548      1.1  itojun 	if (!Debug) {
    549      1.1  itojun 		openlog("pflogd", LOG_PID | LOG_CONS, LOG_DAEMON);
    550      1.1  itojun 		if (daemon(0, 0)) {
    551      1.1  itojun 			logmsg(LOG_WARNING, "Failed to become daemon: %s",
    552      1.1  itojun 			    strerror(errno));
    553      1.1  itojun 		}
    554      1.1  itojun 		pidfile(NULL);
    555      1.1  itojun 	}
    556      1.1  itojun 
    557  1.1.1.3   peter 	tzset();
    558      1.1  itojun 	(void)umask(S_IRWXG | S_IRWXO);
    559      1.1  itojun 
    560      1.1  itojun 	/* filter will be used by the privileged process */
    561      1.1  itojun 	if (argc) {
    562      1.1  itojun 		filter = copy_argv(argv);
    563      1.1  itojun 		if (filter == NULL)
    564      1.1  itojun 			logmsg(LOG_NOTICE, "Failed to form filter expression");
    565      1.1  itojun 	}
    566      1.1  itojun 
    567      1.1  itojun 	/* initialize pcap before dropping privileges */
    568      1.1  itojun 	if (init_pcap()) {
    569      1.1  itojun 		logmsg(LOG_ERR, "Exiting, init failure");
    570      1.1  itojun 		exit(1);
    571      1.1  itojun 	}
    572      1.1  itojun 
    573      1.1  itojun 	/* Privilege separation begins here */
    574      1.1  itojun 	if (priv_init()) {
    575      1.1  itojun 		logmsg(LOG_ERR, "unable to privsep");
    576      1.1  itojun 		exit(1);
    577      1.1  itojun 	}
    578      1.1  itojun 
    579      1.1  itojun 	setproctitle("[initializing]");
    580      1.1  itojun 	/* Process is now unprivileged and inside a chroot */
    581      1.1  itojun 	signal(SIGTERM, sig_close);
    582      1.1  itojun 	signal(SIGINT, sig_close);
    583      1.1  itojun 	signal(SIGQUIT, sig_close);
    584      1.1  itojun 	signal(SIGALRM, sig_alrm);
    585      1.1  itojun 	signal(SIGHUP, sig_hup);
    586      1.1  itojun 	alarm(delay);
    587      1.1  itojun 
    588      1.1  itojun 	buffer = malloc(PFLOGD_BUFSIZE);
    589      1.1  itojun 
    590      1.1  itojun 	if (buffer == NULL) {
    591      1.1  itojun 		logmsg(LOG_WARNING, "Failed to allocate output buffer");
    592      1.1  itojun 		phandler = dump_packet_nobuf;
    593      1.1  itojun 	} else {
    594      1.1  itojun 		bufleft = buflen = PFLOGD_BUFSIZE;
    595      1.1  itojun 		bufpos = buffer;
    596      1.1  itojun 		bufpkt = 0;
    597      1.1  itojun 	}
    598      1.1  itojun 
    599      1.1  itojun 	if (reset_dump()) {
    600      1.1  itojun 		if (Xflag)
    601      1.1  itojun 			return (1);
    602      1.1  itojun 
    603      1.1  itojun 		logmsg(LOG_ERR, "Logging suspended: open error");
    604      1.1  itojun 		set_suspended(1);
    605      1.1  itojun 	} else if (Xflag)
    606      1.1  itojun 		return (0);
    607      1.1  itojun 
    608      1.1  itojun 	while (1) {
    609      1.1  itojun 		np = pcap_dispatch(hpcap, PCAP_NUM_PKTS,
    610  1.1.1.3   peter 		    phandler, (u_char *)dpcap);
    611      1.1  itojun 		if (np < 0)
    612      1.1  itojun 			logmsg(LOG_NOTICE, "%s", pcap_geterr(hpcap));
    613      1.1  itojun 
    614      1.1  itojun 		if (gotsig_close)
    615      1.1  itojun 			break;
    616      1.1  itojun 		if (gotsig_hup) {
    617      1.1  itojun 			if (reset_dump()) {
    618      1.1  itojun 				logmsg(LOG_ERR,
    619      1.1  itojun 				    "Logging suspended: open error");
    620      1.1  itojun 				set_suspended(1);
    621      1.1  itojun 			}
    622      1.1  itojun 			gotsig_hup = 0;
    623      1.1  itojun 		}
    624      1.1  itojun 
    625      1.1  itojun 		if (gotsig_alrm) {
    626      1.1  itojun 			if (dpcap)
    627      1.1  itojun 				flush_buffer(dpcap);
    628      1.1  itojun 			gotsig_alrm = 0;
    629      1.1  itojun 			alarm(delay);
    630      1.1  itojun 		}
    631      1.1  itojun 	}
    632      1.1  itojun 
    633      1.1  itojun 	logmsg(LOG_NOTICE, "Exiting");
    634      1.1  itojun 	if (dpcap) {
    635      1.1  itojun 		flush_buffer(dpcap);
    636      1.1  itojun 		fclose(dpcap);
    637      1.1  itojun 	}
    638      1.1  itojun 	purge_buffer();
    639      1.1  itojun 
    640      1.1  itojun 	if (pcap_stats(hpcap, &pstat) < 0)
    641      1.1  itojun 		logmsg(LOG_WARNING, "Reading stats: %s", pcap_geterr(hpcap));
    642      1.1  itojun 	else
    643      1.1  itojun 		logmsg(LOG_NOTICE,
    644      1.1  itojun 		    "%u packets received, %u/%u dropped (kernel/pflogd)",
    645      1.1  itojun 		    pstat.ps_recv, pstat.ps_drop, packets_dropped);
    646      1.1  itojun 
    647      1.1  itojun 	pcap_close(hpcap);
    648      1.1  itojun 	if (!Debug)
    649      1.1  itojun 		closelog();
    650      1.1  itojun 	return (0);
    651      1.1  itojun }
    652