home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / t / op / pwent.t < prev    next >
Text File  |  1999-07-27  |  3KB  |  138 lines

  1. #!./perl
  2.  
  3. BEGIN {
  4.     chdir 't' if -d 't';
  5.     unshift @INC, "../lib" if -d "../lib";
  6.     eval {my @n = getpwuid 0};
  7.     if ($@ && $@ =~ /(The \w+ function is unimplemented)/) {
  8.     print "1..0 # Skip: $1\n";
  9.     exit 0;
  10.     }
  11.     eval { require Config; import Config; };
  12.     my $reason;
  13.     if ($Config{'i_pwd'} ne 'define') {
  14.     $reason = '$Config{i_pwd} undefined';
  15.     }
  16.     elsif (not -f "/etc/passwd" ) { # Play safe.
  17.     $reason = 'no /etc/passwd file';
  18.     }
  19.  
  20.     if (not defined $where) {    # Try NIS.
  21.     foreach my $ypcat (qw(/usr/bin/ypcat /bin/ypcat /etc/ypcat)) {
  22.         if (-x $ypcat &&
  23.         open(PW, "$ypcat passwd 2>/dev/null |") &&
  24.         defined(<PW>)) {
  25.         $where = "NIS passwd";
  26.         undef $reason;
  27.         last;
  28.         }
  29.     }
  30.     }
  31.  
  32.     if (not defined $where) {    # Try NetInfo.
  33.     foreach my $nidump (qw(/usr/bin/nidump)) {
  34.         if (-x $nidump &&
  35.         open(PW, "$nidump passwd . 2>/dev/null |") &&
  36.         defined(<PW>)) {
  37.         $where = "NetInfo passwd";
  38.         undef $reason;
  39.         last;
  40.         }
  41.     }
  42.     }
  43.  
  44.     if (not defined $where) {    # Try local.
  45.     my $PW = "/etc/passwd";
  46.     if (-f $PW && open(PW, $PW) && defined(<PW>)) {
  47.         $where = $PW;
  48.         undef $reason;
  49.     }
  50.     }
  51.  
  52.     if ($reason) {    # Give up.
  53.     print "1..0 # Skip: $reason\n";
  54.     exit 0;
  55.     }
  56. }
  57.  
  58. # By now PW filehandle should be open and full of juicy password entries.
  59.  
  60. print "1..1\n";
  61.  
  62. # Go through at most this many users.
  63. # (note that the first entry has been read away by now)
  64. my $max = 25;
  65.  
  66. my $n = 0;
  67. my $tst = 1;
  68. my %perfect;
  69. my %seen;
  70.  
  71. while (<PW>) {
  72.     chomp;
  73.     my @s = split /:/;
  74.     my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s;
  75.     next if /^\+/; # ignore NIS includes
  76.     if (@s) {
  77.     push @{ $seen{$name_s} }, $.;
  78.     } else {
  79.     warn "# Your $where line $. is empty.\n";
  80.     next;
  81.     }
  82.     if ($n == $max) {
  83.     local $/;
  84.     my $junk = <PW>;
  85.     last;
  86.     }
  87.     # In principle we could whine if @s != 7 but do we know enough
  88.     # of passwd file formats everywhere?
  89.     if (@s == 7) {
  90.     @n = getpwuid($uid_s);
  91.     # 'nobody' et al.
  92.     next unless @n;
  93.     my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
  94.     # Protect against one-to-many and many-to-one mappings.
  95.     if ($name_s ne $name) {
  96.         @n = getpwnam($name_s);
  97.         ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
  98.         next if $name_s ne $name;
  99.     }
  100.     $perfect{$name_s}++
  101.         if $name    eq $name_s    and
  102.                $uid     eq $uid_s     and
  103. # Do not compare passwords: think shadow passwords.
  104.                $gid     eq $gid_s     and
  105.                $gcos    eq $gcos_s    and
  106.                $home    eq $home_s    and
  107.                $shell   eq $shell_s;
  108.     }
  109.     $n++;
  110. }
  111.  
  112. if (keys %perfect == 0) {
  113.     $max++;
  114.     print <<EOEX;
  115. #
  116. # The failure of op/pwent test is not necessarily serious.
  117. # It may fail due to local password administration conventions.
  118. # If you are for example using both NIS and local passwords,
  119. # test failure is possible.  Any distributed password scheme
  120. # can cause such failures.
  121. #
  122. # What the pwent test is doing is that it compares the $max first
  123. # entries of $where
  124. # with the results of getpwuid() and getpwnam() call.  If it finds no
  125. # matches at all, it suspects something is wrong.
  126. EOEX
  127.     print "not ";
  128.     $not = 1;
  129. } else {
  130.     $not = 0;
  131. }
  132. print "ok ", $tst++;
  133. print "\t# (not necessarily serious: run t/op/pwent.t by itself)" if $not;
  134. print "\n";
  135.  
  136. close(PW);
  137.