ldap(3tcl)
π ldap π β LDAP client, and yes bestie we're doing the whole thing
okay so real talk: this is the ldap(3tcl) manual page, aka the LDAP client page, and it's long, so we're doing it in three parts. Settle in, grab a snack, we're gonna slay this. π«Ά
β¨ SYNOPSIS β¨
Like, here's the whole vibe upfront:
package require Tcl 8.5 9
package require ldap ?1.10.2?
::ldap::connect host ?port?
::ldap::tlsoptions reset
::ldap::tlsoptions ?opt1 val1? ?opt2 val2? ...
::ldap::secure_connect host ?port?
::ldap::secure_connect host ?port? ?verify_cert? ?sni_servername?
::ldap::disconnect handle
::ldap::starttls handle
::ldap::starttls handle ?cafile? ?certfile? ?keyfile? ?verify_cert?
?sni_servername?
::ldap::bind handle ?name? ?password?
::ldap::bindSASL handle ?name? ?password?
::ldap::unbind handle
::ldap::search handle baseObject filterString attributes options
::ldap::searchInit handle baseObject filterString attributes options
::ldap::searchNext handle
::ldap::searchEnd handle
::ldap::modify handle dn attrValToReplace ?attrToDelete? ?attrValToAdd?
::ldap::modifyMulti handle dn attrValToReplace ?attrValToDelete?
?attrValToAdd?
::ldap::add handle dn attrValueTuples
::ldap::addMulti handle dn attrValueTuples
::ldap::delete handle dn
::ldap::modifyDN handle dn newrdn ?deleteOld? ?newSuperior?
::ldap::info ip handle
::ldap::info bound handle
::ldap::info bounduser handle
::ldap::info connections
::ldap::info tls handle
::ldap::info tlsstatus handle
::ldap::info saslmechanisms handle
::ldap::info control handle
::ldap::info extensions extensions
::ldap::info whoami handle
πΈ DESCRIPTION πΈ
So the ldap package hands you a Tcl-only client library for the LDAPv3 protocol, exactly as specified in RFC 4511 (http://www.rfc-editor.org/rfc/rfc4511.txt). The way it works is honestly so straightforward it's giving main character energy: it opens the standard (or secure) LDAP socket on the server, and then gives you a Tcl API to reach the LDAP protocol commands. π
And here's the part you must remember, no cap: all server errors come back as Tcl errors (thrown), which means you have to catch them with the Tcl catch command. Every single one. Don't be the person who forgets. π
π TLS SECURITY CONSIDERATIONS π
This package uses the TLS package to handle the security for LDAPS connections. Iconic of it.
Now, policy decisions β like which set of protocols to support and which ciphers to use β are not the responsibility of TLS, and honestly not of this package either. Those decisions belong to whichever application is using the package, and they're probably gonna be influenced by the set of servers that application is gonna talk to as well. It's giving "not our job, bestie."
For example: in light of the recent POODLE attack [http://googleonlinesecurity.blogspot.co.uk/2014/10/this-poodle-bites-exploiting-ssl-30.html] that Google discovered, many servers will disable support for the SSLv3 protocol. To handle this change, the applications using TLS must be patched β not this package, and not TLS itself. And like, that patch may be as simple as generally activating tls1 support, as shown in the example below. π¦
ldap::tlsoptions -tls1 1 -ssl2 0 -ssl3 0 ;# forcibly activate support for the TLS1 protocol
... your own application code ...
π COMMANDS π
π ::ldap::connect host ?port?
Opens a LDAPv3 connection to the specified host, at the given port, and returns a token for the connection. This token is the handle argument for all other commands. If no port is specified it will default to 389.
The command blocks until the connection has been established, or establishment definitely failed. No half-vibes here.
π ::ldap::tlsoptions reset
This command resets TLS options to default values. It returns the set of options. Using this command is incompatible with the obsolete form of ::ldap::secure_connect and ::ldap_starttls. (yes, note that spelling, it's written exactly like that in the original, we don't fix it, we respect it π«‘)
π ::ldap::tlsoptions ?opt1 val1? ?opt2 val2? ...
This command adds one or more options to some value, and you may use it more than one time in order to add options in several steps. A complete description of options may be found in the tls package documentation. Valid options and values are:
-cadirdirectory β Provide the directory containing the CA certificates. No default.-cafilefile β Provide the CA file. No default.-cipherstring β Provide the cipher suites to use. No default.-dhparamsfile β Provide a Diffie-Hellman parameters file. No default.-requestboolean β Request a certificate from peer during SSL handshake. Default:true.-requireboolean β Require a valid certificate from peer during SSL handshake. If this is set totruethen-requestmust also be set totrue. Default:false-servernamehost β Only available if the OpenSSL library the TLS package is linked against supports the TLS hostname extension for 'Server Name Indication' (SNI). Use to name the logical host we are talking to and expecting a certificate for. No default.-ssl2bool β Enable use of SSL v2. Default:false-ssl3bool β Enable use of SSL v3. Default:false-tls1bool β Enable use of TLS v1 Default:true-tls1.1bool β Enable use of TLS v1.1 Default:true-tls1.2bool β Enable use of TLS v1.2 Default:true
This command returns the current set of TLS options and values. In particular, one may use this command without any arguments to get the current set of options. Obsessed with a getter that's also a setter, tbh. β¨
Using this command is incompatible with the obsolete form of ::ldap::secure_connect and ::ldap_starttls (see below).
π ::ldap::secure_connect host ?port?
Like ::ldap::connect, except that the created connection is secured by SSL. The port defaults to 636. This command depends on the availability of the package TLS, which is a SSL binding for Tcl. If TLS is not available, then this command will fail. π
TLS options are specified with ::ldap::tlsoptions.
The command blocks until the connection has been established, or establishment definitely failed.
π ::ldap::secure_connect host ?port? ?verify_cert? ?sni_servername?
Note: this form of the command is deprecated, since TLS options had to be specified with a combination of parameters to this command (verify_certandsni_servername) and arguments to::tls::init(from packagetls) for example to setup defaults for trusted certificates. Prefer the above form (without theverify_certandsni_servernameparameters) and set TLS options with::ldap::tlsoptions.
If verify_cert is set to 1, the default, this checks the server certificate against the known hosts. If sni_servername is set, the given hostname is used as the hostname for Server Name Indication in the TLS handshake.
Use ::tls::init to setup defaults for trusted certificates.
TLS supports different protocol levels. In common use are the versions 1.0, 1.1 and 1.2. By default all those versions are offered. If you need to modify the acceptable protocols, you can change the ::ldap::tlsProtocols list (deprecated). π«
π ::ldap::disconnect handle
Closes the ldap connection refered to by the token handle. Returns the empty string as its result. Cute, clean, no notes. π
π ::ldap::starttls handle
Start TLS negotiation on the connection denoted by handle, with TLS parameters set with ::ldap::tlsoptions.
π ::ldap::starttls handle ?cafile? ?certfile? ?keyfile? ?verify_cert? ?sni_servername?
Note: this form of the command is deprecated, since TLS options had to be specified with a combination of parameters to this command (cafile,certfile,keyfile,verify_certandsni_servername) and arguments to::tls::init(from packagetls). Prefer the above form (without specific TLS arguments) and set TLS options with::ldap::tlsoptions.
Start TLS negotiation on the connection denoted by handle. You need to set at least the cafile argument to a file with trusted certificates, if verify_cert is 1, which is the default. The sni_servername can be used to signal a different hostname during the TLS handshake. The announced protocols are determined in the same way as ::ldap::secure_connect. You can specify a TLS client certificate with the certfile and keyfile options. π₯
π ::ldap::bind handle ?name? ?password?
This command authenticates the ldap connection refered to by the token in handle, with a user name and associated password. It blocks until a response from the ldap server arrives. Its result is the empty string. Both name and passwd default to the empty string if they are not specified. By leaving out name and passwd you can make an anonymous bind to the ldap server. You can issue ::ldap::bind again to bind with different credentials β glow up whenever you want. π
π ::ldap::bindSASL handle ?name? ?password?
This command uses SASL authentication mechanisms to do a multistage bind. It's otherwise identical to the standard ::ldap::bind. This feature is currently experimental and subject to change. See the documentation for the SASL and the "SASL.txt" in the tcllib CVS repository for details how to setup and use SASL with openldap.
π ::ldap::unbind handle
This command asks the ldap server to release the last bind done for the connection refered to by the token in handle. The handle is invalid after the unbind, as the server closes the connection. So this is effectivly just a more polite disconnect operation. Ghosting but, like, respectfully. π
π ::ldap::search handle baseObject filterString attributes options
This command performs a LDAP search below the baseObject tree using a complex LDAP search expression filterString and returns the specified attributes of all matching objects (DNs). If the list of attributes was empty all attributes are returned. The command blocks until it has received all results. The valid options are identical to the options listed for ::ldap::searchInit.
An example of a search expression is
set filterString "|(cn=Linus*)(sn=Torvalds*)"
The return value of the command is a list of nested dictionaries. The first level keys are object identifiers (DNs), second levels keys are attribute names. In other words, it is in the form
{dn1 {attr1 {val11 val12 ...} attr2 {val21...} ...}} {dn2 {a1 {v11 ...} ...}} ...
::ldap::searchInit handle baseObject filterString attributes optionsβ okay bestie this is the one that kicks off a whole LDAP search</p> π
It initiates a LDAP search below the baseObject tree, using a complex LDAP search expression filterString. The search grabs the specified attributes of all the matching objects (their DNs). Important vibe check: this command only starts the search β to actually get the results you gotta use ::ldap::searchNext. And a search can be terminated at any time with ::ldap::searchEnd, which tells the server no further results should be sent by sending an ABANDON message, and cleans up the internal state of the search. π§Ήβ¨
β οΈ Only one::ldap::searchcan be active at a given time. And yes, that includes the introspection commands::ldap::info saslmechanisms,ldap::info controlandldap::info extensions, because those invoke a search internally. iykyk π
Error responses coming back from the server β wrong arguments, that kind of thing β are returned with the first ::ldap::searchNext call, and that's where you should check for them and deal with them. No cap.
If the list of requested attributes is empty, all attributes will be returned. π
The parameter options specifies the options to be used in the search, and has the following format:
{-option1 value1 -option2 value2 ... }
Following options are available:
-scope base one subβ controls the scope of the search to be one ofbase,one, orsub, to specify a base object, one-level, or subtree search. The default issub.-derefaliases never search find alwaysβ controls how aliases dereferencing is done. Should be one ofnever,always,search, orfind, to specify that aliases are never dereferenced, always dereferenced, dereferenced when searching, or dereferenced only when locating the base object for the search. The default is to never dereference aliases. π«-sizelimit numβ determines the maximum number of entries to return in a search. If specified as0no limit is enforced. Heads up though: the server may enforce a configuration dependent sizelimit, which may be lower than the one you gave with this option. π The default is0, no limit.-timelimit secondsβ asks the server to use a timelimit ofsecondsfor the search. Zero means no limit. The default is0, no limit. β³-attrsonly booleanβ if set to1, only the attribute names but not the values will be present in the search result. The default is to retrieve attribute names and values.-referencevar varnameβ if set, the search result reference LDAPURIs, if any, are returned in the given variable. The caller can then decide to follow those references and query other LDAP servers for further results. π¦
::ldap::searchNext handleβ the results-fetching bestie π«Ά
This command returns the next entry from a LDAP search initiated by ::ldap::searchInit. It returns only after a new result is received, or when no further results are available β but it takes care to keep the event loop alive. π
The returned entry is a list with two elements: the first is the DN of the entry, the second is the list of attributes and values, in this format:
dn {attr1 {val11 val12 ...} attr2 {val21...} ...}
The ::ldap::searchNext command returns an empty list at the end of the search. π
::ldap::searchEnd handleβ cleanup era π§Ό
This command terminates a LDAP search initiated by ::ldap::searchInit. It also cleans up the internal state so a new search can be initiated. If the client has not yet received all results, the client sends an ABANDON message to inform the server that no further results for the previous search should be sent.
::ldap::modify handle dn attrValToReplace ?attrToDelete? ?attrValToAdd?β the glow-up command π β¨
This command modifies the object dn on the ldap server we are connected to via handle. It replaces attributes with new values, deletes attributes, and adds new attributes with new values. All arguments are dictionaries mapping attribute names to values. The optional arguments default to the empty dictionary, which means that no attributes will be deleted nor added. π«§
- dictionary
attrValToReplace(in) β no attributes will be changed if this argument is empty. The dictionary contains the new attributes and their values. They replace all attributes known to the object. π - dictionary
attrToDelete(in) β no attributes will be deleted if this argument is empty. The dictionary values are restrictions on the deletion. An attribute listed here will be deleted if and only if its current value at the server matches the value specified in the dictionary, or if the value in the dictionary is the empty string. π - dictionary
attrValToAdd(in) β no attributes will be added if this argument is empty. The dictionary values are the values for the new attributes. π±
The command blocks until all modifications have completed. Its result is the empty string.
::ldap::modifyMulti handle dn attrValToReplace ?attrValToDelete? ?attrValToAdd?β same energy, list-flavored π
This command modifies the object dn on the ldap server we are connected to via handle. It replaces attributes with new values, deletes attributes, and adds new attributes with new values. All arguments are lists with the format:
attr1 {val11 val12 ...} attr2 {val21...} ...
where each value list may be empty for deleting all attributes. The optional arguments default to empty lists of attributes to delete and to add.
- list
attrValToReplace(in) β no attributes will be changed if this argument is empty. The dictionary contains the new attributes and their values. They replace all attributes known to the object. π - list
attrValToDelete(in) β no attributes will be deleted if this argument is empty. If no value is specified, the whole set of values for an attribute will be deleted. π₯ - list
attrValToAdd(in) β no attributes will be added if this argument is empty. π±
The command blocks until all modifications have completed. Its result is the empty string.
::ldap::add handle dn attrValueTuplesβ new object, who dis π
This command creates a new object using the specified dn. The attributes of the new object are set to the values in the list attrValueTuples. Multiple valuated attributes may be specified using multiple tuples. The command blocks until the operation has completed. Its result is the empty string.
::ldap::addMulti handle dn attrValueTuplesβ the preferred one, periodt β¨
This command is the preferred one to create a new object using the specified dn. The attributes of the new object are set to the values in the dictionary attrValueTuples (which is keyed by the attribute names). Each tuple is a list containing multiple values. The command blocks until the operation has completed. Its result is the empty string.
::ldap::delete handle dnβ yeet π
This command removes the object specified by dn, and all its attributes, from the server. The command blocks until the operation has completed. Its result is the empty string.
::ldap::modifyDN handle dn newrdn ?deleteOld? ?newSuperior?β moving day π
This command moves or copies the object specified by dn to a new location in the tree of objects. This location is specified by newrdn, a relative designation, or by newrdn and newSuperior, an absolute designation. The optional argument deleteOld defaults to true, i.e. a move operation. If deleteOld is not set, then the operation will create a copy of dn in the new location. The optional argument newSuperior defaults to an empty string, meaning that the object must not be relocated in another branch of the tree. If this argument is given, the argument deleteOld must be specified also. β οΈ The command blocks until the operation has completed. Its result is the empty string.
::ldap::info ip handleβ returns the IP address of the remote LDAP server thehandleis connected to. π::ldap::info bound handleβ returns1if a handle has successfully completed a::ldap::bind. If no bind was done or it failed, a0is returned. π―::ldap::info bounduser handleβ returns the username used in the bind operation if a handle has successfully completed a::ldap::bind. If no bound was done or it failed, an empty string is returned. πͺͺ::ldap::info connectionsβ returns all currently existing ldap connection handles. π::ldap::info tls handleβ returns1if the ldap connection handle used TLS/SSL for connection vialdap::secure_connector completedldap::starttls,0otherwise. π::ldap::info tlsstatus handleβ returns the current security status of an TLS secured channel. The result is a list of key-value pairs describing the connected peer (see the TLS package documentation for the returned values). If the connection is not secured with TLS, an empty list is returned. π‘οΈ::ldap::info saslmechanisms handleβ returns the supported SASL mechanisms advertised by the server. Only valid in a bound state (anonymous or other). π::ldap::info control handleβ returns the supported controls advertised by the server as a list of OIDs. Only valid in a bound state. This is currently experimental and subject to change. π§ͺ::ldap::info extensions extensionsβ returns the supported LDAP extensions as list of OIDs. Only valid in a bound state. This is currently experimental and subject to change. π§ͺ::ldap::info whoami handleβ returnsauthzIdfor the current connection. This implements the RFC 4532 protocol extension. πͺ
β¨ EXAMPLES β¨
okay so here's a little example, literally extracted from the test app that comes with this code. it's giving "learn by doing" energy, and we're obsessed. π
package require ldap
# Connect, bind, add a new object, modify it in various ways
set handle [ldap::connect localhost 9009]
set dn "cn=Manager, o=University of Michigan, c=US"
set pw secret
ldap::bind $handle $dn $pw
set dn "cn=Test User,ou=People,o=University of Michigan,c=US"
ldap::add $handle $dn {
objectClass OpenLDAPperson
cn {Test User}
mail test.user@google.com
uid testuid
sn User
telephoneNumber +31415926535
telephoneNumber +27182818285
}
set dn "cn=Another User,ou=People,o=University of Michigan,c=US"
ldap::addMulti $handle $dn {
objectClass {OpenLDAPperson}
cn {{Anotther User}}
mail {test.user@google.com}
uid {testuid}
sn {User}
telephoneNumber {+31415926535 +27182818285}
}
# Replace all attributes
ldap::modify $handle $dn [list drink icetea uid JOLO]
# Add some more
ldap::modify $handle $dn {} {} [list drink water drink orangeJuice pager "+1 313 555 7671"]
# Delete
ldap::modify $handle $dn {} [list drink water pager ""]
# Move
ldap::modifyDN $handle $dn "cn=Tester"
# Kill the test object, and shut the connection down.
set dn "cn=Tester,ou=People,o=University of Michigan,c=US"
ldap::delete $handle $dn
ldap::unbind $handle
ldap::disconnect $handle
and now for round two: a simple query, plus how to process the results. no cap, this one's kinda iconic. β¨
package require ldap
set handle [ldap::connect ldap.acme.com 389]
ldap::bind $handle
set results [ldap::search $handle "o=acme,dc=com" "(uid=jdoe)" {}]
foreach result $results {
foreach {object attributes} $result break
# The processing here is similar to what 'parray' does.
# I.e. finding the longest attribute name and then
# generating properly aligned output listing all attributes
# and their values.
set width 0
set sortedAttribs {}
foreach {type values} $attributes {
if {[string length $type] > $width} {
set width [string length $type]
}
lappend sortedAttribs [list $type $values]
}
puts "object='$object'"
foreach sortedAttrib $sortedAttribs {
foreach {type values} $sortedAttrib break
foreach value $values {
regsub -all "\[\x01-\x1f\]" $value ? value
puts [format " %-${width}s %s" $type $value]
}
}
puts ""
}
ldap::unbind $handle
ldap::disconnect $handle
π BUGS, IDEAS, FEEDBACK π
so like, this document and the package it describes? yeah, they will undoubtedly contain bugs and other problems. it's not a vibe. please report such in the category ldap of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. also, if you have ideas for enhancements for either the package and/or the documentation, send those too. bestie, we're listening. π«Ά
when you propose code changes, please provide unified diffs, i.e. the output of diff -u.
also note: attachments are strongly preferred over inlined patches. you can make attachments 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. periodt. π
π KEYWORDS π
directory accessinternetldapldap clientprotocolrfc 2251rfc 4511x.500
π CATEGORY π
Networking
Β©οΈ COPYRIGHT Β©οΈ
Copyright (c) 2004 Andreas Kupries <andreas_kupries@users.sourceforge.net>Copyright (c) 2004 Jochen Loewer <loewerj@web.de>Copyright (c) 2006 Michael Schlenker <mic42@users.sourceforge.net>
and that's the whole page, bestie. this one's from tcllib 1.10.2, page ldap(3tcl). stay slay. π β¨π¦
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2004 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- Copyright (c) 2004 Jochen Loewer <loewerj@web\&.de>
- Copyright (c) 2006 Michael Schlenker <mic42@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms