logwtmp.c revision 133936
1133936Sobrien/*	$NetBSD: logwtmp.c,v 1.22 2004-08-09 12:56:48 lukem Exp $	*/
279968Sobrien
379968Sobrien/*
479968Sobrien * Copyright (c) 1988, 1993
579968Sobrien *	The Regents of the University of California.  All rights reserved.
679968Sobrien *
779968Sobrien * Redistribution and use in source and binary forms, with or without
879968Sobrien * modification, are permitted provided that the following conditions
979968Sobrien * are met:
1079968Sobrien * 1. Redistributions of source code must retain the above copyright
1179968Sobrien *    notice, this list of conditions and the following disclaimer.
1279968Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1379968Sobrien *    notice, this list of conditions and the following disclaimer in the
1479968Sobrien *    documentation and/or other materials provided with the distribution.
15133936Sobrien * 3. Neither the name of the University nor the names of its contributors
1679968Sobrien *    may be used to endorse or promote products derived from this software
1779968Sobrien *    without specific prior written permission.
1879968Sobrien *
1979968Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2079968Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2179968Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2279968Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2379968Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2479968Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2579968Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2679968Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2779968Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2879968Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2979968Sobrien * SUCH DAMAGE.
3079968Sobrien *
3179968Sobrien */
3279968Sobrien
3379968Sobrien
34108746Sobrien#include <sys/cdefs.h>
35108746Sobrien#ifndef lint
36108746Sobrien#if 0
37108746Sobrienstatic char sccsid[] = "@(#)logwtmp.c	8.1 (Berkeley) 6/4/93";
38108746Sobrien#else
39133936Sobrien__RCSID("$NetBSD: logwtmp.c,v 1.22 2004-08-09 12:56:48 lukem Exp $");
40108746Sobrien#endif
41108746Sobrien#endif /* not lint */
42108746Sobrien
43108746Sobrien#include <sys/types.h>
44108746Sobrien#include <sys/param.h>
45108746Sobrien#include <sys/time.h>
46108746Sobrien#include <sys/stat.h>
47133936Sobrien#include <sys/wait.h>
48108746Sobrien
49108746Sobrien#include <fcntl.h>
50108746Sobrien#include <signal.h>
51108746Sobrien#include <stdio.h>
52108746Sobrien#include <string.h>
53108746Sobrien#include <time.h>
54108746Sobrien#include <unistd.h>
55108746Sobrien#include <utmp.h>
56133936Sobrien#ifdef SUPPORT_UTMPX
57133936Sobrien#include <utmpx.h>
58133936Sobrien#endif
59108746Sobrien#include <util.h>
60108746Sobrien
61108746Sobrien#ifdef KERBEROS5
62108746Sobrien#include <krb5/krb5.h>
63108746Sobrien#endif
64108746Sobrien
6579968Sobrien#include "extern.h"
6679968Sobrien
6779968Sobrienstatic int fd = -1;
68133936Sobrien#ifdef SUPPORT_UTMPX
69133936Sobrienstatic int fdx = -1;
70133936Sobrien#endif
7179968Sobrien
7279968Sobrien/*
7379968Sobrien * Modified version of logwtmp that holds wtmp file open
7479968Sobrien * after first call, for use with ftp (which may chroot
7579968Sobrien * after login, but before logout).
7679968Sobrien */
7779968Sobrienvoid
78133936Sobrienftpd_logwtmp(const char *line, const char *name, const char *host)
7979968Sobrien{
8079968Sobrien	struct utmp ut;
8179968Sobrien	struct stat buf;
8279968Sobrien
8379968Sobrien	if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
8479968Sobrien		return;
8579968Sobrien	if (fstat(fd, &buf) == 0) {
8679968Sobrien		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
8779968Sobrien		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
8879968Sobrien		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
8979968Sobrien		(void)time(&ut.ut_time);
9079968Sobrien		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
9179968Sobrien		    sizeof(struct utmp))
9279968Sobrien			(void)ftruncate(fd, buf.st_size);
9379968Sobrien	}
9479968Sobrien}
95133936Sobrien
96133936Sobrien#ifdef SUPPORT_UTMPX
97133936Sobrienvoid
98133936Sobrienftpd_logwtmpx(const char *line, const char *name, const char *host, int status, int utx_type)
99133936Sobrien{
100133936Sobrien	struct utmpx ut;
101133936Sobrien	struct stat buf;
102133936Sobrien
103133936Sobrien	if (fdx < 0 && (fdx = open(_PATH_WTMPX, O_WRONLY|O_APPEND, 0)) < 0)
104133936Sobrien		return;
105133936Sobrien	if (fstat(fdx, &buf) == 0) {
106133936Sobrien		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
107133936Sobrien		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
108133936Sobrien		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
109133936Sobrien		ut.ut_type = utx_type;
110133936Sobrien		if (WIFEXITED(status))
111133936Sobrien			ut.ut_exit.e_exit = (uint16_t)WEXITSTATUS(status);
112133936Sobrien		if (WIFSIGNALED(status))
113133936Sobrien		ut.ut_exit.e_termination = (uint16_t)WTERMSIG(status);
114133936Sobrien		(void)gettimeofday(&ut.ut_tv, NULL);
115133936Sobrien		if(write(fdx, (char *)&ut, sizeof(struct utmpx)) !=
116133936Sobrien		    sizeof(struct utmpx))
117133936Sobrien			(void)ftruncate(fdx, buf.st_size);
118133936Sobrien	}
119133936Sobrien}
120133936Sobrien#endif
121