home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part04 / checkptys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  5.9 KB  |  252 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/file.h>
  5. #include "config/devmty.h"
  6. #include "config/devsty.h"
  7. #include "config/posix.h" /* XXX: why? */
  8. #include "config/ptybin.h"
  9. #include "config/ptydir.h"
  10. #include "config/ptyext.h"
  11. #include "config/ptygroup.h"
  12. #include "config/ptymodes.h"
  13. #include "config/ptyopts.h"
  14. #include "config/sessconnfile.h"
  15. #include "config/sessfile.h"
  16. #include <utmp.h>
  17. #include "config/utmpfile.h"
  18. #include "config/wtmpfile.h"
  19.  
  20. char ptybin[] = PTYBIN;
  21. char ptydir[] = PTYDIR;
  22. char sessconnnow[] = SESSCONNNOW_FILE;
  23. char sessconnlog[] = SESSCONNLOG_FILE;
  24. char sessnow[] = SESSNOW_FILE;
  25. char sesslog[] = SESSLOG_FILE;
  26. char utmp[] = UTMP_FILE;
  27. char wtmp[] = WTMP_FILE;
  28.  
  29. char devmty[sizeof(DEVMTY) + 5] = DEVMTY;
  30. char devsty[sizeof(DEVSTY) + 5] = DEVSTY;
  31. char pty1[] = PTYEXT1;
  32. char pty2[] = PTYEXT2;
  33.  
  34. void aack(s,t)
  35. char *s;
  36. char *t;
  37. {
  38.  printf("aack! %s: %s\n",s,t);
  39. }
  40.  
  41. void checkbin(s,level)
  42. char *s;
  43. int level; /* 0 script 1 normal 2 setgid 3 setuid */
  44. {
  45.  struct stat st;
  46.  
  47.  if (stat(s,&st) == -1)
  48.   {
  49.    aack("executable does not exist",s);
  50.    return;
  51.   }
  52.  if (!(st.st_mode & 001))
  53.    aack("program isn't world-executable",s);
  54.  switch(st.st_mode & 06000)
  55.   {
  56.    case 0:
  57.      if (level == 2)
  58.        aack("program should be setgid but isn't",s);
  59.      if (level == 3)
  60.        aack("program should be setuid but isn't",s);
  61.      break;
  62.    case 06000:
  63.    case 04000:
  64.      if (level == 0)
  65.        aack("SHELL SCRIPT IS SETUID! FIX IMMEDIATELY!",s);
  66.      if (level == 1)
  67.        aack("program is SETUID, shouldn't be",s);
  68.      if (level == 2)
  69.        aack("program is SETUID, should be setgid instead",s);
  70.      break;
  71.    case 02000:
  72.      if (level == 0)
  73.        aack("SHELL SCRIPT IS SETGID! FIX IMMEDIATELY!",s);
  74.      if (level == 1)
  75.        aack("program is setgid, shouldn't be",s);
  76.      if (level == 3)
  77.        aack("program is setgid, should be setuid instead",s);
  78.      break;
  79.    default:
  80.      aack("computer doesn't understand binary arithmetic",s);
  81.   }
  82.  if (st.st_mode & 002)
  83.    aack("program is WORLD-writable",s);
  84.  if ((level == 2) && (st.st_gid != PTYGROUP))
  85.    aack("program should be setgid to tty group but isn't",s);
  86. }
  87.  
  88. void testpath(s)
  89. char *s;
  90. {
  91.  char *t;
  92.  struct stat st;
  93.  char old;
  94.  
  95.  if (*s != '/')
  96.   {
  97.    aack("path does not start with a slash",s);
  98.    return;
  99.   }
  100.  t = s + 1;
  101.  for (;;)
  102.   {
  103.    if ((*t == '/') || (*t == 0))
  104.     {
  105.      old = *t;
  106.      *t = 0;
  107.      if (stat(*s ? s : "/",&st) == -1)
  108.        aack("cannot stat component of path",*s ? s : "/");
  109.      else
  110.       {
  111.        if (st.st_mode & 022)
  112.      aack("component directory is WORLD- and group-writable",*s ? s : "/");
  113.        else if (st.st_mode & 002)
  114.      aack("component directory is WORLD-writable",*s ? s : "/");
  115.        else if (st.st_mode & 020)
  116.      aack("component directory is group-writable",*s ? s : "/");
  117.       }
  118.      *t = old;
  119.     }
  120.    if (!*t)
  121.      break;
  122.    ++t;
  123.   }
  124. }
  125.  
  126. void testpty(fnm,fns,unusedptyowner)
  127. char *fnm;
  128. char *fns;
  129. int unusedptyowner;
  130. {
  131.  struct stat stm;
  132.  struct stat sts;
  133.  int fdm;
  134.  
  135.  if (stat(fnm,&stm) == -1)
  136.    return;
  137.  if (stat(fns,&sts) == -1)
  138.   {
  139.    aack("pty master has no corresponding slave",fnm);
  140.    return;
  141.   }
  142.  
  143.  fdm = open(fnm,O_RDWR);
  144.  if (stm.st_mode & 0777 != 0666)
  145.   {
  146.    aack("pty master has weird mode",fnm);
  147.   }
  148.  if (fdm == -1)
  149.   {
  150.    printf("pty master %s in use, slave owned by uid %d\n",fnm,sts.st_uid);
  151.    if (sts.st_mode & 006)
  152.      aack("pty slave allows WORLD access",fns);
  153.    if (sts.st_gid != PTYGROUP)
  154.      aack("pty slave group does not match standard tty group",fns);
  155.    if (sts.st_mode & 020)
  156.      printf("pty slave %s messages on\n",fns);
  157.    if (sts.st_mode & 0100)
  158.      printf("pty slave %s biff on\n",fns);
  159.   }
  160.  else
  161.   {
  162.    if (sts.st_uid != unusedptyowner)
  163.      aack("unused pty slave not owned by standard unused tty owner",fns);
  164.    close(fdm);
  165.   }
  166. }
  167.  
  168. main()
  169. {
  170.  int p1;
  171.  int p2;
  172.  
  173.  printf("Testing main pty directory %s...\n",ptydir);
  174.  testpath(ptydir);
  175.  printf("Testing utmp file %s...\n",utmp);
  176.  testpath(utmp);
  177.  printf("Testing wtmp file %s...\n",wtmp);
  178.  testpath(wtmp);
  179.  printf("Testing session log file %s...\n",sesslog);
  180.  testpath(sesslog);
  181.  printf("Testing current session file %s...\n",sessnow);
  182.  testpath(sessnow);
  183.  printf("Testing session-connection log file %s...\n",sessconnlog);
  184.  testpath(sessconnlog);
  185.  printf("Testing current session-connection file %s...\n",sessconnnow);
  186.  testpath(sessconnnow);
  187.  printf("Testing pty binary directory %s...\n",ptybin);
  188.  testpath(ptybin);
  189.  
  190.  for (p1 = 0;pty1[p1];++p1)
  191.    for (p2 = 0;pty2[p2];++p2)
  192.     {
  193.      devmty[sizeof(DEVMTY) - 1] = pty1[p1];
  194.      devmty[sizeof(DEVMTY)] = pty2[p2];
  195.      devsty[sizeof(DEVSTY) - 1] = pty1[p1];
  196.      devsty[sizeof(DEVSTY)] = pty2[p2];
  197.      testpty(devmty,devsty,geteuid());
  198.     }
  199.  
  200.  printf("Checking for actual pty-related binaries in %s...\n",ptybin);
  201.  if (chdir(ptybin) == -1)
  202.   {
  203.    aack("cannot switch to pty binary directory",ptybin);
  204.   }
  205.  else
  206.   {
  207.    checkbin("argv0",1);
  208.    checkbin("biff",1);
  209.    checkbin("checkptys",1);
  210.    checkbin("condom",0);
  211.    checkbin("ctrlv",1);
  212.    checkbin("disconnect",3);
  213.    checkbin("excloff",1);
  214.    checkbin("exclon",1);
  215.    checkbin("lock",1);
  216.    checkbin("mesg",1);
  217.    checkbin("nobuf",0);
  218.    checkbin("pty",3);
  219.    checkbin("reconnect",3);
  220.    checkbin("script",0);
  221.    checkbin("script.tidy",0);
  222.    checkbin("sess",0);
  223.    checkbin("sesskill",3);
  224.    checkbin("sesslist",3);
  225.    checkbin("sessmenu",1);
  226.    checkbin("sessname",3);
  227.    checkbin("sesswhere",1);
  228.    checkbin("sesswho",1);
  229.    checkbin("tiocsti",1);
  230.    checkbin("tplay",1);
  231.    checkbin("trecord",1);
  232.    checkbin("tscript",0);
  233.    checkbin("tty",1);
  234.    checkbin("ttydetach",1);
  235.    checkbin("ttyprotect",0);
  236.    checkbin("users",1);
  237.    checkbin("utmpinit",1);
  238.    checkbin("waitfor",1);
  239.    checkbin("wall",2);
  240.    checkbin("who",1);
  241.    checkbin("whoami",1);
  242.    checkbin("write",2);
  243.    checkbin("wtmprotate",0);
  244.    checkbin("sessrotate",0);
  245.    checkbin("sclogrotate",0);
  246.    checkbin("sessnowinit",0);
  247.    checkbin("scnowinit",0);
  248.   }
  249.  
  250.  exit(0);
  251. }
  252.