xref: /original-bsd/libexec/ftpd/logwtmp.c (revision c8089215)
1 /*
2  * Copyright (c) 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)logwtmp.c	5.7 (Berkeley) 02/25/91";
11 #endif /* not lint */
12 
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <utmp.h>
18 #include <unistd.h>
19 #include <string.h>
20 
21 static int fd = -1;
22 
23 /*
24  * Modified version of logwtmp that holds wtmp file open
25  * after first call, for use with ftp (which may chroot
26  * after login, but before logout).
27  */
28 logwtmp(line, name, host)
29 	char *line, *name, *host;
30 {
31 	struct utmp ut;
32 	struct stat buf;
33 	time_t time();
34 	char *strncpy();
35 
36 	if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
37 		return;
38 	if (fstat(fd, &buf) == 0) {
39 		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
40 		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
41 		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
42 		(void)time(&ut.ut_time);
43 		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
44 		    sizeof(struct utmp))
45 			(void)ftruncate(fd, buf.st_size);
46 	}
47 }
48