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