3 * Copyright 1998-2003 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_val == NULL ) {
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 /* send the message */
143 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
156 LDAP_CONST char *mechanism,
158 LDAPControl **sctrls,
159 LDAPControl **cctrls,
160 struct berval **servercredp )
164 struct berval *scredp = NULL;
167 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_sasl_bind_s\n", 0, 0, 0 );
169 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
172 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
173 if( servercredp != NULL ) {
174 if (ld->ld_version < LDAP_VERSION3) {
175 ld->ld_errno = LDAP_NOT_SUPPORTED;
181 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
183 if ( rc != LDAP_SUCCESS ) {
187 #ifdef LDAP_CONNECTIONLESS
188 if (LDAP_IS_UDP(ld)) {
193 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
194 return( ld->ld_errno ); /* ldap_result sets ld_errno */
197 /* parse the results */
199 if( servercredp != NULL ) {
200 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
203 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
204 ldap_msgfree( result );
208 rc = ldap_result2error( ld, result, 1 );
210 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
211 if( servercredp != NULL ) {
212 *servercredp = scredp;
217 if ( scredp != NULL ) {
226 * Parse BindResponse:
228 * BindResponse ::= [APPLICATION 1] SEQUENCE {
229 * COMPONENTS OF LDAPResult,
230 * serverSaslCreds [7] OCTET STRING OPTIONAL }
232 * LDAPResult ::= SEQUENCE {
233 * resultCode ENUMERATED,
235 * errorMessage LDAPString,
236 * referral [3] Referral OPTIONAL }
240 ldap_parse_sasl_bind_result(
243 struct berval **servercredp,
247 struct berval* scred;
253 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
255 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
258 assert( ld != NULL );
259 assert( LDAP_VALID( ld ) );
260 assert( res != NULL );
262 if( servercredp != NULL ) {
263 if( ld->ld_version < LDAP_VERSION2 ) {
264 return LDAP_NOT_SUPPORTED;
269 if( res->lm_msgtype != LDAP_RES_BIND ) {
270 ld->ld_errno = LDAP_PARAM_ERROR;
276 if ( ld->ld_error ) {
277 LDAP_FREE( ld->ld_error );
280 if ( ld->ld_matched ) {
281 LDAP_FREE( ld->ld_matched );
282 ld->ld_matched = NULL;
287 ber = ber_dup( res->lm_ber );
290 ld->ld_errno = LDAP_NO_MEMORY;
294 if ( ld->ld_version < LDAP_VERSION2 ) {
295 tag = ber_scanf( ber, "{ia}",
296 &errcode, &ld->ld_error );
298 if( tag == LBER_ERROR ) {
300 ld->ld_errno = LDAP_DECODING_ERROR;
307 tag = ber_scanf( ber, "{iaa" /*}*/,
308 &errcode, &ld->ld_matched, &ld->ld_error );
310 if( tag == LBER_ERROR ) {
312 ld->ld_errno = LDAP_DECODING_ERROR;
316 tag = ber_peek_tag(ber, &len);
318 if( tag == LDAP_TAG_REFERRAL ) {
320 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
322 ld->ld_errno = LDAP_DECODING_ERROR;
326 tag = ber_peek_tag(ber, &len);
329 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
330 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
332 ld->ld_errno = LDAP_DECODING_ERROR;
340 if ( servercredp != NULL ) {
341 *servercredp = scred;
343 } else if ( scred != NULL ) {
347 ld->ld_errno = errcode;
353 return( ld->ld_errno );
357 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
359 /* we need to query the server for supported mechs anyway */
360 LDAPMessage *res, *e;
361 char *attrs[] = { "supportedSASLMechanisms", NULL };
362 char **values, *mechlist;
366 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
368 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
371 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
372 NULL, attrs, 0, &res );
374 if ( rc != LDAP_SUCCESS ) {
378 e = ldap_first_entry( ld, res );
381 if ( ld->ld_errno == LDAP_SUCCESS ) {
382 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
387 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
388 if ( values == NULL ) {
390 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
394 mechlist = ldap_charray2str( values, " " );
395 if ( mechlist == NULL ) {
396 LDAP_VFREE( values );
398 ld->ld_errno = LDAP_NO_MEMORY;
402 LDAP_VFREE( values );
405 *pmechlist = mechlist;
411 * ldap_sasl_interactive_bind_s - interactive SASL authentication
413 * This routine uses interactive callbacks.
415 * LDAP_SUCCESS is returned upon success, the ldap error code
419 ldap_sasl_interactive_bind_s(
421 LDAP_CONST char *dn, /* usually NULL */
422 LDAP_CONST char *mechs,
423 LDAPControl **serverControls,
424 LDAPControl **clientControls,
426 LDAP_SASL_INTERACT_PROC *interact,
431 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
432 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
434 #ifdef LDAP_CONNECTIONLESS
435 if( LDAP_IS_UDP(ld) ) {
436 /* Just force it to simple bind, silly to make the user
437 * ask all the time. No, we don't ever actually bind, but I'll
438 * let the final bind handler take care of saving the cdn.
440 rc = ldap_simple_bind(ld, dn, NULL);
441 return rc < 0 ? rc : 0;
444 if( mechs == NULL || *mechs == '\0' ) {
447 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
449 if( rc != LDAP_SUCCESS ) {
454 LDAP_LOG ( TRANSPORT, DETAIL1,
455 "ldap_interactive_sasl_bind_s: server supports: %s\n",
458 Debug( LDAP_DEBUG_TRACE,
459 "ldap_interactive_sasl_bind_s: server supports: %s\n",
467 LDAP_LOG ( TRANSPORT, DETAIL1,
468 "ldap_interactive_sasl_bind_s: user selected: %s\n", mechs, 0, 0 );
470 Debug( LDAP_DEBUG_TRACE,
471 "ldap_interactive_sasl_bind_s: user selected: %s\n",
476 rc = ldap_int_sasl_bind( ld, dn, mechs,
477 serverControls, clientControls,
478 flags, interact, defaults );
481 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
482 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );