From: Jarkko Hietaniemi Date: Sat, 28 Nov 1998 14:27:36 +0000 (+0000) Subject: Passwd and group file groveling. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=55ec6b6309c634e33b4056d21286b6381092bd30;p=p5sagit%2Fp5-mst-13.2.git Passwd and group file groveling. p4raw-id: //depot/cfgperl@2349 --- diff --git a/t/op/grent.t b/t/op/grent.t index 9d2b01d..e0cd7a8 100755 --- a/t/op/grent.t +++ b/t/op/grent.t @@ -7,10 +7,18 @@ BEGIN { my $GR = "/etc/group"; - if (($^O eq 'next' and not open(GR, "nidump group .|")) - or (defined $Config{'i_grp'} and $Config{'i_grp'} ne 'define') - or not -f $GR or not open(GR, $GR) - ) { + $where = $GR; + + if (-x "/usr/bin/nidump") { + if (open(GR, "nidump group . |")) { + $where = "NetInfo"; + } else { + print "1..0\n"; + exit 0; + } + } elsif ((defined $Config{'i_grp'} and $Config{'i_grp'} ne 'define') + or not -f $GR or not open(GR, $GR) + ) { print "1..0\n"; exit 0; } @@ -19,19 +27,27 @@ BEGIN { print "1..1\n"; # Go through at most this many groups. -my $max = 25; # +my $max = 25; -my $n = 0; -my $not; +my $n = 0; my $tst = 1; +my %suspect; +my %seen; -$not = 0; while () { - last if $n == $max; chomp; - @s = split /:/; + my @s = split /:/; + my ($name_s,$passwd_s,$gid_s,$members_s) = @s; + if (@s) { + push @{ $seen{$name_s} }, $.; + } else { + warn "# Your $where line $. is empty.\n"; + next; + } + next if $n == $max; + # In principle we could whine if @s != 4 but do we know enough + # of group file formats everywhere? if (@s == 4) { - my ($name_s,$passwd_s,$gid_s,$members_s) = @s; $members_s =~ s/\s*,\s*/,/g; $members_s =~ s/\s+$//; $members_s =~ s/^\s+//; @@ -46,10 +62,10 @@ while () { next if $name_s ne $name; } $members =~ s/\s+/,/g; - $not = 1, last + $suspect{$name_s}++ if $name ne $name_s or # Shadow passwords confuse this. -# Not that group passwords are used much but still. +# Not that group passwords are used much but better not assume anything. # $passwd ne $passwd_s or $gid ne $gid_s or $members ne $members_s; @@ -57,7 +73,19 @@ while () { $n++; } -print "not " if $not; +# Drop the multiply defined groups. + +foreach (sort keys %seen) { + my $times = @{ $seen{$_} }; + if ($times > 1) { + # Multiply defined groups are rarely intentional. + local $" = ", "; + warn "# Group '$_' defined multiple times in $where, lines: @{$seen{$_}}.\n"; + delete $suspect{$_}; + } +} + +print "not " if keys %suspect; print "ok ", $tst++, "\n"; close(GR); diff --git a/t/op/pwent.t b/t/op/pwent.t index 87b2ac1..1365588 100755 --- a/t/op/pwent.t +++ b/t/op/pwent.t @@ -7,10 +7,17 @@ BEGIN { my $PW = "/etc/passwd"; - if (($^O eq 'next' and not open(PW, "nidump passwd .|")) - or (defined $Config{'i_pwd'} and $Config{'i_pwd'} ne 'define') - or not -f $PW or not open(PW, $PW) - ) { + $where = $PW; + + if (-x "/usr/bin/nidump") { + if (open(PW, "nidump passwd . |")) { + $where = "NetInfo"; + } else { + print "1..0\n"; + exit 0; + } + } elsif ((defined $Config{'i_pwd'} and $Config{'i_pwd'} ne 'define') + or not -f $PW or not open(PW, $PW)) { print "1..0\n"; exit 0; } @@ -22,16 +29,24 @@ print "1..1\n"; my $max = 25; # my $n = 0; -my $not; my $tst = 1; +my %suspect; +my %seen; -$not = 0; while () { - last if $n == $max; chomp; - @s = split /:/; + my @s = split /:/; + my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s; + if (@s) { + push @{ $seen{$name_s} }, $.; + } else { + warn "# Your $where line $. is empty.\n"; + next; + } + next if $n == $max; + # In principle we could whine if @s != 7 but do we know enough + # of passwd file formats everywhere? if (@s == 7) { - my ($name_s, $passwd_s, $uid_s, $gid_s, $gcos_s, $home_s, $shell_s) = @s; @n = getpwuid($uid_s); # 'nobody' et al. next unless @n; @@ -42,7 +57,7 @@ while () { ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$home,$shell) = @n; next if $name_s ne $name; } - $not = 1, last + $suspect{$name_s}++ if $name ne $name_s or # Shadow passwords confuse this. # Think about non-crypt(3) encryptions, too, before you do anything rash. @@ -56,7 +71,19 @@ while () { $n++; } -print "not " if $not; +# Drop the multiply defined users. + +foreach (sort keys %seen) { + my $times = @{ $seen{$_} }; + if ($times > 1) { + # Multiply defined users are rarely intentional. + local $" = ", "; + warn "# User '$_' defined multiple times in $where, lines: @{$seen{$_}}.\n"; + delete $suspect{$_}; + } +} + +print "not " if keys %suspect; print "ok ", $tst++, "\n"; close(PW);