logutmp.c revision 1.3
1/*	$OpenBSD: logutmp.c,v 1.3 1999/12/10 10:41:03 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. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/types.h>
37
38#include <fcntl.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <utmp.h>
42#include <stdio.h>
43#include <string.h>
44#include <ttyent.h>
45
46typedef struct utmp UTMP;
47
48static int fd = -1;
49static int topslot = -1;
50
51/*
52 * Special versions of login()/logout() which hold the utmp file open,
53 * for use with ftpd.
54 */
55
56void
57login(ut)
58	UTMP *ut;
59{
60	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(UTMP)), SEEK_SET);
79
80	while (1) {
81		if (read(fd, &ubuf, sizeof(UTMP)) == sizeof(UTMP)) {
82			if (!ubuf.ut_name[0]) {
83				(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
84				break;
85			}
86			topslot++;
87		} else {
88			(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
89			break;
90		}
91	}
92
93	(void)write(fd, ut, sizeof(UTMP));
94}
95
96int
97logout(line)
98	register char *line;
99{
100	UTMP ut;
101	int rval;
102
103	rval = 0;
104	if (fd < 0)
105		return(rval);
106
107	(void)lseek(fd, 0, SEEK_SET);
108
109	while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
110		if (!ut.ut_name[0] ||
111		    strncmp(ut.ut_line, line, UT_LINESIZE))
112			continue;
113		bzero(ut.ut_name, UT_NAMESIZE);
114		bzero(ut.ut_host, UT_HOSTSIZE);
115		(void)time(&ut.ut_time);
116		(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
117		(void)write(fd, &ut, sizeof(UTMP));
118		rval = 1;
119	}
120	return(rval);
121}
122