Novell Home

Modify octet values in eDirectory using LDAP and Perl

From Developer Community

This sample shows how to use the perl-ldap module to modify octet values retrieved from eDirectory.

Sample Code

#!perl
use Net::LDAP;
use Net::LDAP::LDIF;
use File::Path qw(rmtree);
use File::Basename qw(basename);

print "Content-type: text/html\n\n";

$HOST = '137.65.138.159';
$PORT = 389;
$PASSWD   = 'novell';
$BASEDN   = "o=novell";
$ADMIN= "cn=admin,o=novell";
$searchstring="(&(objectclass=user)(cn=pjones))";
$attnames=["cn","loginallowedtimemap"];

#connect to the server
until($ldap = Net::LDAP->new($HOST, port => $PORT)) {
  die "Can not connect to ldap://$HOST:$PORT/" if ++$count > 10;
  sleep 1;
}

#bind as admin
$r = $ldap->bind($ADMIN, password => $PASSWD, version=>3);
die $r->error if $r->code;

#find a user object with a loginallowedtimemap
$r = $ldap->search(	base 	=> 	$BASEDN,
					scope	=>	'subtree',
					filter 	=> 	$searchstring,
					attrs	=>	$attnames);


foreach my $entry ($r->entries){
	print $entry->dn."<br>";
	my $latm = $entry->get_value('loginallowedtimemap');
#decode the time map
	my $bits = unpack "b*",$latm;
	print "$bits<br>";
#swap all the bits
	$bits =~ s/0/2/gs;
	$bits =~ s/1/0/gs;
	$bits =~ s/2/1/gs;
	print "$bits<br>";
#encode the time map
	$latm = pack "b336", $bits;
#update the directory
	$r = $ldap->modify($entry->dn, replace => { loginallowedtimemap => $latm });
	die $r->error if $r->code;
}
print "Finished";
$ldap->unbind;

--Paul Jones

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.