]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_log.c
Recommit everything, add chpass, improve history (except for a few files that git...
[pw-darwin.git] / pw / pw_log.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (C) 1996
5 * David L. Nugent. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #ifndef lint
30 static const char rcsid[] =
31 "$FreeBSD$";
32 #endif /* not lint */
33
34 #include <ctype.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <time.h>
40
41 #include "pw.h"
42
43 static FILE *logfile = NULL;
44
45 void
46 pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
47 {
48 va_list argp;
49 time_t now;
50 const char *cp, *name;
51 struct tm *t;
52 int fd, i, rlen;
53 char nfmt[256], sname[32];
54
55 if (cnf->logfile == NULL || cnf->logfile[0] == '\0') {
56 return;
57 }
58
59 if (logfile == NULL) {
60 /* With umask==0 we need to control file access modes on create */
61 fd = open(cnf->logfile, O_WRONLY | O_CREAT | O_APPEND, 0600);
62 if (fd == -1) {
63 return;
64 }
65 logfile = fdopen(fd, "a");
66 if (logfile == NULL) {
67 return;
68 }
69 }
70
71 if ((name = getenv("LOGNAME")) == NULL &&
72 (name = getenv("USER")) == NULL) {
73 strcpy(sname, "unknown");
74 } else {
75 /*
76 * Since "name" will be embedded in a printf-like format,
77 * we must sanitize it:
78 *
79 * Limit its length so other information in the message
80 * is not truncated
81 *
82 * Squeeze out embedded whitespace for the benefit of
83 * log file parsers
84 *
85 * Escape embedded % characters with another %
86 */
87 for (i = 0, cp = name;
88 *cp != '\0' && i < (int)sizeof(sname) - 1; cp++) {
89 if (*cp == '%') {
90 if (i < (int)sizeof(sname) - 2) {
91 sname[i++] = '%';
92 sname[i++] = '%';
93 } else {
94 break;
95 }
96 } else if (!isspace(*cp)) {
97 sname[i++] = *cp;
98 } /* else do nothing */
99 }
100 if (i == 0) {
101 strcpy(sname, "unknown");
102 } else {
103 sname[i] = '\0';
104 }
105 }
106 now = time(NULL);
107 t = localtime(&now);
108 /* ISO 8601 International Standard Date format */
109 strftime(nfmt, sizeof nfmt, "%Y-%m-%d %T ", t);
110 rlen = sizeof(nfmt) - strlen(nfmt);
111 if (rlen <= 0 || snprintf(nfmt + strlen(nfmt), rlen,
112 "[%s:%s%s] %s\n", sname, Which[which], Modes[mode],
113 fmt) >= rlen) {
114 warnx("log format overflow, user name=%s", sname);
115 } else {
116 va_start(argp, fmt);
117 vfprintf(logfile, nfmt, argp);
118 va_end(argp);
119 fflush(logfile);
120 }
121 }