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 ( TRANSPORT, ENTRY, "ldap_sasl_bind\n", 0, 0, 0 );
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( mechanism == LDAP_SASL_SIMPLE ) {
79 if( dn == NULL && cred != NULL ) {
80 /* use default binddn */
81 dn = ld->ld_defbinddn;
84 } else if( ld->ld_version < LDAP_VERSION3 ) {
85 ld->ld_errno = LDAP_NOT_SUPPORTED;
93 /* create a message to send */
94 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
95 ld->ld_errno = LDAP_NO_MEMORY;
99 assert( LBER_VALID( ber ) );
101 if( mechanism == LDAP_SASL_SIMPLE ) {
103 rc = ber_printf( ber, "{it{istON}" /*}*/,
104 ++ld->ld_msgid, LDAP_REQ_BIND,
105 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
108 } else if ( cred == NULL || !cred->bv_len ) {
109 /* SASL bind w/o creditials */
110 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
111 ++ld->ld_msgid, LDAP_REQ_BIND,
112 ld->ld_version, dn, LDAP_AUTH_SASL,
116 /* SASL bind w/ creditials */
117 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
118 ++ld->ld_msgid, LDAP_REQ_BIND,
119 ld->ld_version, dn, LDAP_AUTH_SASL,
124 ld->ld_errno = LDAP_ENCODING_ERROR;
129 /* Put Server Controls */
130 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
135 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
136 ld->ld_errno = LDAP_ENCODING_ERROR;
142 if ( ld->ld_cache != NULL ) {
143 ldap_flush_cache( ld );
145 #endif /* !LDAP_NOCACHE */
147 /* send the message */
148 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
161 LDAP_CONST char *mechanism,
163 LDAPControl **sctrls,
164 LDAPControl **cctrls,
165 struct berval **servercredp )
169 struct berval *scredp = NULL;
172 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_sasl_bind_s\n", 0, 0, 0 );
174 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
177 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
178 if( servercredp != NULL ) {
179 if (ld->ld_version < LDAP_VERSION3) {
180 ld->ld_errno = LDAP_NOT_SUPPORTED;
186 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
188 if ( rc != LDAP_SUCCESS ) {
192 #ifdef LDAP_CONNECTIONLESS
193 if (LDAP_IS_UDP(ld)) {
198 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
199 return( ld->ld_errno ); /* ldap_result sets ld_errno */
202 /* parse the results */
204 if( servercredp != NULL ) {
205 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
208 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
209 ldap_msgfree( result );
213 rc = ldap_result2error( ld, result, 1 );
215 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
216 if( servercredp != NULL ) {
217 *servercredp = scredp;
222 if ( scredp != NULL ) {
231 * Parse BindResponse:
233 * BindResponse ::= [APPLICATION 1] SEQUENCE {
234 * COMPONENTS OF LDAPResult,
235 * serverSaslCreds [7] OCTET STRING OPTIONAL }
237 * LDAPResult ::= SEQUENCE {
238 * resultCode ENUMERATED,
240 * errorMessage LDAPString,
241 * referral [3] Referral OPTIONAL }
245 ldap_parse_sasl_bind_result(
248 struct berval **servercredp,
252 struct berval* scred;
258 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
260 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
263 assert( ld != NULL );
264 assert( LDAP_VALID( ld ) );
265 assert( res != NULL );
267 if( servercredp != NULL ) {
268 if( ld->ld_version < LDAP_VERSION2 ) {
269 return LDAP_NOT_SUPPORTED;
274 if( res->lm_msgtype != LDAP_RES_BIND ) {
275 ld->ld_errno = LDAP_PARAM_ERROR;
281 if ( ld->ld_error ) {
282 LDAP_FREE( ld->ld_error );
285 if ( ld->ld_matched ) {
286 LDAP_FREE( ld->ld_matched );
287 ld->ld_matched = NULL;
292 ber = ber_dup( res->lm_ber );
295 ld->ld_errno = LDAP_NO_MEMORY;
299 if ( ld->ld_version < LDAP_VERSION2 ) {
300 tag = ber_scanf( ber, "{ia}",
301 &errcode, &ld->ld_error );
303 if( tag == LBER_ERROR ) {
305 ld->ld_errno = LDAP_DECODING_ERROR;
312 tag = ber_scanf( ber, "{iaa" /*}*/,
313 &errcode, &ld->ld_matched, &ld->ld_error );
315 if( tag == LBER_ERROR ) {
317 ld->ld_errno = LDAP_DECODING_ERROR;
321 tag = ber_peek_tag(ber, &len);
323 if( tag == LDAP_TAG_REFERRAL ) {
325 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
327 ld->ld_errno = LDAP_DECODING_ERROR;
331 tag = ber_peek_tag(ber, &len);
334 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
335 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
337 ld->ld_errno = LDAP_DECODING_ERROR;
345 if ( servercredp != NULL ) {
346 *servercredp = scred;
348 } else if ( scred != NULL ) {
352 ld->ld_errno = errcode;
358 return( ld->ld_errno );
362 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
364 /* we need to query the server for supported mechs anyway */
365 LDAPMessage *res, *e;
366 char *attrs[] = { "supportedSASLMechanisms", NULL };
367 char **values, *mechlist;
371 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
373 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
376 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
377 NULL, attrs, 0, &res );
379 if ( rc != LDAP_SUCCESS ) {
383 e = ldap_first_entry( ld, res );
386 if ( ld->ld_errno == LDAP_SUCCESS ) {
387 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
392 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
393 if ( values == NULL ) {
395 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
399 mechlist = ldap_charray2str( values, " " );
400 if ( mechlist == NULL ) {
401 LDAP_VFREE( values );
403 ld->ld_errno = LDAP_NO_MEMORY;
407 LDAP_VFREE( values );
410 *pmechlist = mechlist;
416 * ldap_sasl_interactive_bind_s - interactive SASL authentication
418 * This routine uses interactive callbacks.
420 * LDAP_SUCCESS is returned upon success, the ldap error code
424 ldap_sasl_interactive_bind_s(
426 LDAP_CONST char *dn, /* usually NULL */
427 LDAP_CONST char *mechs,
428 LDAPControl **serverControls,
429 LDAPControl **clientControls,
431 LDAP_SASL_INTERACT_PROC *interact,
436 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
437 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
439 #ifdef LDAP_CONNECTIONLESS
440 if( LDAP_IS_UDP(ld) ) {
441 /* Just force it to simple bind, silly to make the user
442 * ask all the time. No, we don't ever actually bind, but I'll
443 * let the final bind handler take care of saving the cdn.
445 rc = ldap_simple_bind(ld, dn, NULL);
446 return rc < 0 ? rc : 0;
449 if( mechs == NULL || *mechs == '\0' ) {
452 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
454 if( rc != LDAP_SUCCESS ) {
459 LDAP_LOG ( TRANSPORT, DETAIL1,
460 "ldap_interactive_sasl_bind_s: server supports: %s\n",
463 Debug( LDAP_DEBUG_TRACE,
464 "ldap_interactive_sasl_bind_s: server supports: %s\n",
472 LDAP_LOG ( TRANSPORT, DETAIL1,
473 "ldap_interactive_sasl_bind_s: user selected: %s\n", mechs, 0, 0 );
475 Debug( LDAP_DEBUG_TRACE,
476 "ldap_interactive_sasl_bind_s: user selected: %s\n",
481 rc = ldap_int_sasl_bind( ld, dn, mechs,
482 serverControls, clientControls,
483 flags, interact, defaults );
486 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
487 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );