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