]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Rework error handling. Add error descriptions.
[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-1999 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         char    *text;
45         struct berval   cred;
46         Backend         *be;
47
48         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
49
50         dn = NULL;
51         ndn = NULL;
52         mech = NULL;
53         cred.bv_val = NULL;
54
55         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
56
57         /* Force to connection to "anonymous" until bind succeeds.
58          * This may need to be relocated or done on a case by case basis
59          * to handle certain SASL mechanisms.
60          */
61
62         if ( conn->c_cdn != NULL ) {
63                 free( conn->c_cdn );
64                 conn->c_cdn = NULL;
65         }
66
67         if ( conn->c_dn != NULL ) {
68                 free( conn->c_dn );
69                 conn->c_dn = NULL;
70         }
71
72         conn->c_authz_backend = NULL;
73
74         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
75
76         if ( op->o_dn != NULL ) {
77                 free( op->o_dn );
78                 op->o_dn = ch_strdup( "" );
79         }
80
81         if ( op->o_ndn != NULL ) {
82                 free( op->o_ndn );
83                 op->o_ndn = ch_strdup( "" );
84         }
85
86         /*
87          * Parse the bind request.  It looks like this:
88          *
89          *      BindRequest ::= SEQUENCE {
90          *              version         INTEGER,                 -- version
91          *              name            DistinguishedName,       -- dn
92          *              authentication  CHOICE {
93          *                      simple          [0] OCTET STRING -- passwd
94          *                      krbv42ldap      [1] OCTET STRING
95          *                      krbv42dsa       [2] OCTET STRING
96          *                      SASL            [3] SaslCredentials
97          *              }
98          *      }
99          *
100          *      SaslCredentials ::= SEQUENCE {
101      *          mechanism           LDAPString,
102      *          credentials         OCTET STRING OPTIONAL
103          *      }
104          */
105
106         tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
107
108         if ( tag == LBER_ERROR ) {
109                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
110                 send_ldap_disconnect( conn, op,
111                         LDAP_PROTOCOL_ERROR, "decoding error" );
112                 rc = -1;
113                 goto cleanup;
114         }
115
116         op->o_protocol = version;
117
118         if( method != LDAP_AUTH_SASL ) {
119                 tag = ber_scanf( ber, /*{*/ "o}", &cred );
120
121         } else {
122                 tag = ber_scanf( ber, "{a" /*}*/, &mech );
123
124                 if ( tag != LBER_ERROR ) {
125                         ber_len_t len;
126                         tag = ber_peek_tag( ber, &len );
127
128                         if ( tag == LDAP_TAG_LDAPCRED ) { 
129                                 tag = ber_scanf( ber, "o", &cred );
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                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
148                 goto cleanup;
149         } 
150
151         ndn = ch_strdup( dn );
152
153         if ( dn_normalize( ndn ) == NULL ) {
154                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
155                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
156                     "invalid DN", NULL, NULL );
157                 goto cleanup;
158         }
159
160         if( method == LDAP_AUTH_SASL ) {
161                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
162                         dn, mech, NULL );
163         } else {
164                 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
165                         (unsigned long) version, dn, (unsigned long) method );
166         }
167
168         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
169             op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
170
171         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
172                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
173                         (unsigned long) version, 0, 0 );
174                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
175                         NULL, "version not supported", NULL, NULL );
176                 goto cleanup;
177         }
178
179         if ( method == LDAP_AUTH_SASL ) {
180                 if ( version < LDAP_VERSION3 ) {
181                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
182                                 (unsigned long) version, 0, 0 );
183                         send_ldap_disconnect( conn, op,
184                                 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
185                         rc = SLAPD_DISCONNECT;
186                         goto cleanup;
187                 }
188
189                 if( mech == NULL || *mech == '\0' ) {
190                         Debug( LDAP_DEBUG_ANY,
191                                 "do_bind: no sasl mechanism provided\n",
192                                 0, 0, 0 );
193                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
194                                 NULL, "no sasl mechanism provided", NULL, NULL );
195                         goto cleanup;
196                 }
197
198                 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
199                         Debug( LDAP_DEBUG_ANY,
200                                 "do_bind: sasl mechanism=\"%s\" not supported.\n",
201                                 mech, 0, 0 );
202                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
203                                 NULL, "sasl mechanism not supported", NULL, NULL );
204                         goto cleanup;
205                 }
206
207                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
208
209                 if ( conn->c_authmech != NULL ) {
210                         assert( conn->c_bind_in_progress );
211
212                         if((strcmp(conn->c_authmech, mech) != 0)) {
213                                 /* mechanism changed, cancel in progress bind */
214                                 conn->c_bind_in_progress = 0;
215                                 if( conn->c_authstate != NULL ) {
216                                         free(conn->c_authstate);
217                                         conn->c_authstate = NULL;
218                                 }
219                                 free(conn->c_authmech);
220                                 conn->c_authmech = NULL;
221                         }
222
223 #ifdef LDAP_DEBUG
224                 } else {
225                         assert( !conn->c_bind_in_progress );
226                         assert( conn->c_authmech == NULL );
227                         assert( conn->c_authstate == NULL );
228 #endif
229                 }
230         } else {
231                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
232
233                 if ( conn->c_authmech != NULL ) {
234                         assert( conn->c_bind_in_progress );
235
236                         /* cancel in progress bind */
237                         conn->c_bind_in_progress = 0;
238
239                         if( conn->c_authstate != NULL ) {
240                                 free(conn->c_authstate);
241                                 conn->c_authstate = NULL;
242                         }
243                         free(conn->c_authmech);
244                         conn->c_authmech = NULL;
245                 }
246         }
247
248         conn->c_protocol = version;
249         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
250
251         /* accept null binds */
252         if ( ndn == NULL || *ndn == '\0' ) {
253                 /*
254                  * we already forced connection to "anonymous", we just
255                  * need to send success
256                  */
257                 send_ldap_result( conn, op, LDAP_SUCCESS,
258                         NULL, NULL, NULL, NULL );
259                 goto cleanup;
260         }
261
262         /*
263          * We could be serving multiple database backends.  Select the
264          * appropriate one, or send a referral to our "referral server"
265          * if we don't hold it.
266          */
267
268         if ( (be = select_backend( ndn )) == NULL ) {
269                 if ( cred.bv_len == 0 ) {
270                         send_ldap_result( conn, op, LDAP_SUCCESS,
271                                 NULL, NULL, NULL, NULL );
272
273                 } else if ( default_referral ) {
274                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
275                                 NULL, NULL, default_referral, NULL );
276
277                 } else {
278                         /* noSuchObject is not allowed to be returned by bind */
279                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
280                                 NULL, NULL, NULL, NULL );
281                 }
282
283                 goto cleanup;
284         }
285
286         conn->c_authz_backend = be;
287
288         /* make sure this backend recongizes critical controls */
289         rc = backend_check_controls( be, conn, op, &text ) ;
290
291         if( rc != LDAP_SUCCESS ) {
292                 send_ldap_result( conn, op, rc,
293                         NULL, text, NULL, NULL );
294                 goto cleanup;
295         }
296
297         if ( be->be_bind ) {
298                 int ret;
299                 /* alias suffix */
300                 char *edn = NULL;
301
302                 /* deref suffix alias if appropriate */
303                 ndn = suffix_alias( be, ndn );
304
305                 ret = (*be->be_bind)( be, conn, op, dn, ndn,
306                         method, mech, &cred, &edn );
307
308                 if ( ret == 0 ) {
309 #ifdef HAVE_CYRUS_SASL
310                         int ssf = 0;
311 #endif
312
313                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
314
315                         conn->c_cdn = dn;
316                         dn = NULL;
317
318                         if(edn != NULL) {
319                                 conn->c_dn = edn;
320                         } else {
321                                 conn->c_dn = ndn;
322                                 ndn = NULL;
323                         }
324
325                         Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
326                         conn->c_cdn, conn->c_dn, method );
327
328                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
329
330                         /* send this here to avoid a race condition */
331                         send_ldap_result( conn, op, LDAP_SUCCESS,
332                                 NULL, NULL, NULL, NULL );
333
334 #ifdef HAVE_CYRUS_SASL
335                         if ( conn->c_sasl_context != NULL && 
336                                 sasl_getprop( conn->c_sasl_context, SASL_SSF, (void **)&ssf )
337                                         == SASL_OK && ssf ) {
338                                 /* Enable encode/decode */
339                                 ldap_pvt_sasl_install( conn->c_sb, conn->c_sasl_context );
340                         }
341 #endif
342                 } else if (edn != NULL) {
343                         free( edn );
344                 }
345
346         } else {
347                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
348                         NULL, "Function not implemented", NULL, NULL );
349         }
350
351 cleanup:
352         if( dn != NULL ) {
353                 free( dn );
354         }
355         if( ndn != NULL ) {
356                 free( ndn );
357         }
358         if ( mech != NULL ) {
359                 free( mech );
360         }
361         if ( cred.bv_val != NULL ) {
362                 free( cred.bv_val );
363         }
364
365         return rc;
366 }