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