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