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;
37 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
40 * Parse the bind request. It looks like this:
42 * BindRequest ::= SEQUENCE {
43 * version INTEGER, -- version
44 * name DistinguishedName, -- dn
45 * authentication CHOICE {
46 * simple [0] OCTET STRING -- passwd
47 * krbv42ldap [1] OCTET STRING
48 * krbv42dsa [1] OCTET STRING
55 * in version 3.0 there is an extra SEQUENCE tag after the
56 * BindRequest SEQUENCE tag.
61 unsigned long tlen, ttag;
64 ttag = ber_skip_tag( &tber, &tlen );
65 if ( ber_peek_tag( &tber, &tlen ) == LBER_SEQUENCE ) {
66 Debug( LDAP_DEBUG_ANY, "version 3.0 detected\n", 0, 0, 0 );
68 rc = ber_scanf(ber, "{{iato}}", &version, &cdn, &method, &cred);
70 rc = ber_scanf( ber, "{iato}", &version, &cdn, &method, &cred );
74 rc = ber_scanf( ber, "{iato}", &version, &cdn, &method, &cred );
77 if ( rc == LBER_ERROR ) {
78 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
79 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
84 if ( conn->c_version == 30 ) {
86 case LDAP_AUTH_SIMPLE_30:
87 method = LDAP_AUTH_SIMPLE;
90 case LDAP_AUTH_KRBV41_30:
91 method = LDAP_AUTH_KRBV41;
93 case LDAP_AUTH_KRBV42_30:
94 method = LDAP_AUTH_KRBV42;
101 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
102 version, cdn, method );
104 ndn = dn_normalize_case( ch_strdup( cdn ) );
106 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
107 conn->c_connid, op->o_opid, ndn, method, 0 );
109 if ( version != LDAP_VERSION2 ) {
116 if ( cred.bv_val != NULL ) {
120 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
121 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
122 "version not supported" );
126 /* accept null binds */
127 if ( ndn == NULL || *ndn == '\0' ) {
134 if ( cred.bv_val != NULL ) {
138 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
140 conn->c_protocol = version;
142 if ( conn->c_cdn != NULL ) {
147 if ( conn->c_dn != NULL ) {
152 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
154 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
159 * We could be serving multiple database backends. Select the
160 * appropriate one, or send a referral to our "referral server"
161 * if we don't hold it.
164 if ( (be = select_backend( ndn )) == NULL ) {
167 if ( cred.bv_val != NULL ) {
170 if ( cred.bv_len == 0 ) {
171 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
173 conn->c_protocol = version;
175 if ( conn->c_cdn != NULL ) {
180 if ( conn->c_dn != NULL ) {
185 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
187 send_ldap_result( conn, op, LDAP_SUCCESS,
189 } else if ( default_referral && *default_referral ) {
190 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS,
191 NULL, default_referral );
193 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
194 NULL, default_referral );
203 ndn = suffixAlias( ndn, op, be );
205 if ( (*be->be_bind)( be, conn, op, ndn, method, &cred, &edn ) == 0 ) {
206 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
208 conn->c_protocol = version;
210 if ( conn->c_cdn != NULL ) {
217 if ( conn->c_dn != NULL ) {
228 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
229 conn->c_cdn, conn->c_dn, method );
231 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
233 /* send this here to avoid a race condition */
234 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
236 } else if (edn != NULL) {
241 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
242 "Function not implemented" );
251 if ( cred.bv_val != NULL ) {