]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
sync with HEAD
[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;
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         if ( method == LDAP_AUTH_SASL ) {
235                 slap_ssf_t ssf = 0;
236
237                 if ( version < LDAP_VERSION3 ) {
238 #ifdef NEW_LOGGING
239                         LDAP_LOG( OPERATION, INFO, 
240                                 "do_bind: conn %d  sasl with LDAPv%ld\n",
241                                 conn->c_connid, (unsigned long)version , 0 );
242 #else
243                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
244                                 (unsigned long) version, 0, 0 );
245 #endif
246                         send_ldap_disconnect( conn, op,
247                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
248                         rc = SLAPD_DISCONNECT;
249                         goto cleanup;
250                 }
251
252                 if( mech.bv_len == 0 ) {
253 #ifdef NEW_LOGGING
254                         LDAP_LOG( OPERATION, INFO, 
255                                    "do_bind: conn %d  no SASL mechanism provided\n",
256                                    conn->c_connid, 0, 0 );
257 #else
258                         Debug( LDAP_DEBUG_ANY,
259                                 "do_bind: no sasl mechanism provided\n",
260                                 0, 0, 0 );
261 #endif
262                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
263                                 NULL, "no SASL mechanism provided", NULL, NULL );
264                         goto cleanup;
265                 }
266
267                 /* check restrictions */
268                 rc = backend_check_restrictions( NULL, conn, op, &mech, &text );
269                 if( rc != LDAP_SUCCESS ) {
270                         send_ldap_result( conn, op, rc,
271                                 NULL, text, NULL, NULL );
272                         goto cleanup;
273                 }
274
275                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
276                 if ( conn->c_sasl_bind_in_progress ) {
277                         if((ber_bvcmp(&conn->c_sasl_bind_mech, &mech) != 0)) {
278                                 /* mechanism changed between bind steps */
279                                 slap_sasl_reset(conn);
280                         }
281                 } else {
282                         conn->c_sasl_bind_mech = mech;
283                         mech.bv_val = NULL;
284                         mech.bv_len = 0;
285                 }
286                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
287
288                 rc = slap_sasl_bind( conn, op,
289                         &pdn, &ndn,
290                         &cred, &edn, &ssf );
291
292                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
293                 if( rc == LDAP_SUCCESS ) {
294                         conn->c_dn = edn;
295                         if( edn.bv_len != 0 ) {
296                                 /* edn is always normalized already */
297                                 ber_dupbv( &conn->c_ndn, &conn->c_dn );
298                         }
299                         conn->c_authmech = conn->c_sasl_bind_mech;
300                         conn->c_sasl_bind_mech.bv_val = NULL;
301                         conn->c_sasl_bind_mech.bv_len = 0;
302                         conn->c_sasl_bind_in_progress = 0;
303
304                         conn->c_sasl_ssf = ssf;
305                         if( ssf > conn->c_ssf ) {
306                                 conn->c_ssf = ssf;
307                         }
308
309                         if( conn->c_dn.bv_len != 0 ) {
310                                 ber_len_t max = sockbuf_max_incoming;
311                                 ber_sockbuf_ctrl( conn->c_sb,
312                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
313                         }
314
315 #ifdef NEW_LOGGING
316                         LDAP_LOG( OPERATION, DETAIL1, 
317                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
318                                 conn->c_authmech.bv_val, conn->c_dn.bv_val, ssf );
319 #else
320                         Debug( LDAP_DEBUG_TRACE,
321                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
322                                 conn->c_authmech.bv_val, conn->c_dn.bv_val, ssf );
323 #endif
324
325                 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
326                         conn->c_sasl_bind_in_progress = 1;
327
328                 } else {
329                         if ( conn->c_sasl_bind_mech.bv_val ) {
330                                 free( conn->c_sasl_bind_mech.bv_val );
331                                 conn->c_sasl_bind_mech.bv_val = NULL;
332                                 conn->c_sasl_bind_mech.bv_len = 0;
333                         }
334                         conn->c_sasl_bind_in_progress = 0;
335                 }
336                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
337
338                 goto cleanup;
339
340         } else {
341                 /* Not SASL, cancel any in-progress bind */
342                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
343
344                 if ( conn->c_sasl_bind_mech.bv_val != NULL ) {
345                         free(conn->c_sasl_bind_mech.bv_val);
346                         conn->c_sasl_bind_mech.bv_val = NULL;
347                         conn->c_sasl_bind_mech.bv_len = 0;
348                 }
349                 conn->c_sasl_bind_in_progress = 0;
350
351                 slap_sasl_reset( conn );
352                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
353         }
354
355         if ( method == LDAP_AUTH_SIMPLE ) {
356                 /* accept "anonymous" binds */
357                 if ( cred.bv_len == 0 || ndn.bv_len == 0 ) {
358                         rc = LDAP_SUCCESS;
359                         text = NULL;
360
361                         if( cred.bv_len &&
362                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
363                         {
364                                 /* cred is not empty, disallow */
365                                 rc = LDAP_INVALID_CREDENTIALS;
366
367                         } else if ( ndn.bv_len &&
368                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
369                         {
370                                 /* DN is not empty, disallow */
371                                 rc = LDAP_UNWILLING_TO_PERFORM;
372                                 text = "unwilling to allow anonymous bind with non-empty DN";
373
374                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
375                                 /* disallow */
376                                 rc = LDAP_INAPPROPRIATE_AUTH;
377                                 text = "anonymous bind disallowed";
378
379                         } else {
380                                 rc = backend_check_restrictions( NULL, conn, op,
381                                         &mech, &text );
382                         }
383
384                         /*
385                          * we already forced connection to "anonymous",
386                          * just need to send success
387                          */
388                         send_ldap_result( conn, op, rc,
389                                 NULL, text, NULL, NULL );
390 #ifdef NEW_LOGGING
391                         LDAP_LOG( OPERATION, DETAIL1, 
392                                    "do_bind: conn %d  v%d anonymous bind\n",
393                                    conn->c_connid, version , 0 );
394 #else
395                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
396                                 version, 0, 0 );
397 #endif
398                         goto cleanup;
399
400                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
401                         /* disallow simple authentication */
402                         rc = LDAP_UNWILLING_TO_PERFORM;
403                         text = "unwilling to perform simple authentication";
404
405                         send_ldap_result( conn, op, rc,
406                                 NULL, text, NULL, NULL );
407 #ifdef NEW_LOGGING
408                         LDAP_LOG( OPERATION, INFO, 
409                                    "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
410                                    conn->c_connid, version, ndn.bv_val );
411 #else
412                         Debug( LDAP_DEBUG_TRACE,
413                                 "do_bind: v%d simple bind(%s) disallowed\n",
414                                 version, ndn.bv_val, 0 );
415 #endif
416                         goto cleanup;
417
418                 } else if (( global_disallows & SLAP_DISALLOW_BIND_SIMPLE_UNPROTECTED )
419                         && ( op->o_ssf < global_ssf_set.sss_ssf ))
420                 {
421                         rc = LDAP_CONFIDENTIALITY_REQUIRED;
422                         text = "unwilling to perform simple authentication "
423                                 "without confidentilty protection";
424
425                         send_ldap_result( conn, op, rc,
426                                 NULL, text, NULL, NULL );
427
428 #ifdef NEW_LOGGING
429                         LDAP_LOG( OPERATION, INFO, "do_bind: conn %d  "
430                                 "v%d unprotected simple bind(%s) disallowed\n",
431                                 conn->c_connid, version, ndn.bv_val );
432 #else
433                         Debug( LDAP_DEBUG_TRACE,
434                                 "do_bind: v%d unprotected simple bind(%s) disallowed\n",
435                                 version, ndn.bv_val, 0 );
436 #endif
437                         goto cleanup;
438                 }
439
440 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
441         } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
442                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
443                         /* disallow simple authentication */
444                         rc = LDAP_UNWILLING_TO_PERFORM;
445                         text = "unwilling to perform Kerberos V4 bind";
446
447                         send_ldap_result( conn, op, rc,
448                                 NULL, text, NULL, NULL );
449 #ifdef NEW_LOGGING
450                         LDAP_LOG( OPERATION, DETAIL1, 
451                                    "do_bind: conn %d  v%d Kerberos V4 bind\n",
452                                    conn->c_connid, version , 0 );
453 #else
454                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
455                                 version, 0, 0 );
456 #endif
457                         goto cleanup;
458                 }
459 #endif
460
461         } else {
462                 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
463                 text = "unknown authentication method";
464
465                 send_ldap_result( conn, op, rc,
466                         NULL, text, NULL, NULL );
467 #ifdef NEW_LOGGING
468                 LDAP_LOG( OPERATION, INFO, 
469                            "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
470                            conn->c_connid, version, method );
471 #else
472                 Debug( LDAP_DEBUG_TRACE,
473                         "do_bind: v%d unknown authentication method (%ld)\n",
474                         version, method, 0 );
475 #endif
476                 goto cleanup;
477         }
478
479         /*
480          * We could be serving multiple database backends.  Select the
481          * appropriate one, or send a referral to our "referral server"
482          * if we don't hold it.
483          */
484
485         if ( (be = select_backend( &ndn, 0, 0 )) == NULL ) {
486                 if ( default_referral ) {
487                         BerVarray ref = referral_rewrite( default_referral,
488                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
489
490                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
491                                 NULL, NULL, ref ? ref : default_referral, NULL );
492
493                         ber_bvarray_free( ref );
494
495                 } else {
496                         /* noSuchObject is not allowed to be returned by bind */
497                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
498                                 NULL, NULL, NULL, NULL );
499                 }
500
501                 goto cleanup;
502         }
503
504         /* check restrictions */
505         rc = backend_check_restrictions( be, conn, op, NULL, &text );
506         if( rc != LDAP_SUCCESS ) {
507                 send_ldap_result( conn, op, rc,
508                         NULL, text, NULL, NULL );
509                 goto cleanup;
510         }
511
512         if ( be->be_bind ) {
513                 int ret;
514
515                 /* deref suffix alias if appropriate */
516                 suffix_alias( be, &ndn );
517
518                 ret = (*be->be_bind)( be, conn, op,
519                         &pdn, &ndn, method, &cred, &edn );
520
521                 if ( ret == 0 ) {
522                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
523
524                         if( conn->c_authz_backend == NULL ) {
525                                 conn->c_authz_backend = be;
526                         }
527
528                         if(edn.bv_len) {
529                                 conn->c_dn = edn;
530                         } else {
531                                 conn->c_dn = pdn;
532                                 pdn.bv_val = NULL;
533                                 pdn.bv_len = 0;
534                         }
535
536                         conn->c_ndn = ndn;
537                         ndn.bv_val = NULL;
538                         ndn.bv_len = 0;
539
540                         if( conn->c_dn.bv_len != 0 ) {
541                                 ber_len_t max = sockbuf_max_incoming;
542                                 ber_sockbuf_ctrl( conn->c_sb,
543                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
544                         }
545
546 #ifdef NEW_LOGGING
547                         LDAP_LOG( OPERATION, DETAIL1, 
548                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
549                                 version, conn->c_dn.bv_val, conn->c_dn.bv_val );
550 #else
551                         Debug( LDAP_DEBUG_TRACE,
552                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
553                                 version, dn.bv_val, conn->c_dn.bv_val );
554 #endif
555
556                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
557
558                         /* send this here to avoid a race condition */
559                         send_ldap_result( conn, op, LDAP_SUCCESS,
560                                 NULL, NULL, NULL, NULL );
561
562                 } else if (edn.bv_val != NULL) {
563                         free( edn.bv_val );
564                 }
565
566         } else {
567                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
568                         NULL, "operation not supported within namingContext",
569                         NULL, NULL );
570         }
571
572 cleanup:
573         if( pdn.bv_val != NULL ) {
574                 free( pdn.bv_val );
575         }
576         if( ndn.bv_val != NULL ) {
577                 free( ndn.bv_val );
578         }
579         if ( mech.bv_val != NULL ) {
580                 free( mech.bv_val );
581         }
582
583         return rc;
584 }