LDAP_OPEN(3)

✨ NAME ✨

okay so this is LDAP_OPEN(3), a Library Functions Manual page, and it's literally the glow-up guide for four-ish little functions β€” ldap_init, ldap_initialize, ldap_open β€” whose whole job is to initialize the LDAP library and open a connection to an LDAP server. bestie, this is the "hello world" of talking to a directory. πŸ’…


πŸ“š LIBRARY πŸ“š

OpenLDAP LDAP, aka libldap, and you link it with -lldap. no cap. 🫢


πŸŽ€ SYNOPSIS πŸŽ€

#include <ldap.h>

LDAP *ldap_open(host, port)
char *host;
int port;

LDAP *ldap_init(host, port)
char *host;
int port;

int ldap_initialize(ldp, uri)
LDAP **ldp;
char *uri;

int ldap_connect(ldp)
LDAP *ldp;

int ldap_set_urllist_proc(ld, proc, params)
LDAP *ld;
LDAP_URLLIST_PROC *proc;
void *params;

int (LDAP_URLLIST_PROC)(ld, urllist, url, params);
LDAP *ld;
LDAPURLDesc **urllist;
LDAPURLDesc **url;
void *params;

#include <openldap.h>

int ldap_init_fd(fd, proto, uri, ldp)
ber_socket_t fd;
int proto;
char *uri;
LDAP **ldp;

like, K&R style, we're respecting the elders. 🧚


πŸ’– DESCRIPTION πŸ’–

okay so the whole vibe: ldap_open() opens a connection to an LDAP server and allocates an LDAP structure, and that structure is the little main-character handle you use to identify the connection and to maintain per-connection information. iconic.

meanwhile:

  • ldap_init() β€” allocates an LDAP structure but does not open an initial connection. it's giving "I'm ready but I'm not texting first." πŸ’€
  • ldap_initialize() β€” also allocates an LDAP structure but does not open an initial connection.
  • ldap_init_fd() β€” allocates an LDAP structure using an existing connection on the provided socket. recycling era. ♻️
🚨 literally the one rule: one of these routines must be called before any operations are attempted. no handle, no operations. periodt.

🌸 ldap_open() β€” the original queen 🌸

ldap_open() takes host, the hostname on which the LDAP server is running, and port, the port number to which to connect. if you want the default IANA-assigned port of 389, you should specify LDAP_PORT for port. cute, right?

the host parameter may contain a blank-separated list of hosts to try to connect to, and each host may optionally be of the form host:port. if present, the :port overrides the port parameter to ldap_open(). so yes, she can be sneaky like that. πŸ‘‘

upon successfully making a connection to an LDAP server, ldap_open() returns a pointer to an opaque LDAP structure, which should be passed to subsequent calls to ldap_bind(), ldap_search(), etc. certain fields in the LDAP structure can be set to indicate size limit, time limit, and how aliases are handled during operations β€” and read and write access to those fields must occur by calling ldap_get_option(3) and ldap_set_option(3) respectively, whenever possible. don't be out here poking fields directly, bestie. πŸ™ˆ

πŸ¦‹ ldap_init() β€” same energy, zero commitment πŸ¦‹

ldap_init() acts just like ldap_open(), but does not open a connection to the LDAP server. the actual connection open will occur when the first operation is attempted. she's "I'll get ready when we actually have plans." πŸ’…

🌈 ldap_initialize() β€” the upgrade 🌈

ldap_initialize() acts like ldap_init(), but it returns an integer indicating either success or the failure reason, and it lets you specify details for the connection in the schema portion of the URI. huge.

the uri parameter may be a comma- or whitespace-separated list of URIs containing only the schema, the host, and the port fields. apart from ldap, the other (non-standard) recognized values of the schema field are:

  • ldaps β€” LDAP over TLS πŸ”
  • ldapi β€” LDAP over IPC πŸ”Œ
  • cldap β€” connectionless LDAP πŸ“‘
⚠️ if other fields are present, the behavior is undefined. like, we don't know her. we've never met her. πŸ’€

at this time, ldap_open() and ldap_init() are deprecated in favor of ldap_initialize(), essentially because the latter lets you specify a schema in the URI and it explicitly returns an error code. it's giving "your fave could never." 😭

🫢 ldap_connect() β€” the introvert's bestie 🫢

ldap_connect() causes a handle created by ldap_initialize() to connect to the server. this is useful in situations where a file descriptor is required before a request is performed. sometimes you just need the fd first, y'know? πŸ’œ

🧚 ldap_init_fd() β€” bring-your-own-connection 🧚

ldap_init_fd() allows an LDAP structure to be initialized using an already-opened connection. the proto parameter should be one of:

  • LDAP_PROTO_TCP β€” for a connection using TCP
  • LDAP_PROTO_UDP β€” for a connection using UDP
  • LDAP_PROTO_IPC β€” for a connection using IPC

the value LDAP_PROTO_EXT may also be specified if user-supplied sockbuf handlers are going to be used. note that support for UDP is not implemented unless libldap was built with LDAP_CONNECTIONLESS defined. so don't come for her about UDP unless you compiled her that way. πŸ“

the uri parameter may optionally be provided for informational purposes. purely vibes. ✨

⭐ ldap_set_urllist_proc() β€” the list-manipulator ⭐

ldap_set_urllist_proc() allows you to set a function proc of type LDAP_URLLIST_PROC that is called when a successful connection can be established. this function receives the list of URIs parsed from the uri string originally passed to ldap_initialize(), and the one that successfully connected.

the function may manipulate the URI list β€” the typical use consists in moving the successful URI to the head of the list, so that subsequent attempts to connect to one of the URIs using the same LDAP handle will try it first. she ate and left no crumbs. πŸ”₯

  • if ld is null, proc is set as a global parameter that is inherited by all handlers within the process that are created after the call to ldap_set_urllist_proc(). πŸ‘‘
  • by default, no LDAP_URLLIST_PROC is set. 😌
  • in a multithreaded environment, ldap_set_urllist_proc() must be called before any concurrent operation using the LDAP handle is started. don't be messy about it. πŸ’…
πŸ“’ NOTE: the first call into the LDAP library also initializes the global options for the library. as such, the first call should be single-threaded or otherwise protected to insure that only one call is active. it is recommended that ldap_get_option() or ldap_set_option() be used in the program's main thread before any additional threads are created. see ldap_get_option(3). this is the "one at a time, ladies" rule and she is not joking. 😭

πŸ’€ ERRORS πŸ’€

if an error occurs, ldap_open() and ldap_init() will return NULL and errno should be set appropriately.

ldap_initialize() and ldap_init_fd() will directly return the LDAP code associated to the error (or LDAP_SUCCESS in case of success); errno should be set as well whenever appropriate.

ldap_set_urllist_proc() returns LDAP_OPT_ERROR on error, and LDAP_OPT_SUCCESS on success.

so like, three different error-reporting personalities in one manpage. they contain multitudes. πŸ¦‹


🌸 SEE ALSO 🌸

go read them, iykyk. πŸ“šπŸ’–


πŸŽ€ ACKNOWLEDGEMENTS πŸŽ€

OpenLDAP Software is developed and maintained by The OpenLDAP Project <http://www.openldap.org/>. OpenLDAP Software is derived from the University of Michigan LDAP 3.3 Release. shoutout the ancestors, fr fr. πŸ«ΆπŸ‘‘


and that's the whole page, bestie β€” OpenLDAP 2.6.16, dated 2026/10/21, LDAP_OPEN(3) forever. now go open a connection, slay, and remember: handle first, operations second. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from