Solved

PERL MS Exchange address book (web based)

Posted on 2006-07-11
5
473 Views
Last Modified: 2008-01-09
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
Comment
Question by:ledeni
  • 3
5 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 17086372
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
 
LVL 25

Accepted Solution

by:
clockwatcher earned 300 total points
ID: 17086915
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
 

Author Comment

by:ledeni
ID: 17087040
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
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17087191
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
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17087794
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl script to parse xml file 2 103
Search in text file in column and compare 4 46
git fat pull happens nothing in centos 6.3 python 2.6.6 33 207
Perl string filter 5 72
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now