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

  1. :
  2. #
  3. #   group.chk
  4. #
  5. #  Check group file -- /etc/group -- for incorrect number of fields,
  6. # duplicate groups, non-alphanumeric group names, and non-numeric group
  7. # id's.
  8. #
  9. # Awk part based on _passwd_ from _The AWK Programming Language_, page 78
  10. #
  11. #   Mechanism:  Group.check uses awk to ensure that each line of the group
  12. # has 4 fields, as well as examining each line for any duplicate groups or
  13. # any duplicate user id's in a given group by using "sort -u" to ferret
  14. # out any duplications.  It also checks to make sure that the password
  15. # field (the second one) is a "*", meaning the group has no password (a
  16. # group password is usually not necessary because each member listed on 
  17. # the line has all the privilages that the group has.)  All results are
  18. # echoed to standard output.  Finally it ensures that the group names
  19. # are alphanumeric, that the group id's are numeric, and that there are
  20. # no blank lines.  For yellow pages groups, it does the same checking,
  21. # but in order to get a listing of all members of the groups, it does a
  22. # "ypcat group > ./$$" and uses that temporary file for a groupfile.
  23. # It removes the tmp file after using it, of course.
  24. #   The /etc/group file has a very specific format, making the task
  25. # fairly simple.  Normally it has lines with 4 fields, each field
  26. # separated by a colon (:).  The first field is the group name, the second
  27. # field is the encrypted password (an asterix (*) means the group has no
  28. # password, otherwise the first two characters are the salt), the third
  29. # field is the group id number, and the fourth field is a list of user
  30. # ids in the group.  If a line begins with a plus sign (+), it is a yellow
  31. # pages entry.  See group(5) for more information.
  32. #
  33. #
  34. AWK=/bin/awk
  35. SED=/bin/sed
  36. ECHO=/bin/echo
  37. TEST=/bin/test
  38. SORT=/usr/bin/sort
  39. UNIQ=/usr/bin/uniq
  40. YPCAT=/usr/bin/ypcat
  41. RM=/bin/rm
  42.  
  43. #   Used for Sun C2 security group file.  FALSE (default) will flag
  44. # valid C2 group syntax as an error, TRUE attempts to validate it.
  45. # Thanks to Pete Troxell for pointing this out.
  46. C2=FALSE
  47.  
  48. etc_group=/etc/group
  49. yp_group=./$$
  50. yp=false
  51.  
  52. if $TEST -f $YPCAT
  53.     then
  54. if $TEST -s $YPCAT
  55.     then
  56.     $YPCAT group > $yp_group
  57.     if $TEST $? -eq 0
  58.         then
  59.         yp=true
  60.         fi
  61.     fi
  62. fi
  63.  
  64. # Testing $etc_group for potential problems....
  65.  
  66. #   First line is for a yellow pages entry in the group file.
  67. # It really should check for correct yellow pages syntax....
  68. $AWK 'BEGIN {FS = ":" }
  69.     {
  70.     if (substr($1,1,1) != "+") { \
  71.         if ($0 ~ /^[     ]*$/) {
  72.             printf("Warning!  Group file, line %d, is blank\n", NR)
  73.             }
  74.         else {
  75.             if (NF != 4) {
  76.                 printf("Warning!  Group file, line %d, does not have 4 fields: %s\n", NR, $0)
  77.                 } \
  78.             if ($1 !~ /[A-Za-z0-9]/) {
  79.                 printf("Warning!  Group file, line %d, nonalphanumeric user id: %s\n", NR, $0)
  80.                 } \
  81.             if ($2 != "" && $2 != "*" && $2 != "!") {
  82.                 if ("'$C2'" != "TRUE") {
  83.                     if (length($2) == 13)
  84.                         printf("Warning!  Group file, line %d, group has password: %s\n", NR, $0)
  85.                         }
  86.                     else {
  87.                         if ("#$"$1 != $2)
  88.                             printf("Warning!  Group file, line %d, group has invalid field for C2:\n%s\n", NR, $0)
  89.                         } \
  90.                     } \
  91.                 if ($3 !~ /[0-9]/) {
  92.                     printf("Warning!  Group file, line %d, nonnumeric group id: %s\n", NR, $0)
  93.                     }
  94.                 }
  95.             }
  96.         }' $etc_group
  97.  
  98. #
  99. #  Look for duplications in groups in $etc_group
  100. #
  101. result=`$AWK -F: '{print $1}' $etc_group | $SORT |$UNIQ -d`
  102. if $TEST "$result"
  103.     then
  104.     $ECHO "Warning!  Duplicate Group(s) found in $etc_group:"
  105.     $ECHO $result
  106. fi
  107.  
  108. #
  109. #   Next, check for duplicate users in a group in /etc/group.  Let
  110. # awk do all the work (thanks, adri!)
  111. #
  112.  
  113. # Ignore all groups with less than two members.
  114. #
  115. awk -F: 'split($4, users, ",") > 1 {
  116.     ct = 0
  117.     for (i in users) {
  118.         curuser = users[i]
  119.         for (j in users) {
  120.             if (j > i && curuser == users[j]) {
  121.                 if (ct++ == 0) print "Warning!  Group "$1" has duplicate user(s):"
  122.                     print curuser
  123.                 }
  124.             }
  125.         }
  126.     }' $etc_group
  127.  
  128.  
  129. #
  130. # Test yellow pages groups as well
  131. if $TEST "$yp" = "true"
  132.     then
  133. $AWK 'BEGIN {FS = ":" }
  134.     {
  135.     if ($0 ~ /^[     ]*$/) {
  136.         printf("Warning!  YGroup file, line %d, is blank\n", NR)
  137.         }
  138.     else {
  139.         if (NF != 4) {
  140.             printf("Warning!  YGroup file, line %d, does not have 4 fields: %s\n", NR, $0)
  141.             } \
  142.         if ($1 !~ /[A-Za-z0-9]/) {
  143.             printf("Warning!  YGroup file, line %d, nonalphanumeric user id: %s\n", NR, $0)
  144.             } \
  145.         if ($2 != "" && $2 != "*") {
  146.             if (length($2) == 13)
  147.                 printf("Warning!  YGroup file, line %d, group has password: %s\n", NR, $0)
  148.             } \
  149.         if ($3 !~ /[0-9]/) {
  150.             printf("Warning!  YGroup file, line %d, nonnumeric group id: %s\n", NR, $0)
  151.             }
  152.         }
  153.     }' $yp_group
  154.  
  155. #
  156. #  Look for duplications in groups in yellow pages groups
  157. #
  158.     yresult=`$AWK -F: '{print $1}' $yp_group | $SORT |$UNIQ -d`
  159.     if $TEST "$yresult"
  160.         then
  161.         $ECHO "Warning!  Duplicate Group(s) found in yellow pages group:"
  162.         $ECHO $result
  163.     fi
  164. #
  165. #   Next, check for duplicate users in a group in yellow groups.  Let
  166. # awk do all the work (thanks, adri!) 
  167.  
  168. # ignore all groups with one member.
  169. #
  170.     awk -F: 'split($4, users, ",") > 1 {
  171.         ct = 0
  172.         for (i in users) {
  173.             curuser = users[i]
  174.                 for (j in users) {
  175.                     if (j > i && curuser == users[j]) {
  176.                         if (ct++ == 0) 
  177.                             print "Warning!  YGroup "$1" has duplicate user(s):"
  178.                         print curuser
  179.                         }
  180.                     }
  181.                 }
  182.         }' $yp_group
  183.  
  184. fi
  185.  
  186. $RM -f $yp_group
  187.  
  188. # end
  189.