1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
4 * Copyright (c) 1995 Regents of the University of Michigan.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that this notice is preserved and that due credit is given
9 * to the University of Michigan at Ann Arbor. The name of the University
10 * may not be used to endorse or promote products derived from this
11 * software without specific prior written permission. This software
12 * is provided ``as is'' without express or implied warranty.
19 #include <ac/string.h>
20 #include <ac/socket.h>
24 char *supportedSASLMechanisms[] = {
36 BerElement *ber = op->o_ber;
42 int rc = LDAP_SUCCESS;
46 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
53 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
55 /* Force to connection to "anonymous" until bind succeeds.
56 * This may need to be relocated or done on a case by case basis
57 * to handle certain SASL mechanisms.
60 if ( conn->c_cdn != NULL ) {
65 if ( conn->c_dn != NULL ) {
70 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
72 if ( op->o_ndn != NULL ) {
77 if ( op->o_dn != NULL ) {
83 * Parse the bind request. It looks like this:
85 * BindRequest ::= SEQUENCE {
86 * version INTEGER, -- version
87 * name DistinguishedName, -- dn
88 * authentication CHOICE {
89 * simple [0] OCTET STRING -- passwd
90 * krbv42ldap [1] OCTET STRING
91 * krbv42dsa [2] OCTET STRING
92 * SASL [3] SaslCredentials
96 * SaslCredentials ::= SEQUENCE {
97 * mechanism LDAPString,
98 * credentials OCTET STRING OPTIONAL
102 tag = ber_scanf( ber, "{iat" /*}*/, &version, &cdn, &method );
104 if ( tag == LBER_ERROR ) {
105 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
106 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
111 if( method != LDAP_AUTH_SASL ) {
112 tag = ber_scanf( ber, /*{*/ "o}", &cred );
115 tag = ber_scanf( ber, "{a" /*}*/, &mech );
117 if ( tag != LBER_ERROR ) {
119 tag = ber_peek_tag( ber, &len );
121 if ( tag == LDAP_TAG_LDAPCRED ) {
122 tag = ber_scanf( ber, "o", &cred );
125 if ( tag != LBER_ERROR ) {
126 tag = ber_scanf( ber, /*{{*/ "}}" );
131 if ( tag == LBER_ERROR ) {
132 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
138 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
139 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
144 if( method == LDAP_AUTH_SASL ) {
145 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
148 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
149 version, cdn, method );
152 ndn = dn_normalize_case( ch_strdup( cdn ) );
154 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
155 conn->c_connid, op->o_opid, ndn, method, 0 );
157 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
158 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
159 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
160 "version not supported" );
164 if ( method == LDAP_AUTH_SASL ) {
165 if ( version < LDAP_VERSION3 ) {
166 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%d\n",
168 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
169 "sasl bind requires LDAPv3" );
173 if( mech == NULL || *mech == '\0' ) {
174 Debug( LDAP_DEBUG_ANY,
175 "do_bind: no sasl mechanism provided\n",
177 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
178 NULL, "no sasl mechanism provided" );
182 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
183 Debug( LDAP_DEBUG_ANY,
184 "do_bind: sasl mechanism \"%s\" not supported.\n",
186 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
187 NULL, "sasl mechanism not supported" );
191 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
193 if ( conn->c_authmech != NULL ) {
194 assert( conn->c_bind_in_progress );
196 if((strcmp(conn->c_authmech, mech) != 0)) {
197 /* mechanism changed, cancel in progress bind */
198 conn->c_bind_in_progress = 0;
199 if( conn->c_authstate != NULL ) {
200 free(conn->c_authstate);
201 conn->c_authstate = NULL;
203 free(conn->c_authmech);
204 conn->c_authmech = NULL;
209 assert( !conn->c_bind_in_progress );
210 assert( conn->c_authmech == NULL );
211 assert( conn->c_authstate == NULL );
216 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
218 if ( conn->c_authmech != NULL ) {
219 assert( conn->c_bind_in_progress );
221 /* cancel in progress bind */
222 conn->c_bind_in_progress = 0;
224 if( conn->c_authstate != NULL ) {
225 free(conn->c_authstate);
226 conn->c_authstate = NULL;
228 free(conn->c_authmech);
229 conn->c_authmech = NULL;
233 conn->c_protocol = version;
234 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
236 /* accept null binds */
237 if ( ndn == NULL || *ndn == '\0' ) {
239 * we already forced connection to "anonymous", we just
240 * need to send success
242 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
247 * We could be serving multiple database backends. Select the
248 * appropriate one, or send a referral to our "referral server"
249 * if we don't hold it.
252 if ( (be = select_backend( ndn )) == NULL ) {
253 if ( cred.bv_len == 0 ) {
254 send_ldap_result( conn, op, LDAP_SUCCESS,
257 } else if ( default_referral && *default_referral ) {
258 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS,
259 NULL, default_referral );
262 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
263 NULL, default_referral );
273 ndn = suffixAlias( ndn, op, be );
275 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
276 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
288 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
289 conn->c_cdn, conn->c_dn, method );
291 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
293 /* send this here to avoid a race condition */
294 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
296 } else if (edn != NULL) {
301 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
302 "Function not implemented" );
312 if ( mech != NULL ) {
315 if ( cred.bv_val != NULL ) {