xref: /netbsd-src/usr.sbin/syslogd/syslogd.c (revision 4d12bfcd155352508213ace5ccc59ce930ea2974)
1 /*	$NetBSD: syslogd.c,v 1.115 2013/05/27 23:15:51 christos 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\
35 	The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)syslogd.c	8.3 (Berkeley) 4/4/94";
41 #else
42 __RCSID("$NetBSD: syslogd.c,v 1.115 2013/05/27 23:15:51 christos Exp $");
43 #endif
44 #endif /* not lint */
45 
46 /*
47  *  syslogd -- log system messages
48  *
49  * This program implements a system log. It takes a series of lines.
50  * Each line may have a priority, signified as "<n>" as
51  * the first characters of the line.  If this is
52  * not present, a default priority is used.
53  *
54  * To kill syslogd, send a signal 15 (terminate).  A signal 1 (hup) will
55  * cause it to reread its configuration file.
56  *
57  * Defined Constants:
58  *
59  * MAXLINE -- the maximimum line length that can be handled.
60  * DEFUPRI -- the default priority for user messages
61  * DEFSPRI -- the default priority for kernel messages
62  *
63  * Author: Eric Allman
64  * extensive changes by Ralph Campbell
65  * more extensive changes by Eric Allman (again)
66  * Extension to log by program name as well as facility and priority
67  *   by Peter da Silva.
68  * -U and -v by Harlan Stenn.
69  * Priority comparison code by Harlan Stenn.
70  * TLS, syslog-protocol, and syslog-sign code by Martin Schuette.
71  */
72 #define SYSLOG_NAMES
73 #include <poll.h>
74 #include "syslogd.h"
75 #include "extern.h"
76 
77 #ifndef DISABLE_SIGN
78 #include "sign.h"
79 struct sign_global_t GlobalSign = {
80 	.rsid = 0,
81 	.sig2_delims = STAILQ_HEAD_INITIALIZER(GlobalSign.sig2_delims)
82 };
83 #endif /* !DISABLE_SIGN */
84 
85 #ifndef DISABLE_TLS
86 #include "tls.h"
87 #endif /* !DISABLE_TLS */
88 
89 #ifdef LIBWRAP
90 int allow_severity = LOG_AUTH|LOG_INFO;
91 int deny_severity = LOG_AUTH|LOG_WARNING;
92 #endif
93 
94 const char	*ConfFile = _PATH_LOGCONF;
95 char	ctty[] = _PATH_CONSOLE;
96 
97 /*
98  * Queue of about-to-be-dead processes we should watch out for.
99  */
100 TAILQ_HEAD(, deadq_entry) deadq_head = TAILQ_HEAD_INITIALIZER(deadq_head);
101 
102 typedef struct deadq_entry {
103 	pid_t				dq_pid;
104 	int				dq_timeout;
105 	TAILQ_ENTRY(deadq_entry)	dq_entries;
106 } *dq_t;
107 
108 /*
109  * The timeout to apply to processes waiting on the dead queue.	 Unit
110  * of measure is "mark intervals", i.e. 20 minutes by default.
111  * Processes on the dead queue will be terminated after that time.
112  */
113 #define DQ_TIMO_INIT	2
114 
115 /*
116  * Intervals at which we flush out "message repeated" messages,
117  * in seconds after previous message is logged.	 After each flush,
118  * we move to the next interval until we reach the largest.
119  */
120 int	repeatinterval[] = { 30, 120, 600 };	/* # of secs before flush */
121 #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
122 #define REPEATTIME(f)	((f)->f_time + repeatinterval[(f)->f_repeatcount])
123 #define BACKOFF(f)	{ if ((size_t)(++(f)->f_repeatcount) > MAXREPEAT) \
124 				 (f)->f_repeatcount = MAXREPEAT; \
125 			}
126 
127 /* values for f_type */
128 #define F_UNUSED	0		/* unused entry */
129 #define F_FILE		1		/* regular file */
130 #define F_TTY		2		/* terminal */
131 #define F_CONSOLE	3		/* console terminal */
132 #define F_FORW		4		/* remote machine */
133 #define F_USERS		5		/* list of users */
134 #define F_WALL		6		/* everyone logged on */
135 #define F_PIPE		7		/* pipe to program */
136 #define F_TLS		8
137 
138 struct TypeInfo {
139 	const char *name;
140 	char	   *queue_length_string;
141 	const char *default_length_string;
142 	char	   *queue_size_string;
143 	const char *default_size_string;
144 	int64_t	    queue_length;
145 	int64_t	    queue_size;
146 	int   max_msg_length;
147 } TypeInfo[] = {
148 	/* numeric values are set in init()
149 	 * -1 in length/size or max_msg_length means infinite */
150 	{"UNUSED",  NULL,    "0", NULL,	  "0", 0, 0,	 0},
151 	{"FILE",    NULL, "1024", NULL,	 "1M", 0, 0, 16384},
152 	{"TTY",	    NULL,    "0", NULL,	  "0", 0, 0,  1024},
153 	{"CONSOLE", NULL,    "0", NULL,	  "0", 0, 0,  1024},
154 	{"FORW",    NULL,    "0", NULL,	 "1M", 0, 0, 16384},
155 	{"USERS",   NULL,    "0", NULL,	  "0", 0, 0,  1024},
156 	{"WALL",    NULL,    "0", NULL,	  "0", 0, 0,  1024},
157 	{"PIPE",    NULL, "1024", NULL,	 "1M", 0, 0, 16384},
158 #ifndef DISABLE_TLS
159 	{"TLS",	    NULL,   "-1", NULL, "16M", 0, 0, 16384}
160 #endif /* !DISABLE_TLS */
161 };
162 
163 struct	filed *Files = NULL;
164 struct	filed consfile;
165 
166 time_t	now;
167 int	Debug = D_NONE;		/* debug flag */
168 int	daemonized = 0;		/* we are not daemonized yet */
169 char	*LocalFQDN = NULL;	       /* our FQDN */
170 char	*oldLocalFQDN = NULL;	       /* our previous FQDN */
171 char	LocalHostName[MAXHOSTNAMELEN]; /* our hostname */
172 struct socketEvent *finet;	/* Internet datagram sockets and events */
173 int   *funix;			/* Unix domain datagram sockets */
174 #ifndef DISABLE_TLS
175 struct socketEvent *TLS_Listen_Set; /* TLS/TCP sockets and events */
176 #endif /* !DISABLE_TLS */
177 int	Initialized = 0;	/* set when we have initialized ourselves */
178 int	ShuttingDown;		/* set when we die() */
179 int	MarkInterval = 20 * 60; /* interval between marks in seconds */
180 int	MarkSeq = 0;		/* mark sequence number */
181 int	SecureMode = 0;		/* listen only on unix domain socks */
182 int	UseNameService = 1;	/* make domain name queries */
183 int	NumForwards = 0;	/* number of forwarding actions in conf file */
184 char	**LogPaths;		/* array of pathnames to read messages from */
185 int	NoRepeat = 0;		/* disable "repeated"; log always */
186 int	RemoteAddDate = 0;	/* always add date to messages from network */
187 int	SyncKernel = 0;		/* write kernel messages synchronously */
188 int	UniquePriority = 0;	/* only log specified priority */
189 int	LogFacPri = 0;		/* put facility and priority in log messages: */
190 				/* 0=no, 1=numeric, 2=names */
191 bool	BSDOutputFormat = true;	/* if true emit traditional BSD Syslog lines,
192 				 * otherwise new syslog-protocol lines
193 				 *
194 				 * Open Issue: having a global flag is the
195 				 * easiest solution. If we get a more detailed
196 				 * config file this could/should be changed
197 				 * into a destination-specific flag.
198 				 * Most output code should be ready to handle
199 				 * this, it will only break some syslog-sign
200 				 * configurations (e.g. with SG="0").
201 				 */
202 char	appname[]   = "syslogd";/* the APPNAME for own messages */
203 char   *include_pid = NULL;	/* include PID in own messages */
204 
205 
206 /* init and setup */
207 void		usage(void) __attribute__((__noreturn__));
208 void		logpath_add(char ***, int *, int *, const char *);
209 void		logpath_fileadd(char ***, int *, int *, const char *);
210 void		init(int fd, short event, void *ev);  /* SIGHUP kevent dispatch routine */
211 struct socketEvent*
212 		socksetup(int, const char *);
213 int		getmsgbufsize(void);
214 char	       *getLocalFQDN(void);
215 void		trim_anydomain(char *);
216 /* pipe & subprocess handling */
217 int		p_open(char *, pid_t *);
218 void		deadq_enter(pid_t, const char *);
219 int		deadq_remove(pid_t);
220 void		log_deadchild(pid_t, int, const char *);
221 void		reapchild(int fd, short event, void *ev); /* SIGCHLD kevent dispatch routine */
222 /* input message parsing & formatting */
223 const char     *cvthname(struct sockaddr_storage *);
224 void		printsys(char *);
225 struct buf_msg *printline_syslogprotocol(const char*, char*, int, int);
226 struct buf_msg *printline_bsdsyslog(const char*, char*, int, int);
227 struct buf_msg *printline_kernelprintf(const char*, char*, int, int);
228 size_t		check_timestamp(unsigned char *, char **, bool, bool);
229 char	       *copy_utf8_ascii(char*, size_t);
230 uint_fast32_t	get_utf8_value(const char*);
231 unsigned	valid_utf8(const char *);
232 static unsigned check_sd(char*);
233 static unsigned check_msgid(char *);
234 /* event handling */
235 static void	dispatch_read_klog(int fd, short event, void *ev);
236 static void	dispatch_read_finet(int fd, short event, void *ev);
237 static void	dispatch_read_funix(int fd, short event, void *ev);
238 static void	domark(int fd, short event, void *ev); /* timer kevent dispatch routine */
239 /* log messages */
240 void		logmsg_async(int, const char *, const char *, int);
241 void		logmsg(struct buf_msg *);
242 int		matches_spec(const char *, const char *,
243 		char *(*)(const char *, const char *));
244 void		udp_send(struct filed *, char *, size_t);
245 void		wallmsg(struct filed *, struct iovec *, size_t);
246 /* buffer & queue functions */
247 size_t		message_queue_purge(struct filed *f, size_t, int);
248 size_t		message_allqueues_check(void);
249 static struct buf_queue *
250 		find_qentry_to_delete(const struct buf_queue_head *, int, bool);
251 struct buf_queue *
252 		message_queue_add(struct filed *, struct buf_msg *);
253 size_t		buf_queue_obj_size(struct buf_queue*);
254 /* configuration & parsing */
255 void		cfline(size_t, const char *, struct filed *, const char *,
256     const char *);
257 void		read_config_file(FILE*, struct filed**);
258 void		store_sign_delim_sg2(char*);
259 int		decode(const char *, CODE *);
260 bool		copy_config_value(const char *, char **, const char **,
261     const char *, int);
262 bool		copy_config_value_word(char **, const char **);
263 
264 /* config parsing */
265 #ifndef DISABLE_TLS
266 void		free_cred_SLIST(struct peer_cred_head *);
267 static inline void
268 		free_incoming_tls_sockets(void);
269 #endif /* !DISABLE_TLS */
270 static int writev1(int, struct iovec *, size_t);
271 
272 /* for make_timestamp() */
273 #define TIMESTAMPBUFSIZE 35
274 char timestamp[TIMESTAMPBUFSIZE];
275 
276 /*
277  * Global line buffer.	Since we only process one event at a time,
278  * a global one will do.  But for klog, we use own buffer so that
279  * partial line at the end of buffer can be deferred.
280  */
281 char *linebuf, *klog_linebuf;
282 size_t linebufsize, klog_linebufoff;
283 
284 static const char *bindhostname = NULL;
285 
286 #ifndef DISABLE_TLS
287 struct TLS_Incoming TLS_Incoming_Head = \
288 	SLIST_HEAD_INITIALIZER(TLS_Incoming_Head);
289 extern char *SSL_ERRCODE[];
290 struct tls_global_options_t tls_opt;
291 #endif /* !DISABLE_TLS */
292 
293 int
294 main(int argc, char *argv[])
295 {
296 	int ch, j, fklog;
297 	int funixsize = 0, funixmaxsize = 0;
298 	struct sockaddr_un sunx;
299 	char **pp;
300 	struct event *ev;
301 	uid_t uid = 0;
302 	gid_t gid = 0;
303 	char *user = NULL;
304 	char *group = NULL;
305 	const char *root = "/";
306 	char *endp;
307 	struct group   *gr;
308 	struct passwd  *pw;
309 	unsigned long l;
310 
311 	/* should we set LC_TIME="C" to ensure correct timestamps&parsing? */
312 	(void)setlocale(LC_ALL, "");
313 
314 	while ((ch = getopt(argc, argv, "b:dnsSf:m:o:p:P:ru:g:t:TUv")) != -1)
315 		switch(ch) {
316 		case 'b':
317 			bindhostname = optarg;
318 			break;
319 		case 'd':		/* debug */
320 			Debug = D_DEFAULT;
321 			/* is there a way to read the integer value
322 			 * for Debug as an optional argument? */
323 			break;
324 		case 'f':		/* configuration file */
325 			ConfFile = optarg;
326 			break;
327 		case 'g':
328 			group = optarg;
329 			if (*group == '\0')
330 				usage();
331 			break;
332 		case 'm':		/* mark interval */
333 			MarkInterval = atoi(optarg) * 60;
334 			break;
335 		case 'n':		/* turn off DNS queries */
336 			UseNameService = 0;
337 			break;
338 		case 'o':		/* message format */
339 #define EQ(a)		(strncmp(optarg, # a, sizeof(# a) - 1) == 0)
340 			if (EQ(bsd) || EQ(rfc3264))
341 				BSDOutputFormat = true;
342 			else if (EQ(syslog) || EQ(rfc5424))
343 				BSDOutputFormat = false;
344 			else
345 				usage();
346 			/* TODO: implement additional output option "osyslog"
347 			 *	 for old syslogd behaviour as introduced after
348 			 *	 FreeBSD PR#bin/7055.
349 			 */
350 			break;
351 		case 'p':		/* path */
352 			logpath_add(&LogPaths, &funixsize,
353 			    &funixmaxsize, optarg);
354 			break;
355 		case 'P':		/* file of paths */
356 			logpath_fileadd(&LogPaths, &funixsize,
357 			    &funixmaxsize, optarg);
358 			break;
359 		case 'r':		/* disable "repeated" compression */
360 			NoRepeat++;
361 			break;
362 		case 's':		/* no network listen mode */
363 			SecureMode++;
364 			break;
365 		case 'S':
366 			SyncKernel = 1;
367 			break;
368 		case 't':
369 			root = optarg;
370 			if (*root == '\0')
371 				usage();
372 			break;
373 		case 'T':
374 			RemoteAddDate = 1;
375 			break;
376 		case 'u':
377 			user = optarg;
378 			if (*user == '\0')
379 				usage();
380 			break;
381 		case 'U':		/* only log specified priority */
382 			UniquePriority = 1;
383 			break;
384 		case 'v':		/* log facility and priority */
385 			if (LogFacPri < 2)
386 				LogFacPri++;
387 			break;
388 		default:
389 			usage();
390 		}
391 	if ((argc -= optind) != 0)
392 		usage();
393 
394 	setlinebuf(stdout);
395 	tzset(); /* init TZ information for localtime. */
396 
397 	if (user != NULL) {
398 		if (isdigit((unsigned char)*user)) {
399 			errno = 0;
400 			endp = NULL;
401 			l = strtoul(user, &endp, 0);
402 			if (errno || *endp != '\0')
403 				goto getuser;
404 			uid = (uid_t)l;
405 			if (uid != l) {/* TODO: never executed */
406 				errno = 0;
407 				logerror("UID out of range");
408 				die(0, 0, NULL);
409 			}
410 		} else {
411 getuser:
412 			if ((pw = getpwnam(user)) != NULL) {
413 				uid = pw->pw_uid;
414 			} else {
415 				errno = 0;
416 				logerror("Cannot find user `%s'", user);
417 				die(0, 0, NULL);
418 			}
419 		}
420 	}
421 
422 	if (group != NULL) {
423 		if (isdigit((unsigned char)*group)) {
424 			errno = 0;
425 			endp = NULL;
426 			l = strtoul(group, &endp, 0);
427 			if (errno || *endp != '\0')
428 				goto getgroup;
429 			gid = (gid_t)l;
430 			if (gid != l) {/* TODO: never executed */
431 				errno = 0;
432 				logerror("GID out of range");
433 				die(0, 0, NULL);
434 			}
435 		} else {
436 getgroup:
437 			if ((gr = getgrnam(group)) != NULL) {
438 				gid = gr->gr_gid;
439 			} else {
440 				errno = 0;
441 				logerror("Cannot find group `%s'", group);
442 				die(0, 0, NULL);
443 			}
444 		}
445 	}
446 
447 	if (access(root, F_OK | R_OK)) {
448 		logerror("Cannot access `%s'", root);
449 		die(0, 0, NULL);
450 	}
451 
452 	consfile.f_type = F_CONSOLE;
453 	(void)strlcpy(consfile.f_un.f_fname, ctty,
454 	    sizeof(consfile.f_un.f_fname));
455 	linebufsize = getmsgbufsize();
456 	if (linebufsize < MAXLINE)
457 		linebufsize = MAXLINE;
458 	linebufsize++;
459 
460 	if (!(linebuf = malloc(linebufsize))) {
461 		logerror("Couldn't allocate buffer");
462 		die(0, 0, NULL);
463 	}
464 	if (!(klog_linebuf = malloc(linebufsize))) {
465 		logerror("Couldn't allocate buffer for klog");
466 		die(0, 0, NULL);
467 	}
468 
469 
470 #ifndef SUN_LEN
471 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
472 #endif
473 	if (funixsize == 0)
474 		logpath_add(&LogPaths, &funixsize,
475 		    &funixmaxsize, _PATH_LOG);
476 	funix = malloc(sizeof(*funix) * funixsize);
477 	if (funix == NULL) {
478 		logerror("Couldn't allocate funix descriptors");
479 		die(0, 0, NULL);
480 	}
481 	for (j = 0, pp = LogPaths; *pp; pp++, j++) {
482 		DPRINTF(D_NET, "Making unix dgram socket `%s'\n", *pp);
483 		unlink(*pp);
484 		memset(&sunx, 0, sizeof(sunx));
485 		sunx.sun_family = AF_LOCAL;
486 		(void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path));
487 		funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0);
488 		if (funix[j] < 0 || bind(funix[j],
489 		    (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
490 		    chmod(*pp, 0666) < 0) {
491 			logerror("Cannot create `%s'", *pp);
492 			die(0, 0, NULL);
493 		}
494 		DPRINTF(D_NET, "Listening on unix dgram socket `%s'\n", *pp);
495 	}
496 
497 	if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) {
498 		DPRINTF(D_FILE, "Can't open `%s' (%d)\n", _PATH_KLOG, errno);
499 	} else {
500 		DPRINTF(D_FILE, "Listening on kernel log `%s' with fd %d\n",
501 		    _PATH_KLOG, fklog);
502 	}
503 
504 #if (!defined(DISABLE_TLS) && !defined(DISABLE_SIGN))
505 	/* basic OpenSSL init */
506 	SSL_load_error_strings();
507 	(void) SSL_library_init();
508 	OpenSSL_add_all_digests();
509 	/* OpenSSL PRNG needs /dev/urandom, thus initialize before chroot() */
510 	if (!RAND_status()) {
511 		errno = 0;
512 		logerror("Unable to initialize OpenSSL PRNG");
513 	} else {
514 		DPRINTF(D_TLS, "Initializing PRNG\n");
515 	}
516 #endif /* (!defined(DISABLE_TLS) && !defined(DISABLE_SIGN)) */
517 #ifndef DISABLE_SIGN
518 	/* initialize rsid -- we will use that later to determine
519 	 * whether sign_global_init() was already called */
520 	GlobalSign.rsid = 0;
521 #endif /* !DISABLE_SIGN */
522 #if (IETF_NUM_PRIVALUES != (LOG_NFACILITIES<<3))
523 	logerror("Warning: system defines %d priority values, but "
524 	    "syslog-protocol/syslog-sign specify %d values",
525 	    LOG_NFACILITIES, SIGN_NUM_PRIVALS);
526 #endif
527 
528 	/*
529 	 * All files are open, we can drop privileges and chroot
530 	 */
531 	DPRINTF(D_MISC, "Attempt to chroot to `%s'\n", root);
532 	if (chroot(root) == -1) {
533 		logerror("Failed to chroot to `%s'", root);
534 		die(0, 0, NULL);
535 	}
536 	DPRINTF(D_MISC, "Attempt to set GID/EGID to `%d'\n", gid);
537 	if (setgid(gid) || setegid(gid)) {
538 		logerror("Failed to set gid to `%d'", gid);
539 		die(0, 0, NULL);
540 	}
541 	DPRINTF(D_MISC, "Attempt to set UID/EUID to `%d'\n", uid);
542 	if (setuid(uid) || seteuid(uid)) {
543 		logerror("Failed to set uid to `%d'", uid);
544 		die(0, 0, NULL);
545 	}
546 	/*
547 	 * We cannot detach from the terminal before we are sure we won't
548 	 * have a fatal error, because error message would not go to the
549 	 * terminal and would not be logged because syslogd dies.
550 	 * All die() calls are behind us, we can call daemon()
551 	 */
552 	if (!Debug) {
553 		(void)daemon(0, 0);
554 		daemonized = 1;
555 		/* tuck my process id away, if i'm not in debug mode */
556 #ifdef __NetBSD_Version__
557 		pidfile(NULL);
558 #endif /* __NetBSD_Version__ */
559 	}
560 
561 #define MAX_PID_LEN 5
562 	include_pid = malloc(MAX_PID_LEN+1);
563 	snprintf(include_pid, MAX_PID_LEN+1, "%d", getpid());
564 
565 	/*
566 	 * Create the global kernel event descriptor.
567 	 *
568 	 * NOTE: We MUST do this after daemon(), bacause the kqueue()
569 	 * API dictates that kqueue descriptors are not inherited
570 	 * across forks (lame!).
571 	 */
572 	(void)event_init();
573 
574 	/*
575 	 * We must read the configuration file for the first time
576 	 * after the kqueue descriptor is created, because we install
577 	 * events during this process.
578 	 */
579 	init(0, 0, NULL);
580 
581 	/*
582 	 * Always exit on SIGTERM.  Also exit on SIGINT and SIGQUIT
583 	 * if we're debugging.
584 	 */
585 	(void)signal(SIGTERM, SIG_IGN);
586 	(void)signal(SIGINT, SIG_IGN);
587 	(void)signal(SIGQUIT, SIG_IGN);
588 
589 	ev = allocev();
590 	signal_set(ev, SIGTERM, die, ev);
591 	EVENT_ADD(ev);
592 
593 	if (Debug) {
594 		ev = allocev();
595 		signal_set(ev, SIGINT, die, ev);
596 		EVENT_ADD(ev);
597 		ev = allocev();
598 		signal_set(ev, SIGQUIT, die, ev);
599 		EVENT_ADD(ev);
600 	}
601 
602 	ev = allocev();
603 	signal_set(ev, SIGCHLD, reapchild, ev);
604 	EVENT_ADD(ev);
605 
606 	ev = allocev();
607 	schedule_event(&ev,
608 		&((struct timeval){TIMERINTVL, 0}),
609 		domark, ev);
610 
611 	(void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */
612 
613 	/* Re-read configuration on SIGHUP. */
614 	(void) signal(SIGHUP, SIG_IGN);
615 	ev = allocev();
616 	signal_set(ev, SIGHUP, init, ev);
617 	EVENT_ADD(ev);
618 
619 #ifndef DISABLE_TLS
620 	ev = allocev();
621 	signal_set(ev, SIGUSR1, dispatch_force_tls_reconnect, ev);
622 	EVENT_ADD(ev);
623 #endif /* !DISABLE_TLS */
624 
625 	if (fklog >= 0) {
626 		ev = allocev();
627 		DPRINTF(D_EVENT,
628 			"register klog for fd %d with ev@%p\n", fklog, ev);
629 		event_set(ev, fklog, EV_READ | EV_PERSIST,
630 			dispatch_read_klog, ev);
631 		EVENT_ADD(ev);
632 	}
633 	for (j = 0, pp = LogPaths; *pp; pp++, j++) {
634 		ev = allocev();
635 		event_set(ev, funix[j], EV_READ | EV_PERSIST,
636 			dispatch_read_funix, ev);
637 		EVENT_ADD(ev);
638 	}
639 
640 	DPRINTF(D_MISC, "Off & running....\n");
641 
642 	j = event_dispatch();
643 	/* normal termination via die(), reaching this is an error */
644 	DPRINTF(D_MISC, "event_dispatch() returned %d\n", j);
645 	die(0, 0, NULL);
646 	/*NOTREACHED*/
647 	return 0;
648 }
649 
650 void
651 usage(void)
652 {
653 
654 	(void)fprintf(stderr,
655 	    "usage: %s [-dnrSsTUv] [-b bind_address] [-f config_file] [-g group]\n"
656 	    "\t[-m mark_interval] [-P file_list] [-p log_socket\n"
657 	    "\t[-p log_socket2 ...]] [-t chroot_dir] [-u user]\n",
658 	    getprogname());
659 	exit(1);
660 }
661 
662 /*
663  * Dispatch routine for reading /dev/klog
664  *
665  * Note: slightly different semantic in dispatch_read functions:
666  *	 - read_klog() might give multiple messages in linebuf and
667  *	   leaves the task of splitting them to printsys()
668  *	 - all other read functions receive one message and
669  *	   then call printline() with one buffer.
670  */
671 static void
672 dispatch_read_klog(int fd, short event, void *ev)
673 {
674 	ssize_t rv;
675 	size_t resid = linebufsize - klog_linebufoff;
676 
677 	DPRINTF((D_CALL|D_EVENT), "Kernel log active (%d, %d, %p)"
678 		" with linebuf@%p, length %zu)\n", fd, event, ev,
679 		klog_linebuf, linebufsize);
680 
681 	rv = read(fd, &klog_linebuf[klog_linebufoff], resid - 1);
682 	if (rv > 0) {
683 		klog_linebuf[klog_linebufoff + rv] = '\0';
684 		printsys(klog_linebuf);
685 	} else if (rv < 0 && errno != EINTR) {
686 		/*
687 		 * /dev/klog has croaked.  Disable the event
688 		 * so it won't bother us again.
689 		 */
690 		logerror("klog failed");
691 		event_del(ev);
692 	}
693 }
694 
695 /*
696  * Dispatch routine for reading Unix domain sockets.
697  */
698 static void
699 dispatch_read_funix(int fd, short event, void *ev)
700 {
701 	struct sockaddr_un myname, fromunix;
702 	ssize_t rv;
703 	socklen_t sunlen;
704 
705 	sunlen = sizeof(myname);
706 	if (getsockname(fd, (struct sockaddr *)&myname, &sunlen) != 0) {
707 		/*
708 		 * This should never happen, so ensure that it doesn't
709 		 * happen again.
710 		 */
711 		logerror("getsockname() unix failed");
712 		event_del(ev);
713 		return;
714 	}
715 
716 	DPRINTF((D_CALL|D_EVENT|D_NET), "Unix socket (%.*s) active (%d, %d %p)"
717 		" with linebuf@%p, size %zu)\n", (int)(myname.sun_len
718 		- sizeof(myname.sun_len) - sizeof(myname.sun_family)),
719 		myname.sun_path, fd, event, ev, linebuf, linebufsize-1);
720 
721 	sunlen = sizeof(fromunix);
722 	rv = recvfrom(fd, linebuf, linebufsize-1, 0,
723 	    (struct sockaddr *)&fromunix, &sunlen);
724 	if (rv > 0) {
725 		linebuf[rv] = '\0';
726 		printline(LocalFQDN, linebuf, 0);
727 	} else if (rv < 0 && errno != EINTR) {
728 		logerror("recvfrom() unix `%.*s'",
729 			myname.sun_len, myname.sun_path);
730 	}
731 }
732 
733 /*
734  * Dispatch routine for reading Internet sockets.
735  */
736 static void
737 dispatch_read_finet(int fd, short event, void *ev)
738 {
739 #ifdef LIBWRAP
740 	struct request_info req;
741 #endif
742 	struct sockaddr_storage frominet;
743 	ssize_t rv;
744 	socklen_t len;
745 	int reject = 0;
746 
747 	DPRINTF((D_CALL|D_EVENT|D_NET), "inet socket active (%d, %d %p) "
748 		" with linebuf@%p, size %zu)\n",
749 		fd, event, ev, linebuf, linebufsize-1);
750 
751 #ifdef LIBWRAP
752 	request_init(&req, RQ_DAEMON, appname, RQ_FILE, fd, NULL);
753 	fromhost(&req);
754 	reject = !hosts_access(&req);
755 	if (reject)
756 		DPRINTF(D_NET, "access denied\n");
757 #endif
758 
759 	len = sizeof(frominet);
760 	rv = recvfrom(fd, linebuf, linebufsize-1, 0,
761 	    (struct sockaddr *)&frominet, &len);
762 	if (rv == 0 || (rv < 0 && errno == EINTR))
763 		return;
764 	else if (rv < 0) {
765 		logerror("recvfrom inet");
766 		return;
767 	}
768 
769 	linebuf[rv] = '\0';
770 	if (!reject)
771 		printline(cvthname(&frominet), linebuf,
772 		    RemoteAddDate ? ADDDATE : 0);
773 }
774 
775 /*
776  * given a pointer to an array of char *'s, a pointer to its current
777  * size and current allocated max size, and a new char * to add, add
778  * it, update everything as necessary, possibly allocating a new array
779  */
780 void
781 logpath_add(char ***lp, int *szp, int *maxszp, const char *new)
782 {
783 	char **nlp;
784 	int newmaxsz;
785 
786 	DPRINTF(D_FILE, "Adding `%s' to the %p logpath list\n", new, *lp);
787 	if (*szp == *maxszp) {
788 		if (*maxszp == 0) {
789 			newmaxsz = 4;	/* start of with enough for now */
790 			*lp = NULL;
791 		} else
792 			newmaxsz = *maxszp * 2;
793 		nlp = realloc(*lp, sizeof(char *) * (newmaxsz + 1));
794 		if (nlp == NULL) {
795 			logerror("Couldn't allocate line buffer");
796 			die(0, 0, NULL);
797 		}
798 		*lp = nlp;
799 		*maxszp = newmaxsz;
800 	}
801 	if (((*lp)[(*szp)++] = strdup(new)) == NULL) {
802 		logerror("Couldn't allocate logpath");
803 		die(0, 0, NULL);
804 	}
805 	(*lp)[(*szp)] = NULL;		/* always keep it NULL terminated */
806 }
807 
808 /* do a file of log sockets */
809 void
810 logpath_fileadd(char ***lp, int *szp, int *maxszp, const char *file)
811 {
812 	FILE *fp;
813 	char *line;
814 	size_t len;
815 
816 	fp = fopen(file, "r");
817 	if (fp == NULL) {
818 		logerror("Could not open socket file list `%s'", file);
819 		die(0, 0, NULL);
820 	}
821 
822 	while ((line = fgetln(fp, &len)) != NULL) {
823 		line[len - 1] = 0;
824 		logpath_add(lp, szp, maxszp, line);
825 	}
826 	fclose(fp);
827 }
828 
829 /*
830  * checks UTF-8 codepoint
831  * returns either its length in bytes or 0 if *input is invalid
832 */
833 unsigned
834 valid_utf8(const char *c) {
835 	unsigned rc, nb;
836 
837 	/* first byte gives sequence length */
838 	     if ((*c & 0x80) == 0x00) return 1; /* 0bbbbbbb -- ASCII */
839 	else if ((*c & 0xc0) == 0x80) return 0; /* 10bbbbbb -- trailing byte */
840 	else if ((*c & 0xe0) == 0xc0) nb = 2;	/* 110bbbbb */
841 	else if ((*c & 0xf0) == 0xe0) nb = 3;	/* 1110bbbb */
842 	else if ((*c & 0xf8) == 0xf0) nb = 4;	/* 11110bbb */
843 	else return 0; /* UTF-8 allows only up to 4 bytes */
844 
845 	/* catch overlong encodings */
846 	if ((*c & 0xfe) == 0xc0)
847 		return 0; /* 1100000b ... */
848 	else if (((*c & 0xff) == 0xe0) && ((*(c+1) & 0xe0) == 0x80))
849 		return 0; /* 11100000 100bbbbb ... */
850 	else if (((*c & 0xff) == 0xf0) && ((*(c+1) & 0xf0) == 0x80))
851 		return 0; /* 11110000 1000bbbb ... ... */
852 
853 	/* and also filter UTF-16 surrogates (=invalid in UTF-8) */
854 	if (((*c & 0xff) == 0xed) && ((*(c+1) & 0xe0) == 0xa0))
855 		return 0; /* 11101101 101bbbbb ... */
856 
857 	rc = nb;
858 	/* check trailing bytes */
859 	switch (nb) {
860 	default: return 0;
861 	case 4: if ((*(c+3) & 0xc0) != 0x80) return 0; /*FALLTHROUGH*/
862 	case 3: if ((*(c+2) & 0xc0) != 0x80) return 0; /*FALLTHROUGH*/
863 	case 2: if ((*(c+1) & 0xc0) != 0x80) return 0; /*FALLTHROUGH*/
864 	}
865 	return rc;
866 }
867 #define UTF8CHARMAX 4
868 
869 /*
870  * read UTF-8 value
871  * returns a the codepoint number
872  */
873 uint_fast32_t
874 get_utf8_value(const char *c) {
875 	uint_fast32_t sum;
876 	unsigned nb, i;
877 
878 	/* first byte gives sequence length */
879 	     if ((*c & 0x80) == 0x00) return *c;/* 0bbbbbbb -- ASCII */
880 	else if ((*c & 0xc0) == 0x80) return 0; /* 10bbbbbb -- trailing byte */
881 	else if ((*c & 0xe0) == 0xc0) {		/* 110bbbbb */
882 		nb = 2;
883 		sum = (*c & ~0xe0) & 0xff;
884 	} else if ((*c & 0xf0) == 0xe0) {	/* 1110bbbb */
885 		nb = 3;
886 		sum = (*c & ~0xf0) & 0xff;
887 	} else if ((*c & 0xf8) == 0xf0) {	/* 11110bbb */
888 		nb = 4;
889 		sum = (*c & ~0xf8) & 0xff;
890 	} else return 0; /* UTF-8 allows only up to 4 bytes */
891 
892 	/* check trailing bytes -- 10bbbbbb */
893 	i = 1;
894 	while (i < nb) {
895 		sum <<= 6;
896 		sum |= ((*(c+i) & ~0xc0) & 0xff);
897 		i++;
898 	}
899 	return sum;
900 }
901 
902 /* note previous versions transscribe
903  * control characters, e.g. \007 --> "^G"
904  * did anyone rely on that?
905  *
906  * this new version works on only one buffer and
907  * replaces control characters with a space
908  */
909 #define NEXTFIELD(ptr) if (*(p) == ' ') (p)++; /* SP */			\
910 		       else {						\
911 				DPRINTF(D_DATA, "format error\n");	\
912 				if (*(p) == '\0') start = (p);		\
913 				goto all_syslog_msg;			\
914 		       }
915 #define FORCE2ASCII(c) ((iscntrl((unsigned char)(c)) && (c) != '\t')	\
916 			? ((c) == '\n' ? ' ' : '?')			\
917 			: (c) & 0177)
918 
919 /* following syslog-protocol */
920 #define printusascii(ch) (ch >= 33 && ch <= 126)
921 #define sdname(ch) (ch != '=' && ch != ' ' \
922 		 && ch != ']' && ch != '"' \
923 		 && printusascii(ch))
924 
925 /* checks whether the first word of string p can be interpreted as
926  * a syslog-protocol MSGID and if so returns its length.
927  *
928  * otherwise returns 0
929  */
930 static unsigned
931 check_msgid(char *p)
932 {
933 	char *q = p;
934 
935 	/* consider the NILVALUE to be valid */
936 	if (*q == '-' && *(q+1) == ' ')
937 		return 1;
938 
939 	for (;;) {
940 		if (*q == ' ')
941 			return q - p;
942 		else if (*q == '\0' || !printusascii(*q) || q - p >= MSGID_MAX)
943 			return 0;
944 		else
945 			q++;
946 	}
947 }
948 
949 /*
950  * returns number of chars found in SD at beginning of string p
951  * thus returns 0 if no valid SD is found
952  *
953  * if ascii == true then substitute all non-ASCII chars
954  * otherwise use syslog-protocol rules to allow UTF-8 in values
955  * note: one pass for filtering and scanning, so a found SD
956  * is always filtered, but an invalid one could be partially
957  * filtered up to the format error.
958  */
959 static unsigned
960 check_sd(char* p)
961 {
962 	char *q = p;
963 	bool esc = false;
964 
965 	/* consider the NILVALUE to be valid */
966 	if (*q == '-' && (*(q+1) == ' ' || *(q+1) == '\0'))
967 		return 1;
968 
969 	for(;;) { /* SD-ELEMENT */
970 		if (*q++ != '[') return 0;
971 		/* SD-ID */
972 		if (!sdname(*q)) return 0;
973 		while (sdname(*q)) {
974 			*q = FORCE2ASCII(*q);
975 			q++;
976 		}
977 		for(;;) { /* SD-PARAM */
978 			if (*q == ']') {
979 				q++;
980 				if (*q == ' ' || *q == '\0') return q - p;
981 				else if (*q == '[') break;
982 			} else if (*q++ != ' ') return 0;
983 
984 			/* PARAM-NAME */
985 			if (!sdname(*q)) return 0;
986 			while (sdname(*q)) {
987 				*q = FORCE2ASCII(*q);
988 				q++;
989 			}
990 
991 			if (*q++ != '=') return 0;
992 			if (*q++ != '"') return 0;
993 
994 			for(;;) { /* PARAM-VALUE */
995 				if (esc) {
996 					esc = false;
997 					if (*q == '\\' || *q == '"' ||
998 					    *q == ']') {
999 						q++;
1000 						continue;
1001 					}
1002 					/* no else because invalid
1003 					 * escape sequences are accepted */
1004 				}
1005 				else if (*q == '"') break;
1006 				else if (*q == '\0' || *q == ']') return 0;
1007 				else if (*q == '\\') esc = true;
1008 				else {
1009 					int i;
1010 					i = valid_utf8(q);
1011 					if (i == 0)
1012 						*q = '?';
1013 					else if (i == 1)
1014 						*q = FORCE2ASCII(*q);
1015 					else /* multi byte char */
1016 						q += (i-1);
1017 				}
1018 				q++;
1019 			}
1020 			q++;
1021 		}
1022 	}
1023 }
1024 
1025 struct buf_msg *
1026 printline_syslogprotocol(const char *hname, char *msg,
1027 	int flags, int pri)
1028 {
1029 	struct buf_msg *buffer;
1030 	char *p, *start;
1031 	unsigned sdlen = 0, i = 0;
1032 	bool utf8allowed = false; /* for some fields */
1033 
1034 	DPRINTF((D_CALL|D_BUFFER|D_DATA), "printline_syslogprotocol("
1035 	    "\"%s\", \"%s\", %d, %d)\n", hname, msg, flags, pri);
1036 
1037 	buffer = buf_msg_new(0);
1038 	p = msg;
1039 	p += check_timestamp((unsigned char*) p,
1040 		&buffer->timestamp, true, !BSDOutputFormat);
1041 	DPRINTF(D_DATA, "Got timestamp \"%s\"\n", buffer->timestamp);
1042 
1043 	if (flags & ADDDATE) {
1044 		FREEPTR(buffer->timestamp);
1045 		buffer->timestamp = strdup(make_timestamp(NULL,
1046 			!BSDOutputFormat));
1047 	}
1048 
1049 	start = p;
1050 	NEXTFIELD(p);
1051 	/* extract host */
1052 	for (start = p;; p++) {
1053 		if ((*p == ' ' || *p == '\0')
1054 		    && start == p-1 && *(p-1) == '-') {
1055 			/* NILVALUE */
1056 			break;
1057 		} else if ((*p == ' ' || *p == '\0')
1058 		    && (start != p-1 || *(p-1) != '-')) {
1059 			buffer->host = strndup(start, p - start);
1060 			break;
1061 		} else {
1062 			*p = FORCE2ASCII(*p);
1063 		}
1064 	}
1065 	/* p @ SP after host */
1066 	DPRINTF(D_DATA, "Got host \"%s\"\n", buffer->host);
1067 
1068 	/* extract app-name */
1069 	NEXTFIELD(p);
1070 	for (start = p;; p++) {
1071 		if ((*p == ' ' || *p == '\0')
1072 		    && start == p-1 && *(p-1) == '-') {
1073 			/* NILVALUE */
1074 			break;
1075 		} else if ((*p == ' ' || *p == '\0')
1076 		    && (start != p-1 || *(p-1) != '-')) {
1077 			buffer->prog = strndup(start, p - start);
1078 			break;
1079 		} else {
1080 			*p = FORCE2ASCII(*p);
1081 		}
1082 	}
1083 	DPRINTF(D_DATA, "Got prog \"%s\"\n", buffer->prog);
1084 
1085 	/* extract procid */
1086 	NEXTFIELD(p);
1087 	for (start = p;; p++) {
1088 		if ((*p == ' ' || *p == '\0')
1089 		    && start == p-1 && *(p-1) == '-') {
1090 			/* NILVALUE */
1091 			break;
1092 		} else if ((*p == ' ' || *p == '\0')
1093 		    && (start != p-1 || *(p-1) != '-')) {
1094 			buffer->pid = strndup(start, p - start);
1095 			start = p;
1096 			break;
1097 		} else {
1098 			*p = FORCE2ASCII(*p);
1099 		}
1100 	}
1101 	DPRINTF(D_DATA, "Got pid \"%s\"\n", buffer->pid);
1102 
1103 	/* extract msgid */
1104 	NEXTFIELD(p);
1105 	for (start = p;; p++) {
1106 		if ((*p == ' ' || *p == '\0')
1107 		    && start == p-1 && *(p-1) == '-') {
1108 			/* NILVALUE */
1109 			start = p+1;
1110 			break;
1111 		} else if ((*p == ' ' || *p == '\0')
1112 		    && (start != p-1 || *(p-1) != '-')) {
1113 			buffer->msgid = strndup(start, p - start);
1114 			start = p+1;
1115 			break;
1116 		} else {
1117 			*p = FORCE2ASCII(*p);
1118 		}
1119 	}
1120 	DPRINTF(D_DATA, "Got msgid \"%s\"\n", buffer->msgid);
1121 
1122 	/* extract SD */
1123 	NEXTFIELD(p);
1124 	start = p;
1125 	sdlen = check_sd(p);
1126 	DPRINTF(D_DATA, "check_sd(\"%s\") returned %d\n", p, sdlen);
1127 
1128 	if (sdlen == 1 && *p == '-') {
1129 		/* NILVALUE */
1130 		p++;
1131 	} else if (sdlen > 1) {
1132 		buffer->sd = strndup(p, sdlen);
1133 		p += sdlen;
1134 	} else {
1135 		DPRINTF(D_DATA, "format error\n");
1136 	}
1137 	if	(*p == '\0') start = p;
1138 	else if (*p == ' ')  start = ++p; /* SP */
1139 	DPRINTF(D_DATA, "Got SD \"%s\"\n", buffer->sd);
1140 
1141 	/* and now the message itself
1142 	 * note: move back to last start to check for BOM
1143 	 */
1144 all_syslog_msg:
1145 	p = start;
1146 
1147 	/* check for UTF-8-BOM */
1148 	if (IS_BOM(p)) {
1149 		DPRINTF(D_DATA, "UTF-8 BOM\n");
1150 		utf8allowed = true;
1151 		p += 3;
1152 	}
1153 
1154 	if (*p != '\0' && !utf8allowed) {
1155 		size_t msglen;
1156 
1157 		msglen = strlen(p);
1158 		assert(!buffer->msg);
1159 		buffer->msg = copy_utf8_ascii(p, msglen);
1160 		buffer->msgorig = buffer->msg;
1161 		buffer->msglen = buffer->msgsize = strlen(buffer->msg)+1;
1162 	} else if (*p != '\0' && utf8allowed) {
1163 		while (*p != '\0') {
1164 			i = valid_utf8(p);
1165 			if (i == 0)
1166 				*p++ = '?';
1167 			else if (i == 1)
1168 				*p = FORCE2ASCII(*p);
1169 			p += i;
1170 		}
1171 		assert(p != start);
1172 		assert(!buffer->msg);
1173 		buffer->msg = strndup(start, p - start);
1174 		buffer->msgorig = buffer->msg;
1175 		buffer->msglen = buffer->msgsize = 1 + p - start;
1176 	}
1177 	DPRINTF(D_DATA, "Got msg \"%s\"\n", buffer->msg);
1178 
1179 	buffer->recvhost = strdup(hname);
1180 	buffer->pri = pri;
1181 	buffer->flags = flags;
1182 
1183 	return buffer;
1184 }
1185 
1186 /* copies an input into a new ASCII buffer
1187  * ASCII controls are converted to format "^X"
1188  * multi-byte UTF-8 chars are converted to format "<ab><cd>"
1189  */
1190 #define INIT_BUFSIZE 512
1191 char *
1192 copy_utf8_ascii(char *p, size_t p_len)
1193 {
1194 	size_t idst = 0, isrc = 0, dstsize = INIT_BUFSIZE, i;
1195 	char *dst, *tmp_dst;
1196 
1197 	MALLOC(dst, dstsize);
1198 	while (isrc < p_len) {
1199 		if (dstsize < idst + 10) {
1200 			/* check for enough space for \0 and a UTF-8
1201 			 * conversion; longest possible is <U+123456> */
1202 			tmp_dst = realloc(dst, dstsize + INIT_BUFSIZE);
1203 			if (!tmp_dst)
1204 				break;
1205 			dst = tmp_dst;
1206 			dstsize += INIT_BUFSIZE;
1207 		}
1208 
1209 		i = valid_utf8(&p[isrc]);
1210 		if (i == 0) { /* invalid encoding */
1211 			dst[idst++] = '?';
1212 			isrc++;
1213 		} else if (i == 1) { /* check printable */
1214 			if (iscntrl((unsigned char)p[isrc])
1215 			 && p[isrc] != '\t') {
1216 				if (p[isrc] == '\n') {
1217 					dst[idst++] = ' ';
1218 					isrc++;
1219 				} else {
1220 					dst[idst++] = '^';
1221 					dst[idst++] = p[isrc++] ^ 0100;
1222 				}
1223 			} else
1224 				dst[idst++] = p[isrc++];
1225 		} else {  /* convert UTF-8 to ASCII */
1226 			dst[idst++] = '<';
1227 			idst += snprintf(&dst[idst], dstsize - idst, "U+%x",
1228 			    get_utf8_value(&p[isrc]));
1229 			isrc += i;
1230 			dst[idst++] = '>';
1231 		}
1232 	}
1233 	dst[idst] = '\0';
1234 
1235 	/* shrink buffer to right size */
1236 	tmp_dst = realloc(dst, idst+1);
1237 	if (tmp_dst)
1238 		return tmp_dst;
1239 	else
1240 		return dst;
1241 }
1242 
1243 struct buf_msg *
1244 printline_bsdsyslog(const char *hname, char *msg,
1245 	int flags, int pri)
1246 {
1247 	struct buf_msg *buffer;
1248 	char *p, *start;
1249 	unsigned msgidlen = 0, sdlen = 0;
1250 
1251 	DPRINTF((D_CALL|D_BUFFER|D_DATA), "printline_bsdsyslog("
1252 		"\"%s\", \"%s\", %d, %d)\n", hname, msg, flags, pri);
1253 
1254 	buffer = buf_msg_new(0);
1255 	p = msg;
1256 	p += check_timestamp((unsigned char*) p,
1257 		&buffer->timestamp, false, !BSDOutputFormat);
1258 	DPRINTF(D_DATA, "Got timestamp \"%s\"\n", buffer->timestamp);
1259 
1260 	if (flags & ADDDATE || !buffer->timestamp) {
1261 		FREEPTR(buffer->timestamp);
1262 		buffer->timestamp = strdup(make_timestamp(NULL,
1263 			!BSDOutputFormat));
1264 	}
1265 
1266 	if (*p == ' ') p++; /* SP */
1267 	else goto all_bsd_msg;
1268 	/* in any error case we skip header parsing and
1269 	 * treat all following data as message content */
1270 
1271 	/* extract host */
1272 	for (start = p;; p++) {
1273 		if (*p == ' ' || *p == '\0') {
1274 			buffer->host = strndup(start, p - start);
1275 			break;
1276 		} else if (*p == '[' || (*p == ':'
1277 			&& (*(p+1) == ' ' || *(p+1) == '\0'))) {
1278 			/* no host in message */
1279 			buffer->host = LocalFQDN;
1280 			buffer->prog = strndup(start, p - start);
1281 			break;
1282 		} else {
1283 			*p = FORCE2ASCII(*p);
1284 		}
1285 	}
1286 	DPRINTF(D_DATA, "Got host \"%s\"\n", buffer->host);
1287 	/* p @ SP after host, or @ :/[ after prog */
1288 
1289 	/* extract program */
1290 	if (!buffer->prog) {
1291 		if (*p == ' ') p++; /* SP */
1292 		else goto all_bsd_msg;
1293 
1294 		for (start = p;; p++) {
1295 			if (*p == ' ' || *p == '\0') { /* error */
1296 				goto all_bsd_msg;
1297 			} else if (*p == '[' || (*p == ':'
1298 				&& (*(p+1) == ' ' || *(p+1) == '\0'))) {
1299 				buffer->prog = strndup(start, p - start);
1300 				break;
1301 			} else {
1302 				*p = FORCE2ASCII(*p);
1303 			}
1304 		}
1305 	}
1306 	DPRINTF(D_DATA, "Got prog \"%s\"\n", buffer->prog);
1307 	start = p;
1308 
1309 	/* p @ :/[ after prog */
1310 	if (*p == '[') {
1311 		p++;
1312 		if (*p == ' ') p++; /* SP */
1313 		for (start = p;; p++) {
1314 			if (*p == ' ' || *p == '\0') { /* error */
1315 				goto all_bsd_msg;
1316 			} else if (*p == ']') {
1317 				buffer->pid = strndup(start, p - start);
1318 				break;
1319 			} else {
1320 				*p = FORCE2ASCII(*p);
1321 			}
1322 		}
1323 	}
1324 	DPRINTF(D_DATA, "Got pid \"%s\"\n", buffer->pid);
1325 
1326 	if (*p == ']') p++;
1327 	if (*p == ':') p++;
1328 	if (*p == ' ') p++;
1329 
1330 	/* p @ msgid, @ opening [ of SD or @ first byte of message
1331 	 * accept either case and try to detect MSGID and SD fields
1332 	 *
1333 	 * only limitation: we do not accept UTF-8 data in
1334 	 * BSD Syslog messages -- so all SD values are ASCII-filtered
1335 	 *
1336 	 * I have found one scenario with 'unexpected' behaviour:
1337 	 * if there is only a SD intended, but a) it is short enough
1338 	 * to be a MSGID and b) the first word of the message can also
1339 	 * be parsed as an SD.
1340 	 * example:
1341 	 * "<35>Jul  6 12:39:08 tag[123]: [exampleSDID@0] - hello"
1342 	 * --> parsed as
1343 	 *     MSGID = "[exampleSDID@0]"
1344 	 *     SD    = "-"
1345 	 *     MSG   = "hello"
1346 	 */
1347 	start = p;
1348 	msgidlen = check_msgid(p);
1349 	if (msgidlen) /* check for SD in 2nd field */
1350 		sdlen = check_sd(p+msgidlen+1);
1351 
1352 	if (msgidlen && sdlen) {
1353 		/* MSGID in 1st and SD in 2nd field
1354 		 * now check for NILVALUEs and copy */
1355 		if (msgidlen == 1 && *p == '-') {
1356 			p++; /* - */
1357 			p++; /* SP */
1358 			DPRINTF(D_DATA, "Got MSGID \"-\"\n");
1359 		} else {
1360 			/* only has ASCII chars after check_msgid() */
1361 			buffer->msgid = strndup(p, msgidlen);
1362 			p += msgidlen;
1363 			p++; /* SP */
1364 			DPRINTF(D_DATA, "Got MSGID \"%s\"\n",
1365 				buffer->msgid);
1366 		}
1367 	} else {
1368 		/* either no msgid or no SD in 2nd field
1369 		 * --> check 1st field for SD */
1370 		DPRINTF(D_DATA, "No MSGID\n");
1371 		sdlen = check_sd(p);
1372 	}
1373 
1374 	if (sdlen == 0) {
1375 		DPRINTF(D_DATA, "No SD\n");
1376 	} else if (sdlen > 1) {
1377 		buffer->sd = copy_utf8_ascii(p, sdlen);
1378 		DPRINTF(D_DATA, "Got SD \"%s\"\n", buffer->sd);
1379 	} else if (sdlen == 1 && *p == '-') {
1380 		p++;
1381 		DPRINTF(D_DATA, "Got SD \"-\"\n");
1382 	} else {
1383 		DPRINTF(D_DATA, "Error\n");
1384 	}
1385 
1386 	if (*p == ' ') p++;
1387 	start = p;
1388 	/* and now the message itself
1389 	 * note: do not reset start, because we might come here
1390 	 * by goto and want to have the incomplete field as part
1391 	 * of the msg
1392 	 */
1393 all_bsd_msg:
1394 	if (*p != '\0') {
1395 		size_t msglen = strlen(p);
1396 		buffer->msg = copy_utf8_ascii(p, msglen);
1397 		buffer->msgorig = buffer->msg;
1398 		buffer->msglen = buffer->msgsize = strlen(buffer->msg)+1;
1399 	}
1400 	DPRINTF(D_DATA, "Got msg \"%s\"\n", buffer->msg);
1401 
1402 	buffer->recvhost = strdup(hname);
1403 	buffer->pri = pri;
1404 	buffer->flags = flags | BSDSYSLOG;
1405 
1406 	return buffer;
1407 }
1408 
1409 struct buf_msg *
1410 printline_kernelprintf(const char *hname, char *msg,
1411 	int flags, int pri)
1412 {
1413 	struct buf_msg *buffer;
1414 	char *p;
1415 	unsigned sdlen = 0;
1416 
1417 	DPRINTF((D_CALL|D_BUFFER|D_DATA), "printline_kernelprintf("
1418 		"\"%s\", \"%s\", %d, %d)\n", hname, msg, flags, pri);
1419 
1420 	buffer = buf_msg_new(0);
1421 	buffer->timestamp = strdup(make_timestamp(NULL, !BSDOutputFormat));
1422 	buffer->pri = pri;
1423 	buffer->flags = flags;
1424 
1425 	/* assume there is no MSGID but there might be SD */
1426 	p = msg;
1427 	sdlen = check_sd(p);
1428 
1429 	if (sdlen == 0) {
1430 		DPRINTF(D_DATA, "No SD\n");
1431 	} else if (sdlen > 1) {
1432 		buffer->sd = copy_utf8_ascii(p, sdlen);
1433 		DPRINTF(D_DATA, "Got SD \"%s\"\n", buffer->sd);
1434 	} else if (sdlen == 1 && *p == '-') {
1435 		p++;
1436 		DPRINTF(D_DATA, "Got SD \"-\"\n");
1437 	} else {
1438 		DPRINTF(D_DATA, "Error\n");
1439 	}
1440 
1441 	if (*p == ' ') p++;
1442 	if (*p != '\0') {
1443 		size_t msglen = strlen(p);
1444 		buffer->msg = copy_utf8_ascii(p, msglen);
1445 		buffer->msgorig = buffer->msg;
1446 		buffer->msglen = buffer->msgsize = strlen(buffer->msg)+1;
1447 	}
1448 	DPRINTF(D_DATA, "Got msg \"%s\"\n", buffer->msg);
1449 
1450 	return buffer;
1451 }
1452 
1453 /*
1454  * Take a raw input line, read priority and version, call the
1455  * right message parsing function, then call logmsg().
1456  */
1457 void
1458 printline(const char *hname, char *msg, int flags)
1459 {
1460 	struct buf_msg *buffer;
1461 	int pri;
1462 	char *p, *q;
1463 	long n;
1464 	bool bsdsyslog = true;
1465 
1466 	DPRINTF((D_CALL|D_BUFFER|D_DATA),
1467 		"printline(\"%s\", \"%s\", %d)\n", hname, msg, flags);
1468 
1469 	/* test for special codes */
1470 	pri = DEFUPRI;
1471 	p = msg;
1472 	if (*p == '<') {
1473 		errno = 0;
1474 		n = strtol(p + 1, &q, 10);
1475 		if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) {
1476 			p = q + 1;
1477 			pri = (int)n;
1478 			/* check for syslog-protocol version */
1479 			if (*p == '1' && p[1] == ' ') {
1480 				p += 2;	 /* skip version and space */
1481 				bsdsyslog = false;
1482 			} else {
1483 				bsdsyslog = true;
1484 			}
1485 		}
1486 	}
1487 	if (pri & ~(LOG_FACMASK|LOG_PRIMASK))
1488 		pri = DEFUPRI;
1489 
1490 	/*
1491 	 * Don't allow users to log kernel messages.
1492 	 * NOTE: Since LOG_KERN == 0, this will also match
1493 	 *	 messages with no facility specified.
1494 	 */
1495 	if ((pri & LOG_FACMASK) == LOG_KERN)
1496 		pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
1497 
1498 	if (bsdsyslog) {
1499 		buffer = printline_bsdsyslog(hname, p, flags, pri);
1500 	} else {
1501 		buffer = printline_syslogprotocol(hname, p, flags, pri);
1502 	}
1503 	logmsg(buffer);
1504 	DELREF(buffer);
1505 }
1506 
1507 /*
1508  * Take a raw input line from /dev/klog, split and format similar to syslog().
1509  */
1510 void
1511 printsys(char *msg)
1512 {
1513 	int n, is_printf, pri, flags;
1514 	char *p, *q;
1515 	struct buf_msg *buffer;
1516 
1517 	klog_linebufoff = 0;
1518 	for (p = msg; *p != '\0'; ) {
1519 		bool bsdsyslog = true;
1520 
1521 		is_printf = 1;
1522 		flags = ISKERNEL | ADDDATE | BSDSYSLOG;
1523 		if (SyncKernel)
1524 			flags |= SYNC_FILE;
1525 		if (is_printf) /* kernel printf's come out on console */
1526 			flags |= IGN_CONS;
1527 		pri = DEFSPRI;
1528 
1529 		if (*p == '<') {
1530 			errno = 0;
1531 			n = (int)strtol(p + 1, &q, 10);
1532 			if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) {
1533 				p = q + 1;
1534 				is_printf = 0;
1535 				pri = n;
1536 				if (*p == '1') { /* syslog-protocol version */
1537 					p += 2;	 /* skip version and space */
1538 					bsdsyslog = false;
1539 				} else {
1540 					bsdsyslog = true;
1541 				}
1542 			}
1543 		}
1544 		for (q = p; *q != '\0' && *q != '\n'; q++)
1545 			/* look for end of line; no further checks.
1546 			 * trust the kernel to send ASCII only */;
1547 		if (*q != '\0')
1548 			*q++ = '\0';
1549 		else {
1550 			memcpy(linebuf, p, klog_linebufoff = q - p);
1551 			break;
1552 		}
1553 
1554 		if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
1555 			pri = DEFSPRI;
1556 
1557 		/* allow all kinds of input from kernel */
1558 		if (is_printf)
1559 			buffer = printline_kernelprintf(
1560 			    LocalFQDN, p, flags, pri);
1561 		else {
1562 			if (bsdsyslog)
1563 				buffer = printline_bsdsyslog(
1564 				    LocalFQDN, p, flags, pri);
1565 			else
1566 				buffer = printline_syslogprotocol(
1567 				    LocalFQDN, p, flags, pri);
1568 		}
1569 
1570 		/* set fields left open */
1571 		if (!buffer->prog)
1572 			buffer->prog = strdup(_PATH_UNIX);
1573 		if (!buffer->host)
1574 			buffer->host = LocalFQDN;
1575 		if (!buffer->recvhost)
1576 			buffer->recvhost = LocalFQDN;
1577 
1578 		logmsg(buffer);
1579 		DELREF(buffer);
1580 		p = q;
1581 	}
1582 }
1583 
1584 /*
1585  * Check to see if `name' matches the provided specification, using the
1586  * specified strstr function.
1587  */
1588 int
1589 matches_spec(const char *name, const char *spec,
1590     char *(*check)(const char *, const char *))
1591 {
1592 	const char *s;
1593 	const char *cursor;
1594 	char prev, next;
1595 	size_t len;
1596 
1597 	if (name[0] == '\0')
1598 		return 0;
1599 
1600 	if (strchr(name, ',')) /* sanity */
1601 		return 0;
1602 
1603 	len = strlen(name);
1604 	cursor = spec;
1605 	while ((s = (*check)(cursor, name)) != NULL) {
1606 		prev = s == spec ? ',' : *(s - 1);
1607 		cursor = s + len;
1608 		next = *cursor;
1609 
1610 		if (prev == ',' && (next == '\0' || next == ','))
1611 			return 1;
1612 	}
1613 
1614 	return 0;
1615 }
1616 
1617 /*
1618  * wrapper with old function signature,
1619  * keeps calling code shorter and hides buffer allocation
1620  */
1621 void
1622 logmsg_async(int pri, const char *sd, const char *msg, int flags)
1623 {
1624 	struct buf_msg *buffer;
1625 	size_t msglen;
1626 
1627 	DPRINTF((D_CALL|D_DATA), "logmsg_async(%d, \"%s\", \"%s\", %d)\n",
1628 	    pri, sd, msg, flags);
1629 
1630 	if (msg) {
1631 		msglen = strlen(msg);
1632 		msglen++;		/* adds \0 */
1633 		buffer = buf_msg_new(msglen);
1634 		buffer->msglen = strlcpy(buffer->msg, msg, msglen) + 1;
1635 	} else {
1636 		buffer = buf_msg_new(0);
1637 	}
1638 	if (sd) buffer->sd = strdup(sd);
1639 	buffer->timestamp = strdup(make_timestamp(NULL, !BSDOutputFormat));
1640 	buffer->prog = appname;
1641 	buffer->pid = include_pid;
1642 	buffer->recvhost = buffer->host = LocalFQDN;
1643 	buffer->pri = pri;
1644 	buffer->flags = flags;
1645 
1646 	logmsg(buffer);
1647 	DELREF(buffer);
1648 }
1649 
1650 /* read timestamp in from_buf, convert into a timestamp in to_buf
1651  *
1652  * returns length of timestamp found in from_buf (= number of bytes consumed)
1653  */
1654 size_t
1655 check_timestamp(unsigned char *from_buf, char **to_buf,
1656 	bool from_iso, bool to_iso)
1657 {
1658 	unsigned char *q;
1659 	int p;
1660 	bool found_ts = false;
1661 
1662 	DPRINTF((D_CALL|D_DATA), "check_timestamp(%p = \"%s\", from_iso=%d, "
1663 	    "to_iso=%d)\n", from_buf, from_buf, from_iso, to_iso);
1664 
1665 	if (!from_buf) return 0;
1666 	/*
1667 	 * Check to see if msg looks non-standard.
1668 	 * looks at every char because we do not have a msg length yet
1669 	 */
1670 	/* detailed checking adapted from Albert Mietus' sl_timestamp.c */
1671 	if (from_iso) {
1672 		if (from_buf[4] == '-' && from_buf[7] == '-'
1673 		    && from_buf[10] == 'T' && from_buf[13] == ':'
1674 		    && from_buf[16] == ':'
1675 		    && isdigit(from_buf[0]) && isdigit(from_buf[1])
1676 		    && isdigit(from_buf[2]) && isdigit(from_buf[3])  /* YYYY */
1677 		    && isdigit(from_buf[5]) && isdigit(from_buf[6])
1678 		    && isdigit(from_buf[8]) && isdigit(from_buf[9])  /* mm dd */
1679 		    && isdigit(from_buf[11]) && isdigit(from_buf[12]) /* HH */
1680 		    && isdigit(from_buf[14]) && isdigit(from_buf[15]) /* MM */
1681 		    && isdigit(from_buf[17]) && isdigit(from_buf[18]) /* SS */
1682 		    )  {
1683 			/* time-secfrac */
1684 			if (from_buf[19] == '.')
1685 				for (p=20; isdigit(from_buf[p]); p++) /* NOP*/;
1686 			else
1687 				p = 19;
1688 			/* time-offset */
1689 			if (from_buf[p] == 'Z'
1690 			 || ((from_buf[p] == '+' || from_buf[p] == '-')
1691 			    && from_buf[p+3] == ':'
1692 			    && isdigit(from_buf[p+1]) && isdigit(from_buf[p+2])
1693 			    && isdigit(from_buf[p+4]) && isdigit(from_buf[p+5])
1694 			 ))
1695 				found_ts = true;
1696 		}
1697 	} else {
1698 		if (from_buf[3] == ' ' && from_buf[6] == ' '
1699 		    && from_buf[9] == ':' && from_buf[12] == ':'
1700 		    && (from_buf[4] == ' ' || isdigit(from_buf[4]))
1701 		    && isdigit(from_buf[5]) /* dd */
1702 		    && isdigit(from_buf[7])  && isdigit(from_buf[8])   /* HH */
1703 		    && isdigit(from_buf[10]) && isdigit(from_buf[11])  /* MM */
1704 		    && isdigit(from_buf[13]) && isdigit(from_buf[14])  /* SS */
1705 		    && isupper(from_buf[0]) && islower(from_buf[1]) /* month */
1706 		    && islower(from_buf[2]))
1707 			found_ts = true;
1708 	}
1709 	if (!found_ts) {
1710 		if (from_buf[0] == '-' && from_buf[1] == ' ') {
1711 			/* NILVALUE */
1712 			if (to_iso) {
1713 				/* with ISO = syslog-protocol output leave
1714 			 	 * it as is, because it is better to have
1715 			 	 * no timestamp than a wrong one.
1716 			 	 */
1717 				*to_buf = strdup("-");
1718 			} else {
1719 				/* with BSD Syslog the field is reqired
1720 				 * so replace it with current time
1721 				 */
1722 				*to_buf = strdup(make_timestamp(NULL, false));
1723 			}
1724 			return 2;
1725 		}
1726 		return 0;
1727 	}
1728 
1729 	if (!from_iso && !to_iso) {
1730 		/* copy BSD timestamp */
1731 		DPRINTF(D_CALL, "check_timestamp(): copy BSD timestamp\n");
1732 		*to_buf = strndup((char *)from_buf, BSD_TIMESTAMPLEN);
1733 		return BSD_TIMESTAMPLEN;
1734 	} else if (from_iso && to_iso) {
1735 		/* copy ISO timestamp */
1736 		DPRINTF(D_CALL, "check_timestamp(): copy ISO timestamp\n");
1737 		if (!(q = (unsigned char *) strchr((char *)from_buf, ' ')))
1738 			q = from_buf + strlen((char *)from_buf);
1739 		*to_buf = strndup((char *)from_buf, q - from_buf);
1740 		return q - from_buf;
1741 	} else if (from_iso && !to_iso) {
1742 		/* convert ISO->BSD */
1743 		struct tm parsed;
1744 		time_t timeval;
1745 		char tsbuf[MAX_TIMESTAMPLEN];
1746 		int i = 0;
1747 
1748 		DPRINTF(D_CALL, "check_timestamp(): convert ISO->BSD\n");
1749 		for(i = 0; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0'
1750 		    && from_buf[i] != '.' && from_buf[i] != ' '; i++)
1751 			tsbuf[i] = from_buf[i]; /* copy date & time */
1752 		for(; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0'
1753 		    && from_buf[i] != '+' && from_buf[i] != '-'
1754 		    && from_buf[i] != 'Z' && from_buf[i] != ' '; i++)
1755 			;			   /* skip fraction digits */
1756 		for(; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0'
1757 		    && from_buf[i] != ':' && from_buf[i] != ' ' ; i++)
1758 			tsbuf[i] = from_buf[i]; /* copy TZ */
1759 		if (from_buf[i] == ':') i++;	/* skip colon */
1760 		for(; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0'
1761 		    && from_buf[i] != ' ' ; i++)
1762 			tsbuf[i] = from_buf[i]; /* copy TZ */
1763 
1764 		(void)memset(&parsed, 0, sizeof(parsed));
1765 		parsed.tm_isdst = -1;
1766 		(void)strptime(tsbuf, "%FT%T%z", &parsed);
1767 		timeval = mktime(&parsed);
1768 
1769 		*to_buf = strndup(make_timestamp(&timeval, false),
1770 		    BSD_TIMESTAMPLEN);
1771 		return i;
1772 	} else if (!from_iso && to_iso) {
1773 		/* convert BSD->ISO */
1774 		struct tm parsed;
1775 		struct tm *current;
1776 		time_t timeval;
1777 		char *rc;
1778 
1779 		(void)memset(&parsed, 0, sizeof(parsed));
1780 		parsed.tm_isdst = -1;
1781 		DPRINTF(D_CALL, "check_timestamp(): convert BSD->ISO\n");
1782 		rc = strptime((char *)from_buf, "%b %d %T", &parsed);
1783 		current = gmtime(&now);
1784 
1785 		/* use current year and timezone */
1786 		parsed.tm_isdst = current->tm_isdst;
1787 		parsed.tm_gmtoff = current->tm_gmtoff;
1788 		parsed.tm_year = current->tm_year;
1789 		if (current->tm_mon == 0 && parsed.tm_mon == 11)
1790 			parsed.tm_year--;
1791 
1792 		timeval = mktime(&parsed);
1793 		rc = make_timestamp(&timeval, true);
1794 		*to_buf = strndup(rc, MAX_TIMESTAMPLEN-1);
1795 
1796 		return BSD_TIMESTAMPLEN;
1797 	} else {
1798 		DPRINTF(D_MISC,
1799 			"Executing unreachable code in check_timestamp()\n");
1800 		return 0;
1801 	}
1802 }
1803 
1804 /*
1805  * Log a message to the appropriate log files, users, etc. based on
1806  * the priority.
1807  */
1808 void
1809 logmsg(struct buf_msg *buffer)
1810 {
1811 	struct filed *f;
1812 	int fac, omask, prilev;
1813 
1814 	DPRINTF((D_CALL|D_BUFFER), "logmsg: buffer@%p, pri 0%o/%d, flags 0x%x,"
1815 	    " timestamp \"%s\", from \"%s\", sd \"%s\", msg \"%s\"\n",
1816 	    buffer, buffer->pri, buffer->pri, buffer->flags,
1817 	    buffer->timestamp, buffer->recvhost, buffer->sd, buffer->msg);
1818 
1819 	omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
1820 
1821 	/* sanity check */
1822 	assert(buffer->refcount == 1);
1823 	assert(buffer->msglen <= buffer->msgsize);
1824 	assert(buffer->msgorig <= buffer->msg);
1825 	assert((buffer->msg && buffer->msglen == strlen(buffer->msg)+1)
1826 	      || (!buffer->msg && !buffer->msglen));
1827 	if (!buffer->msg && !buffer->sd && !buffer->msgid)
1828 		DPRINTF(D_BUFFER, "Empty message?\n");
1829 
1830 	/* extract facility and priority level */
1831 	if (buffer->flags & MARK)
1832 		fac = LOG_NFACILITIES;
1833 	else
1834 		fac = LOG_FAC(buffer->pri);
1835 	prilev = LOG_PRI(buffer->pri);
1836 
1837 	/* log the message to the particular outputs */
1838 	if (!Initialized) {
1839 		f = &consfile;
1840 		f->f_file = open(ctty, O_WRONLY | O_NDELAY, 0);
1841 
1842 		if (f->f_file >= 0) {
1843 			DELREF(f->f_prevmsg);
1844 			f->f_prevmsg = NEWREF(buffer);
1845 			fprintlog(f, NEWREF(buffer), NULL);
1846 			DELREF(buffer);
1847 			(void)close(f->f_file);
1848 		}
1849 		(void)sigsetmask(omask);
1850 		return;
1851 	}
1852 
1853 	for (f = Files; f; f = f->f_next) {
1854 		/* skip messages that are incorrect priority */
1855 		if (!MATCH_PRI(f, fac, prilev)
1856 		    || f->f_pmask[fac] == INTERNAL_NOPRI)
1857 			continue;
1858 
1859 		/* skip messages with the incorrect host name */
1860 		/* do we compare with host (IMHO correct) or recvhost */
1861 		/* (compatible)? */
1862 		if (f->f_host != NULL && buffer->host != NULL) {
1863 			char shost[MAXHOSTNAMELEN + 1], *h;
1864 			if (!BSDOutputFormat) {
1865 				h = buffer->host;
1866 			} else {
1867 				(void)strlcpy(shost, buffer->host,
1868 				    sizeof(shost));
1869 				trim_anydomain(shost);
1870 				h = shost;
1871 			}
1872 			switch (f->f_host[0]) {
1873 			case '+':
1874 				if (! matches_spec(h, f->f_host + 1,
1875 				    strcasestr))
1876 					continue;
1877 				break;
1878 			case '-':
1879 				if (matches_spec(h, f->f_host + 1,
1880 				    strcasestr))
1881 					continue;
1882 				break;
1883 			}
1884 		}
1885 
1886 		/* skip messages with the incorrect program name */
1887 		if (f->f_program != NULL && buffer->prog != NULL) {
1888 			switch (f->f_program[0]) {
1889 			case '+':
1890 				if (!matches_spec(buffer->prog,
1891 				    f->f_program + 1, strstr))
1892 					continue;
1893 				break;
1894 			case '-':
1895 				if (matches_spec(buffer->prog,
1896 				    f->f_program + 1, strstr))
1897 					continue;
1898 				break;
1899 			default:
1900 				if (!matches_spec(buffer->prog,
1901 				    f->f_program, strstr))
1902 					continue;
1903 				break;
1904 			}
1905 		}
1906 
1907 		if (f->f_type == F_CONSOLE && (buffer->flags & IGN_CONS))
1908 			continue;
1909 
1910 		/* don't output marks to recently written files */
1911 		if ((buffer->flags & MARK)
1912 		 && (now - f->f_time) < MarkInterval / 2)
1913 			continue;
1914 
1915 		/*
1916 		 * suppress duplicate lines to this file unless NoRepeat
1917 		 */
1918 #define MSG_FIELD_EQ(x) ((!buffer->x && !f->f_prevmsg->x) ||	\
1919     (buffer->x && f->f_prevmsg->x && !strcmp(buffer->x, f->f_prevmsg->x)))
1920 
1921 		if ((buffer->flags & MARK) == 0 &&
1922 		    f->f_prevmsg &&
1923 		    buffer->msglen == f->f_prevmsg->msglen &&
1924 		    !NoRepeat &&
1925 		    MSG_FIELD_EQ(host) &&
1926 		    MSG_FIELD_EQ(sd) &&
1927 		    MSG_FIELD_EQ(msg)
1928 		    ) {
1929 			f->f_prevcount++;
1930 			DPRINTF(D_DATA, "Msg repeated %d times, %ld sec of %d\n",
1931 			    f->f_prevcount, (long)(now - f->f_time),
1932 			    repeatinterval[f->f_repeatcount]);
1933 			/*
1934 			 * If domark would have logged this by now,
1935 			 * flush it now (so we don't hold isolated messages),
1936 			 * but back off so we'll flush less often
1937 			 * in the future.
1938 			 */
1939 			if (now > REPEATTIME(f)) {
1940 				fprintlog(f, NEWREF(buffer), NULL);
1941 				DELREF(buffer);
1942 				BACKOFF(f);
1943 			}
1944 		} else {
1945 			/* new line, save it */
1946 			if (f->f_prevcount)
1947 				fprintlog(f, NULL, NULL);
1948 			f->f_repeatcount = 0;
1949 			DELREF(f->f_prevmsg);
1950 			f->f_prevmsg = NEWREF(buffer);
1951 			fprintlog(f, NEWREF(buffer), NULL);
1952 			DELREF(buffer);
1953 		}
1954 	}
1955 	(void)sigsetmask(omask);
1956 }
1957 
1958 /*
1959  * format one buffer into output format given by flag BSDOutputFormat
1960  * line is allocated and has to be free()d by caller
1961  * size_t pointers are optional, if not NULL then they will return
1962  *   different lenghts used for formatting and output
1963  */
1964 #define OUT(x) ((x)?(x):"-")
1965 bool
1966 format_buffer(struct buf_msg *buffer, char **line, size_t *ptr_linelen,
1967 	size_t *ptr_msglen, size_t *ptr_tlsprefixlen, size_t *ptr_prilen)
1968 {
1969 #define FPBUFSIZE 30
1970 	static char ascii_empty[] = "";
1971 	char fp_buf[FPBUFSIZE] = "\0";
1972 	char *hostname, *shorthostname = NULL;
1973 	char *ascii_sd = ascii_empty;
1974 	char *ascii_msg = ascii_empty;
1975 	size_t linelen, msglen, tlsprefixlen, prilen, j;
1976 
1977 	DPRINTF(D_CALL, "format_buffer(%p)\n", buffer);
1978 	if (!buffer) return false;
1979 
1980 	/* All buffer fields are set with strdup(). To avoid problems
1981 	 * on memory exhaustion we allow them to be empty and replace
1982 	 * the essential fields with already allocated generic values.
1983 	 */
1984 	if (!buffer->timestamp)
1985 		buffer->timestamp = timestamp;
1986 	if (!buffer->host && !buffer->recvhost)
1987 		buffer->host = LocalFQDN;
1988 
1989 	if (LogFacPri) {
1990 		const char *f_s = NULL, *p_s = NULL;
1991 		int fac = buffer->pri & LOG_FACMASK;
1992 		int pri = LOG_PRI(buffer->pri);
1993 		char f_n[5], p_n[5];
1994 
1995 		if (LogFacPri > 1) {
1996 			CODE *c;
1997 
1998 			for (c = facilitynames; c->c_name != NULL; c++) {
1999 				if (c->c_val == fac) {
2000 					f_s = c->c_name;
2001 					break;
2002 				}
2003 			}
2004 			for (c = prioritynames; c->c_name != NULL; c++) {
2005 				if (c->c_val == pri) {
2006 					p_s = c->c_name;
2007 					break;
2008 				}
2009 			}
2010 		}
2011 		if (f_s == NULL) {
2012 			snprintf(f_n, sizeof(f_n), "%d", LOG_FAC(fac));
2013 			f_s = f_n;
2014 		}
2015 		if (p_s == NULL) {
2016 			snprintf(p_n, sizeof(p_n), "%d", pri);
2017 			p_s = p_n;
2018 		}
2019 		snprintf(fp_buf, sizeof(fp_buf), "<%s.%s>", f_s, p_s);
2020 	}
2021 
2022 	/* hostname or FQDN */
2023 	hostname = (buffer->host ? buffer->host : buffer->recvhost);
2024 	if (BSDOutputFormat
2025 	 && (shorthostname = strdup(hostname))) {
2026 		/* if the previous BSD output format with "host [recvhost]:"
2027 		 * gets implemented, this is the right place to distinguish
2028 		 * between buffer->host and buffer->recvhost
2029 		 */
2030 		trim_anydomain(shorthostname);
2031 		hostname = shorthostname;
2032 	}
2033 
2034 	/* new message formatting:
2035 	 * instead of using iov always assemble one complete TLS-ready line
2036 	 * with length and priority (depending on BSDOutputFormat either in
2037 	 * BSD Syslog or syslog-protocol format)
2038 	 *
2039 	 * additionally save the length of the prefixes,
2040 	 * so UDP destinations can skip the length prefix and
2041 	 * file/pipe/wall destinations can omit length and priority
2042 	 */
2043 	/* first determine required space */
2044 	if (BSDOutputFormat) {
2045 		/* only output ASCII chars */
2046 		if (buffer->sd)
2047 			ascii_sd = copy_utf8_ascii(buffer->sd,
2048 				strlen(buffer->sd));
2049 		if (buffer->msg) {
2050 			if (IS_BOM(buffer->msg))
2051 				ascii_msg = copy_utf8_ascii(buffer->msg,
2052 					buffer->msglen - 1);
2053 			else /* assume already converted at input */
2054 				ascii_msg = buffer->msg;
2055 		}
2056 		msglen = snprintf(NULL, 0, "<%d>%s%.15s %s %s%s%s%s: %s%s%s",
2057 			     buffer->pri, fp_buf, buffer->timestamp,
2058 			     hostname, OUT(buffer->prog),
2059 			     buffer->pid ? "[" : "",
2060 			     buffer->pid ? buffer->pid : "",
2061 			     buffer->pid ? "]" : "", ascii_sd,
2062 			     (buffer->sd && buffer->msg ? " ": ""), ascii_msg);
2063 	} else
2064 		msglen = snprintf(NULL, 0, "<%d>1 %s%s %s %s %s %s %s%s%s",
2065 			     buffer->pri, fp_buf, buffer->timestamp,
2066 			     hostname, OUT(buffer->prog), OUT(buffer->pid),
2067 			     OUT(buffer->msgid), OUT(buffer->sd),
2068 			     (buffer->msg ? " ": ""),
2069 			     (buffer->msg ? buffer->msg: ""));
2070 	/* add space for length prefix */
2071 	tlsprefixlen = 0;
2072 	for (j = msglen; j; j /= 10)
2073 		tlsprefixlen++;
2074 	/* one more for the space */
2075 	tlsprefixlen++;
2076 
2077 	prilen = snprintf(NULL, 0, "<%d>", buffer->pri);
2078 	if (!BSDOutputFormat)
2079 		prilen += 2; /* version char and space */
2080 	MALLOC(*line, msglen + tlsprefixlen + 1);
2081 	if (BSDOutputFormat)
2082 		linelen = snprintf(*line,
2083 		     msglen + tlsprefixlen + 1,
2084 		     "%zu <%d>%s%.15s %s %s%s%s%s: %s%s%s",
2085 		     msglen, buffer->pri, fp_buf, buffer->timestamp,
2086 		     hostname, OUT(buffer->prog),
2087 		     (buffer->pid ? "[" : ""),
2088 		     (buffer->pid ? buffer->pid : ""),
2089 		     (buffer->pid ? "]" : ""), ascii_sd,
2090 		     (buffer->sd && buffer->msg ? " ": ""), ascii_msg);
2091 	else
2092 		linelen = snprintf(*line,
2093 		     msglen + tlsprefixlen + 1,
2094 		     "%zu <%d>1 %s%s %s %s %s %s %s%s%s",
2095 		     msglen, buffer->pri, fp_buf, buffer->timestamp,
2096 		     hostname, OUT(buffer->prog), OUT(buffer->pid),
2097 		     OUT(buffer->msgid), OUT(buffer->sd),
2098 		     (buffer->msg ? " ": ""),
2099 		     (buffer->msg ? buffer->msg: ""));
2100 	DPRINTF(D_DATA, "formatted %zu octets to: '%.*s' (linelen %zu, "
2101 	    "msglen %zu, tlsprefixlen %zu, prilen %zu)\n", linelen,
2102 	    (int)linelen, *line, linelen, msglen, tlsprefixlen, prilen);
2103 
2104 	FREEPTR(shorthostname);
2105 	if (ascii_sd != ascii_empty)
2106 		FREEPTR(ascii_sd);
2107 	if (ascii_msg != ascii_empty && ascii_msg != buffer->msg)
2108 		FREEPTR(ascii_msg);
2109 
2110 	if (ptr_linelen)      *ptr_linelen	= linelen;
2111 	if (ptr_msglen)	      *ptr_msglen	= msglen;
2112 	if (ptr_tlsprefixlen) *ptr_tlsprefixlen = tlsprefixlen;
2113 	if (ptr_prilen)	      *ptr_prilen	= prilen;
2114 	return true;
2115 }
2116 
2117 /*
2118  * if qentry == NULL: new message, if temporarily undeliverable it will be enqueued
2119  * if qentry != NULL: a temporarily undeliverable message will not be enqueued,
2120  *		    but after delivery be removed from the queue
2121  */
2122 void
2123 fprintlog(struct filed *f, struct buf_msg *passedbuffer, struct buf_queue *qentry)
2124 {
2125 	static char crnl[] = "\r\n";
2126 	struct buf_msg *buffer = passedbuffer;
2127 	struct iovec iov[4];
2128 	struct iovec *v = iov;
2129 	bool error = false;
2130 	int e = 0, len = 0;
2131 	size_t msglen, linelen, tlsprefixlen, prilen;
2132 	char *p, *line = NULL, *lineptr = NULL;
2133 #ifndef DISABLE_SIGN
2134 	bool newhash = false;
2135 #endif
2136 #define REPBUFSIZE 80
2137 	char greetings[200];
2138 #define ADDEV() do { v++; assert((size_t)(v - iov) < A_CNT(iov)); } while(/*CONSTCOND*/0)
2139 
2140 	DPRINTF(D_CALL, "fprintlog(%p, %p, %p)\n", f, buffer, qentry);
2141 
2142 	f->f_time = now;
2143 
2144 	/* increase refcount here and lower again at return.
2145 	 * this enables the buffer in the else branch to be freed
2146 	 * --> every branch needs one NEWREF() or buf_msg_new()! */
2147 	if (buffer) {
2148 		(void)NEWREF(buffer);
2149 	} else {
2150 		if (f->f_prevcount > 1) {
2151 			/* possible syslog-sign incompatibility:
2152 			 * assume destinations f1 and f2 share one SG and
2153 			 * get the same message sequence.
2154 			 *
2155 			 * now both f1 and f2 generate "repeated" messages
2156 			 * "repeated" messages are different due to different
2157 			 * timestamps
2158 			 * the SG will get hashes for the two "repeated" messages
2159 			 *
2160 			 * now both f1 and f2 are just fine, but a verification
2161 			 * will report that each 'lost' a message, i.e. the
2162 			 * other's "repeated" message
2163 			 *
2164 			 * conditions for 'safe configurations':
2165 			 * - use NoRepeat option,
2166 			 * - use SG 3, or
2167 			 * - have exactly one destination for every PRI
2168 			 */
2169 			buffer = buf_msg_new(REPBUFSIZE);
2170 			buffer->msglen = snprintf(buffer->msg, REPBUFSIZE,
2171 			    "last message repeated %d times", f->f_prevcount);
2172 			buffer->timestamp =
2173 				strdup(make_timestamp(NULL, !BSDOutputFormat));
2174 			buffer->pri = f->f_prevmsg->pri;
2175 			buffer->host = LocalFQDN;
2176 			buffer->prog = appname;
2177 			buffer->pid = include_pid;
2178 
2179 		} else {
2180 			buffer = NEWREF(f->f_prevmsg);
2181 		}
2182 	}
2183 
2184 	/* no syslog-sign messages to tty/console/... */
2185 	if ((buffer->flags & SIGN_MSG)
2186 	    && ((f->f_type == F_UNUSED)
2187 	    || (f->f_type == F_TTY)
2188 	    || (f->f_type == F_CONSOLE)
2189 	    || (f->f_type == F_USERS)
2190 	    || (f->f_type == F_WALL))) {
2191 		DELREF(buffer);
2192 		return;
2193 	}
2194 
2195 	/* buffering works only for few types */
2196 	if (qentry
2197 	    && (f->f_type != F_TLS)
2198 	    && (f->f_type != F_PIPE)
2199 	    && (f->f_type != F_FILE)) {
2200 		errno = 0;
2201 		logerror("Warning: unexpected message type %d in buffer",
2202 		    f->f_type);
2203 		DELREF(buffer);
2204 		return;
2205 	}
2206 
2207 	if (!format_buffer(buffer, &line,
2208 	    &linelen, &msglen, &tlsprefixlen, &prilen)) {
2209 		DPRINTF(D_CALL, "format_buffer() failed, skip message\n");
2210 		DELREF(buffer);
2211 		return;
2212 	}
2213 	/* assert maximum message length */
2214 	if (TypeInfo[f->f_type].max_msg_length != -1
2215 	    && (size_t)TypeInfo[f->f_type].max_msg_length
2216 	    < linelen - tlsprefixlen - prilen) {
2217 		linelen = TypeInfo[f->f_type].max_msg_length
2218 		    + tlsprefixlen + prilen;
2219 		DPRINTF(D_DATA, "truncating oversized message to %zu octets\n",
2220 		    linelen);
2221 	}
2222 
2223 #ifndef DISABLE_SIGN
2224 	/* keep state between appending the hash (before buffer is sent)
2225 	 * and possibly sending a SB (after buffer is sent): */
2226 	/* get hash */
2227 	if (!(buffer->flags & SIGN_MSG) && !qentry) {
2228 		char *hash = NULL;
2229 		struct signature_group_t *sg;
2230 
2231 		if ((sg = sign_get_sg(buffer->pri, f)) != NULL) {
2232 			if (sign_msg_hash(line + tlsprefixlen, &hash))
2233 				newhash = sign_append_hash(hash, sg);
2234 			else
2235 				DPRINTF(D_SIGN,
2236 					"Unable to hash line \"%s\"\n", line);
2237 		}
2238 	}
2239 #endif /* !DISABLE_SIGN */
2240 
2241 	/* set start and length of buffer and/or fill iovec */
2242 	switch (f->f_type) {
2243 	case F_UNUSED:
2244 		/* nothing */
2245 		break;
2246 	case F_TLS:
2247 		/* nothing, as TLS uses whole buffer to send */
2248 		lineptr = line;
2249 		len = linelen;
2250 		break;
2251 	case F_FORW:
2252 		lineptr = line + tlsprefixlen;
2253 		len = linelen - tlsprefixlen;
2254 		break;
2255 	case F_PIPE:
2256 	case F_FILE:  /* fallthrough */
2257 		if (f->f_flags & FFLAG_FULL) {
2258 			v->iov_base = line + tlsprefixlen;
2259 			v->iov_len = linelen - tlsprefixlen;
2260 		} else {
2261 			v->iov_base = line + tlsprefixlen + prilen;
2262 			v->iov_len = linelen - tlsprefixlen - prilen;
2263 		}
2264 		ADDEV();
2265 		v->iov_base = &crnl[1];
2266 		v->iov_len = 1;
2267 		ADDEV();
2268 		break;
2269 	case F_CONSOLE:
2270 	case F_TTY:
2271 		/* filter non-ASCII */
2272 		p = line;
2273 		while (*p) {
2274 			*p = FORCE2ASCII(*p);
2275 			p++;
2276 		}
2277 		v->iov_base = line + tlsprefixlen + prilen;
2278 		v->iov_len = linelen - tlsprefixlen - prilen;
2279 		ADDEV();
2280 		v->iov_base = crnl;
2281 		v->iov_len = 2;
2282 		ADDEV();
2283 		break;
2284 	case F_WALL:
2285 		v->iov_base = greetings;
2286 		v->iov_len = snprintf(greetings, sizeof(greetings),
2287 		    "\r\n\7Message from syslogd@%s at %s ...\r\n",
2288 		    (buffer->host ? buffer->host : buffer->recvhost),
2289 		    buffer->timestamp);
2290 		ADDEV();
2291 	case F_USERS: /* fallthrough */
2292 		/* filter non-ASCII */
2293 		p = line;
2294 		while (*p) {
2295 			*p = FORCE2ASCII(*p);
2296 			p++;
2297 		}
2298 		v->iov_base = line + tlsprefixlen + prilen;
2299 		v->iov_len = linelen - tlsprefixlen - prilen;
2300 		ADDEV();
2301 		v->iov_base = &crnl[1];
2302 		v->iov_len = 1;
2303 		ADDEV();
2304 		break;
2305 	}
2306 
2307 	/* send */
2308 	switch (f->f_type) {
2309 	case F_UNUSED:
2310 		DPRINTF(D_MISC, "Logging to %s\n", TypeInfo[f->f_type].name);
2311 		break;
2312 
2313 	case F_FORW:
2314 		DPRINTF(D_MISC, "Logging to %s %s\n",
2315 		    TypeInfo[f->f_type].name, f->f_un.f_forw.f_hname);
2316 		udp_send(f, lineptr, len);
2317 		break;
2318 
2319 #ifndef DISABLE_TLS
2320 	case F_TLS:
2321 		DPRINTF(D_MISC, "Logging to %s %s\n",
2322 		    TypeInfo[f->f_type].name,
2323 		    f->f_un.f_tls.tls_conn->hostname);
2324 		/* make sure every message gets queued once
2325 		 * it will be removed when sendmsg is sent and free()d */
2326 		if (!qentry)
2327 			qentry = message_queue_add(f, NEWREF(buffer));
2328 		(void)tls_send(f, lineptr, len, qentry);
2329 		break;
2330 #endif /* !DISABLE_TLS */
2331 
2332 	case F_PIPE:
2333 		DPRINTF(D_MISC, "Logging to %s %s\n",
2334 		    TypeInfo[f->f_type].name, f->f_un.f_pipe.f_pname);
2335 		if (f->f_un.f_pipe.f_pid == 0) {
2336 			/* (re-)open */
2337 			if ((f->f_file = p_open(f->f_un.f_pipe.f_pname,
2338 			    &f->f_un.f_pipe.f_pid)) < 0) {
2339 				f->f_type = F_UNUSED;
2340 				logerror("%s", f->f_un.f_pipe.f_pname);
2341 				message_queue_freeall(f);
2342 				break;
2343 			} else if (!qentry) /* prevent recursion */
2344 				SEND_QUEUE(f);
2345 		}
2346 		if (writev(f->f_file, iov, v - iov) < 0) {
2347 			e = errno;
2348 			if (f->f_un.f_pipe.f_pid > 0) {
2349 				(void) close(f->f_file);
2350 				deadq_enter(f->f_un.f_pipe.f_pid,
2351 				    f->f_un.f_pipe.f_pname);
2352 			}
2353 			f->f_un.f_pipe.f_pid = 0;
2354 			/*
2355 			 * If the error was EPIPE, then what is likely
2356 			 * has happened is we have a command that is
2357 			 * designed to take a single message line and
2358 			 * then exit, but we tried to feed it another
2359 			 * one before we reaped the child and thus
2360 			 * reset our state.
2361 			 *
2362 			 * Well, now we've reset our state, so try opening
2363 			 * the pipe and sending the message again if EPIPE
2364 			 * was the error.
2365 			 */
2366 			if (e == EPIPE) {
2367 				if ((f->f_file = p_open(f->f_un.f_pipe.f_pname,
2368 				     &f->f_un.f_pipe.f_pid)) < 0) {
2369 					f->f_type = F_UNUSED;
2370 					logerror("%s", f->f_un.f_pipe.f_pname);
2371 					message_queue_freeall(f);
2372 					break;
2373 				}
2374 				if (writev(f->f_file, iov, v - iov) < 0) {
2375 					e = errno;
2376 					if (f->f_un.f_pipe.f_pid > 0) {
2377 					    (void) close(f->f_file);
2378 					    deadq_enter(f->f_un.f_pipe.f_pid,
2379 						f->f_un.f_pipe.f_pname);
2380 					}
2381 					f->f_un.f_pipe.f_pid = 0;
2382 					error = true;	/* enqueue on return */
2383 				} else
2384 					e = 0;
2385 			}
2386 			if (e != 0 && !error) {
2387 				errno = e;
2388 				logerror("%s", f->f_un.f_pipe.f_pname);
2389 			}
2390 		}
2391 		if (e == 0 && qentry) { /* sent buffered msg */
2392 			message_queue_remove(f, qentry);
2393 		}
2394 		break;
2395 
2396 	case F_CONSOLE:
2397 		if (buffer->flags & IGN_CONS) {
2398 			DPRINTF(D_MISC, "Logging to %s (ignored)\n",
2399 				TypeInfo[f->f_type].name);
2400 			break;
2401 		}
2402 		/* FALLTHROUGH */
2403 
2404 	case F_TTY:
2405 	case F_FILE:
2406 		DPRINTF(D_MISC, "Logging to %s %s\n",
2407 			TypeInfo[f->f_type].name, f->f_un.f_fname);
2408 	again:
2409 		if ((f->f_type == F_FILE ? writev(f->f_file, iov, v - iov) :
2410 		    writev1(f->f_file, iov, v - iov)) < 0) {
2411 			e = errno;
2412 			if (f->f_type == F_FILE && e == ENOSPC) {
2413 				int lasterror = f->f_lasterror;
2414 				f->f_lasterror = e;
2415 				if (lasterror != e)
2416 					logerror("%s", f->f_un.f_fname);
2417 				error = true;	/* enqueue on return */
2418 			}
2419 			(void)close(f->f_file);
2420 			/*
2421 			 * Check for errors on TTY's due to loss of tty
2422 			 */
2423 			if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
2424 				f->f_file = open(f->f_un.f_fname,
2425 				    O_WRONLY|O_APPEND|O_NONBLOCK, 0);
2426 				if (f->f_file < 0) {
2427 					f->f_type = F_UNUSED;
2428 					logerror("%s", f->f_un.f_fname);
2429 					message_queue_freeall(f);
2430 				} else
2431 					goto again;
2432 			} else {
2433 				f->f_type = F_UNUSED;
2434 				errno = e;
2435 				f->f_lasterror = e;
2436 				logerror("%s", f->f_un.f_fname);
2437 				message_queue_freeall(f);
2438 			}
2439 		} else {
2440 			f->f_lasterror = 0;
2441 			if ((buffer->flags & SYNC_FILE)
2442 			 && (f->f_flags & FFLAG_SYNC))
2443 				(void)fsync(f->f_file);
2444 			/* Problem with files: We cannot check beforehand if
2445 			 * they would be writeable and call send_queue() first.
2446 			 * So we call send_queue() after a successful write,
2447 			 * which means the first message will be out of order.
2448 			 */
2449 			if (!qentry) /* prevent recursion */
2450 				SEND_QUEUE(f);
2451 			else if (qentry) /* sent buffered msg */
2452 				message_queue_remove(f, qentry);
2453 		}
2454 		break;
2455 
2456 	case F_USERS:
2457 	case F_WALL:
2458 		DPRINTF(D_MISC, "Logging to %s\n", TypeInfo[f->f_type].name);
2459 		wallmsg(f, iov, v - iov);
2460 		break;
2461 	}
2462 	f->f_prevcount = 0;
2463 
2464 	if (error && !qentry)
2465 		message_queue_add(f, NEWREF(buffer));
2466 #ifndef DISABLE_SIGN
2467 	if (newhash) {
2468 		struct signature_group_t *sg;
2469 		sg = sign_get_sg(buffer->pri, f);
2470 		(void)sign_send_signature_block(sg, false);
2471 	}
2472 #endif /* !DISABLE_SIGN */
2473 	/* this belongs to the ad-hoc buffer at the first if(buffer) */
2474 	DELREF(buffer);
2475 	/* TLS frees on its own */
2476 	if (f->f_type != F_TLS)
2477 		FREEPTR(line);
2478 }
2479 
2480 /* send one line by UDP */
2481 void
2482 udp_send(struct filed *f, char *line, size_t len)
2483 {
2484 	int lsent, fail, retry, j;
2485 	struct addrinfo *r;
2486 
2487 	DPRINTF((D_NET|D_CALL), "udp_send(f=%p, line=\"%s\", "
2488 	    "len=%zu) to dest.\n", f, line, len);
2489 
2490 	if (!finet)
2491 		return;
2492 
2493 	lsent = -1;
2494 	fail = 0;
2495 	assert(f->f_type == F_FORW);
2496 	for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) {
2497 		retry = 0;
2498 		for (j = 0; j < finet->fd; j++) {
2499 			if (finet[j+1].af != r->ai_family)
2500 				continue;
2501 sendagain:
2502 			lsent = sendto(finet[j+1].fd, line, len, 0,
2503 			    r->ai_addr, r->ai_addrlen);
2504 			if (lsent == -1) {
2505 				switch (errno) {
2506 				case ENOBUFS:
2507 					/* wait/retry/drop */
2508 					if (++retry < 5) {
2509 						usleep(1000);
2510 						goto sendagain;
2511 					}
2512 					break;
2513 				case EHOSTDOWN:
2514 				case EHOSTUNREACH:
2515 				case ENETDOWN:
2516 					/* drop */
2517 					break;
2518 				default:
2519 					/* busted */
2520 					fail++;
2521 					break;
2522 				}
2523 			} else if ((size_t)lsent == len)
2524 				break;
2525 		}
2526 		if ((size_t)lsent != len && fail) {
2527 			f->f_type = F_UNUSED;
2528 			logerror("sendto() failed");
2529 		}
2530 	}
2531 }
2532 
2533 /*
2534  *  WALLMSG -- Write a message to the world at large
2535  *
2536  *	Write the specified message to either the entire
2537  *	world, or a list of approved users.
2538  */
2539 void
2540 wallmsg(struct filed *f, struct iovec *iov, size_t iovcnt)
2541 {
2542 #ifdef __NetBSD_Version__
2543 	static int reenter;			/* avoid calling ourselves */
2544 	int i;
2545 	char *p;
2546 	struct utmpentry *ep;
2547 
2548 	if (reenter++)
2549 		return;
2550 
2551 	(void)getutentries(NULL, &ep);
2552 	/* NOSTRICT */
2553 	for (; ep; ep = ep->next) {
2554 		if (f->f_type == F_WALL) {
2555 			if ((p = ttymsg(iov, iovcnt, ep->line, TTYMSGTIME))
2556 			    != NULL) {
2557 				errno = 0;	/* already in msg */
2558 				logerror("%s", p);
2559 			}
2560 			continue;
2561 		}
2562 		/* should we send the message to this user? */
2563 		for (i = 0; i < MAXUNAMES; i++) {
2564 			if (!f->f_un.f_uname[i][0])
2565 				break;
2566 			if (strcmp(f->f_un.f_uname[i], ep->name) == 0) {
2567 				struct stat st;
2568 				char tty[MAXPATHLEN];
2569 				snprintf(tty, sizeof(tty), "%s/%s", _PATH_DEV,
2570 				    ep->line);
2571 				if (stat(tty, &st) != -1 &&
2572 				    (st.st_mode & S_IWGRP) == 0)
2573 					break;
2574 
2575 				if ((p = ttymsg(iov, iovcnt, ep->line,
2576 				    TTYMSGTIME)) != NULL) {
2577 					errno = 0;	/* already in msg */
2578 					logerror("%s", p);
2579 				}
2580 				break;
2581 			}
2582 		}
2583 	}
2584 	reenter = 0;
2585 #endif /* __NetBSD_Version__ */
2586 }
2587 
2588 void
2589 /*ARGSUSED*/
2590 reapchild(int fd, short event, void *ev)
2591 {
2592 	int status;
2593 	pid_t pid;
2594 	struct filed *f;
2595 
2596 	while ((pid = wait3(&status, WNOHANG, NULL)) > 0) {
2597 		if (!Initialized || ShuttingDown) {
2598 			/*
2599 			 * Be silent while we are initializing or
2600 			 * shutting down.
2601 			 */
2602 			continue;
2603 		}
2604 
2605 		if (deadq_remove(pid))
2606 			continue;
2607 
2608 		/* Now, look in the list of active processes. */
2609 		for (f = Files; f != NULL; f = f->f_next) {
2610 			if (f->f_type == F_PIPE &&
2611 			    f->f_un.f_pipe.f_pid == pid) {
2612 				(void) close(f->f_file);
2613 				f->f_un.f_pipe.f_pid = 0;
2614 				log_deadchild(pid, status,
2615 				    f->f_un.f_pipe.f_pname);
2616 				break;
2617 			}
2618 		}
2619 	}
2620 }
2621 
2622 /*
2623  * Return a printable representation of a host address (FQDN if available)
2624  */
2625 const char *
2626 cvthname(struct sockaddr_storage *f)
2627 {
2628 	int error;
2629 	int niflag = NI_DGRAM;
2630 	static char host[NI_MAXHOST], ip[NI_MAXHOST];
2631 
2632 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
2633 	    ip, sizeof ip, NULL, 0, NI_NUMERICHOST|niflag);
2634 
2635 	DPRINTF(D_CALL, "cvthname(%s)\n", ip);
2636 
2637 	if (error) {
2638 		DPRINTF(D_NET, "Malformed from address %s\n",
2639 		    gai_strerror(error));
2640 		return "???";
2641 	}
2642 
2643 	if (!UseNameService)
2644 		return ip;
2645 
2646 	error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
2647 	    host, sizeof host, NULL, 0, niflag);
2648 	if (error) {
2649 		DPRINTF(D_NET, "Host name for your address (%s) unknown\n", ip);
2650 		return ip;
2651 	}
2652 
2653 	return host;
2654 }
2655 
2656 void
2657 trim_anydomain(char *host)
2658 {
2659 	bool onlydigits = true;
2660 	int i;
2661 
2662 	if (!BSDOutputFormat)
2663 		return;
2664 
2665 	/* if non-digits found, then assume hostname and cut at first dot (this
2666 	 * case also covers IPv6 addresses which should not contain dots),
2667 	 * if only digits then assume IPv4 address and do not cut at all */
2668 	for (i = 0; host[i]; i++) {
2669 		if (host[i] == '.' && !onlydigits)
2670 			host[i] = '\0';
2671 		else if (!isdigit((unsigned char)host[i]) && host[i] != '.')
2672 			onlydigits = false;
2673 	}
2674 }
2675 
2676 static void
2677 /*ARGSUSED*/
2678 domark(int fd, short event, void *ev)
2679 {
2680 	struct event *ev_pass = (struct event *)ev;
2681 	struct filed *f;
2682 	dq_t q, nextq;
2683 	sigset_t newmask, omask;
2684 
2685 	schedule_event(&ev_pass,
2686 		&((struct timeval){TIMERINTVL, 0}),
2687 		domark, ev_pass);
2688 	DPRINTF((D_CALL|D_EVENT), "domark()\n");
2689 
2690 	BLOCK_SIGNALS(omask, newmask);
2691 	now = time(NULL);
2692 	MarkSeq += TIMERINTVL;
2693 	if (MarkSeq >= MarkInterval) {
2694 		logmsg_async(LOG_INFO, NULL, "-- MARK --", ADDDATE|MARK);
2695 		MarkSeq = 0;
2696 	}
2697 
2698 	for (f = Files; f; f = f->f_next) {
2699 		if (f->f_prevcount && now >= REPEATTIME(f)) {
2700 			DPRINTF(D_DATA, "Flush %s: repeated %d times, %d sec.\n",
2701 			    TypeInfo[f->f_type].name, f->f_prevcount,
2702 			    repeatinterval[f->f_repeatcount]);
2703 			fprintlog(f, NULL, NULL);
2704 			BACKOFF(f);
2705 		}
2706 	}
2707 	message_allqueues_check();
2708 	RESTORE_SIGNALS(omask);
2709 
2710 	/* Walk the dead queue, and see if we should signal somebody. */
2711 	for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = nextq) {
2712 		nextq = TAILQ_NEXT(q, dq_entries);
2713 		switch (q->dq_timeout) {
2714 		case 0:
2715 			/* Already signalled once, try harder now. */
2716 			if (kill(q->dq_pid, SIGKILL) != 0)
2717 				(void) deadq_remove(q->dq_pid);
2718 			break;
2719 
2720 		case 1:
2721 			/*
2722 			 * Timed out on the dead queue, send terminate
2723 			 * signal.  Note that we leave the removal from
2724 			 * the dead queue to reapchild(), which will
2725 			 * also log the event (unless the process
2726 			 * didn't even really exist, in case we simply
2727 			 * drop it from the dead queue).
2728 			 */
2729 			if (kill(q->dq_pid, SIGTERM) != 0) {
2730 				(void) deadq_remove(q->dq_pid);
2731 				break;
2732 			}
2733 			/* FALLTHROUGH */
2734 
2735 		default:
2736 			q->dq_timeout--;
2737 		}
2738 	}
2739 #ifndef DISABLE_SIGN
2740 	if (GlobalSign.rsid) {	/* check if initialized */
2741 		struct signature_group_t *sg;
2742 		STAILQ_FOREACH(sg, &GlobalSign.SigGroups, entries) {
2743 			sign_send_certificate_block(sg);
2744 		}
2745 	}
2746 #endif /* !DISABLE_SIGN */
2747 }
2748 
2749 /*
2750  * Print syslogd errors some place.
2751  */
2752 void
2753 logerror(const char *fmt, ...)
2754 {
2755 	static int logerror_running;
2756 	va_list ap;
2757 	char tmpbuf[BUFSIZ];
2758 	char buf[BUFSIZ];
2759 	char *outbuf;
2760 
2761 	/* If there's an error while trying to log an error, give up. */
2762 	if (logerror_running)
2763 		return;
2764 	logerror_running = 1;
2765 
2766 	va_start(ap, fmt);
2767 	(void)vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
2768 	va_end(ap);
2769 
2770 	if (errno) {
2771 		(void)snprintf(buf, sizeof(buf), "%s: %s",
2772 		    tmpbuf, strerror(errno));
2773 		outbuf = buf;
2774 	} else {
2775 		(void)snprintf(buf, sizeof(buf), "%s", tmpbuf);
2776 		outbuf = tmpbuf;
2777 	}
2778 
2779 	if (daemonized)
2780 		logmsg_async(LOG_SYSLOG|LOG_ERR, NULL, outbuf, ADDDATE);
2781 	if (!daemonized && Debug)
2782 		DPRINTF(D_MISC, "%s\n", outbuf);
2783 	if (!daemonized && !Debug)
2784 		printf("%s\n", outbuf);
2785 
2786 	logerror_running = 0;
2787 }
2788 
2789 /*
2790  * Print syslogd info some place.
2791  */
2792 void
2793 loginfo(const char *fmt, ...)
2794 {
2795 	va_list ap;
2796 	char buf[BUFSIZ];
2797 
2798 	va_start(ap, fmt);
2799 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
2800 	va_end(ap);
2801 
2802 	DPRINTF(D_MISC, "%s\n", buf);
2803 	logmsg_async(LOG_SYSLOG|LOG_INFO, NULL, buf, ADDDATE);
2804 }
2805 
2806 #ifndef DISABLE_TLS
2807 static inline void
2808 free_incoming_tls_sockets(void)
2809 {
2810 	struct TLS_Incoming_Conn *tls_in;
2811 	int i;
2812 
2813 	/*
2814 	 * close all listening and connected TLS sockets
2815 	 */
2816 	if (TLS_Listen_Set)
2817 		for (i = 0; i < TLS_Listen_Set->fd; i++) {
2818 			if (close(TLS_Listen_Set[i+1].fd) == -1)
2819 				logerror("close() failed");
2820 			DEL_EVENT(TLS_Listen_Set[i+1].ev);
2821 			FREEPTR(TLS_Listen_Set[i+1].ev);
2822 		}
2823 	FREEPTR(TLS_Listen_Set);
2824 	/* close/free incoming TLS connections */
2825 	while (!SLIST_EMPTY(&TLS_Incoming_Head)) {
2826 		tls_in = SLIST_FIRST(&TLS_Incoming_Head);
2827 		SLIST_REMOVE_HEAD(&TLS_Incoming_Head, entries);
2828 		FREEPTR(tls_in->inbuf);
2829 		free_tls_conn(tls_in->tls_conn);
2830 		free(tls_in);
2831 	}
2832 }
2833 #endif /* !DISABLE_TLS */
2834 
2835 void
2836 /*ARGSUSED*/
2837 die(int fd, short event, void *ev)
2838 {
2839 	struct filed *f, *next;
2840 	char **p;
2841 	sigset_t newmask, omask;
2842 	int i;
2843 	size_t j;
2844 
2845 	ShuttingDown = 1;	/* Don't log SIGCHLDs. */
2846 	/* prevent recursive signals */
2847 	BLOCK_SIGNALS(omask, newmask);
2848 
2849 	errno = 0;
2850 	if (ev != NULL)
2851 		logerror("Exiting on signal %d", fd);
2852 	else
2853 		logerror("Fatal error, exiting");
2854 
2855 	/*
2856 	 *  flush any pending output
2857 	 */
2858 	for (f = Files; f != NULL; f = f->f_next) {
2859 		/* flush any pending output */
2860 		if (f->f_prevcount)
2861 			fprintlog(f, NULL, NULL);
2862 		SEND_QUEUE(f);
2863 	}
2864 
2865 #ifndef DISABLE_TLS
2866 	free_incoming_tls_sockets();
2867 #endif /* !DISABLE_TLS */
2868 #ifndef DISABLE_SIGN
2869 	sign_global_free();
2870 #endif /* !DISABLE_SIGN */
2871 
2872 	/*
2873 	 *  Close all open log files.
2874 	 */
2875 	for (f = Files; f != NULL; f = next) {
2876 		message_queue_freeall(f);
2877 
2878 		switch (f->f_type) {
2879 		case F_FILE:
2880 		case F_TTY:
2881 		case F_CONSOLE:
2882 			(void)close(f->f_file);
2883 			break;
2884 		case F_PIPE:
2885 			if (f->f_un.f_pipe.f_pid > 0) {
2886 				(void)close(f->f_file);
2887 			}
2888 			f->f_un.f_pipe.f_pid = 0;
2889 			break;
2890 		case F_FORW:
2891 			if (f->f_un.f_forw.f_addr)
2892 				freeaddrinfo(f->f_un.f_forw.f_addr);
2893 			break;
2894 #ifndef DISABLE_TLS
2895 		case F_TLS:
2896 			free_tls_conn(f->f_un.f_tls.tls_conn);
2897 			break;
2898 #endif /* !DISABLE_TLS */
2899 		}
2900 		next = f->f_next;
2901 		DELREF(f->f_prevmsg);
2902 		FREEPTR(f->f_program);
2903 		FREEPTR(f->f_host);
2904 		DEL_EVENT(f->f_sq_event);
2905 		free((char *)f);
2906 	}
2907 
2908 	/*
2909 	 *  Close all open UDP sockets
2910 	 */
2911 	if (finet) {
2912 		for (i = 0; i < finet->fd; i++) {
2913 			if (close(finet[i+1].fd) < 0) {
2914 				logerror("close() failed");
2915 				die(0, 0, NULL);
2916 			}
2917 			DEL_EVENT(finet[i+1].ev);
2918 			FREEPTR(finet[i+1].ev);
2919 		}
2920 		FREEPTR(finet);
2921 	}
2922 
2923 	/* free config options */
2924 	for (j = 0; j < A_CNT(TypeInfo); j++) {
2925 		FREEPTR(TypeInfo[j].queue_length_string);
2926 		FREEPTR(TypeInfo[j].queue_size_string);
2927 	}
2928 
2929 #ifndef DISABLE_TLS
2930 	FREEPTR(tls_opt.CAdir);
2931 	FREEPTR(tls_opt.CAfile);
2932 	FREEPTR(tls_opt.keyfile);
2933 	FREEPTR(tls_opt.certfile);
2934 	FREEPTR(tls_opt.x509verify);
2935 	FREEPTR(tls_opt.bindhost);
2936 	FREEPTR(tls_opt.bindport);
2937 	FREEPTR(tls_opt.server);
2938 	FREEPTR(tls_opt.gen_cert);
2939 	free_cred_SLIST(&tls_opt.cert_head);
2940 	free_cred_SLIST(&tls_opt.fprint_head);
2941 	FREE_SSL_CTX(tls_opt.global_TLS_CTX);
2942 #endif /* !DISABLE_TLS */
2943 
2944 	FREEPTR(funix);
2945 	for (p = LogPaths; p && *p; p++)
2946 		unlink(*p);
2947 	exit(0);
2948 }
2949 
2950 #ifndef DISABLE_SIGN
2951 /*
2952  * get one "sign_delim_sg2" item, convert and store in ordered queue
2953  */
2954 void
2955 store_sign_delim_sg2(char *tmp_buf)
2956 {
2957 	struct string_queue *sqentry, *sqe1, *sqe2;
2958 
2959 	if(!(sqentry = malloc(sizeof(*sqentry)))) {
2960 		logerror("Unable to allocate memory");
2961 		return;
2962 	}
2963 	/*LINTED constcond/null effect */
2964 	assert(sizeof(int64_t) == sizeof(uint_fast64_t));
2965 	if (dehumanize_number(tmp_buf, (int64_t*) &(sqentry->key)) == -1
2966 	    || sqentry->key > (LOG_NFACILITIES<<3)) {
2967 		DPRINTF(D_PARSE, "invalid sign_delim_sg2: %s\n", tmp_buf);
2968 		free(sqentry);
2969 		FREEPTR(tmp_buf);
2970 		return;
2971 	}
2972 	sqentry->data = tmp_buf;
2973 
2974 	if (STAILQ_EMPTY(&GlobalSign.sig2_delims)) {
2975 		STAILQ_INSERT_HEAD(&GlobalSign.sig2_delims,
2976 		    sqentry, entries);
2977 		return;
2978 	}
2979 
2980 	/* keep delimiters sorted */
2981 	sqe1 = sqe2 = STAILQ_FIRST(&GlobalSign.sig2_delims);
2982 	if (sqe1->key > sqentry->key) {
2983 		STAILQ_INSERT_HEAD(&GlobalSign.sig2_delims,
2984 		    sqentry, entries);
2985 		return;
2986 	}
2987 
2988 	while ((sqe1 = sqe2)
2989 	   && (sqe2 = STAILQ_NEXT(sqe1, entries))) {
2990 		if (sqe2->key > sqentry->key) {
2991 			break;
2992 		} else if (sqe2->key == sqentry->key) {
2993 			DPRINTF(D_PARSE, "duplicate sign_delim_sg2: %s\n",
2994 			    tmp_buf);
2995 			FREEPTR(sqentry);
2996 			FREEPTR(tmp_buf);
2997 			return;
2998 		}
2999 	}
3000 	STAILQ_INSERT_AFTER(&GlobalSign.sig2_delims, sqe1, sqentry, entries);
3001 }
3002 #endif /* !DISABLE_SIGN */
3003 
3004 /*
3005  * read syslog.conf
3006  */
3007 void
3008 read_config_file(FILE *cf, struct filed **f_ptr)
3009 {
3010 	size_t linenum = 0;
3011 	size_t i;
3012 	struct filed *f, **nextp;
3013 	char cline[LINE_MAX];
3014 	char prog[NAME_MAX + 1];
3015 	char host[MAXHOSTNAMELEN];
3016 	const char *p;
3017 	char *q;
3018 	bool found_keyword;
3019 #ifndef DISABLE_TLS
3020 	struct peer_cred *cred = NULL;
3021 	struct peer_cred_head *credhead = NULL;
3022 #endif /* !DISABLE_TLS */
3023 #ifndef DISABLE_SIGN
3024 	char *sign_sg_str = NULL;
3025 #endif /* !DISABLE_SIGN */
3026 #if (!defined(DISABLE_TLS) || !defined(DISABLE_SIGN))
3027 	char *tmp_buf = NULL;
3028 #endif /* (!defined(DISABLE_TLS) || !defined(DISABLE_SIGN)) */
3029 	/* central list of recognized configuration keywords
3030 	 * and an address for their values as strings */
3031 	const struct config_keywords {
3032 		const char *keyword;
3033 		char **variable;
3034 	} config_keywords[] = {
3035 #ifndef DISABLE_TLS
3036 		/* TLS settings */
3037 		{"tls_ca",		  &tls_opt.CAfile},
3038 		{"tls_cadir",		  &tls_opt.CAdir},
3039 		{"tls_cert",		  &tls_opt.certfile},
3040 		{"tls_key",		  &tls_opt.keyfile},
3041 		{"tls_verify",		  &tls_opt.x509verify},
3042 		{"tls_bindport",	  &tls_opt.bindport},
3043 		{"tls_bindhost",	  &tls_opt.bindhost},
3044 		{"tls_server",		  &tls_opt.server},
3045 		{"tls_gen_cert",	  &tls_opt.gen_cert},
3046 		/* special cases in parsing */
3047 		{"tls_allow_fingerprints",&tmp_buf},
3048 		{"tls_allow_clientcerts", &tmp_buf},
3049 		/* buffer settings */
3050 		{"tls_queue_length",	  &TypeInfo[F_TLS].queue_length_string},
3051 		{"tls_queue_size",	  &TypeInfo[F_TLS].queue_size_string},
3052 #endif /* !DISABLE_TLS */
3053 		{"file_queue_length",	  &TypeInfo[F_FILE].queue_length_string},
3054 		{"pipe_queue_length",	  &TypeInfo[F_PIPE].queue_length_string},
3055 		{"file_queue_size",	  &TypeInfo[F_FILE].queue_size_string},
3056 		{"pipe_queue_size",	  &TypeInfo[F_PIPE].queue_size_string},
3057 #ifndef DISABLE_SIGN
3058 		/* syslog-sign setting */
3059 		{"sign_sg",		  &sign_sg_str},
3060 		/* also special case in parsing */
3061 		{"sign_delim_sg2",	  &tmp_buf},
3062 #endif /* !DISABLE_SIGN */
3063 	};
3064 
3065 	DPRINTF(D_CALL, "read_config_file()\n");
3066 
3067 	/* free all previous config options */
3068 	for (i = 0; i < A_CNT(TypeInfo); i++) {
3069 		if (TypeInfo[i].queue_length_string
3070 		    && TypeInfo[i].queue_length_string
3071 		    != TypeInfo[i].default_length_string) {
3072 			FREEPTR(TypeInfo[i].queue_length_string);
3073 			TypeInfo[i].queue_length_string =
3074 				strdup(TypeInfo[i].default_length_string);
3075 		 }
3076 		if (TypeInfo[i].queue_size_string
3077 		    && TypeInfo[i].queue_size_string
3078 		    != TypeInfo[i].default_size_string) {
3079 			FREEPTR(TypeInfo[i].queue_size_string);
3080 			TypeInfo[i].queue_size_string =
3081 				strdup(TypeInfo[i].default_size_string);
3082 		 }
3083 	}
3084 	for (i = 0; i < A_CNT(config_keywords); i++)
3085 		FREEPTR(*config_keywords[i].variable);
3086 	/*
3087 	 * global settings
3088 	 */
3089 	while (fgets(cline, sizeof(cline), cf) != NULL) {
3090 		linenum++;
3091 		for (p = cline; isspace((unsigned char)*p); ++p)
3092 			continue;
3093 		if ((*p == '\0') || (*p == '#'))
3094 			continue;
3095 
3096 		for (i = 0; i < A_CNT(config_keywords); i++) {
3097 			if (copy_config_value(config_keywords[i].keyword,
3098 			    config_keywords[i].variable, &p, ConfFile,
3099 			    linenum)) {
3100 				DPRINTF((D_PARSE|D_MEM),
3101 				    "found option %s, saved @%p\n",
3102 				    config_keywords[i].keyword,
3103 				    *config_keywords[i].variable);
3104 #ifndef DISABLE_SIGN
3105 				if (!strcmp("sign_delim_sg2",
3106 				    config_keywords[i].keyword))
3107 					do {
3108 						store_sign_delim_sg2(tmp_buf);
3109 					} while (copy_config_value_word(
3110 					    &tmp_buf, &p));
3111 
3112 #endif /* !DISABLE_SIGN */
3113 
3114 #ifndef DISABLE_TLS
3115 				/* special cases with multiple parameters */
3116 				if (!strcmp("tls_allow_fingerprints",
3117 				    config_keywords[i].keyword))
3118 					credhead = &tls_opt.fprint_head;
3119 				else if (!strcmp("tls_allow_clientcerts",
3120 				    config_keywords[i].keyword))
3121 					credhead = &tls_opt.cert_head;
3122 
3123 				if (credhead) do {
3124 					if(!(cred = malloc(sizeof(*cred)))) {
3125 						logerror("Unable to "
3126 							"allocate memory");
3127 						break;
3128 					}
3129 					cred->data = tmp_buf;
3130 					tmp_buf = NULL;
3131 					SLIST_INSERT_HEAD(credhead,
3132 						cred, entries);
3133 				} while /* additional values? */
3134 					(copy_config_value_word(&tmp_buf, &p));
3135 				credhead = NULL;
3136 				break;
3137 #endif /* !DISABLE_TLS */
3138 			}
3139 		}
3140 	}
3141 	/* convert strings to integer values */
3142 	for (i = 0; i < A_CNT(TypeInfo); i++) {
3143 		if (!TypeInfo[i].queue_length_string
3144 		    || dehumanize_number(TypeInfo[i].queue_length_string,
3145 		    &TypeInfo[i].queue_length) == -1)
3146 			TypeInfo[i].queue_length = strtol(
3147 			    TypeInfo[i].default_length_string, NULL, 10);
3148 		if (!TypeInfo[i].queue_size_string
3149 		    || dehumanize_number(TypeInfo[i].queue_size_string,
3150 		    &TypeInfo[i].queue_size) == -1)
3151 			TypeInfo[i].queue_size = strtol(
3152 			    TypeInfo[i].default_size_string, NULL, 10);
3153 	}
3154 
3155 #ifndef DISABLE_SIGN
3156 	if (sign_sg_str) {
3157 		if (sign_sg_str[1] == '\0'
3158 		    && (sign_sg_str[0] == '0' || sign_sg_str[0] == '1'
3159 		    || sign_sg_str[0] == '2' || sign_sg_str[0] == '3'))
3160 			GlobalSign.sg = sign_sg_str[0] - '0';
3161 		else {
3162 			GlobalSign.sg = SIGN_SG;
3163 			DPRINTF(D_MISC, "Invalid sign_sg value `%s', "
3164 			    "use default value `%d'\n",
3165 			    sign_sg_str, GlobalSign.sg);
3166 		}
3167 	} else	/* disable syslog-sign */
3168 		GlobalSign.sg = -1;
3169 #endif /* !DISABLE_SIGN */
3170 
3171 	rewind(cf);
3172 	linenum = 0;
3173 	/*
3174 	 *  Foreach line in the conf table, open that file.
3175 	 */
3176 	f = NULL;
3177 	nextp = &f;
3178 
3179 	strcpy(prog, "*");
3180 	strcpy(host, "*");
3181 	while (fgets(cline, sizeof(cline), cf) != NULL) {
3182 		linenum++;
3183 		found_keyword = false;
3184 		/*
3185 		 * check for end-of-section, comments, strip off trailing
3186 		 * spaces and newline character.  #!prog is treated specially:
3187 		 * following lines apply only to that program.
3188 		 */
3189 		for (p = cline; isspace((unsigned char)*p); ++p)
3190 			continue;
3191 		if (*p == '\0')
3192 			continue;
3193 		if (*p == '#') {
3194 			p++;
3195 			if (*p != '!' && *p != '+' && *p != '-')
3196 				continue;
3197 		}
3198 
3199 		for (i = 0; i < A_CNT(config_keywords); i++) {
3200 			if (!strncasecmp(p, config_keywords[i].keyword,
3201 				strlen(config_keywords[i].keyword))) {
3202 				DPRINTF(D_PARSE,
3203 				    "skip cline %zu with keyword %s\n",
3204 				    linenum, config_keywords[i].keyword);
3205 				found_keyword = true;
3206 			}
3207 		}
3208 		if (found_keyword)
3209 			continue;
3210 
3211 		if (*p == '+' || *p == '-') {
3212 			host[0] = *p++;
3213 			while (isspace((unsigned char)*p))
3214 				p++;
3215 			if (*p == '\0' || *p == '*') {
3216 				strcpy(host, "*");
3217 				continue;
3218 			}
3219 			/* the +hostname expression will continue
3220 			 * to use the LocalHostName, not the FQDN */
3221 			for (i = 1; i < MAXHOSTNAMELEN - 1; i++) {
3222 				if (*p == '@') {
3223 					(void)strncpy(&host[i], LocalHostName,
3224 					    sizeof(host) - 1 - i);
3225 					host[sizeof(host) - 1] = '\0';
3226 					i = strlen(host) - 1;
3227 					p++;
3228 					continue;
3229 				}
3230 				if (!isalnum((unsigned char)*p) &&
3231 				    *p != '.' && *p != '-' && *p != ',')
3232 					break;
3233 				host[i] = *p++;
3234 			}
3235 			host[i] = '\0';
3236 			continue;
3237 		}
3238 		if (*p == '!') {
3239 			p++;
3240 			while (isspace((unsigned char)*p))
3241 				p++;
3242 			if (*p == '\0' || *p == '*') {
3243 				strcpy(prog, "*");
3244 				continue;
3245 			}
3246 			for (i = 0; i < NAME_MAX; i++) {
3247 				if (!isprint((unsigned char)p[i]))
3248 					break;
3249 				prog[i] = p[i];
3250 			}
3251 			prog[i] = '\0';
3252 			continue;
3253 		}
3254 		for (q = strchr(cline, '\0'); isspace((unsigned char)*--q);)
3255 			continue;
3256 		*++q = '\0';
3257 		if ((f = calloc(1, sizeof(*f))) == NULL) {
3258 			logerror("alloc failed");
3259 			die(0, 0, NULL);
3260 		}
3261 		if (!*f_ptr) *f_ptr = f; /* return first node */
3262 		*nextp = f;
3263 		nextp = &f->f_next;
3264 		cfline(linenum, cline, f, prog, host);
3265 	}
3266 }
3267 
3268 /*
3269  *  INIT -- Initialize syslogd from configuration table
3270  */
3271 void
3272 /*ARGSUSED*/
3273 init(int fd, short event, void *ev)
3274 {
3275 	FILE *cf;
3276 	int i;
3277 	struct filed *f, *newf, **nextp, *f2;
3278 	char *p;
3279 	sigset_t newmask, omask;
3280 #ifndef DISABLE_TLS
3281 	char *tls_status_msg = NULL;
3282 	struct peer_cred *cred = NULL;
3283 #endif /* !DISABLE_TLS */
3284 
3285 	/* prevent recursive signals */
3286 	BLOCK_SIGNALS(omask, newmask);
3287 
3288 	DPRINTF((D_EVENT|D_CALL), "init\n");
3289 
3290 	/*
3291 	 * be careful about dependencies and order of actions:
3292 	 * 1. flush buffer queues
3293 	 * 2. flush -sign SBs
3294 	 * 3. flush/delete buffer queue again, in case an SB got there
3295 	 * 4. close files/connections
3296 	 */
3297 
3298 	/*
3299 	 *  flush any pending output
3300 	 */
3301 	for (f = Files; f != NULL; f = f->f_next) {
3302 		/* flush any pending output */
3303 		if (f->f_prevcount)
3304 			fprintlog(f, NULL, NULL);
3305 		SEND_QUEUE(f);
3306 	}
3307 	/* some actions only on SIGHUP and not on first start */
3308 	if (Initialized) {
3309 #ifndef DISABLE_SIGN
3310 		sign_global_free();
3311 #endif /* !DISABLE_SIGN */
3312 #ifndef DISABLE_TLS
3313 		free_incoming_tls_sockets();
3314 #endif /* !DISABLE_TLS */
3315 		Initialized = 0;
3316 	}
3317 	/*
3318 	 *  Close all open log files.
3319 	 */
3320 	for (f = Files; f != NULL; f = f->f_next) {
3321 		switch (f->f_type) {
3322 		case F_FILE:
3323 		case F_TTY:
3324 		case F_CONSOLE:
3325 			(void)close(f->f_file);
3326 			break;
3327 		case F_PIPE:
3328 			if (f->f_un.f_pipe.f_pid > 0) {
3329 				(void)close(f->f_file);
3330 				deadq_enter(f->f_un.f_pipe.f_pid,
3331 				    f->f_un.f_pipe.f_pname);
3332 			}
3333 			f->f_un.f_pipe.f_pid = 0;
3334 			break;
3335 		case F_FORW:
3336 			if (f->f_un.f_forw.f_addr)
3337 				freeaddrinfo(f->f_un.f_forw.f_addr);
3338 			break;
3339 #ifndef DISABLE_TLS
3340 		case F_TLS:
3341 			free_tls_sslptr(f->f_un.f_tls.tls_conn);
3342 			break;
3343 #endif /* !DISABLE_TLS */
3344 		}
3345 	}
3346 
3347 	/*
3348 	 *  Close all open UDP sockets
3349 	 */
3350 	if (finet) {
3351 		for (i = 0; i < finet->fd; i++) {
3352 			if (close(finet[i+1].fd) < 0) {
3353 				logerror("close() failed");
3354 				die(0, 0, NULL);
3355 			}
3356 			DEL_EVENT(finet[i+1].ev);
3357 			FREEPTR(finet[i+1].ev);
3358 		}
3359 		FREEPTR(finet);
3360 	}
3361 
3362 	/* get FQDN and hostname/domain */
3363 	FREEPTR(oldLocalFQDN);
3364 	oldLocalFQDN = LocalFQDN;
3365 	LocalFQDN = getLocalFQDN();
3366 	if ((p = strchr(LocalFQDN, '.')) != NULL)
3367 		(void)strlcpy(LocalHostName, LocalFQDN, 1+p-LocalFQDN);
3368 	else
3369 		(void)strlcpy(LocalHostName, LocalFQDN, sizeof(LocalHostName));
3370 
3371 	/*
3372 	 *  Reset counter of forwarding actions
3373 	 */
3374 
3375 	NumForwards=0;
3376 
3377 	/* new destination list to replace Files */
3378 	newf = NULL;
3379 	nextp = &newf;
3380 
3381 	/* open the configuration file */
3382 	if ((cf = fopen(ConfFile, "r")) == NULL) {
3383 		DPRINTF(D_FILE, "Cannot open `%s'\n", ConfFile);
3384 		*nextp = (struct filed *)calloc(1, sizeof(*f));
3385 		cfline(0, "*.ERR\t/dev/console", *nextp, "*", "*");
3386 		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
3387 		cfline(0, "*.PANIC\t*", (*nextp)->f_next, "*", "*");
3388 		Initialized = 1;
3389 		RESTORE_SIGNALS(omask);
3390 		return;
3391 	}
3392 
3393 #ifndef DISABLE_TLS
3394 	/* init with new TLS_CTX
3395 	 * as far as I see one cannot change the cert/key of an existing CTX
3396 	 */
3397 	FREE_SSL_CTX(tls_opt.global_TLS_CTX);
3398 
3399 	free_cred_SLIST(&tls_opt.cert_head);
3400 	free_cred_SLIST(&tls_opt.fprint_head);
3401 #endif /* !DISABLE_TLS */
3402 
3403 	/* read and close configuration file */
3404 	read_config_file(cf, &newf);
3405 	newf = *nextp;
3406 	(void)fclose(cf);
3407 	DPRINTF(D_MISC, "read_config_file() returned newf=%p\n", newf);
3408 
3409 #define MOVE_QUEUE(dst, src) do {				\
3410 	struct buf_queue *buf;					\
3411 	STAILQ_CONCAT(&dst->f_qhead, &src->f_qhead);		\
3412 	STAILQ_FOREACH(buf, &dst->f_qhead, entries) {		\
3413 	      dst->f_qelements++;				\
3414 	      dst->f_qsize += buf_queue_obj_size(buf);		\
3415 	}							\
3416 	src->f_qsize = 0;					\
3417 	src->f_qelements = 0;					\
3418 } while (/*CONSTCOND*/0)
3419 
3420 	/*
3421 	 *  Free old log files.
3422 	 */
3423 	for (f = Files; f != NULL;) {
3424 		struct filed *ftmp;
3425 
3426 		/* check if a new logfile is equal, if so pass the queue */
3427 		for (f2 = newf; f2 != NULL; f2 = f2->f_next) {
3428 			if (f->f_type == f2->f_type
3429 			    && ((f->f_type == F_PIPE
3430 			    && !strcmp(f->f_un.f_pipe.f_pname,
3431 			    f2->f_un.f_pipe.f_pname))
3432 #ifndef DISABLE_TLS
3433 			    || (f->f_type == F_TLS
3434 			    && !strcmp(f->f_un.f_tls.tls_conn->hostname,
3435 			    f2->f_un.f_tls.tls_conn->hostname)
3436 			    && !strcmp(f->f_un.f_tls.tls_conn->port,
3437 			    f2->f_un.f_tls.tls_conn->port))
3438 #endif /* !DISABLE_TLS */
3439 			    || (f->f_type == F_FORW
3440 			    && !strcmp(f->f_un.f_forw.f_hname,
3441 			    f2->f_un.f_forw.f_hname)))) {
3442 				DPRINTF(D_BUFFER, "move queue from f@%p "
3443 				    "to f2@%p\n", f, f2);
3444 				MOVE_QUEUE(f2, f);
3445 			 }
3446 		}
3447 		message_queue_freeall(f);
3448 		DELREF(f->f_prevmsg);
3449 #ifndef DISABLE_TLS
3450 		if (f->f_type == F_TLS)
3451 			free_tls_conn(f->f_un.f_tls.tls_conn);
3452 #endif /* !DISABLE_TLS */
3453 		FREEPTR(f->f_program);
3454 		FREEPTR(f->f_host);
3455 		DEL_EVENT(f->f_sq_event);
3456 
3457 		ftmp = f->f_next;
3458 		free((char *)f);
3459 		f = ftmp;
3460 	}
3461 	Files = newf;
3462 	Initialized = 1;
3463 
3464 	if (Debug) {
3465 		for (f = Files; f; f = f->f_next) {
3466 			for (i = 0; i <= LOG_NFACILITIES; i++)
3467 				if (f->f_pmask[i] == INTERNAL_NOPRI)
3468 					printf("X ");
3469 				else
3470 					printf("%d ", f->f_pmask[i]);
3471 			printf("%s: ", TypeInfo[f->f_type].name);
3472 			switch (f->f_type) {
3473 			case F_FILE:
3474 			case F_TTY:
3475 			case F_CONSOLE:
3476 				printf("%s", f->f_un.f_fname);
3477 				break;
3478 
3479 			case F_FORW:
3480 				printf("%s", f->f_un.f_forw.f_hname);
3481 				break;
3482 #ifndef DISABLE_TLS
3483 			case F_TLS:
3484 				printf("[%s]", f->f_un.f_tls.tls_conn->hostname);
3485 				break;
3486 #endif /* !DISABLE_TLS */
3487 			case F_PIPE:
3488 				printf("%s", f->f_un.f_pipe.f_pname);
3489 				break;
3490 
3491 			case F_USERS:
3492 				for (i = 0;
3493 				    i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
3494 					printf("%s, ", f->f_un.f_uname[i]);
3495 				break;
3496 			}
3497 			if (f->f_program != NULL)
3498 				printf(" (%s)", f->f_program);
3499 			printf("\n");
3500 		}
3501 	}
3502 
3503 	finet = socksetup(PF_UNSPEC, bindhostname);
3504 	if (finet) {
3505 		if (SecureMode) {
3506 			for (i = 0; i < finet->fd; i++) {
3507 				if (shutdown(finet[i+1].fd, SHUT_RD) < 0) {
3508 					logerror("shutdown() failed");
3509 					die(0, 0, NULL);
3510 				}
3511 			}
3512 		} else
3513 			DPRINTF(D_NET, "Listening on inet and/or inet6 socket\n");
3514 		DPRINTF(D_NET, "Sending on inet and/or inet6 socket\n");
3515 	}
3516 
3517 #ifndef DISABLE_TLS
3518 	/* TLS setup -- after all local destinations opened  */
3519 	DPRINTF(D_PARSE, "Parsed options: tls_ca: %s, tls_cadir: %s, "
3520 	    "tls_cert: %s, tls_key: %s, tls_verify: %s, "
3521 	    "bind: %s:%s, max. queue_lengths: %"
3522 	    PRId64 ", %" PRId64 ", %" PRId64 ", "
3523 	    "max. queue_sizes: %"
3524 	    PRId64 ", %" PRId64 ", %" PRId64 "\n",
3525 	    tls_opt.CAfile, tls_opt.CAdir,
3526 	    tls_opt.certfile, tls_opt.keyfile, tls_opt.x509verify,
3527 	    tls_opt.bindhost, tls_opt.bindport,
3528 	    TypeInfo[F_TLS].queue_length, TypeInfo[F_FILE].queue_length,
3529 	    TypeInfo[F_PIPE].queue_length,
3530 	    TypeInfo[F_TLS].queue_size, TypeInfo[F_FILE].queue_size,
3531 	    TypeInfo[F_PIPE].queue_size);
3532 	SLIST_FOREACH(cred, &tls_opt.cert_head, entries) {
3533 		DPRINTF(D_PARSE, "Accepting peer certificate "
3534 		    "from file: \"%s\"\n", cred->data);
3535 	}
3536 	SLIST_FOREACH(cred, &tls_opt.fprint_head, entries) {
3537 		DPRINTF(D_PARSE, "Accepting peer certificate with "
3538 		    "fingerprint: \"%s\"\n", cred->data);
3539 	}
3540 
3541 	/* Note: The order of initialization is important because syslog-sign
3542 	 * should use the TLS cert for signing. -- So we check first if TLS
3543 	 * will be used and initialize it before starting -sign.
3544 	 *
3545 	 * This means that if we are a client without TLS destinations TLS
3546 	 * will not be initialized and syslog-sign will generate a new key.
3547 	 * -- Even if the user has set a usable tls_cert.
3548 	 * Is this the expected behaviour? The alternative would be to always
3549 	 * initialize the TLS structures, even if they will not be needed
3550 	 * (or only needed to read the DSA key for -sign).
3551 	 */
3552 
3553 	/* Initialize TLS only if used */
3554 	if (tls_opt.server)
3555 		tls_status_msg = init_global_TLS_CTX();
3556 	else
3557 		for (f = Files; f; f = f->f_next) {
3558 			if (f->f_type != F_TLS)
3559 				continue;
3560 			tls_status_msg = init_global_TLS_CTX();
3561 			break;
3562 		}
3563 
3564 #endif /* !DISABLE_TLS */
3565 
3566 #ifndef DISABLE_SIGN
3567 	/* only initialize -sign if actually used */
3568 	if (GlobalSign.sg == 0 || GlobalSign.sg == 1 || GlobalSign.sg == 2)
3569 		(void)sign_global_init(Files);
3570 	else if (GlobalSign.sg == 3)
3571 		for (f = Files; f; f = f->f_next)
3572 			if (f->f_flags & FFLAG_SIGN) {
3573 				(void)sign_global_init(Files);
3574 				break;
3575 			}
3576 #endif /* !DISABLE_SIGN */
3577 
3578 #ifndef DISABLE_TLS
3579 	if (tls_status_msg) {
3580 		loginfo("%s", tls_status_msg);
3581 		free(tls_status_msg);
3582 	}
3583 	DPRINTF((D_NET|D_TLS), "Preparing sockets for TLS\n");
3584 	TLS_Listen_Set =
3585 		socksetup_tls(PF_UNSPEC, tls_opt.bindhost, tls_opt.bindport);
3586 
3587 	for (f = Files; f; f = f->f_next) {
3588 		if (f->f_type != F_TLS)
3589 			continue;
3590 		if (!tls_connect(f->f_un.f_tls.tls_conn)) {
3591 			logerror("Unable to connect to TLS server %s",
3592 			    f->f_un.f_tls.tls_conn->hostname);
3593 			/* Reconnect after x seconds  */
3594 			schedule_event(&f->f_un.f_tls.tls_conn->event,
3595 			    &((struct timeval){TLS_RECONNECT_SEC, 0}),
3596 			    tls_reconnect, f->f_un.f_tls.tls_conn);
3597 		}
3598 	}
3599 #endif /* !DISABLE_TLS */
3600 
3601 	loginfo("restart");
3602 	/*
3603 	 * Log a change in hostname, but only on a restart (we detect this
3604 	 * by checking to see if we're passed a kevent).
3605 	 */
3606 	if (oldLocalFQDN && strcmp(oldLocalFQDN, LocalFQDN) != 0)
3607 		loginfo("host name changed, \"%s\" to \"%s\"",
3608 		    oldLocalFQDN, LocalFQDN);
3609 
3610 	RESTORE_SIGNALS(omask);
3611 }
3612 
3613 /*
3614  * Crack a configuration file line
3615  */
3616 void
3617 cfline(size_t linenum, const char *line, struct filed *f, const char *prog,
3618     const char *host)
3619 {
3620 	struct addrinfo hints, *res;
3621 	int    error, i, pri, syncfile;
3622 	const char   *p, *q;
3623 	char *bp;
3624 	char   buf[MAXLINE];
3625 
3626 	DPRINTF((D_CALL|D_PARSE),
3627 		"cfline(%zu, \"%s\", f, \"%s\", \"%s\")\n",
3628 		linenum, line, prog, host);
3629 
3630 	errno = 0;	/* keep strerror() stuff out of logerror messages */
3631 
3632 	/* clear out file entry */
3633 	memset(f, 0, sizeof(*f));
3634 	for (i = 0; i <= LOG_NFACILITIES; i++)
3635 		f->f_pmask[i] = INTERNAL_NOPRI;
3636 	STAILQ_INIT(&f->f_qhead);
3637 
3638 	/*
3639 	 * There should not be any space before the log facility.
3640 	 * Check this is okay, complain and fix if it is not.
3641 	 */
3642 	q = line;
3643 	if (isblank((unsigned char)*line)) {
3644 		errno = 0;
3645 		logerror("Warning: `%s' space or tab before the log facility",
3646 		    line);
3647 		/* Fix: strip all spaces/tabs before the log facility */
3648 		while (*q++ && isblank((unsigned char)*q))
3649 			/* skip blanks */;
3650 		line = q;
3651 	}
3652 
3653 	/*
3654 	 * q is now at the first char of the log facility
3655 	 * There should be at least one tab after the log facility
3656 	 * Check this is okay, and complain and fix if it is not.
3657 	 */
3658 	q = line + strlen(line);
3659 	while (!isblank((unsigned char)*q) && (q != line))
3660 		q--;
3661 	if ((q == line) && strlen(line)) {
3662 		/* No tabs or space in a non empty line: complain */
3663 		errno = 0;
3664 		logerror(
3665 		    "Error: `%s' log facility or log target missing",
3666 		    line);
3667 		return;
3668 	}
3669 
3670 	/* save host name, if any */
3671 	if (*host == '*')
3672 		f->f_host = NULL;
3673 	else {
3674 		f->f_host = strdup(host);
3675 		trim_anydomain(f->f_host);
3676 	}
3677 
3678 	/* save program name, if any */
3679 	if (*prog == '*')
3680 		f->f_program = NULL;
3681 	else
3682 		f->f_program = strdup(prog);
3683 
3684 	/* scan through the list of selectors */
3685 	for (p = line; *p && !isblank((unsigned char)*p);) {
3686 		int pri_done, pri_cmp, pri_invert;
3687 
3688 		/* find the end of this facility name list */
3689 		for (q = p; *q && !isblank((unsigned char)*q) && *q++ != '.'; )
3690 			continue;
3691 
3692 		/* get the priority comparison */
3693 		pri_cmp = 0;
3694 		pri_done = 0;
3695 		pri_invert = 0;
3696 		if (*q == '!') {
3697 			pri_invert = 1;
3698 			q++;
3699 		}
3700 		while (! pri_done) {
3701 			switch (*q) {
3702 			case '<':
3703 				pri_cmp = PRI_LT;
3704 				q++;
3705 				break;
3706 			case '=':
3707 				pri_cmp = PRI_EQ;
3708 				q++;
3709 				break;
3710 			case '>':
3711 				pri_cmp = PRI_GT;
3712 				q++;
3713 				break;
3714 			default:
3715 				pri_done = 1;
3716 				break;
3717 			}
3718 		}
3719 
3720 		/* collect priority name */
3721 		for (bp = buf; *q && !strchr("\t ,;", *q); )
3722 			*bp++ = *q++;
3723 		*bp = '\0';
3724 
3725 		/* skip cruft */
3726 		while (strchr(",;", *q))
3727 			q++;
3728 
3729 		/* decode priority name */
3730 		if (*buf == '*') {
3731 			pri = LOG_PRIMASK + 1;
3732 			pri_cmp = PRI_LT | PRI_EQ | PRI_GT;
3733 		} else {
3734 			pri = decode(buf, prioritynames);
3735 			if (pri < 0) {
3736 				errno = 0;
3737 				logerror("Unknown priority name `%s'", buf);
3738 				return;
3739 			}
3740 		}
3741 		if (pri_cmp == 0)
3742 			pri_cmp = UniquePriority ? PRI_EQ
3743 						 : PRI_EQ | PRI_GT;
3744 		if (pri_invert)
3745 			pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT;
3746 
3747 		/* scan facilities */
3748 		while (*p && !strchr("\t .;", *p)) {
3749 			for (bp = buf; *p && !strchr("\t ,;.", *p); )
3750 				*bp++ = *p++;
3751 			*bp = '\0';
3752 			if (*buf == '*')
3753 				for (i = 0; i < LOG_NFACILITIES; i++) {
3754 					f->f_pmask[i] = pri;
3755 					f->f_pcmp[i] = pri_cmp;
3756 				}
3757 			else {
3758 				i = decode(buf, facilitynames);
3759 				if (i < 0) {
3760 					errno = 0;
3761 					logerror("Unknown facility name `%s'",
3762 					    buf);
3763 					return;
3764 				}
3765 				f->f_pmask[i >> 3] = pri;
3766 				f->f_pcmp[i >> 3] = pri_cmp;
3767 			}
3768 			while (*p == ',' || *p == ' ')
3769 				p++;
3770 		}
3771 
3772 		p = q;
3773 	}
3774 
3775 	/* skip to action part */
3776 	while (isblank((unsigned char)*p))
3777 		p++;
3778 
3779 	/*
3780 	 * should this be "#ifndef DISABLE_SIGN" or is it a general option?
3781 	 * '+' before file destination: write with PRI field for later
3782 	 * verification
3783 	 */
3784 	if (*p == '+') {
3785 		f->f_flags |= FFLAG_FULL;
3786 		p++;
3787 	}
3788 	if (*p == '-') {
3789 		syncfile = 0;
3790 		p++;
3791 	} else
3792 		syncfile = 1;
3793 
3794 	switch (*p) {
3795 	case '@':
3796 #ifndef DISABLE_SIGN
3797 		if (GlobalSign.sg == 3)
3798 			f->f_flags |= FFLAG_SIGN;
3799 #endif /* !DISABLE_SIGN */
3800 #ifndef DISABLE_TLS
3801 		if (*(p+1) == '[') {
3802 			/* TLS destination */
3803 			if (!parse_tls_destination(p, f, linenum)) {
3804 				logerror("Unable to parse action %s", p);
3805 				break;
3806 			}
3807 			f->f_type = F_TLS;
3808 			break;
3809 		}
3810 #endif /* !DISABLE_TLS */
3811 		(void)strlcpy(f->f_un.f_forw.f_hname, ++p,
3812 		    sizeof(f->f_un.f_forw.f_hname));
3813 		memset(&hints, 0, sizeof(hints));
3814 		hints.ai_family = AF_UNSPEC;
3815 		hints.ai_socktype = SOCK_DGRAM;
3816 		hints.ai_protocol = 0;
3817 		error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints,
3818 		    &res);
3819 		if (error) {
3820 			errno = 0;
3821 			logerror("%s", gai_strerror(error));
3822 			break;
3823 		}
3824 		f->f_un.f_forw.f_addr = res;
3825 		f->f_type = F_FORW;
3826 		NumForwards++;
3827 		break;
3828 
3829 	case '/':
3830 #ifndef DISABLE_SIGN
3831 		if (GlobalSign.sg == 3)
3832 			f->f_flags |= FFLAG_SIGN;
3833 #endif /* !DISABLE_SIGN */
3834 		(void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname));
3835 		if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
3836 			f->f_type = F_UNUSED;
3837 			logerror("%s", p);
3838 			break;
3839 		}
3840 		if (isatty(f->f_file)) {
3841 			f->f_type = F_TTY;
3842 			if (strcmp(p, ctty) == 0)
3843 				f->f_type = F_CONSOLE;
3844 			if (fcntl(f->f_file, F_SETFL, O_NONBLOCK) == -1)
3845 				logerror("Warning: cannot change tty fd for"
3846 				    " `%s' to non-blocking.", p);
3847 		} else
3848 			f->f_type = F_FILE;
3849 		if (syncfile)
3850 			f->f_flags |= FFLAG_SYNC;
3851 		break;
3852 
3853 	case '|':
3854 #ifndef DISABLE_SIGN
3855 		if (GlobalSign.sg == 3)
3856 			f->f_flags |= FFLAG_SIGN;
3857 #endif
3858 		f->f_un.f_pipe.f_pid = 0;
3859 		(void) strlcpy(f->f_un.f_pipe.f_pname, p + 1,
3860 		    sizeof(f->f_un.f_pipe.f_pname));
3861 		f->f_type = F_PIPE;
3862 		break;
3863 
3864 	case '*':
3865 		f->f_type = F_WALL;
3866 		break;
3867 
3868 	default:
3869 		for (i = 0; i < MAXUNAMES && *p; i++) {
3870 			for (q = p; *q && *q != ','; )
3871 				q++;
3872 			(void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
3873 			if ((q - p) > UT_NAMESIZE)
3874 				f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
3875 			else
3876 				f->f_un.f_uname[i][q - p] = '\0';
3877 			while (*q == ',' || *q == ' ')
3878 				q++;
3879 			p = q;
3880 		}
3881 		f->f_type = F_USERS;
3882 		break;
3883 	}
3884 }
3885 
3886 
3887 /*
3888  *  Decode a symbolic name to a numeric value
3889  */
3890 int
3891 decode(const char *name, CODE *codetab)
3892 {
3893 	CODE *c;
3894 	char *p, buf[40];
3895 
3896 	if (isdigit((unsigned char)*name))
3897 		return atoi(name);
3898 
3899 	for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
3900 		if (isupper((unsigned char)*name))
3901 			*p = tolower((unsigned char)*name);
3902 		else
3903 			*p = *name;
3904 	}
3905 	*p = '\0';
3906 	for (c = codetab; c->c_name; c++)
3907 		if (!strcmp(buf, c->c_name))
3908 			return c->c_val;
3909 
3910 	return -1;
3911 }
3912 
3913 /*
3914  * Retrieve the size of the kernel message buffer, via sysctl.
3915  */
3916 int
3917 getmsgbufsize(void)
3918 {
3919 #ifdef __NetBSD_Version__
3920 	int msgbufsize, mib[2];
3921 	size_t size;
3922 
3923 	mib[0] = CTL_KERN;
3924 	mib[1] = KERN_MSGBUFSIZE;
3925 	size = sizeof msgbufsize;
3926 	if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) {
3927 		DPRINTF(D_MISC, "Couldn't get kern.msgbufsize\n");
3928 		return 0;
3929 	}
3930 	return msgbufsize;
3931 #else
3932 	return MAXLINE;
3933 #endif /* __NetBSD_Version__ */
3934 }
3935 
3936 /*
3937  * Retrieve the hostname, via sysctl.
3938  */
3939 char *
3940 getLocalFQDN(void)
3941 {
3942 	int mib[2];
3943 	char *hostname;
3944 	size_t len;
3945 
3946 	mib[0] = CTL_KERN;
3947 	mib[1] = KERN_HOSTNAME;
3948 	sysctl(mib, 2, NULL, &len, NULL, 0);
3949 
3950 	if (!(hostname = malloc(len))) {
3951 		logerror("Unable to allocate memory");
3952 		die(0,0,NULL);
3953 	} else if (sysctl(mib, 2, hostname, &len, NULL, 0) == -1) {
3954 		DPRINTF(D_MISC, "Couldn't get kern.hostname\n");
3955 		(void)gethostname(hostname, sizeof(len));
3956 	}
3957 	return hostname;
3958 }
3959 
3960 struct socketEvent *
3961 socksetup(int af, const char *hostname)
3962 {
3963 	struct addrinfo hints, *res, *r;
3964 	int error, maxs;
3965 	int on = 1;
3966 	struct socketEvent *s, *socks;
3967 
3968 	if(SecureMode && !NumForwards)
3969 		return NULL;
3970 
3971 	memset(&hints, 0, sizeof(hints));
3972 	hints.ai_flags = AI_PASSIVE;
3973 	hints.ai_family = af;
3974 	hints.ai_socktype = SOCK_DGRAM;
3975 	error = getaddrinfo(hostname, "syslog", &hints, &res);
3976 	if (error) {
3977 		errno = 0;
3978 		logerror("%s", gai_strerror(error));
3979 		die(0, 0, NULL);
3980 	}
3981 
3982 	/* Count max number of sockets we may open */
3983 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
3984 		continue;
3985 	socks = calloc(maxs+1, sizeof(*socks));
3986 	if (!socks) {
3987 		logerror("Couldn't allocate memory for sockets");
3988 		die(0, 0, NULL);
3989 	}
3990 
3991 	socks->fd = 0;	 /* num of sockets counter at start of array */
3992 	s = socks + 1;
3993 	for (r = res; r; r = r->ai_next) {
3994 		s->fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
3995 		if (s->fd < 0) {
3996 			logerror("socket() failed");
3997 			continue;
3998 		}
3999 		s->af = r->ai_family;
4000 		if (r->ai_family == AF_INET6 && setsockopt(s->fd, IPPROTO_IPV6,
4001 		    IPV6_V6ONLY, &on, sizeof(on)) < 0) {
4002 			logerror("setsockopt(IPV6_V6ONLY) failed");
4003 			close(s->fd);
4004 			continue;
4005 		}
4006 
4007 		if (!SecureMode) {
4008 			if (bind(s->fd, r->ai_addr, r->ai_addrlen) < 0) {
4009 				logerror("bind() failed");
4010 				close(s->fd);
4011 				continue;
4012 			}
4013 			s->ev = allocev();
4014 			event_set(s->ev, s->fd, EV_READ | EV_PERSIST,
4015 				dispatch_read_finet, s->ev);
4016 			if (event_add(s->ev, NULL) == -1) {
4017 				DPRINTF((D_EVENT|D_NET),
4018 				    "Failure in event_add()\n");
4019 			} else {
4020 				DPRINTF((D_EVENT|D_NET),
4021 				    "Listen on UDP port "
4022 				    "(event@%p)\n", s->ev);
4023 			}
4024 		}
4025 
4026 		socks->fd++;  /* num counter */
4027 		s++;
4028 	}
4029 
4030 	if (res)
4031 		freeaddrinfo(res);
4032 	if (socks->fd == 0) {
4033 		free (socks);
4034 		if(Debug)
4035 			return NULL;
4036 		else
4037 			die(0, 0, NULL);
4038 	}
4039 	return socks;
4040 }
4041 
4042 /*
4043  * Fairly similar to popen(3), but returns an open descriptor, as opposed
4044  * to a FILE *.
4045  */
4046 int
4047 p_open(char *prog, pid_t *rpid)
4048 {
4049 	static char sh[] = "sh", mc[] = "-c";
4050 	int pfd[2], nulldesc, i;
4051 	pid_t pid;
4052 	char *argv[4];	/* sh -c cmd NULL */
4053 
4054 	if (pipe(pfd) == -1)
4055 		return -1;
4056 	if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) {
4057 		/* We are royally screwed anyway. */
4058 		return -1;
4059 	}
4060 
4061 	switch ((pid = fork())) {
4062 	case -1:
4063 		(void) close(nulldesc);
4064 		return -1;
4065 
4066 	case 0:
4067 		argv[0] = sh;
4068 		argv[1] = mc;
4069 		argv[2] = prog;
4070 		argv[3] = NULL;
4071 
4072 		(void) setsid();	/* avoid catching SIGHUPs. */
4073 
4074 		/*
4075 		 * Reset ignored signals to their default behavior.
4076 		 */
4077 		(void)signal(SIGTERM, SIG_DFL);
4078 		(void)signal(SIGINT, SIG_DFL);
4079 		(void)signal(SIGQUIT, SIG_DFL);
4080 		(void)signal(SIGPIPE, SIG_DFL);
4081 		(void)signal(SIGHUP, SIG_DFL);
4082 
4083 		dup2(pfd[0], STDIN_FILENO);
4084 		dup2(nulldesc, STDOUT_FILENO);
4085 		dup2(nulldesc, STDERR_FILENO);
4086 		for (i = getdtablesize(); i > 2; i--)
4087 			(void) close(i);
4088 
4089 		(void) execvp(_PATH_BSHELL, argv);
4090 		_exit(255);
4091 	}
4092 
4093 	(void) close(nulldesc);
4094 	(void) close(pfd[0]);
4095 
4096 	/*
4097 	 * Avoid blocking on a hung pipe.  With O_NONBLOCK, we are
4098 	 * supposed to get an EWOULDBLOCK on writev(2), which is
4099 	 * caught by the logic above anyway, which will in turn
4100 	 * close the pipe, and fork a new logging subprocess if
4101 	 * necessary.  The stale subprocess will be killed some
4102 	 * time later unless it terminated itself due to closing
4103 	 * its input pipe.
4104 	 */
4105 	if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) {
4106 		/* This is bad. */
4107 		logerror("Warning: cannot change pipe to pid %d to "
4108 		    "non-blocking.", (int) pid);
4109 	}
4110 	*rpid = pid;
4111 	return pfd[1];
4112 }
4113 
4114 void
4115 deadq_enter(pid_t pid, const char *name)
4116 {
4117 	dq_t p;
4118 	int status;
4119 
4120 	/*
4121 	 * Be paranoid: if we can't signal the process, don't enter it
4122 	 * into the dead queue (perhaps it's already dead).  If possible,
4123 	 * we try to fetch and log the child's status.
4124 	 */
4125 	if (kill(pid, 0) != 0) {
4126 		if (waitpid(pid, &status, WNOHANG) > 0)
4127 			log_deadchild(pid, status, name);
4128 		return;
4129 	}
4130 
4131 	p = malloc(sizeof(*p));
4132 	if (p == NULL) {
4133 		logerror("panic: out of memory!");
4134 		exit(1);
4135 	}
4136 
4137 	p->dq_pid = pid;
4138 	p->dq_timeout = DQ_TIMO_INIT;
4139 	TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries);
4140 }
4141 
4142 int
4143 deadq_remove(pid_t pid)
4144 {
4145 	dq_t q;
4146 
4147 	for (q = TAILQ_FIRST(&deadq_head); q != NULL;
4148 	     q = TAILQ_NEXT(q, dq_entries)) {
4149 		if (q->dq_pid == pid) {
4150 			TAILQ_REMOVE(&deadq_head, q, dq_entries);
4151 			free(q);
4152 			return 1;
4153 		}
4154 	}
4155 	return 0;
4156 }
4157 
4158 void
4159 log_deadchild(pid_t pid, int status, const char *name)
4160 {
4161 	int code;
4162 	char buf[256];
4163 	const char *reason;
4164 
4165 	/* Keep strerror() struff out of logerror messages. */
4166 	errno = 0;
4167 	if (WIFSIGNALED(status)) {
4168 		reason = "due to signal";
4169 		code = WTERMSIG(status);
4170 	} else {
4171 		reason = "with status";
4172 		code = WEXITSTATUS(status);
4173 		if (code == 0)
4174 			return;
4175 	}
4176 	(void) snprintf(buf, sizeof(buf),
4177 	    "Logging subprocess %d (%s) exited %s %d.",
4178 	    pid, name, reason, code);
4179 	logerror("%s", buf);
4180 }
4181 
4182 struct event *
4183 allocev(void)
4184 {
4185 	struct event *ev;
4186 
4187 	if (!(ev = calloc(1, sizeof(*ev))))
4188 		logerror("Unable to allocate memory");
4189 	return ev;
4190 }
4191 
4192 /* *ev is allocated if necessary */
4193 void
4194 schedule_event(struct event **ev, struct timeval *tv,
4195 	void (*cb)(int, short, void *), void *arg)
4196 {
4197 	if (!*ev && !(*ev = allocev())) {
4198 		return;
4199 	}
4200 	event_set(*ev, 0, 0, cb, arg);
4201 	DPRINTF(D_EVENT, "event_add(%s@%p)\n", "schedule_ev", *ev); \
4202 	if (event_add(*ev, tv) == -1) {
4203 		DPRINTF(D_EVENT, "Failure in event_add()\n");
4204 	}
4205 }
4206 
4207 #ifndef DISABLE_TLS
4208 /* abbreviation for freeing credential lists */
4209 void
4210 free_cred_SLIST(struct peer_cred_head *head)
4211 {
4212 	struct peer_cred *cred;
4213 
4214 	while (!SLIST_EMPTY(head)) {
4215 		cred = SLIST_FIRST(head);
4216 		SLIST_REMOVE_HEAD(head, entries);
4217 		FREEPTR(cred->data);
4218 		free(cred);
4219 	}
4220 }
4221 #endif /* !DISABLE_TLS */
4222 
4223 /*
4224  * send message queue after reconnect
4225  */
4226 /*ARGSUSED*/
4227 void
4228 send_queue(int fd, short event, void *arg)
4229 {
4230 	struct filed *f = (struct filed *) arg;
4231 	struct buf_queue *qentry;
4232 #define SQ_CHUNK_SIZE 250
4233 	size_t cnt = 0;
4234 
4235 #ifndef DISABLE_TLS
4236 	if (f->f_type == F_TLS) {
4237 		/* use a flag to prevent recursive calls to send_queue() */
4238 		if (f->f_un.f_tls.tls_conn->send_queue)
4239 			return;
4240 		else
4241 			f->f_un.f_tls.tls_conn->send_queue = true;
4242 	}
4243 	DPRINTF((D_DATA|D_CALL), "send_queue(f@%p with %zu msgs, "
4244 		"cnt@%p = %zu)\n", f, f->f_qelements, &cnt, cnt);
4245 #endif /* !DISABLE_TLS */
4246 
4247 	while ((qentry = STAILQ_FIRST(&f->f_qhead))) {
4248 #ifndef DISABLE_TLS
4249 		/* send_queue() might be called with an unconnected destination
4250 		 * from init() or die() or one message might take longer,
4251 		 * leaving the connection in state ST_WAITING and thus not
4252 		 * ready for the next message.
4253 		 * this check is a shortcut to skip these unnecessary calls */
4254 		if (f->f_type == F_TLS
4255 		    && f->f_un.f_tls.tls_conn->state != ST_TLS_EST) {
4256 			DPRINTF(D_TLS, "abort send_queue(cnt@%p = %zu) "
4257 			    "on TLS connection in state %d\n",
4258 			    &cnt, cnt, f->f_un.f_tls.tls_conn->state);
4259 			return;
4260 		 }
4261 #endif /* !DISABLE_TLS */
4262 		fprintlog(f, qentry->msg, qentry);
4263 
4264 		/* Sending a long queue can take some time during which
4265 		 * SIGHUP and SIGALRM are blocked and no events are handled.
4266 		 * To avoid that we only send SQ_CHUNK_SIZE messages at once
4267 		 * and then reschedule ourselves to continue. Thus the control
4268 		 * will return first from all signal-protected functions so a
4269 		 * possible SIGHUP/SIGALRM is handled and then back to the
4270 		 * main loop which can handle possible input.
4271 		 */
4272 		if (++cnt >= SQ_CHUNK_SIZE) {
4273 			if (!f->f_sq_event) { /* alloc on demand */
4274 				f->f_sq_event = allocev();
4275 				event_set(f->f_sq_event, 0, 0, send_queue, f);
4276 			}
4277 			if (event_add(f->f_sq_event, &((struct timeval){0, 1})) == -1) {
4278 				DPRINTF(D_EVENT, "Failure in event_add()\n");
4279 			}
4280 			break;
4281 		}
4282 	}
4283 #ifndef DISABLE_TLS
4284 	if (f->f_type == F_TLS)
4285 		f->f_un.f_tls.tls_conn->send_queue = false;
4286 #endif
4287 
4288 }
4289 
4290 /*
4291  * finds the next queue element to delete
4292  *
4293  * has stateful behaviour, before using it call once with reset = true
4294  * after that every call will return one next queue elemen to delete,
4295  * depending on strategy either the oldest or the one with the lowest priority
4296  */
4297 static struct buf_queue *
4298 find_qentry_to_delete(const struct buf_queue_head *head, int strategy,
4299     bool reset)
4300 {
4301 	static int pri;
4302 	static struct buf_queue *qentry_static;
4303 
4304 	struct buf_queue *qentry_tmp;
4305 
4306 	if (reset || STAILQ_EMPTY(head)) {
4307 		pri = LOG_DEBUG;
4308 		qentry_static = STAILQ_FIRST(head);
4309 		return NULL;
4310 	}
4311 
4312 	/* find elements to delete */
4313 	if (strategy == PURGE_BY_PRIORITY) {
4314 		qentry_tmp = qentry_static;
4315 		while ((qentry_tmp = STAILQ_NEXT(qentry_tmp, entries)) != NULL)
4316 		{
4317 			if (LOG_PRI(qentry_tmp->msg->pri) == pri) {
4318 				/* save the successor, because qentry_tmp
4319 				 * is probably deleted by the caller */
4320 				qentry_static = STAILQ_NEXT(qentry_tmp, entries);
4321 				return qentry_tmp;
4322 			}
4323 		}
4324 		/* nothing found in while loop --> next pri */
4325 		if (--pri)
4326 			return find_qentry_to_delete(head, strategy, false);
4327 		else
4328 			return NULL;
4329 	} else /* strategy == PURGE_OLDEST or other value */ {
4330 		qentry_tmp = qentry_static;
4331 		qentry_static = STAILQ_NEXT(qentry_tmp, entries);
4332 		return qentry_tmp;  /* is NULL on empty queue */
4333 	}
4334 }
4335 
4336 /* note on TAILQ: newest message added at TAIL,
4337  *		  oldest to be removed is FIRST
4338  */
4339 /*
4340  * checks length of a destination's message queue
4341  * if del_entries == 0 then assert queue length is
4342  *   less or equal to configured number of queue elements
4343  * otherwise del_entries tells how many entries to delete
4344  *
4345  * returns the number of removed queue elements
4346  * (which not necessarily means free'd messages)
4347  *
4348  * strategy PURGE_OLDEST to delete oldest entry, e.g. after it was resent
4349  * strategy PURGE_BY_PRIORITY to delete messages with lowest priority first,
4350  *	this is much slower but might be desirable when unsent messages have
4351  *	to be deleted, e.g. in call from domark()
4352  */
4353 size_t
4354 message_queue_purge(struct filed *f, size_t del_entries, int strategy)
4355 {
4356 	size_t removed = 0;
4357 	struct buf_queue *qentry = NULL;
4358 
4359 	DPRINTF((D_CALL|D_BUFFER), "purge_message_queue(%p, %zu, %d) with "
4360 	    "f_qelements=%zu and f_qsize=%zu\n",
4361 	    f, del_entries, strategy,
4362 	    f->f_qelements, f->f_qsize);
4363 
4364 	/* reset state */
4365 	(void)find_qentry_to_delete(&f->f_qhead, strategy, true);
4366 
4367 	while (removed < del_entries
4368 	    || (TypeInfo[f->f_type].queue_length != -1
4369 	    && (size_t)TypeInfo[f->f_type].queue_length > f->f_qelements)
4370 	    || (TypeInfo[f->f_type].queue_size != -1
4371 	    && (size_t)TypeInfo[f->f_type].queue_size > f->f_qsize)) {
4372 		qentry = find_qentry_to_delete(&f->f_qhead, strategy, 0);
4373 		if (message_queue_remove(f, qentry))
4374 			removed++;
4375 		else
4376 			break;
4377 	}
4378 	return removed;
4379 }
4380 
4381 /* run message_queue_purge() for all destinations to free memory */
4382 size_t
4383 message_allqueues_purge(void)
4384 {
4385 	size_t sum = 0;
4386 	struct filed *f;
4387 
4388 	for (f = Files; f; f = f->f_next)
4389 		sum += message_queue_purge(f,
4390 		    f->f_qelements/10, PURGE_BY_PRIORITY);
4391 
4392 	DPRINTF(D_BUFFER,
4393 	    "message_allqueues_purge(): removed %zu buffer entries\n", sum);
4394 	return sum;
4395 }
4396 
4397 /* run message_queue_purge() for all destinations to check limits */
4398 size_t
4399 message_allqueues_check(void)
4400 {
4401 	size_t sum = 0;
4402 	struct filed *f;
4403 
4404 	for (f = Files; f; f = f->f_next)
4405 		sum += message_queue_purge(f, 0, PURGE_BY_PRIORITY);
4406 	DPRINTF(D_BUFFER,
4407 	    "message_allqueues_check(): removed %zu buffer entries\n", sum);
4408 	return sum;
4409 }
4410 
4411 struct buf_msg *
4412 buf_msg_new(const size_t len)
4413 {
4414 	struct buf_msg *newbuf;
4415 
4416 	CALLOC(newbuf, sizeof(*newbuf));
4417 
4418 	if (len) { /* len = 0 is valid */
4419 		MALLOC(newbuf->msg, len);
4420 		newbuf->msgorig = newbuf->msg;
4421 		newbuf->msgsize = len;
4422 	}
4423 	return NEWREF(newbuf);
4424 }
4425 
4426 void
4427 buf_msg_free(struct buf_msg *buf)
4428 {
4429 	if (!buf)
4430 		return;
4431 
4432 	buf->refcount--;
4433 	if (buf->refcount == 0) {
4434 		FREEPTR(buf->timestamp);
4435 		/* small optimizations: the host/recvhost may point to the
4436 		 * global HostName/FQDN. of course this must not be free()d
4437 		 * same goes for appname and include_pid
4438 		 */
4439 		if (buf->recvhost != buf->host
4440 		    && buf->recvhost != LocalHostName
4441 		    && buf->recvhost != LocalFQDN
4442 		    && buf->recvhost != oldLocalFQDN)
4443 			FREEPTR(buf->recvhost);
4444 		if (buf->host != LocalHostName
4445 		    && buf->host != LocalFQDN
4446 		    && buf->host != oldLocalFQDN)
4447 			FREEPTR(buf->host);
4448 		if (buf->prog != appname)
4449 			FREEPTR(buf->prog);
4450 		if (buf->pid != include_pid)
4451 			FREEPTR(buf->pid);
4452 		FREEPTR(buf->msgid);
4453 		FREEPTR(buf->sd);
4454 		FREEPTR(buf->msgorig);	/* instead of msg */
4455 		FREEPTR(buf);
4456 	}
4457 }
4458 
4459 size_t
4460 buf_queue_obj_size(struct buf_queue *qentry)
4461 {
4462 	size_t sum = 0;
4463 
4464 	if (!qentry)
4465 		return 0;
4466 	sum += sizeof(*qentry)
4467 	    + sizeof(*qentry->msg)
4468 	    + qentry->msg->msgsize
4469 	    + SAFEstrlen(qentry->msg->timestamp)+1
4470 	    + SAFEstrlen(qentry->msg->msgid)+1;
4471 	if (qentry->msg->prog
4472 	    && qentry->msg->prog != include_pid)
4473 		sum += strlen(qentry->msg->prog)+1;
4474 	if (qentry->msg->pid
4475 	    && qentry->msg->pid != appname)
4476 		sum += strlen(qentry->msg->pid)+1;
4477 	if (qentry->msg->recvhost
4478 	    && qentry->msg->recvhost != LocalHostName
4479 	    && qentry->msg->recvhost != LocalFQDN
4480 	    && qentry->msg->recvhost != oldLocalFQDN)
4481 		sum += strlen(qentry->msg->recvhost)+1;
4482 	if (qentry->msg->host
4483 	    && qentry->msg->host != LocalHostName
4484 	    && qentry->msg->host != LocalFQDN
4485 	    && qentry->msg->host != oldLocalFQDN)
4486 		sum += strlen(qentry->msg->host)+1;
4487 
4488 	return sum;
4489 }
4490 
4491 bool
4492 message_queue_remove(struct filed *f, struct buf_queue *qentry)
4493 {
4494 	if (!f || !qentry || !qentry->msg)
4495 		return false;
4496 
4497 	assert(!STAILQ_EMPTY(&f->f_qhead));
4498 	STAILQ_REMOVE(&f->f_qhead, qentry, buf_queue, entries);
4499 	f->f_qelements--;
4500 	f->f_qsize -= buf_queue_obj_size(qentry);
4501 
4502 	DPRINTF(D_BUFFER, "msg @%p removed from queue @%p, new qlen = %zu\n",
4503 	    qentry->msg, f, f->f_qelements);
4504 	DELREF(qentry->msg);
4505 	FREEPTR(qentry);
4506 	return true;
4507 }
4508 
4509 /*
4510  * returns *qentry on success and NULL on error
4511  */
4512 struct buf_queue *
4513 message_queue_add(struct filed *f, struct buf_msg *buffer)
4514 {
4515 	struct buf_queue *qentry;
4516 
4517 	/* check on every call or only every n-th time? */
4518 	message_queue_purge(f, 0, PURGE_BY_PRIORITY);
4519 
4520 	while (!(qentry = malloc(sizeof(*qentry)))
4521 	    && message_queue_purge(f, 1, PURGE_OLDEST))
4522 		continue;
4523 	if (!qentry) {
4524 		logerror("Unable to allocate memory");
4525 		DPRINTF(D_BUFFER, "queue empty, no memory, msg dropped\n");
4526 		return NULL;
4527 	} else {
4528 		qentry->msg = buffer;
4529 		f->f_qelements++;
4530 		f->f_qsize += buf_queue_obj_size(qentry);
4531 		STAILQ_INSERT_TAIL(&f->f_qhead, qentry, entries);
4532 
4533 		DPRINTF(D_BUFFER, "msg @%p queued @%p, qlen = %zu\n",
4534 		    buffer, f, f->f_qelements);
4535 		return qentry;
4536 	}
4537 }
4538 
4539 void
4540 message_queue_freeall(struct filed *f)
4541 {
4542 	struct buf_queue *qentry;
4543 
4544 	if (!f) return;
4545 	DPRINTF(D_MEM, "message_queue_freeall(f@%p) with f_qhead@%p\n", f,
4546 	    &f->f_qhead);
4547 
4548 	while (!STAILQ_EMPTY(&f->f_qhead)) {
4549 		qentry = STAILQ_FIRST(&f->f_qhead);
4550 		STAILQ_REMOVE(&f->f_qhead, qentry, buf_queue, entries);
4551 		DELREF(qentry->msg);
4552 		FREEPTR(qentry);
4553 	}
4554 
4555 	f->f_qelements = 0;
4556 	f->f_qsize = 0;
4557 }
4558 
4559 #ifndef DISABLE_TLS
4560 /* utility function for tls_reconnect() */
4561 struct filed *
4562 get_f_by_conninfo(struct tls_conn_settings *conn_info)
4563 {
4564 	struct filed *f;
4565 
4566 	for (f = Files; f; f = f->f_next) {
4567 		if ((f->f_type == F_TLS) && f->f_un.f_tls.tls_conn == conn_info)
4568 			return f;
4569 	}
4570 	DPRINTF(D_TLS, "get_f_by_conninfo() called on invalid conn_info\n");
4571 	return NULL;
4572 }
4573 
4574 /*
4575  * Called on signal.
4576  * Lets the admin reconnect without waiting for the reconnect timer expires.
4577  */
4578 /*ARGSUSED*/
4579 void
4580 dispatch_force_tls_reconnect(int fd, short event, void *ev)
4581 {
4582 	struct filed *f;
4583 	DPRINTF((D_TLS|D_CALL|D_EVENT), "dispatch_force_tls_reconnect()\n");
4584 	for (f = Files; f; f = f->f_next) {
4585 		if (f->f_type == F_TLS &&
4586 		    f->f_un.f_tls.tls_conn->state == ST_NONE)
4587 			tls_reconnect(fd, event, f->f_un.f_tls.tls_conn);
4588 	}
4589 }
4590 #endif /* !DISABLE_TLS */
4591 
4592 /*
4593  * return a timestamp in a static buffer,
4594  * either format the timestamp given by parameter in_now
4595  * or use the current time if in_now is NULL.
4596  */
4597 char *
4598 make_timestamp(time_t *in_now, bool iso)
4599 {
4600 	int frac_digits = 6;
4601 	struct timeval tv;
4602 	time_t mytime;
4603 	struct tm ltime;
4604 	int len = 0;
4605 	int tzlen = 0;
4606 	/* uses global var: time_t now; */
4607 
4608 	if (in_now) {
4609 		mytime = *in_now;
4610 	} else {
4611 		gettimeofday(&tv, NULL);
4612 		mytime = now = (time_t) tv.tv_sec;
4613 	}
4614 
4615 	if (!iso) {
4616 		strlcpy(timestamp, ctime(&mytime) + 4, TIMESTAMPBUFSIZE);
4617 		timestamp[BSD_TIMESTAMPLEN] = '\0';
4618 		return timestamp;
4619 	}
4620 
4621 	localtime_r(&mytime, &ltime);
4622 	len += strftime(timestamp, TIMESTAMPBUFSIZE, "%FT%T", &ltime);
4623 	snprintf(&(timestamp[len]), frac_digits+2, ".%.*ld",
4624 		frac_digits, (long)tv.tv_usec);
4625 	len += frac_digits+1;
4626 	tzlen = strftime(&(timestamp[len]), TIMESTAMPBUFSIZE-len, "%z", &ltime);
4627 	len += tzlen;
4628 
4629 	if (tzlen == 5) {
4630 		/* strftime gives "+0200", but we need "+02:00" */
4631 		timestamp[len+1] = timestamp[len];
4632 		timestamp[len] = timestamp[len-1];
4633 		timestamp[len-1] = timestamp[len-2];
4634 		timestamp[len-2] = ':';
4635 	}
4636 	return timestamp;
4637 }
4638 
4639 /* auxillary code to allocate memory and copy a string */
4640 bool
4641 copy_string(char **mem, const char *p, const char *q)
4642 {
4643 	const size_t len = 1 + q - p;
4644 	if (!(*mem = malloc(len))) {
4645 		logerror("Unable to allocate memory for config");
4646 		return false;
4647 	}
4648 	strlcpy(*mem, p, len);
4649 	return true;
4650 }
4651 
4652 /* keyword has to end with ",  everything until next " is copied */
4653 bool
4654 copy_config_value_quoted(const char *keyword, char **mem, const char **p)
4655 {
4656 	const char *q;
4657 	if (strncasecmp(*p, keyword, strlen(keyword)))
4658 		return false;
4659 	q = *p += strlen(keyword);
4660 	if (!(q = strchr(*p, '"'))) {
4661 		errno = 0;
4662 		logerror("unterminated \"\n");
4663 		return false;
4664 	}
4665 	if (!(copy_string(mem, *p, q)))
4666 		return false;
4667 	*p = ++q;
4668 	return true;
4669 }
4670 
4671 /* for config file:
4672  * following = required but whitespace allowed, quotes optional
4673  * if numeric, then conversion to integer and no memory allocation
4674  */
4675 bool
4676 copy_config_value(const char *keyword, char **mem,
4677 	const char **p, const char *file, int line)
4678 {
4679 	if (strncasecmp(*p, keyword, strlen(keyword)))
4680 		return false;
4681 	*p += strlen(keyword);
4682 
4683 	while (isspace((unsigned char)**p))
4684 		*p += 1;
4685 	if (**p != '=') {
4686 		errno = 0;
4687 		logerror("expected \"=\" in file %s, line %d", file, line);
4688 		return false;
4689 	}
4690 	*p += 1;
4691 
4692 	return copy_config_value_word(mem, p);
4693 }
4694 
4695 /* copy next parameter from a config line */
4696 bool
4697 copy_config_value_word(char **mem, const char **p)
4698 {
4699 	const char *q;
4700 	while (isspace((unsigned char)**p))
4701 		*p += 1;
4702 	if (**p == '"')
4703 		return copy_config_value_quoted("\"", mem, p);
4704 
4705 	/* without quotes: find next whitespace or end of line */
4706 	(void)((q = strchr(*p, ' ')) || (q = strchr(*p, '\t'))
4707 	     || (q = strchr(*p, '\n')) || (q = strchr(*p, '\0')));
4708 
4709 	if (q-*p == 0 || !(copy_string(mem, *p, q)))
4710 		return false;
4711 
4712 	*p = ++q;
4713 	return true;
4714 }
4715 
4716 static int
4717 writev1(int fd, struct iovec *iov, size_t count)
4718 {
4719 	ssize_t nw = 0, tot = 0;
4720 	size_t ntries = 5;
4721 
4722 	if (count == 0)
4723 		return 0;
4724 	while (ntries--) {
4725 		switch ((nw = writev(fd, iov, count))) {
4726 		case -1:
4727 			if (errno == EAGAIN || errno == EWOULDBLOCK) {
4728 				struct pollfd pfd;
4729 				pfd.fd = fd;
4730 				pfd.events = POLLOUT;
4731 				pfd.revents = 0;
4732 				(void)poll(&pfd, 1, 500);
4733 				continue;
4734 			}
4735 			return -1;
4736 		case 0:
4737 			return 0;
4738 		default:
4739 			tot += nw;
4740 			while (nw > 0) {
4741 				if (iov->iov_len > (size_t)nw) {
4742 					iov->iov_len -= nw;
4743 					iov->iov_base =
4744 					    (char *)iov->iov_base + nw;
4745 					break;
4746 				} else {
4747 					if (--count == 0)
4748 						return tot;
4749 					nw -= iov->iov_len;
4750 					iov++;
4751 				}
4752 			}
4753 		}
4754 	}
4755 	return tot == 0 ? nw : tot;
4756 }
4757