]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Fix objectSubClassIndexer bug
[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-2002 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         struct berval mech = { 0, NULL };
40         struct berval dn = { 0, NULL };
41         struct berval pdn = { 0, NULL };
42         struct berval ndn = { 0, NULL };
43         struct berval edn = { 0, NULL };
44         ber_tag_t tag;
45         int     rc = LDAP_SUCCESS;
46         const char *text;
47         struct berval cred = { 0, NULL };
48         Backend *be = NULL;
49
50 #ifdef NEW_LOGGING
51         LDAP_LOG( OPERATION, ENTRY, "do_bind: conn %d\n", conn->c_connid, 0, 0 );
52 #else
53         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
54 #endif
55
56         /*
57          * Force to connection to "anonymous" until bind succeeds.
58          */
59         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
60         if ( conn->c_sasl_bind_in_progress ) be = conn->c_authz_backend;
61         connection2anonymous( conn );
62         if ( conn->c_sasl_bind_in_progress ) conn->c_authz_backend = be;
63         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
64
65         if ( op->o_dn.bv_val != NULL ) {
66                 free( op->o_dn.bv_val );
67                 op->o_dn.bv_val = ch_strdup( "" );
68                 op->o_dn.bv_len = 0;
69         }
70
71         if ( op->o_ndn.bv_val != NULL ) {
72                 free( op->o_ndn.bv_val );
73                 op->o_ndn.bv_val = ch_strdup( "" );
74                 op->o_ndn.bv_len = 0;
75         }
76
77         /*
78          * Parse the bind request.  It looks like this:
79          *
80          *      BindRequest ::= SEQUENCE {
81          *              version         INTEGER,                 -- version
82          *              name            DistinguishedName,       -- dn
83          *              authentication  CHOICE {
84          *                      simple          [0] OCTET STRING -- passwd
85          *                      krbv42ldap      [1] OCTET STRING
86          *                      krbv42dsa       [2] OCTET STRING
87          *                      SASL            [3] SaslCredentials
88          *              }
89          *      }
90          *
91          *      SaslCredentials ::= SEQUENCE {
92      *          mechanism           LDAPString,
93      *          credentials         OCTET STRING OPTIONAL
94          *      }
95          */
96
97         tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
98
99         if ( tag == LBER_ERROR ) {
100 #ifdef NEW_LOGGING
101                 LDAP_LOG( OPERATION, ERR, 
102                         "do_bind: conn %d  ber_scanf failed\n", conn->c_connid, 0, 0 );
103 #else
104                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
105 #endif
106                 send_ldap_disconnect( conn, op,
107                         LDAP_PROTOCOL_ERROR, "decoding error" );
108                 rc = -1;
109                 goto cleanup;
110         }
111
112         op->o_protocol = version;
113
114         if( method != LDAP_AUTH_SASL ) {
115                 tag = ber_scanf( ber, /*{*/ "m}", &cred );
116
117         } else {
118                 tag = ber_scanf( ber, "{o" /*}*/, &mech );
119
120                 if ( tag != LBER_ERROR ) {
121                         ber_len_t len;
122                         tag = ber_peek_tag( ber, &len );
123
124                         if ( tag == LDAP_TAG_LDAPCRED ) { 
125                                 tag = ber_scanf( ber, "m", &cred );
126                         } else {
127                                 tag = LDAP_TAG_LDAPCRED;
128                                 cred.bv_val = NULL;
129                                 cred.bv_len = 0;
130                         }
131
132                         if ( tag != LBER_ERROR ) {
133                                 tag = ber_scanf( ber, /*{{*/ "}}" );
134                         }
135                 }
136         }
137
138         if ( tag == LBER_ERROR ) {
139                 send_ldap_disconnect( conn, op,
140                         LDAP_PROTOCOL_ERROR,
141                 "decoding error" );
142                 rc = SLAPD_DISCONNECT;
143                 goto cleanup;
144         }
145
146         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
147 #ifdef NEW_LOGGING
148                 LDAP_LOG( OPERATION, INFO, 
149                         "do_bind: conn %d  get_ctrls failed\n", conn->c_connid, 0, 0 );
150 #else
151                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
152 #endif
153                 goto cleanup;
154         } 
155
156         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
157         if ( rc != LDAP_SUCCESS ) {
158 #ifdef NEW_LOGGING
159                 LDAP_LOG( OPERATION, INFO, 
160                         "do_bind: conn %d  invalid dn (%s)\n", 
161                         conn->c_connid, dn.bv_val, 0 );
162 #else
163                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
164                         dn.bv_val, 0, 0 );
165 #endif
166                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
167                     "invalid DN", NULL, NULL );
168                 goto cleanup;
169         }
170
171         if( method == LDAP_AUTH_SASL ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION,     DETAIL1, 
174                         "do_sasl_bind: conn %d  dn (%s) mech %s\n", 
175                         conn->c_connid, pdn.bv_val, mech.bv_val );
176 #else
177                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
178                         pdn.bv_val, mech.bv_val, NULL );
179 #endif
180
181         } else {
182 #ifdef NEW_LOGGING
183                 LDAP_LOG( OPERATION, DETAIL1, 
184                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
185                         (unsigned long) version, pdn.bv_val, (unsigned long)method );
186 #else
187                 Debug( LDAP_DEBUG_TRACE,
188                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
189                         (unsigned long) version,
190                         pdn.bv_val, (unsigned long) method );
191 #endif
192         }
193
194         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
195             op->o_connid, op->o_opid, pdn.bv_val, (unsigned long) method, 0 );
196
197         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
198 #ifdef NEW_LOGGING
199                 LDAP_LOG( OPERATION, INFO, 
200                         "do_bind: conn %d  unknown version = %ld\n",
201                         conn->c_connid, (unsigned long)version, 0 );
202 #else
203                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
204                         (unsigned long) version, 0, 0 );
205 #endif
206                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
207                         NULL, "requested protocol version not supported", NULL, NULL );
208                 goto cleanup;
209
210         } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
211                 version < LDAP_VERSION3 )
212         {
213                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
214                         NULL, "requested protocol version not allowed", NULL, NULL );
215                 goto cleanup;
216         }
217
218         /* we set connection version regardless of whether bind succeeds
219          * or not.
220          */
221         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
222         conn->c_protocol = version;
223         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
224
225         /* check for inappropriate controls */
226         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
227                 send_ldap_result( conn, op,
228                         rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
229                         NULL, "manageDSAit control inappropriate",
230                         NULL, NULL );
231                 goto cleanup;
232         }
233
234         /* Set the bindop for the benefit of in-directory SASL lookups */
235         conn->c_sasl_bindop = op;
236
237         if ( method == LDAP_AUTH_SASL ) {
238                 slap_ssf_t ssf = 0;
239
240                 if ( version < LDAP_VERSION3 ) {
241 #ifdef NEW_LOGGING
242                         LDAP_LOG( OPERATION, INFO, 
243                                 "do_bind: conn %d  sasl with LDAPv%ld\n",
244                                 conn->c_connid, (unsigned long)version , 0 );
245 #else
246                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
247                                 (unsigned long) version, 0, 0 );
248 #endif
249                         send_ldap_disconnect( conn, op,
250                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
251                         rc = SLAPD_DISCONNECT;
252                         goto cleanup;
253                 }
254
255                 if( mech.bv_len == 0 ) {
256 #ifdef NEW_LOGGING
257                         LDAP_LOG( OPERATION, INFO, 
258                                    "do_bind: conn %d  no SASL mechanism provided\n",
259                                    conn->c_connid, 0, 0 );
260 #else
261                         Debug( LDAP_DEBUG_ANY,
262                                 "do_bind: no sasl mechanism provided\n",
263                                 0, 0, 0 );
264 #endif
265                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
266                                 NULL, "no SASL mechanism provided", NULL, NULL );
267                         goto cleanup;
268                 }
269
270                 /* check restrictions */
271                 rc = backend_check_restrictions( NULL, conn, op, &mech, &text );
272                 if( rc != LDAP_SUCCESS ) {
273                         send_ldap_result( conn, op, rc,
274                                 NULL, text, NULL, NULL );
275                         goto cleanup;
276                 }
277
278                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
279                 if ( conn->c_sasl_bind_in_progress ) {
280                         if((ber_bvcmp(&conn->c_sasl_bind_mech, &mech) != 0)) {
281                                 /* mechanism changed between bind steps */
282                                 slap_sasl_reset(conn);
283                         }
284                 } else {
285                         conn->c_sasl_bind_mech = mech;
286                         mech.bv_val = NULL;
287                         mech.bv_len = 0;
288                 }
289                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
290
291                 rc = slap_sasl_bind( conn, op,
292                         &pdn, &ndn,
293                         &cred, &edn, &ssf );
294
295                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
296                 if( rc == LDAP_SUCCESS ) {
297                         conn->c_dn = edn;
298                         if( edn.bv_len != 0 ) {
299                                 /* edn is always normalized already */
300                                 ber_dupbv( &conn->c_ndn, &conn->c_dn );
301                         }
302                         conn->c_authmech = conn->c_sasl_bind_mech;
303                         conn->c_sasl_bind_mech.bv_val = NULL;
304                         conn->c_sasl_bind_mech.bv_len = 0;
305                         conn->c_sasl_bind_in_progress = 0;
306
307                         conn->c_sasl_ssf = ssf;
308                         if( ssf > conn->c_ssf ) {
309                                 conn->c_ssf = ssf;
310                         }
311
312                         if( conn->c_dn.bv_len != 0 ) {
313                                 ber_len_t max = sockbuf_max_incoming_auth;
314                                 ber_sockbuf_ctrl( conn->c_sb,
315                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
316                         }
317
318 #ifdef NEW_LOGGING
319                         LDAP_LOG( OPERATION, DETAIL1, 
320                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
321                                 conn->c_authmech.bv_val, conn->c_dn.bv_val, ssf );
322 #else
323                         Debug( LDAP_DEBUG_TRACE,
324                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
325                                 conn->c_authmech.bv_val, conn->c_dn.bv_val, ssf );
326 #endif
327
328                 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
329                         conn->c_sasl_bind_in_progress = 1;
330
331                 } else {
332                         if ( conn->c_sasl_bind_mech.bv_val ) {
333                                 free( conn->c_sasl_bind_mech.bv_val );
334                                 conn->c_sasl_bind_mech.bv_val = NULL;
335                                 conn->c_sasl_bind_mech.bv_len = 0;
336                         }
337                         conn->c_sasl_bind_in_progress = 0;
338                 }
339                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
340
341                 goto cleanup;
342
343         } else {
344                 /* Not SASL, cancel any in-progress bind */
345                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
346
347                 if ( conn->c_sasl_bind_mech.bv_val != NULL ) {
348                         free(conn->c_sasl_bind_mech.bv_val);
349                         conn->c_sasl_bind_mech.bv_val = NULL;
350                         conn->c_sasl_bind_mech.bv_len = 0;
351                 }
352                 conn->c_sasl_bind_in_progress = 0;
353
354                 slap_sasl_reset( conn );
355                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
356         }
357
358         if ( method == LDAP_AUTH_SIMPLE ) {
359                 /* accept "anonymous" binds */
360                 if ( cred.bv_len == 0 || ndn.bv_len == 0 ) {
361                         rc = LDAP_SUCCESS;
362                         text = NULL;
363
364                         if( cred.bv_len &&
365                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
366                         {
367                                 /* cred is not empty, disallow */
368                                 rc = LDAP_INVALID_CREDENTIALS;
369
370                         } else if ( ndn.bv_len &&
371                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
372                         {
373                                 /* DN is not empty, disallow */
374                                 rc = LDAP_UNWILLING_TO_PERFORM;
375                                 text = "unwilling to allow anonymous bind with non-empty DN";
376
377                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
378                                 /* disallow */
379                                 rc = LDAP_INAPPROPRIATE_AUTH;
380                                 text = "anonymous bind disallowed";
381
382                         } else {
383                                 rc = backend_check_restrictions( NULL, conn, op,
384                                         &mech, &text );
385                         }
386
387                         /*
388                          * we already forced connection to "anonymous",
389                          * just need to send success
390                          */
391                         send_ldap_result( conn, op, rc,
392                                 NULL, text, NULL, NULL );
393 #ifdef NEW_LOGGING
394                         LDAP_LOG( OPERATION, DETAIL1, 
395                                    "do_bind: conn %d  v%d anonymous bind\n",
396                                    conn->c_connid, version , 0 );
397 #else
398                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
399                                 version, 0, 0 );
400 #endif
401                         goto cleanup;
402
403                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
404                         /* disallow simple authentication */
405                         rc = LDAP_UNWILLING_TO_PERFORM;
406                         text = "unwilling to perform simple authentication";
407
408                         send_ldap_result( conn, op, rc,
409                                 NULL, text, NULL, NULL );
410 #ifdef NEW_LOGGING
411                         LDAP_LOG( OPERATION, INFO, 
412                                    "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
413                                    conn->c_connid, version, ndn.bv_val );
414 #else
415                         Debug( LDAP_DEBUG_TRACE,
416                                 "do_bind: v%d simple bind(%s) disallowed\n",
417                                 version, ndn.bv_val, 0 );
418 #endif
419                         goto cleanup;
420
421                 } else if (( global_disallows & SLAP_DISALLOW_BIND_SIMPLE_UNPROTECTED )
422                         && ( op->o_ssf < global_ssf_set.sss_ssf ))
423                 {
424                         rc = LDAP_CONFIDENTIALITY_REQUIRED;
425                         text = "unwilling to perform simple authentication "
426                                 "without confidentilty protection";
427
428                         send_ldap_result( conn, op, rc,
429                                 NULL, text, NULL, NULL );
430
431 #ifdef NEW_LOGGING
432                         LDAP_LOG( OPERATION, INFO, "do_bind: conn %d  "
433                                 "v%d unprotected simple bind(%s) disallowed\n",
434                                 conn->c_connid, version, ndn.bv_val );
435 #else
436                         Debug( LDAP_DEBUG_TRACE,
437                                 "do_bind: v%d unprotected simple bind(%s) disallowed\n",
438                                 version, ndn.bv_val, 0 );
439 #endif
440                         goto cleanup;
441                 }
442
443 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
444         } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
445                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
446                         /* disallow simple authentication */
447                         rc = LDAP_UNWILLING_TO_PERFORM;
448                         text = "unwilling to perform Kerberos V4 bind";
449
450                         send_ldap_result( conn, op, rc,
451                                 NULL, text, NULL, NULL );
452 #ifdef NEW_LOGGING
453                         LDAP_LOG( OPERATION, DETAIL1, 
454                                    "do_bind: conn %d  v%d Kerberos V4 bind\n",
455                                    conn->c_connid, version , 0 );
456 #else
457                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
458                                 version, 0, 0 );
459 #endif
460                         goto cleanup;
461                 }
462 #endif
463
464         } else {
465                 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
466                 text = "unknown authentication method";
467
468                 send_ldap_result( conn, op, rc,
469                         NULL, text, NULL, NULL );
470 #ifdef NEW_LOGGING
471                 LDAP_LOG( OPERATION, INFO, 
472                            "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
473                            conn->c_connid, version, method );
474 #else
475                 Debug( LDAP_DEBUG_TRACE,
476                         "do_bind: v%d unknown authentication method (%ld)\n",
477                         version, method, 0 );
478 #endif
479                 goto cleanup;
480         }
481
482         /*
483          * We could be serving multiple database backends.  Select the
484          * appropriate one, or send a referral to our "referral server"
485          * if we don't hold it.
486          */
487
488         if ( (be = select_backend( &ndn, 0, 0 )) == NULL ) {
489                 if ( default_referral ) {
490                         BerVarray ref = referral_rewrite( default_referral,
491                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
492
493                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
494                                 NULL, NULL, ref ? ref : default_referral, NULL );
495
496                         ber_bvarray_free( ref );
497
498                 } else {
499                         /* noSuchObject is not allowed to be returned by bind */
500                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
501                                 NULL, NULL, NULL, NULL );
502                 }
503
504                 goto cleanup;
505         }
506
507         /* check restrictions */
508         rc = backend_check_restrictions( be, conn, op, NULL, &text );
509         if( rc != LDAP_SUCCESS ) {
510                 send_ldap_result( conn, op, rc,
511                         NULL, text, NULL, NULL );
512                 goto cleanup;
513         }
514
515         if ( be->be_bind ) {
516                 int ret;
517
518                 /* deref suffix alias if appropriate */
519                 suffix_alias( be, &ndn );
520
521                 ret = (*be->be_bind)( be, conn, op,
522                         &pdn, &ndn, method, &cred, &edn );
523
524                 if ( ret == 0 ) {
525                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
526
527                         if( conn->c_authz_backend == NULL ) {
528                                 conn->c_authz_backend = be;
529                         }
530
531                         if(edn.bv_len) {
532                                 conn->c_dn = edn;
533                         } else {
534                                 conn->c_dn = pdn;
535                                 pdn.bv_val = NULL;
536                                 pdn.bv_len = 0;
537                         }
538
539                         conn->c_ndn = ndn;
540                         ndn.bv_val = NULL;
541                         ndn.bv_len = 0;
542
543                         if( conn->c_dn.bv_len != 0 ) {
544                                 ber_len_t max = sockbuf_max_incoming_auth;
545                                 ber_sockbuf_ctrl( conn->c_sb,
546                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
547                         }
548
549 #ifdef NEW_LOGGING
550                         LDAP_LOG( OPERATION, DETAIL1, 
551                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
552                                 version, conn->c_dn.bv_val, conn->c_dn.bv_val );
553 #else
554                         Debug( LDAP_DEBUG_TRACE,
555                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
556                                 version, dn.bv_val, conn->c_dn.bv_val );
557 #endif
558
559                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
560
561                         /* send this here to avoid a race condition */
562                         send_ldap_result( conn, op, LDAP_SUCCESS,
563                                 NULL, NULL, NULL, NULL );
564
565                 } else if (edn.bv_val != NULL) {
566                         free( edn.bv_val );
567                 }
568
569         } else {
570                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
571                         NULL, "operation not supported within namingContext",
572                         NULL, NULL );
573         }
574
575 cleanup:
576         conn->c_sasl_bindop = NULL;
577
578         if( pdn.bv_val != NULL ) {
579                 free( pdn.bv_val );
580         }
581         if( ndn.bv_val != NULL ) {
582                 free( ndn.bv_val );
583         }
584         if ( mech.bv_val != NULL ) {
585                 free( mech.bv_val );
586         }
587
588         return rc;
589 }