129964Sache/* logwtmp.c: Put an entry in the wtmp file.
229964Sache
329964Sache%%% portions-copyright-cmetz-96
492906SmarkmPortions of this software are Copyright 1996-1999 by Craig Metz, All Rights
529964SacheReserved. The Inner Net License Version 2 applies to these portions of
629964Sachethe software.
729964SacheYou should have received a copy of the license with this software. If
829964Sacheyou didn't get a copy, you may request one from <license@inner.net>.
929964Sache
1029964SachePortions of this software are Copyright 1995 by Randall Atkinson and Dan
1129964SacheMcDonald, All Rights Reserved. All Rights under this copyright are assigned
1229964Sacheto the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
1329964SacheLicense Agreement applies to this software.
1429964Sache
1529964Sache	History:
1629964Sache
1792906Smarkm	Modified by cmetz for OPIE 2.4. Set process to dead if name is null.
1892906Smarkm		Added support for ut_id and ut_syslen.
1959118Skris	Modified by cmetz for OPIE 2.32. Don't leave line=NULL, skip
2059118Skris		past /dev/ in line. Fill in ut_host on systems with UTMPX and
2159118Skris		ut_host.
2229964Sache	Modified by cmetz for OPIE 2.31. Move wtmp log functions here, to
2359118Skris		improve portability. Added DISABLE_WTMP.
2429964Sache	Modified by cmetz for OPIE 2.22. Call gettimeofday() properly.
2529964Sache	Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
2659118Skris        	Ifdef around some headers. Added file close hook.
2729964Sache	Modified at NRL for OPIE 2.1. Set process type for HPUX.
2829964Sache	Modified at NRL for OPIE 2.0.
2929964Sache	Originally from BSD.
3029964Sache*/
3129964Sache/*
3229964Sache * Copyright (c) 1988 The Regents of the University of California.
3329964Sache * All rights reserved.
3429964Sache *
3529964Sache * Redistribution and use in source and binary forms, with or without
3629964Sache * modification, are permitted provided that the following conditions
3729964Sache * are met:
3829964Sache * 1. Redistributions of source code must retain the above copyright
3929964Sache *    notice, this list of conditions and the following disclaimer.
4029964Sache * 2. Redistributions in binary form must reproduce the above copyright
4129964Sache *    notice, this list of conditions and the following disclaimer in the
4229964Sache *    documentation and/or other materials provided with the distribution.
4329964Sache * 3. All advertising materials mentioning features or use of this software
4429964Sache *    must display the following acknowledgement:
4529964Sache *      This product includes software developed by the University of
4629964Sache *      California, Berkeley and its contributors.
4729964Sache * 4. Neither the name of the University nor the names of its contributors
4829964Sache *    may be used to endorse or promote products derived from this software
4929964Sache *    without specific prior written permission.
5029964Sache *
5129964Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5229964Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5329964Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5429964Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5529964Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5629964Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5729964Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5829964Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5929964Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6029964Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6129964Sache * SUCH DAMAGE.
6229964Sache *
6329964Sache */
6429964Sache
6529964Sache#include "opie_cfg.h"
6629964Sache
6729964Sache#include <sys/types.h>
6829964Sache#if HAVE_SYS_TIME_H
6929964Sache#include <sys/time.h>
7029964Sache#endif /* HAVE_SYS_TIME_H */
7129964Sache#include <sys/stat.h>
7229964Sache#include <fcntl.h>
7329964Sache#include <utmp.h>
7429964Sache#if HAVE_UNISTD_H
7529964Sache#include <unistd.h>
7629964Sache#endif /* HAVE_UNISTD_H */
7729964Sache#if HAVE_STRING_H
7829964Sache#include <string.h>
7929964Sache#endif /* HAVE_STRING_H */
8029964Sache
8129964Sache#include "opie.h"
8229964Sache
8329964Sachestatic int fd = -1;
8429964Sache
8529964Sache#if DOUTMPX
8629964Sachestatic int fdx = -1;
8729964Sache#include <utmpx.h>
8829964Sache#endif	/* DOUTMPX */
8929964Sache
9029964Sache#ifndef _PATH_WTMP
9129964Sache#ifdef WTMP_FILE
9229964Sache#define _PATH_WTMP WTMP_FILE
9329964Sache#else /* WTMP_FILE */
9429964Sache#ifdef PATH_WTMP_AC
9529964Sache#define _PATH_WTMP PATH_WTMP_AC
9629964Sache#endif /* PATH_WTMP_AC */
9729964Sache#endif /* WTMP_FILE */
9829964Sache#endif /* _PATH_WTMP */
9929964Sache
10029964Sache#ifndef _PATH_WTMPX
10129964Sache#ifdef WTMPX_FILE
10229964Sache#define _PATH_WTMPX WTMPX_FILE
10329964Sache#else /* WTMPX_FILE */
10429964Sache#ifdef PATH_WTMPX_AC
10529964Sache#define _PATH_WTMPX PATH_WTMPX_AC
10629964Sache#endif /* PATH_WTMPX_AC */
10729964Sache#endif /* WTMPX_FILE */
10829964Sache#endif /* _PATH_WTMPX */
10929964Sache
11029964Sache/*
11129964Sache * Modified version of logwtmp that holds wtmp file open
11229964Sache * after first call, for use with ftp (which may chroot
11329964Sache * after login, but before logout).
11429964Sache */
11592906SmarkmVOIDRET opielogwtmp FUNCTION((line, name, host), char *line AND char *name AND char *host AND char *id)
11629964Sache{
11729964Sache#if !DISABLE_WTMP
11829964Sache  struct utmp ut;
11929964Sache
12029964Sache#if DOUTMPX && defined(_PATH_WTMPX)
12129964Sache  struct utmpx utx;
12229964Sache#endif /* DOUTMPX && defined(_PATH_WTMPX) */
12329964Sache  struct stat buf;
12429964Sache
12529964Sache  memset(&ut, 0, sizeof(struct utmp));
12629964Sache
12729964Sache  if (!line) {
12829964Sache    close(fd);
12929964Sache#if DOUTMPX && defined(_PATH_WTMPX)
13029964Sache    close(fdx);
13129964Sache#endif /* DOUTMPX && defined(_PATH_WTMPX) */
13259118Skris    line = "";
13359118Skris  } else
13459118Skris    if (!strncmp(line, "/dev/", 5))
13559118Skris      line += 5;
13629964Sache
13729964Sache  if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY | O_APPEND, 0)) < 0)
13829964Sache    return;
13929964Sache  if (fstat(fd, &buf) == 0) {
14029964Sache#if HAVE_UT_TYPE && defined(USER_PROCESS)
14192906Smarkm    if (name && *name)
14292906Smarkm      ut.ut_type = USER_PROCESS;
14392906Smarkm    else
14492906Smarkm      ut.ut_type = DEAD_PROCESS;
14529964Sache#endif /* HAVE_UT_TYPE && defined(USER_PROCESS) */
14692906Smarkm#if HAVE_UT_ID
14792906Smarkm    if (id)
14892906Smarkm      strncpy(ut.ut_id, id, sizeof(ut.ut_id));
14992906Smarkm#endif /* HAVE_UT_ID */
15029964Sache#if HAVE_UT_PID
15129964Sache    ut.ut_pid = getpid();
15229964Sache#endif /* HAVE_UT_PID */
15329964Sache    strncpy(ut.ut_line, line, sizeof(ut.ut_line));
15429964Sache    strncpy(ut.ut_name, name, sizeof(ut.ut_name));
15559118Skris#if HAVE_UT_HOST
15629964Sache    strncpy(ut.ut_host, host, sizeof(ut.ut_host));
15759118Skris#endif /* HAVE_UT_HOST */
15829964Sache    time(&ut.ut_time);
15929964Sache    if (write(fd, (char *) &ut, sizeof(struct utmp)) !=
16029964Sache	sizeof(struct utmp))
16129964Sache    ftruncate(fd, buf.st_size);
16229964Sache  }
16329964Sache
16429964Sache#if DOUTMPX && defined(_PATH_WTMPX)
16529964Sache  memset(&utx, 0, sizeof(struct utmpx));
16629964Sache
16729964Sache  if (fdx < 0 && (fdx = open(_PATH_WTMPX, O_WRONLY | O_APPEND, 0)) < 0)
16829964Sache    return;
16929964Sache  if (fstat(fdx, &buf) == 0) {
17029964Sache    strncpy(utx.ut_line, line, sizeof(utx.ut_line));
17129964Sache    strncpy(utx.ut_name, name, sizeof(utx.ut_name));
17229964Sache    strncpy(utx.ut_host, host, sizeof(utx.ut_host));
17392906Smarkm#ifdef USER_PROCESS
17492906Smarkm    if (name && *name)
17592906Smarkm      utx.ut_type = USER_PROCESS;
17692906Smarkm    else
17792906Smarkm      utx.ut_type = DEAD_PROCESS;
17892906Smarkm#endif /* USER_PROCESS */
17992906Smarkm    if (id)
18092906Smarkm      strncpy(utx.ut_id, id, sizeof(utx.ut_id));
18192906Smarkm    utx.ut_pid = getpid();
18292906Smarkm#if HAVE_UTX_SYSLEN
18392906Smarkm    utx.ut_syslen = strlen(utx.ut_host) + 1;
18492906Smarkm#endif /* HAVE_UTX_SYSLEN */
18529964Sache#if HAVE_GETTIMEOFDAY
18629964Sache#if HAVE_ONE_ARG_GETTIMEOFDAY
18729964Sache    gettimeofday(&utx.ut_tv);
18829964Sache#else /* HAVE_ONE_ARG_GETTIMEOFDAY */
18929964Sache    gettimeofday(&utx.ut_tv, NULL);
19029964Sache#endif /* HAVE_ONE_ARG_GETTIMEOFDAY */
19129964Sache#endif /* HAVE_GETTIMEOFDAY */
19229964Sache    if (write(fdx, (char *) &utx, sizeof(struct utmpx)) != sizeof(struct utmpx))
19329964Sache    ftruncate(fdx, buf.st_size);
19429964Sache  }
19529964Sache#endif /* DOUTMPX && defined(_PATH_WTMPX) */
19629964Sache#endif /* !DISABLE_WTMP */
19729964Sache}
198