Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

PERL MS Exchange address book (web based)

Hi everyone,

I am building an online application that would access the MS Exchange address book and display company contacts, aside many other things that this app does. I was wondering if anyone has done that in PERL and if PERL holds any libraries that help do that. If you can provide a code example it would be just amazing. I looked at the CPAN but I could not find anything there.

Thanks for you help.

L.
0
ledeni
Asked:
ledeni
  • 3
1 Solution
 
Kim RyanIT ConsultantCommented:
You could try using LDAP:  http://search.cpan.org/~gbarr/perl-ldap-0.33/lib/Net/LDAP.pod
MS Exchange 5.0 is compatible with LDAP
0
 
clockwatcherCommented:
Here's an example pulled from one of the scripts I use here at work to connect to our Exchange 5.5 box.  It uses ADO (basically an LDAP example but hidden underneath ADSI).  I went with it instead of straight LDAP because I had issues with the login.  If you have anonymous LDAP access enabled on your exchange server, normal LDAP shouldn't be a problem.  And the login problems may have just been me anyway.  If you're trying to query it from a non-windows box, this script won't work and normal LDAP will probably be your only option.

No matter which way you go a reference for the LDAP schema for Exchange 5.5 is here: http://www.microsoft.com/technet/prodtechnol/exchange/55/support/adsi.mspx

Ignore the commented section, I pull the SID account and do a SID to account name translation to get the windows account name for the mailbox-- chopped that section out because it doesn't pertain to your question.

Anyway, the following works for me:

use Win32::OLE;

my $conn = Win32::OLE->new('ADODB.Connection');

my $query = '<GC://YOUREXCHANGESERVERNAME:389>;(objectClass=organizationalPerson);Assoc-NT-Account,Uid,ADsPath,cn,givenName,sn,mail,initials,telephoneNumber;subtree';

if ($conn) {

   $conn->{Provider} = "ADsDSOObject";
   $conn->Properties->{"User ID"} = "domainname\\username";
   $conn->Properties->{"Password"} = "userpassword";
   $conn->Properties->{"Encrypt Password"} = 1;

   $conn->Open("ADs Provider");
   my $rs = Win32::OLE->new('ADODB.Recordset');
   $rs->Open($query, $conn);
   if (! $rs->{EOF} ) {

     print "First Name^MI^Last Name^Alias^Emails^Phone #\n";

     my ($email, $nt, $cname, $uid, $fname, $lname, $phone);

     while (! $rs->{EOF} )
     {
      $email = $rs->fields("mail")->{Value};      
      $nt = $rs->fields("Assoc-NT-Account")->{Value};
      $cname = $rs->fields("cn")->{Value};
      $uid = $rs->fields("uid")->{Value};
      $fname = $rs->fields("givenName")->{Value};
      $lname = $rs->fields("sn")->{Value};
      $phone = $rs->fields("telephoneNumber")->{Value};
      $mi = $rs->fields("initials")->{Value};
                #  $sid = $rs->fields("Assoc-NT-Account")->{Value};
      #  $accountname = "";
      #  $accountName = GetNameFromSID($sid) if $sid =~ /^[A-F0-9]+$/ ;
      print "$fname^$mi^$lname^$uid^$email^$phone^$accountName\n" unless $accountName =~ /^zz/i;
      $rs->MoveNext();

     }
     
   }

   $rs->close;
   undef $rs;
   $conn->close;
   undef $conn;
}
0
 
ledeniAuthor Commented:
clockwatcher ... thanks a lot ... works like a charm !!

However, I noticed that this is using the win:32 library would this work if the website is deployed on the unix server ??? if not .. do you have any suggestions how it could be done.

Thanks .. credits well spent.

L.
0
 
clockwatcherCommented:
No.  It won't work on a unix server.  If you're going to do this on unix, you'll need to use straight LDAP.   As I remember it, I had problems with authentication when I tried it that way though.  I'll see if I can put together a few minutes to try and get a working example using Net::LDAP.
0
 
clockwatcherCommented:
Here's a Net::LDAP example that works for me.  If you don't have SSL enabled for your Exchange's LDAP, it'll transmit the password in clear text.  You might want to try an anonymous bind to see if you have any luck with it (I don't).

use Net::LDAP;

my $exchserver = "MyExchangeServer";
my $ntdomain = "NTDomainName";
my $ntaccount = "NTAccountName";
my $ntpass = "NTPassword";

my $organization = "EXCHANGE_ORGANIZATION";  # the o section of your X.400 address
my $site = "EXCHANGE_SITE";  # the ou section of your X.400 address

my $ldap = Net::LDAP->new("$exchserver");

my $mesg = $ldap->bind( "cn=$ntaccount,cn=$ntdomain", password => $ntpass) ;  #authenticated

# my $mesg = $ldap->bind() ; # anonymous -- this may work for you -- try it first -- doesn't for my set up

$mesg = $ldap->search(
                  base => "cn=Recipients,ou=$site,o=$organization",
                  filter => 'cn=*'
                  );

print "First Name^MI^Last Name^Alias^Emails^Phone #\n";

foreach $entry ($mesg->entries) {

  foreach ( qw(givenName initials sn uid mail telephoneNumber) )
  {
       print $entry->get_value($_)."^";
  }
  print "\n";

}

$mesg = $ldap->unbind;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now