Commit | Line | Data |
c5987ebb |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
20822f61 |
5 | @INC = '../lib'; |
b62e3068 |
6 | eval {my @n = getpwuid 0; setpwent()}; |
df284ca6 |
7 | if ($@ && $@ =~ /(The \w+ function is unimplemented)/) { |
8 | print "1..0 # Skip: $1\n"; |
9 | exit 0; |
10 | } |
c5987ebb |
11 | eval { require Config; import Config; }; |
45c0de28 |
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'; |
b91c0863 |
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"; |
45c0de28 |
26 | undef $reason; |
b91c0863 |
27 | last; |
28 | } |
29 | } |
30 | } |
c5987ebb |
31 | |
b91c0863 |
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"; |
45c0de28 |
38 | undef $reason; |
b91c0863 |
39 | last; |
40 | } |
41 | } |
42 | } |
55ec6b63 |
43 | |
b91c0863 |
44 | if (not defined $where) { # Try local. |
45 | my $PW = "/etc/passwd"; |
46 | if (-f $PW && open(PW, $PW) && defined(<PW>)) { |
47 | $where = $PW; |
45c0de28 |
48 | undef $reason; |
55ec6b63 |
49 | } |
b91c0863 |
50 | } |
51 | |
45c0de28 |
52 | if ($reason) { # Give up. |
53 | print "1..0 # Skip: $reason\n"; |
c5987ebb |
54 | exit 0; |
55 | } |
56 | } |
57 | |
765e9edb |
58 | # By now the PW filehandle should be open and full of juicy password entries. |
b91c0863 |
59 | |
765e9edb |
60 | print "1..2\n"; |
c5987ebb |
61 | |
62 | # Go through at most this many users. |
b91c0863 |
63 | # (note that the first entry has been read away by now) |
64 | my $max = 25; |
c5987ebb |
65 | |
66 | my $n = 0; |
c5987ebb |
67 | my $tst = 1; |
b91c0863 |
68 | my %perfect; |
55ec6b63 |
69 | my %seen; |
c5987ebb |
70 | |
bd055eb9 |
71 | setpwent(); |
c5987ebb |
72 | while (<PW>) { |
c5987ebb |
73 | chomp; |
a941e390 |
74 | # LIMIT -1 so that users with empty shells don't fall off |
75 | my @s = split /:/, $_, -1; |
32b4ad3c |
76 | my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s); |
77 | if ($^O eq 'darwin') { |
78 | ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s[0,1,2,3,7,8,9]; |
79 | } else { |
80 | ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s; |
81 | } |
b91c0863 |
82 | next if /^\+/; # ignore NIS includes |
55ec6b63 |
83 | if (@s) { |
84 | push @{ $seen{$name_s} }, $.; |
85 | } else { |
86 | warn "# Your $where line $. is empty.\n"; |
87 | next; |
88 | } |
09ac174e |
89 | if ($n == $max) { |
90 | local $/; |
91 | my $junk = <PW>; |
92 | last; |
93 | } |
55ec6b63 |
94 | # In principle we could whine if @s != 7 but do we know enough |
95 | # of passwd file formats everywhere? |
32b4ad3c |
96 | if (@s == 7 || ($^O eq 'darwin' && @s == 10)) { |
c5987ebb |
97 | @n = getpwuid($uid_s); |
98 | # 'nobody' et al. |
99 | next unless @n; |
100 | my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n; |
101 | # Protect against one-to-many and many-to-one mappings. |
102 | if ($name_s ne $name) { |
103 | @n = getpwnam($name_s); |
104 | ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n; |
105 | next if $name_s ne $name; |
106 | } |
b91c0863 |
107 | $perfect{$name_s}++ |
108 | if $name eq $name_s and |
109 | $uid eq $uid_s and |
110 | # Do not compare passwords: think shadow passwords. |
111 | $gid eq $gid_s and |
112 | $gcos eq $gcos_s and |
113 | $home eq $home_s and |
114 | $shell eq $shell_s; |
c5987ebb |
115 | } |
116 | $n++; |
117 | } |
bd055eb9 |
118 | endpwent(); |
c5987ebb |
119 | |
b91c0863 |
120 | if (keys %perfect == 0) { |
121 | $max++; |
122 | print <<EOEX; |
123 | # |
124 | # The failure of op/pwent test is not necessarily serious. |
125 | # It may fail due to local password administration conventions. |
126 | # If you are for example using both NIS and local passwords, |
127 | # test failure is possible. Any distributed password scheme |
128 | # can cause such failures. |
129 | # |
130 | # What the pwent test is doing is that it compares the $max first |
131 | # entries of $where |
132 | # with the results of getpwuid() and getpwnam() call. If it finds no |
133 | # matches at all, it suspects something is wrong. |
134 | # |
135 | EOEX |
136 | print "not "; |
137 | $not = 1; |
138 | } else { |
139 | $not = 0; |
55ec6b63 |
140 | } |
b91c0863 |
141 | print "ok ", $tst++; |
142 | print "\t# (not necessarily serious: run t/op/pwent.t by itself)" if $not; |
143 | print "\n"; |
c5987ebb |
144 | |
91e74348 |
145 | # Test both the scalar and list contexts. |
765e9edb |
146 | |
147 | my @pw1; |
148 | |
765e9edb |
149 | setpwent(); |
150 | for (1..$max) { |
151 | my $pw = scalar getpwent(); |
152 | last unless defined $pw; |
153 | push @pw1, $pw; |
154 | } |
bd055eb9 |
155 | endpwent(); |
765e9edb |
156 | |
157 | my @pw2; |
158 | |
765e9edb |
159 | setpwent(); |
160 | for (1..$max) { |
161 | my ($pw) = (getpwent()); |
162 | last unless defined $pw; |
163 | push @pw2, $pw; |
164 | } |
bd055eb9 |
165 | endpwent(); |
765e9edb |
166 | |
167 | print "not " unless "@pw1" eq "@pw2"; |
168 | print "ok ", $tst++, "\n"; |
169 | |
c5987ebb |
170 | close(PW); |