#!/usr/bin/perl # # Author name: mspidle # Creation date: 01/30/03 # # Description: This is to connect to the Active Directory through perl # use Net::LDAP; use Net::LDAP::Util qw(ldap_error_name ldap_error_text) ; # use for Error handling #$authuser = "mspidle"; $authuser = "Gilmore-Baldwin, John"; $passwd = "apple1"; $ldap = Net::LDAP->new("oly-lclci01.theolympian.com") or die "$@"; $mesg = $ldap->bind($authuser, password=> $passwd) || die"failed to bind as $opt_D"; #$mesg = $ldap->bind() || die"failed to bind as $opt_D"; if ( $mesg->code ) { LDAPerror("Binding",$mesg); #print "Error code: $errstr\n"; } my @Attrs = (); # request all available attributes # to be returned. my $result = LDAPsearch($ldap,"mailNickname=jbaldwin"); # my $result = LDAPsearch($ldap,"sAMAccount=jbaldwin"); my $href = $result->as_struct; # get an array of the DN names my @arrayOfDNs = keys %$href ; # use DN hashes # process each DN using it as a key foreach (@arrayOfDNs) { print $_,"\n"; my $valref = $$href{$_}; # get an array of the attribute names # passed for this one DN. my @arrayOfAttrs = sort keys %$valref; #use Attr hashes my $attrName; foreach $attrName (@arrayOfAttrs) { # skip any binary data: yuck! next if ( $attrName =~ /;binary$/ ); # get the attribute value (pointer) using the # attribute name as the hash my $attrVal = @$valref{$attrName} ; print "\t $attrName: @$attrVal \n"; } print "#-------------------------------\n"; # End of that DN } sub LDAPsearch { my ($ldap,$searchString,$attrs,$base) = @_ ; #print "@_\n\n"; # if they don't pass a base... set it for them if (!$base ) { $base = "ou=Users, ou=Olympia, dc=us, dc=ad, dc=gannett, dc=com"; } # if they don't pass an array of attributes... # set up something for them if (!$attrs ) { $attrs = ['cn','mail' ] ; } my $result = $ldap->search ( base => "$base", scope => "sub", filter => "$searchString", attrs => $attrs ); } sub LDAPerror { my ($from,$mesg) = @_; print "Return code: ",$mesg->code ; print "\tMessage: ", ldap_error_name($mesg->code); print " :", ldap_error_text($mesg->code); print "MessageID: ",$mesg->mesg_id; print "\tDN: ",$mesg->dn; #--- # Programmer note: # # "$mesg->error" DOESN'T work!!! # #print "\tMessage: ", $mesg->error; #----- }