2 * SPDX-License-Identifier: BSD-2-Clause
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static FILE *logfile = NULL;
40 pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
44 const char *cp, *name;
47 char nfmt[256], sname[32];
49 if (cnf->logfile == NULL || cnf->logfile[0] == '\0') {
53 if (logfile == NULL) {
54 /* With umask==0 we need to control file access modes on create */
55 fd = open(cnf->logfile, O_WRONLY | O_CREAT | O_APPEND, 0600);
59 logfile = fdopen(fd, "a");
60 if (logfile == NULL) {
65 if ((name = getenv("LOGNAME")) == NULL &&
66 (name = getenv("USER")) == NULL) {
67 strcpy(sname, "unknown");
70 * Since "name" will be embedded in a printf-like format,
71 * we must sanitize it:
73 * Limit its length so other information in the message
76 * Squeeze out embedded whitespace for the benefit of
79 * Escape embedded % characters with another %
81 for (i = 0, cp = name;
82 *cp != '\0' && i < (int)sizeof(sname) - 1; cp++) {
84 if (i < (int)sizeof(sname) - 2) {
90 } else if (!isspace(*cp)) {
92 } /* else do nothing */
95 strcpy(sname, "unknown");
102 /* ISO 8601 International Standard Date format */
103 strftime(nfmt, sizeof nfmt, "%Y-%m-%d %T ", t);
104 rlen = sizeof(nfmt) - strlen(nfmt);
105 if (rlen <= 0 || snprintf(nfmt + strlen(nfmt), rlen,
106 "[%s:%s%s] %s\n", sname, Which[which], Modes[mode],
108 warnx("log format overflow, user name=%s", sname);
111 vfprintf(logfile, nfmt, argp);