---
name: "ldap_unbind_s"
section: "3"
platform: "linux"
package: "libldap"
version: "2.7.2-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/ldap_bind"
---

# ldap\_unbind\_s(3)

LDAP bind routines

Part of [ldap\_bind(3)](/ldap_bind/3.md).

## NAME

ldap\_bind, ldap\_bind\_s, ldap\_simple\_bind, ldap\_simple\_bind\_s, ldap\_sasl\_bind, ldap\_sasl\_bind\_s, ldap\_sasl\_interactive\_bind\_s, ldap\_parse\_sasl\_bind\_result, ldap\_unbind, ldap\_unbind\_s, ldap\_unbind\_ext, ldap\_unbind\_ext\_s, ldap\_set\_rebind\_proc - LDAP bind routines

## LIBRARY

OpenLDAP LDAP (libldap, -lldap)

## SYNOPSIS

```
#include <ldap.h>
```

**int ldap\_bind(LDAP \****ld***, const char \****who***, const char \****cred***,**

**int** *method***);**

**int ldap\_bind\_s(LDAP \****ld***, const char \****who***, const char \****cred***,**

**int** *method***);**

**int ldap\_simple\_bind(LDAP \****ld***, const char \****who***, const char \****passwd***);**

**int ldap\_simple\_bind\_s(LDAP \****ld***, const char \****who***, const char \****passwd***);**

**int ldap\_sasl\_bind(LDAP \****ld***, const char \****dn***, const char \****mechanism***,**

**struct berval \****cred***, LDAPControl \****sctrls***\[\],** **LDAPControl \****cctrls***\[\], int \****msgidp***);**

**int ldap\_sasl\_bind\_s(LDAP \****ld***, const char \****dn***, const char \****mechanism***,**

**struct berval \****cred***, LDAPControl \****sctrls***\[\],** **LDAPControl \****cctrls***\[\], struct berval \*\****servercredp***);**

**int ldap\_parse\_sasl\_bind\_result(LDAP \****ld***, LDAPMessage \****res***,**

**struct berval \*\****servercredp***, int** *freeit***);**

**int ldap\_sasl\_interactive\_bind\_s(LDAP \****ld***, const char \****dn***,**

**const char \****mechs***,** **LDAPControl \****sctrls***\[\], LDAPControl \****cctrls***\[\],** **unsigned** *flags***, LDAP\_SASL\_INTERACT\_PROC \****interact***,** **void \****defaults***);**

**int ldap\_sasl\_interactive\_bind(LDAP \****ld***, const char \****dn***,**

**const char \****mechs***,** **LDAPControl \****sctrls***\[\], LDAPControl \****cctrls***\[\],** **unsigned** *flags***, LDAP\_SASL\_INTERACT\_PROC \****interact***,** **void \****defaults***, LDAPMessage \****result***,** **const char \*\****rmechp***, int \****msgidp***);**

**int (LDAP\_SASL\_INTERACT\_PROC)(LDAP \****ld***, unsigned** *flags***, void \****defaults***, void \****sasl\_interact***);**

**int ldap\_unbind(LDAP \****ld***);**

**int ldap\_unbind\_s(LDAP \****ld***);**

**int ldap\_unbind\_ext(LDAP \****ld***, LDAPControl \****sctrls***\[\],**

**LDAPControl \****cctrls***\[\]);**

**int ldap\_unbind\_ext\_s(LDAP \****ld***, LDAPControl \****sctrls***\[\],**

**LDAPControl \****cctrls***\[\]);**

**int ldap\_set\_rebind\_proc (LDAP \****ld***, LDAP\_REBIND\_PROC \****ldap\_proc***, void \****params***);**

**int (LDAP\_REBIND\_PROC)(LDAP \****ld***, LDAP\_CONST char \****url***, ber\_tag\_t** *request***, ber\_int\_t** *msgid***, void \****params***);**

## DESCRIPTION

These routines provide various interfaces to the LDAP bind operation. After an association with an LDAP server is made using [**ldap\_init**(3)](/ldap_init/3.md), an LDAP bind operation should be performed before other operations are attempted over the connection.  An LDAP bind is required when using Version 2 of the LDAP protocol; it is optional for Version 3 but is usually needed due to security considerations.

