]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Merge in all -devel changes made since branch was created.
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  * Copyright (c) 1995 Regents of the University of Michigan.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms are permitted
12  * provided that this notice is preserved and that due credit is given
13  * to the University of Michigan at Ann Arbor. The name of the University
14  * may not be used to endorse or promote products derived from this
15  * software without specific prior written permission. This software
16  * is provided ``as is'' without express or implied warranty.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/string.h>
24 #include <ac/socket.h>
25
26 #include "slap.h"
27
28 int
29 do_bind(
30     Connection  *conn,
31     Operation   *op
32 )
33 {
34         BerElement      *ber = op->o_ber;
35         ber_int_t               version;
36         ber_tag_t method;
37         char            *mech;
38         char            *dn, *ndn;
39         ber_tag_t       tag;
40         int                     rc = LDAP_SUCCESS;
41         struct berval   cred;
42         Backend         *be;
43
44         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
45
46         dn = NULL;
47         ndn = NULL;
48         mech = NULL;
49         cred.bv_val = NULL;
50
51         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
52
53         /* Force to connection to "anonymous" until bind succeeds.
54          * This may need to be relocated or done on a case by case basis
55          * to handle certain SASL mechanisms.
56          */
57
58         if ( conn->c_cdn != NULL ) {
59                 free( conn->c_cdn );
60                 conn->c_cdn = NULL;
61         }
62
63         if ( conn->c_dn != NULL ) {
64                 free( conn->c_dn );
65                 conn->c_dn = NULL;
66         }
67
68         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
69
70         if ( op->o_dn != NULL ) {
71                 free( op->o_dn );
72                 op->o_dn = ch_strdup( "" );
73         }
74
75         if ( op->o_ndn != NULL ) {
76                 free( op->o_ndn );
77                 op->o_ndn = ch_strdup( "" );
78         }
79
80         /*
81          * Parse the bind request.  It looks like this:
82          *
83          *      BindRequest ::= SEQUENCE {
84          *              version         INTEGER,                 -- version
85          *              name            DistinguishedName,       -- dn
86          *              authentication  CHOICE {
87          *                      simple          [0] OCTET STRING -- passwd
88          *                      krbv42ldap      [1] OCTET STRING
89          *                      krbv42dsa       [2] OCTET STRING
90          *                      SASL            [3] SaslCredentials
91          *              }
92          *      }
93          *
94          *      SaslCredentials ::= SEQUENCE {
95      *          mechanism           LDAPString,
96      *          credentials         OCTET STRING OPTIONAL
97          *      }
98          */
99
100         tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
101
102         if ( tag == LBER_ERROR ) {
103                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
104                 send_ldap_disconnect( conn, op,
105                         LDAP_PROTOCOL_ERROR, "decoding error" );
106                 rc = -1;
107                 goto cleanup;
108         }
109
110         ndn = ch_strdup( dn );
111
112         if ( dn_normalize_case( ndn ) == NULL ) {
113                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
114                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
115                     "invalid DN", NULL, NULL );
116                 goto cleanup;
117         }
118
119         op->o_protocol = version;
120
121         if( method != LDAP_AUTH_SASL ) {
122                 tag = ber_scanf( ber, /*{*/ "o}", &cred );
123
124         } else {
125                 tag = ber_scanf( ber, "{a" /*}*/, &mech );
126
127                 if ( tag != LBER_ERROR ) {
128                         ber_len_t len;
129                         tag = ber_peek_tag( ber, &len );
130
131                         if ( tag == LDAP_TAG_LDAPCRED ) { 
132                                 tag = ber_scanf( ber, "o", &cred );
133                         }
134
135                         if ( tag != LBER_ERROR ) {
136                                 tag = ber_scanf( ber, /*{{*/ "}}" );
137                         }
138                 }
139         }
140
141         if ( tag == LBER_ERROR ) {
142                 send_ldap_disconnect( conn, op,
143                         LDAP_PROTOCOL_ERROR,
144                 "decoding error" );
145                 rc = -1;
146                 goto cleanup;
147         }
148
149         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
150                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
151                 goto cleanup;
152         } 
153
154         if( method == LDAP_AUTH_SASL ) {
155                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
156                         dn, mech, NULL );
157         } else {
158                 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
159                         (unsigned long) version, dn, (unsigned long) method );
160         }
161
162         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
163             op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
164
165         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
166                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
167                         (unsigned long) version, 0, 0 );
168                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
169                         NULL, "version not supported", NULL, NULL );
170                 goto cleanup;
171         }
172
173         if ( method == LDAP_AUTH_SASL ) {
174                 if ( version < LDAP_VERSION3 ) {
175                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
176                                 (unsigned long) version, 0, 0 );
177                         send_ldap_disconnect( conn, op,
178                                 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
179                         rc = -1;
180                         goto cleanup;
181                 }
182
183                 if( mech == NULL || *mech == '\0' ) {
184                         Debug( LDAP_DEBUG_ANY,
185                                 "do_bind: no sasl mechanism provided\n",
186                                 0, 0, 0 );
187                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
188                                 NULL, "no sasl mechanism provided", NULL, NULL );
189                         goto cleanup;
190                 }
191
192                 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
193                         Debug( LDAP_DEBUG_ANY,
194                                 "do_bind: sasl mechanism=\"%s\" not supported.\n",
195                                 mech, 0, 0 );
196                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
197                                 NULL, "sasl mechanism not supported", NULL, NULL );
198                         goto cleanup;
199                 }
200
201                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
202
203                 if ( conn->c_authmech != NULL ) {
204                         assert( conn->c_bind_in_progress );
205
206                         if((strcmp(conn->c_authmech, mech) != 0)) {
207                                 /* mechanism changed, cancel in progress bind */
208                                 conn->c_bind_in_progress = 0;
209                                 if( conn->c_authstate != NULL ) {
210                                         free(conn->c_authstate);
211                                         conn->c_authstate = NULL;
212                                 }
213                                 free(conn->c_authmech);
214                                 conn->c_authmech = NULL;
215                         }
216
217 #ifdef LDAP_DEBUG
218                 } else {
219                         assert( !conn->c_bind_in_progress );
220                         assert( conn->c_authmech == NULL );
221                         assert( conn->c_authstate == NULL );
222 #endif
223                 }
224
225         } else {
226                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
227
228                 if ( conn->c_authmech != NULL ) {
229                         assert( conn->c_bind_in_progress );
230
231                         /* cancel in progress bind */
232                         conn->c_bind_in_progress = 0;
233
234                         if( conn->c_authstate != NULL ) {
235                                 free(conn->c_authstate);
236                                 conn->c_authstate = NULL;
237                         }
238                         free(conn->c_authmech);
239                         conn->c_authmech = NULL;
240                 }
241         }
242
243         conn->c_protocol = version;
244         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
245
246         /* accept null binds */
247         if ( ndn == NULL || *ndn == '\0' ) {
248                 /*
249                  * we already forced connection to "anonymous", we just
250                  * need to send success
251                  */
252                 send_ldap_result( conn, op, LDAP_SUCCESS,
253                         NULL, NULL, NULL, NULL );
254                 goto cleanup;
255         }
256
257         /*
258          * We could be serving multiple database backends.  Select the
259          * appropriate one, or send a referral to our "referral server"
260          * if we don't hold it.
261          */
262
263         if ( (be = select_backend( ndn )) == NULL ) {
264                 if ( cred.bv_len == 0 ) {
265                         send_ldap_result( conn, op, LDAP_SUCCESS,
266                                 NULL, NULL, NULL, NULL );
267
268                 } else if ( default_referral ) {
269                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
270                                 NULL, NULL, default_referral, NULL );
271
272                 } else {
273                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
274                                 NULL, NULL, NULL, NULL );
275                 }
276
277                 goto cleanup;
278         }
279
280         if ( be->be_bind ) {
281                 /* alias suffix */
282                 char *edn;
283
284                 /* deref suffix alias if appropriate */
285                 ndn = suffix_alias( be, ndn );
286
287                 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
288                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
289
290                         conn->c_cdn = dn;
291                         dn = NULL;
292
293                         if(edn != NULL) {
294                                 conn->c_dn = edn;
295                         } else {
296                                 conn->c_dn = ndn;
297                                 ndn = NULL;
298                         }
299
300                         Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
301                         conn->c_cdn, conn->c_dn, method );
302
303                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
304
305                         /* send this here to avoid a race condition */
306                         send_ldap_result( conn, op, LDAP_SUCCESS,
307                                 NULL, NULL, NULL, NULL );
308
309                 } else if (edn != NULL) {
310                         free( edn );
311                 }
312
313         } else {
314                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
315                         NULL, "Function not implemented", NULL, NULL );
316         }
317
318 cleanup:
319         if( dn != NULL ) {
320                 free( dn );
321         }
322         if( ndn != NULL ) {
323                 free( ndn );
324         }
325         if ( mech != NULL ) {
326                 free( mech );
327         }
328         if ( cred.bv_val != NULL ) {
329                 free( cred.bv_val );
330         }
331
332         return rc;
333 }