NAME

ldaputils -- Basic Ldap functions for adding, modifying and removing objects


SYNOPSIS

 use ldaputils;

 $ldap = ldaputils->new(configfile       => '/path/to/configfile',
                        ldap_bind_user   => 'ldap_bind_user',
                        ldap_bind_secret => 'ldap_bind_secret');


DESCRIPTION

ldaputils is collection of basic functions to add, remove and modify ldap objects.

The basic idea for writing this module was to get some basic functions which simplify implementing scripts to deal with the various LDAP objects.

The difficulty was that different LDAP object classes differ in number, names and type of attributes. Therefore any information about this classes is kept in a separate schema file. See CONFIGURATION FILES below.

Some basic configuration stuff is kept in a separate configuration file. See CONFIGURATION FILES below.


CONSTRUCTOR

new

Constructor to create a new ldaputils object. Parameters are given in form of a hash as key - value pairs.

configfile

Full or relative path name of the config file to be read

ldap_bind_username

Username to use for the ldap bind. (username _not_ dn)

ldap_bind_secret

Password to use for ldap bind

Example

$ldaputils = new ldaputils( configfile=>'/usr/local/ldap/etc/ldaputils.conf );


METHODS


OBJECT RELATED METHODS

get_objects

Performs an ldap search and returns all objects found. The results are returned as reference to a result hash. Keys in the result hash are the values found in the attribute named by 'keyattr'. Every value in the result hash is a reference to hash containing a single object. Keys are the attribute names from the 'attr' array. Every entry in this hash is a reference to an array containing all the values for one attribute.

Got it? ;-)

Better take a look at the example below.

Parameters are given in form of a hash as key - value pairs.

filter

Filterstring to use for the ldap search

keyattr

Object's attribute to use as a primary key in the result hash

attr

Refernce to an array containing the attributes to retrieve for all found ldap objects

Example Call get_objects as

$result = $ldaputils -> get_objects( filter => ``(objectClass=sambaAccount)'', keyattr => ntUid, attr => [rid,smbHome,profile]);

The filter (objectClass=sambaAccount) should return several LDAP objects. Eg: One entry for 'Administrator' and one entry for 'nobody'.

'ntUid' is given as 'keyattr' and the values found in the attribute 'ntUid' of the objects 'Administrator' and 'nobody' will be used as primary key in the result hash.

Therefore you can access these two entries by means of:

$$result{Administrator} and $$result{nobody}

The $$result is necessary as $result is only a reference to the result hash. You can now access the additional attributes fetched by means of: $$result{Administrator}{rid} or $$result{nobody}{profile}

As every attribute might contain multiple values (think of the sambaMember attribute) the above are refernces to arrays: $$result{Administrator}{rid}[0] should contain the Administrators rid: '1F4'

Sometimes you may want to access all the keys in the result hash. Consider something like:

foreach $user ( @all_found_users ) {...}

Therefore you have to use a syntax like this:

foreach $user ( keys %{$result} ) {...}

or even more complicated if you want to run something like:

foreach $attribute ( @all_found_attributes ) {...}

use

foreach $attribute ( keys %{${$result}{nobody}} ) {...}

get_object

Gets exaktly one ldap object, can be use to get objects of whom you now they are unique. Think of users, groups, ...

The object_type used should be one which is defined in the schema file, as get_object calls get_objects with a filter build of the objectclass taken from the schemafile and an additional filter build of the given attr_name and attr_value. If more or less than one object is found, an empty result is returned. Parameter hash:

object_type

Object type from the schema definitions file

attr_name

Attribute name to be used as search criteria

attr_value

Attribute value to be used as search criteria

Example $obj_handle = $ldaputils -> get_object( object_type => 'user', attr_name => 'uid', attr_value => 'root' );

print $$obj_handle{cn};

get

Similar to get_object but you only have to supply object_type and unique_id as parameters. The object_type used should be one which is defined in the schema file, as get calls get_object. If more or less than one object is found, an empty result is returned. Parameter hash:

object_type

Object type from the schema definitions file

unique_id

Attribute name to be used as search criteria

Example $obj_handle = $ldaputils -> get( object_type => 'user', unique_id => 'root' );

print $$obj_handle{cn};

object_exists

Checks if the specified object exists in the LDAP database. Calls get_object, checks the result and return true or false. Parameter hash:

