xref: /netbsd-src/libexec/ftpd/logwtmp.c (revision c09e755be0594519b83146078345abb4037d24e4)
1*c09e755bSshm /*	$NetBSD: logwtmp.c,v 1.27 2015/08/09 20:34:24 shm Exp $	*/
26a991762Scgd 
361f28255Scgd /*
4d6743f02Sderaadt  * Copyright (c) 1988, 1993
5d6743f02Sderaadt  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
158e6ab883Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  *
3161f28255Scgd  */
3261f28255Scgd 
332424c4f9Schristos 
342424c4f9Schristos #include <sys/cdefs.h>
3561f28255Scgd #ifndef lint
366a991762Scgd #if 0
37d6743f02Sderaadt static char sccsid[] = "@(#)logwtmp.c	8.1 (Berkeley) 6/4/93";
386a991762Scgd #else
39*c09e755bSshm __RCSID("$NetBSD: logwtmp.c,v 1.27 2015/08/09 20:34:24 shm Exp $");
406a991762Scgd #endif
4161f28255Scgd #endif /* not lint */
4261f28255Scgd 
4361f28255Scgd #include <sys/types.h>
44f318090cSlukem #include <sys/param.h>
4561f28255Scgd #include <sys/time.h>
4661f28255Scgd #include <sys/stat.h>
473bab95ceStacha #include <sys/wait.h>
48d6743f02Sderaadt 
4961f28255Scgd #include <fcntl.h>
509e9281f6Skleink #include <signal.h>
51d6743f02Sderaadt #include <stdio.h>
5261f28255Scgd #include <string.h>
53cd7d0f55Skleink #include <time.h>
54ea7965ebSchristos #include <syslog.h>
550e5bdd51Slukem #include <unistd.h>
56ea7965ebSchristos #ifdef SUPPORT_UTMP
570e5bdd51Slukem #include <utmp.h>
58ea7965ebSchristos #endif
593bab95ceStacha #ifdef SUPPORT_UTMPX
603bab95ceStacha #include <utmpx.h>
613bab95ceStacha #endif
624056c9f7Schristos #include <util.h>
630e5bdd51Slukem 
64b2f939acSexplorer #ifdef KERBEROS5
65cee9ac24Schristos #include <krb5/krb5.h>
66b2f939acSexplorer #endif
67b2f939acSexplorer 
68d6743f02Sderaadt #include "extern.h"
6961f28255Scgd 
70ea7965ebSchristos #ifdef SUPPORT_UTMP
7161f28255Scgd static int fd = -1;
72ea7965ebSchristos 
73ea7965ebSchristos void
ftpd_initwtmp(void)74ea7965ebSchristos ftpd_initwtmp(void)
75ea7965ebSchristos {
76ea7965ebSchristos 	const char *wf = _PATH_WTMP;
77ea7965ebSchristos 	if ((fd = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
78ea7965ebSchristos 		syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
79ea7965ebSchristos }
8061f28255Scgd 
8161f28255Scgd /*
8261f28255Scgd  * Modified version of logwtmp that holds wtmp file open
8361f28255Scgd  * after first call, for use with ftp (which may chroot
8461f28255Scgd  * after login, but before logout).
8561f28255Scgd  */
86d6743f02Sderaadt void
ftpd_logwtmp(const char * line,const char * name,const char * host)8755803244Slukem ftpd_logwtmp(const char *line, const char *name, const char *host)
8861f28255Scgd {
8961f28255Scgd 	struct utmp ut;
9061f28255Scgd 	struct stat buf;
9161f28255Scgd 
9247e7cfa9Sxtraeme 	if (fd < 0)
9361f28255Scgd 		return;
9461f28255Scgd 	if (fstat(fd, &buf) == 0) {
95a05a73b5Sitojun 		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
96a05a73b5Sitojun 		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
97a05a73b5Sitojun 		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
9861f28255Scgd 		(void)time(&ut.ut_time);
9961f28255Scgd 		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
10061f28255Scgd 		    sizeof(struct utmp))
10161f28255Scgd 			(void)ftruncate(fd, buf.st_size);
10261f28255Scgd 	}
10361f28255Scgd }
104ea7965ebSchristos #endif
1053bab95ceStacha 
1063bab95ceStacha #ifdef SUPPORT_UTMPX
107ea7965ebSchristos static int fdx = -1;
108ea7965ebSchristos 
109ea7965ebSchristos void
ftpd_initwtmpx(void)110ea7965ebSchristos ftpd_initwtmpx(void)
111ea7965ebSchristos {
112ea7965ebSchristos 	const char *wf = _PATH_WTMPX;
11347e7cfa9Sxtraeme 	if ((fdx = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
114ea7965ebSchristos 		syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
115ea7965ebSchristos }
116ea7965ebSchristos 
1173bab95ceStacha void
ftpd_logwtmpx(const char * line,const char * name,const char * host,struct sockinet * haddr,int status,int utx_type)1183b40bfaeSchristos ftpd_logwtmpx(const char *line, const char *name, const char *host,
1193b40bfaeSchristos     struct sockinet *haddr, int status, int utx_type)
1203bab95ceStacha {
1213bab95ceStacha 	struct utmpx ut;
1223bab95ceStacha 	struct stat buf;
1233bab95ceStacha 
124ea7965ebSchristos 	if (fdx < 0)
1253bab95ceStacha 		return;
1263bab95ceStacha 	if (fstat(fdx, &buf) == 0) {
1270c7f5afeSshm 		(void)memset(&ut, 0, sizeof(ut));
1283bab95ceStacha 		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1293bab95ceStacha 		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1303bab95ceStacha 		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1313b40bfaeSchristos 		if (haddr)
1323b40bfaeSchristos 			(void)memcpy(&ut.ut_ss, &haddr->si_su, haddr->su_len);
1333bab95ceStacha 		ut.ut_type = utx_type;
1343bab95ceStacha 		if (WIFEXITED(status))
1353bab95ceStacha 			ut.ut_exit.e_exit = (uint16_t)WEXITSTATUS(status);
1363bab95ceStacha 		if (WIFSIGNALED(status))
1373bab95ceStacha 		ut.ut_exit.e_termination = (uint16_t)WTERMSIG(status);
1383bab95ceStacha 		(void)gettimeofday(&ut.ut_tv, NULL);
1393bab95ceStacha 		if(write(fdx, (char *)&ut, sizeof(struct utmpx)) !=
1403bab95ceStacha 		    sizeof(struct utmpx))
1413bab95ceStacha 			(void)ftruncate(fdx, buf.st_size);
1423bab95ceStacha 	}
1433bab95ceStacha }
1443bab95ceStacha #endif
145