Home | History | Annotate | Line # | Download | only in ftpd
logutmp.c revision 1.1
      1  1.1  lukem /*
      2  1.1  lukem  * Portions Copyright (c) 1988, 1993
      3  1.1  lukem  *	The Regents of the University of California.  All rights reserved.
      4  1.1  lukem  * Portions Copyright (c) 1996, Jason Downs.  All rights reserved.
      5  1.1  lukem  *
      6  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      7  1.1  lukem  * modification, are permitted provided that the following conditions
      8  1.1  lukem  * are met:
      9  1.1  lukem  * 1. Redistributions of source code must retain the above copyright
     10  1.1  lukem  *    notice, this list of conditions and the following disclaimer.
     11  1.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  lukem  *    documentation and/or other materials provided with the distribution.
     14  1.1  lukem  * 3. All advertising materials mentioning features or use of this software
     15  1.1  lukem  *    must display the following acknowledgement:
     16  1.1  lukem  *	This product includes software developed by the University of
     17  1.1  lukem  *	California, Berkeley and its contributors.
     18  1.1  lukem  * 4. Neither the name of the University nor the names of its contributors
     19  1.1  lukem  *    may be used to endorse or promote products derived from this software
     20  1.1  lukem  *    without specific prior written permission.
     21  1.1  lukem  *
     22  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1  lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  lukem  * SUCH DAMAGE.
     33  1.1  lukem  */
     34  1.1  lukem 
     35  1.1  lukem #include <sys/types.h>
     36  1.1  lukem 
     37  1.1  lukem #include <fcntl.h>
     38  1.1  lukem #include <stdio.h>
     39  1.1  lukem #include <stdlib.h>
     40  1.1  lukem #include <string.h>
     41  1.1  lukem #include <ttyent.h>
     42  1.1  lukem #include <unistd.h>
     43  1.1  lukem #include <utmp.h>
     44  1.1  lukem #include <util.h>
     45  1.1  lukem 
     46  1.1  lukem typedef struct utmp UTMP;
     47  1.1  lukem 
     48  1.1  lukem static int fd = -1;
     49  1.1  lukem static int topslot = -1;
     50  1.1  lukem 
     51  1.1  lukem /*
     52  1.1  lukem  * Special versions of login()/logout() which hold the utmp file open,
     53  1.1  lukem  * for use with ftpd.
     54  1.1  lukem  */
     55  1.1  lukem 
     56  1.1  lukem void
     57  1.1  lukem login(ut)
     58  1.1  lukem 	const UTMP *ut;
     59  1.1  lukem {
     60  1.1  lukem 	UTMP ubuf;
     61  1.1  lukem 
     62  1.1  lukem 	/*
     63  1.1  lukem 	 * First, loop through /etc/ttys, if needed, to initialize the
     64  1.1  lukem 	 * top of the tty slots, since ftpd has no tty.
     65  1.1  lukem 	 */
     66  1.1  lukem 	if (topslot < 0) {
     67  1.1  lukem 		topslot = 0;
     68  1.1  lukem 		while (getttyent() != (struct ttyent *)NULL)
     69  1.1  lukem 			topslot++;
     70  1.1  lukem 	}
     71  1.1  lukem 	if ((topslot < 0) || ((fd < 0)
     72  1.1  lukem 	    && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
     73  1.1  lukem 	    	return;
     74  1.1  lukem 
     75  1.1  lukem 	/*
     76  1.1  lukem 	 * Now find a slot that's not in use...
     77  1.1  lukem 	 */
     78  1.1  lukem 	(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
     79  1.1  lukem 
     80  1.1  lukem 	while (1) {
     81  1.1  lukem 		if (read(fd, &ubuf, sizeof(UTMP)) == sizeof(UTMP)) {
     82  1.1  lukem 			if (!ubuf.ut_name[0]) {
     83  1.1  lukem 				(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
     84  1.1  lukem 				break;
     85  1.1  lukem 			}
     86  1.1  lukem 			topslot++;
     87  1.1  lukem 		} else {
     88  1.1  lukem 			(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)),
     89  1.1  lukem 			    SEEK_SET);
     90  1.1  lukem 			break;
     91  1.1  lukem 		}
     92  1.1  lukem 	}
     93  1.1  lukem 
     94  1.1  lukem 	(void)write(fd, ut, sizeof(UTMP));
     95  1.1  lukem }
     96  1.1  lukem 
     97  1.1  lukem int
     98  1.1  lukem logout(line)
     99  1.1  lukem 	const char *line;
    100  1.1  lukem {
    101  1.1  lukem 	UTMP ut;
    102  1.1  lukem 	int rval;
    103  1.1  lukem 
    104  1.1  lukem 	rval = 0;
    105  1.1  lukem 	if (fd < 0)
    106  1.1  lukem 		return(rval);
    107  1.1  lukem 
    108  1.1  lukem 	(void)lseek(fd, 0, SEEK_SET);
    109  1.1  lukem 
    110  1.1  lukem 	while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
    111  1.1  lukem 		if (!ut.ut_name[0]
    112  1.1  lukem 		    || strncmp(ut.ut_line, line, UT_LINESIZE))
    113  1.1  lukem 			continue;
    114  1.1  lukem 		memset(ut.ut_name, 0, UT_NAMESIZE);
    115  1.1  lukem 		memset(ut.ut_host, 0, UT_HOSTSIZE);
    116  1.1  lukem 		(void)time(&ut.ut_time);
    117  1.1  lukem 		(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
    118  1.1  lukem 		(void)write(fd, &ut, sizeof(UTMP));
    119  1.1  lukem 		rval = 1;
    120  1.1  lukem 	}
    121  1.1  lukem 	return(rval);
    122  1.1  lukem }
    123