From ba0486aad6de3ed31d0fcfb9f65abc609633051a Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Tue, 13 Nov 2007 19:02:38 -0600 Subject: [PATCH] LDAP test and example LDIF file. Signed-off-by: Chris Cormack Signed-off-by: Joshua Ferraro --- t/LDAP/example3.ldif | 118 +++++++++++++++++++++++++++++++++++++++++++ t/LDAP/joe_ldap.pl | 99 ++++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 t/LDAP/example3.ldif create mode 100755 t/LDAP/joe_ldap.pl diff --git a/t/LDAP/example3.ldif b/t/LDAP/example3.ldif new file mode 100644 index 0000000000..a389095a7f --- /dev/null +++ b/t/LDAP/example3.ldif @@ -0,0 +1,118 @@ +dn: dc=metavore,dc=com +dc: metavore +description: Metavore as a company +objectclass: dcObject +objectclass: organization +objectclass: top +o: metavore + +# dn: cn=Manager,dc=metavore,dc=com +# objectclass: top +# objectclass: organizationalRole +# description: Manager of LDAP +# cn: Manager + +dn: ou=people,dc=metavore,dc=com +objectClass: organizationalunit +objectClass: top +ou: people +description: Persons in the organization + +dn: cn=jts,dc=metavore,dc=com +objectClass: inetOrgPerson +o: metavore +postalAddress: 345 Fake Street +l: Cleveland +st: Ohio +postalCode: 43366 +initials: jts +cn: jts +cn: John Smith +cn: John T. Smith +givenname: John +sn: Smith +userid: jts +userPassword: password1 +sn;lang-en: Smith +sn;lang-de: Schmidt +ou: people +mail: fake_user@liblime.com +telephoneNumber: 1 555 123-4567 + +dn: cn=sss,dc=metavore,dc=com +objectClass: inetOrgPerson +o: metavore +postalAddress: Hugstetter Str. 55 +l: Freiburg +st: Baden-Wurttemberg +postalCode: 79106 +initials: sss +cn: sss +cn: Steve Smith +cn: Steve S. Smith +givenname: Steve +sn: Smith +userid: sss +userPassword: password1 +sn;lang-en: Smith +sn;lang-de: Schmidt +ou: people +mail: fake_user@liblime.com +telephoneNumber: +49 761 270-2020 + +dn: cn=rch,dc=metavore,dc=com +objectClass: inetOrgPerson +o: metavore +postalAddress: 449 E. State St. +l: Athens +st: Ohio +initials: rch +cn: rch +cn: Ryan Higgins +givenname: Ryan +sn: Higgins +userPassword: password2 +userid: rch +ou: people +mail: rch@liblime.com +telephoneNumber: 1 740 593-6589 + +dn: cn=jmf,dc=metavore,dc=com +objectClass: person +objectClass: inetOrgPerson +o: metavore +postalAddress: 449 E. State St. +l: Athens +st: Ohio +initials: jmf +cn: jmf +cn: Josh M. Ferraro +givenname: Josh +sn: Ferraro +userid: jmf +userPassword: password3 +ou: people +mail: jmf@liblime.com +telephoneNumber: 1 740 707 7654 + +# dn: o=University of Alaska Fairbanks, c=US +# o: University of Alaska Fairbanks +# description: Preparing Alaska for a brave new yesterday +# description: leaf node only +# +# dn: o=University of Colorado at Boulder, c=US +# o: University of Colorado at Boulder +# description: No personnel information +# description: Institution of education and research +# +# dn: o=University of Colorado at Denver, c=US +# o: University of Colorado at Denver +# o: UCD +# o: CU/Denver +# o: CU-Denver +# description: Institute for Higher Learning and Research +# +# dn: o=University of Florida, c=US +# o: University of Florida +# o: UFl +# description: Warper of young minds diff --git a/t/LDAP/joe_ldap.pl b/t/LDAP/joe_ldap.pl new file mode 100755 index 0000000000..1a67748f0c --- /dev/null +++ b/t/LDAP/joe_ldap.pl @@ -0,0 +1,99 @@ +#!/usr/bin/perl +# +# To start out, try something like this against your LDAP: +# ldapadd -w metavore -D'cn=Manager,dc=metavore,dc=com' -c -f example3.ldif +# ldapmodify -w metavore -D'cn=Manager,dc=metavore,dc=com' -c -f example3.ldif +# +# Then run this script to test perl interaction w/ LDAP. +# + +use warnings; +use strict; + +use Net::LDAP; +use Net::LDAP::Filter; + +my $host = (@ARGV) ? shift : 'localhost'; +my $filter = Net::LDAP::Filter->new((@ARGV) ? shift : 'objectClass=inetOrgPerson'); +my %params = ( + base => (@ARGV) ? shift : 'dc=metavore,dc=com', + filter => $filter, +); + +my $ldap = Net::LDAP->new($host) or die "Cannot connect to ldap on $host"; +$ldap->bind("cn=Manager," . $params{'base'}, password=>'metavore') or die "Cannot bind to ldap on $host"; +&ldap_dse; +&ldap_search; +&ldap_add; +&ldap_search; + +sub hashup { + my $query = shift or die "Bad hashup call"; + my %memberhash = (); + my $key; + foreach my $user ($query->shift_entry){ + foreach my $k (@$user) { + foreach my $k2 ( keys %$k ) { + if ($k2 eq 'type') { + $key = $$k{$k2}; + } else { + $memberhash{$key} .= map {$_ . " "} @$k{$k2}; + } + } + } + } + return %memberhash; +} + +sub recursive_breakdown { + my $dse = shift or return undef; + if (ref($dse) =~ /HASH/) { + return join "\n", map {"$_\t=> " . recursive_breakdown($dse->{$_})} keys %$dse; + } elsif (ref($dse) =~ /ARRAY/) { + return " (\n" . join("\n", map {recursive_breakdown($_)} @$dse) . "\n)\n"; + } else { + return $dse; + } +} + +sub ldap_dse { + print "my root DSE: \n"; + print recursive_breakdown $ldap->root_dse(); +} + +sub ldap_search { + my $query = $ldap->search(%params) or print "Search failed\n"; + $query->code and die sprintf 'error (code:%s) - %s', $query->code , $query->error; + my $size = scalar($query->entries); + my $i=5; + print "\nNumber of records returned from search: $size.\n"; + ($size > $i) and print "Displaying the last $i records.\n\n"; + foreach ($query->entries) { + ($size-- > $i) and next; + $_->dump; + } +} + +sub ldap_add { + my $cn = shift or return 0; + my $mail = lc $cn; + $mail =~ s/\s+/./; + print "Adding user $cn\n"; + my $add; + $add = $ldap->add( + "cn=$cn," . $params{'base'}, + attr => [ + cn => $cn, + sn => 'atz', + mail => $mail . '@liblime.com', + telephoneNumber => '1 614 266 9798', + description => 'Implementer and Destroyer', + objectclass => ['person','inetOrgPerson'], + ]) + or printf "Add failed (code %s): %s\n", ($add->code||'unknown'), ($add->error||'unknown'); +} + +END { + ($ldap) and $ldap->unbind; + print "\ndone.\n"; +} -- 2.39.5