Home | History | Annotate | Line # | Download | only in syslogd
syslogd.c revision 1.46
      1 /*	$NetBSD: syslogd.c,v 1.46 2001/07/01 16:23:42 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)syslogd.c	8.3 (Berkeley) 4/4/94";
     45 #else
     46 __RCSID("$NetBSD: syslogd.c,v 1.46 2001/07/01 16:23:42 itojun Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 /*
     51  *  syslogd -- log system messages
     52  *
     53  * This program implements a system log. It takes a series of lines.
     54  * Each line may have a priority, signified as "<n>" as
     55  * the first characters of the line.  If this is
     56  * not present, a default priority is used.
     57  *
     58  * To kill syslogd, send a signal 15 (terminate).  A signal 1 (hup) will
     59  * cause it to reread its configuration file.
     60  *
     61  * Defined Constants:
     62  *
     63  * MAXLINE -- the maximimum line length that can be handled.
     64  * DEFUPRI -- the default priority for user messages
     65  * DEFSPRI -- the default priority for kernel messages
     66  *
     67  * Author: Eric Allman
     68  * extensive changes by Ralph Campbell
     69  * more extensive changes by Eric Allman (again)
     70  */
     71 
     72 #define	MAXLINE		1024		/* maximum line length */
     73 #define	MAXSVLINE	120		/* maximum saved line length */
     74 #define DEFUPRI		(LOG_USER|LOG_NOTICE)
     75 #define DEFSPRI		(LOG_KERN|LOG_CRIT)
     76 #define TIMERINTVL	30		/* interval for checking flush, mark */
     77 #define TTYMSGTIME	1		/* timeout passed to ttymsg */
     78 
     79 #include <sys/param.h>
     80 #include <sys/ioctl.h>
     81 #include <sys/stat.h>
     82 #include <sys/wait.h>
     83 #include <sys/socket.h>
     84 #include <sys/msgbuf.h>
     85 #include <sys/uio.h>
     86 #include <sys/poll.h>
     87 #include <sys/un.h>
     88 #include <sys/time.h>
     89 #include <sys/resource.h>
     90 #include <sys/sysctl.h>
     91 
     92 #include <netinet/in.h>
     93 #include <netdb.h>
     94 #include <arpa/inet.h>
     95 
     96 #include <ctype.h>
     97 #include <errno.h>
     98 #include <fcntl.h>
     99 #include <locale.h>
    100 #include <setjmp.h>
    101 #include <signal.h>
    102 #include <stdio.h>
    103 #include <stdlib.h>
    104 #include <string.h>
    105 #include <unistd.h>
    106 #include <utmp.h>
    107 #include <util.h>
    108 #include "pathnames.h"
    109 
    110 #define SYSLOG_NAMES
    111 #include <sys/syslog.h>
    112 
    113 #ifdef LIBWRAP
    114 #include <tcpd.h>
    115 
    116 int allow_severity = LOG_AUTH|LOG_INFO;
    117 int deny_severity = LOG_AUTH|LOG_WARNING;
    118 #endif
    119 
    120 char	*ConfFile = _PATH_LOGCONF;
    121 char	ctty[] = _PATH_CONSOLE;
    122 
    123 #define FDMASK(fd)	(1 << (fd))
    124 
    125 #define	dprintf		if (Debug) printf
    126 
    127 #define MAXUNAMES	20	/* maximum number of user names */
    128 
    129 /*
    130  * Flags to logmsg().
    131  */
    132 
    133 #define IGN_CONS	0x001	/* don't print on console */
    134 #define SYNC_FILE	0x002	/* do fsync on file after printing */
    135 #define ADDDATE		0x004	/* add a date to the message */
    136 #define MARK		0x008	/* this message is a mark */
    137 
    138 /*
    139  * This structure represents the files that will have log
    140  * copies printed.
    141  */
    142 
    143 struct filed {
    144 	struct	filed *f_next;		/* next in linked list */
    145 	short	f_type;			/* entry type, see below */
    146 	short	f_file;			/* file descriptor */
    147 	time_t	f_time;			/* time this was last written */
    148 	u_char	f_pmask[LOG_NFACILITIES+1];	/* priority mask */
    149 	union {
    150 		char	f_uname[MAXUNAMES][UT_NAMESIZE+1];
    151 		struct {
    152 			char	f_hname[MAXHOSTNAMELEN+1];
    153 			struct	addrinfo *f_addr;
    154 		} f_forw;		/* forwarding address */
    155 		char	f_fname[MAXPATHLEN];
    156 	} f_un;
    157 	char	f_prevline[MAXSVLINE];		/* last message logged */
    158 	char	f_lasttime[16];			/* time of last occurrence */
    159 	char	f_prevhost[MAXHOSTNAMELEN+1];	/* host from which recd. */
    160 	int	f_prevpri;			/* pri of f_prevline */
    161 	int	f_prevlen;			/* length of f_prevline */
    162 	int	f_prevcount;			/* repetition cnt of prevline */
    163 	int	f_repeatcount;			/* number of "repeated" msgs */
    164 };
    165 
    166 /*
    167  * Intervals at which we flush out "message repeated" messages,
    168  * in seconds after previous message is logged.  After each flush,
    169  * we move to the next interval until we reach the largest.
    170  */
    171 int	repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
    172 #define	MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
    173 #define	REPEATTIME(f)	((f)->f_time + repeatinterval[(f)->f_repeatcount])
    174 #define	BACKOFF(f)	{ if (++(f)->f_repeatcount > MAXREPEAT) \
    175 				 (f)->f_repeatcount = MAXREPEAT; \
    176 			}
    177 
    178 /* values for f_type */
    179 #define F_UNUSED	0		/* unused entry */
    180 #define F_FILE		1		/* regular file */
    181 #define F_TTY		2		/* terminal */
    182 #define F_CONSOLE	3		/* console terminal */
    183 #define F_FORW		4		/* remote machine */
    184 #define F_USERS		5		/* list of users */
    185 #define F_WALL		6		/* everyone logged on */
    186 
    187 char	*TypeNames[7] = {
    188 	"UNUSED",	"FILE",		"TTY",		"CONSOLE",
    189 	"FORW",		"USERS",	"WALL"
    190 };
    191 
    192 struct	filed *Files;
    193 struct	filed consfile;
    194 
    195 int	Debug;			/* debug flag */
    196 char	LocalHostName[MAXHOSTNAMELEN+1];	/* our hostname */
    197 char	*LocalDomain;		/* our local domain name */
    198 int	*finet = NULL;			/* Internet datagram sockets */
    199 int	Initialized = 0;	/* set when we have initialized ourselves */
    200 int	MarkInterval = 20 * 60;	/* interval between marks in seconds */
    201 int	MarkSeq = 0;		/* mark sequence number */
    202 int	SecureMode = 0;		/* listen only on unix domain socks */
    203 int	UseNameService = 1;	/* make domain name queries */
    204 int	NumForwards = 0;	/* number of forwarding actions in conf file */
    205 char	**LogPaths;		/* array of pathnames to read messages from */
    206 
    207 void	cfline __P((char *, struct filed *));
    208 char   *cvthname __P((struct sockaddr_storage *));
    209 int	decode __P((const char *, CODE *));
    210 void	die __P((int));
    211 void	domark __P((int));
    212 void	fprintlog __P((struct filed *, int, char *));
    213 int	getmsgbufsize __P((void));
    214 int*	socksetup __P((int));
    215 void	init __P((int));
    216 void	logerror __P((char *));
    217 void	logmsg __P((int, char *, char *, int));
    218 void	printline __P((char *, char *));
    219 void	printsys __P((char *));
    220 void	reapchild __P((int));
    221 void	usage __P((void));
    222 void	wallmsg __P((struct filed *, struct iovec *));
    223 int	main __P((int, char *[]));
    224 void	logpath_add __P((char ***, int *, int *, char *));
    225 void	logpath_fileadd __P((char ***, int *, int *, char *));
    226 
    227 int
    228 main(argc, argv)
    229 	int argc;
    230 	char *argv[];
    231 {
    232 	int ch, *funix, i, j, fklog, len, linesize;
    233 	int *nfinetix, nfklogix, nfunixbaseix, nfds;
    234 	int funixsize = 0, funixmaxsize = 0;
    235 	struct sockaddr_un sunx, fromunix;
    236 	struct sockaddr_storage frominet;
    237 	char *p, *line, **pp;
    238 	struct pollfd *readfds;
    239 
    240 	(void)setlocale(LC_ALL, "");
    241 
    242 	while ((ch = getopt(argc, argv, "dnsf:m:p:P:")) != -1)
    243 		switch(ch) {
    244 		case 'd':		/* debug */
    245 			Debug++;
    246 			break;
    247 		case 'f':		/* configuration file */
    248 			ConfFile = optarg;
    249 			break;
    250 		case 'm':		/* mark interval */
    251 			MarkInterval = atoi(optarg) * 60;
    252 			break;
    253 		case 'n':		/* turn off DNS queries */
    254 			UseNameService = 0;
    255 			break;
    256 		case 'p':		/* path */
    257 			logpath_add(&LogPaths, &funixsize,
    258 			    &funixmaxsize, optarg);
    259 			break;
    260 		case 'P':		/* file of paths */
    261 			logpath_fileadd(&LogPaths, &funixsize,
    262 			    &funixmaxsize, optarg);
    263 			break;
    264 		case 's':		/* no network listen mode */
    265 			SecureMode++;
    266 			break;
    267 		case '?':
    268 		default:
    269 			usage();
    270 		}
    271 	if ((argc -= optind) != 0)
    272 		usage();
    273 
    274 	if (!Debug)
    275 		(void)daemon(0, 0);
    276 	else
    277 		setlinebuf(stdout);
    278 
    279 	consfile.f_type = F_CONSOLE;
    280 	(void)strcpy(consfile.f_un.f_fname, ctty);
    281 	(void)gethostname(LocalHostName, sizeof(LocalHostName));
    282 	LocalHostName[sizeof(LocalHostName) - 1] = '\0';
    283 	if ((p = strchr(LocalHostName, '.')) != NULL) {
    284 		*p++ = '\0';
    285 		LocalDomain = p;
    286 	} else
    287 		LocalDomain = "";
    288 	linesize = getmsgbufsize();
    289 	if (linesize < MAXLINE)
    290 		linesize = MAXLINE;
    291 	linesize++;
    292 	line = malloc(linesize);
    293 	if (line == NULL) {
    294 		logerror("couldn't allocate line buffer");
    295 		die(0);
    296 	}
    297 	(void)signal(SIGTERM, die);
    298 	(void)signal(SIGINT, Debug ? die : SIG_IGN);
    299 	(void)signal(SIGQUIT, Debug ? die : SIG_IGN);
    300 	(void)signal(SIGCHLD, reapchild);
    301 	(void)signal(SIGALRM, domark);
    302 	(void)alarm(TIMERINTVL);
    303 
    304 #ifndef SUN_LEN
    305 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    306 #endif
    307 	if (funixsize == 0)
    308 		logpath_add(&LogPaths, &funixsize,
    309 		    &funixmaxsize, _PATH_LOG);
    310 	funix = (int *)malloc(sizeof(int) * funixsize);
    311 	if (funix == NULL) {
    312 		logerror("couldn't allocate funix descriptors");
    313 		die(0);
    314 	}
    315 	for (j = 0, pp = LogPaths; *pp; pp++, j++) {
    316 		dprintf("making unix dgram socket %s\n", *pp);
    317 		unlink(*pp);
    318 		memset(&sunx, 0, sizeof(sunx));
    319 		sunx.sun_family = AF_LOCAL;
    320 		(void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path));
    321 		funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0);
    322 		if (funix[j] < 0 || bind(funix[j],
    323 		    (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
    324 		    chmod(*pp, 0666) < 0) {
    325 			int serrno = errno;
    326 			(void)snprintf(line, sizeof line,
    327 			    "cannot create %s", *pp);
    328 			errno = serrno;
    329 			logerror(line);
    330 			errno = serrno;
    331 			dprintf("cannot create %s (%d)\n", *pp, errno);
    332 			die(0);
    333 		}
    334 		dprintf("listening on unix dgram socket %s\n", *pp);
    335 	}
    336 
    337 	init(0);
    338 
    339 	if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) {
    340 		dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
    341 	} else {
    342 		dprintf("listening on kernel log %s\n", _PATH_KLOG);
    343 	}
    344 
    345 	/* tuck my process id away, if i'm not in debug mode */
    346 	if (Debug == 0)
    347 		pidfile(NULL);
    348 
    349 	dprintf("off & running....\n");
    350 
    351 	(void)signal(SIGHUP, init);
    352 
    353 	/* setup pollfd set. */
    354 	readfds = (struct pollfd *)malloc(sizeof(struct pollfd) *
    355 			(funixsize + (finet ? *finet : 0) + 1));
    356 	if (readfds == NULL) {
    357 		logerror("couldn't allocate pollfds");
    358 		die(0);
    359 	}
    360 	nfds = 0;
    361 	if (fklog >= 0) {
    362 		nfklogix = nfds++;
    363 		readfds[nfklogix].fd = fklog;
    364 		readfds[nfklogix].events = POLLIN | POLLPRI;
    365 	}
    366 	if (finet && !SecureMode) {
    367 		nfinetix = malloc(*finet * sizeof(*nfinetix));
    368 		for (j = 0; j < *finet; j++) {
    369 			nfinetix[j] = nfds++;
    370 			readfds[nfinetix[j]].fd = finet[j+1];
    371 			readfds[nfinetix[j]].events = POLLIN | POLLPRI;
    372 		}
    373 	}
    374 	nfunixbaseix = nfds;
    375 	for (j = 0, pp = LogPaths; *pp; pp++) {
    376 		readfds[nfds].fd = funix[j++];
    377 		readfds[nfds++].events = POLLIN | POLLPRI;
    378 	}
    379 
    380 	for (;;) {
    381 		int rv;
    382 
    383 		rv = poll(readfds, nfds, INFTIM);
    384 		if (rv == 0)
    385 			continue;
    386 		if (rv < 0) {
    387 			if (errno != EINTR)
    388 				logerror("poll");
    389 			continue;
    390 		}
    391 		dprintf("got a message (%d)\n", rv);
    392 		if (fklog >= 0 &&
    393 		    (readfds[nfklogix].revents & (POLLIN | POLLPRI))) {
    394 			dprintf("kernel log active\n");
    395 			i = read(fklog, line, linesize - 1);
    396 			if (i > 0) {
    397 				line[i] = '\0';
    398 				printsys(line);
    399 			} else if (i < 0 && errno != EINTR) {
    400 				logerror("klog");
    401 				fklog = -1;
    402 			}
    403 		}
    404 		for (j = 0, pp = LogPaths; *pp; pp++, j++) {
    405 			if ((readfds[nfunixbaseix + j].revents &
    406 			    (POLLIN | POLLPRI)) == 0)
    407 				continue;
    408 
    409 			dprintf("unix socket (%s) active\n", *pp);
    410 			len = sizeof(fromunix);
    411 			i = recvfrom(funix[j], line, MAXLINE, 0,
    412 			    (struct sockaddr *)&fromunix, &len);
    413 			if (i > 0) {
    414 				line[i] = '\0';
    415 				printline(LocalHostName, line);
    416 			} else if (i < 0 && errno != EINTR) {
    417 				char buf[MAXPATHLEN];
    418 				int serrno = errno;
    419 
    420 				(void)snprintf(buf, sizeof buf,
    421 				    "recvfrom unix %s", *pp);
    422 				errno = serrno;
    423 				logerror(buf);
    424 			}
    425 		}
    426 		if (finet && !SecureMode) {
    427 			for (j = 0; j < *finet; j++) {
    428 		    		if (readfds[nfinetix[j]].revents &
    429 				    (POLLIN | POLLPRI)) {
    430 #ifdef LIBWRAP
    431 					struct request_info req;
    432 #endif
    433 					int reject = 0;
    434 
    435 					dprintf("inet socket active\n");
    436 
    437 #ifdef LIBWRAP
    438 					request_init(&req, RQ_DAEMON, "syslogd",
    439 					    RQ_FILE, finet[j + 1], NULL);
    440 					fromhost(&req);
    441 					reject = !hosts_access(&req);
    442 					if (reject)
    443 						dprintf("access denied\n");
    444 #endif
    445 
    446 					len = sizeof(frominet);
    447 					i = recvfrom(finet[j+1], line, MAXLINE,
    448 					    0, (struct sockaddr *)&frominet,
    449 					    &len);
    450 					if (i == 0 || (i < 0 && errno == EINTR))
    451 						continue;
    452 					else if (i < 0) {
    453 						logerror("recvfrom inet");
    454 						continue;
    455 					}
    456 
    457 					line[i] = '\0';
    458 					if (!reject)
    459 						printline(cvthname(&frominet),
    460 						    line);
    461 				}
    462 			}
    463 		}
    464 	}
    465 }
    466 
    467 void
    468 usage()
    469 {
    470 
    471 	(void)fprintf(stderr,
    472 "usage: %s [-ds] [-f conffile] [-m markinterval] [-P logpathfile] [-p logpath1] [-p logpath2 ..]\n",
    473 	    getprogname());
    474 	exit(1);
    475 }
    476 
    477 /*
    478  * given a pointer to an array of char *'s, a pointer to it's current
    479  * size and current allocated max size, and a new char * to add, add
    480  * it, update everything as necessary, possibly allocating a new array
    481  */
    482 void
    483 logpath_add(lp, szp, maxszp, new)
    484 	char ***lp;
    485 	int *szp;
    486 	int *maxszp;
    487 	char *new;
    488 {
    489 
    490 	dprintf("adding %s to the %p logpath list\n", new, *lp);
    491 	if (*szp == *maxszp) {
    492 		if (*maxszp == 0) {
    493 			*maxszp = 4;	/* start of with enough for now */
    494 			*lp = NULL;
    495 		}
    496 		else
    497 			*maxszp *= 2;
    498 		*lp = realloc(*lp, sizeof(char *) * (*maxszp + 1));
    499 		if (*lp == NULL) {
    500 			logerror("couldn't allocate line buffer");
    501 			die(0);
    502 		}
    503 	}
    504 	(*lp)[(*szp)++] = new;
    505 	(*lp)[(*szp)] = NULL;		/* always keep it NULL terminated */
    506 }
    507 
    508 /* do a file of log sockets */
    509 void
    510 logpath_fileadd(lp, szp, maxszp, file)
    511 	char ***lp;
    512 	int *szp;
    513 	int *maxszp;
    514 	char *file;
    515 {
    516 	FILE *fp;
    517 	char *line;
    518 	size_t len;
    519 
    520 	fp = fopen(file, "r");
    521 	if (fp == NULL) {
    522 		int serrno = errno;
    523 
    524 		dprintf("can't open %s (%d)\n", file, errno);
    525 		errno = serrno;
    526 		logerror("could not open socket file list");
    527 		die(0);
    528 	}
    529 
    530 	while ((line = fgetln(fp, &len))) {
    531 		line[len - 1] = 0;
    532 		logpath_add(lp, szp, maxszp, line);
    533 	}
    534 	fclose(fp);
    535 }
    536 
    537 /*
    538  * Take a raw input line, decode the message, and print the message
    539  * on the appropriate log files.
    540  */
    541 void
    542 printline(hname, msg)
    543 	char *hname;
    544 	char *msg;
    545 {
    546 	int c, pri;
    547 	char *p, *q, line[MAXLINE + 1];
    548 
    549 	/* test for special codes */
    550 	pri = DEFUPRI;
    551 	p = msg;
    552 	if (*p == '<') {
    553 		pri = 0;
    554 		while (isdigit(*++p))
    555 			pri = 10 * pri + (*p - '0');
    556 		if (*p == '>')
    557 			++p;
    558 	}
    559 	if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    560 		pri = DEFUPRI;
    561 
    562 	/* don't allow users to log kernel messages */
    563 	if (LOG_FAC(pri) == LOG_KERN)
    564 		pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
    565 
    566 	q = line;
    567 
    568 	while ((c = *p++) != '\0' &&
    569 	    q < &line[sizeof(line) - 2]) {
    570 		c &= 0177;
    571 		if (iscntrl(c))
    572 			if (c == '\n')
    573 				*q++ = ' ';
    574 			else if (c == '\t')
    575 				*q++ = '\t';
    576 			else {
    577 				*q++ = '^';
    578 				*q++ = c ^ 0100;
    579 			}
    580 		else
    581 			*q++ = c;
    582 	}
    583 	*q = '\0';
    584 
    585 	logmsg(pri, line, hname, 0);
    586 }
    587 
    588 /*
    589  * Take a raw input line from /dev/klog, split and format similar to syslog().
    590  */
    591 void
    592 printsys(msg)
    593 	char *msg;
    594 {
    595 	int c, pri, flags;
    596 	char *lp, *p, *q, line[MAXLINE + 1];
    597 
    598 	(void)strcpy(line, _PATH_UNIX);
    599 	(void)strcat(line, ": ");
    600 	lp = line + strlen(line);
    601 	for (p = msg; *p != '\0'; ) {
    602 		flags = SYNC_FILE | ADDDATE;	/* fsync file after write */
    603 		pri = DEFSPRI;
    604 		if (*p == '<') {
    605 			pri = 0;
    606 			while (isdigit(*++p))
    607 				pri = 10 * pri + (*p - '0');
    608 			if (*p == '>')
    609 				++p;
    610 		} else {
    611 			/* kernel printf's come out on console */
    612 			flags |= IGN_CONS;
    613 		}
    614 		if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
    615 			pri = DEFSPRI;
    616 		q = lp;
    617 		while (*p != '\0' && (c = *p++) != '\n' &&
    618 		    q < &line[MAXLINE])
    619 			*q++ = c;
    620 		*q = '\0';
    621 		logmsg(pri, line, LocalHostName, flags);
    622 	}
    623 }
    624 
    625 time_t	now;
    626 
    627 /*
    628  * Log a message to the appropriate log files, users, etc. based on
    629  * the priority.
    630  */
    631 void
    632 logmsg(pri, msg, from, flags)
    633 	int pri;
    634 	char *msg, *from;
    635 	int flags;
    636 {
    637 	struct filed *f;
    638 	int fac, msglen, omask, prilev;
    639 	char *timestamp;
    640 
    641 	dprintf("logmsg: pri 0%o, flags 0x%x, from %s, msg %s\n",
    642 	    pri, flags, from, msg);
    643 
    644 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
    645 
    646 	/*
    647 	 * Check to see if msg looks non-standard.
    648 	 */
    649 	msglen = strlen(msg);
    650 	if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
    651 	    msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
    652 		flags |= ADDDATE;
    653 
    654 	(void)time(&now);
    655 	if (flags & ADDDATE)
    656 		timestamp = ctime(&now) + 4;
    657 	else {
    658 		timestamp = msg;
    659 		msg += 16;
    660 		msglen -= 16;
    661 	}
    662 
    663 	/* extract facility and priority level */
    664 	if (flags & MARK)
    665 		fac = LOG_NFACILITIES;
    666 	else
    667 		fac = LOG_FAC(pri);
    668 	prilev = LOG_PRI(pri);
    669 
    670 	/* log the message to the particular outputs */
    671 	if (!Initialized) {
    672 		f = &consfile;
    673 		f->f_file = open(ctty, O_WRONLY, 0);
    674 
    675 		if (f->f_file >= 0) {
    676 			fprintlog(f, flags, msg);
    677 			(void)close(f->f_file);
    678 		}
    679 		(void)sigsetmask(omask);
    680 		return;
    681 	}
    682 	for (f = Files; f; f = f->f_next) {
    683 		/* skip messages that are incorrect priority */
    684 		if (f->f_pmask[fac] < prilev ||
    685 		    f->f_pmask[fac] == INTERNAL_NOPRI)
    686 			continue;
    687 
    688 		if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
    689 			continue;
    690 
    691 		/* don't output marks to recently written files */
    692 		if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
    693 			continue;
    694 
    695 		/*
    696 		 * suppress duplicate lines to this file
    697 		 */
    698 		if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
    699 		    !strcmp(msg, f->f_prevline) &&
    700 		    !strcmp(from, f->f_prevhost)) {
    701 			(void)strncpy(f->f_lasttime, timestamp, 15);
    702 			f->f_prevcount++;
    703 			dprintf("msg repeated %d times, %ld sec of %d\n",
    704 			    f->f_prevcount, (long)(now - f->f_time),
    705 			    repeatinterval[f->f_repeatcount]);
    706 			/*
    707 			 * If domark would have logged this by now,
    708 			 * flush it now (so we don't hold isolated messages),
    709 			 * but back off so we'll flush less often
    710 			 * in the future.
    711 			 */
    712 			if (now > REPEATTIME(f)) {
    713 				fprintlog(f, flags, (char *)NULL);
    714 				BACKOFF(f);
    715 			}
    716 		} else {
    717 			/* new line, save it */
    718 			if (f->f_prevcount)
    719 				fprintlog(f, 0, (char *)NULL);
    720 			f->f_repeatcount = 0;
    721 			f->f_prevpri = pri;
    722 			(void)strncpy(f->f_lasttime, timestamp, 15);
    723 			(void)strncpy(f->f_prevhost, from,
    724 					sizeof(f->f_prevhost));
    725 			if (msglen < MAXSVLINE) {
    726 				f->f_prevlen = msglen;
    727 				(void)strcpy(f->f_prevline, msg);
    728 				fprintlog(f, flags, (char *)NULL);
    729 			} else {
    730 				f->f_prevline[0] = 0;
    731 				f->f_prevlen = 0;
    732 				fprintlog(f, flags, msg);
    733 			}
    734 		}
    735 	}
    736 	(void)sigsetmask(omask);
    737 }
    738 
    739 void
    740 fprintlog(f, flags, msg)
    741 	struct filed *f;
    742 	int flags;
    743 	char *msg;
    744 {
    745 	struct iovec iov[6];
    746 	struct iovec *v;
    747 	struct addrinfo *r;
    748 	int j, l, lsent;
    749 	char line[MAXLINE + 1], repbuf[80], greetings[200];
    750 
    751 	v = iov;
    752 	if (f->f_type == F_WALL) {
    753 		v->iov_base = greetings;
    754 		v->iov_len = snprintf(greetings, sizeof greetings,
    755 		    "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
    756 		    f->f_prevhost, ctime(&now));
    757 		v++;
    758 		v->iov_base = "";
    759 		v->iov_len = 0;
    760 		v++;
    761 	} else {
    762 		v->iov_base = f->f_lasttime;
    763 		v->iov_len = 15;
    764 		v++;
    765 		v->iov_base = " ";
    766 		v->iov_len = 1;
    767 		v++;
    768 	}
    769 	v->iov_base = f->f_prevhost;
    770 	v->iov_len = strlen(v->iov_base);
    771 	v++;
    772 	v->iov_base = " ";
    773 	v->iov_len = 1;
    774 	v++;
    775 
    776 	if (msg) {
    777 		v->iov_base = msg;
    778 		v->iov_len = strlen(msg);
    779 	} else if (f->f_prevcount > 1) {
    780 		v->iov_base = repbuf;
    781 		v->iov_len = snprintf(repbuf, sizeof repbuf,
    782 		    "last message repeated %d times", f->f_prevcount);
    783 	} else {
    784 		v->iov_base = f->f_prevline;
    785 		v->iov_len = f->f_prevlen;
    786 	}
    787 	v++;
    788 
    789 	dprintf("Logging to %s", TypeNames[f->f_type]);
    790 	f->f_time = now;
    791 
    792 	switch (f->f_type) {
    793 	case F_UNUSED:
    794 		dprintf("\n");
    795 		break;
    796 
    797 	case F_FORW:
    798 		dprintf(" %s\n", f->f_un.f_forw.f_hname);
    799 			/*
    800 			 * check for local vs remote messages
    801 			 * (from FreeBSD PR#bin/7055)
    802 			 */
    803 		if (strcmp(f->f_prevhost, LocalHostName)) {
    804 			l = snprintf(line, sizeof(line) - 1,
    805 				     "<%d>%.15s [%s]: %s",
    806 				     f->f_prevpri, (char *) iov[0].iov_base,
    807 				     f->f_prevhost, (char *) iov[4].iov_base);
    808 		} else {
    809 			l = snprintf(line, sizeof(line) - 1, "<%d>%.15s %s",
    810 				     f->f_prevpri, (char *) iov[0].iov_base,
    811 				     (char *) iov[4].iov_base);
    812 		}
    813 		if (l > MAXLINE)
    814 			l = MAXLINE;
    815 		if (finet) {
    816 			for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) {
    817 				for (j = 0; j < *finet; j++) {
    818 #if 0
    819 					/*
    820 					 * should we check AF first, or just
    821 					 * trial and error? FWD
    822 					 */
    823 					if (r->ai_family ==
    824 					    address_family_of(finet[j+1]))
    825 #endif
    826 					lsent = sendto(finet[j+1], line, l, 0,
    827 					    r->ai_addr, r->ai_addrlen);
    828 					if (lsent == l)
    829 						break;
    830 				}
    831 			}
    832 			if (lsent != l) {
    833 				f->f_type = F_UNUSED;
    834 				logerror("sendto");
    835 			}
    836 		}
    837 		break;
    838 
    839 	case F_CONSOLE:
    840 		if (flags & IGN_CONS) {
    841 			dprintf(" (ignored)\n");
    842 			break;
    843 		}
    844 		/* FALLTHROUGH */
    845 
    846 	case F_TTY:
    847 	case F_FILE:
    848 		dprintf(" %s\n", f->f_un.f_fname);
    849 		if (f->f_type != F_FILE) {
    850 			v->iov_base = "\r\n";
    851 			v->iov_len = 2;
    852 		} else {
    853 			v->iov_base = "\n";
    854 			v->iov_len = 1;
    855 		}
    856 	again:
    857 		if (writev(f->f_file, iov, 6) < 0) {
    858 			int e = errno;
    859 			(void)close(f->f_file);
    860 			/*
    861 			 * Check for errors on TTY's due to loss of tty
    862 			 */
    863 			if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
    864 				f->f_file = open(f->f_un.f_fname,
    865 				    O_WRONLY|O_APPEND, 0);
    866 				if (f->f_file < 0) {
    867 					f->f_type = F_UNUSED;
    868 					logerror(f->f_un.f_fname);
    869 				} else
    870 					goto again;
    871 			} else {
    872 				f->f_type = F_UNUSED;
    873 				errno = e;
    874 				logerror(f->f_un.f_fname);
    875 			}
    876 		} else if (flags & SYNC_FILE)
    877 			(void)fsync(f->f_file);
    878 		break;
    879 
    880 	case F_USERS:
    881 	case F_WALL:
    882 		dprintf("\n");
    883 		v->iov_base = "\r\n";
    884 		v->iov_len = 2;
    885 		wallmsg(f, iov);
    886 		break;
    887 	}
    888 	f->f_prevcount = 0;
    889 }
    890 
    891 /*
    892  *  WALLMSG -- Write a message to the world at large
    893  *
    894  *	Write the specified message to either the entire
    895  *	world, or a list of approved users.
    896  */
    897 void
    898 wallmsg(f, iov)
    899 	struct filed *f;
    900 	struct iovec *iov;
    901 {
    902 	static int reenter;			/* avoid calling ourselves */
    903 	FILE *uf;
    904 	struct utmp ut;
    905 	int i;
    906 	char *p;
    907 	char line[sizeof(ut.ut_line) + 1];
    908 
    909 	if (reenter++)
    910 		return;
    911 	if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
    912 		logerror(_PATH_UTMP);
    913 		reenter = 0;
    914 		return;
    915 	}
    916 	/* NOSTRICT */
    917 	while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) {
    918 		if (ut.ut_name[0] == '\0')
    919 			continue;
    920 		strncpy(line, ut.ut_line, sizeof(ut.ut_line));
    921 		line[sizeof(ut.ut_line)] = '\0';
    922 		if (f->f_type == F_WALL) {
    923 			if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL) {
    924 				errno = 0;	/* already in msg */
    925 				logerror(p);
    926 			}
    927 			continue;
    928 		}
    929 		/* should we send the message to this user? */
    930 		for (i = 0; i < MAXUNAMES; i++) {
    931 			if (!f->f_un.f_uname[i][0])
    932 				break;
    933 			if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
    934 			    UT_NAMESIZE)) {
    935 				if ((p = ttymsg(iov, 6, line, TTYMSGTIME))
    936 								!= NULL) {
    937 					errno = 0;	/* already in msg */
    938 					logerror(p);
    939 				}
    940 				break;
    941 			}
    942 		}
    943 	}
    944 	(void)fclose(uf);
    945 	reenter = 0;
    946 }
    947 
    948 void
    949 reapchild(signo)
    950 	int signo;
    951 {
    952 	union wait status;
    953 
    954 	while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0)
    955 		;
    956 }
    957 
    958 /*
    959  * Return a printable representation of a host address.
    960  */
    961 char *
    962 cvthname(f)
    963 	struct sockaddr_storage *f;
    964 {
    965 	int error;
    966 	char *p;
    967 #ifdef KAME_SCOPEID
    968 	const int niflag = NI_DGRAM | NI_WITHSCOPEID;
    969 #else
    970 	const int niflag = NI_DGRAM;
    971 #endif
    972 	static char host[NI_MAXHOST], ip[NI_MAXHOST];
    973 
    974 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
    975 			ip, sizeof ip, NULL, 0, NI_NUMERICHOST|niflag);
    976 
    977 	dprintf("cvthname(%s)\n", ip);
    978 
    979 	if (error) {
    980 		dprintf("Malformed from address %s\n", gai_strerror(error));
    981 		return ("???");
    982 	}
    983 
    984 	if (!UseNameService)
    985 		return (ip);
    986 
    987 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
    988 			host, sizeof host, NULL, 0, niflag);
    989 	if (error) {
    990 		dprintf("Host name for your address (%s) unknown\n", ip);
    991 		return (ip);
    992 	}
    993 	if ((p = strchr(host, '.')) && strcmp(p + 1, LocalDomain) == 0)
    994 		*p = '\0';
    995 	return (host);
    996 }
    997 
    998 void
    999 domark(signo)
   1000 	int signo;
   1001 {
   1002 	struct filed *f;
   1003 
   1004 	now = time((time_t *)NULL);
   1005 	MarkSeq += TIMERINTVL;
   1006 	if (MarkSeq >= MarkInterval) {
   1007 		logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
   1008 		MarkSeq = 0;
   1009 	}
   1010 
   1011 	for (f = Files; f; f = f->f_next) {
   1012 		if (f->f_prevcount && now >= REPEATTIME(f)) {
   1013 			dprintf("flush %s: repeated %d times, %d sec.\n",
   1014 			    TypeNames[f->f_type], f->f_prevcount,
   1015 			    repeatinterval[f->f_repeatcount]);
   1016 			fprintlog(f, 0, (char *)NULL);
   1017 			BACKOFF(f);
   1018 		}
   1019 	}
   1020 	(void)alarm(TIMERINTVL);
   1021 }
   1022 
   1023 /*
   1024  * Print syslogd errors some place.
   1025  */
   1026 void
   1027 logerror(type)
   1028 	char *type;
   1029 {
   1030 	char buf[100];
   1031 
   1032 	if (errno)
   1033 		(void)snprintf(buf,
   1034 		    sizeof(buf), "syslogd: %s: %s", type, strerror(errno));
   1035 	else
   1036 		(void)snprintf(buf, sizeof(buf), "syslogd: %s", type);
   1037 	errno = 0;
   1038 	dprintf("%s\n", buf);
   1039 	logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
   1040 }
   1041 
   1042 void
   1043 die(signo)
   1044 	int signo;
   1045 {
   1046 	struct filed *f;
   1047 	char buf[100], **p;
   1048 
   1049 	for (f = Files; f != NULL; f = f->f_next) {
   1050 		/* flush any pending output */
   1051 		if (f->f_prevcount)
   1052 			fprintlog(f, 0, (char *)NULL);
   1053 	}
   1054 	if (signo) {
   1055 		dprintf("syslogd: exiting on signal %d\n", signo);
   1056 		(void)snprintf(buf, sizeof buf, "exiting on signal %d", signo);
   1057 		errno = 0;
   1058 		logerror(buf);
   1059 	}
   1060 	for (p = LogPaths; p && *p; p++)
   1061 		unlink(*p);
   1062 	exit(0);
   1063 }
   1064 
   1065 /*
   1066  *  INIT -- Initialize syslogd from configuration table
   1067  */
   1068 void
   1069 init(signo)
   1070 	int signo;
   1071 {
   1072 	int i;
   1073 	FILE *cf;
   1074 	struct filed *f, *next, **nextp;
   1075 	char *p;
   1076 	char cline[LINE_MAX];
   1077 
   1078 	dprintf("init\n");
   1079 
   1080 	/*
   1081 	 *  Close all open log files.
   1082 	 */
   1083 	Initialized = 0;
   1084 	for (f = Files; f != NULL; f = next) {
   1085 		/* flush any pending output */
   1086 		if (f->f_prevcount)
   1087 			fprintlog(f, 0, (char *)NULL);
   1088 
   1089 		switch (f->f_type) {
   1090 		case F_FILE:
   1091 		case F_TTY:
   1092 		case F_CONSOLE:
   1093 			(void)close(f->f_file);
   1094 			break;
   1095 		case F_FORW:
   1096 			if (f->f_un.f_forw.f_addr)
   1097 				freeaddrinfo(f->f_un.f_forw.f_addr);
   1098 			break;
   1099 		}
   1100 		next = f->f_next;
   1101 		free((char *)f);
   1102 	}
   1103 	Files = NULL;
   1104 	nextp = &Files;
   1105 
   1106 	/*
   1107 	 *  Close all open sockets
   1108 	 */
   1109 
   1110 	if (finet) {
   1111 		for (i = 0; i < *finet; i++) {
   1112 			if (close(finet[i+1]) < 0) {
   1113 				logerror("close");
   1114 				die(0);
   1115 			}
   1116 		}
   1117 	}
   1118 
   1119 	/*
   1120 	 *  Reset counter of forwarding actions
   1121 	 */
   1122 
   1123 	NumForwards=0;
   1124 
   1125 	/* open the configuration file */
   1126 	if ((cf = fopen(ConfFile, "r")) == NULL) {
   1127 		dprintf("cannot open %s\n", ConfFile);
   1128 		*nextp = (struct filed *)calloc(1, sizeof(*f));
   1129 		cfline("*.ERR\t/dev/console", *nextp);
   1130 		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
   1131 		cfline("*.PANIC\t*", (*nextp)->f_next);
   1132 		Initialized = 1;
   1133 		return;
   1134 	}
   1135 
   1136 	/*
   1137 	 *  Foreach line in the conf table, open that file.
   1138 	 */
   1139 	f = NULL;
   1140 	while (fgets(cline, sizeof(cline), cf) != NULL) {
   1141 		/*
   1142 		 * check for end-of-section, comments, strip off trailing
   1143 		 * spaces and newline character.
   1144 		 */
   1145 		for (p = cline; isspace(*p); ++p)
   1146 			continue;
   1147 		if (*p == '\0' || *p == '#')
   1148 			continue;
   1149 		for (p = strchr(cline, '\0'); isspace(*--p);)
   1150 			continue;
   1151 		*++p = '\0';
   1152 		f = (struct filed *)calloc(1, sizeof(*f));
   1153 		*nextp = f;
   1154 		nextp = &f->f_next;
   1155 		cfline(cline, f);
   1156 	}
   1157 
   1158 	/* close the configuration file */
   1159 	(void)fclose(cf);
   1160 
   1161 	Initialized = 1;
   1162 
   1163 	if (Debug) {
   1164 		for (f = Files; f; f = f->f_next) {
   1165 			for (i = 0; i <= LOG_NFACILITIES; i++)
   1166 				if (f->f_pmask[i] == INTERNAL_NOPRI)
   1167 					printf("X ");
   1168 				else
   1169 					printf("%d ", f->f_pmask[i]);
   1170 			printf("%s: ", TypeNames[f->f_type]);
   1171 			switch (f->f_type) {
   1172 			case F_FILE:
   1173 			case F_TTY:
   1174 			case F_CONSOLE:
   1175 				printf("%s", f->f_un.f_fname);
   1176 				break;
   1177 
   1178 			case F_FORW:
   1179 				printf("%s", f->f_un.f_forw.f_hname);
   1180 				break;
   1181 
   1182 			case F_USERS:
   1183 				for (i = 0;
   1184 				    i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
   1185 					printf("%s, ", f->f_un.f_uname[i]);
   1186 				break;
   1187 			}
   1188 			printf("\n");
   1189 		}
   1190 	}
   1191 
   1192 	finet = socksetup(PF_UNSPEC);
   1193 	if (finet) {
   1194 		if (SecureMode) {
   1195 			for (i = 0; i < *finet; i++) {
   1196 				if (shutdown(finet[i+1], SHUT_RD) < 0) {
   1197 					logerror("shutdown");
   1198 					die(0);
   1199 				}
   1200 			}
   1201 		} else
   1202 			dprintf("listening on inet and/or inet6 socket\n");
   1203 		dprintf("sending on inet and/or inet6 socket\n");
   1204 	}
   1205 
   1206 	logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
   1207 	dprintf("syslogd: restarted\n");
   1208 }
   1209 
   1210 /*
   1211  * Crack a configuration file line
   1212  */
   1213 void
   1214 cfline(line, f)
   1215 	char *line;
   1216 	struct filed *f;
   1217 {
   1218 	struct addrinfo hints, *res;
   1219 	int    error, i, pri;
   1220 	char   *bp, *p, *q;
   1221 	char   buf[MAXLINE], ebuf[100];
   1222 
   1223 	dprintf("cfline(%s)\n", line);
   1224 
   1225 	errno = 0;	/* keep strerror() stuff out of logerror messages */
   1226 
   1227 	/* clear out file entry */
   1228 	memset(f, 0, sizeof(*f));
   1229 	for (i = 0; i <= LOG_NFACILITIES; i++)
   1230 		f->f_pmask[i] = INTERNAL_NOPRI;
   1231 
   1232 	/* scan through the list of selectors */
   1233 	for (p = line; *p && *p != '\t';) {
   1234 
   1235 		/* find the end of this facility name list */
   1236 		for (q = p; *q && *q != '\t' && *q++ != '.'; )
   1237 			continue;
   1238 
   1239 		/* collect priority name */
   1240 		for (bp = buf; *q && !strchr("\t,;", *q); )
   1241 			*bp++ = *q++;
   1242 		*bp = '\0';
   1243 
   1244 		/* skip cruft */
   1245 		while (strchr(", ;", *q))
   1246 			q++;
   1247 
   1248 		/* decode priority name */
   1249 		if (*buf == '*')
   1250 			pri = LOG_PRIMASK + 1;
   1251 		else {
   1252 			pri = decode(buf, prioritynames);
   1253 			if (pri < 0) {
   1254 				(void)snprintf(ebuf, sizeof ebuf,
   1255 				    "unknown priority name \"%s\"", buf);
   1256 				logerror(ebuf);
   1257 				return;
   1258 			}
   1259 		}
   1260 
   1261 		/* scan facilities */
   1262 		while (*p && !strchr("\t.;", *p)) {
   1263 			for (bp = buf; *p && !strchr("\t,;.", *p); )
   1264 				*bp++ = *p++;
   1265 			*bp = '\0';
   1266 			if (*buf == '*')
   1267 				for (i = 0; i < LOG_NFACILITIES; i++)
   1268 					f->f_pmask[i] = pri;
   1269 			else {
   1270 				i = decode(buf, facilitynames);
   1271 				if (i < 0) {
   1272 					(void)snprintf(ebuf, sizeof ebuf,
   1273 					    "unknown facility name \"%s\"",
   1274 					    buf);
   1275 					logerror(ebuf);
   1276 					return;
   1277 				}
   1278 				f->f_pmask[i >> 3] = pri;
   1279 			}
   1280 			while (*p == ',' || *p == ' ')
   1281 				p++;
   1282 		}
   1283 
   1284 		p = q;
   1285 	}
   1286 
   1287 	/* skip to action part */
   1288 	while (*p == '\t')
   1289 		p++;
   1290 
   1291 	switch (*p)
   1292 	{
   1293 	case '@':
   1294 		(void)strcpy(f->f_un.f_forw.f_hname, ++p);
   1295 		memset(&hints, 0, sizeof(hints));
   1296 		hints.ai_family = AF_UNSPEC;
   1297 		hints.ai_socktype = SOCK_DGRAM;
   1298 		hints.ai_protocol = 0;
   1299 		error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints,
   1300 		    &res);
   1301 		if (error) {
   1302 			logerror(gai_strerror(error));
   1303 			break;
   1304 		}
   1305 		f->f_un.f_forw.f_addr = res;
   1306 		f->f_type = F_FORW;
   1307 		NumForwards++;
   1308 		break;
   1309 
   1310 	case '/':
   1311 		(void)strcpy(f->f_un.f_fname, p);
   1312 		if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
   1313 			f->f_type = F_UNUSED;
   1314 			logerror(p);
   1315 			break;
   1316 		}
   1317 		if (isatty(f->f_file))
   1318 			f->f_type = F_TTY;
   1319 		else
   1320 			f->f_type = F_FILE;
   1321 		if (strcmp(p, ctty) == 0)
   1322 			f->f_type = F_CONSOLE;
   1323 		break;
   1324 
   1325 	case '*':
   1326 		f->f_type = F_WALL;
   1327 		break;
   1328 
   1329 	default:
   1330 		for (i = 0; i < MAXUNAMES && *p; i++) {
   1331 			for (q = p; *q && *q != ','; )
   1332 				q++;
   1333 			(void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
   1334 			if ((q - p) > UT_NAMESIZE)
   1335 				f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
   1336 			else
   1337 				f->f_un.f_uname[i][q - p] = '\0';
   1338 			while (*q == ',' || *q == ' ')
   1339 				q++;
   1340 			p = q;
   1341 		}
   1342 		f->f_type = F_USERS;
   1343 		break;
   1344 	}
   1345 }
   1346 
   1347 
   1348 /*
   1349  *  Decode a symbolic name to a numeric value
   1350  */
   1351 int
   1352 decode(name, codetab)
   1353 	const char *name;
   1354 	CODE *codetab;
   1355 {
   1356 	CODE *c;
   1357 	char *p, buf[40];
   1358 
   1359 	if (isdigit(*name))
   1360 		return (atoi(name));
   1361 
   1362 	for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
   1363 		if (isupper(*name))
   1364 			*p = tolower(*name);
   1365 		else
   1366 			*p = *name;
   1367 	}
   1368 	*p = '\0';
   1369 	for (c = codetab; c->c_name; c++)
   1370 		if (!strcmp(buf, c->c_name))
   1371 			return (c->c_val);
   1372 
   1373 	return (-1);
   1374 }
   1375 
   1376 /*
   1377  * Retrieve the size of the kernel message buffer, via sysctl.
   1378  */
   1379 int
   1380 getmsgbufsize()
   1381 {
   1382 	int msgbufsize, mib[2];
   1383 	size_t size;
   1384 
   1385 	mib[0] = CTL_KERN;
   1386 	mib[1] = KERN_MSGBUFSIZE;
   1387 	size = sizeof msgbufsize;
   1388 	if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) {
   1389 		dprintf("couldn't get kern.msgbufsize\n");
   1390 		return (0);
   1391 	}
   1392 	return (msgbufsize);
   1393 }
   1394 
   1395 int *
   1396 socksetup(af)
   1397 	int af;
   1398 {
   1399 	struct addrinfo hints, *res, *r;
   1400 	int error, maxs, *s, *socks;
   1401 
   1402 	if(SecureMode && !NumForwards)
   1403 		return(NULL);
   1404 
   1405 	memset(&hints, 0, sizeof(hints));
   1406 	hints.ai_flags = AI_PASSIVE;
   1407 	hints.ai_family = af;
   1408 	hints.ai_socktype = SOCK_DGRAM;
   1409 	error = getaddrinfo(NULL, "syslog", &hints, &res);
   1410 	if (error) {
   1411 		logerror(gai_strerror(error));
   1412 		errno = 0;
   1413 		die(0);
   1414 	}
   1415 
   1416 	/* Count max number of sockets we may open */
   1417 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
   1418 		continue;
   1419 	socks = malloc ((maxs+1) * sizeof(int));
   1420 	if (!socks) {
   1421 		logerror("couldn't allocate memory for sockets");
   1422 		die(0);
   1423 	}
   1424 
   1425 	*socks = 0;   /* num of sockets counter at start of array */
   1426 	s = socks+1;
   1427 	for (r = res; r; r = r->ai_next) {
   1428 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
   1429 		if (*s < 0) {
   1430 			logerror("socket");
   1431 			continue;
   1432 		}
   1433 		if (!SecureMode && bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
   1434 			close (*s);
   1435 			logerror("bind");
   1436 			continue;
   1437 		}
   1438 
   1439 		*socks = *socks + 1;
   1440 		s++;
   1441 	}
   1442 
   1443 	if (*socks == 0) {
   1444 		free (socks);
   1445 		if(Debug)
   1446 			return(NULL);
   1447 		else
   1448 			die(0);
   1449 	}
   1450 	if (res)
   1451 		freeaddrinfo(res);
   1452 
   1453 	return(socks);
   1454 }
   1455