logwtmp.c revision 50476
1119679Smbr/*
2119679Smbr * Copyright (c) 1988, 1993
3119679Smbr *	The Regents of the University of California.  All rights reserved.
4119679Smbr *
5119679Smbr * Redistribution and use in source and binary forms, with or without
6119679Smbr * modification, are permitted provided that the following conditions
7119679Smbr * are met:
8119679Smbr * 1. Redistributions of source code must retain the above copyright
9119679Smbr *    notice, this list of conditions and the following disclaimer.
10119679Smbr * 2. Redistributions in binary form must reproduce the above copyright
11119679Smbr *    notice, this list of conditions and the following disclaimer in the
12119679Smbr *    documentation and/or other materials provided with the distribution.
13119679Smbr * 3. All advertising materials mentioning features or use of this software
14119679Smbr *    must display the following acknowledgement:
15119679Smbr *	This product includes software developed by the University of
16119679Smbr *	California, Berkeley and its contributors.
17119679Smbr * 4. Neither the name of the University nor the names of its contributors
18119679Smbr *    may be used to endorse or promote products derived from this software
19119679Smbr *    without specific prior written permission.
20119679Smbr *
21119679Smbr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22119679Smbr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23119679Smbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24119679Smbr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25119679Smbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26119679Smbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27119679Smbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28119679Smbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29119679Smbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30119679Smbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31119679Smbr * SUCH DAMAGE.
32119679Smbr */
33119679Smbr
34119679Smbr#ifndef lint
35119679Smbr#if 0
36119679Smbrstatic char sccsid[] = "@(#)logwtmp.c	8.1 (Berkeley) 6/4/93";
37119679Smbr#endif
38119679Smbrstatic const char rcsid[] =
39119679Smbr  "$FreeBSD: head/libexec/ftpd/logwtmp.c 50476 1999-08-28 00:22:10Z peter $";
40119679Smbr#endif /* not lint */
41119679Smbr
42119679Smbr#include <sys/types.h>
43119679Smbr#include <sys/stat.h>
44119679Smbr#include <netinet/in.h>
45119679Smbr#include <arpa/inet.h>
46119679Smbr
47119679Smbr#include <fcntl.h>
48119679Smbr#include <time.h>
49119679Smbr#include <netdb.h>
50119679Smbr#include <utmp.h>
51119679Smbr#include <unistd.h>
52119679Smbr#include <stdio.h>
53119679Smbr#include <string.h>
54119679Smbr#include "extern.h"
55119679Smbr
56119679Smbrstatic int fd = -1;
57119679Smbr
58119679Smbr/*
59119679Smbr * Modified version of logwtmp that holds wtmp file open
60119679Smbr * after first call, for use with ftp (which may chroot
61119679Smbr * after login, but before logout).
62119679Smbr */
63119679Smbrvoid
64119679Smbrftpd_logwtmp(line, name, host)
65119679Smbr	char *line, *name, *host;
66119679Smbr{
67119679Smbr	struct utmp ut;
68119679Smbr	struct stat buf;
69119679Smbr
70119679Smbr	if (strlen(host) > UT_HOSTSIZE) {
71119679Smbr		struct hostent *hp = gethostbyname(host);
72119679Smbr
73119679Smbr		if (hp != NULL) {
74119679Smbr			struct in_addr in;
75119679Smbr
76119679Smbr			memmove(&in, hp->h_addr, sizeof(in));
77119679Smbr			host = inet_ntoa(in);
78119679Smbr		} else
79119679Smbr			host = "invalid hostname";
80119679Smbr	}
81119679Smbr
82119679Smbr	if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
83119679Smbr		return;
84119679Smbr	if (fstat(fd, &buf) == 0) {
85119679Smbr		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
86119679Smbr		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
87119679Smbr		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
88119679Smbr		(void)time(&ut.ut_time);
89119679Smbr		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
90119679Smbr		    sizeof(struct utmp))
91119679Smbr			(void)ftruncate(fd, buf.st_size);
92119679Smbr	}
93119679Smbr}
94119679Smbr