home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Networking / wu-ftpd-2.4.2b13-MIHS / src / logwtmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-03  |  5.3 KB  |  176 lines

  1. /* Copyright (c) 1988 The Regents of the University of California. All rights
  2.  * reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are
  6.  * met: 1. Redistributions of source code must retain the above copyright
  7.  * notice, this list of conditions and the following disclaimer. 2.
  8.  * Redistributions in binary form must reproduce the above copyright notice,
  9.  * this list of conditions and the following disclaimer in the documentation
  10.  * and/or other materials provided with the distribution. 3. All advertising
  11.  * materials mentioning features or use of this software must display the
  12.  * following acknowledgement: This product includes software developed by the
  13.  * University of California, Berkeley and its contributors. 4. Neither the
  14.  * name of the University nor the names of its contributors may be used to
  15.  * endorse or promote products derived from this software without specific
  16.  * prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  19.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21.  * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
  22.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28.  * SUCH DAMAGE.
  29.  *
  30.  */
  31.  
  32. #ifndef lint
  33. static char sccsid[] = "@(#)logwtmp.c   5.7 (Berkeley) 2/25/91";
  34. static char rcsid[] = "@(#)$Id: logwtmp.c,v 1.9 1997/03/02 01:32:23 sob Exp $";
  35. #endif /* not lint */
  36.  
  37. #include "config.h"
  38.  
  39. #include <sys/types.h>
  40. #ifdef BSD
  41. #include <sys/time.h>
  42. #else
  43. #include <time.h>
  44. #endif
  45. #include <sys/stat.h>
  46. #include <fcntl.h>
  47. #include <utmp.h>
  48. #ifdef SVR4
  49. #if !(defined(LINUX) || defined(__hpux)|| defined(_AIX)) 
  50. #include <utmpx.h>
  51. #ifndef _SCO_DS
  52. #include <sac.h>
  53. #endif
  54. #endif
  55. #endif
  56. #ifdef BSD
  57. #include <strings.h>
  58. #else
  59. #include <string.h>
  60. #endif
  61. #ifdef SYSSYSLOG
  62. #include <sys/syslog.h>
  63. #else
  64. #include <syslog.h>
  65. #endif
  66. #ifdef __FreeBSD__
  67. #include <netinet/in.h>
  68. #include <arpa/inet.h>
  69. #include <netdb.h>
  70. #endif
  71.  
  72. #include "pathnames.h"
  73.  
  74. static int fd = -1;
  75. #ifdef SVR4
  76. static int fdx = -1;
  77. #endif
  78.  
  79. /* Modified version of logwtmp that holds wtmp file open after first call,
  80.  * for use with ftp (which may chroot after login, but before logout). */
  81.  
  82. #ifdef __STDC__
  83. logwtmp(char *line, char *name, char *host)
  84. #else
  85. logwtmp(line,name,host)
  86. char *line;
  87. char *name;
  88. char *host;
  89. #endif
  90. {
  91.     struct stat buf;
  92.     struct utmp ut;
  93.  
  94. #ifdef SVR4
  95. #if !(defined(LINUX) || defined(__hpux) || defined (_AIX))
  96.     struct utmpx utx;
  97.  
  98.     if (fdx < 0 && (fdx = open(WTMPX_FILE, O_WRONLY | O_APPEND, 0)) < 0) {
  99.         syslog(LOG_ERR, "wtmpx %s %m", WTMPX_FILE);
  100.         return;
  101.     }
  102.  
  103.     if (fstat(fdx, &buf) == 0) {
  104.         memset((void *)&utx, '\0', sizeof(utx));
  105.         (void) strncpy(utx.ut_user, name, sizeof(utx.ut_user));
  106.         (void) strncpy(utx.ut_id, "ftp", sizeof(utx.ut_id));
  107.         (void) strncpy(utx.ut_line, line, sizeof(utx.ut_line));
  108.         (void) strncpy(utx.ut_host, host, sizeof(utx.ut_host));
  109.         utx.ut_syslen = strlen(utx.ut_host)+1;
  110.         utx.ut_pid = getpid();
  111.         (void) time (&utx.ut_tv.tv_sec);
  112.         if (name && *name)
  113.             utx.ut_type = USER_PROCESS;
  114.         else
  115.             utx.ut_type = DEAD_PROCESS;
  116.         utx.ut_exit.e_termination = 0;
  117.         utx.ut_exit.e_exit = 0;
  118.         if (write(fdx, (char *) &utx, sizeof(struct utmpx)) !=
  119.             sizeof(struct utmpx))
  120.       (void) ftruncate(fdx, buf.st_size);
  121.       }
  122. #endif
  123. #endif
  124.  
  125. #ifdef __FreeBSD__
  126.       if (strlen(host) > UT_HOSTSIZE) {
  127.         struct hostent *hp = gethostbyname(host);
  128.  
  129.         if (hp != NULL) {
  130.             struct in_addr in;
  131.  
  132.             memmove(&in, hp->h_addr, sizeof(in));
  133.             host = inet_ntoa(in);
  134.         } else
  135.             host = "invalid hostname";
  136.       }
  137. #endif
  138.  
  139.     if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY | O_APPEND, 0)) < 0) {
  140.         syslog(LOG_ERR, "wtmp %s %m", _PATH_WTMP);
  141.         return;
  142.     }
  143.     if (fstat(fd, &buf) == 0) {
  144. #ifdef UTMAXTYPE
  145.         memset((void *)&ut, 0, sizeof(ut));
  146.         (void) strncpy(ut.ut_user, name, sizeof(ut.ut_user));
  147. #ifdef LINUX
  148.         (void) strncpy(ut.ut_id, "", sizeof(ut.ut_id));
  149. #else
  150.         (void) strncpy(ut.ut_id, "ftp", sizeof(ut.ut_id));
  151. #endif
  152.         (void) strncpy(ut.ut_line, line, sizeof(ut.ut_line));
  153.         ut.ut_pid = getpid();
  154.         if (name && *name)
  155.             ut.ut_type = USER_PROCESS;
  156.         else
  157.             ut.ut_type = DEAD_PROCESS;
  158. #ifndef LINUX
  159.         ut.ut_exit.e_termination = 0;
  160.         ut.ut_exit.e_exit = 0;
  161. #endif
  162. #else
  163.         (void) strncpy(ut.ut_line, line, sizeof(ut.ut_line));
  164.         (void) strncpy(ut.ut_name, name, sizeof(ut.ut_name));
  165. #endif /* UTMAXTYPE */
  166. #ifdef HAVE_UT_UT_HOST  /* does have host in utmp */
  167.         (void) strncpy(ut.ut_host, host, sizeof(ut.ut_host));
  168. #endif
  169.  
  170.         (void) time(&ut.ut_time);
  171.         if (write(fd, (char *) &ut, sizeof(struct utmp)) !=
  172.             sizeof(struct utmp))
  173.               (void) ftruncate(fd, buf.st_size);
  174.     }
  175. }
  176.