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>
30 BerElement *ber = op->o_ber;
39 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
42 * Parse the bind request. It looks like this:
44 * BindRequest ::= SEQUENCE {
45 * version INTEGER, -- version
46 * name DistinguishedName, -- dn
47 * authentication CHOICE {
48 * simple [0] OCTET STRING -- passwd
49 * krbv42ldap [1] OCTET STRING
50 * krbv42dsa [1] OCTET STRING
55 rc = ber_scanf( ber, "{iat" /*}*/, &version, &cdn, &method );
57 if ( rc == LBER_ERROR ) {
58 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
59 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
67 if( method != LDAP_AUTH_SASL ) {
68 rc = ber_scanf( ber, /*{*/ "o}", &cred );
71 rc = ber_scanf( ber, "{a" /*}*/, &mech );
73 if ( rc != LBER_ERROR ) {
75 rc = ber_peek_tag( ber, &len );
77 if ( rc == LDAP_TAG_LDAPCRED ) {
78 rc = ber_scanf( ber, "o", &cred );
81 if ( rc != LBER_ERROR ) {
82 rc = ber_scanf( ber, /*{{*/ "}}" );
87 if ( rc == LBER_ERROR ) {
94 if ( cred.bv_val != NULL ) {
98 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
99 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
106 if( get_ctrls( conn, op, 1 ) == -1 ) {
110 if ( mech != NULL ) {
113 if ( cred.bv_val != NULL ) {
116 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
121 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
122 version, cdn, method );
124 ndn = dn_normalize_case( ch_strdup( cdn ) );
126 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
127 conn->c_connid, op->o_opid, ndn, method, 0 );
129 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
136 if ( mech != NULL ) {
139 if ( cred.bv_val != NULL ) {
143 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
144 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
145 "version not supported" );
149 /* accept null binds */
150 if ( ndn == NULL || *ndn == '\0' ) {
157 if ( mech != NULL ) {
160 if ( cred.bv_val != NULL ) {
164 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
166 conn->c_protocol = version;
168 if ( conn->c_cdn != NULL ) {
173 if ( conn->c_dn != NULL ) {
178 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
180 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
185 * We could be serving multiple database backends. Select the
186 * appropriate one, or send a referral to our "referral server"
187 * if we don't hold it.
190 if ( (be = select_backend( ndn )) == NULL ) {
193 if ( cred.bv_val != NULL ) {
196 if ( cred.bv_len == 0 ) {
197 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
199 conn->c_protocol = version;
201 if ( conn->c_cdn != NULL ) {
206 if ( conn->c_dn != NULL ) {
211 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
213 send_ldap_result( conn, op, LDAP_SUCCESS,
215 } else if ( default_referral && *default_referral ) {
216 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS,
217 NULL, default_referral );
219 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
220 NULL, default_referral );
229 ndn = suffixAlias( ndn, op, be );
231 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
232 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
234 conn->c_protocol = version;
236 if ( conn->c_cdn != NULL ) {
243 if ( conn->c_dn != NULL ) {
254 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
255 conn->c_cdn, conn->c_dn, method );
257 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
259 /* send this here to avoid a race condition */
260 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
262 } else if (edn != NULL) {
267 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
268 "Function not implemented" );
277 if ( mech != NULL ) {
280 if ( cred.bv_val != NULL ) {