1 /* $OpenBSD: logutmp.c,v 1.10 2005/05/27 04:27:27 millert 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/time.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
39
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <utmp.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <ttyent.h>
47 #include "extern.h"
48
49 static int fd = -1;
50 static int topslot = -1;
51
52 /*
53 * Special versions of login()/logout() which hold the utmp file open,
54 * for use with ftpd.
55 */
56
57 void
ftpd_login(struct utmp * ut)58 ftpd_login(struct utmp *ut)
59 {
60 struct utmp ubuf;
61
62 /*
63 * First, loop through /etc/ttys, if needed, to initialize the
64 * top of the tty slots, since ftpd has no tty.
65 */
66 if (topslot < 0) {
67 topslot = 0;
68 while (getttyent() != (struct ttyent *)NULL)
69 topslot++;
70 }
71 if ((topslot < 0) || ((fd < 0) &&
72 (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
73 return;
74
75 /*
76 * Now find a slot that's not in use...
77 */
78 (void)lseek(fd, (off_t)(topslot * sizeof(struct utmp)), SEEK_SET);
79
80 while (1) {
81 if (read(fd, &ubuf, sizeof(struct utmp)) ==
82 sizeof(struct utmp)) {
83 if (!ubuf.ut_name[0]) {
84 (void)lseek(fd, -(off_t)sizeof(struct utmp),
85 SEEK_CUR);
86 break;
87 }
88 topslot++;
89 } else {
90 (void)lseek(fd, (off_t)(topslot *
91 sizeof(struct utmp)), SEEK_SET);
92 break;
93 }
94 }
95
96 (void)write(fd, ut, sizeof(struct utmp));
97 }
98
99 int
ftpd_logout(char * line)100 ftpd_logout(char *line)
101 {
102 struct timeval tv;
103 struct utmp ut;
104 int rval;
105
106 rval = 0;
107 if (fd < 0)
108 return(rval);
109
110 (void)lseek(fd, 0, SEEK_SET);
111
112 while (read(fd, &ut, sizeof(struct utmp)) == sizeof(struct utmp)) {
113 if (!ut.ut_name[0] ||
114 strncmp(ut.ut_line, line, UT_LINESIZE))
115 continue;
116 bzero(ut.ut_name, UT_NAMESIZE);
117 bzero(ut.ut_host, UT_HOSTSIZE);
118 gettimeofday(&tv, NULL);
119 ut.ut_time = tv.tv_sec;
120 (void)lseek(fd, -(off_t)sizeof(struct utmp), SEEK_CUR);
121 (void)write(fd, &ut, sizeof(struct utmp));
122 rval = 1;
123 }
124 return(rval);
125 }
126