1d6f907dcSJoerg Wunsch /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4ad7cf975SJoerg Wunsch * Copyright (C) 1996
5ad7cf975SJoerg Wunsch * David L. Nugent. All rights reserved.
6d6f907dcSJoerg Wunsch *
7d6f907dcSJoerg Wunsch * Redistribution and use in source and binary forms, with or without
8d6f907dcSJoerg Wunsch * modification, are permitted provided that the following conditions
9d6f907dcSJoerg Wunsch * are met:
10d6f907dcSJoerg Wunsch * 1. Redistributions of source code must retain the above copyright
11ad7cf975SJoerg Wunsch * notice, this list of conditions and the following disclaimer.
12d6f907dcSJoerg Wunsch * 2. Redistributions in binary form must reproduce the above copyright
13d6f907dcSJoerg Wunsch * notice, this list of conditions and the following disclaimer in the
14d6f907dcSJoerg Wunsch * documentation and/or other materials provided with the distribution.
15d6f907dcSJoerg Wunsch *
16ad7cf975SJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17d6f907dcSJoerg Wunsch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d6f907dcSJoerg Wunsch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ad7cf975SJoerg Wunsch * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20d6f907dcSJoerg Wunsch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d6f907dcSJoerg Wunsch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d6f907dcSJoerg Wunsch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d6f907dcSJoerg Wunsch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d6f907dcSJoerg Wunsch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d6f907dcSJoerg Wunsch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d6f907dcSJoerg Wunsch * SUCH DAMAGE.
27d6f907dcSJoerg Wunsch */
28d6f907dcSJoerg Wunsch
29660d4fe2SDon Lewis #include <ctype.h>
30660d4fe2SDon Lewis #include <err.h>
31d6f907dcSJoerg Wunsch #include <fcntl.h>
32bcbdb01eSBaptiste Daroussin #include <string.h>
33bcbdb01eSBaptiste Daroussin #include <stdarg.h>
34d6f907dcSJoerg Wunsch
35d6f907dcSJoerg Wunsch #include "pw.h"
36d6f907dcSJoerg Wunsch
37d6f907dcSJoerg Wunsch static FILE *logfile = NULL;
38d6f907dcSJoerg Wunsch
39d6f907dcSJoerg Wunsch void
pw_log(struct userconf * cnf,int mode,int which,char const * fmt,...)40d6f907dcSJoerg Wunsch pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
41d6f907dcSJoerg Wunsch {
42d6f907dcSJoerg Wunsch va_list argp;
43660d4fe2SDon Lewis time_t now;
44660d4fe2SDon Lewis const char *cp, *name;
45660d4fe2SDon Lewis struct tm *t;
46660d4fe2SDon Lewis int fd, i, rlen;
47660d4fe2SDon Lewis char nfmt[256], sname[32];
48d6f907dcSJoerg Wunsch
49660d4fe2SDon Lewis if (cnf->logfile == NULL || cnf->logfile[0] == '\0') {
50660d4fe2SDon Lewis return;
51660d4fe2SDon Lewis }
52660d4fe2SDon Lewis
53660d4fe2SDon Lewis if (logfile == NULL) {
54660d4fe2SDon Lewis /* With umask==0 we need to control file access modes on create */
55660d4fe2SDon Lewis fd = open(cnf->logfile, O_WRONLY | O_CREAT | O_APPEND, 0600);
56660d4fe2SDon Lewis if (fd == -1) {
57660d4fe2SDon Lewis return;
58660d4fe2SDon Lewis }
59660d4fe2SDon Lewis logfile = fdopen(fd, "a");
60660d4fe2SDon Lewis if (logfile == NULL) {
61660d4fe2SDon Lewis return;
62660d4fe2SDon Lewis }
63660d4fe2SDon Lewis }
64660d4fe2SDon Lewis
65660d4fe2SDon Lewis if ((name = getenv("LOGNAME")) == NULL &&
66660d4fe2SDon Lewis (name = getenv("USER")) == NULL) {
67660d4fe2SDon Lewis strcpy(sname, "unknown");
68660d4fe2SDon Lewis } else {
69660d4fe2SDon Lewis /*
70660d4fe2SDon Lewis * Since "name" will be embedded in a printf-like format,
71660d4fe2SDon Lewis * we must sanitize it:
72660d4fe2SDon Lewis *
73660d4fe2SDon Lewis * Limit its length so other information in the message
74660d4fe2SDon Lewis * is not truncated
75660d4fe2SDon Lewis *
76660d4fe2SDon Lewis * Squeeze out embedded whitespace for the benefit of
77660d4fe2SDon Lewis * log file parsers
78660d4fe2SDon Lewis *
79660d4fe2SDon Lewis * Escape embedded % characters with another %
80660d4fe2SDon Lewis */
81660d4fe2SDon Lewis for (i = 0, cp = name;
82660d4fe2SDon Lewis *cp != '\0' && i < (int)sizeof(sname) - 1; cp++) {
83660d4fe2SDon Lewis if (*cp == '%') {
84660d4fe2SDon Lewis if (i < (int)sizeof(sname) - 2) {
85660d4fe2SDon Lewis sname[i++] = '%';
86660d4fe2SDon Lewis sname[i++] = '%';
87660d4fe2SDon Lewis } else {
88660d4fe2SDon Lewis break;
89660d4fe2SDon Lewis }
90660d4fe2SDon Lewis } else if (!isspace(*cp)) {
91660d4fe2SDon Lewis sname[i++] = *cp;
92660d4fe2SDon Lewis } /* else do nothing */
93660d4fe2SDon Lewis }
94660d4fe2SDon Lewis if (i == 0) {
95660d4fe2SDon Lewis strcpy(sname, "unknown");
96660d4fe2SDon Lewis } else {
97660d4fe2SDon Lewis sname[i] = '\0';
98660d4fe2SDon Lewis }
99660d4fe2SDon Lewis }
100660d4fe2SDon Lewis now = time(NULL);
101660d4fe2SDon Lewis t = localtime(&now);
102e7161f36SAndrey A. Chernov /* ISO 8601 International Standard Date format */
103e7161f36SAndrey A. Chernov strftime(nfmt, sizeof nfmt, "%Y-%m-%d %T ", t);
104660d4fe2SDon Lewis rlen = sizeof(nfmt) - strlen(nfmt);
105660d4fe2SDon Lewis if (rlen <= 0 || snprintf(nfmt + strlen(nfmt), rlen,
106660d4fe2SDon Lewis "[%s:%s%s] %s\n", sname, Which[which], Modes[mode],
107660d4fe2SDon Lewis fmt) >= rlen) {
108660d4fe2SDon Lewis warnx("log format overflow, user name=%s", sname);
109660d4fe2SDon Lewis } else {
110d6f907dcSJoerg Wunsch va_start(argp, fmt);
111d6f907dcSJoerg Wunsch vfprintf(logfile, nfmt, argp);
112d6f907dcSJoerg Wunsch va_end(argp);
113d6f907dcSJoerg Wunsch fflush(logfile);
114d6f907dcSJoerg Wunsch }
115d6f907dcSJoerg Wunsch }
116