#!/usr/bin/perl -nl

setpwent;

$inform_type = shift || 'display';

while ( ($u,$c) = getpwent ) {
    inform( $u, $_ ) if crypt($_,$c) eq $c;
}

sub inform {
    my ( $u, $p ) = @_;

    if ( $inform_type eq 'display' ) {
        # 1: just display to stdout
        print "$u $p\n";
    } elsif ( $inform_type eq 'mailuser' ) {
        # 2: tell the owner
        open OWNER, "|mail $u";
        print OWNER "You have an easily guessed password.",
            "  Please change it.\n";
        close OWNER;
    } elsif ( $inform_type eq 'mailsecurity' ) {
        # 3: tell the security department about all poor passwords
        $format = "%20s %s\n";
        unless ( $mail_started ) {
            open SECURITY, "|mail password-security";
            printf SECURITY $format, 'User', 'Password';
            printf SECURITY $format, '----', '--------';
            ++$mail_started;
        }
        printf SECURITY $format, $u, $p;
    } # Add more display methods as needed.
}

sub END {
    close SECURITY if $mail_started;
}
