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