120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD$";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3220253Sjoerg#include <fcntl.h>
3320253Sjoerg
3420253Sjoerg#include "pw.h"
3520253Sjoerg
3620253Sjoergstatic FILE    *logfile = NULL;
3720253Sjoerg
3820253Sjoergvoid
3920253Sjoergpw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
4020253Sjoerg{
4120253Sjoerg	if (cnf->logfile && *cnf->logfile) {
4220253Sjoerg		if (logfile == NULL) {	/* With umask==0 we need to control file access modes on create */
4320253Sjoerg			int             fd = open(cnf->logfile, O_WRONLY | O_CREAT | O_APPEND, 0600);
4420253Sjoerg
4520253Sjoerg			if (fd != -1)
4620253Sjoerg				logfile = fdopen(fd, "a");
4720253Sjoerg		}
4820253Sjoerg		if (logfile != NULL) {
4920253Sjoerg			va_list         argp;
5020253Sjoerg			int             l;
5120253Sjoerg			time_t          now = time(NULL);
5220253Sjoerg			struct tm      *t = localtime(&now);
5320253Sjoerg			char            nfmt[256];
5420253Sjoerg			char           *name;
5520253Sjoerg
5620253Sjoerg			if ((name = getenv("LOGNAME")) == NULL && (name = getenv("USER")) == NULL)
5720253Sjoerg				name = "unknown";
5861957Sache			/* ISO 8601 International Standard Date format */
5961957Sache			strftime(nfmt, sizeof nfmt, "%Y-%m-%d %T ", t);
6020253Sjoerg			l = strlen(nfmt);
6120253Sjoerg			sprintf(nfmt + strlen(nfmt), "[%s:%s%s] %s\n", name, Which[which], Modes[mode], fmt);
6220253Sjoerg			va_start(argp, fmt);
6320253Sjoerg			vfprintf(logfile, nfmt, argp);
6420253Sjoerg			va_end(argp);
6520253Sjoerg			fflush(logfile);
6620253Sjoerg		}
6720253Sjoerg	}
6820253Sjoerg}
69