xref: /minix3/libexec/ftpd/logwtmp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: logwtmp.c,v 1.27 2015/08/09 20:34:24 shm Exp $	*/
262da0113SBen Gras 
362da0113SBen Gras /*
462da0113SBen Gras  * Copyright (c) 1988, 1993
562da0113SBen Gras  *	The Regents of the University of California.  All rights reserved.
662da0113SBen Gras  *
762da0113SBen Gras  * Redistribution and use in source and binary forms, with or without
862da0113SBen Gras  * modification, are permitted provided that the following conditions
962da0113SBen Gras  * are met:
1062da0113SBen Gras  * 1. Redistributions of source code must retain the above copyright
1162da0113SBen Gras  *    notice, this list of conditions and the following disclaimer.
1262da0113SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
1362da0113SBen Gras  *    notice, this list of conditions and the following disclaimer in the
1462da0113SBen Gras  *    documentation and/or other materials provided with the distribution.
1562da0113SBen Gras  * 3. Neither the name of the University nor the names of its contributors
1662da0113SBen Gras  *    may be used to endorse or promote products derived from this software
1762da0113SBen Gras  *    without specific prior written permission.
1862da0113SBen Gras  *
1962da0113SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2062da0113SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2162da0113SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2262da0113SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2362da0113SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2462da0113SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2562da0113SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2662da0113SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2762da0113SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2862da0113SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2962da0113SBen Gras  * SUCH DAMAGE.
3062da0113SBen Gras  *
3162da0113SBen Gras  */
3262da0113SBen Gras 
3362da0113SBen Gras 
3462da0113SBen Gras #include <sys/cdefs.h>
3562da0113SBen Gras #ifndef lint
3662da0113SBen Gras #if 0
3762da0113SBen Gras static char sccsid[] = "@(#)logwtmp.c	8.1 (Berkeley) 6/4/93";
3862da0113SBen Gras #else
39*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: logwtmp.c,v 1.27 2015/08/09 20:34:24 shm Exp $");
4062da0113SBen Gras #endif
4162da0113SBen Gras #endif /* not lint */
4262da0113SBen Gras 
4362da0113SBen Gras #include <sys/types.h>
4462da0113SBen Gras #include <sys/param.h>
4562da0113SBen Gras #include <sys/time.h>
4662da0113SBen Gras #include <sys/stat.h>
4762da0113SBen Gras #include <sys/wait.h>
4862da0113SBen Gras 
4962da0113SBen Gras #include <fcntl.h>
5062da0113SBen Gras #include <signal.h>
5162da0113SBen Gras #include <stdio.h>
5262da0113SBen Gras #include <string.h>
5362da0113SBen Gras #include <time.h>
5462da0113SBen Gras #include <syslog.h>
5562da0113SBen Gras #include <unistd.h>
5662da0113SBen Gras #ifdef SUPPORT_UTMP
5762da0113SBen Gras #include <utmp.h>
5862da0113SBen Gras #endif
5962da0113SBen Gras #ifdef SUPPORT_UTMPX
6062da0113SBen Gras #include <utmpx.h>
6162da0113SBen Gras #endif
6262da0113SBen Gras #include <util.h>
6362da0113SBen Gras 
6462da0113SBen Gras #ifdef KERBEROS5
6562da0113SBen Gras #include <krb5/krb5.h>
6662da0113SBen Gras #endif
6762da0113SBen Gras 
6862da0113SBen Gras #include "extern.h"
6962da0113SBen Gras 
7062da0113SBen Gras #ifdef SUPPORT_UTMP
7162da0113SBen Gras static int fd = -1;
7262da0113SBen Gras 
7362da0113SBen Gras void
ftpd_initwtmp(void)7462da0113SBen Gras ftpd_initwtmp(void)
7562da0113SBen Gras {
7662da0113SBen Gras 	const char *wf = _PATH_WTMP;
7762da0113SBen Gras 	if ((fd = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
7862da0113SBen Gras 		syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
7962da0113SBen Gras }
8062da0113SBen Gras 
8162da0113SBen Gras /*
8262da0113SBen Gras  * Modified version of logwtmp that holds wtmp file open
8362da0113SBen Gras  * after first call, for use with ftp (which may chroot
8462da0113SBen Gras  * after login, but before logout).
8562da0113SBen Gras  */
8662da0113SBen Gras void
ftpd_logwtmp(const char * line,const char * name,const char * host)8762da0113SBen Gras ftpd_logwtmp(const char *line, const char *name, const char *host)
8862da0113SBen Gras {
8962da0113SBen Gras 	struct utmp ut;
9062da0113SBen Gras 	struct stat buf;
9162da0113SBen Gras 
9262da0113SBen Gras 	if (fd < 0)
9362da0113SBen Gras 		return;
9462da0113SBen Gras 	if (fstat(fd, &buf) == 0) {
9562da0113SBen Gras 		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
9662da0113SBen Gras 		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
9762da0113SBen Gras 		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
9862da0113SBen Gras 		(void)time(&ut.ut_time);
9962da0113SBen Gras 		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
10062da0113SBen Gras 		    sizeof(struct utmp))
10162da0113SBen Gras 			(void)ftruncate(fd, buf.st_size);
10262da0113SBen Gras 	}
10362da0113SBen Gras }
10462da0113SBen Gras #endif
10562da0113SBen Gras 
10662da0113SBen Gras #ifdef SUPPORT_UTMPX
10762da0113SBen Gras static int fdx = -1;
10862da0113SBen Gras 
10962da0113SBen Gras void
ftpd_initwtmpx(void)11062da0113SBen Gras ftpd_initwtmpx(void)
11162da0113SBen Gras {
11262da0113SBen Gras 	const char *wf = _PATH_WTMPX;
11362da0113SBen Gras 	if ((fdx = open(wf, O_WRONLY|O_APPEND, 0)) == -1)
11462da0113SBen Gras 		syslog(LOG_ERR, "Cannot open `%s' (%m)", wf);
11562da0113SBen Gras }
11662da0113SBen Gras 
11762da0113SBen Gras void
ftpd_logwtmpx(const char * line,const char * name,const char * host,struct sockinet * haddr,int status,int utx_type)11862da0113SBen Gras ftpd_logwtmpx(const char *line, const char *name, const char *host,
11962da0113SBen Gras     struct sockinet *haddr, int status, int utx_type)
12062da0113SBen Gras {
12162da0113SBen Gras 	struct utmpx ut;
12262da0113SBen Gras 	struct stat buf;
12362da0113SBen Gras 
12462da0113SBen Gras 	if (fdx < 0)
12562da0113SBen Gras 		return;
12662da0113SBen Gras 	if (fstat(fdx, &buf) == 0) {
127*0a6a1f1dSLionel Sambuc 		(void)memset(&ut, 0, sizeof(ut));
12862da0113SBen Gras 		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
12962da0113SBen Gras 		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
13062da0113SBen Gras 		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
13162da0113SBen Gras 		if (haddr)
13262da0113SBen Gras 			(void)memcpy(&ut.ut_ss, &haddr->si_su, haddr->su_len);
13362da0113SBen Gras 		ut.ut_type = utx_type;
13462da0113SBen Gras 		if (WIFEXITED(status))
13562da0113SBen Gras 			ut.ut_exit.e_exit = (uint16_t)WEXITSTATUS(status);
13662da0113SBen Gras 		if (WIFSIGNALED(status))
13762da0113SBen Gras 		ut.ut_exit.e_termination = (uint16_t)WTERMSIG(status);
13862da0113SBen Gras 		(void)gettimeofday(&ut.ut_tv, NULL);
13962da0113SBen Gras 		if(write(fdx, (char *)&ut, sizeof(struct utmpx)) !=
14062da0113SBen Gras 		    sizeof(struct utmpx))
14162da0113SBen Gras 			(void)ftruncate(fdx, buf.st_size);
14262da0113SBen Gras 	}
14362da0113SBen Gras }
14462da0113SBen Gras #endif
145