]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Regarding previous commit:
[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-CRAM-MD5",
26         "X-DIGEST-MD5",
27         NULL
28 };
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            *cdn, *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         cdn = 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, &cdn, &method );
103
104         if ( tag == LBER_ERROR ) {
105                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
106                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
107                     "decoding error" );
108                 goto cleanup;
109         }
110
111         if( method != LDAP_AUTH_SASL ) {
112                 tag = ber_scanf( ber, /*{*/ "o}", &cred );
113
114         } else {
115                 tag = ber_scanf( ber, "{a" /*}*/, &mech );
116
117                 if ( tag != LBER_ERROR ) {
118                         ber_len_t len;
119                         tag = ber_peek_tag( ber, &len );
120
121                         if ( tag == LDAP_TAG_LDAPCRED ) { 
122                                 tag = ber_scanf( ber, "o", &cred );
123                         }
124
125                         if ( tag != LBER_ERROR ) {
126                                 tag = ber_scanf( ber, /*{{*/ "}}" );
127                         }
128                 }
129         }
130
131         if ( tag == LBER_ERROR ) {
132                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
133                 "decoding error" );
134                 goto cleanup;
135         }
136
137 #ifdef GET_CTRLS
138         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
139                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
140                 goto cleanup;
141         } 
142 #endif
143
144         if( method == LDAP_AUTH_SASL ) {
145                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
146                         cdn, mech, NULL );
147         } else {
148                 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
149                         version, cdn, method );
150         }
151
152         ndn = dn_normalize_case( ch_strdup( cdn ) );
153
154         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
155             conn->c_connid, op->o_opid, ndn, method, 0 );
156
157         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
158                 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
159                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
160                     "version not supported" );
161                 goto cleanup;
162         }
163
164         if ( method == LDAP_AUTH_SASL ) {
165                 if ( version < LDAP_VERSION3 ) {
166                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%d\n",
167                                 version, 0, 0 );
168                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
169                                 "sasl bind requires LDAPv3" );
170                         goto cleanup;
171                 }
172
173                 if( mech == NULL || *mech == '\0' ) {
174                         Debug( LDAP_DEBUG_ANY,
175                                 "do_bind: no sasl mechanism provided\n",
176                                 version, 0, 0 );
177                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
178                                 NULL, "no sasl mechanism provided" );
179                         goto cleanup;
180                 }
181
182                 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
183                         Debug( LDAP_DEBUG_ANY,
184                                 "do_bind: sasl mechanism \"%s\" not supported.\n",
185                                 mech, 0, 0 );
186                         send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
187                                 NULL, "sasl mechanism not supported" );
188                         goto cleanup;
189                 }
190
191                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
192
193                 if ( conn->c_authmech != NULL ) {
194                         assert( conn->c_bind_in_progress );
195
196                         if((strcmp(conn->c_authmech, mech) != 0)) {
197                                 /* mechanism changed, cancel in progress bind */
198                                 conn->c_bind_in_progress = 0;
199                                 if( conn->c_authstate != NULL ) {
200                                         free(conn->c_authstate);
201                                         conn->c_authstate = NULL;
202                                 }
203                                 free(conn->c_authmech);
204                                 conn->c_authmech = NULL;
205                         }
206
207 #ifdef LDAP_DEBUG
208                 } else {
209                         assert( !conn->c_bind_in_progress );
210                         assert( conn->c_authmech == NULL );
211                         assert( conn->c_authstate == NULL );
212 #endif
213                 }
214
215         } else {
216                 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
217
218                 if ( conn->c_authmech != NULL ) {
219                         assert( conn->c_bind_in_progress );
220
221                         /* cancel in progress bind */
222                         conn->c_bind_in_progress = 0;
223
224                         if( conn->c_authstate != NULL ) {
225                                 free(conn->c_authstate);
226                                 conn->c_authstate = NULL;
227                         }
228                         free(conn->c_authmech);
229                         conn->c_authmech = NULL;
230                 }
231         }
232
233         conn->c_protocol = version;
234         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
235
236         /* accept null binds */
237         if ( ndn == NULL || *ndn == '\0' ) {
238                 /*
239                  * we already forced connection to "anonymous", we just
240                  * need to send success
241                  */
242                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
243                 goto cleanup;
244         }
245
246         /*
247          * We could be serving multiple database backends.  Select the
248          * appropriate one, or send a referral to our "referral server"
249          * if we don't hold it.
250          */
251
252         if ( (be = select_backend( ndn )) == NULL ) {
253                 if ( cred.bv_len == 0 ) {
254                         send_ldap_result( conn, op, LDAP_SUCCESS,
255                                 NULL, NULL );
256
257                 } else if ( default_referral && *default_referral ) {
258                         send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS,
259                                 NULL, default_referral );
260
261                 } else {
262                         send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
263                                 NULL, default_referral );
264                 }
265
266                 goto cleanup;
267         }
268
269         if ( be->be_bind ) {
270                 /* alias suffix */
271                 char *edn;
272
273                 ndn = suffixAlias( ndn, op, be );
274
275                 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
276                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
277
278                         conn->c_cdn = cdn;
279                         cdn = NULL;
280
281                         if(edn != NULL) {
282                                 conn->c_dn = edn;
283                         } else {
284                                 conn->c_dn = ndn;
285                                 ndn = NULL;
286                         }
287
288                         Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
289                         conn->c_cdn, conn->c_dn, method );
290
291                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
292
293                         /* send this here to avoid a race condition */
294                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
295
296                 } else if (edn != NULL) {
297                         free( edn );
298                 }
299
300         } else {
301                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
302                     "Function not implemented" );
303         }
304
305 cleanup:
306         if( cdn != NULL ) {
307                 free( cdn );
308         }
309         if( ndn != NULL ) {
310                 free( ndn );
311         }
312         if ( mech != NULL ) {
313                 free( mech );
314         }
315         if ( cred.bv_val != NULL ) {
316                 free( cred.bv_val );
317         }
318
319         return rc;
320 }