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