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