4 my ($where, $args, @pathnames) = @_;
5 foreach my $prog (@pathnames) {
7 next unless open PW, '-|', "$prog $args 2>/dev/null";
8 next unless defined <PW>;
17 eval {my @n = getpwuid 0; setpwent()};
18 if ($@ && $@ =~ /(The \w+ function is unimplemented)/) {
19 print "1..0 # Skip: $1\n";
22 eval { require Config; import Config; };
24 if ($Config{'i_pwd'} ne 'define') {
25 $reason = '$Config{i_pwd} undefined';
27 elsif (not -f "/etc/passwd" ) { # Play safe.
28 $reason = 'no /etc/passwd file';
32 $where = try_prog('NIS passwd', 'passwd',
33 qw(/usr/bin/ypcat /bin/ypcat /etc/ypcat));
36 $where //= try_prog('NetInfo passwd', 'passwd .', '/usr/bin/nidump');
38 if (not defined $where && # Try dscl
39 $Config{useperlio} eq 'define') { # need perlio
41 # Map dscl items to passwd fields, and provide support for
42 # mucking with the dscl output if we need to (and we do).
45 map {$_ => {inx => $inx++, mung => sub {$_[0]}}}
46 qw{RecordName Password UniqueID PrimaryGroupID
47 RealName NFSHomeDirectory UserShell};
50 # The RecordName for a /User record is the username. In some
51 # cases there are synonyms (e.g. _www and www), in which case we
52 # get a blank-delimited list. We prefer the first entry in the
53 # list because getpwnam() does.
54 $want{RecordName}{mung} = sub {(split '\s+', $_[0], 2)[0]};
56 # The UniqueID and PrimaryGroupID for a /User record are the
57 # user ID and the primary group ID respectively. In cases where
58 # the high bit is set, 'dscl' returns a negative number, whereas
59 # getpwnam() returns its twos complement. This mungs the dscl
60 # output to agree with what getpwnam() produces. Interestingly
61 # enough, getpwuid(-2) returns the right record ('nobody'), even
62 # though it returns the uid as 4294967294. If you track uid_t
63 # on an i386, you find it is an unsigned int, which makes the
64 # unsigned version the right one; but both /etc/passwd and
65 # /etc/master.passwd contain negative numbers.
66 $want{UniqueID}{mung} = $want{PrimaryGroupID}{mung} = sub {
67 unpack 'L', pack 'l', $_[0]};
69 foreach my $dscl (qw(/usr/bin/dscl)) {
71 open (my $fh, '-|', join (' ', $dscl, qw{. -readall /Users},
72 keys %want, '2>/dev/null')) or next;
78 @rec and $data .= join (':', @rec) . "\n";
82 my ($name, $value) = split ':\s+', $_, 2;
83 unless (defined $value) {
90 if (defined (my $info = $want{$name})) {
91 $rec[$info->{inx}] = $info->{mung}->($value);
94 @rec and $data .= join (':', @rec) . "\n";
95 if (open (PW, '<', \$data)) {
96 $where = "dscl . -readall /Users";
102 if (not defined $where) { # Try local.
103 my $PW = "/etc/passwd";
104 if (-f $PW && open(PW, $PW) && defined(<PW>)) {
110 $where //= try_prog('NIS+', 'passwd.org_dir', '/bin/niscat');
112 undef $reason if defined $where;
114 if ($reason) { # Give up.
115 print "1..0 # Skip: $reason\n";
120 # By now the PW filehandle should be open and full of juicy password entries.
124 # Go through at most this many users.
125 # (note that the first entry has been read away by now)
133 print "# where $where\n";
139 # LIMIT -1 so that users with empty shells don't fall off
140 my @s = split /:/, $_, -1;
141 my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s);
142 (my $v) = $Config{osvers} =~ /^(\d+)/;
143 if ($^O eq 'darwin' && $v < 9) {
144 ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s[0,1,2,3,7,8,9];
146 ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s;
148 next if /^\+/; # ignore NIS includes
150 push @{ $seen{$name_s} }, $.;
152 warn "# Your $where line $. is empty.\n";
160 # In principle we could whine if @s != 7 but do we know enough
161 # of passwd file formats everywhere?
162 if (@s == 7 || ($^O eq 'darwin' && @s == 10)) {
163 @n = getpwuid($uid_s);
166 my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
167 # Protect against one-to-many and many-to-one mappings.
168 if ($name_s ne $name) {
169 @n = getpwnam($name_s);
170 ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
171 next if $name_s ne $name;
174 if $name eq $name_s and
176 # Do not compare passwords: think shadow passwords.
187 print "# max = $max, n = $n, perfect = ", scalar keys %perfect, "\n";
189 if (keys %perfect == 0 && $n) {
193 # The failure of op/pwent test is not necessarily serious.
194 # It may fail due to local password administration conventions.
195 # If you are for example using both NIS and local passwords,
196 # test failure is possible. Any distributed password scheme
197 # can cause such failures.
199 # What the pwent test is doing is that it compares the $max first
201 # with the results of getpwuid() and getpwnam() call. If it finds no
202 # matches at all, it suspects something is wrong.
211 print "\t# (not necessarily serious: run t/op/pwent.t by itself)" if $not;
214 # Test both the scalar and list contexts.
220 my $pw = scalar getpwent();
221 last unless defined $pw;
230 my ($pw) = (getpwent());
231 last unless defined $pw;
236 print "not " unless "@pw1" eq "@pw2";
237 print "ok ", $tst++, "\n";