ldapx(n)
๐ ldapx โ LDAP extended object interface ๐
okay bestie, sit down, hydrate, because we're about to get so comfy with ldapx(n) โ the manual page, the moment, the icon. ๐ซถ
โจ NAME โจ
ldapx โ LDAP extended object interface
no cap, that's literally the whole vibe of this page: an extended object interface for LDAP. periodt.
๐ SYNOPSIS ๐
here's the whole menu of what you can yell at it, fr fr. every line verbatim, don't @ me:
package require Tcl 8.5 9
package require ldapx ?1.3?
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
like, that's a lot of little commands and honestly? slay. ๐
๐ฆ DESCRIPTION ๐ฆ
so the ldapx package hands you an extended Tcl interface to LDAP directories and LDIF files. it's built on top of the ldap package, because that's where the low-level LDAP access lives โ ldapx is the glow-up on top. โจ
LDAP access here is compatible with RFC 2251 (http://www.rfc-editor.org/rfc/rfc2251.txt), and LDIF access is compatible with RFC 2849 (http://www.rfc-editor.org/rfc/rfc2849.txt). standards girlies rise. ๐
๐ OVERVIEW ๐
the ldapx package gives you objects to interact with LDAP directories and LDIF files through an easy-to-use programming interface. it implements three snit::type classes. three! a little trio. a squad, if you will. ๐
๐ธ the entry class ๐ธ
the first class, entry, is the one that stores individual entries. you get two different formats, and they are not the same thing, so listen up:
- the first format is the standard format, which represents an entry the way it was read from the directory.
- the second format is the change format, which stores the differences between two standard entries. it's giving "receipts of what changed". ๐งพ
so here's the workflow, bestie. an application that 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
- compute the differences between the two entries into a new (change) entry
- commit that change entry to the directory
this kind of modification is used so heavily 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
- computes the differences between the entry and its backup
and those differences are then committed to the directory. same energy, less bookkeeping. ๐
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. all the little tools you need, handed to you on a platter. ๐
๐ the ldap class ๐
the second class is ldap. it gives you a method to connect and bind to the directory, with uniform access to LDAP and LDAPS through a URL (ldap:// or ldaps://) โ one interface, two protocols, main character energy. ๐
traverseis the control structure that executes a body for each entry found in the directory.commitapplies some changes (represented as entry objects) to the directory.
and because some attributes are represented as UTF-8 strings, there's a -utf8 option that controls which attributes must be converted and which attributes must not be converted. picky? maybe. correct? absolutely. โจ
๐ง the ldif class ๐ง
the last class is ldif. it gives you a method to associate a standard Tcl channel to an LDIF object. then the methods read and write read or write entries from or to that 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. pick a lane, babe. ๐ซ
the -utf8 option works exactly as it does with the ldap class. consistency, we love to see it. ๐
more coming, bestie, don't go anywhere ๐
๐ ENTRY CLASS ๐
okay so this whole section is about the entry class, aka the actual little object where one (1) LDAP entry lives. it's the main character of this page, no cap.
๐ฆ ENTRY INSTANCE DATA ๐ฆ
An instance of the entry class keeps the following data:
dnโ this is the DN of the entry, which includes (in LDAP terminology) the RDN (relative DN) and the Superior parts.formatโ the format may be uninitialized (entry not yet used), standard or change. Most methods check the format of the entry, which can be reset with the reset method.attrvalsโ in a standard entry, this is where the attributes and associated values are stored. Many methods provide access to these informations. Attribute names are always converted into lower case.backupโ in a standard entry, the backup may contain a copy of the dn and all attributes and values. Methods backup and restore manipulate these data, and method diff may use this backup.changeโ in a change entry, these data represent the modifications. Such modifications are handled by specialized methods such as apply or commit. Detailed format should not be used directly by programs.
Internally, modifications are represented as a list of elements, each element has one of the following formats (which match the corresponding LDAP operations):
[1]โ addition of a new entry.
{add {attr1 {val1...valn} attr2 {...} ...}}
[2]โ modification of one or more attributes and/or values, where<modop>can be modadd, moddel or modrepl (see the LDAP modify operation).
{mod {modop {attr1 ?val1...valn?} attr2 ...} {modop ...} ...}
[3]โ deletion of an old entry.
{del}
[4]โ renaming of an entry.
{modrdn newrdn deleteoldrdn ?newsuperior?}
โจ ENTRY OPTIONS โจ
No option is defined by this class.
like... zero options. none. the class said "I'm enough on my own ๐ " and honestly? respect.
๐ธ METHODS FOR ALL KINDS OF ENTRIES ๐ธ
e resetโ This method resets the entry to an uninitialized state.e dn ?newdn?โ This method returns the current DN of the entry. If the optional newdn is specified, it replaces the current DN of the entry.e rdnโ This method returns the RDN part of the DN of the entry.e superiorโ This method returns the superior part of the DN of the entry.e printโ This method returns the entry as a string ready to be printed.
๐ METHODS FOR STANDARD ENTRIES ONLY ๐
In all methods, attribute names are converted in lower case.
okay noted, everything is lowercase, we love a consistent girlie ๐ซถ
se isemptyโ This method returns1if the entry is empty (i.e. without any attribute).se get attrโ This method returns all values of the attribute attr, or the empty list if the attribute is not fond.
(yes bestie, the page literally says "not fond" ๐ญ we're leaving it, it's iconic, it means not found, iykyk)
se get1 attrโ This method returns the first value of the attribute.se set attr valuesโ This method sets the values (list values) of the attribute attr. If the list is empty, this method deletes all
...and the sentence just ends there on the page ๐ "deletes all" what? the mystery continues. we report, we don't invent.
se set1 attr valueโ This method sets the values of the attribute attr to be an unique value value. Previous values, if any, are replaced by the new value.se add attr valuesโ This method adds all elements the list values to the values of the attribute attr.se add1 attr valueโ This method adds a single value given by the parameter value to the attribute attr.se del attr ?values?โ If the optional list values is specified, this method deletes all specified values from the attribute attr. If the argument values is not specified, this method deletes all values.se del1 attr valueโ This method deletes a unique value from the attribute attr.se getattrโ This method returns all attributes names.se getallโ This method returns all attributes and values from the entry, packed in a list of pairs<attribute, list of values>.se setall avpairsโ This method sets at once all attributes and values. The format of the avpairs argument is the same as the one returned by method getall.se backup ?other?โ This method stores in an other standard entry object a copy of the current DN and attributes/values. If the optional other argument is not specified, copy is done in the current entry (in a specific place, see section OVERVIEW).se swapโ This method swaps the current and backup contexts of the entry.se restore ?other?โ If the optional argument other is given, which must then be a standard entry, this method restores the current entry into the other entry. If the argument other argument is not specified, this methods restores the current entry from its internal backup (see section OVERVIEW).se apply centryโ This method applies changes defined in the centry argument, which must be a change entry.
๐ฅ METHODS FOR CHANGE ENTRIES ONLY ๐ฅ
ce change ?new?โ If the optional argument new is specified, this method modifies the change list (see subsection Entry Instance Data for the exact format). In both cases, current change list is returned.
Warning: values returned by this method should only be used by specialized methods such as apply or commit. fr fr, do not go poking at that list yourself, that's not your lane bestie ๐
ce diff new ?old?โ This method computes the differences between the new and old entries under the form of a change list, and stores this list into the current change entry. If the optional argument old is not specified, difference is computed from the entry and its internal backup (see section OVERVIEW). Return value is the computed change list.
๐ ENTRY EXAMPLE ๐
the moment you've all been waiting for, a full little demo, she ate and left no crumbs โจ
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
clean-up crew at the end, always destroy your entries, leave no trace, that's the vibe ๐
๐ LDAP CLASS ๐
Okay bestie, welcome to the ldap class, the main character of this whole package. Let's get into it.
๐ฆ LDAP INSTANCE DATA ๐ฆ
An instance of the ldap class keeps these two little data besties:
channelโ this is the channel that theldappackage uses to talk to the LDAP server. Like the group chat between you and the server. ๐ฌlastErrorโ this variable holds the error message from the most recent method of theldapclass. Basically every method updates this string, like almost all of them. You can grab it with theerrormethod. ๐จ
โจ LDAP OPTIONS โจ
You set options on ldap instances with the configure method. Periodt.
The first option is all about TLS parameters:
-tlsoptions listโ tell it the set of TLS options to use when connecting to the LDAP server (peep theconnectmethod). For the full list of valid options, go check the LDAP package docs. The default is:
-request 1 -require 1 -ssl2 no -ssl3 no -tls1 yes -tls1.1 yes -tls1.2 yes
Example:
$l configure -tlsoptions {-request yes -require yes}
Then there's a whole set of options that kick in during search operations (so traverse, search and read, see below).
-scope base|one|subโ sets the scope of the LDAP search tobase,oneorsub, meaning a base object, one-level or subtree search. Default issub. ๐ณ-derefaliases never|seach|find|alwaysโ controls how aliases dereferencing is handled.nevermeans aliases are never dereferenced,alwaysmeans they're always dereferenced,searchmeans they're dereferenced when searching, andfindmeans they're dereferenced only when locating the base object for the search. Default isnever. (And yes, the manpage saysseachnotsearchโ we keep it exactly like that, bestie. ๐)-sizelimit integerโ the max number of entries to be retrieved during a search. A value of0means no limit. Default is0. ๐-timelimit integerโ the time limit for a search to complete. A value of0means no limit. Default is0. โณ-attrsonly 0|1โ if set to1, only attribute names are retrieved. Normally (0), attribute values are also retrieved. Default is0. ๐ท๏ธ
The last option is used when getting entries or committing changes in the directory:
-utf8 pattern-yes pattern-noโ tells it which attribute values are encoded in UTF-8. This is specific to the LDAP schema your app is using, because some attributes likejpegPhoto, for example, are not UTF-8. This option takes a list with two regular expressions suitable for theregexpcommand (anchored by^and$). The first regexp says which attribute names are to be UTF-8 encoded, and the second selects, among those, the attribute names that will not be UTF-8 encoded. So you can say: convert all attributes, exceptjpegPhoto. Default is{{.*} {}}, meaning all attributes are converted, no exceptions. ๐
๐ฅ LDAP METHODS ๐ฅ
Time for the methods, bestie. These are the moves you'll be making.
la error ?newmsg?โ returns the error message that happened in the last call to anldapclass method. If you pass the optionalnewmsgargument, it becomes the new last error message. ๐la connect url ?binddn? ?bindpw? ?starttls?โ connects to the LDAP server using the givenurl(which can look likeldap://host:portorldaps://host:port). If you give the optionalbinddnargument together withbindpw, the connect binds to the LDAP server using that DN and password. Ifstarttlsis given a true value (1,yes, etc.) and the URL uses theldap://scheme, a TLS negotiation is initiated with the newly created connection, before LDAP binding. Default value:no. Returns1if the connection was successful, or0if an error occurred (use theerrormethod to get the message). ๐la disconnectโ disconnects (and unbinds, if necessary) from the LDAP server. Bye bestie! ๐la traverse base filter attrs entry bodyโ this is a new control structure, okay? It searches the LDAP directory from the specifiedbaseDN (given by thebaseargument) and selects entries based on thefilterargument. For each entry it finds, it fetches the attributes specified byattrs(or all attributes if it's an empty list), stores them in theentryinstance of classentry, and runs the script defined by thebodyargument. Options refine the search. ๐จ Caution: when you use this method, the scriptbodycannot perform another LDAP search (so notraverse,searchorreadinside it). ๐จla search base filter attrsโ searches the directory the same way astraverse. All found entries are stored in newly created instances of classentry, which are returned in a list. You should destroy those new instances when you're done with them. ๐๏ธla read base filter entry ... entryโ reads one or more entries using the same search criteria astraverseandsearch. All attributes are stored in the entries. This is a quick way to read some entries. It returns the number of entries found in the directory (which may be more than the number of read entries). If you call it without anyentryargument, it just returns the number of entries found, without returning any data. ๐la commit entry ... entryโ commits the changes stored in theentryarguments. Each entry may be either a change entry, or a standard entry with a backup. ๐ Note: in the future, this method should use the LDAP transaction extension provided by OpenLDAP 2.3 and later. ๐ฎ
๐ธ LDAP EXAMPLE ๐ธ
Here's the whole vibe in action, bestie:
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://server.mycomp.com"
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 CLASS โจ
okay so the ldif class is literally file-format royalty ๐. This is where you read and write LDIF files like the icon you are, and no cap it's giving main character energy. Let's get into the data, the options, and the methods, periodt.
๐งธ LDIF INSTANCE DATA ๐งธ
An instance of the ldif class keeps the following data, and it's the whole vibe:
channelโ This is the Tcl channel used to retrieve or store LDIF file contents. The association between an instance and a channel is made by the methodchannel. There is no need to disrupt this association when the LDIF file operation has ended. so clingy, but in a cute way ๐formatโ LDIF files may contain standard entries or change entries, but not both. This variable contains the detected format of the file (when reading) or the format of entries written to the file (when writing). pick a lane, bestie โจlastErrorโ This variable contains the error message which appeared in the last method of theldifclass (this string is modified in nearly all methods). Theerrormethod may be used to fetch this message. the receipts, fr fr ๐ญversionโ This is the version of the LDIF file. Only version 1 is supported: the methodreadcan only read from version 1 files, and methodwriteonly creates version 1 files. version 1 or nothing, we don't do beta ๐
โ๏ธ LDIF OPTIONS โ๏ธ
This class defines two options, and they're both kinda iconic:
-ignorelist-of-attributes โ This option is used to ignore certain attribute names on reading. For example, to read OpenLDAP replica files (replog), one must ignorereplicaandtimeattributes since they do not conform to the RFC 2849 standard for LDIF files. Default is empty list: no attribute is ignored. so unbothered, so free ๐ธ-utf8pattern-yes pattern-no โ Specify which attribute values are encoded in UTF-8. This information is specific to the LDAP schema in use by the application, since some attributes such asjpegPhoto, for example, are not encoded in UTF-8. This option takes the form of a list with two regular expressions suitable for theregexpcommand (anchored by^and$). The first specifies which attribute names are to be UTF-8 encoded, and the second selects, among those, the attribute names which will not be UTF-8 encoded. It is thus possible to say: convert all attributes, exceptjpegPhoto. Default is{{.*} {}}, meaning: all attributes are converted, without exception. encoding drama, but make it fashion ๐
๐ ๏ธ LDIF METHODS ๐ ๏ธ
li channelchan โ This method associates the Tcl channel named chan with the LDIF instance. It resets the type of LDIF object to uninitialized. fresh start, who dis? โจli error?newmsg? โ This method returns the error message that occurred in the last call to aldifclass method. If the optional argument newmsg is supplied, it becomes the last error message. spill the tea โli readentry โ This method reads the next entry from the LDIF file and stores it in the entry object of classentry. The entry may be a standard or change entry. one entry at a time, we love a slow burn ๐li writeentry โ This method writes the entry given in the argument entry to the LDIF file. put it on the page, periodt ๐
๐ LDIF EXAMPLE ๐
Here's the example, bestie, and yes it's a whole serve. It reads a LDIF file, compares entries to an LDAP directory, and writes changes to standard output so the LDAP directory matches exactly the LDIF file. iconic behavior, no notes ๐
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://server.mycomp.com"]} 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
๐ REFERENCES ๐
๐ 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 [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation. we love a collaborative queen ๐
When proposing code changes, please provide unified diffs, i.e the output of diff -u. no messy pasted patches, bestie ๐ญ
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. upload that file, it's giving organized โจ
๐ KEYWORDS ๐
directory access, internet, ldap, ldap client, ldif, protocol, rfc 2251, rfc 2849
๐ท๏ธ CATEGORY ๐ท๏ธ
Networking
ยฉ๏ธ COPYRIGHT ยฉ๏ธ
Copyright (c) 2006-2018 Pierre David <pdav@users.sourceforge.net>
and that's the whole ldapx(n) page, bestie ๐ this came from tcllib, version 1.3, and it's the ldapx(n) manual โ the footer didn't give a date, so we're calling it timeless, periodt ๐โจ
๐ฆ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 ยท TCL
- fedora 44: tcllib 2.0-4.fc44 ยท TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2006-2018 Pierre David <pdav@users\&.sourceforge\&.net>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms