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 );
73 * Parse the bind request. It looks like this:
75 * BindRequest ::= SEQUENCE {
76 * version INTEGER, -- version
77 * name DistinguishedName, -- dn
78 * authentication CHOICE {
79 * simple [0] OCTET STRING -- passwd
80 * krbv42ldap [1] OCTET STRING
81 * krbv42dsa [2] OCTET STRING
82 * SASL [3] SaslCredentials
86 * SaslCredentials ::= SEQUENCE {
87 * mechanism LDAPString,
88 * credentials OCTET STRING OPTIONAL
92 tag = ber_scanf( ber, "{iat" /*}*/, &version, &cdn, &method );
94 if ( tag == LBER_ERROR ) {
95 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
96 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
101 if( method != LDAP_AUTH_SASL ) {
102 tag = ber_scanf( ber, /*{*/ "o}", &cred );
105 tag = ber_scanf( ber, "{a" /*}*/, &mech );
107 if ( tag != LBER_ERROR ) {
109 tag = ber_peek_tag( ber, &len );
111 if ( tag == LDAP_TAG_LDAPCRED ) {
112 tag = ber_scanf( ber, "o", &cred );
115 if ( tag != LBER_ERROR ) {
116 tag = ber_scanf( ber, /*{{*/ "}}" );
121 if ( tag == LBER_ERROR ) {
122 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
128 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
129 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
134 if( method == LDAP_AUTH_SASL ) {
135 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
138 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
139 version, cdn, method );
142 ndn = dn_normalize_case( ch_strdup( cdn ) );
144 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
145 conn->c_connid, op->o_opid, ndn, method, 0 );
147 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
148 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
149 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
150 "version not supported" );
154 if ( method == LDAP_AUTH_SASL ) {
155 if ( version < LDAP_VERSION3 ) {
156 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%d\n",
158 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
159 "sasl bind requires LDAPv3" );
163 if( mech == NULL || *mech == '\0' ) {
164 Debug( LDAP_DEBUG_ANY,
165 "do_bind: no sasl mechanism provided\n",
167 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
168 NULL, "no sasl mechanism provided" );
172 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
173 Debug( LDAP_DEBUG_ANY,
174 "do_bind: sasl mechanism \"%s\" not supported.\n",
176 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
177 NULL, "sasl mechanism not supported" );
181 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
183 if ( conn->c_authmech != NULL ) {
184 assert( conn->c_bind_in_progress );
186 if((strcmp(conn->c_authmech, mech) != 0)) {
187 /* mechanism changed, cancel in progress bind */
188 conn->c_bind_in_progress = 0;
189 if( conn->c_authstate != NULL ) {
190 free(conn->c_authstate);
191 conn->c_authstate = NULL;
193 free(conn->c_authmech);
194 conn->c_authmech = NULL;
199 assert( !conn->c_bind_in_progress );
200 assert( conn->c_authmech == NULL );
201 assert( conn->c_authstate == NULL );
206 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
208 if ( conn->c_authmech != NULL ) {
209 assert( conn->c_bind_in_progress );
211 /* cancel in progress bind */
212 conn->c_bind_in_progress = 0;
214 if( conn->c_authstate != NULL ) {
215 free(conn->c_authstate);
216 conn->c_authstate = NULL;
218 free(conn->c_authmech);
219 conn->c_authmech = NULL;
223 conn->c_protocol = version;
224 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
226 /* accept null binds */
227 if ( ndn == NULL || *ndn == '\0' ) {
229 * we already forced connection to "anonymous", we just
230 * need to send success
232 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
237 * We could be serving multiple database backends. Select the
238 * appropriate one, or send a referral to our "referral server"
239 * if we don't hold it.
242 if ( (be = select_backend( ndn )) == NULL ) {
243 if ( cred.bv_len == 0 ) {
244 send_ldap_result( conn, op, LDAP_SUCCESS,
247 } else if ( default_referral && *default_referral ) {
248 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS,
249 NULL, default_referral );
252 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
253 NULL, default_referral );
263 ndn = suffixAlias( ndn, op, be );
265 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
266 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
278 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
279 conn->c_cdn, conn->c_dn, method );
281 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
283 /* send this here to avoid a race condition */
284 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
286 } else if (edn != NULL) {
291 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
292 "Function not implemented" );
302 if ( mech != NULL ) {
305 if ( cred.bv_val != NULL ) {