Article 13129 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:13129
Path: feenix.metronet.com!news.utdallas.edu!chpc.utexas.edu!cs.utexas.edu!not-for-mail
From: [email protected] (Contr Karl E. Vogel)
Newsgroups: comp.lang.perl
Subject: [SCRIPT] checkaliases
Date: 25 Apr 1994 17:36:28 -0500
Organization: Control Data Systems Inc.
Lines: 114
Sender: [email protected]
Message-ID: <[email protected]>
NNTP-Posting-Host: cs.utexas.edu

I read Larry's call to send scripts of work in progress, etc.  Here is
"checkaliases", which does a consistency check of our /usr/lib/aliases file.
Comments/suggestions welcome.

---------------------------- cut here
#!/usr/bin/perl
#
# $Id: checkaliases,v 1.1 1992/05/08 20:13:01 vogel Exp $
#
# NAME:
#       checkaliases
#
# SYNOPSIS:
#       checkaliases [file]
#
# DESCRIPTION:
#       "checkaliases" does consistency checking on a file in the format used
#       by /usr/lib/aliases.  We are looking for any member of a mail group
#       which is not a user or another mail group.
#
#       The script also finds any "blank" userids in the aliases file, i.e.
#       two adjacent commas in the middle of a list of userids.
#
# OPTIONS:
#       "file" is the name of an aliases file to check.  The default file
#       is "/usr/lib/aliases".
#
# AUTHOR:
#       Karl Vogel
#
# BUGS:
#       None noticed yet.
#
# HISTORY:
#       $Log: checkaliases,v $
# Revision 1.1  1992/05/08  20:13:01  vogel
# Initial revision
#
#

if ($#ARGV == -1)   { $alias_file = "/usr/lib/aliases"; }
elsif ($#ARGV == 0) { $alias_file = $ARGV[0]; }
else                { die "usage: $0 [file]\n"; }

#
#       Read the aliases file, and create 2 arrays containing the mail
#       groups and the contents of those groups.  Ignore remote users
#       (@ or %), pipes to programs (|), and direct feeds to files (/).
#

open (ALIAS, "$alias_file") || die "Can't open $alias_file: $!\n";
$line = 0;

while (<ALIAS>)
{
       chop;
       $line++;

       next if /^#/;           # Strip comments.
       next if /^$/;           # Strip blank lines.
       s/\s+//;                # Remove whitespace.

       ($grp, $rest) = split (/:/);
       $mailgroups{$grp} = $grp;

       @usr = split (/,/, $rest);

       foreach (@usr)
       {
               next if /@/;
               next if /%/;
               next if /\|/;
               next if /\//;

               if (length() == 0)
               {
                       print "ERROR: zero-length alias at line $line.\n";
                       next;
               }

               $mailusers{$_} = $line;
       }
}

close (ALIAS);

#
#       Read the passwd file, and create an array holding the users.
#

open (PASSWD, "/etc/passwd") || die "Can't open passwd file: $!\n";

while (<PASSWD>)
{
       ($name) = split (/:/);
       $userid{$name} = $name;
}

close (PASSWD);

#
#       For each entry in the mailusers array:  check through the strings
#       holding the mail groups and regular userids.
#

while (($key, $value) = each %mailusers)
{
       if ($userid{$key} !~ $key && $mailgroups{$key} !~ $key)
       {
               print "ERROR: unrecognized user \"$key\" at line $value.\n";
       }
}

exit (0);