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