Tcl Library Source Code


[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]


ldapx - LDAP extended object interface

Table Of Contents


package require Tcl 8.5 9
package require ldapx ?1.2?

e reset
e dn ?newdn?
e rdn
e superior
e print
se isempty
se get attr
se get1 attr
se set attr values
se set1 attr value
se add attr values
se add1 attr value
se del attr ?values?
se del1 attr value
se getattr
se getall
se setall avpairs
se backup ?other?
se swap
se restore ?other?
se apply centry
ce change ?new?
ce diff new ?old?
la error ?newmsg?
la connect url ?binddn? ?bindpw? ?starttls?
la disconnect
la traverse base filter attrs entry body
la search base filter attrs
la read base filter entry ... entry
la commit entry ... entry
li channel chan
li error ?newmsg?
li read entry
li write entry


The ldapx package provides an extended Tcl interface to LDAP directores and LDIF files. The ldapx package is built upon the ldap package in order to get low level LDAP access.

LDAP access is compatible with RFC 2251 ( LDIF access is compatible with RFC 2849 (


The ldapx package provides objects to interact with LDAP directories and LDIF files with an easy to use programming interface. It implements three snit::type classes.

The first class, entry, is used to store individual entries. Two different formats are available: the first one is the standard format, which represents an entry as read from the directory. The second format is the change format, which stores differences between two standard entries.

With these entries, an application which wants to modify an entry in a directory needs to read a (standard) entry from the directory, create a fresh copy into a new (standard) entry, modify the new copy, and then compute the differences between the two entries into a new (change) entry, which may be commited to the directory.

Such kinds of modifications are so heavily used that standard entries may contain their own copy of the original data. With such a copy, the application described above reads a (standard) entry from the directory, backs-up the original data, modifies the entry, and computes the differences between the entry and its backup. These differences are then commited to the directory.

Methods are provided to compute differences between two entries, to apply differences to an entry in order to get a new entry, and to get or set attributes in standard entries.

The second class is the ldap class. It provides a method to connect and bind to the directory with a uniform access to LDAP and LDAPS through an URL (ldap:// or ldaps://). The traverse control structure executes a body for each entry found in the directory. The commit method applies some changes (represented as entry objects) to the directory. Since some attributes are represented as UTF-8 strings, the option -utf8 controls which attributes must be converted and which attributes must not be converted.

The last class is the ldif class. It provides a method to associate a standard Tcl channel to an LDIF object. Then, methods read and write read or write entries from or to this channel. This class can make use of standard or change entries, according to the type of the LDIF file which may contain either standard entries or change entries (but not both at the same time). The option -utf8 works exactly as with the ldap class.


Entry Instance Data

An instance of the entry class keeps the following data:

Entry Options

No option is defined by this class.

Methods for all kinds of entries

Methods for standard entries only

In all methods, attribute names are converted in lower case.

Methods for change entries only

Entry Example

package require ldapx

# Create an entry and fill it as a standard entry with
# attributes and values
::ldapx::entry create e
e dn "uid=joe,ou=people,o=mycomp"
e set1 "uid"             "joe"
e set  "objectClass"     {person anotherObjectClass}
e set1 "givenName"       "Joe"
e set1 "sn"              "User"
e set  "telephoneNumber" {+31415926535 +2182818}
e set1 "anotherAttr"     "This is a beautiful day, isn't it?"

puts stdout "e\n[e print]"

# Create a second entry as a backup of the first, and
# make some changes on it.
# Entry is named automatically by snit.

set b [::ldapx::entry create %AUTO%]
e backup $b

puts stdout "$b\n[$b print]"

$b del  "anotherAttr"
$b del1 "objectClass" "anotherObjectClass"

# Create a change entry, a compute differences between first
# and second entry.

::ldapx::entry create c
c diff e $b

puts stdout "$c\n[$c print]"

# Apply changes to first entry. It should be the same as the
# second entry, now.

e apply c

::ldapx::entry create nc
nc diff e $b

puts stdout "nc\n[nc print]"

# Clean-up

e destroy
$b destroy
c destroy
nc destroy


Ldap Instance Data

An instance of the ldap class keeps the following data:

Ldap Options

Options are configured on ldap instances using the configure method.

The first option is used for TLS parameters:

A set of options of the ldap class is used during search operations (methods traverse, search and read, see below).

The last option is used when getting entries or committing changes in the directory:

Ldap Methods

Ldap Example

    package require ldapx

    # Connects to the LDAP directory using StartTLS

    ::ldapx::ldap create l
    l configure -tlsoptions {-cadir /etc/ssl/certs -request yes -require yes}
    set url "ldap://"
    if {! [l connect $url "cn=admin,o=mycomp" "mypasswd" yes]} then {
	puts stderr "error: [l error]"
	exit 1

    # Search all entries matching some criterion

    l configure -scope one
    ::ldapx::entry create e
    set n 0
    l traverse "ou=people,o=mycomp" "(sn=Joe*)" {sn givenName} e {
	puts "dn: [e dn]"
	puts "  sn:        [e get1 sn]"
	puts "  givenName: [e get1 givenName]"
	incr n
    puts "$n entries found"
    e destroy

    # Add a telephone number to some entries
    # Note this modification cannot be done in the "traverse" operation.

    set lent [l search "ou=people,o=mycomp" "(sn=Joe*)" {}]
    ::ldapx::entry create c
    foreach e $lent {
	$e backup
	$e add1 "telephoneNumber" "+31415926535"
	c diff $e
	if {! [l commit c]} then {
	    puts stderr "error: [l error]"
	    exit 1
	$e destroy
    c destroy

    l disconnect
    l destroy


Ldif Instance Data

An instance of the ldif class keeps the following data:

Ldif Options

This class defines two options:

Ldif Methods

Ldif Example

    package require ldapx

    # This examples reads a LDIF file containing entries,
    # compare them to a LDAP directory, and writes on standard
    # output an LDIF file containing changes to apply to the
    # LDAP directory to match exactly the LDIF file.

    ::ldapx::ldif create liin
    liin channel stdin

    ::ldapx::ldif create liout
    liout channel stdout

    ::ldapx::ldap create la
    if {! [la connect "ldap://"]} then {
	puts stderr "error: [la error]"
	exit 1
    la configure -scope one

    # Reads LDIF file

    ::ldapx::entry create e1
    ::ldapx::entry create e2
    ::ldapx::entry create c

    while {[liin read e1] != 0} {
	set base [e1 superior]
	set id [e1 rdn]
	if {[la read $base "($id)" e2] == 0} then {
	    e2 reset

	c diff e1 e2
	if {[llength [c change]] != 0} then {
	    liout write c

    la disconnect
    la destroy
    e1 destroy
    e2 destroy
    c destroy
    liout destroy
    liin destroy


Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category ldap of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.


directory access, internet, ldap, ldap client, ldif, protocol, rfc 2251, rfc 2849




Copyright © 2006-2018 Pierre David