]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Assert ID != NOID when fetching from the datastore.
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  * Copyright (c) 1995 Regents of the University of Michigan.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms are permitted
12  * provided that this notice is preserved and that due credit is given
13  * to the University of Michigan at Ann Arbor. The name of the University
14  * may not be used to endorse or promote products derived from this
15  * software without specific prior written permission. This software
16  * is provided ``as is'' without express or implied warranty.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/string.h>
24 #include <ac/socket.h>
25
26 #include "slap.h"
27
28 int
29 do_bind(
30     Connection  *conn,
31     Operation   *op
32 )
33 {
34         BerElement      *ber = op->o_ber;
35         ber_int_t               version;
36         ber_tag_t method;
37         char            *mech;
38         char            *dn;
39         char *ndn;
40         ber_tag_t       tag;
41         int                     rc = LDAP_SUCCESS;
42         struct berval   cred;
43         Backend         *be;
44
45         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
46
47         dn = NULL;
48         ndn = NULL;
49         mech = NULL;
50         cred.bv_val = NULL;
51
52         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
53
54         /* Force to connection to "anonymous" until bind succeeds.
55          * This may need to be relocated or done on a case by case basis
56          * to handle certain SASL mechanisms.
57          */
58
59         if ( conn->c_cdn != NULL ) {
60                 free( conn->c_cdn );
61                 conn->c_cdn = NULL;
62         }
63
64         if ( conn->c_dn != NULL ) {
65                 free( conn->c_dn );
66                 conn->c_dn = NULL;
67         }
68
69         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
70
71         if ( op->o_dn != NULL ) {
72                 free( op->o_dn );
73                 op->o_dn = ch_strdup( "" );
74         }
75
76         if ( op->o_ndn != NULL ) {
77                 free( op->o_ndn );
78                 op->o_ndn = ch_strdup( "" );
79         }
80
81         /*
82          * Parse the bind request.  It looks like this:
83          *
84          *      BindRequest ::= SEQUENCE {
85          *              version         INTEGER,                 -- version
86          *              name            DistinguishedName,       -- dn
87          *              authentication  CHOICE {
88          *                      simple          [0] OCTET STRING -- passwd
89          *                      krbv42ldap      [1] OCTET STRING
90          *                      krbv42dsa       [2] OCTET STRING
91          *                      SASL            [3] SaslCredentials
92          *              }
93          *      }
94          *
95          *      SaslCredentials ::= SEQUENCE {
96      *          mechanism           LDAPString,
97      *          credentials         OCTET STRING OPTIONAL
98          *      }
99          */
100
101         tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
102
103         if ( tag == LBER_ERROR ) {
104                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
105                 send_ldap_disconnect( conn, op,
106                         LDAP_PROTOCOL_ERROR, "decoding error" );
107                 rc = -1;
108                 goto cleanup;
109         }
110
111         ndn = ch_strdup( dn );
112
113         if ( dn_normalize_case( ndn ) == NULL ) {
114                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
115                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
116                     "invalid DN", NULL, NULL );
117                 goto cleanup;
118         }
119
120         op->o_protocol = version;
121
122         if( method != LDAP_AUTH_SASL ) {
123                 tag = ber_scanf( ber, /*{*/ "o}", &cred );
124
125         } else {
126                 tag = ber_scanf( ber, "{a" /*}*/, &mech );
127
128                 if ( tag != LBER_ERROR ) {
129                         ber_len_t len;
130                         tag = ber_peek_tag( ber, &len );
131
132                         if ( tag == LDAP_TAG_LDAPCRED ) { 
133                                 tag = ber_scanf( ber, "o", &cred );
134                         }
135
136                         if ( tag != LBER_ERROR ) {
137                                 tag = ber_scanf( ber, /*{{*/ "}}" );
138                         }
139                 }
140         }
141
142         if ( tag == LBER_ERROR ) {
143                 send_ldap_disconnect( conn, op,
144                         LDAP_PROTOCOL_ERROR,
145                 "decoding error" );
146                 rc = -1;
147                 goto cleanup;
148         }
149
150         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
151                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
152                 goto cleanup;
153         } 
154
155         if( method == LDAP_AUTH_SASL ) {
156                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
157                         dn, mech, NULL );
158         } else {
159                 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
160                         (unsigned long) version, dn, (unsigned long) method );
161         }
162
163         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
164             op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
165
166         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
167                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
168                         (unsigned long) version, 0, 0 );
169                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
170                         NULL, "version not supported", NULL, NULL );
171                 goto cleanup;
172         }
173
174         if ( method == LDAP_AUTH_SASL ) {
175                 if ( version < LDAP_VERSION3 ) {
176                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
177                                 (unsigned long) version, 0, 0 );
178                         send_ldap_disconnect( conn, op,
179                                 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
180                         rc = -1;
181                         goto cleanup;
182                 }
183
184                 if( mech == NULL || *mech == '\0' ) {
185                         Debug( LDAP_DEBUG_ANY,
186                                 "do_bind: no sasl mechanism provided\n",
187                                 0, 0, 0 );
188                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
189                                 NULL, "no sasl mechanism provided", NULL, NULL );
190                         goto cleanup;
191                 }
192
193                 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
194                         Debug( LDAP_DEBUG_ANY,
195                                 "do_bind: sasl mechanism=\"%s\" not supported.\n",
196                                 mech, 0, 0 );
197                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
198                                 NULL, "sasl mechanism not supported", NULL, NULL );
199                         goto cleanup;
200                 }
201
202                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
203
204                 if ( conn->c_authmech != NULL ) {
205                         assert( conn->c_bind_in_progress );
206
207                         if((strcmp(conn->c_authmech, mech) != 0)) {
208                                 /* mechanism changed, cancel in progress bind */
209                                 conn->c_bind_in_progress = 0;
210                                 if( conn->c_authstate != NULL ) {
211                                         free(conn->c_authstate);
212                                         conn->c_authstate = NULL;
213                                 }
214                                 free(conn->c_authmech);
215                                 conn->c_authmech = NULL;
216                         }
217
218 #ifdef LDAP_DEBUG
219                 } else {
220                         assert( !conn->c_bind_in_progress );
221                         assert( conn->c_authmech == NULL );
222                         assert( conn->c_authstate == NULL );
223 #endif
224                 }
225
226         } else {
227                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
228
229                 if ( conn->c_authmech != NULL ) {
230                         assert( conn->c_bind_in_progress );
231
232                         /* cancel in progress bind */
233                         conn->c_bind_in_progress = 0;
234
235                         if( conn->c_authstate != NULL ) {
236                                 free(conn->c_authstate);
237                                 conn->c_authstate = NULL;
238                         }
239                         free(conn->c_authmech);
240                         conn->c_authmech = NULL;
241                 }
242         }
243
244         conn->c_protocol = version;
245         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
246
247         /* accept null binds */
248         if ( ndn == NULL || *ndn == '\0' ) {
249                 /*
250                  * we already forced connection to "anonymous", we just
251                  * need to send success
252                  */
253                 send_ldap_result( conn, op, LDAP_SUCCESS,
254                         NULL, NULL, NULL, NULL );
255                 goto cleanup;
256         }
257
258         /*
259          * We could be serving multiple database backends.  Select the
260          * appropriate one, or send a referral to our "referral server"
261          * if we don't hold it.
262          */
263
264         if ( (be = select_backend( ndn )) == NULL ) {
265                 if ( cred.bv_len == 0 ) {
266                         send_ldap_result( conn, op, LDAP_SUCCESS,
267                                 NULL, NULL, NULL, NULL );
268
269                 } else if ( default_referral ) {
270                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
271                                 NULL, NULL, default_referral, NULL );
272
273                 } else {
274                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
275                                 NULL, NULL, NULL, NULL );
276                 }
277
278                 goto cleanup;
279         }
280
281         if ( be->be_bind ) {
282                 /* alias suffix */
283                 char *edn;
284
285                 /* deref suffix alias if appropriate */
286                 ndn = suffix_alias( be, ndn );
287
288                 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
289                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
290
291                         conn->c_cdn = dn;
292                         dn = NULL;
293
294                         if(edn != NULL) {
295                                 conn->c_dn = edn;
296                         } else {
297                                 conn->c_dn = ndn;
298                                 ndn = NULL;
299                         }
300
301                         Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
302                         conn->c_cdn, conn->c_dn, method );
303
304                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
305
306                         /* send this here to avoid a race condition */
307                         send_ldap_result( conn, op, LDAP_SUCCESS,
308                                 NULL, NULL, NULL, NULL );
309
310                 } else if (edn != NULL) {
311                         free( edn );
312                 }
313
314         } else {
315                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
316                         NULL, "Function not implemented", NULL, NULL );
317         }
318
319 cleanup:
320         if( dn != NULL ) {
321                 free( dn );
322         }
323         if( ndn != NULL ) {
324                 free( ndn );
325         }
326         if ( mech != NULL ) {
327                 free( mech );
328         }
329         if ( cred.bv_val != NULL ) {
330                 free( cred.bv_val );
331         }
332
333         return rc;
334 }