Image

Image

Search This Blog

Tuesday, September 16, 2008

getexch.pl
Posted in by rain on November 7th, 2007

#!/usr/bin/perl -T -w

use Net::LDAP;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant ( “LDAP_CONTROL_PAGED” );

$VALID = “/etc/initsetup/exchange_recipients”;
$list = “/etc/initsetup/tmpnusr”;

$dc1=”ctrl1.fakedomain.local”;
$dc2=”ctrl2.fakedomain.local”;

#$hqbase=”cn=Users,dc=fakedomain,dc=local”;
$hqbase=”dc=fakedomain,dc=local”;

#$user=”cn=identity,cn=Users,dc=fakedomain,dc=local”;
$user=”identity\@fakedomain.local”;
$passwd=”secret”;

$noldapserver=0;
$ldap = Net::LDAP->new($dc1) or
$noldapserver=1;
if ($noldapserver == 1) {
$ldap = Net::LDAP->new($dc2) or
die “Error connecting to specified domain controllers $@ \n”;
}

$mesg = $ldap->bind ( dn => $user,
password =>$passwd);
if ( $mesg->code()) {
die (”error:”, $mesg->code(),”\n”,”error name: “,$mesg->error_name(),
“\n”, “error text: “,$mesg->error_text(),”\n”);
}

$page = Net::LDAP::Control::Paged->new( size => 990 );

@args = ( base => $hqbase,

# filter => “(&(sAMAccountName=*)(mail=*))”,
filter => “(& (mailnickname=*) (| (&(objectCategory=person)
(objectClass=)(!(homeMDB=*))(!(msExchHomeServerName=*)))
(&(objectCategory=person)(objectClass=)(|(homeMDB=*)
(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=contact))
(objectCategory=group)(objectCategory=publicFolder)(objectClass=msExchDynamicDistributionList) ))”,
control => [ $page ],
attrs => “proxyAddresses”,
);

my $cookie;
while(1) {
my $mesg = $ldap->search( @args );

foreach my $entry ( $mesg->entries ) {
my $name = $entry->get_value( “cn” );
foreach my $mail ( $entry->get_value( “proxyAddresses” ) ) {
push(@tmpvalid, $name.” \n”);
if ( $mail =~ s/^(smtp|SMTP)://gs ) {
push(@valid, $mail.” OK\n”);
}
}
}

$mesg->code and last;

my($resp) = $mesg->control( LDAP_CONTROL_PAGED ) or last;
$cookie = $resp->cookie or last;

$page->cookie($cookie);
}

if ($cookie) {
$page->cookie($cookie);
$page->size(0);
$ldap->search( @args );
die(”LDAP query unsuccessful”);
}
open VALID, “>$list” or die “not possible to open $!”;
print VALID @tmpvalid;
open VALID, “>$VALID” or die “CANNOT OPEN $VALID $!”;
print VALID @valid;
#print VALID “\@example. OK\n”;
#print VALID “1\@example. 550 User unknown.\n”;
#print VALID “bad.example. 550 User does not exist.\n”;

close VALID;

No comments:

Post a Comment

Blog Archive