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 && ld->ld_version < LDAP_VERSION3)
76 ld->ld_errno = LDAP_NOT_SUPPORTED;
83 /* create a message to send */
84 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
85 ld->ld_errno = LDAP_NO_MEMORY;
89 assert( BER_VALID( ber ) );
91 if( mechanism == LDAP_SASL_SIMPLE ) {
93 rc = ber_printf( ber, "{it{istO}" /*}*/,
94 ++ld->ld_msgid, LDAP_REQ_BIND,
95 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
98 } else if ( cred == NULL ) {
99 /* SASL bind w/o creditials */
100 rc = ber_printf( ber, "{it{ist{s}}" /*}*/,
101 ++ld->ld_msgid, LDAP_REQ_BIND,
102 ld->ld_version, dn, LDAP_AUTH_SASL,
106 /* SASL bind w/ creditials */
107 rc = ber_printf( ber, "{it{ist{sO}}" /*}*/,
108 ++ld->ld_msgid, LDAP_REQ_BIND,
109 ld->ld_version, dn, LDAP_AUTH_SASL,
114 ld->ld_errno = LDAP_ENCODING_ERROR;
119 /* Put Server Controls */
120 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
125 if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
126 ld->ld_errno = LDAP_ENCODING_ERROR;
132 if ( ld->ld_cache != NULL ) {
133 ldap_flush_cache( ld );
135 #endif /* !LDAP_NOCACHE */
137 /* send the message */
138 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
147 * ldap_sasl_bind_s - bind to the ldap server (and X.500) using simple
148 * authentication. The dn and password of the entry to which to bind are
149 * supplied. LDAP_SUCCESS is returned upon success, the ldap error code
153 * ldap_sasl_bind_s( ld, "cn=manager, o=university of michigan, c=us",
154 * "mechanism", "secret", NULL, NULL, &servercred )
161 LDAP_CONST char *mechanism,
163 LDAPControl **sctrls,
164 LDAPControl **cctrls,
165 struct berval **servercredp )
169 struct berval *scredp = NULL;
171 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
173 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
174 if( servercredp != NULL ) {
175 if (ld->ld_version < LDAP_VERSION3) {
176 ld->ld_errno = LDAP_NOT_SUPPORTED;
182 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
184 if ( rc != LDAP_SUCCESS ) {
188 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
189 return( ld->ld_errno ); /* ldap_result sets ld_errno */
192 /* parse the results */
194 if( servercredp != NULL ) {
195 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
198 if( rc != LDAP_SUCCESS ) {
199 ldap_msgfree( result );
203 rc = ldap_result2error( ld, result, 1 );
205 if( rc == LDAP_SUCCESS ) {
206 if( servercredp != NULL ) {
207 *servercredp = scredp;
210 } else 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;
267 errcode = LDAP_SUCCESS;
270 if ( ld->ld_error ) {
271 LDAP_FREE( ld->ld_error );
274 if ( ld->ld_matched ) {
275 LDAP_FREE( ld->ld_matched );
276 ld->ld_matched = NULL;
281 ber = ber_dup( res->lm_ber );
283 if ( ld->ld_version < LDAP_VERSION2 ) {
284 tag = ber_scanf( ber, "{ia}",
285 &errcode, &ld->ld_error );
290 tag = ber_scanf( ber, "{iaa" /*}*/,
291 &errcode, &ld->ld_matched, &ld->ld_error );
293 if( tag != LBER_ERROR ) {
294 tag = ber_peek_tag(ber, &len);
297 if( tag == LDAP_TAG_REFERRAL ) {
299 tag = ber_scanf( ber, "x" );
301 if( tag != LBER_ERROR ) {
302 tag = ber_peek_tag(ber, &len);
306 /* need to clean out misc items */
307 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
308 tag = ber_scanf( ber, "O", &scred );
312 if ( tag == LBER_ERROR ) {
313 errcode = LDAP_DECODING_ERROR;
321 if ( errcode == LDAP_SUCCESS && servercredp != NULL ) {
322 *servercredp = scred;
324 } else if ( scred != NULL ) {
332 ld->ld_errno = errcode;
333 return( ld->ld_errno );