home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  2.0 KB  |  93 lines

  1. /*
  2.  * Copyright 1991, John F. Haugh II and Chip Rosenthal
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #ifndef lint
  13. static    char    sccsid[] = "@(#)console.c    3.1    07:47:49    9/17/91";
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #ifndef BSD
  18. # include <string.h>
  19. #else
  20. # include <strings.h>
  21. #endif
  22.  
  23. extern    char    *getdef_str();
  24.  
  25. /*
  26.  * tty - return 1 if the "tty" is a console device, else 0.
  27.  *
  28.  * Note - we need to take extreme care here to avoid locking out root logins
  29.  * if something goes awry.  That's why we do things like call everything a
  30.  * console if the consoles file can't be opened.  Because of this, we must
  31.  * warn the user to protect against the remove of the consoles file since
  32.  * that would allow an unauthorized root login.
  33.  */
  34.  
  35. int
  36. console (tty)
  37. char    *tty;
  38. {
  39.     FILE    *fp;
  40.     char    buf[BUFSIZ], *console, *s;
  41.  
  42.     /*
  43.      * If the CONSOLE configuration definition isn't given, call
  44.      * everything a valid console.
  45.      */
  46.  
  47.     if ((console = getdef_str("CONSOLE")) == NULL)
  48.         return 1;
  49.  
  50.     /*
  51.      * If this isn't a filename, then it is a ":" delimited list of
  52.      * console devices upon which root logins are allowed.
  53.      */
  54.  
  55.     if (*console != '/') {
  56.         console = strcpy(buf,console);
  57.         while ((s = strtok(console,":")) != NULL) {
  58.             if (strcmp(s,tty) == 0)
  59.                 return 1;
  60.  
  61.             console = NULL;
  62.         }
  63.         return 0;
  64.     }
  65.  
  66.     /*
  67.      * If we can't open the console list, then call everything a
  68.      * console - otherwise root will never be allowed to login.
  69.      */
  70.  
  71.     if ((fp = fopen(console,"r")) == NULL)
  72.         return 1;
  73.  
  74.     /*
  75.      * See if this tty is listed in the console file.
  76.      */
  77.  
  78.     while (fgets(buf,sizeof(buf),fp) != NULL) {
  79.         buf[strlen(buf)-1] = '\0';
  80.         if (strcmp(buf,tty) == 0) {
  81.             (void) fclose(fp);
  82.             return 1;
  83.         }
  84.     }
  85.  
  86.     /*
  87.      * This tty isn't a console.
  88.      */
  89.  
  90.     (void) fclose(fp);
  91.     return 0;
  92. }
  93.