Home | History | Annotate | Line # | Download | only in ftpd
logutmp.c revision 1.10
      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  *
     42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
     43  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     44  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     45  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
     46  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     47  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     48  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     49  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     52  * SUCH DAMAGE.
     53  */
     54 
     55 #include <sys/types.h>
     56 #include <sys/param.h>
     57 
     58 #include <fcntl.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <ttyent.h>
     63 #include <unistd.h>
     64 #include <utmp.h>
     65 #ifdef SUPPORT_UTMPX
     66 #include <utmpx.h>
     67 #endif
     68 #include <util.h>
     69 
     70 #include "extern.h"
     71 
     72 #ifdef SUPPORT_UTMP
     73 typedef struct utmp UTMP;
     74 
     75 static int fd = -1;
     76 static int topslot = -1;
     77 
     78 /*
     79  * Special versions of login()/logout() which hold the utmp file open,
     80  * for use with ftpd.
     81  */
     82 
     83 void
     84 ftpd_login(const struct utmp *ut)
     85 {
     86 	UTMP ubuf;
     87 
     88 	/*
     89 	 * First, loop through /etc/ttys, if needed, to initialize the
     90 	 * top of the tty slots, since ftpd has no tty.
     91 	 */
     92 	if (topslot < 0) {
     93 		topslot = 0;
     94 		while (getttyent() != (struct ttyent *)NULL)
     95 			topslot++;
     96 	}
     97 	if ((topslot < 0) || ((fd < 0)
     98 	    && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
     99 		return;
    100 
    101 	/*
    102 	 * Now find a slot that's not in use...
    103 	 */
    104 	(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
    105 
    106 	while (1) {
    107 		if (read(fd, &ubuf, sizeof(UTMP)) == sizeof(UTMP)) {
    108 			if (!ubuf.ut_name[0]) {
    109 				(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
    110 				break;
    111 			}
    112 			topslot++;
    113 		} else {
    114 			(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)),
    115 			    SEEK_SET);
    116 			break;
    117 		}
    118 	}
    119 
    120 	(void)write(fd, ut, sizeof(UTMP));
    121 }
    122 
    123 int
    124 ftpd_logout(const char *line)
    125 {
    126 	UTMP ut;
    127 	int rval;
    128 
    129 	rval = 0;
    130 	if (fd < 0)
    131 		return(rval);
    132 
    133 	(void)lseek(fd, 0, SEEK_SET);
    134 
    135 	while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
    136 		if (!ut.ut_name[0]
    137 		    || strncmp(ut.ut_line, line, UT_LINESIZE))
    138 			continue;
    139 		memset(ut.ut_name, 0, UT_NAMESIZE);
    140 		memset(ut.ut_host, 0, UT_HOSTSIZE);
    141 		(void)time(&ut.ut_time);
    142 		(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
    143 		(void)write(fd, &ut, sizeof(UTMP));
    144 		rval = 1;
    145 	}
    146 	return(rval);
    147 }
    148 #endif /* SUPPORT_UTMP */
    149 
    150 #ifdef SUPPORT_UTMPX
    151 /*
    152  * special version of loginx which updates utmpx only.
    153  */
    154 void
    155 ftpd_loginx(const struct utmpx *ut)
    156 {
    157 	(void)pututxline(ut);
    158 }
    159 
    160 int
    161 ftpd_logoutx(const char *line, int status, int mode)
    162 {
    163 	return logoutx(line, status, mode);
    164 }
    165 #endif
    166