1161764Sobrien/*	$NetBSD: logwtmp.c,v 1.24 2005/06/23 04:20:41 christos 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
39161764Sobrien__RCSID("$NetBSD: logwtmp.c,v 1.24 2005/06/23 04:20:41 christos 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>
54161764Sobrien#include <syslog.h>
55108746Sobrien#include <unistd.h>
56161764Sobrien#ifdef SUPPORT_UTMP
57108746Sobrien#include <utmp.h>
58161764Sobrien#endif
59133936Sobrien#ifdef SUPPORT_UTMPX
60133936Sobrien#include <utmpx.h>
61133936Sobrien#endif
62108746Sobrien#include <util.h>
63108746Sobrien
64108746Sobrien#ifdef KERBEROS5
65108746Sobrien#include <krb5/krb5.h>
66108746Sobrien#endif
67108746Sobrien
6879968Sobrien#include "extern.h"
6979968Sobrien
70161764Sobrien#ifdef SUPPORT_UTMP
7179968Sobrienstatic int fd = -1;
7279968Sobrien
73161764Sobrienvoid
74161764Sobrienftpd_initwtmp(void)
75161764Sobrien{
76161764Sobrien	const char *wf = _PATH_WTMP;
77161764Sobrien	if ((fd = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
78161764Sobrien		syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
79161764Sobrien}
80161764Sobrien
8179968Sobrien/*
8279968Sobrien * Modified version of logwtmp that holds wtmp file open
8379968Sobrien * after first call, for use with ftp (which may chroot
8479968Sobrien * after login, but before logout).
8579968Sobrien */
8679968Sobrienvoid
87133936Sobrienftpd_logwtmp(const char *line, const char *name, const char *host)
8879968Sobrien{
8979968Sobrien	struct utmp ut;
9079968Sobrien	struct stat buf;
9179968Sobrien
9279968Sobrien	if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
9379968Sobrien		return;
9479968Sobrien	if (fstat(fd, &buf) == 0) {
9579968Sobrien		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
9679968Sobrien		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
9779968Sobrien		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
9879968Sobrien		(void)time(&ut.ut_time);
9979968Sobrien		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
10079968Sobrien		    sizeof(struct utmp))
10179968Sobrien			(void)ftruncate(fd, buf.st_size);
10279968Sobrien	}
10379968Sobrien}
104161764Sobrien#endif
105133936Sobrien
106133936Sobrien#ifdef SUPPORT_UTMPX
107161764Sobrienstatic int fdx = -1;
108161764Sobrien
109133936Sobrienvoid
110161764Sobrienftpd_initwtmpx(void)
111133936Sobrien{
112161764Sobrien	const char *wf = _PATH_WTMPX;
113161764Sobrien	if ((fd = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
114161764Sobrien		syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
115161764Sobrien}
116161764Sobrien
117161764Sobrienvoid
118161764Sobrienftpd_logwtmpx(const char *line, const char *name, const char *host,
119161764Sobrien    struct sockinet *haddr, int status, int utx_type)
120161764Sobrien{
121133936Sobrien	struct utmpx ut;
122133936Sobrien	struct stat buf;
123133936Sobrien
124161764Sobrien	if (fdx < 0)
125133936Sobrien		return;
126133936Sobrien	if (fstat(fdx, &buf) == 0) {
127133936Sobrien		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
128133936Sobrien		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
129133936Sobrien		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
130161764Sobrien		if (haddr)
131161764Sobrien			(void)memcpy(&ut.ut_ss, &haddr->si_su, haddr->su_len);
132161764Sobrien		else
133161764Sobrien			(void)memset(&ut.ut_ss, 0, sizeof(ut.ut_ss));
134133936Sobrien		ut.ut_type = utx_type;
135133936Sobrien		if (WIFEXITED(status))
136133936Sobrien			ut.ut_exit.e_exit = (uint16_t)WEXITSTATUS(status);
137133936Sobrien		if (WIFSIGNALED(status))
138133936Sobrien		ut.ut_exit.e_termination = (uint16_t)WTERMSIG(status);
139133936Sobrien		(void)gettimeofday(&ut.ut_tv, NULL);
140133936Sobrien		if(write(fdx, (char *)&ut, sizeof(struct utmpx)) !=
141133936Sobrien		    sizeof(struct utmpx))
142133936Sobrien			(void)ftruncate(fdx, buf.st_size);
143133936Sobrien	}
144133936Sobrien}
145133936Sobrien#endif
146