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>
42 * ldap_sasl_bind - bind to the ldap server (and X.500).
43 * The dn (usually NULL), mechanism, and credentials are provided.
44 * The message id of the request initiated is provided upon successful
45 * (LDAP_SUCCESS) return.
48 * ldap_sasl_bind( ld, NULL, "mechanism",
49 * cred, NULL, NULL, &msgid )
56 LDAP_CONST char *mechanism,
65 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
68 assert( LDAP_VALID( ld ) );
69 assert( msgidp != NULL );
71 if( msgidp == NULL ) {
72 ld->ld_errno = LDAP_PARAM_ERROR;
76 if( mechanism == LDAP_SASL_SIMPLE ) {
77 if( dn == NULL && cred != NULL ) {
78 /* use default binddn */
79 dn = ld->ld_defbinddn;
82 } else if( ld->ld_version < LDAP_VERSION3 ) {
83 ld->ld_errno = LDAP_NOT_SUPPORTED;
91 /* create a message to send */
92 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
93 ld->ld_errno = LDAP_NO_MEMORY;
97 assert( BER_VALID( ber ) );
99 if( mechanism == LDAP_SASL_SIMPLE ) {
101 rc = ber_printf( ber, "{it{istON}" /*}*/,
102 ++ld->ld_msgid, LDAP_REQ_BIND,
103 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
106 } else if ( cred == NULL ) {
107 /* SASL bind w/o creditials */
108 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
109 ++ld->ld_msgid, LDAP_REQ_BIND,
110 ld->ld_version, dn, LDAP_AUTH_SASL,
114 /* SASL bind w/ creditials */
115 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
116 ++ld->ld_msgid, LDAP_REQ_BIND,
117 ld->ld_version, dn, LDAP_AUTH_SASL,
122 ld->ld_errno = LDAP_ENCODING_ERROR;
127 /* Put Server Controls */
128 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
133 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
134 ld->ld_errno = LDAP_ENCODING_ERROR;
140 if ( ld->ld_cache != NULL ) {
141 ldap_flush_cache( ld );
143 #endif /* !LDAP_NOCACHE */
145 /* send the message */
146 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
159 LDAP_CONST char *mechanism,
161 LDAPControl **sctrls,
162 LDAPControl **cctrls,
163 struct berval **servercredp )
167 struct berval *scredp = NULL;
169 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
171 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
172 if( servercredp != NULL ) {
173 if (ld->ld_version < LDAP_VERSION3) {
174 ld->ld_errno = LDAP_NOT_SUPPORTED;
180 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
182 if ( rc != LDAP_SUCCESS ) {
186 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
187 return( ld->ld_errno ); /* ldap_result sets ld_errno */
190 /* parse the results */
192 if( servercredp != NULL ) {
193 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
196 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
197 ldap_msgfree( result );
201 rc = ldap_result2error( ld, result, 1 );
203 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
204 if( servercredp != NULL ) {
205 *servercredp = scredp;
210 if ( scredp != NULL ) {
219 * Parse BindResponse:
221 * BindResponse ::= [APPLICATION 1] SEQUENCE {
222 * COMPONENTS OF LDAPResult,
223 * serverSaslCreds [7] OCTET STRING OPTIONAL }
225 * LDAPResult ::= SEQUENCE {
226 * resultCode ENUMERATED,
228 * errorMessage LDAPString,
229 * referral [3] Referral OPTIONAL }
233 ldap_parse_sasl_bind_result(
236 struct berval **servercredp,
240 struct berval* scred;
245 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
247 assert( ld != NULL );
248 assert( LDAP_VALID( ld ) );
249 assert( res != NULL );
251 if ( ld == NULL || res == NULL ) {
252 return LDAP_PARAM_ERROR;
255 if( servercredp != NULL ) {
256 if( ld->ld_version < LDAP_VERSION2 ) {
257 return LDAP_NOT_SUPPORTED;
262 if( res->lm_msgtype != LDAP_RES_BIND ) {
263 ld->ld_errno = LDAP_PARAM_ERROR;
269 if ( ld->ld_error ) {
270 LDAP_FREE( ld->ld_error );
273 if ( ld->ld_matched ) {
274 LDAP_FREE( ld->ld_matched );
275 ld->ld_matched = NULL;
280 ber = ber_dup( res->lm_ber );
283 ld->ld_errno = LDAP_NO_MEMORY;
287 if ( ld->ld_version < LDAP_VERSION2 ) {
288 tag = ber_scanf( ber, "{ia}",
289 &errcode, &ld->ld_error );
291 if( tag == LBER_ERROR ) {
293 ld->ld_errno = LDAP_DECODING_ERROR;
300 tag = ber_scanf( ber, "{iaa" /*}*/,
301 &errcode, &ld->ld_matched, &ld->ld_error );
303 if( tag == LBER_ERROR ) {
305 ld->ld_errno = LDAP_DECODING_ERROR;
309 tag = ber_peek_tag(ber, &len);
311 if( tag == LDAP_TAG_REFERRAL ) {
313 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
315 ld->ld_errno = LDAP_DECODING_ERROR;
319 tag = ber_peek_tag(ber, &len);
322 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
323 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
325 ld->ld_errno = LDAP_DECODING_ERROR;
333 if ( servercredp != NULL ) {
334 *servercredp = scred;
336 } else if ( scred != NULL ) {
340 ld->ld_errno = errcode;
346 return( ld->ld_errno );
350 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
352 /* we need to query the server for supported mechs anyway */
353 LDAPMessage *res, *e;
354 char *attrs[] = { "supportedSASLMechanisms", NULL };
355 char **values, *mechlist;
358 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
360 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
361 NULL, attrs, 0, &res );
363 if ( rc != LDAP_SUCCESS ) {
367 e = ldap_first_entry( ld, res );
369 if ( ld->ld_errno == LDAP_SUCCESS ) {
370 ld->ld_errno = LDAP_UNAVAILABLE;
375 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
376 if ( values == NULL ) {
377 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
382 mechlist = ldap_charray2str( values, " " );
383 if ( mechlist == NULL ) {
384 ld->ld_errno = LDAP_NO_MEMORY;
385 LDAP_VFREE( values );
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( mechs == NULL || *mechs == '\0' ) {
422 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
424 if( rc != LDAP_SUCCESS ) {
428 Debug( LDAP_DEBUG_TRACE,
429 "ldap_interactive_sasl_bind_s: server supports: %s\n",
435 Debug( LDAP_DEBUG_TRACE,
436 "ldap_interactive_sasl_bind_s: user selected: %s\n",
440 rc = ldap_int_sasl_bind( ld, dn, mechs,
441 serverControls, clientControls,
442 flags, interact, defaults );