home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / NextLibrary / PrivateFrameworks / Uucp.framework / Versions / A / Resources / contrib / tstout.c < prev    next >
C/C++ Source or Header  |  1992-03-04  |  5KB  |  159 lines

  1. /* tstout.c
  2.    Put together by Ian Lance Taylor <ian@airs.com>
  3.  
  4.    This program is used to logout a program run by the tstuu program.
  5.    I needed this because on Ultrix 4.0 I can't get the uucp program
  6.    to run without invoking it via /bin/login and having it start up
  7.    as a shell.  If I don't do it this way, it gets a SIGSEGV trap
  8.    for some reason.  Most systems probably don't need to do things
  9.    this way.  It will only work on BSD systems anyhow, I suspect.
  10.  
  11.    The code for this comes from "UNIX Network Programming" by W.
  12.    Richard Stevens, Prentice-Hall 1990.  Most of it is from 4.3BSD, as
  13.    noted in the comments.
  14.  
  15.    This program must run suid to root.
  16.    */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. #include <sys/types.h>
  22. #include <sys/file.h>
  23. #include <sys/time.h>
  24. #include <sys/stat.h>
  25. #include <utmp.h>
  26.  
  27. static int logout P((const char *zdev));
  28. static void logwtmp P((const char *zdev, const char *zname,
  29.               const char *zhost));
  30.  
  31. int
  32. main (argc, argv)
  33.      int argc;
  34.      char **argv;
  35. {
  36.   char *z;
  37.  
  38.   if (argc != 2
  39.       || strncmp (argv[1], "/dev/", sizeof "/dev/" - 1) != 0)
  40.     {
  41.       fprintf (stderr, "Usage: tstout device\n");
  42.       exit (EXIT_FAILURE);
  43.     }
  44.  
  45.   z = argv[1] + 5;
  46.  
  47.   if (logout (z))
  48.     logwtmp (z, "", "");
  49.  
  50.   chmod (argv[1], 0666);
  51.   chown (argv[1], 0, 0);
  52.  
  53.   *z = 'p';
  54.   chmod (argv[1], 0666);
  55.   chown (argv[1], 0, 0);
  56.  
  57.   exit (EXIT_SUCCESS);
  58. }
  59.  
  60. /*
  61.  * Copyright (c) 1988 The Regents of the University of California.
  62.  * All rights reserved.
  63.  *
  64.  * Redistribution and use in source and binary forms are permitted
  65.  * provided that the above copyright notice and this paragraph are
  66.  * duplicated in all such forms and that any documentation,
  67.  * advertising materials, and other materials related to such
  68.  * distribution and use acknowledge that the software was developed
  69.  * by the University of California, Berkeley.  The name of the
  70.  * University may not be used to endorse or promote products derived
  71.  * from this software without specific prior written permission.
  72.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  73.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  74.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  75.  */
  76.  
  77. #if defined(LIBC_SCCS) && !defined(lint)
  78. static char sccsid[] = "@(#)logout.c    5.2 (Berkeley) 2/17/89";
  79. #endif /* LIBC_SCCS and not lint */
  80.  
  81. #define    UTMPFILE    "/etc/utmp"
  82.  
  83. /* 0 on failure, 1 on success */
  84.  
  85. static int
  86. logout(line)
  87.     register const char *line;
  88. {
  89.     register FILE *fp;
  90.     struct utmp ut;
  91.     int rval;
  92.     time_t time();
  93.  
  94.     if (!(fp = fopen(UTMPFILE, "r+")))
  95.         return(0);
  96.     rval = 0;
  97.     while (fread((char *)&ut, sizeof(struct utmp), 1, fp) == 1) {
  98.         if (!ut.ut_name[0] ||
  99.             strncmp(ut.ut_line, line, sizeof(ut.ut_line)))
  100.             continue;
  101.         bzero(ut.ut_name, sizeof(ut.ut_name));
  102.         bzero(ut.ut_host, sizeof(ut.ut_host));
  103.         (void)time((time_t *)&ut.ut_time);
  104.         (void)fseek(fp, (long)-sizeof(struct utmp), L_INCR);
  105.         (void)fwrite((char *)&ut, sizeof(struct utmp), 1, fp);
  106.         (void)fseek(fp, (long)0, L_INCR);
  107.         rval = 1;
  108.     }
  109.     (void)fclose(fp);
  110.     return(rval);
  111. }
  112.  
  113. /*
  114.  * Copyright (c) 1988 The Regents of the University of California.
  115.  * All rights reserved.
  116.  *
  117.  * Redistribution and use in source and binary forms are permitted
  118.  * provided that the above copyright notice and this paragraph are
  119.  * duplicated in all such forms and that any documentation,
  120.  * advertising materials, and other materials related to such
  121.  * distribution and use acknowledge that the software was developed
  122.  * by the University of California, Berkeley.  The name of the
  123.  * University may not be used to endorse or promote products derived
  124.  * from this software without specific prior written permission.
  125.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  126.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  127.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  128.  */
  129.  
  130. #if defined(LIBC_SCCS) && !defined(lint)
  131. static char sccsid[] = "@(#)logwtmp.c    5.2 (Berkeley) 9/20/88";
  132. #endif /* LIBC_SCCS and not lint */
  133.  
  134. #define    WTMPFILE    "/usr/adm/wtmp"
  135.  
  136. static void
  137. logwtmp(line, name, host)
  138.      const char *line, *name, *host;
  139. {
  140.     struct utmp ut;
  141.     struct stat buf;
  142.     int fd;
  143.     time_t time();
  144.     char *strncpy();
  145.  
  146.     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
  147.         return;
  148.     if (!fstat(fd, &buf)) {
  149.         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
  150.         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
  151.         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
  152.         (void)time((time_t *)&ut.ut_time);
  153.         if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
  154.             sizeof(struct utmp))
  155.             (void)ftruncate(fd, buf.st_size);
  156.     }
  157.     (void)close(fd);
  158. }
  159.