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 if( msgidp == NULL ) {
71 ld->ld_errno = LDAP_PARAM_ERROR;
75 if( mechanism == LDAP_SASL_SIMPLE ) {
76 if( dn == NULL && cred != NULL ) {
77 /* use default binddn */
78 dn = ld->ld_defbinddn;
81 } else if( ld->ld_version < LDAP_VERSION3 ) {
82 ld->ld_errno = LDAP_NOT_SUPPORTED;
90 /* create a message to send */
91 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
92 ld->ld_errno = LDAP_NO_MEMORY;
96 assert( BER_VALID( ber ) );
98 if( mechanism == LDAP_SASL_SIMPLE ) {
100 rc = ber_printf( ber, "{it{istON}" /*}*/,
101 ++ld->ld_msgid, LDAP_REQ_BIND,
102 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
105 } else if ( cred == NULL || !cred->bv_len ) {
106 /* SASL bind w/o creditials */
107 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
108 ++ld->ld_msgid, LDAP_REQ_BIND,
109 ld->ld_version, dn, LDAP_AUTH_SASL,
113 /* SASL bind w/ creditials */
114 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
115 ++ld->ld_msgid, LDAP_REQ_BIND,
116 ld->ld_version, dn, LDAP_AUTH_SASL,
121 ld->ld_errno = LDAP_ENCODING_ERROR;
126 /* Put Server Controls */
127 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
132 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
133 ld->ld_errno = LDAP_ENCODING_ERROR;
139 if ( ld->ld_cache != NULL ) {
140 ldap_flush_cache( ld );
142 #endif /* !LDAP_NOCACHE */
144 /* send the message */
145 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
158 LDAP_CONST char *mechanism,
160 LDAPControl **sctrls,
161 LDAPControl **cctrls,
162 struct berval **servercredp )
166 struct berval *scredp = NULL;
168 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
170 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
171 if( servercredp != NULL ) {
172 if (ld->ld_version < LDAP_VERSION3) {
173 ld->ld_errno = LDAP_NOT_SUPPORTED;
179 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
181 if ( rc != LDAP_SUCCESS ) {
185 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
186 return( ld->ld_errno ); /* ldap_result sets ld_errno */
189 /* parse the results */
191 if( servercredp != NULL ) {
192 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
195 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
196 ldap_msgfree( result );
200 rc = ldap_result2error( ld, result, 1 );
202 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
203 if( servercredp != NULL ) {
204 *servercredp = scredp;
209 if ( scredp != NULL ) {
218 * Parse BindResponse:
220 * BindResponse ::= [APPLICATION 1] SEQUENCE {
221 * COMPONENTS OF LDAPResult,
222 * serverSaslCreds [7] OCTET STRING OPTIONAL }
224 * LDAPResult ::= SEQUENCE {
225 * resultCode ENUMERATED,
227 * errorMessage LDAPString,
228 * referral [3] Referral OPTIONAL }
232 ldap_parse_sasl_bind_result(
235 struct berval **servercredp,
239 struct berval* scred;
244 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
246 assert( ld != NULL );
247 assert( LDAP_VALID( ld ) );
248 assert( res != NULL );
250 if ( ld == NULL || res == NULL ) {
251 return LDAP_PARAM_ERROR;
254 if( servercredp != NULL ) {
255 if( ld->ld_version < LDAP_VERSION2 ) {
256 return LDAP_NOT_SUPPORTED;
261 if( res->lm_msgtype != LDAP_RES_BIND ) {
262 ld->ld_errno = LDAP_PARAM_ERROR;
268 if ( ld->ld_error ) {
269 LDAP_FREE( ld->ld_error );
272 if ( ld->ld_matched ) {
273 LDAP_FREE( ld->ld_matched );
274 ld->ld_matched = NULL;
279 ber = ber_dup( res->lm_ber );
282 ld->ld_errno = LDAP_NO_MEMORY;
286 if ( ld->ld_version < LDAP_VERSION2 ) {
287 tag = ber_scanf( ber, "{ia}",
288 &errcode, &ld->ld_error );
290 if( tag == LBER_ERROR ) {
292 ld->ld_errno = LDAP_DECODING_ERROR;
299 tag = ber_scanf( ber, "{iaa" /*}*/,
300 &errcode, &ld->ld_matched, &ld->ld_error );
302 if( tag == LBER_ERROR ) {
304 ld->ld_errno = LDAP_DECODING_ERROR;
308 tag = ber_peek_tag(ber, &len);
310 if( tag == LDAP_TAG_REFERRAL ) {
312 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
314 ld->ld_errno = LDAP_DECODING_ERROR;
318 tag = ber_peek_tag(ber, &len);
321 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
322 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
324 ld->ld_errno = LDAP_DECODING_ERROR;
332 if ( servercredp != NULL ) {
333 *servercredp = scred;
335 } else if ( scred != NULL ) {
339 ld->ld_errno = errcode;
345 return( ld->ld_errno );
349 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
351 /* we need to query the server for supported mechs anyway */
352 LDAPMessage *res, *e;
353 char *attrs[] = { "supportedSASLMechanisms", NULL };
354 char **values, *mechlist;
357 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
359 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
360 NULL, attrs, 0, &res );
362 if ( rc != LDAP_SUCCESS ) {
366 e = ldap_first_entry( ld, res );
369 if ( ld->ld_errno == LDAP_SUCCESS ) {
370 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
375 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
376 if ( values == NULL ) {
378 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
382 mechlist = ldap_charray2str( values, " " );
383 if ( mechlist == NULL ) {
384 LDAP_VFREE( values );
386 ld->ld_errno = LDAP_NO_MEMORY;
390 LDAP_VFREE( values );
393 *pmechlist = mechlist;
399 * ldap_sasl_interactive_bind_s - interactive SASL authentication
401 * This routine uses interactive callbacks.
403 * LDAP_SUCCESS is returned upon success, the ldap error code
407 ldap_sasl_interactive_bind_s(
409 LDAP_CONST char *dn, /* usually NULL */
410 LDAP_CONST char *mechs,
411 LDAPControl **serverControls,
412 LDAPControl **clientControls,
414 LDAP_SASL_INTERACT_PROC *interact,
419 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
420 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
423 if( mechs == NULL || *mechs == '\0' ) {
426 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
428 if( rc != LDAP_SUCCESS ) {
432 Debug( LDAP_DEBUG_TRACE,
433 "ldap_interactive_sasl_bind_s: server supports: %s\n",
439 Debug( LDAP_DEBUG_TRACE,
440 "ldap_interactive_sasl_bind_s: user selected: %s\n",
444 rc = ldap_int_sasl_bind( ld, dn, mechs,
445 serverControls, clientControls,
446 flags, interact, defaults );
449 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
450 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );