home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cops_104.zip / cops_104 / src / home.chk.c < prev    next >
C/C++ Source or Header  |  1992-03-10  |  3KB  |  98 lines

  1. /*
  2.  
  3. -------------------------------------------------------------------------
  4. Modification: Aug 2, 1989
  5. Author: Dan Farmer
  6.  
  7.   I made a minor change to this; it uses a bit mask instead of comparing
  8. to various ok modes.  Otherwise it is unchanged....
  9. -------------------------------------------------------------------------
  10.  
  11. Original Comment:
  12.  
  13. This was posted to comp.unix.wizards and net.sources, but I also wanted to
  14. send it here, both for those that don't read or get news, and so that it
  15. will be in the archives for posterity....
  16.  
  17. On the UNIX Security list, quite a while back, mention was made of
  18. problems that could occur when home directories of users are writable.
  19. (Installing |"some command" in ~uucp/.forward remotely and things like
  20. that.)  This prompted me to write the enclosed program, both to check
  21. for this, and to help protect users against themselves.
  22.  
  23. The program looks at all the home directories listed in /etc/passwd,
  24. and prints a message if they don't exist, are not directories, or
  25. their mode is not in the "table" of "OK" modes.  I'm using stat()
  26. instead of lstat(), so symbolic links are perfectly acceptable, as
  27. long as they point to directories....  This program should run on any
  28. version of UNIX that I can think of; if it doesn't, please let me
  29. know.
  30.  
  31. The list of good modes is, of course, subjective.  I initially used
  32. the first set, then added the second set based on the output of the
  33. first run.  I didn't add all the mismatched modes I found; just the
  34. ones that were fairly normal and that I didn't want to hear about....
  35.  
  36. The program is surprisingly (to me) fast.  It took under a second on
  37. our decently loaded VAX-11/785 running 4.3BSD with 501 passwd entries!
  38.  
  39. This program is placed in the public domain - you have only your
  40. conscience to stop you from saying "hey, look at this neat program I
  41. wrote"....
  42.  
  43.     Enjoy!
  44.  
  45. John Owens        Old Dominion University - Norfolk, Virginia, USA
  46. john@ODU.EDU        old arpa: john%odu.edu@RELAY.CS.NET
  47. +1 804 440 3915        old uucp: {seismo,harvard,sun,hoptoad}!xanth!john
  48. */
  49.  
  50. #include <pwd.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53.  
  54.  
  55. /* mask for modes.... (world writable) */
  56. #define DMODE 002
  57.  
  58. main(argc,argv)
  59. char **argv;
  60. {
  61.     register int mode;
  62.     register int *p;
  63.     struct passwd *pp;
  64.     static struct stat statb;
  65.  
  66.     if (argc != 1) {
  67.         printf("Usage: %s\n",argv[0]);
  68.         exit(1);
  69.     }
  70.  
  71.     while ((pp = getpwent()) != (struct passwd *)0) {
  72.         if (stat(pp->pw_dir,&statb) < 0) {
  73.         /*
  74.             perror(pp->pw_dir);
  75.         */
  76.             continue;
  77.         }
  78.  
  79.         if ((statb.st_mode & S_IFMT) != S_IFDIR) {
  80.             printf("Warning!  User %s's home directory %s is not a directory! (mode 0%o)\n",
  81.                 pp->pw_name,pp->pw_dir,statb.st_mode);
  82.             continue;
  83.         }
  84.  
  85.         mode = statb.st_mode & ~S_IFMT;
  86.  
  87.         if (!(mode & DMODE)) goto ok;
  88.  
  89.                 /* note that 3.3 will print 4 if needed */
  90.         printf("Warning!  User %s's home directory %s is mode 0%3.3o!\n",
  91.                pp->pw_name,pp->pw_dir,mode);
  92. ok:    ;
  93.     }
  94.  
  95.     exit(0);
  96.  
  97. }
  98.