]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Update slap_conn to maintain client provided dn and bound dn.
[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                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
139                 return;
140         }
141
142         /*
143          * We could be serving multiple database backends.  Select the
144          * appropriate one, or send a referral to our "referral server"
145          * if we don't hold it.
146          */
147
148         if ( (be = select_backend( ndn )) == NULL ) {
149                 free( cdn );
150                 free( ndn );
151                 if ( cred.bv_val != NULL ) {
152                         free( cred.bv_val );
153                 }
154                 if ( cred.bv_len == 0 ) {
155                         send_ldap_result( conn, op, LDAP_SUCCESS,
156                                 NULL, NULL );
157                 } else if ( default_referral && *default_referral ) {
158                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS,
159                                 NULL, default_referral );
160                 } else {
161                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
162                                 NULL, default_referral );
163                 }
164                 return;
165         }
166
167         if ( be->be_bind != NULL ) {
168                 /* alias suffix */
169                 char *edn;
170
171                 ndn = suffixAlias( ndn, op, be );
172                 dn_normalize_case( ndn );
173
174                 if ( (*be->be_bind)( be, conn, op, ndn, method, &cred, &edn ) == 0 ) {
175                         pthread_mutex_lock( &conn->c_dnmutex );
176
177                         if ( conn->c_cdn != NULL ) {
178                                 free( conn->c_cdn );
179                         }
180
181                         conn->c_cdn = cdn;
182                         cdn = NULL;
183
184                         if ( conn->c_cdn != NULL ) {
185                                 free( conn->c_dn );
186                         }
187
188                         if(edn != NULL) {
189                                 conn->c_dn = edn;
190                         } else {
191                                 conn->c_dn = ndn;
192                                 ndn = NULL;
193                         }
194
195                         Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
196                         conn->c_cdn, conn->c_dn, method );
197
198                         pthread_mutex_unlock( &conn->c_dnmutex );
199
200                         /* send this here to avoid a race condition */
201                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
202
203                 } else if (edn != NULL) {
204                         free( edn );
205                 }
206
207         } else {
208                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
209                     "Function not implemented" );
210         }
211
212         if( cdn != NULL ) {
213                 free( cdn );
214         }
215         if( ndn != NULL ) {
216                 free( ndn );
217         }
218         if ( cred.bv_val != NULL ) {
219                 free( cred.bv_val );
220         }
221 }