1/*	$OpenBSD: syslog_r.c,v 1.20 2024/04/03 04:36:53 deraadt Exp $ */
2/*
3 * Copyright (c) 1983, 1988, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/uio.h>
33#include <netdb.h>
34
35#include <errno.h>
36#include <fcntl.h>
37#include <paths.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <syslog.h>
42#include <time.h>
43#include <unistd.h>
44#include <limits.h>
45#include <stdarg.h>
46
47/* Reentrant version of syslog, i.e. syslog_r() */
48void
49syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
50{
51	va_list ap;
52
53	va_start(ap, fmt);
54	vsyslog_r(pri, data, fmt, ap);
55	va_end(ap);
56}
57DEF_WEAK(syslog_r);
58
59void
60vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
61{
62	__vsyslog_r(pri, data, 1, fmt, ap);
63}
64DEF_WEAK(vsyslog_r);
65
66/*
67 * This is used by both syslog_r and syslog.
68 */
69void
70__vsyslog_r(int pri, struct syslog_data *data,
71    int reentrant, const char *fmt, va_list ap)
72{
73	int cnt;
74	char ch, *p, *t;
75	int saved_errno = errno;	/* use original errno */
76#define	TBUF_SIZE	(LOG_MAXLINE+1)
77#define	FMT_SIZE	(1024+1)
78	char *stdp = NULL, tbuf[TBUF_SIZE], fmt_cpy[FMT_SIZE];
79	int tbuf_left, fmt_left, prlen;
80
81#define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
82	/* Check for invalid bits. */
83	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
84		syslog_r(INTERNALLOG, data,
85		    "syslog%s: unknown facility/priority: %x",
86		    reentrant ? "_r" : "", pri);
87		pri &= LOG_PRIMASK|LOG_FACMASK;
88	}
89
90	/* Check priority against setlogmask values. */
91	if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
92		goto done;
93
94	/* Set default facility if none specified. */
95	if ((pri & LOG_FACMASK) == 0)
96		pri |= data->log_fac;
97
98	p = tbuf;
99	tbuf_left = TBUF_SIZE;
100
101#define	DEC()	\
102	do {					\
103		if (prlen < 0)			\
104			prlen = 0;		\
105		if (prlen >= tbuf_left)		\
106			prlen = tbuf_left - 1;	\
107		p += prlen;			\
108		tbuf_left -= prlen;		\
109	} while (0)
110
111	prlen = snprintf(p, tbuf_left, "<%d>", pri);
112	DEC();
113
114	if (data->log_stat & LOG_PERROR)
115		stdp = p;
116	if (data->log_tag == NULL)
117		data->log_tag = __progname;
118	if (data->log_tag != NULL) {
119		prlen = snprintf(p, tbuf_left, "%.*s", NAME_MAX, data->log_tag);
120		DEC();
121	}
122	if (data->log_stat & LOG_PID) {
123		prlen = snprintf(p, tbuf_left, "[%ld]", (long)getpid());
124		DEC();
125	}
126	if (data->log_tag != NULL) {
127		if (tbuf_left > 1) {
128			*p++ = ':';
129			tbuf_left--;
130		}
131		if (tbuf_left > 1) {
132			*p++ = ' ';
133			tbuf_left--;
134		}
135	}
136
137	for (t = fmt_cpy, fmt_left = FMT_SIZE;
138	    (ch = *fmt) != '\0' && fmt_left > 1;
139	    ++fmt) {
140		if (ch == '%' && fmt[1] == 'm') {
141			char ebuf[NL_TEXTMAX];
142
143			++fmt;
144			(void)strerror_r(saved_errno, ebuf, sizeof(ebuf));
145			prlen = snprintf(t, fmt_left, "%s", ebuf);
146			if (prlen < 0)
147				prlen = 0;
148			if (prlen >= fmt_left)
149				prlen = fmt_left - 1;
150			t += prlen;
151			fmt_left -= prlen;
152		} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
153			++fmt;
154			*t++ = '%';
155			*t++ = '%';
156			fmt_left -= 2;
157		} else {
158			*t++ = ch;
159			fmt_left--;
160		}
161	}
162	*t = '\0';
163
164	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
165	DEC();
166	cnt = p - tbuf;
167	while (cnt > 0 && p[-1] == '\n') {
168		*(--p) = '\0';
169		--cnt;
170	}
171
172	/* Output to stderr if requested. */
173	if (data->log_stat & LOG_PERROR) {
174		struct iovec iov[2];
175
176		iov[0].iov_base = stdp;
177		iov[0].iov_len = cnt > stdp - tbuf ? cnt - (stdp - tbuf) : 0;
178		iov[1].iov_base = "\n";
179		iov[1].iov_len = 1;
180		(void)writev(STDERR_FILENO, iov, 2);
181	}
182
183	/*
184	 * If the sendsyslog() fails, it means that syslogd
185	 * is not running or the kernel ran out of buffers.
186	 */
187	sendsyslog(tbuf, cnt, data->log_stat & LOG_CONS);
188done:
189	errno = saved_errno;
190}
191
192void
193openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
194{
195	if (ident != NULL)
196		data->log_tag = ident;
197	data->log_stat = logstat;
198	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
199		data->log_fac = logfac;
200}
201DEF_WEAK(openlog_r);
202
203void
204closelog_r(struct syslog_data *data)
205{
206	data->log_tag = NULL;
207}
208DEF_WEAK(closelog_r);
209