diff options
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/ldapwalk-dieter.pl | 176 | ||||
-rwxr-xr-x | examples/ldapwalk.pl | 10 | ||||
-rwxr-xr-x | examples/ldapwalk2.pl | 2 | ||||
-rwxr-xr-x | examples/testurl.pl | 83 |
4 files changed, 266 insertions, 5 deletions
diff --git a/examples/ldapwalk-dieter.pl b/examples/ldapwalk-dieter.pl new file mode 100755 index 0000000..e3321be --- /dev/null +++ b/examples/ldapwalk-dieter.pl @@ -0,0 +1,176 @@ +#!/usr/bin/perl +# +# $Id: ldapwalk.pl,v 1.1 2007/07/01 20:14:40 dieter Exp dieter $ +# ldapwalk.pl - Walks through Records Matching a Given Filter +# Author: Clayton Donley, Motorola, <donley@cig.mot.com> +# +# Demonstration of Synchronous Searching in PERL5. +# +# Rather than printing attribute and values directly, they are +# stored in a Hash, where further manipulation would be very simple. +# The output could then be printed to a file or standard output, or +# simply run through the modify or add commands. +# +# Usage: ldapwalk.pl FILTER +# Example: ldapwalk.pl "sn=Donley" +# + +use strict; +use Net::LDAPapi; + +# Define these values + +my $ldap_server = "localhost"; +my $BASEDN = "dc=example, dc=com"; +my $sizelimit = 100; # Set to Maximum Number of Entries to Return + # Can set small to test error routines +my $deref = "search"; + +# Various Variable Declarations... +my $ld; +my $dn; +my $attr; +my $ent; +my $ber; +my @vals; +my %record; +my $rc; +my $result; + +# +# Initialize Connection to LDAP Server + +if (($ld = new Net::LDAPapi($ldap_server)) == -1) +{ + die "Connection Failed!"; +} + +#ldap_set_option(0,LDAP_OPT_DEBUG_LEVEL,-1); + +# +# Bind as NULL User to LDAP connection $ld + +$ld->sasl_parms(-mech=>"DIGEST-MD5",-flags=>LDAP_SASL_AUTOMATIC); + +if ($ld->bind_s("benchmark","xxx",LDAP_AUTH_SASL) != LDAP_SUCCESS) +# if ($ld->bind_s != LDAP_SUCCESS) +{ + $ld->unbind; + die "bind: ", $ld->errstring, ": ", $ld->extramsg; +} +# +# This will set the size limit to $sizelimit from above. The command +# is a Netscape addition, but I've programmed replacement versions for +# other APIs. +$ld->set_option(LDAP_OPT_SIZELIMIT,$sizelimit); +# $ld->set_option(LDAP_OPT_DEREF,$deref); + +# This routine is COMPLETELY unnecessary in this application, since +# the rebind procedure at the end of this program simply rebinds as +# a NULL user. +#$ld->set_rebind_proc(&rebindproc); + +# +# Specify Search Filter and List of Attributes to Return + +my $filter = $ARGV[0]; +my @attrs = ("cn","mail","telephonenumber"); + +# +# Perform Search +my $msgid = $ld->search($BASEDN,LDAP_SCOPE_ONELEVEL,$filter,\@attrs,0); + +if ($msgid < 0) +{ + $ld->unbind; + die "search: ", $ld->errstring, ": ", $ld->extramsg; +} + +# Reset Number of Entries Counter +my $nentries = 0; + +# Set no timeout. +my $timeout = -1; + +# +# Cycle Through Entries +while (($rc = $ld->result($msgid,0,$timeout)) == LDAP_RES_SEARCH_ENTRY) +{ + $nentries++; + + for ($ent = $ld->first_entry; $ent != 0; $ent = $ld->next_entry) + { + +# +# Get Full DN + + if (($dn = $ld->get_dn) eq "") + { + $ld->unbind; + die "get_dn: ", $ld->errstring, ": ", $ld->extramsg; + } + +# +# Cycle Through Each Attribute + + for ($attr = $ld->first_attribute; $attr ne ""; $attr = $ld->next_attribute) + { + +# +# Notice that we're using get_values_len. This will retrieve binary +# as well as text data. You can change to get_values to only get text +# data. +# + @vals = $ld->get_values ($attr); + $record{$dn}->{$attr} = [@vals]; + } + } + $ld->msgfree; + +} +if ($rc == LDAP_RES_SEARCH_RESULT && + $ld->err != LDAP_SUCCESS) +{ + $ld->unbind; + die "result: ", $ld->errstring, ": ", $ld->extramsg; +} + +print "Found $nentries records\n"; + +$ld->unbind; + +foreach $dn (keys %record) +{ + my $item; + print "dn: $dn\n"; + foreach $attr (keys %{$record{$dn}}) + { + for $item ( @{$record{$dn}{$attr}}) + { + if ($attr =~ /binary/ ) + { + print "$attr: <binary>\n"; + } elsif ($attr eq "jpegphoto") { +# +# Notice how easy it is to take a binary attribute and dump it to a file +# or such. Gotta love PERL. +# + print "$attr: JpegPhoto (length: " . length($item). ")\n"; + open (TEST,">$dn.jpg"); + print TEST $item; + close (TEST); + } else { + print "$attr: $item\n"; + } + } + } +} + +exit; + +sub rebindproc +{ + + return("","",LDAP_AUTH_SIMPLE); +} + diff --git a/examples/ldapwalk.pl b/examples/ldapwalk.pl index 3cfbbbe..91b1a86 100755 --- a/examples/ldapwalk.pl +++ b/examples/ldapwalk.pl @@ -40,7 +40,7 @@ my $result; if (($ld = new Net::LDAPapi($ldap_server)) == -1) { - die "Connection Failed!"; + die "Unable to initialize!"; } #ldap_set_option(0,LDAP_OPT_DEBUG_LEVEL,-1); @@ -53,8 +53,9 @@ if (($ld = new Net::LDAPapi($ldap_server)) == -1) #if ($ld->bind_s("tester","tester",LDAP_AUTH_SASL) != LDAP_SUCCESS) if ($ld->bind_s != LDAP_SUCCESS) { + my $errstr=$ld->errstring; $ld->unbind; - die "bind: ", $ld->errstring, ": ", $ld->extramsg; + die "bind: ", $errstr; } # This will set the size limit to $sizelimit from above. The command @@ -114,7 +115,7 @@ while (1) # # Cycle Through Each Attribute - for ($attr = $ld->first_attribute; $attr ne ""; $attr = $ld->next_attribute) + for ($attr = $ld->first_attribute; defined($attr); $attr = $ld->next_attribute) { # @@ -129,7 +130,7 @@ while (1) $ld->msgfree; } -if ( $result == undef && $ld->err != LDAP_SUCCESS) +if ( !defined($result) && $ld->err != LDAP_SUCCESS) { $ld->unbind; die "result: ", $ld->errstring, ": ", $ld->extramsg; @@ -164,6 +165,7 @@ foreach $dn (keys %record) } } } + print "\n"; } exit; diff --git a/examples/ldapwalk2.pl b/examples/ldapwalk2.pl index 759e31d..eb1665a 100755 --- a/examples/ldapwalk2.pl +++ b/examples/ldapwalk2.pl @@ -36,7 +36,7 @@ my $attr; if (($ldcon = new Net::LDAPapi($ldap_server)) == -1) { - die "Unable to Open LDAP Connection"; + die "Unable to initialize!"; } if ($ldcon->bind_s != LDAP_SUCCESS) diff --git a/examples/testurl.pl b/examples/testurl.pl new file mode 100755 index 0000000..f6d9960 --- /dev/null +++ b/examples/testurl.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl -w +# +# testwrite.pl - Test of LDAP URL Operations in Perl5 +# Author: Clayton Donley <donley@cig.mot.com> +# +# This script tests some of the basic LDAP URL functions. +# Call the script with an LDAP URL to perform a search. + +use strict; +use Net::LDAPapi; + +my $urlhref; +my $url = $ARGV[0] || "ldap://ldap.four11.com/??sub?(cn=Clayton Donley)"; + +if (ldap_is_ldap_url($url)) +{ + $urlhref = ldap_url_parse($url); +} else { + die "$url: Not an LDAP Url."; +} + +if ($urlhref) +{ + print "host: " . $urlhref->{'host'} . "\n"; + print "port: " . $urlhref->{'port'} . "\n"; + print "base: " . $urlhref->{'dn'} . "\n"; + + my $attr; + foreach $attr (@{$urlhref->{'attr'}}) + { + print "attr: " . $attr . "\n"; + } + print "filter: " . $urlhref->{'filter'} . "\n"; + print "scope: " . $urlhref->{'scope'} . "\n"; + +# If using Netscape, there is an options key specifying the use of SSL, etc... + + if ($urlhref->{'options'}) + { + print "options: " . $urlhref->{'options'} . "\n" + } + + print "Connecting...\n"; + + my $port = $urlhref->{"port"} || 389; + my $ld = new Net::LDAPapi(-host=>$urlhref->{"host"},-port=>$port); + + if ($ld == -1) + { + die "Connection failed..."; + } + + $ld->bind_s; + + $ld->url_search_s($url,0); + + my %record = %{$ld->get_all_entries}; + + $ld->unbind; + + my @dns = (sort keys %record); + print $#dns+1 . " entries returned.\n"; + + foreach my $dn (@dns) + { + print "dn: $dn\n"; + foreach my $attr (keys %{$record{$dn}}) + { + foreach my $item (@{$record{$dn}{$attr}}) + { + if ($attr =~ /binary/) + { + print "$attr: binary - length=" . length($item) . "\n"; + } else { + print "$attr: $item\n"; + } + } + } + } + +} else { + print "Invalid LDAP URL: $url\n"; +} |