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