1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: releng/11.0/usr.sbin/ctld/log.c 296897 2016-03-15 11:03:45Z trasz $");
33
34#include <errno.h>
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <syslog.h>
40#include <vis.h>
41
42#include "ctld.h"
43
44static int log_level = 0;
45static char *peer_name = NULL;
46static char *peer_addr = NULL;
47
48#define	MSGBUF_LEN	1024
49
50void
51log_init(int level)
52{
53
54	log_level = level;
55	openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
56}
57
58void
59log_set_peer_name(const char *name)
60{
61
62	/*
63	 * XXX: Turn it into assertion?
64	 */
65	if (peer_name != NULL)
66		log_errx(1, "%s called twice", __func__);
67	if (peer_addr == NULL)
68		log_errx(1, "%s called before log_set_peer_addr", __func__);
69
70	peer_name = checked_strdup(name);
71}
72
73void
74log_set_peer_addr(const char *addr)
75{
76
77	/*
78	 * XXX: Turn it into assertion?
79	 */
80	if (peer_addr != NULL)
81		log_errx(1, "%s called twice", __func__);
82
83	peer_addr = checked_strdup(addr);
84}
85
86static void
87log_common(int priority, int log_errno, const char *fmt, va_list ap)
88{
89	static char msgbuf[MSGBUF_LEN];
90	static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
91	char *errstr;
92	int ret;
93
94	ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
95	if (ret < 0) {
96		fprintf(stderr, "%s: snprintf failed", getprogname());
97		syslog(LOG_CRIT, "snprintf failed");
98		exit(1);
99	}
100
101	ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
102	if (ret < 0) {
103		fprintf(stderr, "%s: strnvis failed", getprogname());
104		syslog(LOG_CRIT, "strnvis failed");
105		exit(1);
106	}
107
108	if (log_errno == -1) {
109		if (peer_name != NULL) {
110			fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
111			    peer_addr, peer_name, msgbuf_strvised);
112			syslog(priority, "%s (%s): %s",
113			    peer_addr, peer_name, msgbuf_strvised);
114		} else if (peer_addr != NULL) {
115			fprintf(stderr, "%s: %s: %s\n", getprogname(),
116			    peer_addr, msgbuf_strvised);
117			syslog(priority, "%s: %s",
118			    peer_addr, msgbuf_strvised);
119		} else {
120			fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
121			syslog(priority, "%s", msgbuf_strvised);
122		}
123
124	} else {
125		errstr = strerror(log_errno);
126
127		if (peer_name != NULL) {
128			fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
129			    peer_addr, peer_name, msgbuf_strvised, errstr);
130			syslog(priority, "%s (%s): %s: %s",
131			    peer_addr, peer_name, msgbuf_strvised, errstr);
132		} else if (peer_addr != NULL) {
133			fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
134			    peer_addr, msgbuf_strvised, errstr);
135			syslog(priority, "%s: %s: %s",
136			    peer_addr, msgbuf_strvised, errstr);
137		} else {
138			fprintf(stderr, "%s: %s: %s\n", getprogname(),
139			    msgbuf_strvised, errstr);
140			syslog(priority, "%s: %s",
141			    msgbuf_strvised, errstr);
142		}
143	}
144}
145
146void
147log_err(int eval, const char *fmt, ...)
148{
149	va_list ap;
150
151	va_start(ap, fmt);
152	log_common(LOG_CRIT, errno, fmt, ap);
153	va_end(ap);
154
155	exit(eval);
156}
157
158void
159log_errx(int eval, const char *fmt, ...)
160{
161	va_list ap;
162
163	va_start(ap, fmt);
164	log_common(LOG_CRIT, -1, fmt, ap);
165	va_end(ap);
166
167	exit(eval);
168}
169
170void
171log_warn(const char *fmt, ...)
172{
173	va_list ap;
174
175	va_start(ap, fmt);
176	log_common(LOG_WARNING, errno, fmt, ap);
177	va_end(ap);
178}
179
180void
181log_warnx(const char *fmt, ...)
182{
183	va_list ap;
184
185	va_start(ap, fmt);
186	log_common(LOG_WARNING, -1, fmt, ap);
187	va_end(ap);
188}
189
190void
191log_debugx(const char *fmt, ...)
192{
193	va_list ap;
194
195	if (log_level == 0)
196		return;
197
198	va_start(ap, fmt);
199	log_common(LOG_DEBUG, -1, fmt, ap);
200	va_end(ap);
201}
202