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