regen headers
[p5sagit/p5-mst-13.2.git] / t / op / pwent.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = "../lib" if -d "../lib";
6     eval { require Config; import Config; };
7
8     my $PW = "/etc/passwd";
9
10     $where = $PW;
11
12     if (-x "/usr/bin/nidump") { # nidump is not just NeXT/OpenStep
13         if (open(PW, "nidump passwd . |")) {
14             $where = "NetInfo passwd";
15         } else {
16             print "1..0\n";
17             exit 0;
18         }
19     } elsif ((defined $Config{'i_pwd'} and $Config{'i_pwd'} ne 'define')
20              or not -f $PW or not open(PW, $PW)) {
21         print "1..0\n";
22         exit 0;
23     }
24 }
25
26 print "1..1\n";
27
28 # Go through at most this many users.
29 my $max = 25; #
30
31 my $n = 0;
32 my $tst = 1;
33 my %suspect;
34 my %seen;
35
36 while (<PW>) {
37     chomp;
38     next if /^\+/; # ignore NIS includes
39     my @s = split /:/;
40     my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s;
41     if (@s) {
42         push @{ $seen{$name_s} }, $.;
43     } else {
44         warn "# Your $where line $. is empty.\n";
45         next;
46     }
47     next if $n == $max;
48     # In principle we could whine if @s != 7 but do we know enough
49     # of passwd file formats everywhere?
50     if (@s == 7) {
51         @n = getpwuid($uid_s);
52         # 'nobody' et al.
53         next unless @n;
54         my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
55         # Protect against one-to-many and many-to-one mappings.
56         if ($name_s ne $name) {
57             @n = getpwnam($name_s);
58             ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n;
59             next if $name_s ne $name;
60         }
61         $suspect{$name_s}++
62             if $name    ne $name_s    or
63 # Shadow passwords confuse this.
64 # Think about non-crypt(3) encryptions, too, before you do anything rash.
65 #              $passwd  ne $passwd_s  or
66                $uid     ne $uid_s     or
67                $gid     ne $gid_s     or
68                $gcos    ne $gcos_s    or
69                $home    ne $home_s    or
70                $shell   ne $shell_s;
71     }
72     $n++;
73 }
74
75 # Drop the multiply defined users.
76
77 foreach (sort keys %seen) {
78     my $times = @{ $seen{$_} };
79     if ($times > 1) {
80         # Multiply defined users are rarely intentional.
81         local $" = ", ";
82         print "# User '$_' defined multiple times in $where, lines: @{$seen{$_}}.\n";
83         delete $suspect{$_};
84     }
85 }
86
87 print "not " if keys %suspect;
88 print "ok ", $tst++, "\n";
89
90 close(PW);