2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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,
78 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_sasl_bind\n", 0, 0, 0 );
80 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
84 assert( LDAP_VALID( ld ) );
85 assert( msgidp != NULL );
87 /* check client controls */
88 rc = ldap_int_client_controls( ld, cctrls );
89 if( rc != LDAP_SUCCESS ) return rc;
91 if( mechanism == LDAP_SASL_SIMPLE ) {
92 if( dn == NULL && cred != NULL && cred->bv_len ) {
93 /* use default binddn */
94 dn = ld->ld_defbinddn;
97 } else if( ld->ld_version < LDAP_VERSION3 ) {
98 ld->ld_errno = LDAP_NOT_SUPPORTED;
106 /* create a message to send */
107 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
108 ld->ld_errno = LDAP_NO_MEMORY;
112 assert( LBER_VALID( ber ) );
114 LDAP_NEXT_MSGID( ld, id );
115 if( mechanism == LDAP_SASL_SIMPLE ) {
117 rc = ber_printf( ber, "{it{istON}" /*}*/,
119 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
122 } else if ( cred == NULL || cred->bv_val == NULL ) {
123 /* SASL bind w/o creditials */
124 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
126 ld->ld_version, dn, LDAP_AUTH_SASL,
130 /* SASL bind w/ creditials */
131 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
133 ld->ld_version, dn, LDAP_AUTH_SASL,
138 ld->ld_errno = LDAP_ENCODING_ERROR;
143 /* Put Server Controls */
144 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
149 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
150 ld->ld_errno = LDAP_ENCODING_ERROR;
156 /* send the message */
157 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id );
170 LDAP_CONST char *mechanism,
172 LDAPControl **sctrls,
173 LDAPControl **cctrls,
174 struct berval **servercredp )
178 struct berval *scredp = NULL;
181 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_sasl_bind_s\n", 0, 0, 0 );
183 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
186 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
187 if( servercredp != NULL ) {
188 if (ld->ld_version < LDAP_VERSION3) {
189 ld->ld_errno = LDAP_NOT_SUPPORTED;
195 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
197 if ( rc != LDAP_SUCCESS ) {
201 #ifdef LDAP_CONNECTIONLESS
202 if (LDAP_IS_UDP(ld)) {
207 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
208 return( ld->ld_errno ); /* ldap_result sets ld_errno */
211 /* parse the results */
213 if( servercredp != NULL ) {
214 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
217 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
218 ldap_msgfree( result );
222 rc = ldap_result2error( ld, result, 1 );
224 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
225 if( servercredp != NULL ) {
226 *servercredp = scredp;
231 if ( scredp != NULL ) {
240 * Parse BindResponse:
242 * BindResponse ::= [APPLICATION 1] SEQUENCE {
243 * COMPONENTS OF LDAPResult,
244 * serverSaslCreds [7] OCTET STRING OPTIONAL }
246 * LDAPResult ::= SEQUENCE {
247 * resultCode ENUMERATED,
249 * errorMessage LDAPString,
250 * referral [3] Referral OPTIONAL }
254 ldap_parse_sasl_bind_result(
257 struct berval **servercredp,
261 struct berval* scred;
267 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
269 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
272 assert( ld != NULL );
273 assert( LDAP_VALID( ld ) );
274 assert( res != NULL );
276 if( servercredp != NULL ) {
277 if( ld->ld_version < LDAP_VERSION2 ) {
278 return LDAP_NOT_SUPPORTED;
283 if( res->lm_msgtype != LDAP_RES_BIND ) {
284 ld->ld_errno = LDAP_PARAM_ERROR;
290 if ( ld->ld_error ) {
291 LDAP_FREE( ld->ld_error );
294 if ( ld->ld_matched ) {
295 LDAP_FREE( ld->ld_matched );
296 ld->ld_matched = NULL;
301 ber = ber_dup( res->lm_ber );
304 ld->ld_errno = LDAP_NO_MEMORY;
308 if ( ld->ld_version < LDAP_VERSION2 ) {
309 tag = ber_scanf( ber, "{ia}",
310 &errcode, &ld->ld_error );
312 if( tag == LBER_ERROR ) {
314 ld->ld_errno = LDAP_DECODING_ERROR;
321 tag = ber_scanf( ber, "{iaa" /*}*/,
322 &errcode, &ld->ld_matched, &ld->ld_error );
324 if( tag == LBER_ERROR ) {
326 ld->ld_errno = LDAP_DECODING_ERROR;
330 tag = ber_peek_tag(ber, &len);
332 if( tag == LDAP_TAG_REFERRAL ) {
334 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
336 ld->ld_errno = LDAP_DECODING_ERROR;
340 tag = ber_peek_tag(ber, &len);
343 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
344 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
346 ld->ld_errno = LDAP_DECODING_ERROR;
354 if ( servercredp != NULL ) {
355 *servercredp = scred;
357 } else if ( scred != NULL ) {
361 ld->ld_errno = errcode;
367 return( ld->ld_errno );
371 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
373 /* we need to query the server for supported mechs anyway */
374 LDAPMessage *res, *e;
375 char *attrs[] = { "supportedSASLMechanisms", NULL };
376 char **values, *mechlist;
380 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
382 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
385 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
386 NULL, attrs, 0, &res );
388 if ( rc != LDAP_SUCCESS ) {
392 e = ldap_first_entry( ld, res );
395 if ( ld->ld_errno == LDAP_SUCCESS ) {
396 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
401 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
402 if ( values == NULL ) {
404 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
408 mechlist = ldap_charray2str( values, " " );
409 if ( mechlist == NULL ) {
410 LDAP_VFREE( values );
412 ld->ld_errno = LDAP_NO_MEMORY;
416 LDAP_VFREE( values );
419 *pmechlist = mechlist;
425 * ldap_sasl_interactive_bind_s - interactive SASL authentication
427 * This routine uses interactive callbacks.
429 * LDAP_SUCCESS is returned upon success, the ldap error code
433 ldap_sasl_interactive_bind_s(
435 LDAP_CONST char *dn, /* usually NULL */
436 LDAP_CONST char *mechs,
437 LDAPControl **serverControls,
438 LDAPControl **clientControls,
440 LDAP_SASL_INTERACT_PROC *interact,
446 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
447 ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
449 #ifdef LDAP_CONNECTIONLESS
450 if( LDAP_IS_UDP(ld) ) {
451 /* Just force it to simple bind, silly to make the user
452 * ask all the time. No, we don't ever actually bind, but I'll
453 * let the final bind handler take care of saving the cdn.
455 rc = ldap_simple_bind( ld, dn, NULL );
456 rc = rc < 0 ? rc : 0;
461 #ifdef HAVE_CYRUS_SASL
462 if( mechs == NULL || *mechs == '\0' ) {
463 mechs = ld->ld_options.ldo_def_sasl_mech;
467 if( mechs == NULL || *mechs == '\0' ) {
468 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
469 if( rc != LDAP_SUCCESS ) {
474 LDAP_LOG ( TRANSPORT, DETAIL1,
475 "ldap_sasl_interactive_bind_s: server supports: %s\n",
478 Debug( LDAP_DEBUG_TRACE,
479 "ldap_sasl_interactive_bind_s: server supports: %s\n",
487 LDAP_LOG ( TRANSPORT, DETAIL1,
488 "ldap_sasl_interactive_bind_s: user selected: %s\n",
491 Debug( LDAP_DEBUG_TRACE,
492 "ldap_sasl_interactive_bind_s: user selected: %s\n",
497 rc = ldap_int_sasl_bind( ld, dn, mechs,
498 serverControls, clientControls,
499 flags, interact, defaults );
502 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
503 ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );
505 if ( smechs ) LDAP_FREE( smechs );