#!/usr/bin/perl -w # # Simple script to find unique To: addresses in mail spools which match a certain domain name. Also checks a # user's .virtualmail file to build a list of address that they will need to add. Once these are added, the # user can turn off email wildcarding for their domain. # # Requires standard Perl (>5.004) installation. Run without args to see options. # # TODO: # - The regex that checks for valid email addresses could use some help. # - If this was used system-wide, then the options could be simplified (eg, always knowing what spool # file to check based on username, etc). # # Wm. Rhodes Copyright 2004, released under the GPL: # use strict; use Getopt::Std; use File::Basename; # This is where users' home directories live my $home = "/home"; # Define this to see stuff on STDOUT my $verbose = 0; our ($opt_f, $opt_d, $opt_u); getopt('fdu'); # You'll need all of these to continue my $usage = "Options:\n" . "\t-f Name of path of spool file to check, usually like /var/spool/mail/username\n". "\t-d Domain to match on\n". "\t-u Local username (used to check their .virtualmail file)\n"; my $file = $opt_f || die "You must provide the path and name of a file to check!\n\n$usage\n"; my $domain = $opt_d || die "You must provide a domain name to match on!\n\n$usage\n"; my $user = $opt_u || die "You must provide a system username!\n\n$usage\n"; # Go through the spool file, and find address on a line starting with 'To: ' which match the domain that # we're interested in. If we find a address that we like, add it to our list so we can compare it against # the user's .virtualmail file. That will let us build the list of aliases they will need to add. print "Checking file '$file' for To: addresses matching $domain...\n"; my %addrs; my $count; open (TO, $file) || die "Couldn't open file '$file': $!\n"; while () { next unless /^To:/i; next unless /$domain/i; chomp(); foreach my $possible (split(/,/)) { print "Addr $possible: " if $verbose; if ($possible =~ /?/i) { print "Matched! ($1)\n" if $verbose; $addrs{lc($1)}++; $count++; } else { print "\n" if $verbose; } } print "-"x25, "\n" if $verbose; } close (TO); # Exit or print out stats about what we found. if (%addrs) { print "Found $count addresses, ", scalar(keys(%addrs)), " were unique.\n"; } else { print "Found no addresses matching domain '$domain'. Exiting.\n"; exit; } # Check the aliases file open (ALIASES, "$home/$user/.virtualmail") || print "No .virtualmail file found. Exiting.\n" && exit; my @aliases = ; close (ALIASES); print "The following $domain addresses need to be added to the .virtualmail file for user '$user':\n"; foreach my $addr (sort(keys(%addrs))) { my $mark; foreach (@aliases) { $mark++ if /$addr/; } print "\t$addr\n" unless $mark; }