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;
38 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
41 * Parse the bind request. It looks like this:
43 * BindRequest ::= SEQUENCE {
44 * version INTEGER, -- version
45 * name DistinguishedName, -- dn
46 * authentication CHOICE {
47 * simple [0] OCTET STRING -- passwd
48 * krbv42ldap [1] OCTET STRING
49 * krbv42dsa [1] OCTET STRING
54 rc = ber_scanf( ber, "{iato}", &version, &cdn, &method, &cred );
56 if ( rc == LBER_ERROR ) {
57 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
58 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
63 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
64 version, cdn, method );
66 ndn = dn_normalize_case( ch_strdup( cdn ) );
68 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
69 conn->c_connid, op->o_opid, ndn, method, 0 );
71 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
78 if ( cred.bv_val != NULL ) {
82 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
83 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
84 "version not supported" );
88 /* accept null binds */
89 if ( ndn == NULL || *ndn == '\0' ) {
96 if ( cred.bv_val != NULL ) {
100 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
102 conn->c_protocol = version;
104 if ( conn->c_cdn != NULL ) {
109 if ( conn->c_dn != NULL ) {
114 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
116 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
121 * We could be serving multiple database backends. Select the
122 * appropriate one, or send a referral to our "referral server"
123 * if we don't hold it.
126 if ( (be = select_backend( ndn )) == NULL ) {
129 if ( cred.bv_val != NULL ) {
132 if ( cred.bv_len == 0 ) {
133 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
135 conn->c_protocol = version;
137 if ( conn->c_cdn != NULL ) {
142 if ( conn->c_dn != NULL ) {
147 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
149 send_ldap_result( conn, op, LDAP_SUCCESS,
151 } else if ( default_referral && *default_referral ) {
152 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS,
153 NULL, default_referral );
155 send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
156 NULL, default_referral );
165 ndn = suffixAlias( ndn, op, be );
167 if ( (*be->be_bind)( be, conn, op, ndn, method, NULL, &cred, &edn ) == 0 ) {
168 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
170 conn->c_protocol = version;
172 if ( conn->c_cdn != NULL ) {
179 if ( conn->c_dn != NULL ) {
190 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
191 conn->c_cdn, conn->c_dn, method );
193 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
195 /* send this here to avoid a race condition */
196 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
198 } else if (edn != NULL) {
203 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
204 "Function not implemented" );
213 if ( cred.bv_val != NULL ) {