genericunixprotocols/ftpsrv/src/logutmp.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 //
       
     2 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 // All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 //
       
     9 // Initial Contributors:
       
    10 // Nokia Corporation - initial contribution.
       
    11 //
       
    12 // Contributors:
       
    13 //
       
    14 // Description:
       
    15 //
       
    16 
       
    17 /*
       
    18  * Portions Copyright (c) 1988, 1993
       
    19  *	The Regents of the University of California.  All rights reserved.
       
    20  * Portions Copyright (c) 1996, Jason Downs.  All rights reserved.
       
    21  *
       
    22  * Redistribution and use in source and binary forms, with or without
       
    23  * modification, are permitted provided that the following conditions
       
    24  * are met:
       
    25  * 1. Redistributions of source code must retain the above copyright
       
    26  *    notice, this list of conditions and the following disclaimer.
       
    27  * 2. Redistributions in binary form must reproduce the above copyright
       
    28  *    notice, this list of conditions and the following disclaimer in the
       
    29  *    documentation and/or other materials provided with the distribution.
       
    30  * 3. All advertising materials mentioning features or use of this software
       
    31  *    must display the following acknowledgement:
       
    32  *	This product includes software developed by the University of
       
    33  *	California, Berkeley and its contributors.
       
    34  * 4. Neither the name of the University nor the names of its contributors
       
    35  *    may be used to endorse or promote products derived from this software
       
    36  *    without specific prior written permission.
       
    37  *
       
    38  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
       
    39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    41  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
       
    42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
       
    44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
       
    46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
       
    47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
       
    48  * SUCH DAMAGE.
       
    49  */
       
    50 
       
    51 /*
       
    52  * From: OpenBSD: logutmp.c,v 1.1 1996/06/18 10:09:23 downsj Exp
       
    53  * From: OpenBSD: logutmp.c,v 1.2 1998/07/13 02:11:17 millert Exp
       
    54  */
       
    55 char logutmp_rcsid[] =
       
    56   "$Id: logutmp.c,v 1.4 1999/07/16 01:12:54 dholland Exp $";
       
    57 
       
    58 #include <sys/types.h>
       
    59 
       
    60 #include <fcntl.h>
       
    61 #include <unistd.h>
       
    62 #include <stdlib.h>
       
    63 #include <utmp.h>
       
    64 #include <stdio.h>
       
    65 #include <string.h>
       
    66 //#ifndef __linux__
       
    67 //#include <ttyent.h>
       
    68 //#endif
       
    69 #include "extern.h"
       
    70 
       
    71 typedef struct utmp UTMP;
       
    72 
       
    73 #ifndef __linux__
       
    74 static int fd = -1;
       
    75 static int topslot = -1;
       
    76 #endif
       
    77 
       
    78 /*
       
    79  * Special versions of login()/logout() which hold the utmp file open,
       
    80  * for use with ftpd.
       
    81  */
       
    82 
       
    83 void
       
    84 login(const UTMP *ut)
       
    85 {
       
    86 #ifndef __linux__
       
    87 	UTMP ubuf;
       
    88 
       
    89 	/*
       
    90 	 * First, loop through /etc/ttys, if needed, to initialize the
       
    91 	 * top of the tty slots, since ftpd has no tty.
       
    92 	 */
       
    93 	if (topslot < 0) {
       
    94 		topslot = 0;
       
    95 	}
       
    96 	if ((topslot < 0) || ((fd < 0)
       
    97 	    && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) < 0))
       
    98 	    	return;
       
    99 
       
   100 	/*
       
   101 	 * Now find a slot that's not in use...
       
   102 	 */
       
   103 	(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
       
   104 
       
   105 	while (1) {
       
   106 		if (read(fd, &ubuf, sizeof(UTMP)) == sizeof(UTMP)) {
       
   107 			if (!ubuf.ut_name[0]) {
       
   108 				(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
       
   109 				break;
       
   110 			}
       
   111 			topslot++;
       
   112 		} else {
       
   113 			(void)lseek(fd, (off_t)(topslot * sizeof(UTMP)), SEEK_SET);
       
   114 			break;
       
   115 		}
       
   116 	}
       
   117 
       
   118 	(void)write(fd, ut, sizeof(UTMP));
       
   119 #else
       
   120 	(void)ut;
       
   121 #endif
       
   122 }
       
   123 
       
   124 int
       
   125 logout(register const char *line)
       
   126 {
       
   127 #ifndef __linux__
       
   128 	UTMP ut;
       
   129 	int rval;
       
   130 
       
   131 	rval = 0;
       
   132 	if (fd < 0)
       
   133 		return(rval);
       
   134 
       
   135 	(void)lseek(fd, 0, SEEK_SET);
       
   136 
       
   137 	while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
       
   138 		if (!ut.ut_name[0]
       
   139 		    || strncmp(ut.ut_line, line, UT_LINESIZE))
       
   140 			continue;
       
   141 		bzero(ut.ut_name, UT_NAMESIZE);
       
   142 		bzero(ut.ut_host, UT_HOSTSIZE);
       
   143 		(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
       
   144 		(void)write(fd, &ut, sizeof(UTMP));
       
   145 		rval = 1;
       
   146 	}
       
   147 	return(rval);
       
   148 #else
       
   149 	(void)line;
       
   150 	return 1;
       
   151 #endif
       
   152 }