2 f224a3fe 2021-08-24 xhr * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
3 53d188a0 2021-08-26 xhr * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
5 f224a3fe 2021-08-24 xhr * Permission to use, copy, modify, and distribute this software for any
6 f224a3fe 2021-08-24 xhr * purpose with or without fee is hereby granted, provided that the above
7 f224a3fe 2021-08-24 xhr * copyright notice and this permission notice appear in all copies.
9 f224a3fe 2021-08-24 xhr * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 f224a3fe 2021-08-24 xhr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 f224a3fe 2021-08-24 xhr * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 f224a3fe 2021-08-24 xhr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 f224a3fe 2021-08-24 xhr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 f224a3fe 2021-08-24 xhr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 f224a3fe 2021-08-24 xhr * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f224a3fe 2021-08-24 xhr #define _GNU_SOURCE
20 f224a3fe 2021-08-24 xhr #include <sys/types.h>
21 f224a3fe 2021-08-24 xhr #include <sys/stat.h>
23 f224a3fe 2021-08-24 xhr #include <stdio.h>
24 f224a3fe 2021-08-24 xhr #include <stdlib.h>
25 f224a3fe 2021-08-24 xhr #include <stdarg.h>
26 f224a3fe 2021-08-24 xhr #include <string.h>
27 f224a3fe 2021-08-24 xhr #include <syslog.h>
28 f224a3fe 2021-08-24 xhr #include <errno.h>
29 f224a3fe 2021-08-24 xhr #include <fcntl.h>
30 f224a3fe 2021-08-24 xhr #include <unistd.h>
31 f224a3fe 2021-08-24 xhr #include <time.h>
33 f224a3fe 2021-08-24 xhr #include "log.h"
34 f224a3fe 2021-08-24 xhr #include "twind.h"
36 f224a3fe 2021-08-24 xhr #define MAXLOGLINE 1024
38 f224a3fe 2021-08-24 xhr static int debug;
39 f224a3fe 2021-08-24 xhr static int verbose;
40 f224a3fe 2021-08-24 xhr static int access_fd;
41 f224a3fe 2021-08-24 xhr static int error_fd;
43 f224a3fe 2021-08-24 xhr static const char *log_procname;
46 f224a3fe 2021-08-24 xhr log_init(int n_debug, int facility)
48 f224a3fe 2021-08-24 xhr extern char *__progname;
50 f224a3fe 2021-08-24 xhr debug = n_debug;
51 f224a3fe 2021-08-24 xhr verbose = n_debug;
52 f224a3fe 2021-08-24 xhr log_procinit(__progname);
55 f224a3fe 2021-08-24 xhr openlog(__progname, LOG_PID | LOG_NDELAY, facility);
61 f224a3fe 2021-08-24 xhr log_procinit(const char *procname)
63 f224a3fe 2021-08-24 xhr if (procname != NULL)
64 f224a3fe 2021-08-24 xhr log_procname = procname;
68 f224a3fe 2021-08-24 xhr log_setverbose(int v)
74 f224a3fe 2021-08-24 xhr log_getverbose(void)
76 f224a3fe 2021-08-24 xhr return (verbose);
80 f224a3fe 2021-08-24 xhr logit(int pri, const char *fmt, ...)
84 f224a3fe 2021-08-24 xhr va_start(ap, fmt);
85 f224a3fe 2021-08-24 xhr vlog(pri, fmt, ap);
90 f224a3fe 2021-08-24 xhr vlog(int pri, const char *fmt, va_list ap)
93 f224a3fe 2021-08-24 xhr int saved_errno = errno;
96 f224a3fe 2021-08-24 xhr /* best effort in out of mem situations */
97 f224a3fe 2021-08-24 xhr if (asprintf(&nfmt, "%s\n", fmt) == -1) {
98 f224a3fe 2021-08-24 xhr vfprintf(stderr, fmt, ap);
99 f224a3fe 2021-08-24 xhr fprintf(stderr, "\n");
101 f224a3fe 2021-08-24 xhr vfprintf(stderr, nfmt, ap);
104 f224a3fe 2021-08-24 xhr fflush(stderr);
106 f224a3fe 2021-08-24 xhr vsyslog(pri, fmt, ap);
108 f224a3fe 2021-08-24 xhr errno = saved_errno;
112 f224a3fe 2021-08-24 xhr log_warn(const char *emsg, ...)
116 f224a3fe 2021-08-24 xhr int saved_errno = errno;
118 f224a3fe 2021-08-24 xhr /* best effort to even work in out of memory situations */
119 f224a3fe 2021-08-24 xhr if (emsg == NULL)
120 f224a3fe 2021-08-24 xhr logit(LOG_ERR, "%s", strerror(saved_errno));
122 f224a3fe 2021-08-24 xhr va_start(ap, emsg);
124 f224a3fe 2021-08-24 xhr if (asprintf(&nfmt, "%s: %s", emsg,
125 f224a3fe 2021-08-24 xhr strerror(saved_errno)) == -1) {
126 f224a3fe 2021-08-24 xhr /* we tried it... */
127 f224a3fe 2021-08-24 xhr vlog(LOG_ERR, emsg, ap);
128 f224a3fe 2021-08-24 xhr logit(LOG_ERR, "%s", strerror(saved_errno));
130 f224a3fe 2021-08-24 xhr vlog(LOG_ERR, nfmt, ap);
136 f224a3fe 2021-08-24 xhr errno = saved_errno;
140 f224a3fe 2021-08-24 xhr log_warnx(const char *emsg, ...)
144 f224a3fe 2021-08-24 xhr va_start(ap, emsg);
145 f224a3fe 2021-08-24 xhr vlog(LOG_ERR, emsg, ap);
150 f224a3fe 2021-08-24 xhr log_info(const char *emsg, ...)
154 f224a3fe 2021-08-24 xhr va_start(ap, emsg);
155 f224a3fe 2021-08-24 xhr vlog(LOG_INFO, emsg, ap);
160 f224a3fe 2021-08-24 xhr log_debug(const char *emsg, ...)
164 f224a3fe 2021-08-24 xhr if (verbose) {
165 f224a3fe 2021-08-24 xhr va_start(ap, emsg);
166 f224a3fe 2021-08-24 xhr vlog(LOG_DEBUG, emsg, ap);
172 f224a3fe 2021-08-24 xhr vfatalc(int code, const char *emsg, va_list ap)
174 f224a3fe 2021-08-24 xhr static char s[BUFSIZ];
175 f224a3fe 2021-08-24 xhr const char *sep;
177 f224a3fe 2021-08-24 xhr if (emsg != NULL) {
178 f224a3fe 2021-08-24 xhr (void)vsnprintf(s, sizeof(s), emsg, ap);
181 f224a3fe 2021-08-24 xhr s[0] = '\0';
185 f224a3fe 2021-08-24 xhr logit(LOG_CRIT, "fatal in %s: %s%s%s",
186 f224a3fe 2021-08-24 xhr log_procname, s, sep, strerror(code));
188 f224a3fe 2021-08-24 xhr logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
192 f224a3fe 2021-08-24 xhr fatal(const char *emsg, ...)
196 f224a3fe 2021-08-24 xhr va_start(ap, emsg);
197 f224a3fe 2021-08-24 xhr vfatalc(errno, emsg, ap);
203 f224a3fe 2021-08-24 xhr fatalx(const char *emsg, ...)
207 f224a3fe 2021-08-24 xhr va_start(ap, emsg);
208 f224a3fe 2021-08-24 xhr vfatalc(0, emsg, ap);
214 f224a3fe 2021-08-24 xhr open_twind_logs(void)
216 f224a3fe 2021-08-24 xhr if ((access_fd = open(_PATH_TWIND_ACCESS_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
218 f224a3fe 2021-08-24 xhr fatalx("Cannot open access log: %s", _PATH_TWIND_ACCESS_LOG);
220 f224a3fe 2021-08-24 xhr if ((error_fd = open(_PATH_TWIND_ERROR_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
222 f224a3fe 2021-08-24 xhr fatalx("Cannot open error log: %s", _PATH_TWIND_ACCESS_LOG);
224 a944bd93 2021-08-26 xhr log_debug("Log files open");
228 f224a3fe 2021-08-24 xhr close_twind_logs(void)
230 a944bd93 2021-08-26 xhr log_debug("Closing log file");
231 f224a3fe 2021-08-24 xhr close(access_fd);
232 f224a3fe 2021-08-24 xhr close(error_fd);
236 f224a3fe 2021-08-24 xhr log_access(const struct client_connection *cc, const char *fmt, ...)
238 56b35e42 2021-08-26 xhr char tmstr[29];
239 f224a3fe 2021-08-24 xhr struct tm tm;
242 f224a3fe 2021-08-24 xhr t = time(NULL);
243 f224a3fe 2021-08-24 xhr tm = *localtime(&t);
245 2c012a4d 2021-08-26 xhr if (strftime(tmstr, sizeof(tmstr), "%d/%b/%Y:%H:%M:%S %Z", &tm) == 0) {
246 2c012a4d 2021-08-26 xhr log_warn("strftime couldn't save time string");
250 56b35e42 2021-08-26 xhr user_log(0, "%s - - [%s] %s", cc->client_addr, tmstr, fmt);
254 f224a3fe 2021-08-24 xhr log_error(const struct client_connection *cc, const char *fmt, ...)
256 56b35e42 2021-08-26 xhr char tmstr[29];
257 f224a3fe 2021-08-24 xhr struct tm tm;
260 f224a3fe 2021-08-24 xhr t = time(NULL);
261 f224a3fe 2021-08-24 xhr tm = *localtime(&t);
262 2c012a4d 2021-08-26 xhr if (strftime(tmstr, sizeof(tmstr), "%d/%b/%Y:%H:%M:%S %Z", &tm) == 0) {
263 2c012a4d 2021-08-26 xhr log_warn("strftime couldn't save time string");
267 56b35e42 2021-08-26 xhr user_log(1, "[%s] [error] [client %s] %s", tmstr,
268 56b35e42 2021-08-26 xhr cc->client_addr, fmt);
272 f224a3fe 2021-08-24 xhr user_log(int target, const char *fmt, ...)
275 f224a3fe 2021-08-24 xhr int fd = -1;
277 f224a3fe 2021-08-24 xhr va_start(ap, fmt);
278 f224a3fe 2021-08-24 xhr if (target == 0)
279 f224a3fe 2021-08-24 xhr fd = access_fd;
280 f224a3fe 2021-08-24 xhr else if (target == 1)
281 f224a3fe 2021-08-24 xhr fd = error_fd;
283 f224a3fe 2021-08-24 xhr log_warn("Non-existent user log target");
287 f224a3fe 2021-08-24 xhr vdprintf(fd, fmt, ap);
288 f224a3fe 2021-08-24 xhr dprintf(fd, "\n");