6 eval {my @n = getpwuid 0; setpwent()};
7 if ($@ && $@ =~ /(The \w+ function is unimplemented)/) {
8 print "1..0 # Skip: $1\n";
11 eval { require Config; import Config; };
13 if ($Config{'i_pwd'} ne 'define') {
14 $reason = '$Config{i_pwd} undefined';
16 elsif (not -f "/etc/passwd" ) { # Play safe.
17 $reason = 'no /etc/passwd file';
20 if (not defined $where) { # Try NIS.
21 foreach my $ypcat (qw(/usr/bin/ypcat /bin/ypcat /etc/ypcat)) {
23 open(PW, "$ypcat passwd 2>/dev/null |") &&
25 $where = "NIS passwd";
32 if (not defined $where) { # Try NetInfo.
33 foreach my $nidump (qw(/usr/bin/nidump)) {
35 open(PW, "$nidump passwd . 2>/dev/null |") &&
37 $where = "NetInfo passwd";
44 if (not defined $where && # Try dscl
45 $Config{useperlio} eq 'define') { # need perlio
47 # Map dscl items to passwd fields, and provide support for
48 # mucking with the dscl output if we need to (and we do).
51 map {$_ => {inx => $inx++, mung => sub {$_[0]}}}
52 qw{RecordName Password UniqueID PrimaryGroupID
53 RealName NFSHomeDirectory UserShell};
56 # The RecordName for a /User record is the username. In some
57 # cases there are synonyms (e.g. _www and www), in which case we
58 # get a blank-delimited list. We prefer the first entry in the
59 # list because getpwnam() does.
60 $want{RecordName}{mung} = sub {(split '\s+', $_[0], 2)[0]};
62 # The UniqueID and PrimaryGroupID for a /User record are the
63 # user ID and the primary group ID respectively. In cases where
64 # the high bit is set, 'dscl' returns a negative number, whereas
65 # getpwnam() returns its twos complement. This mungs the dscl
66 # output to agree with what getpwnam() produces. Interestingly
67 # enough, getpwuid(-2) returns the right record ('nobody'), even
68 # though it returns the uid as 4294967294. If you track uid_t
69 # on an i386, you find it is an unsigned int, which makes the
70 # unsigned version the right one; but both /etc/passwd and
71 # /etc/master.passwd contain negative numbers.
72 $want{UniqueID}{mung} = $want{PrimaryGroupID}{mung} = sub {
73 unpack 'L', pack 'l', $_[0]};
75 foreach my $dscl (qw(/usr/bin/dscl)) {
77 open (my $fh, '-|', join (' ', $dscl, qw{. -readall /Users},
78 keys %want, '2>/dev/null')) or next;
84 @rec and $data .= join (':', @rec) . "\n";
88 my ($name, $value) = split ':\s+', $_, 2;
89 unless (defined $value) {
96 if (defined (my $info = $want{$name})) {
97 $rec[$info->{inx}] = $info->{mung}->($value);
100 @rec and $data .= join (':', @rec) . "\n";
101 if (open (PW, '<', \$data)) {
102 $where = "dscl . -readall /Users";
109 if (not defined $where) { # Try local.
110 my $PW = "/etc/passwd";
111 if (-f $PW && open(PW, $PW) && defined(<PW>)) {
117 if (not defined $where) { # Try NIS+
118 foreach my $niscat (qw(/bin/niscat)) {
120 open(PW, "$niscat passwd.org_dir 2>/dev/null |") &&
122 $where = "NIS+ $niscat passwd.org_dir";
129 if ($reason) { # Give up.
130 print "1..0 # Skip: $reason\n";
135 # By now the PW filehandle should be open and full of juicy password entries.
139 # Go through at most this many users.
140 # (note that the first entry has been read away by now)
148 print "# where $where\n";
154 # LIMIT -1 so that users with empty shells don't fall off
155 my @s = split /:/, $_, -1;
156 my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s);
157 (my $v) = $Config{osvers} =~ /^(\d+)/;
158 if ($^O eq 'darwin' && $v < 9) {
159 ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s[0,1,2,3,7,8,9];
161 ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s;
163 next if /^\+/; # ignore NIS includes
165 push @{ $seen{$name_s} }, $.;
167 warn "# Your $where line $. is empty.\n";
175 # In principle we could whine if @s != 7 but do we know enough
176 # of passwd file formats everywhere?
177 if (@s == 7 || ($^O eq 'darwin' && @s == 10)) {
178 @n = getpwuid($uid_s);
181 my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
182 # Protect against one-to-many and many-to-one mappings.
183 if ($name_s ne $name) {
184 @n = getpwnam($name_s);
185 ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
186 next if $name_s ne $name;
189 if $name eq $name_s and
191 # Do not compare passwords: think shadow passwords.
202 print "# max = $max, n = $n, perfect = ", scalar keys %perfect, "\n";
204 if (keys %perfect == 0 && $n) {
208 # The failure of op/pwent test is not necessarily serious.
209 # It may fail due to local password administration conventions.
210 # If you are for example using both NIS and local passwords,
211 # test failure is possible. Any distributed password scheme
212 # can cause such failures.
214 # What the pwent test is doing is that it compares the $max first
216 # with the results of getpwuid() and getpwnam() call. If it finds no
217 # matches at all, it suspects something is wrong.
226 print "\t# (not necessarily serious: run t/op/pwent.t by itself)" if $not;
229 # Test both the scalar and list contexts.
235 my $pw = scalar getpwent();
236 last unless defined $pw;
245 my ($pw) = (getpwent());
246 last unless defined $pw;
251 print "not " unless "@pw1" eq "@pw2";
252 print "ok ", $tst++, "\n";