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