xref: /openbsd-src/libexec/ftpd/logutmp.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: logutmp.c,v 1.7 2003/07/07 03:18:11 deraadt Exp $	*/
2 /*
3  * Portions Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  * Portions Copyright (c) 1996, Jason Downs.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/tcp.h>
38 
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <utmp.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <ttyent.h>
46 #include "extern.h"
47 
48 static int fd = -1;
49 static int topslot = -1;
50 
51 /*
52  * Special versions of login()/logout() which hold the utmp file open,
53  * for use with ftpd.
54  */
55 
56 void
57 ftpd_login(struct utmp *ut)
58 {
59 	struct utmp ubuf;
60 
61 	/*
62 	 * First, loop through /etc/ttys, if needed, to initialize the
63 	 * top of the tty slots, since ftpd has no tty.
64 	 */
65 	if (topslot < 0) {
66 		topslot = 0;
67 		while (getttyent() != (struct ttyent *)NULL)
68 			topslot++;
69 	}
70 	if ((topslot < 0) || ((fd < 0) &&
71 	    (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
72 	    	return;
73 
74 	/*
75 	 * Now find a slot that's not in use...
76 	 */
77 	(void)lseek(fd, (off_t)(topslot * sizeof(struct utmp)), SEEK_SET);
78 
79 	while (1) {
80 		if (read(fd, &ubuf, sizeof(struct utmp)) ==
81 		    sizeof(struct utmp)) {
82 			if (!ubuf.ut_name[0]) {
83 				(void)lseek(fd, -(off_t)sizeof(struct utmp),
84 				    SEEK_CUR);
85 				break;
86 			}
87 			topslot++;
88 		} else {
89 			(void)lseek(fd, (off_t)(topslot *
90 			    sizeof(struct utmp)), SEEK_SET);
91 			break;
92 		}
93 	}
94 
95 	(void)write(fd, ut, sizeof(struct utmp));
96 }
97 
98 int
99 ftpd_logout(char *line)
100 {
101 	struct utmp ut;
102 	int rval;
103 
104 	rval = 0;
105 	if (fd < 0)
106 		return(rval);
107 
108 	(void)lseek(fd, 0, SEEK_SET);
109 
110 	while (read(fd, &ut, sizeof(struct utmp)) == sizeof(struct utmp)) {
111 		if (!ut.ut_name[0] ||
112 		    strncmp(ut.ut_line, line, UT_LINESIZE))
113 			continue;
114 		bzero(ut.ut_name, UT_NAMESIZE);
115 		bzero(ut.ut_host, UT_HOSTSIZE);
116 		(void)time(&ut.ut_time);
117 		(void)lseek(fd, -(off_t)sizeof(struct utmp), SEEK_CUR);
118 		(void)write(fd, &ut, sizeof(struct utmp));
119 		rval = 1;
120 	}
121 	return(rval);
122 }
123