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