xref: /freebsd/usr.sbin/pw/pw_log.c (revision d6f907dc)
1d6f907dcSJoerg Wunsch /*-
2d6f907dcSJoerg Wunsch  * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
3d6f907dcSJoerg Wunsch  * 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
9d6f907dcSJoerg Wunsch  *    notice, this list of conditions and the following disclaimer as
10d6f907dcSJoerg Wunsch  *    the first lines of this file unmodified.
11d6f907dcSJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
12d6f907dcSJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
13d6f907dcSJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
14d6f907dcSJoerg Wunsch  * 3. All advertising materials mentioning features or use of this software
15d6f907dcSJoerg Wunsch  *    must display the following acknowledgement:
16d6f907dcSJoerg Wunsch  *	This product includes software developed by David L. Nugent.
17d6f907dcSJoerg Wunsch  * 4. The name of the author may not be used to endorse or promote products
18d6f907dcSJoerg Wunsch  *    derived from this software without specific prior written permission.
19d6f907dcSJoerg Wunsch  *
20d6f907dcSJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
21d6f907dcSJoerg Wunsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22d6f907dcSJoerg Wunsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23d6f907dcSJoerg Wunsch  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
24d6f907dcSJoerg Wunsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25d6f907dcSJoerg Wunsch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26d6f907dcSJoerg Wunsch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27d6f907dcSJoerg Wunsch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28d6f907dcSJoerg Wunsch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29d6f907dcSJoerg Wunsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30d6f907dcSJoerg Wunsch  * SUCH DAMAGE.
31d6f907dcSJoerg Wunsch  *
32d6f907dcSJoerg Wunsch  *	$Id$
33d6f907dcSJoerg Wunsch  */
34d6f907dcSJoerg Wunsch 
35d6f907dcSJoerg Wunsch #include <fcntl.h>
36d6f907dcSJoerg Wunsch 
37d6f907dcSJoerg Wunsch #include "pw.h"
38d6f907dcSJoerg Wunsch 
39d6f907dcSJoerg Wunsch static FILE    *logfile = NULL;
40d6f907dcSJoerg Wunsch 
41d6f907dcSJoerg Wunsch void
42d6f907dcSJoerg Wunsch pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
43d6f907dcSJoerg Wunsch {
44d6f907dcSJoerg Wunsch 	if (cnf->logfile && *cnf->logfile) {
45d6f907dcSJoerg Wunsch 		if (logfile == NULL) {	/* With umask==0 we need to control file access modes on create */
46d6f907dcSJoerg Wunsch 			int             fd = open(cnf->logfile, O_WRONLY | O_CREAT | O_APPEND, 0600);
47d6f907dcSJoerg Wunsch 
48d6f907dcSJoerg Wunsch 			if (fd != -1)
49d6f907dcSJoerg Wunsch 				logfile = fdopen(fd, "a");
50d6f907dcSJoerg Wunsch 		}
51d6f907dcSJoerg Wunsch 		if (logfile != NULL) {
52d6f907dcSJoerg Wunsch 			va_list         argp;
53d6f907dcSJoerg Wunsch 			int             l;
54d6f907dcSJoerg Wunsch 			time_t          now = time(NULL);
55d6f907dcSJoerg Wunsch 			struct tm      *t = localtime(&now);
56d6f907dcSJoerg Wunsch 			char            nfmt[256];
57d6f907dcSJoerg Wunsch 			char           *name;
58d6f907dcSJoerg Wunsch 
59d6f907dcSJoerg Wunsch 			if ((name = getenv("LOGNAME")) == NULL && (name = getenv("USER")) == NULL)
60d6f907dcSJoerg Wunsch 				name = "unknown";
61d6f907dcSJoerg Wunsch 			strftime(nfmt, sizeof nfmt, "%d-%b-%Y %R ", t);
62d6f907dcSJoerg Wunsch 			l = strlen(nfmt);
63d6f907dcSJoerg Wunsch 			sprintf(nfmt + strlen(nfmt), "[%s:%s%s] %s\n", name, Which[which], Modes[mode], fmt);
64d6f907dcSJoerg Wunsch 			va_start(argp, fmt);
65d6f907dcSJoerg Wunsch 			vfprintf(logfile, nfmt, argp);
66d6f907dcSJoerg Wunsch 			va_end(argp);
67d6f907dcSJoerg Wunsch 			fflush(logfile);
68d6f907dcSJoerg Wunsch 		}
69d6f907dcSJoerg Wunsch 	}
70d6f907dcSJoerg Wunsch }
71