xref: /original-bsd/libexec/ftpd/logwtmp.c (revision 9fa17c5f)
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.6 (Berkeley) 06/01/90";
11 #endif /* not lint */
12 
13 #include <sys/types.h>
14 #include <sys/file.h>
15 #include <sys/time.h>
16 #include <sys/stat.h>
17 #include <utmp.h>
18 
19 static int fd = -1;
20 
21 /*
22  * Modified version of logwtmp that holds wtmp file open
23  * after first call, for use with ftp (which may chroot
24  * after login, but before logout).
25  */
26 logwtmp(line, name, host)
27 	char *line, *name, *host;
28 {
29 	struct utmp ut;
30 	struct stat buf;
31 	time_t time();
32 	char *strncpy();
33 
34 	if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
35 		return;
36 	if (fstat(fd, &buf) == 0) {
37 		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
38 		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
39 		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
40 		(void)time(&ut.ut_time);
41 		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
42 		    sizeof(struct utmp))
43 			(void)ftruncate(fd, buf.st_size);
44 	}
45 }
46