3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * BindRequest ::= SEQUENCE {
10 * name DistinguishedName, -- who
11 * authentication CHOICE {
12 * simple [0] OCTET STRING -- passwd
13 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
14 * krbv42ldap [1] OCTET STRING
15 * krbv42dsa [2] OCTET STRING
17 * sasl [3] SaslCredentials -- LDAPv3
21 * BindResponse ::= SEQUENCE {
22 * COMPONENTS OF LDAPResult,
23 * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
33 #include <ac/socket.h>
34 #include <ac/string.h>
41 * ldap_sasl_bind - bind to the ldap server (and X.500).
42 * The dn (usually NULL), mechanism, and credentials are provided.
43 * The message id of the request initiated is provided upon successful
44 * (LDAP_SUCCESS) return.
47 * ldap_sasl_bind( ld, NULL, "mechanism",
48 * cred, NULL, NULL, &msgid )
55 LDAP_CONST char *mechanism,
64 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
67 assert( LDAP_VALID( ld ) );
68 assert( msgidp != NULL );
70 /* check client controls */
71 rc = ldap_int_client_controls( ld, cctrls );
72 if( rc != LDAP_SUCCESS ) return rc;
74 if( msgidp == NULL ) {
75 ld->ld_errno = LDAP_PARAM_ERROR;
79 if( mechanism == LDAP_SASL_SIMPLE ) {
80 if( dn == NULL && cred != NULL ) {
81 /* use default binddn */
82 dn = ld->ld_defbinddn;
85 } else if( ld->ld_version < LDAP_VERSION3 ) {
86 ld->ld_errno = LDAP_NOT_SUPPORTED;
94 /* create a message to send */
95 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
96 ld->ld_errno = LDAP_NO_MEMORY;
100 assert( LBER_VALID( ber ) );
102 if( mechanism == LDAP_SASL_SIMPLE ) {
104 rc = ber_printf( ber, "{it{istON}" /*}*/,
105 ++ld->ld_msgid, LDAP_REQ_BIND,
106 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
109 } else if ( cred == NULL || !cred->bv_len ) {
110 /* SASL bind w/o creditials */
111 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
112 ++ld->ld_msgid, LDAP_REQ_BIND,
113 ld->ld_version, dn, LDAP_AUTH_SASL,
117 /* SASL bind w/ creditials */
118 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
119 ++ld->ld_msgid, LDAP_REQ_BIND,
120 ld->ld_version, dn, LDAP_AUTH_SASL,
125 ld->ld_errno = LDAP_ENCODING_ERROR;
130 /* Put Server Controls */
131 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
136 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
137 ld->ld_errno = LDAP_ENCODING_ERROR;
143 if ( ld->ld_cache != NULL ) {
144 ldap_flush_cache( ld );
146 #endif /* !LDAP_NOCACHE */
148 /* send the message */
149 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
162 LDAP_CONST char *mechanism,
164 LDAPControl **sctrls,
165 LDAPControl **cctrls,
166 struct berval **servercredp )
170 struct berval *scredp = NULL;
172 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
174 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
175 if( servercredp != NULL ) {
176 if (ld->ld_version < LDAP_VERSION3) {
177 ld->ld_errno = LDAP_NOT_SUPPORTED;
183 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
185 if ( rc != LDAP_SUCCESS ) {
189 #ifdef LDAP_CONNECTIONLESS
190 if (LDAP_IS_UDP(ld)) {
195 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
196 return( ld->ld_errno ); /* ldap_result sets ld_errno */
199 /* parse the results */
201 if( servercredp != NULL ) {
202 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
205 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
206 ldap_msgfree( result );
210 rc = ldap_result2error( ld, result, 1 );
212 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
213 if( servercredp != NULL ) {
214 *servercredp = scredp;
219 if ( scredp != NULL ) {
228 * Parse BindResponse:
230 * BindResponse ::= [APPLICATION 1] SEQUENCE {
231 * COMPONENTS OF LDAPResult,
232 * serverSaslCreds [7] OCTET STRING OPTIONAL }
234 * LDAPResult ::= SEQUENCE {
235 * resultCode ENUMERATED,
237 * errorMessage LDAPString,
238 * referral [3] Referral OPTIONAL }
242 ldap_parse_sasl_bind_result(
245 struct berval **servercredp,
249 struct berval* scred;
254 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
256 assert( ld != NULL );
257 assert( LDAP_VALID( ld ) );
258 assert( res != NULL );
260 if ( ld == NULL || res == NULL ) {
261 return LDAP_PARAM_ERROR;
264 if( servercredp != NULL ) {
265 if( ld->ld_version < LDAP_VERSION2 ) {
266 return LDAP_NOT_SUPPORTED;
271 if( res->lm_msgtype != LDAP_RES_BIND ) {
272 ld->ld_errno = LDAP_PARAM_ERROR;
278 if ( ld->ld_error ) {
279 LDAP_FREE( ld->ld_error );
282 if ( ld->ld_matched ) {
283 LDAP_FREE( ld->ld_matched );
284 ld->ld_matched = NULL;
289 ber = ber_dup( res->lm_ber );
292 ld->ld_errno = LDAP_NO_MEMORY;
296 if ( ld->ld_version < LDAP_VERSION2 ) {
297 tag = ber_scanf( ber, "{ia}",
298 &errcode, &ld->ld_error );
300 if( tag == LBER_ERROR ) {
302 ld->ld_errno = LDAP_DECODING_ERROR;
309 tag = ber_scanf( ber, "{iaa" /*}*/,
310 &errcode, &ld->ld_matched, &ld->ld_error );
312 if( tag == LBER_ERROR ) {
314 ld->ld_errno = LDAP_DECODING_ERROR;
318 tag = ber_peek_tag(ber, &len);
320 if( tag == LDAP_TAG_REFERRAL ) {
322 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
324 ld->ld_errno = LDAP_DECODING_ERROR;
328 tag = ber_peek_tag(ber, &len);
331 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
332 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
334 ld->ld_errno = LDAP_DECODING_ERROR;
342 if ( servercredp != NULL ) {
343 *servercredp = scred;
345 } else if ( scred != NULL ) {
349 ld->ld_errno = errcode;
355 return( ld->ld_errno );
359 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
361 /* we need to query the server for supported mechs anyway */
362 LDAPMessage *res, *e;
363 char *attrs[] = { "supportedSASLMechanisms", NULL };
364 char **values, *mechlist;
367 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
369 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
370 NULL, attrs, 0, &res );
372 if ( rc != LDAP_SUCCESS ) {
376 e = ldap_first_entry( ld, res );
379 if ( ld->ld_errno == LDAP_SUCCESS ) {
380 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
385 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
386 if ( values == NULL ) {
388 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
392 mechlist = ldap_charray2str( values, " " );
393 if ( mechlist == NULL ) {
394 LDAP_VFREE( values );
396 ld->ld_errno = LDAP_NO_MEMORY;
400 LDAP_VFREE( values );
403 *pmechlist = mechlist;
409 * ldap_sasl_interactive_bind_s - interactive SASL authentication
411 * This routine uses interactive callbacks.
413 * LDAP_SUCCESS is returned upon success, the ldap error code
417 ldap_sasl_interactive_bind_s(
419 LDAP_CONST char *dn, /* usually NULL */
420 LDAP_CONST char *mechs,
421 LDAPControl **serverControls,
422 LDAPControl **clientControls,
424 LDAP_SASL_INTERACT_PROC *interact,
429 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
430 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
432 #ifdef LDAP_CONNECTIONLESS
433 if( LDAP_IS_UDP(ld) ) {
434 /* Just force it to simple bind, silly to make the user
435 * ask all the time. No, we don't ever actually bind, but I'll
436 * let the final bind handler take care of saving the cdn.
438 rc = ldap_simple_bind(ld, dn, NULL);
439 return rc < 0 ? rc : 0;
442 if( mechs == NULL || *mechs == '\0' ) {
445 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
447 if( rc != LDAP_SUCCESS ) {
451 Debug( LDAP_DEBUG_TRACE,
452 "ldap_interactive_sasl_bind_s: server supports: %s\n",
458 Debug( LDAP_DEBUG_TRACE,
459 "ldap_interactive_sasl_bind_s: user selected: %s\n",
463 rc = ldap_int_sasl_bind( ld, dn, mechs,
464 serverControls, clientControls,
465 flags, interact, defaults );
468 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
469 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );