2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
17 * BindRequest ::= SEQUENCE {
19 * name DistinguishedName, -- who
20 * authentication CHOICE {
21 * simple [0] OCTET STRING -- passwd
22 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
23 * krbv42ldap [1] OCTET STRING
24 * krbv42dsa [2] OCTET STRING
26 * sasl [3] SaslCredentials -- LDAPv3
30 * BindResponse ::= SEQUENCE {
31 * COMPONENTS OF LDAPResult,
32 * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
41 #include <ac/socket.h>
42 #include <ac/stdlib.h>
43 #include <ac/string.h>
50 * ldap_sasl_bind - bind to the ldap server (and X.500).
51 * The dn (usually NULL), mechanism, and credentials are provided.
52 * The message id of the request initiated is provided upon successful
53 * (LDAP_SUCCESS) return.
56 * ldap_sasl_bind( ld, NULL, "mechanism",
57 * cred, NULL, NULL, &msgid )
64 LDAP_CONST char *mechanism,
74 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
77 assert( LDAP_VALID( ld ) );
78 assert( msgidp != NULL );
80 /* check client controls */
81 rc = ldap_int_client_controls( ld, cctrls );
82 if( rc != LDAP_SUCCESS ) return rc;
84 if( mechanism == LDAP_SASL_SIMPLE ) {
85 if( dn == NULL && cred != NULL && cred->bv_len ) {
86 /* use default binddn */
87 dn = ld->ld_defbinddn;
90 } else if( ld->ld_version < LDAP_VERSION3 ) {
91 ld->ld_errno = LDAP_NOT_SUPPORTED;
99 /* create a message to send */
100 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
101 ld->ld_errno = LDAP_NO_MEMORY;
105 assert( LBER_VALID( ber ) );
107 LDAP_NEXT_MSGID( ld, id );
108 if( mechanism == LDAP_SASL_SIMPLE ) {
110 rc = ber_printf( ber, "{it{istON}" /*}*/,
112 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
115 } else if ( cred == NULL || cred->bv_val == NULL ) {
116 /* SASL bind w/o credentials */
117 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
119 ld->ld_version, dn, LDAP_AUTH_SASL,
123 /* SASL bind w/ credentials */
124 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
126 ld->ld_version, dn, LDAP_AUTH_SASL,
131 ld->ld_errno = LDAP_ENCODING_ERROR;
136 /* Put Server Controls */
137 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
142 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
143 ld->ld_errno = LDAP_ENCODING_ERROR;
149 /* send the message */
150 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id );
163 LDAP_CONST char *mechanism,
165 LDAPControl **sctrls,
166 LDAPControl **cctrls,
167 struct berval **servercredp )
171 struct berval *scredp = NULL;
173 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
175 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
176 if( servercredp != NULL ) {
177 if (ld->ld_version < LDAP_VERSION3) {
178 ld->ld_errno = LDAP_NOT_SUPPORTED;
184 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
186 if ( rc != LDAP_SUCCESS ) {
190 #ifdef LDAP_CONNECTIONLESS
191 if (LDAP_IS_UDP(ld)) {
196 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
197 return( ld->ld_errno ); /* ldap_result sets ld_errno */
200 /* parse the results */
202 if( servercredp != NULL ) {
203 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
206 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
207 ldap_msgfree( result );
211 rc = ldap_result2error( ld, result, 1 );
213 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
214 if( servercredp != NULL ) {
215 *servercredp = scredp;
220 if ( scredp != NULL ) {
229 * Parse BindResponse:
231 * BindResponse ::= [APPLICATION 1] SEQUENCE {
232 * COMPONENTS OF LDAPResult,
233 * serverSaslCreds [7] OCTET STRING OPTIONAL }
235 * LDAPResult ::= SEQUENCE {
236 * resultCode ENUMERATED,
238 * errorMessage LDAPString,
239 * referral [3] Referral OPTIONAL }
243 ldap_parse_sasl_bind_result(
246 struct berval **servercredp,
250 struct berval* scred;
255 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
257 assert( ld != NULL );
258 assert( LDAP_VALID( ld ) );
259 assert( res != NULL );
261 if( servercredp != NULL ) {
262 if( ld->ld_version < LDAP_VERSION2 ) {
263 return LDAP_NOT_SUPPORTED;
268 if( res->lm_msgtype != LDAP_RES_BIND ) {
269 ld->ld_errno = LDAP_PARAM_ERROR;
275 if ( ld->ld_error ) {
276 LDAP_FREE( ld->ld_error );
279 if ( ld->ld_matched ) {
280 LDAP_FREE( ld->ld_matched );
281 ld->ld_matched = NULL;
286 ber = ber_dup( res->lm_ber );
289 ld->ld_errno = LDAP_NO_MEMORY;
293 if ( ld->ld_version < LDAP_VERSION2 ) {
294 tag = ber_scanf( ber, "{iA}",
295 &errcode, &ld->ld_error );
297 if( tag == LBER_ERROR ) {
299 ld->ld_errno = LDAP_DECODING_ERROR;
306 tag = ber_scanf( ber, "{eAA" /*}*/,
307 &errcode, &ld->ld_matched, &ld->ld_error );
309 if( tag == LBER_ERROR ) {
311 ld->ld_errno = LDAP_DECODING_ERROR;
315 tag = ber_peek_tag(ber, &len);
317 if( tag == LDAP_TAG_REFERRAL ) {
319 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
321 ld->ld_errno = LDAP_DECODING_ERROR;
325 tag = ber_peek_tag(ber, &len);
328 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
329 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
331 ld->ld_errno = LDAP_DECODING_ERROR;
339 if ( servercredp != NULL ) {
340 *servercredp = scred;
342 } else if ( scred != NULL ) {
346 ld->ld_errno = errcode;
352 return( ld->ld_errno );
356 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
358 /* we need to query the server for supported mechs anyway */
359 LDAPMessage *res, *e;
360 char *attrs[] = { "supportedSASLMechanisms", NULL };
361 char **values, *mechlist;
364 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
366 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
367 NULL, attrs, 0, &res );
369 if ( rc != LDAP_SUCCESS ) {
373 e = ldap_first_entry( ld, res );
376 if ( ld->ld_errno == LDAP_SUCCESS ) {
377 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
382 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
383 if ( values == NULL ) {
385 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
389 mechlist = ldap_charray2str( values, " " );
390 if ( mechlist == NULL ) {
391 LDAP_VFREE( values );
393 ld->ld_errno = LDAP_NO_MEMORY;
397 LDAP_VFREE( values );
400 *pmechlist = mechlist;
406 * ldap_sasl_interactive_bind_s - interactive SASL authentication
408 * This routine uses interactive callbacks.
410 * LDAP_SUCCESS is returned upon success, the ldap error code
414 ldap_sasl_interactive_bind_s(
416 LDAP_CONST char *dn, /* usually NULL */
417 LDAP_CONST char *mechs,
418 LDAPControl **serverControls,
419 LDAPControl **clientControls,
421 LDAP_SASL_INTERACT_PROC *interact,
427 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
428 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
430 #ifdef LDAP_CONNECTIONLESS
431 if( LDAP_IS_UDP(ld) ) {
432 /* Just force it to simple bind, silly to make the user
433 * ask all the time. No, we don't ever actually bind, but I'll
434 * let the final bind handler take care of saving the cdn.
436 rc = ldap_simple_bind( ld, dn, NULL );
437 rc = rc < 0 ? rc : 0;
442 #ifdef HAVE_CYRUS_SASL
443 if( mechs == NULL || *mechs == '\0' ) {
444 mechs = ld->ld_options.ldo_def_sasl_mech;
448 if( mechs == NULL || *mechs == '\0' ) {
449 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
450 if( rc != LDAP_SUCCESS ) {
454 Debug( LDAP_DEBUG_TRACE,
455 "ldap_sasl_interactive_bind_s: server supports: %s\n",
461 Debug( LDAP_DEBUG_TRACE,
462 "ldap_sasl_interactive_bind_s: user selected: %s\n",
466 rc = ldap_int_sasl_bind( ld, dn, mechs,
467 serverControls, clientControls,
468 flags, interact, defaults );
471 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
472 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );
474 if ( smechs ) LDAP_FREE( smechs );