Net::LDAP - Lightweight Directory Access Protocol |
Net::LDAP - Lightweight Directory Access Protocol
use Net::LDAP;
$ldap = Net::LDAP->new( 'ldap.bigfoot.com' ) or die "$@";
$mesg = $ldap->bind ; # an anonymous bind
$mesg = $ldap->search( # perform a search base => "c=US", filter => "(&(sn=Barr) (o=Texas Instruments))" );
$mesg->code && die $mesg->error;
foreach $entry ($mesg->entries) { $entry->dump; }
$mesg = $ldap->unbind; # take down session
$ldap = Net::LDAP->new( 'ldap.umich.edu' );
# bind to a directory with dn and password $mesg = $ldap->bind( 'cn=root, o=University of Michigan, c=us', password => 'secret' );
$result = $ldap->add( 'cn=Barbara Jensen, o=University of Michigan, c=US', attr => [ 'cn' => ['Barbara Jensen', 'Barbs Jensen'], 'sn' => 'Jensen', 'mail' => 'b.jensen@umich.edu', 'objectclass' => ['top', 'person', 'organizationalPerson', 'inetOrgPerson' ], ] );
$result->code && warn "failed to add entry: ", $result->error ; $mesg = $ldap->unbind; # take down session
Net::LDAP is a collection of modules that implements a LDAP services API for Perl programs. The module may be used to search directories or perform maintenance functions such as adding, deleting or modifying entries.
This document assumes that the reader has some knowledge of the LDAP protocol.
HOST
may be a host name or an IP number. TCP port may be specified
after the host name followed by a colon (such as localhost:10389). The
default TCP port for LDAP is 389.
You can also specify a URI, such as 'ldaps://127.0.0.1:666' or 'ldapi://%2fvar%2flib%2fldap_sock'. Note that '%2f's in the LDAPI socket path will be translated into '/'. This is to support LDAP query options like base, search etc. although the query part of the URI will be ignored in this context. If port was not specified in the URI, the default is either 389 or 636 for 'LDAP' and 'LDAPS' schemes respectively.
HOST
may also be a reference to an array of hosts, host-port pairs
or URIs to try. Each will be tried in order until a connection is
made. Only when all have failed will the result of undef
be
returned.
HOST
.
HOST
.
(Default: ldap)
MultiHomed
parameter
when connecting to the remote server
LocalAddr
parameter, which
sets the client's IP address (as opposed to the server's IP address.)
-w
is in
effect. The method that was called will return undef
.
When running on Perl 5.8 and this option is given Net::LDAP converts all values of attributes not matching this REGEX into Perl UTF-8 strings so that the regular Perl operators (pattern matching, ...) can operate as one expects even on strings with international characters.
If this option is not given or the version of Perl Net::LDAP is running on is too old strings are encoded the same as in earlier versions of perl-ldap.
Example: raw => qr/(?i:^jpegPhoto|;binary)/
HOST
resolves to an
IPv6 target address.
If it resolves to an IPv4 address, the connection is tried using IPv4,
the same way as if this option was not given.
Please note that IPv6 support is considered experimental in IO::Socket::SSL, which is used of SSL/TLS support, and there are a few issues to take care of. See IPv6 in the IO::Socket::SSL manpage for details.
Example
$ldap = Net::LDAP->new( 'remote.host', async => 1 );
LDAPS connections have some extra valid options, see the start_tls method for details. Note the default value for 'sslversion' for LDAPS is 'sslv2/3', and the default port for LDAPS is 636.
For LDAPI connections, HOST is actually the location of a UNIX domain socket to connect to. The default location is '/var/run/ldapi'.
Each of the following methods take as arguments some number of fixed parameters followed by options, these options are passed in a named fashion, for example
$mesg = $ldap->bind( "cn=me,o=example", password => "mypasswd");
The return value from these methods is an object derived from the the Net::LDAP::Message manpage class. The methods of this class allow you to examine the status of the request.
ID
may be a number or an
object which is a sub-class of the Net::LDAP::Message manpage, returned from a
previous method call.
Example
$res = $ldap->search( @search_args );
$mesg = $ldap->abandon( $res ); # This could be written as $res->abandon
DN
can be either a
the Net::LDAP::Entry manpage object or a string.
VALUE
should be a string if only a single value is wanted, or a
reference to an array of strings if multiple values are wanted.
This argument is not used if DN
is a the Net::LDAP::Entry manpage object.
Example
# $entry is an object of class Net::LDAP::Entry $mesg = $ldap->add( $entry );
$mesg = $ldap->add( $dn, attrs => [ name => 'Graham Barr', attr => 'value1', attr => 'value2', multi => [qw(value1 value2)] ] );
DN
is the DN to bind with. An
anonymous bind may be done by calling bind without any arguments.
Example
$mesg = $ldap->bind; # Anonymous bind
$mesg = $ldap->bind( $dn, password => $password );
# $sasl is an object of class Authen::SASL $mesg = $ldap->bind( $dn, sasl => $sasl, version => 3 );
DN
on the
server. DN
may be a string or a the Net::LDAP::Entry manpage object.
Example
$mesg = $ldap->compare( $dn, attr => 'cn', value => 'Graham Barr' );
DN
from the server. DN
may be a string
or a the Net::LDAP::Entry manpage object.
Example
$mesg = $ldap->delete( $dn );
DN
on the server. DN
may be a string
or a the Net::LDAP::Entry manpage object.
DN
.
DN
.
Example
$mesg = $ldap->moddn( $dn, newrdn => 'cn=Graham Barr' );
DN
on the server. DN
may be a string or a the Net::LDAP::Entry manpage object.
VALUE
should be a
string if only a single value is wanted in the attribute, or a
reference to an array of strings if multiple values are wanted.
VALUE
should be a
string if only a single value is being deleted from the attribute, or
a reference to an array of strings if multiple values are being
deleted.
VALUE
. VALUE
should be a string if only a single value is wanted
in the attribute, or a reference to an array of strings if multiple
values are wanted. A reference to an empty array will remove the
entire attribute.
OP
should be
add, delete or replace. VALUE
should be either a string or a
reference to an array of strings, as before.
Use this form if you want to control the order in which the operations will be performed.
Example
$mesg = $ldap->modify( $dn, add => { sn => 'Barr' } );
$mesg = $ldap->modify( $dn, delete => [qw(faxNumber)] );
$mesg = $ldap->modify( $dn, delete => { 'telephoneNumber' => '911' } );
$mesg = $ldap->modify( $dn, replace => { 'mail' => 'gbarr@pobox.com' } );
$mesg = $ldap->modify( $dn, changes => [ # add sn=Barr add => [ sn => 'Barr' ], # delete all fax numbers delete => [ faxNumber => []], # delete phone number 911 delete => [ telephoneNumber => ['911']], # change email address replace => [ mail => 'gbarr@pobox.com'] ] );
The result is an object of class the Net::LDAP::Search manpage.
scope
parameter with one of the following values:
deref
parameter with one of the
following values:
If not specified, then the server will return the attributes that are specified as accessible by default given your bind credentials.
Certain additional attributes such as ``createTimestamp'' and other operational attributes may also be available for the asking:
$mesg = $ldap->search( ... , attrs => ['createTimestamp'] );
To retrieve the default attributes and additional ones, use '*'.
$mesg = $ldap->search( ... , attrs => ['*', 'createTimestamp'] );
To retrieve no attributes (the server only returns the DNs of matching entries), use '1.1':
$mesg = $ldap->search( ... , attrs => ['1.1'] );
When running on Perl 5.8 and this option is given Net::LDAP converts all values of attributes not matching this REGEX into Perl UTF-8 strings so that the regular Perl operators (pattern matching, ...) can operate as one expects even on strings with international characters.
If this option is not given or the version of Perl Net::LDAP is running on is too old strings are encodeed the same as in earlier versions of perl-ldap.
The value provided here overwrites the value inherited from the constructor.
Example: raw => qr/(?i:^jpegPhoto|;binary)/
Example
$mesg = $ldap->search( base => $base_dn, scope => 'sub', filter => '(|(objectclass=rfc822mailgroup)(sn=jones))' );
Net::LDAP::LDIF->new( \*STDOUT,"w" )->write( $mesg->entries );
If you set verify to optional or require, you must also set either cafile or capath. The most secure option is require.
If the private key is encrypted (highly recommended) then keydecrypt should be a subroutine that returns the decrypting key. For example:
$ldap = Net::LDAP->new( 'myhost.example.com', version => 3 ); $mesg = $ldap->start_tls( verify => 'require', clientcert => 'mycert.pem', clientkey => 'mykey.pem', keydecrypt => sub { 'secret'; }, capath => '/usr/local/cacerts/' );
The directory in 'capath' must contain certificates named using the hash value of the certificates' subject names. To generate these names, use OpenSSL like this in Unix:
ln -s cacert.pem `openssl x509 -hash -noout < cacert.pem`.0
(assuming that the certificate of the CA is in cacert.pem.)
See SSL_check_crl in the IO::Socket::SSL manpage for further information.
Example
$mesg = $ldap->unbind;
The following methods are for convenience, and do not return
Net::LDAP::Message
objects.
VALUE
is given the async mode will be set. The previous value
will be returned. The value is true if LDAP operations are being
performed asynchronously.
For example, to get the subject name (in a peculiar OpenSSL-specific format, different from RFC 1779 and RFC 2253) from the server's certificate, do this:
print "Subject DN: " . $ldaps->certificate->subject_name . "\n";
VALUE
is given the debug bit-value will be set. The previous
value will be returned. Debug output will be sent to STDERR
. The
bits of this value are:
1 Show outgoing packets (using asn_hexdump). 2 Show incoming packets (using asn_hexdump). 4 Show outgoing packets (using asn_dump). 8 Show incoming packets (using asn_dump).
The default value is 0.
subschemaSubentry namingContexts altServer supportedExtension supportedFeatures supportedControl supportedSASLMechanisms supportedLDAPVersion
The result is an object of class the Net::LDAP::RootDSE manpage.
Example
my $root = $ldap->root_dse; # get naming Context $root->get_value( 'namingContext', asref => 1 ); # get supported LDAP versions $root->supported_version;
As the root DSE may change in certain circumstances - for instance when you change the connection using start_tls - you should always use the root_dse method to return the most up-to-date copy of the root DSE.
The result is an object of class the Net::LDAP::Schema manpage. Read this documentation for further information about methods that can be performed with this object.
Example
my $schema = $ldap->schema; # get objectClasses @ocs = $schema->all_objectclasses; # Get the attributes @atts = $schema->all_attributes;
IO::Socket
object being used.
undef
in case of LDAPI connections.
As the value returned is that element of the constructor's HOST argument with which the connection was established this may or may not be a legal URI.
MESG
request to be completed by the server. If no
MESG
is given, then wait for all outstanding requests to be completed.
Returns an error code defined in the Net::LDAP::Constant manpage.
MESG
is specified then return as soon as MESG
has been processed.
Returns an error code defined in the Net::LDAP::Constant manpage.
Many of the methods described above accept a control option. This allows the user to pass controls to the server as described in LDAPv3.
A control is a reference to a HASH and should contain the three
elements below. If any of the controls are blessed then the
method to_asn
will be called which should return a reference
to a HASH containing the three elements described below.
For most purposes the Net::LDAP::Control manpage objects are the easiest way to generate controls.
Most of the above commands accept a callback option. This option should be a reference to a subroutine. This subroutine will be called for each packet received from the server as a response to the request sent.
When the subroutine is called the first argument will be the the Net::LDAP::Message manpage object which was returned from the method.
If the request is a search then multiple packets can be received from the server. Each entry is received as a separate packet. For each of these the subroutine will be called with a the Net::LDAP::Entry manpage object as the second argument.
During a search the server may also send a list of references. When such a list is received then the subroutine will be called with a the Net::LDAP::Reference manpage object as the second argument.
Net::LDAP also exports constants for the error codes that can be received from the server, see the Net::LDAP::Constant manpage.
the Net::LDAP::Constant manpage, the Net::LDAP::Control manpage, the Net::LDAP::Entry manpage, the Net::LDAP::Filter manpage, the Net::LDAP::Message manpage, the Net::LDAP::Reference manpage, the Net::LDAP::Search manpage, the Net::LDAP::RFC manpage
The homepage for the perl-ldap modules can be found at http://ldap.perl.org/.
This document is based on a document originally written by Russell Fulton <r.fulton@auckland.ac.nz>.
Chris Ridd <chris.ridd@isode.com> for the many hours spent testing and contribution of the ldap* command line utilities.
A discussion mailing list is hosted by the Perl Foundation at <perl-ldap@perl.org> No subscription is necessary!
We hope you do not find any, but if you do please report them to the mailing list.
If you have a patch, please send it as an attachment to the mailing list.
Graham Barr <gbarr@pobox.com>
Copyright (c) 1997-2004 Graham Barr. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Net::LDAP - Lightweight Directory Access Protocol |