/usr/share/perl5/Net/LDAP/Examples.pod is in libnet-ldap-perl 1:0.5800-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | =head1 NAME
Net::LDAP::Examples - PERL LDAP by Example
=head1 DESCRIPTION
The following examples are of course PERL code, found to work
with the Net::LDAP modules.
The intent of this document is to give the reader a I<cut and paste>
jump start to getting an LDAP application working.
Below you will find snippets of code that should work as-is with only
a small amount of work to correct any variable assignments and LDAP
specifics, e.g. Distinguished Name Syntax, related to the user's
own implementation.
The I<S>tandard I<O>perating I<P>rocedure that is followed here is:
=over 8
=item 1 Package - use Net::LDAP
=item 2 Initialization - new
=item 3 Binding - bind
=item 4 Operation - add modify moddn search
=item 4.1 Processing - displaying data from a search
=item 5 Error - displaying error information
=item 6 Unbinding - unbind
=back
Look to each of these for a snippet of code to meet your needs.
B<What is not covered in these examples at this time:>
=over 4
=item I<abandon> and I<compare> methods
=item I<callback> subroutines
=back
=head1 CODE
=head2 PACKAGE - Definitions
use Net::LDAP;
=head2 INITIALIZING
$ldap = Net::LDAP->new ( "yourLDAPhost.yourCompany.com" ) or die "$@";
=head2 BINDING
$mesg = $ldap->bind ( version => 3 ); # use for searches
$mesg = $ldap->bind ( "$userToAuthenticate",
password => "$passwd",
version => 3 ); # use for changes/edits
# see your LDAP administrator for information concerning the
# user authentication setup at your site.
=head2 OPERATION - Generating a SEARCH
sub LDAPsearch
{
my ($ldap,$searchString,$attrs,$base) = @_;
# if they don't pass a base... set it for them
if (!$base ) { $base = "o=mycompany, c=mycountry"; }
# 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
);
}
my @Attrs = ( ); # request all available attributes
# to be returned.
my $result = LDAPsearch ( $ldap, "sn=*", \@Attrs );
=head2 PROCESSING - Displaying SEARCH Results
#------------
#
# Accessing the data as if in a structure
# i.e. Using the "as_struct" method
#
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
}
#
# end of as_struct method
#
#--------
#------------
#
# handle each of the results independently
# ... i.e. using the walk through method
#
my @entries = $result->entries;
my $entr;
foreach $entr ( @entries ) {
print "DN: ", $entr->dn, "\n";
my $attr;
foreach $attr ( sort $entr->attributes ) {
# skip binary we can't handle
next if ( $attr =~ /;binary$/ );
print " $attr : ", $entr->get_value ( $attr ) ,"\n";
}
print "#-------------------------------\n";
}
#
# end of walk through method
#------------
=head2 OPERATION - Modifying entries
#
# Modify
#
# for each of the modifies below you'll need to supply
# a full DN (Distinguished Name) for the $dn variable.
# example:
# cn=Jo User,ou=person,o=mycompany,c=mycountry
#
# I would recommend doing a search (listed above)
# then use the dn returned to populate the $dn variable.
#
# Do we only have one result returned from the search?
if ( $result->count != 1 ) { exit; } # Nope.. exit
my $dn = $entries[0]->dn; # yes.. get the DN
#######################################
#
# MODIFY using a HASH
#
my %ReplaceHash = ( keyword => "x", proxy => "x" );
my $result = LDAPmodifyUsingHash ( $ldap, $dn, \%ReplaceHash );
sub LDAPmodifyUsingHash
{
my ($ldap, $dn, $whatToChange ) = @_;
my $result = $ldap->modify ( $dn,
replace => { %$whatToChange }
);
return $result;
}
#######################################
#
# MODIFY using a ARRAY List
#
my @ReplaceArrayList = [ 'keyword', "xxxxxxxxxx",
'proxy' , "yyyyyyyyyy" ];
my $result = LDAPmodifyUsingArrayList ( $ldap, $dn, \@ReplaceArrayList );
sub LDAPmodifyUsingArrayList
{
my ($ldap, $dn, $whatToChange ) = @_;
my $result = $ldap->modify ( $dn,
changes => [
replace => @$whatToChange
]
);
return $result;
}
#######################################
#
# MODIFY using a ARRAY
#
my @ReplaceArray = ( 'keyword', "xxxxxxxxxx" ,
'proxy' , "yyyyyyyyyy" );
my $result = LDAPmodifyUsingArray ( $ldap, $dn, \@ReplaceArray );
sub LDAPmodifyUsingArray
{
my ($ldap, $dn, $whatToChange ) = @_;
my $result = $ldap->modify ( $dn,
changes => [
replace => [ @$whatToChange ]
]
);
return $result;
}
#######################################
#
# MODIFY an existing record using 'Changes'
# (or combination of add/delete/replace)
#
my @whatToChange;
my @ReplaceArray;
my @DeleteArray;
my @AddArray;
push @AddArray, 'cn', "me myself";
push @ReplaceArray, 'sn', '!@#$%^&*()__+Hello There';
push @ReplaceArray, 'cn', "me myself I";
push @DeleteArray, 'cn', "me myself";
if ( $#ReplaceArray > 0 ) {
push @whatToChange, 'replace';
push @whatToChange, \@ReplaceArray;
}
if ( $#DeleteArray > 0 ) {
push @whatToChange, 'delete';
push @whatToChange, \@DeleteArray;
}
if ( $#AddArray > 0 ) {
push @whatToChange, 'add';
push @whatToChange, \@AddArray;
}
$result = LDAPmodify ( $ldap, $dn, \@whatToChange );
sub LDAPmodify
{
my ($ldap, $dn, $whatToChange) = @_;
my $result = $ldap->modify ( $dn,
changes => [
@$whatToChange
]
);
return $result;
}
=head2 OPERATION - Changing the RDN
my $newRDN = "cn=Joseph User";
my $result = LDAPrdnChange ( $ldap, $dn, $newRDN, "archive" );
sub LDAPrdnChange
{
my ($ldap,$dn,$whatToChange,$action) = @_;
my $branch;
#
# if the archive action is selected, move this
# entry to another place in the directory.
#
if ( $action =~ /archive/i ) {
$branch = "ou=newbranch, o=mycompany, c=mycountry";
}
#
# use the 'deleteoldrdn' to keep from getting
# multivalues in the NAMING attribute.
# in most cases that would be the 'CN' attribute
#
my $result = $ldap->moddn ( $dn,
newrdn => $whatToChange,
deleteoldrdn => '1',
newsuperior => $branch
);
return $result;
}
=head2 OPERATION - Adding a new Record
my $DNbranch = "ou=bailiwick, o=mycompany, c=mycountry";
#
# check with your Directory Schema or Administrator
# for the correct objectClass... I'm sure it'll be different
#
my $CreateArray = [
objectClass => [ "top", "person", "organizationalPerson", "inetOrgPerson" ],
cn => "Jane User",
uid => "0000001",
sn => "User",
mail => "JaneUser@mycompany.com"
];
#
# create the new DN to look like this
# " cn=Jo User + uid=0000001 , ou=bailiwick, o=mycompany, c=mycountry "
#
# NOTE: this DN MUST be changed to meet your implementation
#
my $NewDN = "@$CreateArray[2]=".
"@$CreateArray[3]+".
"@$CreateArray[4]=".
"@$CreateArray[5],".
$DNbranch;
LDAPentryCreate($ldap, $NewDN, $CreateArray);
#
# CreateArray is a reference to an anonymous array
# you have to dereference it in the subroutine it's
# passed to.
#
sub LDAPentryCreate
{
my ($ldap, $dn, $whatToCreate) = @_;
my $result = $ldap->add ( $dn, attrs => [ @$whatToCreate ] );
return $result;
}
=head2 ERROR - Retrieving and Displaying ERROR information
if ( $result->code ) {
#
# if we've got an error... record it
#
LDAPerror ( "Searching", $result );
}
sub LDAPerror
{
my ($from, $mesg) = @_;
print "Return code: ", $mesg->code;
print "\tMessage: ", $mesg->error_name;
print " :", $mesg->error_text;
print "MessageID: ", $mesg->mesg_id;
print "\tDN: ", $mesg->dn;
#---
# Programmer note:
#
# "$mesg->error" DOESN'T work!!!
#
#print "\tMessage: ", $mesg->error;
#-----
}
=head2 UNBIND
$ldap->unbind;
=head1 LDAP SCHEMA RETRIEVAL
The following code snippet shows how to retrieve schema information.
The first procedure is to initialize a new LDAP object using the
same procedures as listed at the beginning of this document.
The second procedure is to bind to your directory server. Some
servers may require authentication to retrieve the schema from the
directory server. This procedure is listed at the beginning of
this document too.
After a successful bind you are ready to retrieve the schema
information. You do this by initializing a schema object.
$schema = $ldap->schema ( );
In this case Net::LDAP will attempt to determine the dn under which
the schema can be found. First it will look for the attribute
C<subschemaSubentry> in the root DSE. If that cannot be found then
it will default to the assumption of C<cn=schema>
Alternatively you can specify the dn where the schema is to be found
with
$schema = $ldap->schema ( dn => $dn );
Once we have a dn to search for, Net::LDAP will fetch the schema entry with
$mesg = $self->search ( base => $dn,
scope => 'base',
filter => '(objectClass=subschema)',
);
Once the schema object has been initialized, schema methods
are used to retrieve the data. There are a number of ways this
can be done. Information on the schema methods can be found
in the Net::LDAP::Schema pod documentation.
The following is a code snippet showing how to get and display
information about returned attributes.
#
# Get the attributes
#
@attributes = $schema->all_attributes ( );
#
# Display the attributes
#
foreach $ar ( @attributes ) {
print "attributeType: ", $ar->{name}, "\n";
#
# Print all the details
#
foreach $key ( keys %{$ar} ) {
print join ( "\n\t\t", "\t$key:",
ref ( $ar->{$key} ) ? @{$ar->{$key}} : $ar->{$key}
), "\n";
}
}
The process is the basically the same for getting objectClass
information. Where schema-E<gt>all_attributes() is used, substitute
schema-E<gt>all_objectclasses(). From that point on the process is
the same for both objectClasses and attributes.
=head1 BUGS
None known, but there may be some
=head1 AUTHOR (of this document)
Russell Biggs E<lt>rgb@ticnet.comE<gt>
=head1 COPYRIGHT
All rights to this document are hereby relinquished to Graham Barr.
=cut
|