There are three types of bind calls, ones providing simple authentication, ones providing SASL authentication, and general routines capable of doing either simple or SASL authentication.

**SASL** (Simple Authentication and Security Layer) can negotiate one of many different kinds of authentication. Both synchronous and asynchronous versions of each variant of the bind call are provided.  All routines take *ld* as their first parameter, as returned from [**ldap\_init**(3)](/ldap_init/3.md).

## SIMPLE AUTHENTICATION

The simplest form of the bind call is **ldap\_simple\_bind\_s()**. It takes the DN to bind as in *who*, and the userPassword associated with the entry in *passwd*.  It returns an LDAP error indication (see [**ldap\_error**(3)](/ldap_error/3.md)). The **ldap\_simple\_bind()** call is asynchronous, taking the same parameters but only initiating the bind operation and returning the message id of the request it sent.  The result of the operation can be obtained by a subsequent call to [**ldap\_result**(3)](/ldap_result/3.md). The **ldap\_sasl\_bind\_s()** and asynchronous **ldap\_sasl\_bind()** functions can also be used to make a simple bind by using LDAP\_SASL\_SIMPLE as the SASL mechanism.

## GENERAL AUTHENTICATION

The **ldap\_bind()** and **ldap\_bind\_s()** routines can be used when the authentication method to use needs to be selected at runtime.  They both take an extra *method* parameter selecting the authentication method to use.  It should be set to LDAP\_AUTH\_SIMPLE to select simple authentication. **ldap\_bind()** returns the message id of the request it initiates. **ldap\_bind\_s()** returns an LDAP error indication.

## SASL AUTHENTICATION

For SASL binds the server always ignores any provided DN, so the *dn* parameter should always be NULL. **ldap\_sasl\_bind\_s**() sends a single SASL bind request with the given SASL *mechanism* and credentials in the *cred* parameter. The format of the credentials depends on the particular SASL mechanism in use. For mechanisms that provide mutual authentication the server's credentials will be returned in the *servercredp* parameter. The routine returns an LDAP error indication (see [**ldap\_error**(3)](/ldap_error/3.md)). The **ldap\_sasl\_bind**() call is asynchronous, taking the same parameters but only sending the request and returning the message id of the request it sent. The result of the operation can be obtained by a subsequent call to [**ldap\_result**(3)](/ldap_result/3.md). The result must be additionally parsed by **ldap\_parse\_sasl\_bind\_result**() to obtain any server credentials sent from the server.

Any returned server credentials should be freed using **ber\_bvfree**().

Many SASL mechanisms require multiple message exchanges to perform a complete authentication. Applications should generally use **ldap\_sasl\_interactive\_bind\_s**() rather than calling the basic **ldap\_sasl\_bind**() functions directly. The *mechs* parameter should contain a space-separated list of candidate mechanisms to use. If this parameter is NULL or empty the library will query the supportedSASLMechanisms attribute from the server's rootDSE for the list of SASL mechanisms the server supports. The *flags* parameter controls the interaction used to retrieve any necessary SASL authentication parameters and should be one of:

- LDAP\_SASL\_AUTOMATIC

  use defaults if available, prompt otherwise

- LDAP\_SASL\_INTERACTIVE

  always prompt

- LDAP\_SASL\_QUIET

  never prompt

The *interact* function uses the provided *defaults* to handle requests from the SASL library for particular authentication parameters. There is no defined format for the *defaults* information; it is up to the caller to use whatever format is appropriate for the supplied *interact* function. The *sasl\_interact* parameter comes from the underlying SASL library. When used with Cyrus SASL this is an array of **sasl\_interact\_t** structures. The Cyrus SASL library will prompt for a variety of inputs, including:

- SASL\_CB\_GETREALM

  the realm for the authentication attempt

- SASL\_CB\_AUTHNAME

  the username to authenticate

- SASL\_CB\_PASS

  the password for the provided username

- SASL\_CB\_USER

  the username to use for proxy authorization

