]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
First-cut at manageDSAit-aware backend selection.
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * Copyright (c) 1995 Regents of the University of Michigan.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms are permitted
13  * provided that this notice is preserved and that due credit is given
14  * to the University of Michigan at Ann Arbor. The name of the University
15  * may not be used to endorse or promote products derived from this
16  * software without specific prior written permission. This software
17  * is provided ``as is'' without express or implied warranty.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/string.h>
25 #include <ac/socket.h>
26
27 #include "ldap_pvt.h"
28 #include "slap.h"
29
30 int
31 do_bind(
32     Connection  *conn,
33     Operation   *op
34 )
35 {
36         BerElement      *ber = op->o_ber;
37         ber_int_t               version;
38         ber_tag_t method;
39         char            *mech;
40         char            *dn;
41         char *ndn;
42         ber_tag_t       tag;
43         int                     rc = LDAP_SUCCESS;
44         const char      *text;
45         struct berval   cred;
46         Backend         *be;
47
48         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
49
50         dn = NULL;
51         ndn = NULL;
52         mech = NULL;
53         cred.bv_val = NULL;
54
55         /*
56          * Force to connection to "anonymous" until bind succeeds.
57          */
58         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
59         connection2anonymous( conn );
60         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
61
62         if ( op->o_dn != NULL ) {
63                 free( op->o_dn );
64                 op->o_dn = ch_strdup( "" );
65         }
66
67         if ( op->o_ndn != NULL ) {
68                 free( op->o_ndn );
69                 op->o_ndn = ch_strdup( "" );
70         }
71
72         /*
73          * Parse the bind request.  It looks like this:
74          *
75          *      BindRequest ::= SEQUENCE {
76          *              version         INTEGER,                 -- version
77          *              name            DistinguishedName,       -- dn
78          *              authentication  CHOICE {
79          *                      simple          [0] OCTET STRING -- passwd
80          *                      krbv42ldap      [1] OCTET STRING
81          *                      krbv42dsa       [2] OCTET STRING
82          *                      SASL            [3] SaslCredentials
83          *              }
84          *      }
85          *
86          *      SaslCredentials ::= SEQUENCE {
87      *          mechanism           LDAPString,
88      *          credentials         OCTET STRING OPTIONAL
89          *      }
90          */
91
92         tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
93
94         if ( tag == LBER_ERROR ) {
95                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
96                 send_ldap_disconnect( conn, op,
97                         LDAP_PROTOCOL_ERROR, "decoding error" );
98                 rc = -1;
99                 goto cleanup;
100         }
101
102         op->o_protocol = version;
103
104         if( method != LDAP_AUTH_SASL ) {
105                 tag = ber_scanf( ber, /*{*/ "o}", &cred );
106
107         } else {
108                 tag = ber_scanf( ber, "{a" /*}*/, &mech );
109
110                 if ( tag != LBER_ERROR ) {
111                         ber_len_t len;
112                         tag = ber_peek_tag( ber, &len );
113
114                         if ( tag == LDAP_TAG_LDAPCRED ) { 
115                                 tag = ber_scanf( ber, "o", &cred );
116                         } else {
117                                 tag = LDAP_TAG_LDAPCRED;
118                                 cred.bv_val = NULL;
119                                 cred.bv_len = 0;
120                         }
121
122                         if ( tag != LBER_ERROR ) {
123                                 tag = ber_scanf( ber, /*{{*/ "}}" );
124                         }
125                 }
126         }
127
128         if ( tag == LBER_ERROR ) {
129                 send_ldap_disconnect( conn, op,
130                         LDAP_PROTOCOL_ERROR,
131                 "decoding error" );
132                 rc = SLAPD_DISCONNECT;
133                 goto cleanup;
134         }
135
136         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
137                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
138                 goto cleanup;
139         } 
140
141         ndn = ch_strdup( dn );
142
143         if ( dn_normalize( ndn ) == NULL ) {
144                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
145                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
146                     "invalid DN", NULL, NULL );
147                 goto cleanup;
148         }
149
150         if( method == LDAP_AUTH_SASL ) {
151                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
152                         dn, mech, NULL );
153         } else {
154                 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
155                         (unsigned long) version, dn, (unsigned long) method );
156         }
157
158         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
159             op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
160
161         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
162                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
163                         (unsigned long) version, 0, 0 );
164                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
165                         NULL, "requested protocol version not supported", NULL, NULL );
166                 goto cleanup;
167
168         } else if (( global_disallows & SLAP_DISALLOW_BIND_V2 ) &&
169                 version < LDAP_VERSION3 )
170         {
171                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
172                         NULL, "requested protocol version not allowed", NULL, NULL );
173                 goto cleanup;
174         }
175
176         /* we set connection version regardless of whether bind succeeds
177          * or not.
178          */
179         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
180         conn->c_protocol = version;
181         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
182
183         if ( method == LDAP_AUTH_SASL ) {
184                 char *edn;
185                 slap_ssf_t ssf = 0;
186
187                 if ( version < LDAP_VERSION3 ) {
188                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
189                                 (unsigned long) version, 0, 0 );
190                         send_ldap_disconnect( conn, op,
191                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
192                         rc = SLAPD_DISCONNECT;
193                         goto cleanup;
194                 }
195
196                 if( mech == NULL || *mech == '\0' ) {
197                         Debug( LDAP_DEBUG_ANY,
198                                 "do_bind: no sasl mechanism provided\n",
199                                 0, 0, 0 );
200                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
201                                 NULL, "no SASL mechanism provided", NULL, NULL );
202                         goto cleanup;
203                 }
204
205                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
206                 if ( conn->c_sasl_bind_in_progress ) {
207                         if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
208                                 /* mechanism changed between bind steps */
209                                 slap_sasl_reset(conn);
210                         }
211                 } else {
212                         conn->c_sasl_bind_mech = mech;
213                         mech = NULL;
214                 }
215                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
216
217                 edn = NULL;
218                 rc = slap_sasl_bind( conn, op, dn, ndn, &cred, &edn, &ssf );
219
220                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
221                 if( rc == LDAP_SUCCESS ) {
222                         conn->c_dn = edn;
223                         conn->c_authmech = conn->c_sasl_bind_mech;
224                         conn->c_sasl_bind_mech = NULL;
225                         conn->c_sasl_bind_in_progress = 0;
226                         conn->c_sasl_ssf = ssf;
227                         if( ssf > conn->c_ssf ) {
228                                 conn->c_ssf = ssf;
229                         }
230                 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
231                         conn->c_sasl_bind_in_progress = 1;
232
233                 } else {
234                         if ( conn->c_sasl_bind_mech ) {
235                                 free( conn->c_sasl_bind_mech );
236                                 conn->c_sasl_bind_mech = NULL;
237                         }
238                         conn->c_sasl_bind_in_progress = 0;
239                 }
240                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
241
242                 goto cleanup;
243
244         } else {
245                 /* Not SASL, cancel any in-progress bind */
246                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
247
248                 if ( conn->c_sasl_bind_mech != NULL ) {
249                         free(conn->c_sasl_bind_mech);
250                         conn->c_sasl_bind_mech = NULL;
251                 }
252                 conn->c_sasl_bind_in_progress = 0;
253
254                 slap_sasl_reset( conn );
255                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
256         }
257
258         if ( method == LDAP_AUTH_SIMPLE ) {
259                 /* accept "anonymous" binds */
260                 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
261                         rc = LDAP_SUCCESS;
262                         text = NULL;
263
264                         if( cred.bv_len &&
265                                 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
266                         {
267                                 /* cred is not empty, disallow */
268                                 rc = LDAP_INVALID_CREDENTIALS;
269
270                         } else if ( ndn != NULL && *ndn != '\0' &&
271                                 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
272                         {
273                                 /* DN is not empty, disallow */
274                                 rc = LDAP_UNWILLING_TO_PERFORM;
275                                 text = "unwilling to allow anonymous bind with non-empty DN";
276
277                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
278                                 /* disallow */
279                                 rc = LDAP_INAPPROPRIATE_AUTH;
280                                 text = "anonymous bind disallowed";
281                         }
282
283                         /*
284                          * we already forced connection to "anonymous",
285                          * just need to send success
286                          */
287                         send_ldap_result( conn, op, rc,
288                                 NULL, text, NULL, NULL );
289                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
290                                 version, 0, 0 );
291                         goto cleanup;
292
293                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
294                         /* disallow simple authentication */
295                         rc = LDAP_UNWILLING_TO_PERFORM;
296                         text = "unwilling to perform simple authentication";
297
298                         send_ldap_result( conn, op, rc,
299                                 NULL, text, NULL, NULL );
300                         Debug( LDAP_DEBUG_TRACE,
301                                 "do_bind: v%d simple bind(%s) disallowed\n",
302                                 version, ndn, 0 );
303                         goto cleanup;
304                 }
305
306 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
307         } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
308                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
309                         /* disallow simple authentication */
310                         rc = LDAP_UNWILLING_TO_PERFORM;
311                         text = "unwilling to perform Kerberos V4 bind";
312
313                         send_ldap_result( conn, op, rc,
314                                 NULL, text, NULL, NULL );
315                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
316                                 version, 0, 0 );
317                         goto cleanup;
318                 }
319 #endif
320
321         } else {
322                 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
323                 text = "unknown authentication method";
324
325                 send_ldap_result( conn, op, rc,
326                         NULL, text, NULL, NULL );
327                 Debug( LDAP_DEBUG_TRACE,
328                         "do_bind: v%d unknown authentication method (%d)\n",
329                         version, method, 0 );
330                 goto cleanup;
331         }
332
333         /*
334          * We could be serving multiple database backends.  Select the
335          * appropriate one, or send a referral to our "referral server"
336          * if we don't hold it.
337          */
338
339         if ( (be = select_backend( ndn, 0 )) == NULL ) {
340                 if ( default_referral ) {
341                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
342                                 NULL, NULL, default_referral, NULL );
343
344                 } else {
345                         /* noSuchObject is not allowed to be returned by bind */
346                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
347                                 NULL, NULL, NULL, NULL );
348                 }
349
350                 goto cleanup;
351         }
352
353         /* check restrictions */
354         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
355         if( rc != LDAP_SUCCESS ) {
356                 send_ldap_result( conn, op, rc,
357                         NULL, text, NULL, NULL );
358                 goto cleanup;
359         }
360
361         conn->c_authz_backend = be;
362
363         if ( be->be_bind ) {
364                 int ret;
365                 /* alias suffix */
366                 char *edn = NULL;
367
368                 /* deref suffix alias if appropriate */
369                 ndn = suffix_alias( be, ndn );
370
371                 ret = (*be->be_bind)( be, conn, op, dn, ndn,
372                         method, &cred, &edn );
373
374                 if ( ret == 0 ) {
375                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
376
377                         conn->c_cdn = dn;
378                         dn = NULL;
379
380                         if(edn != NULL) {
381                                 conn->c_dn = edn;
382                         } else {
383                                 conn->c_dn = ndn;
384                                 ndn = NULL;
385                         }
386
387                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
388                         version, conn->c_cdn, conn->c_dn );
389
390                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
391
392                         /* send this here to avoid a race condition */
393                         send_ldap_result( conn, op, LDAP_SUCCESS,
394                                 NULL, NULL, NULL, NULL );
395
396                 } else if (edn != NULL) {
397                         free( edn );
398                 }
399
400         } else {
401                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
402                         NULL, "operation not supported within namingContext", NULL, NULL );
403         }
404
405 cleanup:
406         if( dn != NULL ) {
407                 free( dn );
408         }
409         if( ndn != NULL ) {
410                 free( ndn );
411         }
412         if ( mech != NULL ) {
413                 free( mech );
414         }
415         if ( cred.bv_val != NULL ) {
416                 free( cred.bv_val );
417         }
418
419         return rc;
420 }