]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Add limited LDAP_INVALID_DN_SYNTAX support. dn_normalize{,_case}() now returns
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2
3 /*
4  * Copyright (c) 1995 Regents of the University of Michigan.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that this notice is preserved and that due credit is given
9  * to the University of Michigan at Ann Arbor. The name of the University
10  * may not be used to endorse or promote products derived from this
11  * software without specific prior written permission. This software
12  * is provided ``as is'' without express or implied warranty.
13  */
14
15 #include "portable.h"
16
17 #include <stdio.h>
18
19 #include <ac/string.h>
20 #include <ac/socket.h>
21
22 #include "slap.h"
23
24 char *supportedSASLMechanisms[] = {
25         "X-DIGEST-MD5",
26         NULL
27 };
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, *ndn;
40         ber_tag_t       tag;
41         int                     rc = LDAP_SUCCESS;
42         struct berval   cred;
43         Backend         *be;
44
45         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
46
47         dn = NULL;
48         ndn = NULL;
49         mech = NULL;
50         cred.bv_val = NULL;
51
52         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
53
54         /* Force to connection to "anonymous" until bind succeeds.
55          * This may need to be relocated or done on a case by case basis
56          * to handle certain SASL mechanisms.
57          */
58
59         if ( conn->c_cdn != NULL ) {
60                 free( conn->c_cdn );
61                 conn->c_cdn = NULL;
62         }
63
64         if ( conn->c_dn != NULL ) {
65                 free( conn->c_dn );
66                 conn->c_dn = NULL;
67         }
68
69         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
70
71         if ( op->o_dn != NULL ) {
72                 free( op->o_dn );
73                 op->o_dn = ch_strdup( "" );
74         }
75
76         if ( op->o_ndn != NULL ) {
77                 free( op->o_ndn );
78                 op->o_ndn = ch_strdup( "" );
79         }
80
81         /*
82          * Parse the bind request.  It looks like this:
83          *
84          *      BindRequest ::= SEQUENCE {
85          *              version         INTEGER,                 -- version
86          *              name            DistinguishedName,       -- dn
87          *              authentication  CHOICE {
88          *                      simple          [0] OCTET STRING -- passwd
89          *                      krbv42ldap      [1] OCTET STRING
90          *                      krbv42dsa       [2] OCTET STRING
91          *                      SASL            [3] SaslCredentials
92          *              }
93          *      }
94          *
95          *      SaslCredentials ::= SEQUENCE {
96      *          mechanism           LDAPString,
97      *          credentials         OCTET STRING OPTIONAL
98          *      }
99          */
100
101         tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
102
103         if ( tag == LBER_ERROR ) {
104                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
105                 send_ldap_disconnect( conn, op,
106                         LDAP_PROTOCOL_ERROR, "decoding error" );
107                 rc = -1;
108                 goto cleanup;
109         }
110
111         ndn = ch_strdup( dn );
112
113         if ( dn_normalize_case( ndn ) == NULL ) {
114                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
115                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
116                     "invalid DN", NULL, NULL );
117                 goto cleanup;
118         }
119
120         op->o_protocol = version;
121
122         if( method != LDAP_AUTH_SASL ) {
123                 tag = ber_scanf( ber, /*{*/ "o}", &cred );
124
125         } else {
126                 tag = ber_scanf( ber, "{a" /*}*/, &mech );
127
128                 if ( tag != LBER_ERROR ) {
129                         ber_len_t len;
130                         tag = ber_peek_tag( ber, &len );
131
132                         if ( tag == LDAP_TAG_LDAPCRED ) { 
133                                 tag = ber_scanf( ber, "o", &cred );
134                         }
135
136                         if ( tag != LBER_ERROR ) {
137                                 tag = ber_scanf( ber, /*{{*/ "}}" );
138                         }
139                 }
140         }
141
142         if ( tag == LBER_ERROR ) {
143                 send_ldap_disconnect( conn, op,
144                         LDAP_PROTOCOL_ERROR,
145                 "decoding error" );
146                 rc = -1;
147                 goto cleanup;
148         }
149
150         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
151                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
152                 goto cleanup;
153         } 
154
155         if( method == LDAP_AUTH_SASL ) {
156                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
157                         dn, mech, NULL );
158         } else {
159                 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
160                         version, dn, method );
161         }
162
163         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
164             op->o_connid, op->o_opid, ndn, method, 0 );
165
166         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
167                 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", 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%d\n",
176                                 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                                 version, 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                 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
285                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
286
287                         conn->c_cdn = dn;
288                         dn = NULL;
289
290                         if(edn != NULL) {
291                                 conn->c_dn = edn;
292                         } else {
293                                 conn->c_dn = ndn;
294                                 ndn = NULL;
295                         }
296
297                         Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
298                         conn->c_cdn, conn->c_dn, method );
299
300                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
301
302                         /* send this here to avoid a race condition */
303                         send_ldap_result( conn, op, LDAP_SUCCESS,
304                                 NULL, NULL, NULL, NULL );
305
306                 } else if (edn != NULL) {
307                         free( edn );
308                 }
309
310         } else {
311                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
312                         NULL, "Function not implemented", NULL, NULL );
313         }
314
315 cleanup:
316         if( dn != NULL ) {
317                 free( dn );
318         }
319         if( ndn != NULL ) {
320                 free( ndn );
321         }
322         if ( mech != NULL ) {
323                 free( mech );
324         }
325         if ( cred.bv_val != NULL ) {
326                 free( cred.bv_val );
327         }
328
329         return rc;
330 }