]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
minor cleanup
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * Copyright (c) 1995 Regents of the University of Michigan.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms are permitted
13  * provided that this notice is preserved and that due credit is given
14  * to the University of Michigan at Ann Arbor. The name of the University
15  * may not be used to endorse or promote products derived from this
16  * software without specific prior written permission. This software
17  * is provided ``as is'' without express or implied warranty.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/string.h>
25 #include <ac/socket.h>
26
27 #include "ldap_pvt.h"
28 #include "slap.h"
29
30 int
31 do_bind(
32     Connection  *conn,
33     Operation   *op
34 )
35 {
36         BerElement      *ber = op->o_ber;
37         ber_int_t               version;
38         ber_tag_t method;
39         char            *mech;
40         char            *dn;
41         char *ndn;
42         ber_tag_t       tag;
43         int                     rc = LDAP_SUCCESS;
44         const char      *text;
45         struct berval   cred;
46         Backend         *be;
47
48 #ifdef NEW_LOGGING
49         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY, "do_bind: conn %d\n", conn->c_connid ));
50 #else
51         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
52 #endif
53
54         dn = NULL;
55         ndn = NULL;
56         mech = NULL;
57         cred.bv_val = NULL;
58
59         /*
60          * Force to connection to "anonymous" until bind succeeds.
61          */
62         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
63         connection2anonymous( conn );
64         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
65
66         if ( op->o_dn != NULL ) {
67                 free( op->o_dn );
68                 op->o_dn = ch_strdup( "" );
69         }
70
71         if ( op->o_ndn != NULL ) {
72                 free( op->o_ndn );
73                 op->o_ndn = ch_strdup( "" );
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, "{iat" /*}*/, &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, /*{*/ "o}", &cred );
115
116         } else {
117                 tag = ber_scanf( ber, "{a" /*}*/, &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, "o", &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         ndn = ch_strdup( dn );
156
157         if ( dn_normalize( ndn ) == NULL ) {
158 #ifdef NEW_LOGGING
159                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
160                            "do_bind: conn %d  invalid dn (%s)\n", conn->c_connid, dn ));
161 #else
162                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 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                            dn, mech ));
174 #else
175                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
176                         dn, mech, NULL );
177 #endif
178         } else {
179 #ifdef NEW_LOGGING
180                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
181                            "do_bind: conn %d  version=%ld dn=\"%s\" method=%ld\n",
182                            conn->c_connid, (unsigned long) version, dn, (unsigned long)method ));
183 #else
184                 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
185                         (unsigned long) version, dn, (unsigned long) method );
186 #endif
187         }
188
189         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
190             op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
191
192         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
193 #ifdef NEW_LOGGING
194                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
195                            "do_bind: conn %d  unknown version = %ld\n",
196                            conn->c_connid, (unsigned long)version ));
197 #else
198                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
199                         (unsigned long) version, 0, 0 );
200 #endif
201                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
202                         NULL, "requested protocol version not supported", NULL, NULL );
203                 goto cleanup;
204
205         } else if (( global_disallows & SLAP_DISALLOW_BIND_V2 ) &&
206                 version < LDAP_VERSION3 )
207         {
208                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
209                         NULL, "requested protocol version not allowed", NULL, NULL );
210                 goto cleanup;
211         }
212
213         /* we set connection version regardless of whether bind succeeds
214          * or not.
215          */
216         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
217         conn->c_protocol = version;
218         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
219
220         if ( method == LDAP_AUTH_SASL ) {
221                 char *edn;
222                 slap_ssf_t ssf = 0;
223
224                 if ( version < LDAP_VERSION3 ) {
225 #ifdef NEW_LOGGING
226                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
227                                    "do_bind: conn %d  sasl with LDAPv%ld\n",
228                                    conn->c_connid, (unsigned long)version ));
229 #else
230                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
231                                 (unsigned long) version, 0, 0 );
232 #endif
233                         send_ldap_disconnect( conn, op,
234                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
235                         rc = SLAPD_DISCONNECT;
236                         goto cleanup;
237                 }
238
239                 if( mech == NULL || *mech == '\0' ) {
240 #ifdef NEW_LOGGING
241                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
242                                    "do_bind: conn %d  no SASL mechanism provided\n",
243                                    conn->c_connid ));
244 #else
245                         Debug( LDAP_DEBUG_ANY,
246                                 "do_bind: no sasl mechanism provided\n",
247                                 0, 0, 0 );
248 #endif
249                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
250                                 NULL, "no SASL mechanism provided", NULL, NULL );
251                         goto cleanup;
252                 }
253
254                 /* check restrictions */
255                 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
256                 if( rc != LDAP_SUCCESS ) {
257                         send_ldap_result( conn, op, rc,
258                                 NULL, text, NULL, NULL );
259                         goto cleanup;
260                 }
261
262                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
263                 if ( conn->c_sasl_bind_in_progress ) {
264                         if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
265                                 /* mechanism changed between bind steps */
266                                 slap_sasl_reset(conn);
267                         }
268                 } else {
269                         conn->c_sasl_bind_mech = mech;
270                         mech = NULL;
271                 }
272                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
273
274                 edn = NULL;
275                 rc = slap_sasl_bind( conn, op, dn, ndn, &cred, &edn, &ssf );
276
277                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
278                 if( rc == LDAP_SUCCESS ) {
279                         conn->c_dn = edn;
280                         conn->c_authmech = conn->c_sasl_bind_mech;
281                         conn->c_sasl_bind_mech = NULL;
282                         conn->c_sasl_bind_in_progress = 0;
283
284                         conn->c_sasl_ssf = ssf;
285                         if( ssf > conn->c_ssf ) {
286                                 conn->c_ssf = ssf;
287                         }
288
289                         if( conn->c_dn != NULL ) {
290                                 ber_len_t max = sockbuf_max_incoming;
291                                 ber_sockbuf_ctrl( conn->c_sb,
292                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
293                         }
294
295                 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
296                         conn->c_sasl_bind_in_progress = 1;
297
298                 } else {
299                         if ( conn->c_sasl_bind_mech ) {
300                                 free( conn->c_sasl_bind_mech );
301                                 conn->c_sasl_bind_mech = NULL;
302                         }
303                         conn->c_sasl_bind_in_progress = 0;
304                 }
305                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
306
307                 goto cleanup;
308
309         } else {
310                 /* Not SASL, cancel any in-progress bind */
311                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
312
313                 if ( conn->c_sasl_bind_mech != NULL ) {
314                         free(conn->c_sasl_bind_mech);
315                         conn->c_sasl_bind_mech = NULL;
316                 }
317                 conn->c_sasl_bind_in_progress = 0;
318
319                 slap_sasl_reset( conn );
320                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
321         }
322
323         if ( method == LDAP_AUTH_SIMPLE ) {
324                 /* accept "anonymous" binds */
325                 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
326                         rc = LDAP_SUCCESS;
327                         text = NULL;
328
329                         if( cred.bv_len &&
330                                 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
331                         {
332                                 /* cred is not empty, disallow */
333                                 rc = LDAP_INVALID_CREDENTIALS;
334
335                         } else if ( ndn != NULL && *ndn != '\0' &&
336                                 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
337                         {
338                                 /* DN is not empty, disallow */
339                                 rc = LDAP_UNWILLING_TO_PERFORM;
340                                 text = "unwilling to allow anonymous bind with non-empty DN";
341
342                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
343                                 /* disallow */
344                                 rc = LDAP_INAPPROPRIATE_AUTH;
345                                 text = "anonymous bind disallowed";
346
347                         } else {
348                                 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
349                         }
350
351                         /*
352                          * we already forced connection to "anonymous",
353                          * just need to send success
354                          */
355                         send_ldap_result( conn, op, rc,
356                                 NULL, text, NULL, NULL );
357 #ifdef NEW_LOGGING
358                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
359                                    "do_bind: conn %d  v%d anonymous bind\n",
360                                    conn->c_connid, version ));
361 #else
362                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
363                                 version, 0, 0 );
364 #endif
365                         goto cleanup;
366
367                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
368                         /* disallow simple authentication */
369                         rc = LDAP_UNWILLING_TO_PERFORM;
370                         text = "unwilling to perform simple authentication";
371
372                         send_ldap_result( conn, op, rc,
373                                 NULL, text, NULL, NULL );
374 #ifdef NEW_LOGGING
375                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
376                                    "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
377                                    conn->c_connid, version, ndn ));
378 #else
379                         Debug( LDAP_DEBUG_TRACE,
380                                 "do_bind: v%d simple bind(%s) disallowed\n",
381                                 version, ndn, 0 );
382 #endif
383                         goto cleanup;
384                 }
385
386 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
387         } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
388                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
389                         /* disallow simple authentication */
390                         rc = LDAP_UNWILLING_TO_PERFORM;
391                         text = "unwilling to perform Kerberos V4 bind";
392
393                         send_ldap_result( conn, op, rc,
394                                 NULL, text, NULL, NULL );
395 #ifdef NEW_LOGGING
396                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
397                                    "do_bind: conn %d  v%d Kerberos V4 bind\n",
398                                    conn->c_connid, version ));
399 #else
400                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
401                                 version, 0, 0 );
402 #endif
403                         goto cleanup;
404                 }
405 #endif
406
407         } else {
408                 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
409                 text = "unknown authentication method";
410
411                 send_ldap_result( conn, op, rc,
412                         NULL, text, NULL, NULL );
413 #ifdef NEW_LOGGING
414                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
415                            "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
416                            conn->c_connid, version, method ));
417 #else
418                 Debug( LDAP_DEBUG_TRACE,
419                         "do_bind: v%d unknown authentication method (%ld)\n",
420                         version, method, 0 );
421 #endif
422                 goto cleanup;
423         }
424
425         /*
426          * We could be serving multiple database backends.  Select the
427          * appropriate one, or send a referral to our "referral server"
428          * if we don't hold it.
429          */
430
431         if ( (be = select_backend( ndn, 0 )) == NULL ) {
432                 if ( default_referral ) {
433                         struct berval **ref = referral_rewrite( default_referral,
434                                 NULL, dn, LDAP_SCOPE_DEFAULT );
435
436                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
437                                 NULL, NULL, ref ? ref : default_referral, NULL );
438
439                         ber_bvecfree( ref );
440
441                 } else {
442                         /* noSuchObject is not allowed to be returned by bind */
443                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
444                                 NULL, NULL, NULL, NULL );
445                 }
446
447                 goto cleanup;
448         }
449
450         /* check restrictions */
451         rc = backend_check_restrictions( be, conn, op, NULL, &text );
452         if( rc != LDAP_SUCCESS ) {
453                 send_ldap_result( conn, op, rc,
454                         NULL, text, NULL, NULL );
455                 goto cleanup;
456         }
457
458         conn->c_authz_backend = be;
459
460         if ( be->be_bind ) {
461                 int ret;
462                 /* alias suffix */
463                 char *edn = NULL;
464
465                 /* deref suffix alias if appropriate */
466                 ndn = suffix_alias( be, ndn );
467
468                 ret = (*be->be_bind)( be, conn, op, dn, ndn,
469                         method, &cred, &edn );
470
471                 if ( ret == 0 ) {
472                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
473
474                         conn->c_cdn = dn;
475                         dn = NULL;
476
477                         if(edn != NULL) {
478                                 conn->c_dn = edn;
479                         } else {
480                                 conn->c_dn = ndn;
481                                 ndn = NULL;
482                         }
483
484                         if( conn->c_dn != NULL ) {
485                                 ber_len_t max = sockbuf_max_incoming;
486                                 ber_sockbuf_ctrl( conn->c_sb,
487                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
488                         }
489
490 #ifdef NEW_LOGGING
491                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
492                                    "do_bind: conn %d  v%d bind: \"%s\" to \"%s\" \n",
493                                    conn->c_connid, version, conn->c_cdn, conn->c_dn ));
494 #else
495                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
496                         version, conn->c_cdn, conn->c_dn );
497 #endif
498
499                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
500
501                         /* send this here to avoid a race condition */
502                         send_ldap_result( conn, op, LDAP_SUCCESS,
503                                 NULL, NULL, NULL, NULL );
504
505                 } else if (edn != NULL) {
506                         free( edn );
507                 }
508
509         } else {
510                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
511                         NULL, "operation not supported within namingContext", NULL, NULL );
512         }
513
514 cleanup:
515         if( dn != NULL ) {
516                 free( dn );
517         }
518         if( ndn != NULL ) {
519                 free( ndn );
520         }
521         if ( mech != NULL ) {
522                 free( mech );
523         }
524         if ( cred.bv_val != NULL ) {
525                 free( cred.bv_val );
526         }
527
528         return rc;
529 }