#-----------------------------------------------------------------------------
#
# Flip through passwords and look for people without passwords
#

#
# first check null passwords
#

$failed = 0;
&pcheck('NULL passwords');
while (($login, $passwd, $uid, $gid, $quota, $comment, $gcos) = getpwent) {
  if (!$passwd) {
    $tmp = "DANGER: $login ";
    if ( (getgrgid($gid))[0] ) { $tmp .= "(" . (getgrgid($gid))[0] . ") "; }
    if ($gcos) { $tmp .= "[$gcos] "; }
    $tmp .= "has no password.\n";
    &report($tmp);
    $failed = 1;
    }
  }
if ($failed) { &failed; }
else {
  &report("INFO:  No accounts have NULL passwords.\n");
  &passed;
  }
setpwent;

#
# UID/GID 0 accounts
#

$failed = 0;
&pcheck('UID/GID 0 accounts');
while (($login, $passwd, $uid, $gid, $quota, $comment, $gcos) = getpwent) {
  if (($login ne "root") && ($uid == 0) && ($gid == 0)) {
    $tmp = "WARNING: " . &nprint(9, $login) . " ";
    $tmp .= "(group " . &nprint(10, ((getgrgid($gid))[0] || "<unknown>")) . &nprint(13, "[GID $gid])");
    $tmp .= "has UID and GID 0.\n";
    &report($tmp);
    $failed = 1;
    }
  elsif (($login ne "root") && ($uid == 0)) {
    $tmp = "WARNING: " . &nprint(9, $login) . " ";
    $tmp .= "(group " . &nprint(10, ((getgrgid($gid))[0] || "<unknown>")) . &nprint(13, "[GID $gid])");
    $tmp .= "has UID 0.\n";
    &report($tmp);
    $failed = 1;
    }
  elsif (($login ne "root") && ($gid == 0)) {
    $tmp = "WARNING: " . &nprint(9, $login) . " ";
    $tmp .= "(group " . &nprint(10, ((getgrgid($gid))[0] || "<unknown>")) . &nprint(13, "[GID $gid])");
    $tmp .= "has GID 0.\n";
    &report($tmp);
    $failed = 1;
    }
  }
if ($failed) { &warn; &report("\n"); }
else {
  &report("INFO:  There are no accounts with UID/GID 0 that should\n",
          "       not be UID/GID 0.\n");
  &passed;
  }
setpwent;
$failed = 0;

#
# Duplicate UIDs
#

&pcheck('Duplicate UIDs');
while (($login, $passwd, $uid, $gid, $quota, $comment, $gcos) = getpwent) {
  if ($uids{$uid}) { $uids{$uid} .= "::$login"; }
  else { $uids{$uid} = $login; }
  }
foreach $i (sort keys %uids) {
  @tmp = split('::', $uids{$i});
  if ($#tmp != 0) {
    &report("WARNING: UID $i is shared by " . join(", ", @tmp) . ".\n");
    $failed = 1;
    }
  delete $uids{$i};
  }
if ($failed) { &warn; &report("\n"); }
else {
  &report("INFO:  There are no accounts with duplicate UIDs.\n");
  &passed;
  }
setpwent;
$failed = 0;

#
# duplicate logins
#

&pcheck('Duplicate Logins');
while (($login, $passwd, $uid, $gid, $quota, $comment, $gcos) = getpwent) {
  if ($logins{$login}) { $logins{$login} .= "::$uid/" . ((getgrgid($gid))[0] || $gid); }
  else { $logins{$login} = "$uid/" . ((getgrgid($gid))[0] || $gid); }
  }
foreach $i (sort keys %logins) {
  @tmp = split('::', $logins{$i});
  if ($#tmp != 0) {
    &report("WARNING: Login $i occurs " . ($#tmp +1) . " times (UID/GIDs " . join(", ", @tmp) . ").\n");
    $failed = 1;
    }
  delete $logins{$i};
  }
if ($failed) { &warn; &report("\n"); }
else {
  &report("INFO:  There are no accounts with duplicate login names.\n");
  &passed;
  }
setpwent;
$failed = 0;

exit 0;
