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

  1. :
  2. #
  3. #   passswd.chk
  4. #
  5. #  Check passsword file -- /etc/passswd -- for incorrect number of fields,
  6. # duplicate uid's, non-alphanumeric uids, and non-numeric group id's.
  7. #
  8. # Awk part from _The AWK Programming Language_, page 78
  9. #
  10. #  Mechanism:  Passwd.check uses awk to ensure that each line of the file
  11. # has 7 fields, as well as examining the file for any duplicate users
  12. # by using "sort -u".  It also checks to make sure that the password
  13. # field (the second one) is either a "*", meaning the group has no password,
  14. # or a non-null field (which would mean that the account has a null
  15. # password.)  It then checks to ensure that all uids are alphanumeric,
  16. # and that all user id numbers are indeed numeric.  For yellow pages
  17. # passwords, it does the same checking, but in order to get a listing of
  18. # all members of the password file, it does a "ypcat passwd > ./$$" and
  19. # uses that temporary file for a passfile.  It removes the tmp file after
  20. # using it, of course.
  21. #   The /etc/passwd file has a very specific format, making the task
  22. # fairly simple.  Normally it has lines with 7 fields, each field
  23. # separated by a colon (:).  The first field is the user id, the second
  24. # field is the encrypted password (an asterix (*) means the group has no
  25. # password, otherwise the first two characters are the salt), the third
  26. # field is the user id number, the fourth field is the group id number,
  27. # the fifth field is the GECOS field (basically holds miscellaneous
  28. # information, varying from site to site), the sixth field is the home
  29. # directory of the user, and lastly the seventh field is the login shell
  30. # of the user.  No blank lines should be present.  Uid's will be flagged
  31. # if over 8 chars, unless the $OVER_8 variable (line 50) is set to "YES".
  32. #   If a line begins with a plus sign (+), it is a yellow pages entry.
  33. # See passwd(5) for more information, if this applies to your site.
  34. #
  35. AWK=/bin/awk
  36. TEST=/bin/test
  37. ECHO=/bin/echo
  38. SORT=/usr/bin/sort
  39. UNIQ=/usr/bin/uniq
  40. RM=/bin/rm
  41. YPCAT=/usr/bin/ypcat
  42.  
  43. #   Used for Sun C2 security group file.  FALSE (default) will flag
  44. # valid C2 passwd syntax as an error, TRUE attempts to validate it.
  45. # Thanks to Pete Troxell for pointing this out.
  46. C2=FALSE
  47.  
  48. #  Some systems allow long uids; set this to "YES", if so (thanks
  49. # to Pete Shipley (lot of petes around here, eh?)):
  50. OVER_8=NO
  51.  
  52. #
  53. # Important files:
  54. etc_passwd=/etc/passwd
  55. yp_passwd=./$$
  56.  
  57. yp=false
  58.  
  59. # Testing $etc_passwd for potential problems....
  60. if $TEST -s $YPCAT ; then
  61.     # thanks to brent chapman!
  62.         $YPCAT passwd | sort -t: +2n -3 +0 -1 > $yp_passwd
  63.     if $TEST $? -eq 0 ; then
  64.         yp=true
  65.         fi
  66.     fi
  67.  
  68. result=`$AWK -F: '{print $1}' $etc_passwd | $SORT |$UNIQ -d`
  69. if $TEST "$result" ; then
  70.     $ECHO "Warning!  Duplicate uid(s) found in $etc_passwd:"
  71.     $ECHO $result
  72.     fi
  73.  
  74.  
  75. #   First line is for a yellow pages entry in the password file.
  76. # It really should check for correct yellow pages syntax....
  77. $AWK 'BEGIN {FS = ":" }
  78.     {
  79.     if (substr($1,1,1) != "+") {
  80.         if ($0 ~ /^[     ]*$/) {
  81.             printf("Warning!  Password file, line %d, is blank\n", NR)
  82.             }
  83.         else {
  84.             if (NF != 7) {
  85.                 printf("Warning!  Password file, line %d, does not have 7 fields: \n\t%s\n", NR, $0)
  86.                 }
  87.             if ($1 !~ /[A-Za-z0-9]/) {
  88.                 printf("Warning!  Password file, line %d, nonalphanumeric login: \n\t%s\n", NR, $0)
  89.                 }
  90.             if (length($1) > 8 && "'$OVER_8'" != "YES") {
  91.                 printf("Warning!  Password file, line %d, uid > 8 chars\n\t%s\n", NR, $0)
  92.                 }
  93.             if ($2 == "") {
  94.                 printf("Warning!  Password file, line %d, no password: \n\t%s\n", NR, $0)
  95.                 }
  96.             if ("'$C2'" == "TRUE" && $2 ~ /^##/ && "##"$1 != $2) {
  97.                 printf("Warning!  Password file, line %d, invalid password field for C2: \n\t%s\n", NR, $0)
  98.                 }
  99.             if ($3 !~ /^[0-9]/) {
  100.                 if ($3 < 0) {
  101.                     printf("Warning!  Password file, line %d, negative user id: \n\t%s\n", NR, $0)
  102.                     }
  103.                 else {
  104.                     printf("Warning!  Password file, line %d, nonnumeric user id: \n\t%s\n", NR, $0)
  105.                     }
  106.                 }
  107.             if ($3 == "0" && $1 != "root") {
  108.                 printf("Warning!  Password file, line %d, user %s has uid = 0 and is not root\n\t%s\n", NR, $1, $0)
  109.                 }
  110.             if ($4 !~ /[0-9]/) {
  111.                 printf("Warning!  Password file, line %d, nonnumeric group id: \n\t%s\n", NR, $0)
  112.                 }
  113.             if ($6 !~ /^\//) {
  114.                 printf("Warning!  Password file, line %d, invalid login directory: \n\t%s\n", NR, $0)
  115.                 }
  116.             }
  117.         }
  118.     }' $etc_passwd
  119.  
  120. #
  121. # Test yellow pages passwords as well
  122. if $TEST "$yp" = "true"
  123.     then
  124.     yresult=`$AWK -F: '{print $1}' $yp_passwd | $SORT |$UNIQ -d`
  125.     if $TEST "$yresult"
  126.         then
  127.         $ECHO "Warning!  Duplicate uid(s) found in yellow page passwords:"
  128.         $ECHO $yresult
  129.     fi
  130.  
  131.     $AWK 'BEGIN {FS = ":" }
  132.             {
  133.         if ($0 ~ /^[     ]*$/) {
  134.             printf("Warning!  YPassword file, line %d, is blank\n", NR)
  135.             }
  136.         else {
  137.             if (NF != 7) {
  138.                 printf("Warning!  YPassword file, line %d, does not have 7 fields: \n\t%s\n", NR, $0)
  139.                 }
  140.             if ($1 !~ /[A-Za-z0-9]/) {
  141.                 printf("Warning!  YPassword file, line %d, nonalphanumeric login: \n\t%s\n", NR, $0)
  142.                 }
  143.             if (length($1) > 8 && "'$OVER_8'" != "YES") {
  144.                 printf("Warning!  YPassword file, line %d, uid > 8 chars\n\t%s\n", NR, $0)
  145.                 }
  146.             if ($2 == "") {
  147.                 printf("Warning!  YPassword file, line %d, no password: \n\t%s\n", NR, $0)
  148.                 }
  149.             if ($3 !~ /^[0-9]/) {
  150.                 if ($3 < 0) {
  151.                     printf("Warning!  YPassword file, line %d, negative user id: \n\t%s\n", NR, $0)
  152.                 }
  153.                 else {
  154.                     printf("Warning!  YPassword file, line %d, nonnumeric user id: \n\t%s\n", NR, $0)
  155.                     }
  156.                 }
  157.             if ($3 == "0" && $1 != "root") {
  158.                 printf("Warning!  YPassword file, line %d, user %s has uid = 0 and is not root\n\t%s\n", NR, $1, $0)
  159.                 }
  160.             if ($4 !~ /[0-9]/) {
  161.                 printf("Warning!  YPassword file, line %d, nonnumeric group id: \n\t%s\n", NR, $0)
  162.                 }
  163.             if ($6 !~ /^\//) {
  164.                 printf("Warning!  YPassword file, line %d, invalid login directory: \n\t%s\n", NR, $0)
  165.                 }
  166.             }
  167.             }' $yp_passwd
  168.     fi
  169.  
  170. $RM -f $yp_passwd
  171.  
  172. # end
  173.