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