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