home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / inetutil.1 / inetutil / inetutils-1.1 / ftpd / logwtmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-22  |  4.6 KB  |  150 lines

  1. /*
  2.  SYSVish version:
  3.  
  4. Copyright (C) 1996 Free Software Foundation, Inc.
  5. This file is part of the GNU C Library.
  6. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  7.  
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12.  
  13. The GNU C Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with the GNU C Library; see the file COPYING.LIB.  If
  20. not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22.  
  23.  * BSDish version:
  24.  *
  25.  * Copyright (c) 1988, 1993
  26.  *    The Regents of the University of California.  All rights reserved.
  27.  *
  28.  * Redistribution and use in source and binary forms, with or without
  29.  * modification, are permitted provided that the following conditions
  30.  * are met:
  31.  * 1. Redistributions of source code must retain the above copyright
  32.  *    notice, this list of conditions and the following disclaimer.
  33.  * 2. Redistributions in binary form must reproduce the above copyright
  34.  *    notice, this list of conditions and the following disclaimer in the
  35.  *    documentation and/or other materials provided with the distribution.
  36.  * 3. All advertising materials mentioning features or use of this software
  37.  *    must display the following acknowledgement:
  38.  *    This product includes software developed by the University of
  39.  *    California, Berkeley and its contributors.
  40.  * 4. Neither the name of the University nor the names of its contributors
  41.  *    may be used to endorse or promote products derived from this software
  42.  *    without specific prior written permission.
  43.  *
  44.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  45.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  48.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54.  * SUCH DAMAGE.
  55.  *
  56.  */
  57.  
  58. #ifndef lint
  59. static char sccsid[] = "@(#)logwtmp.c    8.1 (Berkeley) 6/4/93";
  60. #endif /* not lint */
  61.  
  62. #ifdef HAVE_CONFIG_H
  63. #include <config.h>
  64. #endif
  65.  
  66. #include <sys/types.h>
  67. #include <sys/time.h>
  68. #include <sys/stat.h>
  69.  
  70. #include <fcntl.h>
  71. #include <utmp.h>
  72. #include <unistd.h>
  73. #include <stdio.h>
  74. #include <string.h>
  75. #include "extern.h"
  76.  
  77. #if !defined (_PATH_WTMP) && defined (WTMP_FILE)
  78. #define _PATH_WTMP WTMP_FILE
  79. #endif
  80.  
  81. /*
  82.  * Modified version of logwtmp that holds wtmp file open
  83.  * after first call, for use with ftp (which may chroot
  84.  * after login, but before logout).
  85.  */
  86. void
  87. logwtmp(line, name, host)
  88.     const char *line, *name, *host;
  89. {
  90.   struct utmp ut;
  91.  
  92.   /* Set information in new entry.  */
  93.   bzero (&ut, sizeof (ut));
  94. #if _HAVE_UT_TYPE - 0
  95.   ut.ut_type = USER_PROCESS;
  96. #endif
  97.   strncpy (ut.ut_line, line, sizeof ut.ut_line);
  98.   strncpy (ut.ut_name, name, sizeof ut.ut_name);
  99. #if _HAVE_UT_HOST - 0
  100.   strncpy (ut.ut_host, host, sizeof ut.ut_host);
  101. #endif
  102.  
  103. #if _HAVE_UT_TV - 0
  104.   gettimeofday (&ut.ut_tv, NULL);
  105. #else
  106.   time (&ut.ut_time);
  107. #endif
  108.  
  109. #if defined (HAVE_UTMPNAME) && defined (HAVE_SETUTENT_R)
  110.   /* XXX I think frobbing the details of DATA is GNU libc specific.  */
  111.   {
  112.     static struct utmp_data data = { -1 };
  113.  
  114.     if (data.ut_fd < 0)
  115.       /* Open the file.  */
  116.       {
  117.     /* Tell that we want to use the WTMP file.  */
  118.     if (utmpname (_PATH_WTMP) == 0)
  119.       return;
  120.  
  121.     /* Open WTMP file.  */
  122.     setutent_r (&data);
  123.       }
  124.  
  125.     /* Position at end of file.  */
  126.     data.loc_utmp = lseek (data.ut_fd, 0, SEEK_END);
  127.     if (data.loc_utmp == -1)
  128.       return;
  129.  
  130.     /* Write the entry.  */
  131.     pututline_r (&ut, &data);
  132.   }
  133. #else
  134.   /* Do things the old way.  */
  135.   {  
  136.     struct utmp ut;
  137.     struct stat buf;
  138.     static int fd = -1;
  139.  
  140.     if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
  141.         return;
  142.     if (fstat(fd, &buf) == 0) {
  143.         if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
  144.             sizeof(struct utmp))
  145.             (void)ftruncate(fd, buf.st_size);
  146.     }
  147.   }
  148. #endif
  149. }
  150.