logwtmp.c revision 16433
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
351592Srgrimes#ifndef lint
361592Srgrimesstatic char sccsid[] = "@(#)logwtmp.c	8.1 (Berkeley) 6/4/93";
371592Srgrimes#endif /* not lint */
381592Srgrimes
391592Srgrimes#include <sys/types.h>
401592Srgrimes#include <sys/stat.h>
4116433Sache#include <netinet/in.h>
4216433Sache#include <arpa/inet.h>
431592Srgrimes
441592Srgrimes#include <fcntl.h>
4516433Sache#include <time.h>
4616433Sache#include <netdb.h>
471592Srgrimes#include <utmp.h>
481592Srgrimes#include <unistd.h>
491592Srgrimes#include <stdio.h>
501592Srgrimes#include <string.h>
511592Srgrimes#include "extern.h"
521592Srgrimes
531592Srgrimesstatic int fd = -1;
541592Srgrimes
551592Srgrimes/*
561592Srgrimes * Modified version of logwtmp that holds wtmp file open
571592Srgrimes * after first call, for use with ftp (which may chroot
581592Srgrimes * after login, but before logout).
591592Srgrimes */
601592Srgrimesvoid
611592Srgrimeslogwtmp(line, name, host)
621592Srgrimes	char *line, *name, *host;
631592Srgrimes{
641592Srgrimes	struct utmp ut;
651592Srgrimes	struct stat buf;
661592Srgrimes
6716433Sache	if (strlen(host) > UT_HOSTSIZE) {
6816433Sache		struct hostent *hp = gethostbyname(host);
6916433Sache
7016433Sache		if (hp != NULL) {
7116433Sache			struct in_addr in;
7216433Sache
7316433Sache			memmove(&in, hp->h_addr, sizeof(in));
7416433Sache			host = inet_ntoa(in);
7516433Sache		} else
7616433Sache			host = "invalid hostname";
7716433Sache	}
7816433Sache
791592Srgrimes	if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
801592Srgrimes		return;
811592Srgrimes	if (fstat(fd, &buf) == 0) {
821592Srgrimes		(void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
831592Srgrimes		(void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
841592Srgrimes		(void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
851592Srgrimes		(void)time(&ut.ut_time);
861592Srgrimes		if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
871592Srgrimes		    sizeof(struct utmp))
881592Srgrimes			(void)ftruncate(fd, buf.st_size);
891592Srgrimes	}
901592Srgrimes}
91