object_type

Object type from the schema definitions file

attr_name

Attribute name to be used as search criteria

attr_value

Attribute value to be used as search criteria

Example if ( $ldaputils -> object_exists( object_type => 'user', attr_name => 'uid', attr_value => 'root' ) ) {...}

exists

Similar to object_exists but you only have to supply object_type and unique_id as parameters.

object_type

Object type from the schema definitions file

unique_id

Attribute name to be used as search criteria

Example if ( $ldaputils -> exists( object_type => 'user', unique_id => 'root' ) ) {...}

get_new_object

Creates a new object hash. Creates an empty object hash, sets all the argument values given on the command line. Normally it should be easier to use add_object instead, but sometimes it might be necessary to modify the newly created object, before adding it to the LDAP Directory. Parameter hash:

object_type

Type of the object to fill with the commandline args (schema file)

unique_id

Attribute value of the unique_id field (eg: name)

cmdl_args

Reference to a hash containing the commandline arguments

Example $new_obj_handle = $ldaputils -> get_new_object( object_type => 'user', unique_id => 'root', cmdl_args => {uid => ``2087'', homeDirectory => ``/home/newuser''} );

add_object

Creates a new LDAP object. Creates an empty object, sets all the commandline argument values and adds the object to the LDAP directory. Gets the objectclass and unique_id fieldname from the schemafile. Parameter hash:

object_type

Type of the object to create

unique_id

Attribute value of the unique_id field

cmdl_args

Reference to a hash containing the commandline arguments

Example $ldaputils -> add_object( object_type => 'user', unique_id => 'newuser', cmdl_args => \%args );

mod_object

Modifies an LDAP object. Gets an existing LDAP object, sets all the commandline argument values and modifies the LDAP object. Gets the objectclass and unique_id fieldname from the schemafile. Parameter hash:

object_type

Type of the LDAP object to modify

unique_id

Attribute value of the unique_id field

cmdl_args

Reference to a hash containing the commandline arguments

Example $ldaputils -> mod_object( object_type => 'user', unique_id => 'newuser', cmdl_args => \%args );

compare_object

Compares the attributes from an existing LDAP object to the given commandline arguments. Gets the objectclass and unique_id fieldname from the schemafile. Parameter hash:

object_type

Type of the object in LDAP directory to compare to commandline arguments

unique_id

Attribute value of the unique_id field

cmdl_args

Reference to a hash containing the commandline arguments

Example if ( $ldaputils -> compare_object( object_type => 'user', unique_id => 'newuser', cmdl_args => \%args ) ) {...}

del_object

Deletes an ldap object. Gets the objectclass and unique_id fieldname from the schemafile. Parameter hash:

object_type

Type of the object to delete

unique_id

Attribute value of the unique_id field

cmdl_args

Reference to a hash containing the commandline arguments keys are the argument names.

Example $ldaputils -> del_object( object_type => 'user', unique_id => 'newuser' );

ATTRIBUTE RELATED METHODS

get_dn

Gets the distinguished name for an existing object Gets the objectclass and unique_id fieldname from the schemafile. Parameter hash:

object_type

Type of the object to fill with the commandline args

unique_id

Attribute value of the unique_id field to fetch the dn for

Example $dn = $ldaputils -> get_dn( object_type => 'user', unique_id => 'newuser' );

get_new_dn

Creates the distinguished name for a new object Gets the objectclass and unique_id fieldname from the schemafile. Parameter hash:

object_type

Type of the object to fill with the commandline args

unique_id

Attribute value of the unique_id field to fetch the dn for

cmdl_args

Reference to a hash containing the commandline arguments keys are the argument names.

Example $newdn = $ldaputils -> get_new_dn( object_type => 'user', unique_id => 'newuser', cmdl_args => \%args );

get_objectclass

Gets the objectClass for an existing object. Gets the objectclass and unique_id fieldname from the schemafile.

object_type

Type of the object to fill with the commandline args

unique_id

Attribute value of the unique_id field to fetch the dn for

Example $objclass = $ldaputils -> get_objectclass( object_type => 'user', unique_id => 'root' );

get_new_objectclass

Creates the objectClass for a new object Gets the objectclass and unique_id fieldname from the schemafile. Parameter hash:

object_type

Type of the object to fill with the commandline args

