3 * Copyright 1998-1999 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
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
32 #include <ac/socket.h>
33 #include <ac/string.h>
40 * ldap_sasl_bind - bind to the ldap server (and X.500). The dn, mechanism, and
41 * credentials of the entry to which to bind are supplied. The message id
42 * of the request initiated is provided upon successful (LDAP_SUCCESS) return.
45 * ldap_sasl_bind( ld, "cn=manager, o=university of michigan, c=us",
46 * "mechanism", "secret", NULL, NULL, &msgid )
53 LDAP_CONST char *mechanism,
62 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
65 assert( LDAP_VALID( ld ) );
66 assert( msgidp != NULL );
68 if( msgidp == NULL ) {
69 ld->ld_errno = LDAP_PARAM_ERROR;
73 if( mechanism == LDAP_SASL_SIMPLE ) {
74 if( dn == NULL && cred != NULL ) {
75 /* use default binddn */
76 dn = ld->ld_defbinddn;
79 } else if( ld->ld_version < LDAP_VERSION3 ) {
80 ld->ld_errno = LDAP_NOT_SUPPORTED;
88 /* create a message to send */
89 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
90 ld->ld_errno = LDAP_NO_MEMORY;
94 assert( BER_VALID( ber ) );
96 if( mechanism == LDAP_SASL_SIMPLE ) {
98 rc = ber_printf( ber, "{it{istO}" /*}*/,
99 ++ld->ld_msgid, LDAP_REQ_BIND,
100 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
103 } else if ( cred == NULL ) {
104 /* SASL bind w/o creditials */
105 rc = ber_printf( ber, "{it{ist{s}}" /*}*/,
106 ++ld->ld_msgid, LDAP_REQ_BIND,
107 ld->ld_version, dn, LDAP_AUTH_SASL,
111 /* SASL bind w/ creditials */
112 rc = ber_printf( ber, "{it{ist{sO}}" /*}*/,
113 ++ld->ld_msgid, LDAP_REQ_BIND,
114 ld->ld_version, dn, LDAP_AUTH_SASL,
119 ld->ld_errno = LDAP_ENCODING_ERROR;
124 /* Put Server Controls */
125 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
130 if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
131 ld->ld_errno = LDAP_ENCODING_ERROR;
137 if ( ld->ld_cache != NULL ) {
138 ldap_flush_cache( ld );
140 #endif /* !LDAP_NOCACHE */
142 /* send the message */
143 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
152 * ldap_sasl_bind_s - bind to the ldap server (and X.500) using simple
153 * authentication. The dn and password of the entry to which to bind are
154 * supplied. LDAP_SUCCESS is returned upon success, the ldap error code
158 * ldap_sasl_bind_s( ld, "cn=manager, o=university of michigan, c=us",
159 * "mechanism", "secret", NULL, NULL, &servercred )
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 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
194 return( ld->ld_errno ); /* ldap_result sets ld_errno */
197 /* parse the results */
199 if( servercredp != NULL ) {
200 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
203 if( rc != LDAP_SUCCESS ) {
204 ldap_msgfree( result );
208 rc = ldap_result2error( ld, result, 1 );
210 if( rc == LDAP_SUCCESS ) {
211 if( servercredp != NULL ) {
212 *servercredp = scredp;
215 } else if (scredp != NULL ) {
224 * Parse BindResponse:
226 * BindResponse ::= [APPLICATION 1] SEQUENCE {
227 * COMPONENTS OF LDAPResult,
228 * serverSaslCreds [7] OCTET STRING OPTIONAL }
230 * LDAPResult ::= SEQUENCE {
231 * resultCode ENUMERATED,
233 * errorMessage LDAPString,
234 * referral [3] Referral OPTIONAL }
238 ldap_parse_sasl_bind_result(
241 struct berval **servercredp,
245 struct berval* scred;
250 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
252 assert( ld != NULL );
253 assert( LDAP_VALID( ld ) );
254 assert( res != NULL );
256 if ( ld == NULL || res == NULL ) {
257 return LDAP_PARAM_ERROR;
260 if( servercredp != NULL ) {
261 if( ld->ld_version < LDAP_VERSION2 ) {
262 return LDAP_NOT_SUPPORTED;
267 if( res->lm_msgtype != LDAP_RES_BIND ) {
268 ld->ld_errno = LDAP_PARAM_ERROR;
274 if ( ld->ld_error ) {
275 LDAP_FREE( ld->ld_error );
278 if ( ld->ld_matched ) {
279 LDAP_FREE( ld->ld_matched );
280 ld->ld_matched = NULL;
285 ber = ber_dup( res->lm_ber );
288 ld->ld_errno = LDAP_NO_MEMORY;
292 if ( ld->ld_version < LDAP_VERSION2 ) {
293 tag = ber_scanf( ber, "{ia}",
294 &errcode, &ld->ld_error );
296 if( tag == LBER_ERROR ) {
298 ld->ld_errno = LDAP_DECODING_ERROR;
305 tag = ber_scanf( ber, "{iaa" /*}*/,
306 &errcode, &ld->ld_matched, &ld->ld_error );
308 if( tag == LBER_ERROR ) {
310 ld->ld_errno = LDAP_DECODING_ERROR;
314 tag = ber_peek_tag(ber, &len);
316 if( tag == LDAP_TAG_REFERRAL ) {
318 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
320 ld->ld_errno = LDAP_DECODING_ERROR;
324 tag = ber_peek_tag(ber, &len);
327 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
328 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
330 ld->ld_errno = LDAP_DECODING_ERROR;
338 if ( servercredp != NULL ) {
339 *servercredp = scred;
341 } else if ( scred != NULL ) {
345 ld->ld_errno = errcode;
351 return( ld->ld_errno );