#!/usr/bin/perl

use Net::LDAP;
use Net::LDAP::Extension::WhoAmI;

my $mesg;
my $ldap = Net::LDAP->new("ldap://localhost");

my $root_bind = $ldap->bind("cn=admin,dc=example,dc=com", password => "secret");
if ( $root_bind->is_error) {
    print "BIND error ".$root_bind->error."\n";
}

$mesg = $ldap->who_am_i();

print "you are bound with authzId ", $mesg->response(), "\n";

my $fail_bind = $ldap->bind("cn=admin,dc=example,dc=com", password => "wrong");
if ( $fail_bind->is_error) {
    print "BIND error ".$fail_bind->error."\n";
}

$mesg = $ldap->who_am_i();

print "you are bound with authzId ", $mesg->response(), "\n";

my $user_bind = $ldap->bind("uid=coudot,ou=users,dc=example,dc=com", password => "secret");
if ( $user_bind->is_error) {
    print "BIND error ".$user_bind->error."\n";
}

$mesg = $ldap->who_am_i();

print "you are bound with authzId ", $mesg->response(), "\n";
