#!/usr/bin/perl
# copyright (c) 2005 Norman Maurer <nm@byteaction.de>
# ByteAction GmbH 
# released under the GNU General Public License
#
#
# Script to get an list of all valid recipients from a 
# Lotus Domino Server. It can be used 
# with qmail-spamcontrol-patch to reject all unkown
# recipients
#
#################################################################

#################################################################
# ChangeLog:
# 
# 2005/08/18: First release
#################################################################

#######################
# Modules             #
#######################

use Net::LDAP;
use File::Copy;
use Getopt::Mixed;
Getopt::Mixed::getOptions("H:s U:s P:s B:s F:s  h File>F Hostname>H Username>U Password>P Base>B help>h");


#######################
# Variables	      #
#######################

my $username = $opt_U if defined $opt_U;
my $password = $opt_P if defined $opt_P;
my $base = $opt_B if defined $opt_B;
my $host = $opt_H if defined $opt_H; 

#######################
# Subs	              #
#######################

&help if ((defined $opt_h) || (!defined $opt_U) || (!defined $opt_P) || (!defined $opt_B) || (!defined $opt_H));
&get_mailboxes;
exit 0;

#######################
# Main Code	      #
#######################

sub help
{
	print "\nScript Usage;\n";
	print "\t-H\t\t--Host\tThe IP or the dnsname of the domino server\n";
	print "\t-B\t\t--Base\tThe base search path to use when search the domino server\n";
	print "\t-U\t\t--User\tThe User to bind to\n";
	print "\t-P\t\t--Password\tThe Password to use with this user\n";
	print "\t-F\t\t--File\tThe File where to write the founded mailboxes\n";
	print "\t\t\\tWhen noone is given it prints the result to STDOUT\n";
	print "\n";
	print "example:\n";
	print "$0 -H192.168.0.1 -Bmysdomain -Uuser -Ppassword\n";
	print "\n";

	exit 0;
}

sub get_mailboxes
{
	
	# Connect to LDAP on Notes Server (If it is down the script will quit).
	my $ldap = Net::LDAP->new( "$host", async => 1, version => 2, onerror => 'die' ) or die "$@";
	my $mesg = $ldap->bind("cn=$username",password=>"$password") or die "Cannot auth\n";
	$mesg = $ldap->search( base => "o=$base", filter => "(&(ObjectClass=*)(uid=*))");
	#$mesg = $ldap->search( base => "o=$base", filter => "(&(ObjectClass=dominoPerson)(uid=*))");


	my $max = $mesg->count;
	if ($max > 0)
	{
		if (defined $opt_F)
		{
			copy("$opt_F","$opt_F.backup") if ( -f "$opt_F");
			open (FILE,">$opt_F");
		}
	
		for ( $i = 0 ; $i < $max ; $i++ ) 
		{
			my $entry = $mesg->entry ( $i );

			my $address = $entry->get_value( "mail" );
			my @alias_array=();
			foreach my $alias ($entry->get_value( "cn"))
			{
				$alias =~ s/ //g;
				$lc_alias = lc $alias;

				push @alias_array, $lc_alias if $lc_alias =~ /@/;
			}

			foreach my $alias2 ($entry->get_value( "uid"))
			{
				$alias2 =~ s/ //g;
				$lc_alias2 = lc $alias2;

				push @alias_array, $lc_alias2 if $lc_alias2 =~ /@/;
			}

			$address =~ s/ //g;
			
			my $lc_address = lc $address;
			if (defined $opt_F)
			{
				print FILE "$lc_address\n";
				
				foreach my $tmp_alias (@alias_array)
				{
					print FILE "$tmp_alias\n";
				}
			}
			else
			{
				print "$lc_address\n";
				foreach my $tmp_alias (@alias_array)
				{
					print "$tmp_alias\n";
				}
			}
		}
		close FILE if defined $opt_F;
	}
	else 
	{
		print "No entries recieve. Maybe you use the wrong Base\n";
	}

	close FILE if defined $opt_F;
	$mesg = $ldap->unbind;
}
