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