179968Sobrien/*
279968Sobrien * Portions Copyright (c) 1988, 1993
379968Sobrien *	The Regents of the University of California.  All rights reserved.
479968Sobrien *
579968Sobrien * Redistribution and use in source and binary forms, with or without
679968Sobrien * modification, are permitted provided that the following conditions
779968Sobrien * are met:
879968Sobrien * 1. Redistributions of source code must retain the above copyright
979968Sobrien *    notice, this list of conditions and the following disclaimer.
1079968Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1179968Sobrien *    notice, this list of conditions and the following disclaimer in the
1279968Sobrien *    documentation and/or other materials provided with the distribution.
13133936Sobrien * 3. Neither the name of the University nor the names of its contributors
1479968Sobrien *    may be used to endorse or promote products derived from this software
1579968Sobrien *    without specific prior written permission.
1679968Sobrien *
1779968Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1879968Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1979968Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2079968Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2179968Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2279968Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2379968Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2479968Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2579968Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2679968Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2779968Sobrien * SUCH DAMAGE.
2879968Sobrien */
2979968Sobrien
30133936Sobrien/*
31133936Sobrien * Portions Copyright (c) 1996, Jason Downs.  All rights reserved.
32133936Sobrien *
33133936Sobrien * Redistribution and use in source and binary forms, with or without
34133936Sobrien * modification, are permitted provided that the following conditions
35133936Sobrien * are met:
36133936Sobrien * 1. Redistributions of source code must retain the above copyright
37133936Sobrien *    notice, this list of conditions and the following disclaimer.
38133936Sobrien * 2. Redistributions in binary form must reproduce the above copyright
39133936Sobrien *    notice, this list of conditions and the following disclaimer in the
40133936Sobrien *    documentation and/or other materials provided with the distribution.
41133936Sobrien *
42133936Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
43133936Sobrien * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
44133936Sobrien * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45133936Sobrien * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
46133936Sobrien * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47133936Sobrien * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48133936Sobrien * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49133936Sobrien * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50133936Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51133936Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52133936Sobrien * SUCH DAMAGE.
53133936Sobrien */
54133936Sobrien
55108746Sobrien#include <sys/types.h>
56133936Sobrien#include <sys/param.h>
5779968Sobrien
58108746Sobrien#include <fcntl.h>
59108746Sobrien#include <stdio.h>
60108746Sobrien#include <stdlib.h>
61108746Sobrien#include <string.h>
62108746Sobrien#include <ttyent.h>
63108746Sobrien#include <unistd.h>
64108746Sobrien#include <utmp.h>
65133936Sobrien#ifdef SUPPORT_UTMPX
66133936Sobrien#include <utmpx.h>
67133936Sobrien#endif
68108746Sobrien#include <util.h>
69108746Sobrien
70133936Sobrien#include "extern.h"
71133936Sobrien
7279968Sobrientypedef struct utmp UTMP;
7379968Sobrien
7479968Sobrienstatic int fd = -1;
7579968Sobrienstatic int topslot = -1;
7679968Sobrien
7779968Sobrien/*
7879968Sobrien * Special versions of login()/logout() which hold the utmp file open,
7979968Sobrien * for use with ftpd.
8079968Sobrien */
8179968Sobrien
8279968Sobrienvoid
83133936Sobrienftpd_login(const struct utmp *ut)
8479968Sobrien{
8579968Sobrien	UTMP ubuf;
8679968Sobrien
8779968Sobrien	/*
8879968Sobrien	 * First, loop through /etc/ttys, if needed, to initialize the
8979968Sobrien	 * top of the tty slots, since ftpd has no tty.
9079968Sobrien	 */
9179968Sobrien	if (topslot < 0) {
9279968Sobrien		topslot = 0;
9379968Sobrien		while (getttyent() != (struct ttyent *)NULL)
9479968Sobrien			topslot++;
9579968Sobrien	}
9679968Sobrien	if ((topslot < 0) || ((fd < 0)
9779968Sobrien	    && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
98108746Sobrien		return;
9979968Sobrien
10079968Sobrien	/*
10179968Sobrien	 * Now find a slot that's not in use...
10279968Sobrien	 */
10379968Sobrien	(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
10479968Sobrien
10579968Sobrien	while (1) {
10679968Sobrien		if (read(fd, &ubuf, sizeof(UTMP)) == sizeof(UTMP)) {
10779968Sobrien			if (!ubuf.ut_name[0]) {
10879968Sobrien				(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
10979968Sobrien				break;
11079968Sobrien			}
11179968Sobrien			topslot++;
11279968Sobrien		} else {
11379968Sobrien			(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)),
11479968Sobrien			    SEEK_SET);
11579968Sobrien			break;
11679968Sobrien		}
11779968Sobrien	}
11879968Sobrien
11979968Sobrien	(void)write(fd, ut, sizeof(UTMP));
12079968Sobrien}
12179968Sobrien
12279968Sobrienint
123133936Sobrienftpd_logout(const char *line)
12479968Sobrien{
12579968Sobrien	UTMP ut;
12679968Sobrien	int rval;
12779968Sobrien
12879968Sobrien	rval = 0;
12979968Sobrien	if (fd < 0)
13079968Sobrien		return(rval);
13179968Sobrien
13279968Sobrien	(void)lseek(fd, 0, SEEK_SET);
13379968Sobrien
13479968Sobrien	while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
13579968Sobrien		if (!ut.ut_name[0]
13679968Sobrien		    || strncmp(ut.ut_line, line, UT_LINESIZE))
13779968Sobrien			continue;
13879968Sobrien		memset(ut.ut_name, 0, UT_NAMESIZE);
13979968Sobrien		memset(ut.ut_host, 0, UT_HOSTSIZE);
14079968Sobrien		(void)time(&ut.ut_time);
14179968Sobrien		(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
14279968Sobrien		(void)write(fd, &ut, sizeof(UTMP));
14379968Sobrien		rval = 1;
14479968Sobrien	}
14579968Sobrien	return(rval);
14679968Sobrien}
147133936Sobrien
148133936Sobrien#ifdef SUPPORT_UTMPX
149133936Sobrien/*
150133936Sobrien * special version of loginx which updates utmpx only.
151133936Sobrien */
152133936Sobrienvoid
153133936Sobrienftpd_loginx(const struct utmpx *ut)
154133936Sobrien{
155133936Sobrien	(void)pututxline(ut);
156133936Sobrien}
157161764Sobrien
158161764Sobrienint
159161764Sobrienftpd_logoutx(const char *line, int status, int mode)
160161764Sobrien{
161161764Sobrien	return logoutx(line, status, mode);
162161764Sobrien}
163133936Sobrien#endif
164