]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
cbbb1383f69489d791b283abe744836b86151679
[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", LDAP_LEVEL_ENTRY,
52                 "do_bind: conn %d\n", conn->c_connid ));
53 #else
54         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
55 #endif
56
57         /*
58          * Force to connection to "anonymous" until bind succeeds.
59          */
60         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
61         connection2anonymous( conn );
62         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
63
64         if ( op->o_dn.bv_val != NULL ) {
65                 free( op->o_dn.bv_val );
66                 op->o_dn.bv_val = ch_strdup( "" );
67                 op->o_dn.bv_len = 0;
68         }
69
70         if ( op->o_ndn.bv_val != NULL ) {
71                 free( op->o_ndn.bv_val );
72                 op->o_ndn.bv_val = ch_strdup( "" );
73                 op->o_ndn.bv_len = 0;
74         }
75
76         /*
77          * Parse the bind request.  It looks like this:
78          *
79          *      BindRequest ::= SEQUENCE {
80          *              version         INTEGER,                 -- version
81          *              name            DistinguishedName,       -- dn
82          *              authentication  CHOICE {
83          *                      simple          [0] OCTET STRING -- passwd
84          *                      krbv42ldap      [1] OCTET STRING
85          *                      krbv42dsa       [2] OCTET STRING
86          *                      SASL            [3] SaslCredentials
87          *              }
88          *      }
89          *
90          *      SaslCredentials ::= SEQUENCE {
91      *          mechanism           LDAPString,
92      *          credentials         OCTET STRING OPTIONAL
93          *      }
94          */
95
96         tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
97
98         if ( tag == LBER_ERROR ) {
99 #ifdef NEW_LOGGING
100                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
101                         "do_bind: conn %d  ber_scanf failed\n", conn->c_connid ));
102 #else
103                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
104 #endif
105                 send_ldap_disconnect( conn, op,
106                         LDAP_PROTOCOL_ERROR, "decoding error" );
107                 rc = -1;
108                 goto cleanup;
109         }
110
111         op->o_protocol = version;
112
113         if( method != LDAP_AUTH_SASL ) {
114                 tag = ber_scanf( ber, /*{*/ "m}", &cred );
115
116         } else {
117                 tag = ber_scanf( ber, "{o" /*}*/, &mech );
118
119                 if ( tag != LBER_ERROR ) {
120                         ber_len_t len;
121                         tag = ber_peek_tag( ber, &len );
122
123                         if ( tag == LDAP_TAG_LDAPCRED ) { 
124                                 tag = ber_scanf( ber, "m", &cred );
125                         } else {
126                                 tag = LDAP_TAG_LDAPCRED;
127                                 cred.bv_val = NULL;
128                                 cred.bv_len = 0;
129                         }
130
131                         if ( tag != LBER_ERROR ) {
132                                 tag = ber_scanf( ber, /*{{*/ "}}" );
133                         }
134                 }
135         }
136
137         if ( tag == LBER_ERROR ) {
138                 send_ldap_disconnect( conn, op,
139                         LDAP_PROTOCOL_ERROR,
140                 "decoding error" );
141                 rc = SLAPD_DISCONNECT;
142                 goto cleanup;
143         }
144
145         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
146 #ifdef NEW_LOGGING
147                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
148                         "do_bind: conn %d  get_ctrls failed\n", conn->c_connid ));
149 #else
150                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
151 #endif
152                 goto cleanup;
153         } 
154
155         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
156         if ( rc != LDAP_SUCCESS ) {
157 #ifdef NEW_LOGGING
158                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
159                         "do_bind: conn %d  invalid dn (%s)\n",
160                         conn->c_connid, dn.bv_val ));
161 #else
162                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
163                         dn.bv_val, 0, 0 );
164 #endif
165                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
166                     "invalid DN", NULL, NULL );
167                 goto cleanup;
168         }
169
170         if( method == LDAP_AUTH_SASL ) {
171 #ifdef NEW_LOGGING
172                 LDAP_LOG(( "operation",  LDAP_LEVEL_DETAIL1,
173                         "do_sasl_bind: conn %d  dn (%s) mech %s\n", conn->c_connid,
174                         pdn.bv_val, mech.bv_val ));
175 #else
176                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
177                         pdn.bv_val, mech.bv_val, NULL );
178 #endif
179
180         } else {
181 #ifdef NEW_LOGGING
182                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
183                         "do_bind: conn %d  version=%ld dn=\"%s\" method=%ld\n",
184                         conn->c_connid, (unsigned long) version,
185                         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", LDAP_LEVEL_INFO,
200                         "do_bind: conn %d  unknown version = %ld\n",
201                         conn->c_connid, (unsigned long)version ));
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", LDAP_LEVEL_INFO,
240                                 "do_bind: conn %d  sasl with LDAPv%ld\n",
241                                 conn->c_connid, (unsigned long)version ));
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", LDAP_LEVEL_INFO,
255                                    "do_bind: conn %d  no SASL mechanism provided\n",
256                                    conn->c_connid ));
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                 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
316                         conn->c_sasl_bind_in_progress = 1;
317
318                 } else {
319                         if ( conn->c_sasl_bind_mech.bv_val ) {
320                                 free( conn->c_sasl_bind_mech.bv_val );
321                                 conn->c_sasl_bind_mech.bv_val = NULL;
322                                 conn->c_sasl_bind_mech.bv_len = 0;
323                         }
324                         conn->c_sasl_bind_in_progress = 0;
325                 }
326                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
327
328                 goto cleanup;
329
330         } else {
331                 /* Not SASL, cancel any in-progress bind */
332                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
333
334                 if ( conn->c_sasl_bind_mech.bv_val != NULL ) {
335                         free(conn->c_sasl_bind_mech.bv_val);
336                         conn->c_sasl_bind_mech.bv_val = NULL;
337                         conn->c_sasl_bind_mech.bv_len = 0;
338                 }
339                 conn->c_sasl_bind_in_progress = 0;
340
341                 slap_sasl_reset( conn );
342                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
343         }
344
345         if ( method == LDAP_AUTH_SIMPLE ) {
346                 /* accept "anonymous" binds */
347                 if ( cred.bv_len == 0 || ndn.bv_len == 0 ) {
348                         rc = LDAP_SUCCESS;
349                         text = NULL;
350
351                         if( cred.bv_len &&
352                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
353                         {
354                                 /* cred is not empty, disallow */
355                                 rc = LDAP_INVALID_CREDENTIALS;
356
357                         } else if ( ndn.bv_len &&
358                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
359                         {
360                                 /* DN is not empty, disallow */
361                                 rc = LDAP_UNWILLING_TO_PERFORM;
362                                 text = "unwilling to allow anonymous bind with non-empty DN";
363
364                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
365                                 /* disallow */
366                                 rc = LDAP_INAPPROPRIATE_AUTH;
367                                 text = "anonymous bind disallowed";
368
369                         } else {
370                                 rc = backend_check_restrictions( NULL, conn, op,
371                                         &mech, &text );
372                         }
373
374                         /*
375                          * we already forced connection to "anonymous",
376                          * just need to send success
377                          */
378                         send_ldap_result( conn, op, rc,
379                                 NULL, text, NULL, NULL );
380 #ifdef NEW_LOGGING
381                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
382                                    "do_bind: conn %d  v%d anonymous bind\n",
383                                    conn->c_connid, version ));
384 #else
385                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
386                                 version, 0, 0 );
387 #endif
388                         goto cleanup;
389
390                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
391                         /* disallow simple authentication */
392                         rc = LDAP_UNWILLING_TO_PERFORM;
393                         text = "unwilling to perform simple authentication";
394
395                         send_ldap_result( conn, op, rc,
396                                 NULL, text, NULL, NULL );
397 #ifdef NEW_LOGGING
398                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
399                                    "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
400                                    conn->c_connid, version, ndn.bv_val ));
401 #else
402                         Debug( LDAP_DEBUG_TRACE,
403                                 "do_bind: v%d simple bind(%s) disallowed\n",
404                                 version, ndn.bv_val, 0 );
405 #endif
406                         goto cleanup;
407                 }
408
409 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
410         } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
411                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
412                         /* disallow simple authentication */
413                         rc = LDAP_UNWILLING_TO_PERFORM;
414                         text = "unwilling to perform Kerberos V4 bind";
415
416                         send_ldap_result( conn, op, rc,
417                                 NULL, text, NULL, NULL );
418 #ifdef NEW_LOGGING
419                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
420                                    "do_bind: conn %d  v%d Kerberos V4 bind\n",
421                                    conn->c_connid, version ));
422 #else
423                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
424                                 version, 0, 0 );
425 #endif
426                         goto cleanup;
427                 }
428 #endif
429
430         } else {
431                 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
432                 text = "unknown authentication method";
433
434                 send_ldap_result( conn, op, rc,
435                         NULL, text, NULL, NULL );
436 #ifdef NEW_LOGGING
437                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
438                            "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
439                            conn->c_connid, version, method ));
440 #else
441                 Debug( LDAP_DEBUG_TRACE,
442                         "do_bind: v%d unknown authentication method (%ld)\n",
443                         version, method, 0 );
444 #endif
445                 goto cleanup;
446         }
447
448         /*
449          * We could be serving multiple database backends.  Select the
450          * appropriate one, or send a referral to our "referral server"
451          * if we don't hold it.
452          */
453
454         if ( (be = select_backend( &ndn, 0, 0 )) == NULL ) {
455                 if ( default_referral ) {
456                         BerVarray ref = referral_rewrite( default_referral,
457                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
458
459                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
460                                 NULL, NULL, ref ? ref : default_referral, NULL );
461
462                         ber_bvarray_free( ref );
463
464                 } else {
465                         /* noSuchObject is not allowed to be returned by bind */
466                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
467                                 NULL, NULL, NULL, NULL );
468                 }
469
470                 goto cleanup;
471         }
472
473         /* check restrictions */
474         rc = backend_check_restrictions( be, conn, op, NULL, &text );
475         if( rc != LDAP_SUCCESS ) {
476                 send_ldap_result( conn, op, rc,
477                         NULL, text, NULL, NULL );
478                 goto cleanup;
479         }
480
481         conn->c_authz_backend = be;
482
483         if ( be->be_bind ) {
484                 int ret;
485
486                 /* deref suffix alias if appropriate */
487                 suffix_alias( be, &ndn );
488
489                 ret = (*be->be_bind)( be, conn, op,
490                         &pdn, &ndn, method, &cred, &edn );
491
492                 if ( ret == 0 ) {
493                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
494
495                         if(edn.bv_len) {
496                                 conn->c_dn = edn;
497                         } else {
498                                 ber_dupbv( &conn->c_dn, &pdn );
499                         }
500                         conn->c_cdn = pdn;
501                         pdn.bv_val = NULL;
502                         pdn.bv_len = 0;
503
504                         conn->c_ndn = ndn;
505                         ndn.bv_val = NULL;
506                         ndn.bv_len = 0;
507
508                         if( conn->c_dn.bv_len != 0 ) {
509                                 ber_len_t max = sockbuf_max_incoming;
510                                 ber_sockbuf_ctrl( conn->c_sb,
511                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
512                         }
513
514 #ifdef NEW_LOGGING
515                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
516                                 "do_bind: conn %d  v%d bind: \"%s\" to \"%s\" \n",
517                                 conn->c_connid, version, conn->c_cdn.bv_val, conn->c_dn.bv_val ));
518 #else
519                         Debug( LDAP_DEBUG_TRACE,
520                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
521                                 version, conn->c_cdn.bv_val, conn->c_dn.bv_val );
522 #endif
523
524                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
525
526                         /* send this here to avoid a race condition */
527                         send_ldap_result( conn, op, LDAP_SUCCESS,
528                                 NULL, NULL, NULL, NULL );
529
530                 } else if (edn.bv_val != NULL) {
531                         free( edn.bv_val );
532                 }
533
534         } else {
535                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
536                         NULL, "operation not supported within namingContext",
537                         NULL, NULL );
538         }
539
540 cleanup:
541         if( pdn.bv_val != NULL ) {
542                 free( pdn.bv_val );
543         }
544         if( ndn.bv_val != NULL ) {
545                 free( ndn.bv_val );
546         }
547         if ( mech.bv_val != NULL ) {
548                 free( mech.bv_val );
549         }
550
551         return rc;
552 }