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 / docs / readme.C2 / group.file.chk < prev    next >
Text File  |  1992-03-10  |  6KB  |  201 lines

  1. #!/bin/sh
  2. #
  3. #   group.file.chk
  4. #
  5. # Awk part based on _passwd_ from _The AWK Programming Language_, page 78
  6. #
  7. #   Mechanism:  Group.check uses awk to ensure that each line of the group
  8. # has 4 fields, as well as examining each line for any duplicate groups or
  9. # any duplicate user id's in a given group by using "sort -u" to ferret
  10. # out any duplications.  It also checks to make sure that the password
  11. # field (the second one) is a "*", meaning the group has no password (a
  12. # group password is usually not necessary because each member listed on 
  13. # the line has all the privilages that the group has.)  All results are
  14. # echoed to standard output.  Finally it ensures that the group names
  15. # are alphanumeric, that the group id's are numeric, and that there are
  16. # no blank lines.  For yellow pages groups, it does the same checking,
  17. # but in order to get a listing of all members of the groups, it does a
  18. # "ypcat group > ./$$" and uses that temporary file for a groupfile.
  19. # It removes the tmp file after using it, of course.
  20. #   The /etc/group file has a very specific format, making the task
  21. # fairly simple.  Normally it has lines with 4 fields, each field
  22. # separated by a colon (:).  The first field is the group name, the second
  23. # field is the encrypted password (an asterix (*) means the group has no
  24. # password, otherwise the first two characters are the salt), the third
  25. # field is the group id number, and the fourth field is a list of user
  26. # ids in the group.  If a line begins with a plus sign (+), it is a yellow
  27. # pages entry.  See group(5) for more information.
  28. #   The SUN /etc/security/group.adjunct file also has a very specific
  29. # format, makeing the check task simple. Each entry has 2 fields separated 
  30. # by a colon (:). THe first field is the user name which matches the user
  31. # name contained in the /etc/group file. The second field is the encrypted
  32. # password (an asterix (*) means the group has no password, otherwise the 
  33. # first two characters are the salt). The password contained in the 
  34. # /etc/group file is comprised of the #$user_id where the user_id matches
  35. # the entry of the first field in both group files.
  36. #
  37.  
  38. #
  39. # Parameters
  40. #
  41. group_file=$1
  42. group_adjunct_file=$2
  43. SUN_SECURITY=$3
  44.  
  45. #
  46. # Utilities
  47. #
  48. AWK=/bin/awk
  49. DIFF=/usr/bin/diff
  50. ECHO=/bin/echo
  51. JOIN=/usr/bin/join
  52. RM=/bin/rm
  53. SORT=/usr/bin/sort
  54. TEST=/bin/test
  55. UNIQ=/usr/bin/uniq
  56.  
  57. #
  58. # Important files:
  59. #
  60. join_group_1=./grp$$.1.join
  61. join_group_2=./grp$$.2.join
  62. sort_group=./grp$$.sort
  63. sort_secure_group=./sgrp$$.sort
  64.  
  65. #
  66. # Testing the group file for problems
  67. #
  68. result=`$AWK -F: '{print $1}' $group_file | $SORT |$UNIQ -d`
  69. if $TEST "$result"
  70.     then
  71.     $ECHO "Warning!  Duplicate gid(s) found in group file:"
  72.     for USER in $result
  73.     do
  74.         $ECHO "    $USER"
  75.     done
  76. fi
  77.  
  78. #
  79. #   First line is for a yellow pages entry in the group file.
  80. # It really should check for correct yellow pages syntax....
  81. #
  82. $AWK 'BEGIN {FS = ":" } {
  83.     if (substr($1,1,1) != "+") { \
  84.     if ($0 ~ /^[     ]*$/) { printf("Warning!  Group file, line %d, is blank\n", NR) } else {
  85.     if (NF != 4) { printf("Warning!  Group file, line %d, does not have 4 fields: \n\t%s\n", NR, $0) } \
  86.     if ($1 !~ /[A-Za-z0-9]/) {
  87.         printf("Warning!  Group file, line %d, nonalphanumeric user id: \n\t%s\n", NR, $0) } \
  88.     if ($2 != "" && $2 != "*") {
  89.         if ("'$SUN_SECURITY'" != "TRUE")
  90.             printf("Warning!  Group file, line %d, has password: \n\t%s\n", NR, $0)
  91.         else {
  92.             if ("#$"$1 != $2)
  93.                 printf("Warning!  Group file, line %d, invalid password field for SUN C2 Security: \n\t%s\n", NR, $0) } \
  94.         } \
  95.     if ($3 !~ /[0-9]/) {
  96.         printf("Warning!  Group file, line %d, nonnumeric group id: \n\t%s\n", NR, $0) \
  97.     }}}} ' $group_file
  98.  
  99. #
  100. # Ignore all groups with less than two members.
  101. #
  102. awk -F: '
  103.     split($4, users, ",") > 1 {
  104.         ct = 0
  105.         for (i in users) {
  106.             curuser = users[i]
  107.             for (j in users) {
  108.                 if (j > i && curuser == users[j]) {
  109.                     if (ct++ == 0) print "Warning!  Group "$1" has duplicate user(s):"
  110.                     print curuser
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     ' $group_file
  116.  
  117. #
  118. # Perform checks on the security enhanced version of SUNOS
  119. #
  120. if $TEST $SUN_SECURITY = "TRUE"
  121.     then
  122.     result=`$AWK -F: '{print $1}' $group_adjunct_file | $SORT -t: | $UNIQ -d`
  123.     if $TEST "$result"
  124.         then
  125.         $ECHO
  126.         $ECHO "Warning!  Duplicate uid(s) found in group adjunct file:"
  127.         for USER in $result
  128.         do
  129.             $ECHO "    $USER"
  130.         done
  131.     fi
  132.     #
  133.     # Check that for each entry in the group file that there is a matching
  134.     # entry in the group.adjunct file.
  135.     #
  136.     $SORT -t: -o $sort_group $group_file
  137.     $SORT -t: -o $sort_secure_group $group_adjunct_file
  138.     $JOIN -t: $sort_group $sort_secure_group > $join_group_1
  139.     $JOIN -t: -a1 $sort_group $sort_secure_group > $join_group_2
  140.     result=`$DIFF $join_group_1 $join_group_2`
  141.     if $TEST "$result"
  142.         then
  143.         $ECHO
  144.         $ECHO "Warning!  Matching record(s) in group adjunct file not found for"
  145.         $ECHO "these records in group file:"
  146.         PREV=$$
  147.         for USER in $result
  148.         do
  149.             if $TEST $PREV = ">"
  150.                 then
  151.                 $ECHO "    $USER"
  152.             fi
  153.             PREV=$USER
  154.         done
  155.     fi
  156.     #
  157.     # Check that for each entry in the group.adjunct file that there is a 
  158.     # matching entry in the group file.
  159.     #
  160.     $RM -f $join_group_2
  161.     $JOIN -t: -a2 $sort_group $sort_secure_group > $join_group_2
  162.     result=`$DIFF $join_group_1 $join_group_2`
  163.     if $TEST "$result"
  164.         then
  165.         $ECHO
  166.         $ECHO "Warning!  Matching record(s) in group file not found for"
  167.         $ECHO "these records in group adjunct file"
  168.         PREV=$$
  169.         for USER in $result
  170.         do
  171.             if $TEST $PREV = ">"
  172.                 then
  173.                 $ECHO "    $USER"
  174.             fi
  175.             PREV=$USER
  176.         done
  177.     fi
  178.     #
  179.     # Test the fields in the group.adjunct file for validity
  180.     #
  181.     $AWK 'BEGIN {FS = ":" } \
  182.         {if (substr($1,1,1) != "+") { \
  183.         if ($0 ~ /^[     ]*$/) { printf("\nWarning!  Group adjunct file, line %d, is blank\n", NR) } else {
  184.         if (NF != 2) {
  185.             printf("\nWarning!  Group adjunct file, line %d, does not have 2 fields: \n\t%s\n", NR, $0) } \
  186.         if ($1 !~ /[A-Za-z0-9]/) {
  187.             printf("\nWarning!  Group adjunct file, line %d, nonalphanumeric login: \n\t%s\n", NR, $0) } \
  188.         if ($2 != "" && $2 != "*") {
  189.             printf("\nWarning!  Group adjunct file, line %d, has password: \n\t%s\n", NR, $0) } \
  190.         }}}' $group_adjunct_file
  191. fi
  192.  
  193. #
  194. # Clean up after ourself
  195. #
  196. $RM -f $join_group_1
  197. $RM -f $join_group_2
  198. $RM -f $sort_group
  199. $RM -f $sort_secure_group
  200. # end
  201.