logwtmp.c revision 56668
11592Srgrimes/*
21592Srgrimes * Copyright (c) 1988, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes */
331592Srgrimes
341592Srgrimes#ifndef lint
3531329Scharnier#if 0
361592Srgrimesstatic char sccsid[] = "@(#)logwtmp.c	8.1 (Berkeley) 6/4/93";
3731329Scharnier#endif
3831329Scharnierstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/libexec/ftpd/logwtmp.c 56668 2000-01-27 09:28:38Z shin $";
401592Srgrimes#endif /* not lint */
411592Srgrimes
421592Srgrimes#include <sys/types.h>
431592Srgrimes#include <sys/stat.h>
4416433Sache#include <netinet/in.h>
4516433Sache#include <arpa/inet.h>
4656668Sshin#include <sys/socket.h>
471592Srgrimes
481592Srgrimes#include <fcntl.h>
4916433Sache#include <time.h>
5016433Sache#include <netdb.h>
511592Srgrimes#include <utmp.h>
521592Srgrimes#include <unistd.h>
531592Srgrimes#include <stdio.h>
541592Srgrimes#include <string.h>
551592Srgrimes#include "extern.h"
561592Srgrimes
571592Srgrimesstatic int fd = -1;
581592Srgrimes
591592Srgrimes/*
601592Srgrimes * Modified version of logwtmp that holds wtmp file open
611592Srgrimes * after first call, for use with ftp (which may chroot
621592Srgrimes * after login, but before logout).
631592Srgrimes */
641592Srgrimesvoid
6529140Stgftpd_logwtmp(line, name, host)
661592Srgrimes	char *line, *name, *host;
671592Srgrimes{
681592Srgrimes	struct utmp ut;
691592Srgrimes	struct stat buf;
701592Srgrimes
7116433Sache	if (strlen(host) > UT_HOSTSIZE) {
7256668Sshin		struct addrinfo hints, *res;
7356668Sshin		int error;
7456668Sshin		static char hostbuf[BUFSIZ];
7516433Sache
7656668Sshin		memset(&hints, 0, sizeof(hints));
7756668Sshin		hints.ai_family = PF_UNSPEC;
7856668Sshin		error = getaddrinfo(host, NULL, &hints, &res);
7956668Sshin		if (error)
8016433Sache			host = "invalid hostname";
8156668Sshin		else {
8256668Sshin			getnameinfo(res->ai_addr, res->ai_addrlen,
8356668Sshin				hostbuf, sizeof(hostbuf), NULL, 0,
8456668Sshin				NI_NUMERICHOST);
8556668Sshin			host = hostbuf;
8656668Sshin			if (strlen(host) > UT_HOSTSIZE)
8756668Sshin				host[UT_HOSTSIZE] = '\0';
8856668Sshin		}
8916433Sache	}
9016433Sache
911592Srgrimes	if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
921592Srgrimes		return;
931592Srgrimes	if (fstat(fd, &buf) == 0) {
941592Srgrimes		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
951592Srgrimes		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
961592Srgrimes		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
971592Srgrimes		(void)time(&ut.ut_time);
981592Srgrimes		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
991592Srgrimes		    sizeof(struct utmp))
1001592Srgrimes			(void)ftruncate(fd, buf.st_size);
1011592Srgrimes	}
1021592Srgrimes}
103