Example $newobjclass = $ldaputils -> get_new_objectclass( object_type => 'user' );

OTHER METHODS

get_free_account_id

Gets the next free account id. Start from the sys_accout_rid value in the configfile and searches for any unused uid/rid pair. Returns the decimal id.

Example $free_id = $ldaputils -> get_free_account_id();

set_free_account_id

Checks if the next free account id in the sambaConfig entry has to be increased.

Example $ldaputils -> set_free_account_id( id => $free_id );

ldap_add

adds an object to the ldap database Parameter hash:

dn

Distinguished name of the object to add

attr

Reference to a hash. keys are the attribute names of the LDAP object, hash values are the attribute values.

ldap_mod

Modifies an object in the ldap database Parameter hash:

dn

Distinguished name of the object to modify

attr

Reference to a hash. keys are the attribute names of the LDAP object, hash values are the attribute values.

ldap_del

Deletes an object from the ldap database Parameter hash:

dn

Distinguished name of the object to delete

CONFIGURATION FILES

Configuration File

The full path to the configuration file has to be passed to the CONSTRUCTOR. The configuration file contains some basic information needed by the ldaputils module. Below you find an example configuration file with explanations for each parameter.

ldap_host = localhost

Hostname or IP-address of the LDAP Server.

ldap_basedn = dc=pingworks,dc=de

Distinguished name of the LDAP base.

sys_schemafile = ldaputils.schema

Full path to the schema file. See CONFIGURATION FILES below.

sys_logfile = ldaputils.log

Full path to the log file. If no log filename is supplied, no logging is done

sys_mkntpwd = /usr/local/ldaputils/sbin/mkntpwd

Full path to the mkntpwd utility.

sys_auth_allow_pwdfiles = 1

Allow password files to be used. with sys_auth_allow_pwdfiles = 0 the user is prompted for a password to use for the LDAP bind. With sys_auth_allow_pwdfiles = 1 the password is read from the file ~/.ldaputils if it exists (see below).

sys_auth_pwdfile = .ldaputils

Name of the password file to use.

sys_passwordtype = [crypt|md5]

Creates crypt or md5 passwords.

sys_debuglevel = 10

Debug Level between 0 and 10.

cyrus = [0|1]

Add and delete cyrus mailbox with useraccounts

cyrus_cyradm = /usr/bin/cyradm

Full path to the cyradm binary

cyrus_host = mail.pingworks.de

Cyrus Imap Server

cyrus_admin = root

Cyrus Admin user

System Parameters

The following options have to be present but you should never need to modify this parameters.

sys_ldap_bind_objecttype = user

sys_cmdl_ou = ou

sys_nextrid_filterclass = SambaConfig

sys_nextrid_filterattr = id

sys_nextrid_filtervalue = root

sys_nextrid_attr = nextRid

Schema File

The schema file contains all information about the object classes used by ldaputils.

For every object class it defines the attribute names, their default values and the commandline switches which should be evaluated to handle the attributes. Let's take a look at an example schema file:
{
  user => {
     default_ou => "ou=People,dc=pingworks,dc=de",
     name_attr => [ "uid", "cn", "ntUid" ],
     unique_id => "uid",
     account_id => {uidNumber=>"dec", rid=>"hex"},
     objectclass => [ "posixAccount", "shadowAccount" ],
     defaults => {
       cn => "",
       sn => "",
       uid => "",
       uidNumber => "",
       gidNumber => "100",
       homeDirectory => "/home/",
       userPassword => ""
     },
     cmdl => {
       cn => "",
       sn => "",
       uid => "",
       uidNumber => "uid",
       gidNumber => "group",
       homeDirectory => "homeDir",
       userPassword => "password"
     }
   },
   group => {
     [..]
   }
};

This schema file defines an object class user (and group).

The default ou where new user objects will be added to the LDAP directory is defined as ou=People,dc=pingworks,dc=de.

name_attr defines all attributes which will automatically be set to the objects name on creation.

unique_id defines the attribute which is used for all functions which take the unique_id argument.

objectclass defines the value of the objectClass attribute on new user objects.

Eg: get(object_type=``user'', unique_id=``root'') will search for LDAP objects with an objectclass of 'posixAccount' and a 'uid' of 'root'.

The default section defines the default values set for new user objects.

The cmdl section defines which commandline switches are evaluated on creation.