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