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 / perl / group.chk < prev    next >
Text File  |  1992-03-10  |  5KB  |  143 lines

  1. #!/bin/sh -- need to mention perl here to avoid recursion
  2. 'true' || eval 'exec perl -S $0 $argv:q';
  3. eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
  4. & eval 'exec /usr/bin/perl -S $0 $argv:q'
  5.         if 0;
  6.  
  7. #
  8. #   group.chk.pl
  9. #
  10. # Perl version: composer@chem.bu.edu
  11. # Based on original shell script, group.chk
  12. #
  13. #  Check group file -- /etc/group -- for incorrect number of fields,
  14. # duplicate groups, non-alphanumeric group names, and non-numeric group
  15. # id's.
  16. #
  17. #   Mechanism:  Group.check uses awk to ensure that each line of the group
  18. # has 4 fields, as well as examining each line for any duplicate groups or
  19. # any duplicate user id's in a given group by using "sort -u" to ferret
  20. # out any duplications.  It also checks to make sure that the password
  21. # field (the second one) is a "*", meaning the group has no password (a
  22. # group password is usually not necessary because each member listed on 
  23. # the line has all the privilages that the group has.)  All results are
  24. # echoed to standard output.  Finally it ensures that the group names
  25. # are alphanumeric, that the group id's are numeric, and that there are
  26. # no blank lines.  For yellow pages groups, it does the same checking,
  27. # but in order to get a listing of all members of the groups, it does a
  28. # "ypcat group".
  29. #
  30. #   The /etc/group file has a very specific format, making the task
  31. # fairly simple.  Normally it has lines with 4 fields, each field
  32. # separated by a colon (:).  The first field is the group name, the second
  33. # field is the encrypted password (an asterix (*) means the group has no
  34. # password, otherwise the first two characters are the salt), the third
  35. # field is the group id number, and the fourth field is a list of user
  36. # ids in the group.  If a line begins with a plus sign (+), it is a yellow
  37. # pages entry.  See group(5) for more information.
  38. #
  39.  
  40. # should get below config stuff from cops.cf file
  41. package main;
  42.  
  43. die "Usage: $0\n" if @ARGV;
  44.  
  45. require 'pathconf.pl';
  46.  
  47. #   Used for Sun C2 security group file.  FALSE (default) will flag
  48. # valid C2 group syntax as an error, TRUE attempts to validate it.
  49. # Thanks to Pete Troxell for pointing this out.
  50. #
  51. # moved to cops.cf
  52.  
  53. package group_chk;
  54.  
  55. $etc_group = $'GROUP || '/etc/group';
  56.  
  57. # Testing $etc_group for potential problems....
  58. open (Group, "< $etc_group") || warn "$0: Can't open $etc_group: $!\n";
  59. &chk_group_file_format('Group');
  60. close Group;
  61.  
  62. # Testing ypcat group for potential problems
  63. $yp=0;
  64. if (-s $'YPCAT && -x _) {
  65.     open(YGroup, "$'YPCAT group 2>/dev/null |")
  66.     || die "$0: Can't popen $'YPCAT: $!\n";
  67.     $yp=1;
  68.     &chk_group_file_format('YGroup');
  69.     close(YGroup);
  70. }
  71.  
  72. # usage: &chk_group_file_format('Filehandle-name');
  73. # skips over lines that begin with "+:"
  74. # It really should check for correct yellow pages syntax....
  75. #
  76. # this routine checks lines read from a filehandle for potential format
  77. # problems .. should be matching group(5)
  78. #
  79. # checks for duplicate users in a group as it reads the lines instead
  80. # of after (as the original shell script does)
  81.  
  82. sub chk_group_file_format {
  83.     local($file) = @_;
  84.     local($W) = "Warning!  $file file,";
  85.     undef %groups;
  86.  
  87.     while (<$file>) {
  88.     # should really check for correct YP syntax
  89.     next if /^[-+]:/;   # skipping YP lines for now
  90.     print "$W line $., is blank\n" if /^\s*$/;
  91.     ($group,$pass,$gid,$users) = split(?:?);
  92.     $groups{$group}++;   # keep track of dups
  93.     print "$W line $., does not have 4 fields:\n\t$_" if (@_ != 4);
  94.     print "$W line $., nonalphanumeric group name:\n\t$_"
  95.         if $group !~ /^[_A-Za-z0-9-]+$/;
  96.     if ($pass && $pass ne '*') {
  97.         if ( ! $C2 || $yp ) {
  98.         print "$W line $., group has password:\n\t$_"
  99.             if length($pass) == 13;
  100.         } else {
  101.         print "$W line $., group has invalid field for C2:\n\t$_"
  102.             if $pass ne "#\$$user";
  103.         }
  104.     }
  105.     # print "$W line $., nonnumeric group id: $_" if $_[2] !~ /^\d+$/;
  106.     if ($gid !~ /^\d+$/) {
  107.         if ($uid < 0) {
  108.             print "$W line $., negative group id:\n\t$_";
  109.             }
  110.         else { print "$W line $., nonnumeric group id:\n\t$_"; }
  111.         }
  112.  
  113.     # look for duplicate users in a group
  114.     # kinda ugly, but it works .. and I have too much other work right
  115.     # now to clean it up.  maybe later.. ;-)
  116.     chop($users);    # down here, 'cos split gets rid of final null fields
  117.     @users = sort split(/\s*,\s*/, $users);
  118.     # %users = # of times user is in group, $dup_user = duplicate found
  119.     undef %users;  $dup_user=0;
  120.     grep(!($users{$_}++) && 0, @users);
  121.     for (keys %users) {
  122.         (print "Warning!  Group $group has duplicate user(s):\n"),
  123.         $dup_user=1 if !$dup_user && $users{$_} > 1;
  124.         print "$_ " if $users{$_} > 1;
  125.     }
  126.     print "\n" if $dup_user;
  127.  
  128.     }
  129.     # find duplicate group names 
  130.     # not the best way, but it works..
  131.     # boy, this is ugly too .. but, not as bad as above.. :)
  132.     $dup_warned = 0;
  133.     for (sort keys %groups) {
  134.     (print "Warning!  Duplicate Group(s) found in $file:\n"), $dup_warned++
  135.         if !$dup_warned && $groups{$_} > 1;
  136.     print "$_ " if $groups{$_} > 1;
  137.     }
  138.     print "\n" if $dup_warned;
  139. }
  140.  
  141. 1;
  142. # end
  143.