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