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