xref: /netbsd-src/libexec/ftpd/logutmp.c (revision 2b8aaed8cd498f784adff8b449eee3eb8e00c2a3)
1*2b8aaed8Splunky /*	$NetBSD: logutmp.c,v 1.12 2011/09/16 16:13:17 plunky Exp $	*/
28cc604a3Slukem 
38aad99ceSlukem /*
48aad99ceSlukem  * Portions Copyright (c) 1988, 1993
58aad99ceSlukem  *	The Regents of the University of California.  All rights reserved.
68e6ab883Sagc  *
78e6ab883Sagc  * Redistribution and use in source and binary forms, with or without
88e6ab883Sagc  * modification, are permitted provided that the following conditions
98e6ab883Sagc  * are met:
108e6ab883Sagc  * 1. Redistributions of source code must retain the above copyright
118e6ab883Sagc  *    notice, this list of conditions and the following disclaimer.
128e6ab883Sagc  * 2. Redistributions in binary form must reproduce the above copyright
138e6ab883Sagc  *    notice, this list of conditions and the following disclaimer in the
148e6ab883Sagc  *    documentation and/or other materials provided with the distribution.
158e6ab883Sagc  * 3. Neither the name of the University nor the names of its contributors
168e6ab883Sagc  *    may be used to endorse or promote products derived from this software
178e6ab883Sagc  *    without specific prior written permission.
188e6ab883Sagc  *
198e6ab883Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208e6ab883Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218e6ab883Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228e6ab883Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238e6ab883Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248e6ab883Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258e6ab883Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268e6ab883Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278e6ab883Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288e6ab883Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298e6ab883Sagc  * SUCH DAMAGE.
308e6ab883Sagc  */
318e6ab883Sagc 
328e6ab883Sagc /*
338aad99ceSlukem  * Portions Copyright (c) 1996, Jason Downs.  All rights reserved.
348aad99ceSlukem  *
358aad99ceSlukem  * Redistribution and use in source and binary forms, with or without
368aad99ceSlukem  * modification, are permitted provided that the following conditions
378aad99ceSlukem  * are met:
388aad99ceSlukem  * 1. Redistributions of source code must retain the above copyright
398aad99ceSlukem  *    notice, this list of conditions and the following disclaimer.
408aad99ceSlukem  * 2. Redistributions in binary form must reproduce the above copyright
418aad99ceSlukem  *    notice, this list of conditions and the following disclaimer in the
428aad99ceSlukem  *    documentation and/or other materials provided with the distribution.
438aad99ceSlukem  *
449f1aac5bSagc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
459f1aac5bSagc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
469f1aac5bSagc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
479f1aac5bSagc  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
489f1aac5bSagc  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
499f1aac5bSagc  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
509f1aac5bSagc  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
519f1aac5bSagc  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
528aad99ceSlukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
538aad99ceSlukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
548aad99ceSlukem  * SUCH DAMAGE.
558aad99ceSlukem  */
568aad99ceSlukem 
578cc604a3Slukem #include <sys/cdefs.h>
588cc604a3Slukem #ifndef lint
59*2b8aaed8Splunky __RCSID("$NetBSD: logutmp.c,v 1.12 2011/09/16 16:13:17 plunky Exp $");
608cc604a3Slukem #endif /* not lint */
618cc604a3Slukem 
628aad99ceSlukem #include <sys/types.h>
6355803244Slukem #include <sys/param.h>
648aad99ceSlukem 
658aad99ceSlukem #include <fcntl.h>
668aad99ceSlukem #include <stdio.h>
678aad99ceSlukem #include <stdlib.h>
688aad99ceSlukem #include <string.h>
698aad99ceSlukem #include <ttyent.h>
708aad99ceSlukem #include <unistd.h>
718aad99ceSlukem #include <utmp.h>
723bab95ceStacha #ifdef SUPPORT_UTMPX
733bab95ceStacha #include <utmpx.h>
743bab95ceStacha #endif
758aad99ceSlukem #include <util.h>
768aad99ceSlukem 
7755803244Slukem #include "extern.h"
7855803244Slukem 
7920480d6fSjdolecek #ifdef SUPPORT_UTMP
808aad99ceSlukem typedef struct utmp UTMP;
818aad99ceSlukem 
828aad99ceSlukem static int fd = -1;
838aad99ceSlukem static int topslot = -1;
848aad99ceSlukem 
858aad99ceSlukem /*
868aad99ceSlukem  * Special versions of login()/logout() which hold the utmp file open,
878aad99ceSlukem  * for use with ftpd.
888aad99ceSlukem  */
898aad99ceSlukem 
908aad99ceSlukem void
ftpd_login(const struct utmp * ut)9155803244Slukem ftpd_login(const struct utmp *ut)
928aad99ceSlukem {
938aad99ceSlukem 	UTMP ubuf;
948aad99ceSlukem 
958aad99ceSlukem 	/*
968aad99ceSlukem 	 * First, loop through /etc/ttys, if needed, to initialize the
978aad99ceSlukem 	 * top of the tty slots, since ftpd has no tty.
988aad99ceSlukem 	 */
998aad99ceSlukem 	if (topslot < 0) {
1008aad99ceSlukem 		topslot = 0;
101*2b8aaed8Splunky 		while (getttyent() != NULL)
1028aad99ceSlukem 			topslot++;
1038aad99ceSlukem 	}
1048aad99ceSlukem 	if ((topslot < 0) || ((fd < 0)
1058aad99ceSlukem 	    && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
1068aad99ceSlukem 		return;
1078aad99ceSlukem 
1088aad99ceSlukem 	/*
1098aad99ceSlukem 	 * Now find a slot that's not in use...
1108aad99ceSlukem 	 */
1118aad99ceSlukem 	(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
1128aad99ceSlukem 
1138aad99ceSlukem 	while (1) {
1148aad99ceSlukem 		if (read(fd, &ubuf, sizeof(UTMP)) == sizeof(UTMP)) {
1158aad99ceSlukem 			if (!ubuf.ut_name[0]) {
1168aad99ceSlukem 				(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
1178aad99ceSlukem 				break;
1188aad99ceSlukem 			}
1198aad99ceSlukem 			topslot++;
1208aad99ceSlukem 		} else {
1218aad99ceSlukem 			(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)),
1228aad99ceSlukem 			    SEEK_SET);
1238aad99ceSlukem 			break;
1248aad99ceSlukem 		}
1258aad99ceSlukem 	}
1268aad99ceSlukem 
1278aad99ceSlukem 	(void)write(fd, ut, sizeof(UTMP));
1288aad99ceSlukem }
1298aad99ceSlukem 
1308aad99ceSlukem int
ftpd_logout(const char * line)13155803244Slukem ftpd_logout(const char *line)
1328aad99ceSlukem {
1338aad99ceSlukem 	UTMP ut;
1348aad99ceSlukem 	int rval;
1358aad99ceSlukem 
1368aad99ceSlukem 	rval = 0;
1378aad99ceSlukem 	if (fd < 0)
1388aad99ceSlukem 		return(rval);
1398aad99ceSlukem 
1408aad99ceSlukem 	(void)lseek(fd, 0, SEEK_SET);
1418aad99ceSlukem 
1428aad99ceSlukem 	while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
1438aad99ceSlukem 		if (!ut.ut_name[0]
1448aad99ceSlukem 		    || strncmp(ut.ut_line, line, UT_LINESIZE))
1458aad99ceSlukem 			continue;
1468aad99ceSlukem 		memset(ut.ut_name, 0, UT_NAMESIZE);
1478aad99ceSlukem 		memset(ut.ut_host, 0, UT_HOSTSIZE);
1488aad99ceSlukem 		(void)time(&ut.ut_time);
1498aad99ceSlukem 		(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
1508aad99ceSlukem 		(void)write(fd, &ut, sizeof(UTMP));
1518aad99ceSlukem 		rval = 1;
1528aad99ceSlukem 	}
1538aad99ceSlukem 	return(rval);
1548aad99ceSlukem }
15520480d6fSjdolecek #endif /* SUPPORT_UTMP */
1563bab95ceStacha 
1573bab95ceStacha #ifdef SUPPORT_UTMPX
1583bab95ceStacha /*
1593bab95ceStacha  * special version of loginx which updates utmpx only.
1603bab95ceStacha  */
1613bab95ceStacha void
ftpd_loginx(const struct utmpx * ut)1623bab95ceStacha ftpd_loginx(const struct utmpx *ut)
1633bab95ceStacha {
1643bab95ceStacha 	(void)pututxline(ut);
1653bab95ceStacha }
166ea7965ebSchristos 
167ea7965ebSchristos int
ftpd_logoutx(const char * line,int status,int mode)168ea7965ebSchristos ftpd_logoutx(const char *line, int status, int mode)
169ea7965ebSchristos {
170ea7965ebSchristos 	return logoutx(line, status, mode);
171ea7965ebSchristos }
1723bab95ceStacha #endif
173