1 /* $NetBSD: logwtmp.c,v 1.3 2019/01/29 12:14:46 lukem Exp $ */
2 /* from NetBSD: logwtmp.c,v 1.27 2015/08/09 20:34:24 shm Exp */
3
4 /*
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 #if defined(HAVE_TNFTPD_H)
35 #include "tnftpd.h"
36 #else /* !defined(HAVE_TNFTPD_H) */
37
38 #include <sys/cdefs.h>
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)logwtmp.c 8.1 (Berkeley) 6/4/93";
42 #else
43 __RCSID(" NetBSD: logwtmp.c,v 1.27 2015/08/09 20:34:24 shm Exp ");
44 #endif
45 #endif /* not lint */
46
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/wait.h>
53
54 #include <fcntl.h>
55 #include <netdb.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <time.h>
60 #include <syslog.h>
61 #include <unistd.h>
62 #ifdef SUPPORT_UTMP
63 #include <utmp.h>
64 #endif
65 #ifdef SUPPORT_UTMPX
66 #include <utmpx.h>
67 #endif
68
69 #ifdef KERBEROS5
70 #include <krb5/krb5.h>
71 #endif
72
73 #endif /* !defined(HAVE_TNFTPD_H) */
74
75 #include "extern.h"
76
77 #ifdef SUPPORT_UTMP
78 static int fd = -1;
79
80 void
ftpd_initwtmp(void)81 ftpd_initwtmp(void)
82 {
83 const char *wf = _PATH_WTMP;
84 if ((fd = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
85 syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
86 }
87
88 /*
89 * Modified version of logwtmp that holds wtmp file open
90 * after first call, for use with ftp (which may chroot
91 * after login, but before logout).
92 */
93 void
ftpd_logwtmp(const char * line,const char * name,const char * host)94 ftpd_logwtmp(const char *line, const char *name, const char *host)
95 {
96 struct utmp ut;
97 struct stat buf;
98
99 if (strlen(host) > UT_HOSTSIZE) {
100 struct addrinfo hints, *res;
101 int error;
102 static char hostbuf[BUFSIZ];
103
104 memset(&hints, 0, sizeof(hints));
105 hints.ai_family = PF_UNSPEC;
106 error = getaddrinfo(host, NULL, &hints, &res);
107 if (error)
108 host = "invalid hostname";
109 else {
110 getnameinfo(res->ai_addr, res->ai_addrlen,
111 hostbuf, sizeof(hostbuf), NULL, 0,
112 NI_NUMERICHOST);
113 host = hostbuf;
114 if (strlen(host) > UT_HOSTSIZE)
115 hostbuf[UT_HOSTSIZE] = '\0';
116 }
117 }
118
119 if (fd < 0)
120 return;
121 if (fstat(fd, &buf) == 0) {
122 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
123 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
124 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
125 (void)time(&ut.ut_time);
126 if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
127 sizeof(struct utmp))
128 (void)ftruncate(fd, buf.st_size);
129 }
130 }
131 #endif
132
133 #if 0
134 static int fdx = -1;
135
136 void
137 ftpd_initwtmpx(void)
138 {
139 const char *wf = _PATH_WTMPX;
140 if ((fdx = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
141 syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
142 }
143
144 void
145 ftpd_logwtmpx(const char *line, const char *name, const char *host,
146 struct sockinet *haddr, int status, int utx_type)
147 {
148 struct utmpx ut;
149 struct stat buf;
150
151 if (fdx < 0)
152 return;
153 if (fstat(fdx, &buf) == 0) {
154 (void)memset(&ut, 0, sizeof(ut));
155 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
156 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
157 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
158 if (haddr)
159 (void)memcpy(&ut.ut_ss, &haddr->si_su, haddr->su_len);
160 ut.ut_type = utx_type;
161 if (WIFEXITED(status))
162 ut.ut_exit.e_exit = (uint16_t)WEXITSTATUS(status);
163 if (WIFSIGNALED(status))
164 ut.ut_exit.e_termination = (uint16_t)WTERMSIG(status);
165 (void)gettimeofday(&ut.ut_tv, NULL);
166 if(write(fdx, (char *)&ut, sizeof(struct utmpx)) !=
167 sizeof(struct utmpx))
168 (void)ftruncate(fdx, buf.st_size);
169 }
170 }
171 #endif
172