logwtmp.c revision 59118
1/* logwtmp.c: Put an entry in the wtmp file. 2 3%%% portions-copyright-cmetz-96 4Portions of this software are Copyright 1996-1998 by Craig Metz, All Rights 5Reserved. The Inner Net License Version 2 applies to these portions of 6the software. 7You should have received a copy of the license with this software. If 8you didn't get a copy, you may request one from <license@inner.net>. 9 10Portions of this software are Copyright 1995 by Randall Atkinson and Dan 11McDonald, All Rights Reserved. All Rights under this copyright are assigned 12to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and 13License Agreement applies to this software. 14 15 History: 16 17 Modified by cmetz for OPIE 2.32. Don't leave line=NULL, skip 18 past /dev/ in line. Fill in ut_host on systems with UTMPX and 19 ut_host. 20 Modified by cmetz for OPIE 2.31. Move wtmp log functions here, to 21 improve portability. Added DISABLE_WTMP. 22 Modified by cmetz for OPIE 2.22. Call gettimeofday() properly. 23 Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al. 24 Ifdef around some headers. Added file close hook. 25 Modified at NRL for OPIE 2.1. Set process type for HPUX. 26 Modified at NRL for OPIE 2.0. 27 Originally from BSD. 28*/ 29/* 30 * Copyright (c) 1988 The Regents of the University of California. 31 * 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 * 3. All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * This product includes software developed by the University of 44 * California, Berkeley and its contributors. 45 * 4. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 */ 62 63#include "opie_cfg.h" 64 65#include <sys/types.h> 66#if HAVE_SYS_TIME_H 67#include <sys/time.h> 68#endif /* HAVE_SYS_TIME_H */ 69#include <sys/stat.h> 70#include <fcntl.h> 71#include <utmp.h> 72#if HAVE_UNISTD_H 73#include <unistd.h> 74#endif /* HAVE_UNISTD_H */ 75#if HAVE_STRING_H 76#include <string.h> 77#endif /* HAVE_STRING_H */ 78 79#include "opie.h" 80 81static int fd = -1; 82 83#if DOUTMPX 84static int fdx = -1; 85#include <utmpx.h> 86#endif /* DOUTMPX */ 87 88#ifndef _PATH_WTMP 89#ifdef WTMP_FILE 90#define _PATH_WTMP WTMP_FILE 91#else /* WTMP_FILE */ 92#ifdef PATH_WTMP_AC 93#define _PATH_WTMP PATH_WTMP_AC 94#endif /* PATH_WTMP_AC */ 95#endif /* WTMP_FILE */ 96#endif /* _PATH_WTMP */ 97 98#ifndef _PATH_WTMPX 99#ifdef WTMPX_FILE 100#define _PATH_WTMPX WTMPX_FILE 101#else /* WTMPX_FILE */ 102#ifdef PATH_WTMPX_AC 103#define _PATH_WTMPX PATH_WTMPX_AC 104#endif /* PATH_WTMPX_AC */ 105#endif /* WTMPX_FILE */ 106#endif /* _PATH_WTMPX */ 107 108/* 109 * Modified version of logwtmp that holds wtmp file open 110 * after first call, for use with ftp (which may chroot 111 * after login, but before logout). 112 */ 113VOIDRET opielogwtmp FUNCTION((line, name, host), char *line AND char *name AND char *host) 114{ 115#if !DISABLE_WTMP 116 struct utmp ut; 117 118#if DOUTMPX && defined(_PATH_WTMPX) 119 struct utmpx utx; 120#endif /* DOUTMPX && defined(_PATH_WTMPX) */ 121 struct stat buf; 122 123 memset(&ut, 0, sizeof(struct utmp)); 124 125 if (!line) { 126 close(fd); 127#if DOUTMPX && defined(_PATH_WTMPX) 128 close(fdx); 129#endif /* DOUTMPX && defined(_PATH_WTMPX) */ 130 line = ""; 131 } else 132 if (!strncmp(line, "/dev/", 5)) 133 line += 5; 134 135 if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY | O_APPEND, 0)) < 0) 136 return; 137 if (fstat(fd, &buf) == 0) { 138#if HAVE_UT_TYPE && defined(USER_PROCESS) 139 ut.ut_type = USER_PROCESS; 140#endif /* HAVE_UT_TYPE && defined(USER_PROCESS) */ 141#if HAVE_UT_PID 142 ut.ut_pid = getpid(); 143#endif /* HAVE_UT_PID */ 144 strncpy(ut.ut_line, line, sizeof(ut.ut_line)); 145 strncpy(ut.ut_name, name, sizeof(ut.ut_name)); 146#if HAVE_UT_HOST 147 strncpy(ut.ut_host, host, sizeof(ut.ut_host)); 148#endif /* HAVE_UT_HOST */ 149 time(&ut.ut_time); 150 if (write(fd, (char *) &ut, sizeof(struct utmp)) != 151 sizeof(struct utmp)) 152 ftruncate(fd, buf.st_size); 153 } 154 155#if DOUTMPX && defined(_PATH_WTMPX) 156 memset(&utx, 0, sizeof(struct utmpx)); 157 158 if (fdx < 0 && (fdx = open(_PATH_WTMPX, O_WRONLY | O_APPEND, 0)) < 0) 159 return; 160 if (fstat(fdx, &buf) == 0) { 161 strncpy(utx.ut_line, line, sizeof(utx.ut_line)); 162 strncpy(utx.ut_name, name, sizeof(utx.ut_name)); 163 strncpy(utx.ut_host, host, sizeof(utx.ut_host)); 164#if HAVE_GETTIMEOFDAY 165#if HAVE_ONE_ARG_GETTIMEOFDAY 166 gettimeofday(&utx.ut_tv); 167#else /* HAVE_ONE_ARG_GETTIMEOFDAY */ 168 gettimeofday(&utx.ut_tv, NULL); 169#endif /* HAVE_ONE_ARG_GETTIMEOFDAY */ 170#endif /* HAVE_GETTIMEOFDAY */ 171 if (write(fdx, (char *) &utx, sizeof(struct utmpx)) != sizeof(struct utmpx)) 172 ftruncate(fdx, buf.st_size); 173 } 174#endif /* DOUTMPX && defined(_PATH_WTMPX) */ 175#endif /* !DISABLE_WTMP */ 176} 177