ldapx(n)

๐Ÿ“ฆ
version 2.0 ยท 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

๐Ÿ’– 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:

  1. read a (standard) entry from the directory
  2. create a fresh copy into a new (standard) entry
  3. modify the new copy
  4. compute the differences between the two entries into a new (change) entry
  5. 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:

  1. reads a (standard) entry from the directory
  2. backs up the original data
  3. modifies the entry
  4. 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. ๐ŸŒˆ

  • traverse is the control structure that executes a body for each entry found in the directory.
  • commit applies 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 returns 1 if 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 the ldap package 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 the ldap class. Basically every method updates this string, like almost all of them. You can grab it with the error method. ๐Ÿšจ

โœจ 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 the connect method). 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 to base, one or sub, meaning a base object, one-level or subtree search. Default is sub. ๐ŸŒณ
  • -derefaliases never|seach|find|always โ€” controls how aliases dereferencing is handled. never means aliases are never dereferenced, always means they're always dereferenced, search means they're dereferenced when searching, and find means they're dereferenced only when locating the base object for the search. Default is never. (And yes, the manpage says seach not search โ€” we keep it exactly like that, bestie. ๐Ÿ’€)
  • -sizelimit integer โ€” the max number of entries to be retrieved during a search. A value of 0 means no limit. Default is 0. ๐Ÿ“
  • -timelimit integer โ€” the time limit for a search to complete. A value of 0 means no limit. Default is 0. โณ
  • -attrsonly 0|1 โ€” if set to 1, only attribute names are retrieved. Normally (0), attribute values are also retrieved. Default is 0. ๐Ÿท๏ธ

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 like jpegPhoto, for example, are not UTF-8. This option takes a list with two regular expressions suitable for the regexp command (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, except jpegPhoto. 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 an ldap class method. If you pass the optional newmsg argument, it becomes the new last error message. ๐Ÿ“
  • la connect url ?binddn? ?bindpw? ?starttls? โ€” connects to the LDAP server using the given url (which can look like ldap://host:port or ldaps://host:port). If you give the optional binddn argument together with bindpw, the connect binds to the LDAP server using that DN and password. If starttls is given a true value (1, yes, etc.) and the URL uses the ldap:// scheme, a TLS negotiation is initiated with the newly created connection, before LDAP binding. Default value: no. Returns 1 if the connection was successful, or 0 if an error occurred (use the error method 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 specified base DN (given by the base argument) and selects entries based on the filter argument. For each entry it finds, it fetches the attributes specified by attrs (or all attributes if it's an empty list), stores them in the entry instance of class entry, and runs the script defined by the body argument. Options refine the search. ๐Ÿšจ Caution: when you use this method, the script body cannot perform another LDAP search (so no traverse, search or read inside it). ๐Ÿšจ
  • la search base filter attrs โ€” searches the directory the same way as traverse. All found entries are stored in newly created instances of class entry, 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 as traverse and search. 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 any entry argument, it just returns the number of entries found, without returning any data. ๐Ÿ“–
  • la commit entry ... entry โ€” commits the changes stored in the entry arguments. 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 method channel. 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 the ldif class (this string is modified in nearly all methods). The error method 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 method read can only read from version 1 files, and method write only 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:

  • -ignore list-of-attributes โ€” This option is used to ignore certain attribute names on reading. For example, to read OpenLDAP replica files (replog), one must ignore replica and time attributes 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 ๐ŸŒธ
  • -utf8 pattern-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 as jpegPhoto, for example, are not encoded in UTF-8. This option takes the form of a list with two regular expressions suitable for the regexp command (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, except jpegPhoto. Default is {{.*} {}}, meaning: all attributes are converted, without exception. encoding drama, but make it fashion ๐ŸŒˆ

๐Ÿ› ๏ธ LDIF METHODS ๐Ÿ› ๏ธ

  • li channel chan โ€” 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 a ldif class method. If the optional argument newmsg is supplied, it becomes the last error message. spill the tea โ˜•
  • li read entry โ€” This method reads the next entry from the LDIF file and stores it in the entry object of class entry. The entry may be a standard or change entry. one entry at a time, we love a slow burn ๐Ÿ’–
  • li write entry โ€” 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 (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