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