my $hostname = "ldap.example.com"; my $ldap = Net::LDAP->new($hostname) or die "Unable to connect to LDAP server $hostname: $@\n"; #### my $result = $ldap->bind(); #### my $binddn = "uid=jblow, ou=People, dc=Example, dc=Com"; my $password = readpassword(); my $result = $ldap->bind(dn => $binddn, password => $password); #### my $result = $ldap->bind(); if ($result->code) { # This makes Net::LDAP get the server response die "An error occurred binding to the LDAP server\n"; } #### use Net::LDAP::Util qw(ldap_error_text); my $result = $ldap->bind(); if ($result->code) { die "An error occurred binding to the LDAP server: " .ldap_error_text($result->code)."\n"; } #### sub ldapassert { my $mesg = shift; my $action = shift; if ($mesg->code) { die "An error occurred $action: " .ldap_error_text($mesg->code)."\n"; } return $mesg; } my $result = ldapassert($ldap->bind(), "binding to the server"); #### my $searchresult = $ldap->search(base => "ou=People, dc=Example, dc=Com", filter => "(objectClass=posixAccount)", scope => "one", attrs => ['cn', 'accountStatus'] ); #### my $searchresult = do_search($ldap); foreach my $entry ($searchresult->entries) { print "Matched: ", $entry->dn, "\n"; } #### my $sr = ldapassert($ldap->search(base => "ou=People, $ourdn", filter => "(objectClass=person)" scope => "one"), "searching the LDAP server"); foreach my $entry ($sr->entries) { # Getting the first value of these attributes # If they don't exist, we may be trying to unref undef here... my $cn = ${$entry->get('cn')}[0]; my $uid = ${$entry->get('uid')}[0]; print "$uid: $cn\n"; } #### sub changename { my $entry = shift; my $newname = shift; $entry->replace(cn => $newname); } #### # Add "extraClass" to everyone's "objectClass" attribute. my $sr = ldapassert($ldap->search(base => $ourdn, filter => "(objectClass=person)"), "searching the LDAP server"); foreach ($sr->entries) { $_->add(objectClass => "extraClass"); ldapassert($_->update($ldap), "updating the LDAP server"); } #### $_->replace(objectClass => [@{$_->get("objectClass")}, "extraClass"]