3 * Copyright 1998-2002 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
32 #include <ac/socket.h>
33 #include <ac/stdlib.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,
65 LDAP_LOG (( "sasl", LDAP_LEVEL_ENTRY, "ldap_sasl_bind\n" ));
67 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
71 assert( LDAP_VALID( ld ) );
72 assert( msgidp != NULL );
74 /* check client controls */
75 rc = ldap_int_client_controls( ld, cctrls );
76 if( rc != LDAP_SUCCESS ) return rc;
78 if( msgidp == NULL ) {
79 ld->ld_errno = LDAP_PARAM_ERROR;
83 if( mechanism == LDAP_SASL_SIMPLE ) {
84 if( dn == NULL && cred != NULL ) {
85 /* use default binddn */
86 dn = ld->ld_defbinddn;
89 } else if( ld->ld_version < LDAP_VERSION3 ) {
90 ld->ld_errno = LDAP_NOT_SUPPORTED;
98 /* create a message to send */
99 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
100 ld->ld_errno = LDAP_NO_MEMORY;
104 assert( LBER_VALID( ber ) );
106 if( mechanism == LDAP_SASL_SIMPLE ) {
108 rc = ber_printf( ber, "{it{istON}" /*}*/,
109 ++ld->ld_msgid, LDAP_REQ_BIND,
110 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
113 } else if ( cred == NULL || !cred->bv_len ) {
114 /* SASL bind w/o creditials */
115 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
116 ++ld->ld_msgid, LDAP_REQ_BIND,
117 ld->ld_version, dn, LDAP_AUTH_SASL,
121 /* SASL bind w/ creditials */
122 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
123 ++ld->ld_msgid, LDAP_REQ_BIND,
124 ld->ld_version, dn, LDAP_AUTH_SASL,
129 ld->ld_errno = LDAP_ENCODING_ERROR;
134 /* Put Server Controls */
135 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
140 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
141 ld->ld_errno = LDAP_ENCODING_ERROR;
147 if ( ld->ld_cache != NULL ) {
148 ldap_flush_cache( ld );
150 #endif /* !LDAP_NOCACHE */
152 /* send the message */
153 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
166 LDAP_CONST char *mechanism,
168 LDAPControl **sctrls,
169 LDAPControl **cctrls,
170 struct berval **servercredp )
174 struct berval *scredp = NULL;
177 LDAP_LOG (( "sasl", LDAP_LEVEL_ENTRY, "ldap_sasl_bind_s\n" ));
179 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
182 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
183 if( servercredp != NULL ) {
184 if (ld->ld_version < LDAP_VERSION3) {
185 ld->ld_errno = LDAP_NOT_SUPPORTED;
191 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
193 if ( rc != LDAP_SUCCESS ) {
197 #ifdef LDAP_CONNECTIONLESS
198 if (LDAP_IS_UDP(ld)) {
203 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
204 return( ld->ld_errno ); /* ldap_result sets ld_errno */
207 /* parse the results */
209 if( servercredp != NULL ) {
210 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
213 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
214 ldap_msgfree( result );
218 rc = ldap_result2error( ld, result, 1 );
220 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
221 if( servercredp != NULL ) {
222 *servercredp = scredp;
227 if ( scredp != NULL ) {
236 * Parse BindResponse:
238 * BindResponse ::= [APPLICATION 1] SEQUENCE {
239 * COMPONENTS OF LDAPResult,
240 * serverSaslCreds [7] OCTET STRING OPTIONAL }
242 * LDAPResult ::= SEQUENCE {
243 * resultCode ENUMERATED,
245 * errorMessage LDAPString,
246 * referral [3] Referral OPTIONAL }
250 ldap_parse_sasl_bind_result(
253 struct berval **servercredp,
257 struct berval* scred;
263 LDAP_LOG (( "sasl", LDAP_LEVEL_ENTRY, "ldap_parse_sasl_bind_result\n" ));
265 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
268 assert( ld != NULL );
269 assert( LDAP_VALID( ld ) );
270 assert( res != NULL );
272 if ( ld == NULL || res == NULL ) {
273 return LDAP_PARAM_ERROR;
276 if( servercredp != NULL ) {
277 if( ld->ld_version < LDAP_VERSION2 ) {
278 return LDAP_NOT_SUPPORTED;
283 if( res->lm_msgtype != LDAP_RES_BIND ) {
284 ld->ld_errno = LDAP_PARAM_ERROR;
290 if ( ld->ld_error ) {
291 LDAP_FREE( ld->ld_error );
294 if ( ld->ld_matched ) {
295 LDAP_FREE( ld->ld_matched );
296 ld->ld_matched = NULL;
301 ber = ber_dup( res->lm_ber );
304 ld->ld_errno = LDAP_NO_MEMORY;
308 if ( ld->ld_version < LDAP_VERSION2 ) {
309 tag = ber_scanf( ber, "{ia}",
310 &errcode, &ld->ld_error );
312 if( tag == LBER_ERROR ) {
314 ld->ld_errno = LDAP_DECODING_ERROR;
321 tag = ber_scanf( ber, "{iaa" /*}*/,
322 &errcode, &ld->ld_matched, &ld->ld_error );
324 if( tag == LBER_ERROR ) {
326 ld->ld_errno = LDAP_DECODING_ERROR;
330 tag = ber_peek_tag(ber, &len);
332 if( tag == LDAP_TAG_REFERRAL ) {
334 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
336 ld->ld_errno = LDAP_DECODING_ERROR;
340 tag = ber_peek_tag(ber, &len);
343 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
344 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
346 ld->ld_errno = LDAP_DECODING_ERROR;
354 if ( servercredp != NULL ) {
355 *servercredp = scred;
357 } else if ( scred != NULL ) {
361 ld->ld_errno = errcode;
367 return( ld->ld_errno );
371 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
373 /* we need to query the server for supported mechs anyway */
374 LDAPMessage *res, *e;
375 char *attrs[] = { "supportedSASLMechanisms", NULL };
376 char **values, *mechlist;
380 LDAP_LOG (( "sasl", LDAP_LEVEL_ENTRY, "ldap_pvt_sasl_getmech\n" ));
382 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
385 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
386 NULL, attrs, 0, &res );
388 if ( rc != LDAP_SUCCESS ) {
392 e = ldap_first_entry( ld, res );
395 if ( ld->ld_errno == LDAP_SUCCESS ) {
396 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
401 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
402 if ( values == NULL ) {
404 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
408 mechlist = ldap_charray2str( values, " " );
409 if ( mechlist == NULL ) {
410 LDAP_VFREE( values );
412 ld->ld_errno = LDAP_NO_MEMORY;
416 LDAP_VFREE( values );
419 *pmechlist = mechlist;
425 * ldap_sasl_interactive_bind_s - interactive SASL authentication
427 * This routine uses interactive callbacks.
429 * LDAP_SUCCESS is returned upon success, the ldap error code
433 ldap_sasl_interactive_bind_s(
435 LDAP_CONST char *dn, /* usually NULL */
436 LDAP_CONST char *mechs,
437 LDAPControl **serverControls,
438 LDAPControl **clientControls,
440 LDAP_SASL_INTERACT_PROC *interact,
445 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
446 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
448 #ifdef LDAP_CONNECTIONLESS
449 if( LDAP_IS_UDP(ld) ) {
450 /* Just force it to simple bind, silly to make the user
451 * ask all the time. No, we don't ever actually bind, but I'll
452 * let the final bind handler take care of saving the cdn.
454 rc = ldap_simple_bind(ld, dn, NULL);
455 return rc < 0 ? rc : 0;
458 if( mechs == NULL || *mechs == '\0' ) {
461 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
463 if( rc != LDAP_SUCCESS ) {
468 LDAP_LOG (( "sasl", LDAP_LEVEL_DETAIL1,
469 "ldap_interactive_sasl_bind_s: server supports: %s\n", smechs ));
471 Debug( LDAP_DEBUG_TRACE,
472 "ldap_interactive_sasl_bind_s: server supports: %s\n",
480 LDAP_LOG (( "sasl", LDAP_LEVEL_DETAIL1,
481 "ldap_interactive_sasl_bind_s: user selected: %s\n", mechs ));
483 Debug( LDAP_DEBUG_TRACE,
484 "ldap_interactive_sasl_bind_s: user selected: %s\n",
489 rc = ldap_int_sasl_bind( ld, dn, mechs,
490 serverControls, clientControls,
491 flags, interact, defaults );
494 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
495 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );