2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>.
15 /* Portions Copyright (C) The Internet Society (1997)
16 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
20 * BindRequest ::= SEQUENCE {
22 * name DistinguishedName, -- who
23 * authentication CHOICE {
24 * simple [0] OCTET STRING -- passwd
25 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
26 * krbv42ldap [1] OCTET STRING
27 * krbv42dsa [2] OCTET STRING
29 * sasl [3] SaslCredentials -- LDAPv3
33 * BindResponse ::= SEQUENCE {
34 * COMPONENTS OF LDAPResult,
35 * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
44 #include <ac/socket.h>
45 #include <ac/stdlib.h>
46 #include <ac/string.h>
53 * ldap_sasl_bind - bind to the ldap server (and X.500).
54 * The dn (usually NULL), mechanism, and credentials are provided.
55 * The message id of the request initiated is provided upon successful
56 * (LDAP_SUCCESS) return.
59 * ldap_sasl_bind( ld, NULL, "mechanism",
60 * cred, NULL, NULL, &msgid )
67 LDAP_CONST char *mechanism,
77 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
80 assert( LDAP_VALID( ld ) );
81 assert( msgidp != NULL );
83 /* check client controls */
84 rc = ldap_int_client_controls( ld, cctrls );
85 if( rc != LDAP_SUCCESS ) return rc;
87 if( mechanism == LDAP_SASL_SIMPLE ) {
88 if( dn == NULL && cred != NULL && cred->bv_len ) {
89 /* use default binddn */
90 dn = ld->ld_defbinddn;
93 } else if( ld->ld_version < LDAP_VERSION3 ) {
94 ld->ld_errno = LDAP_NOT_SUPPORTED;
102 /* create a message to send */
103 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
104 ld->ld_errno = LDAP_NO_MEMORY;
108 assert( LBER_VALID( ber ) );
110 LDAP_NEXT_MSGID( ld, id );
111 if( mechanism == LDAP_SASL_SIMPLE ) {
113 rc = ber_printf( ber, "{it{istON}" /*}*/,
115 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
118 } else if ( cred == NULL || cred->bv_val == NULL ) {
119 /* SASL bind w/o credentials */
120 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
122 ld->ld_version, dn, LDAP_AUTH_SASL,
126 /* SASL bind w/ credentials */
127 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
129 ld->ld_version, dn, LDAP_AUTH_SASL,
134 ld->ld_errno = LDAP_ENCODING_ERROR;
139 /* Put Server Controls */
140 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
145 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
146 ld->ld_errno = LDAP_ENCODING_ERROR;
152 /* send the message */
153 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id );
166 LDAP_CONST char *mechanism,
168 LDAPControl **sctrls,
169 LDAPControl **cctrls,
170 struct berval **servercredp )
174 struct berval *scredp = NULL;
176 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
178 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
179 if( servercredp != NULL ) {
180 if (ld->ld_version < LDAP_VERSION3) {
181 ld->ld_errno = LDAP_NOT_SUPPORTED;
187 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
189 if ( rc != LDAP_SUCCESS ) {
193 #ifdef LDAP_CONNECTIONLESS
194 if (LDAP_IS_UDP(ld)) {
199 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
200 return( ld->ld_errno ); /* ldap_result sets ld_errno */
203 /* parse the results */
205 if( servercredp != NULL ) {
206 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
209 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
210 ldap_msgfree( result );
214 rc = ldap_result2error( ld, result, 1 );
216 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
217 if( servercredp != NULL ) {
218 *servercredp = scredp;
223 if ( scredp != NULL ) {
232 * Parse BindResponse:
234 * BindResponse ::= [APPLICATION 1] SEQUENCE {
235 * COMPONENTS OF LDAPResult,
236 * serverSaslCreds [7] OCTET STRING OPTIONAL }
238 * LDAPResult ::= SEQUENCE {
239 * resultCode ENUMERATED,
241 * errorMessage LDAPString,
242 * referral [3] Referral OPTIONAL }
246 ldap_parse_sasl_bind_result(
249 struct berval **servercredp,
253 struct berval* scred;
258 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
260 assert( ld != NULL );
261 assert( LDAP_VALID( ld ) );
262 assert( res != NULL );
264 if( servercredp != NULL ) {
265 if( ld->ld_version < LDAP_VERSION2 ) {
266 return LDAP_NOT_SUPPORTED;
271 if( res->lm_msgtype != LDAP_RES_BIND ) {
272 ld->ld_errno = LDAP_PARAM_ERROR;
278 if ( ld->ld_error ) {
279 LDAP_FREE( ld->ld_error );
282 if ( ld->ld_matched ) {
283 LDAP_FREE( ld->ld_matched );
284 ld->ld_matched = NULL;
289 ber = ber_dup( res->lm_ber );
292 ld->ld_errno = LDAP_NO_MEMORY;
296 if ( ld->ld_version < LDAP_VERSION2 ) {
297 tag = ber_scanf( ber, "{ia}",
298 &errcode, &ld->ld_error );
300 if( tag == LBER_ERROR ) {
302 ld->ld_errno = LDAP_DECODING_ERROR;
309 tag = ber_scanf( ber, "{eaa" /*}*/,
310 &errcode, &ld->ld_matched, &ld->ld_error );
312 if( tag == LBER_ERROR ) {
314 ld->ld_errno = LDAP_DECODING_ERROR;
318 tag = ber_peek_tag(ber, &len);
320 if( tag == LDAP_TAG_REFERRAL ) {
322 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
324 ld->ld_errno = LDAP_DECODING_ERROR;
328 tag = ber_peek_tag(ber, &len);
331 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
332 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
334 ld->ld_errno = LDAP_DECODING_ERROR;
342 if ( servercredp != NULL ) {
343 *servercredp = scred;
345 } else if ( scred != NULL ) {
349 ld->ld_errno = errcode;
355 return( ld->ld_errno );
359 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
361 /* we need to query the server for supported mechs anyway */
362 LDAPMessage *res, *e;
363 char *attrs[] = { "supportedSASLMechanisms", NULL };
364 char **values, *mechlist;
367 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
369 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
370 NULL, attrs, 0, &res );
372 if ( rc != LDAP_SUCCESS ) {
376 e = ldap_first_entry( ld, res );
379 if ( ld->ld_errno == LDAP_SUCCESS ) {
380 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
385 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
386 if ( values == NULL ) {
388 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
392 mechlist = ldap_charray2str( values, " " );
393 if ( mechlist == NULL ) {
394 LDAP_VFREE( values );
396 ld->ld_errno = LDAP_NO_MEMORY;
400 LDAP_VFREE( values );
403 *pmechlist = mechlist;
409 * ldap_sasl_interactive_bind_s - interactive SASL authentication
411 * This routine uses interactive callbacks.
413 * LDAP_SUCCESS is returned upon success, the ldap error code
417 ldap_sasl_interactive_bind_s(
419 LDAP_CONST char *dn, /* usually NULL */
420 LDAP_CONST char *mechs,
421 LDAPControl **serverControls,
422 LDAPControl **clientControls,
424 LDAP_SASL_INTERACT_PROC *interact,
430 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
431 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
433 #ifdef LDAP_CONNECTIONLESS
434 if( LDAP_IS_UDP(ld) ) {
435 /* Just force it to simple bind, silly to make the user
436 * ask all the time. No, we don't ever actually bind, but I'll
437 * let the final bind handler take care of saving the cdn.
439 rc = ldap_simple_bind( ld, dn, NULL );
440 rc = rc < 0 ? rc : 0;
445 #ifdef HAVE_CYRUS_SASL
446 if( mechs == NULL || *mechs == '\0' ) {
447 mechs = ld->ld_options.ldo_def_sasl_mech;
451 if( mechs == NULL || *mechs == '\0' ) {
452 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
453 if( rc != LDAP_SUCCESS ) {
457 Debug( LDAP_DEBUG_TRACE,
458 "ldap_sasl_interactive_bind_s: server supports: %s\n",
464 Debug( LDAP_DEBUG_TRACE,
465 "ldap_sasl_interactive_bind_s: user selected: %s\n",
469 rc = ldap_int_sasl_bind( ld, dn, mechs,
470 serverControls, clientControls,
471 flags, interact, defaults );
474 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
475 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );
477 if ( smechs ) LDAP_FREE( smechs );