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