- SASL\_CB\_NOECHOPROMPT

  generic prompt for input with input echoing disabled

- SASL\_CB\_ECHOPROMPT

  generic prompt for input with input echoing enabled

- SASL\_CB\_LIST\_END

  indicates the end of the array of prompts

See the Cyrus SASL documentation for more details.

Applications which need to manage connections asynchronously may use **ldap\_sasl\_interactive\_bind**() instead of the synchronous version. A valid mechs parameter must be supplied, otherwise the library will be forced to query the server for a list of supported mechanisms, and this query will be performed synchronously. The other parameters are the same as for the synchronous function, with three additional parameters. The actual SASL mechanism that was used, and the message ID for use with **ldap\_result**() will be returned in rmechp and msgidp, respectively. The value in rmechp must not be modified by the caller and must be passed back on each subsequent call. The message obtained from **ldap\_result**() must be passed in the result parameter. This parameter must be NULL when initiating a new Bind. The caller must free the result message after each call using **ldap\_msgfree**(). The **ldap\_sasl\_interactive\_bind**() function returns an LDAP result code. If the code is LDAP\_SASL\_BIND\_IN\_PROGRESS then the Bind is not complete yet, and this function must be called again with the next result from the server.

## REBINDING

The **ldap\_set\_rebind\_proc** function() sets the process to use for binding when an operation returns a referral. This function is used when an application needs to bind to another server in order to follow a referral or search continuation reference.

The function takes *ld*, the *rebind* function, and the *params*, the arbitrary data like state information which the client might need to properly rebind. The LDAP\_OPT\_REFERRALS option in the *ld* must be set to ON for the libraries to use the rebind function. Use the **ldap\_set\_option** function to set the value.

The rebind function parameters are as follows:

The *ld* parameter must be used by the application when binding to the referred server if the application wants the libraries to follow the referral.

The *url* parameter points to the URL referral string received from the LDAP server. The LDAP application can use the  [**ldap\_url\_parse**(3)](/ldap_url_parse/3.md) function to parse the string into its components.

The *request* parameter specifies the type of request that generated the referral.

The *msgid* parameter specifies the message ID of the request generating the referral.

The *params* parameter is the same value as passed originally to the **ldap\_set\_rebind\_proc**() function.

The LDAP libraries set all the parameters when they call the rebind function. The application should not attempt to free either the ld or the url structures in the rebind function.

The application must supply to the rebind function the required authentication information such as, user name, password, and certificates. The rebind function must use a synchronous bind method.

## UNBINDING

The **ldap\_unbind()** call is used to unbind from the directory, terminate the current association, and free the resources contained in the *ld* structure.  Once it is called, the connection to the LDAP server is closed, and the *ld* structure is invalid. The **ldap\_unbind\_s()** call is just another name for **ldap\_unbind()**; both of these calls are synchronous in nature.

The **ldap\_unbind\_ext()** and **ldap\_unbind\_ext\_s()** allows the operations to specify  controls.

## ERRORS

Asynchronous routines will return -1 in case of error, setting the *ld\_errno* parameter of the *ld* structure.  Synchronous routines return whatever *ld\_errno* is set to.  See [**ldap\_error**(3)](/ldap_error/3.md) for more information.

## NOTES

If an anonymous bind is sufficient for the application, the rebind process need not be provided. The LDAP libraries with the LDAP\_OPT\_REFERRALS option set to ON (default value) will automatically follow referrals using an anonymous bind.

If the application needs stronger authentication than an anonymous bind, you need to provide a rebind process for that authentication method. The bind method must be synchronous.

## SEE ALSO

[**ldap**(3)](/ldap/3.md), [**ldap\_error**(3)](/ldap_error/3.md), [**ldap\_open**(3)](/ldap_open/3.md), [**ldap\_set\_option**(3)](/ldap_set_option/3.md), [**ldap\_url\_parse**(3)](/ldap_url_parse/3.md) **RFC 4422** (http://www.rfc-editor.org), **Cyrus SASL** (http://asg.web.cmu.edu/sasl/)

## 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.
