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 );
47 * Parse the bind request. It looks like this:
49 * BindRequest ::= SEQUENCE {
50 * version INTEGER, -- version
51 * name DistinguishedName, -- dn
52 * authentication CHOICE {
53 * simple [0] OCTET STRING -- passwd
54 * krbv42ldap [1] OCTET STRING
55 * krbv42dsa [1] OCTET STRING
60 rc = ber_scanf( ber, "{iat" /*}*/, &version, &cdn, &method );
62 if ( rc == LBER_ERROR ) {
63 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
64 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
69 if( method != LDAP_AUTH_SASL ) {
70 rc = ber_scanf( ber, /*{*/ "o}", &cred );
73 rc = ber_scanf( ber, "{a" /*}*/, &mech );
75 if ( rc != LBER_ERROR ) {
77 rc = ber_peek_tag( ber, &len );
79 if ( rc == LDAP_TAG_LDAPCRED ) {
80 rc = ber_scanf( ber, "o", &cred );
83 if ( rc != LBER_ERROR ) {
84 rc = ber_scanf( ber, /*{{*/ "}}" );
89 if ( rc == LBER_ERROR ) {
90 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
96 if( get_ctrls( conn, op, 1 ) == -1 ) {
97 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
102 if( method == LDAP_AUTH_SASL ) {
103 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
106 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
107 version, cdn, method );
110 ndn = dn_normalize_case( ch_strdup( cdn ) );
112 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
113 conn->c_connid, op->o_opid, ndn, method, 0 );
115 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
116 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
117 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
118 "version not supported" );
122 if ( method == LDAP_AUTH_SASL ) {
123 if ( version < LDAP_VERSION3 ) {
124 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%d\n",
126 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
127 "sasl bind requires LDAPv3" );
131 if( mech == NULL || *mech == '\0' ) {
132 Debug( LDAP_DEBUG_ANY,
133 "do_bind: no sasl mechanism provided\n",
135 /* XXYYZ need to check this return code */
136 send_ldap_result( conn, op, LDAP_AUTH_METHOD_NOT_SUPPORTED,
137 NULL, "no sasl mechanism provided." );
142 Debug( LDAP_DEBUG_ANY,
143 "do_bind: sasl mechanism \"%s\" not supported.\n",
145 /* XXYYZ need to check this return code */
146 send_ldap_result( conn, op, LDAP_AUTH_METHOD_NOT_SUPPORTED,
147 NULL, "no sasl mechanism provided." );
152 /* accept null binds */
153 if ( ndn == NULL || *ndn == '\0' ) {
154 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
156 conn->c_protocol = version;
158 if ( conn->c_cdn != NULL ) {
163 if ( conn->c_dn != NULL ) {
168 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
170 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
175 * We could be serving multiple database backends. Select the
176 * appropriate one, or send a referral to our "referral server"
177 * if we don't hold it.
180 if ( (be = select_backend( ndn )) == NULL ) {
181 if ( cred.bv_len == 0 ) {
182 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
184 conn->c_protocol = version;
186 if ( conn->c_cdn != NULL ) {
191 if ( conn->c_dn != NULL ) {
196 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
198 send_ldap_result( conn, op, LDAP_SUCCESS,
200 } else if ( default_referral && *default_referral ) {
201 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS,
202 NULL, default_referral );
204 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
205 NULL, default_referral );
214 ndn = suffixAlias( ndn, op, be );
216 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
217 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
219 conn->c_protocol = version;
221 if ( conn->c_cdn != NULL ) {
228 if ( conn->c_dn != NULL ) {
239 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
240 conn->c_cdn, conn->c_dn, method );
242 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
244 /* send this here to avoid a race condition */
245 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
247 } else if (edn != NULL) {
252 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
253 "Function not implemented" );
263 if ( mech != NULL ) {
266 if ( cred.bv_val != NULL ) {