logwtmp.c revision 92907
1582Srgrimes/* logwtmp.c: Put an entry in the wtmp file. 2582Srgrimes 3582Srgrimes%%% portions-copyright-cmetz-96 4582SrgrimesPortions of this software are Copyright 1996-1999 by Craig Metz, All Rights 5582SrgrimesReserved. The Inner Net License Version 2 applies to these portions of 6582Srgrimesthe software. 7582SrgrimesYou should have received a copy of the license with this software. If 8582Srgrimesyou didn't get a copy, you may request one from <license@inner.net>. 9582Srgrimes 10582SrgrimesPortions of this software are Copyright 1995 by Randall Atkinson and Dan 11582SrgrimesMcDonald, All Rights Reserved. All Rights under this copyright are assigned 12582Srgrimesto the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and 13582SrgrimesLicense Agreement applies to this software. 14582Srgrimes 15582Srgrimes History: 16582Srgrimes 17582Srgrimes Modified by cmetz for OPIE 2.4. Set process to dead if name is null. 18582Srgrimes Added support for ut_id and ut_syslen. 19582Srgrimes Modified by cmetz for OPIE 2.32. Don't leave line=NULL, skip 20582Srgrimes past /dev/ in line. Fill in ut_host on systems with UTMPX and 21582Srgrimes ut_host. 22582Srgrimes Modified by cmetz for OPIE 2.31. Move wtmp log functions here, to 23582Srgrimes improve portability. Added DISABLE_WTMP. 24582Srgrimes Modified by cmetz for OPIE 2.22. Call gettimeofday() properly. 25582Srgrimes Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al. 26582Srgrimes Ifdef around some headers. Added file close hook. 27582Srgrimes Modified at NRL for OPIE 2.1. Set process type for HPUX. 28582Srgrimes Modified at NRL for OPIE 2.0. 29582Srgrimes Originally from BSD. 30582Srgrimes*/ 31582Srgrimes/* 32582Srgrimes * Copyright (c) 1988 The Regents of the University of California. 33582Srgrimes * All rights reserved. 3450477Speter * 35582Srgrimes * Redistribution and use in source and binary forms, with or without 36582Srgrimes * modification, are permitted provided that the following conditions 37582Srgrimes * are met: 38582Srgrimes * 1. Redistributions of source code must retain the above copyright 39582Srgrimes * notice, this list of conditions and the following disclaimer. 40582Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 41582Srgrimes * notice, this list of conditions and the following disclaimer in the 42582Srgrimes * documentation and/or other materials provided with the distribution. 43582Srgrimes * 3. All advertising materials mentioning features or use of this software 44582Srgrimes * must display the following acknowledgement: 45582Srgrimes * This product includes software developed by the University of 46582Srgrimes * California, Berkeley and its contributors. 47582Srgrimes * 4. Neither the name of the University nor the names of its contributors 48582Srgrimes * may be used to endorse or promote products derived from this software 49582Srgrimes * without specific prior written permission. 50582Srgrimes * 51582Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52582Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53582Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54582Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55582Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56582Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57582Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58582Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59582Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60582Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61582Srgrimes * SUCH DAMAGE. 62582Srgrimes * 63582Srgrimes */ 64582Srgrimes 65582Srgrimes#include "opie_cfg.h" 66582Srgrimes 67582Srgrimes#include <sys/types.h> 68582Srgrimes#if HAVE_SYS_TIME_H 69582Srgrimes#include <sys/time.h> 70582Srgrimes#endif /* HAVE_SYS_TIME_H */ 71582Srgrimes#include <sys/stat.h> 72582Srgrimes#include <fcntl.h> 73582Srgrimes#include <utmp.h> 74582Srgrimes#if HAVE_UNISTD_H 75582Srgrimes#include <unistd.h> 76582Srgrimes#endif /* HAVE_UNISTD_H */ 77582Srgrimes#if HAVE_STRING_H 78582Srgrimes#include <string.h> 79582Srgrimes#endif /* HAVE_STRING_H */ 80582Srgrimes 81582Srgrimes#include "opie.h" 82582Srgrimes 83582Srgrimesstatic int fd = -1; 84582Srgrimes 85582Srgrimes#if DOUTMPX 86582Srgrimesstatic int fdx = -1; 87582Srgrimes#include <utmpx.h> 88582Srgrimes#endif /* DOUTMPX */ 89582Srgrimes 90582Srgrimes#ifndef _PATH_WTMP 91582Srgrimes#ifdef WTMP_FILE 92582Srgrimes#define _PATH_WTMP WTMP_FILE 93582Srgrimes#else /* WTMP_FILE */ 94582Srgrimes#ifdef PATH_WTMP_AC 95582Srgrimes#define _PATH_WTMP PATH_WTMP_AC 96582Srgrimes#endif /* PATH_WTMP_AC */ 97582Srgrimes#endif /* WTMP_FILE */ 98582Srgrimes#endif /* _PATH_WTMP */ 99582Srgrimes 100582Srgrimes#ifndef _PATH_WTMPX 101582Srgrimes#ifdef WTMPX_FILE 102582Srgrimes#define _PATH_WTMPX WTMPX_FILE 103582Srgrimes#else /* WTMPX_FILE */ 104582Srgrimes#ifdef PATH_WTMPX_AC 105582Srgrimes#define _PATH_WTMPX PATH_WTMPX_AC 106582Srgrimes#endif /* PATH_WTMPX_AC */ 107582Srgrimes#endif /* WTMPX_FILE */ 108582Srgrimes#endif /* _PATH_WTMPX */ 109582Srgrimes 110582Srgrimes/* 111582Srgrimes * Modified version of logwtmp that holds wtmp file open 112582Srgrimes * after first call, for use with ftp (which may chroot 113582Srgrimes * after login, but before logout). 114582Srgrimes */ 115582SrgrimesVOIDRET opielogwtmp FUNCTION((line, name, host), char *line AND char *name AND char *host AND char *id) 116582Srgrimes{ 117582Srgrimes#if !DISABLE_WTMP 118582Srgrimes struct utmp ut; 119582Srgrimes 120582Srgrimes#if DOUTMPX && defined(_PATH_WTMPX) 121582Srgrimes struct utmpx utx; 122582Srgrimes#endif /* DOUTMPX && defined(_PATH_WTMPX) */ 123582Srgrimes struct stat buf; 124582Srgrimes 125582Srgrimes memset(&ut, 0, sizeof(struct utmp)); 126582Srgrimes 127582Srgrimes if (!line) { 128582Srgrimes close(fd); 129582Srgrimes#if DOUTMPX && defined(_PATH_WTMPX) 130582Srgrimes close(fdx); 131582Srgrimes#endif /* DOUTMPX && defined(_PATH_WTMPX) */ 132582Srgrimes line = ""; 133582Srgrimes } else 134582Srgrimes if (!strncmp(line, "/dev/", 5)) 135582Srgrimes line += 5; 136582Srgrimes 137582Srgrimes if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY | O_APPEND, 0)) < 0) 138582Srgrimes return; 139582Srgrimes if (fstat(fd, &buf) == 0) { 140582Srgrimes#if HAVE_UT_TYPE && defined(USER_PROCESS) 141582Srgrimes if (name && *name) 142582Srgrimes ut.ut_type = USER_PROCESS; 143582Srgrimes else 144582Srgrimes ut.ut_type = DEAD_PROCESS; 145582Srgrimes#endif /* HAVE_UT_TYPE && defined(USER_PROCESS) */ 146582Srgrimes#if HAVE_UT_ID 147582Srgrimes if (id) 148582Srgrimes strncpy(ut.ut_id, id, sizeof(ut.ut_id)); 149582Srgrimes#endif /* HAVE_UT_ID */ 150582Srgrimes#if HAVE_UT_PID 151582Srgrimes ut.ut_pid = getpid(); 152582Srgrimes#endif /* HAVE_UT_PID */ 153582Srgrimes strncpy(ut.ut_line, line, sizeof(ut.ut_line)); 154582Srgrimes strncpy(ut.ut_name, name, sizeof(ut.ut_name)); 155582Srgrimes#if HAVE_UT_HOST 156582Srgrimes strncpy(ut.ut_host, host, sizeof(ut.ut_host)); 157582Srgrimes#endif /* HAVE_UT_HOST */ 158582Srgrimes time(&ut.ut_time); 159582Srgrimes if (write(fd, (char *) &ut, sizeof(struct utmp)) != 160582Srgrimes sizeof(struct utmp)) 161582Srgrimes ftruncate(fd, buf.st_size); 162582Srgrimes } 163582Srgrimes 164582Srgrimes#if DOUTMPX && defined(_PATH_WTMPX) 165582Srgrimes memset(&utx, 0, sizeof(struct utmpx)); 166582Srgrimes 167582Srgrimes if (fdx < 0 && (fdx = open(_PATH_WTMPX, O_WRONLY | O_APPEND, 0)) < 0) 168582Srgrimes return; 169582Srgrimes if (fstat(fdx, &buf) == 0) { 170582Srgrimes strncpy(utx.ut_line, line, sizeof(utx.ut_line)); 171582Srgrimes strncpy(utx.ut_name, name, sizeof(utx.ut_name)); 172582Srgrimes strncpy(utx.ut_host, host, sizeof(utx.ut_host)); 173582Srgrimes#ifdef USER_PROCESS 174582Srgrimes if (name && *name) 175582Srgrimes utx.ut_type = USER_PROCESS; 176582Srgrimes else 177582Srgrimes utx.ut_type = DEAD_PROCESS; 178582Srgrimes#endif /* USER_PROCESS */ 179582Srgrimes if (id) 180582Srgrimes strncpy(utx.ut_id, id, sizeof(utx.ut_id)); 181582Srgrimes utx.ut_pid = getpid(); 182582Srgrimes#if HAVE_UTX_SYSLEN 183582Srgrimes utx.ut_syslen = strlen(utx.ut_host) + 1; 184582Srgrimes#endif /* HAVE_UTX_SYSLEN */ 185582Srgrimes#if HAVE_GETTIMEOFDAY 186582Srgrimes#if HAVE_ONE_ARG_GETTIMEOFDAY 187582Srgrimes gettimeofday(&utx.ut_tv); 188582Srgrimes#else /* HAVE_ONE_ARG_GETTIMEOFDAY */ 189582Srgrimes gettimeofday(&utx.ut_tv, NULL); 190582Srgrimes#endif /* HAVE_ONE_ARG_GETTIMEOFDAY */ 191582Srgrimes#endif /* HAVE_GETTIMEOFDAY */ 192582Srgrimes if (write(fdx, (char *) &utx, sizeof(struct utmpx)) != sizeof(struct utmpx)) 193582Srgrimes ftruncate(fdx, buf.st_size); 194582Srgrimes } 195582Srgrimes#endif /* DOUTMPX && defined(_PATH_WTMPX) */ 196582Srgrimes#endif /* !DISABLE_WTMP */ 197582Srgrimes} 198582Srgrimes