]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
81192924fef9a26e6ff7291a9f2373a072d05fed
[openldap] / servers / slapd / back-ldbm / bind.c
1 /* bind.c - ldbm backend bind and unbind routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/krb.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16
17 #include "slap.h"
18 #include "back-ldbm.h"
19 #include "proto-back-ldbm.h"
20
21 int
22 ldbm_back_bind(
23     Operation           *op,
24     SlapReply           *rs )
25 {
26         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
27         Entry           *e;
28         Attribute       *a;
29         int             rc;
30         Entry           *matched;
31 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
32         char            krbname[MAX_K_NAME_SZ + 1];
33         AttributeDescription *krbattr = slap_schema.si_ad_krbName;
34         AUTH_DAT        ad;
35 #endif
36
37         AttributeDescription *password = slap_schema.si_ad_userPassword;
38
39 #ifdef NEW_LOGGING
40         LDAP_LOG( BACK_LDBM, ENTRY, 
41                 "ldbm_back_bind: dn: %s.\n", op->o_req_dn.bv_val, 0, 0 );
42 #else
43         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", op->o_req_dn.bv_val, 0, 0);
44 #endif
45
46         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
47                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
48                 /* front end will send result */
49                 return LDAP_SUCCESS;
50         }
51
52         /* grab giant lock for reading */
53         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
54
55         /* get entry with reader lock */
56         if ( (e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
57                 if( matched != NULL ) {
58                         rs->sr_matched = ch_strdup( matched->e_dn );
59
60                         rs->sr_ref = is_entry_referral( matched )
61                                 ? get_entry_referrals( op, matched )
62                                 : NULL;
63
64                         cache_return_entry_r( &li->li_cache, matched );
65
66                 } else {
67                         rs->sr_ref = referral_rewrite( default_referral,
68                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
69                 }
70
71                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
72
73                 /* allow noauth binds */
74                 rc = 1;
75                 if ( rs->sr_ref != NULL ) {
76                         rs->sr_err = LDAP_REFERRAL;
77                 } else {
78                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
79                 }
80                 send_ldap_result( op, rs );
81
82                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
83                 if ( rs->sr_matched ) free( (char *)rs->sr_matched );
84 #ifdef LDAP_SYNCREPL
85                 return rs->sr_err;
86 #else
87                 return( rc );
88 #endif
89         }
90
91         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
92
93         /* check for deleted */
94 #ifdef LDBM_SUBENTRIES
95         if ( is_entry_subentry( e ) ) {
96                 /* entry is an subentry, don't allow bind */
97 #ifdef NEW_LOGGING
98                 LDAP_LOG ( OPERATION, DETAIL1,
99                                 "bdb_bind: entry is subentry\n", 0, 0, 0 );
100 #else
101                 Debug( LDAP_DEBUG_TRACE,
102                                 "entry is subentry\n", 0, 0, 0 );
103 #endif
104                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
105                 send_ldap_result( op, rs );
106                 goto return_results;
107         }
108 #endif
109
110         if ( is_entry_alias( e ) ) {
111                 /* entry is an alias, don't allow bind */
112 #ifdef NEW_LOGGING
113                 LDAP_LOG( BACK_LDBM, INFO, 
114                         "ldbm_back_bind: entry (%s) is an alias.\n", e->e_name.bv_val, 0, 0 );
115 #else
116                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
117                     0, 0 );
118 #endif
119
120
121                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
122                     "entry is alias" );
123
124 #ifdef LDAP_SYNCREPL
125                 rc = LDAP_ALIAS_PROBLEM;
126 #else
127                 rc = 1;
128 #endif
129                 goto return_results;
130         }
131
132         if ( is_entry_referral( e ) ) {
133                 /* entry is a referral, don't allow bind */
134                 rs->sr_ref = get_entry_referrals( op, e );
135
136 #ifdef NEW_LOGGING
137                 LDAP_LOG( BACK_LDBM, INFO, 
138                            "ldbm_back_bind: entry(%s) is a referral.\n", e->e_dn, 0, 0 );
139 #else
140                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
141                     0, 0 );
142 #endif
143
144
145                 if( rs->sr_ref != NULL ) {
146                         rs->sr_err = LDAP_REFERRAL;
147                         rs->sr_matched = e->e_name.bv_val;
148
149                 } else {
150                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
151                 }
152                 send_ldap_result( op, rs );
153
154                 ber_bvarray_free( rs->sr_ref );
155
156 #ifdef LDAP_SYNCREPL
157                 rc = rs->sr_err;
158 #else
159                 rc = 1;
160 #endif
161                 goto return_results;
162         }
163
164         switch ( op->oq_bind.rb_method ) {
165         case LDAP_AUTH_SIMPLE:
166                 if ( ! access_allowed( op, e,
167                         password, NULL, ACL_AUTH, NULL ) )
168                 {
169                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
170 #ifdef LDAP_SYNCREPL
171                         rc = LDAP_INSUFFICIENT_ACCESS;
172 #else
173                         rc = 1;
174 #endif
175                         goto return_results;
176                 }
177
178                 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
179                         send_ldap_error( op, rs, LDAP_INAPPROPRIATE_AUTH, NULL );
180
181                         /* stop front end from sending result */
182 #ifdef LDAP_SYNCREPL
183                         rc = LDAP_INAPPROPRIATE_AUTH;
184 #else
185                         rc = 1;
186 #endif
187                         goto return_results;
188                 }
189
190                 if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 ) {
191                         send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
192                         /* stop front end from sending result */
193 #ifdef LDAP_SYNCREPL
194                         rc = LDAP_INVALID_CREDENTIALS;
195 #else
196                         rc = 1;
197 #endif
198                         goto return_results;
199                 }
200
201                 rc = 0;
202                 break;
203
204 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
205         case LDAP_AUTH_KRBV41:
206                 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad ) != LDAP_SUCCESS ) {
207                         send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
208 #ifdef LDAP_SYNCREPL
209                         rc = LDAP_INVALID_CREDENTIALS;
210 #else
211                         rc = 1;
212 #endif
213                         goto return_results;
214                 }
215
216                 if ( ! access_allowed( op, e,
217                         krbattr, NULL, ACL_AUTH, NULL ) )
218                 {
219                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
220                                 NULL );
221 #ifdef LDAP_SYNCREPL
222                         rc = LDAP_INSUFFICIENT_ACCESS;
223 #else
224                         rc = 1;
225 #endif
226                         goto return_results;
227                 }
228
229                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
230                     : "", ad.pinst, ad.prealm );
231
232                 if ( (a = attr_find( e->e_attrs, krbattr )) == NULL ) {
233                         /*
234                          * no krbname values present:  check against DN
235                          */
236                         if ( strcasecmp( op->o_req_dn.bv_val, krbname ) == 0 ) {
237                                 rc = 0;
238                                 break;
239                         }
240                         send_ldap_error( op, rs, LDAP_INAPPROPRIATE_AUTH, NULL );
241 #ifdef LDAP_SYNCREPL
242                         rc = LDAP_INAPPROPRIATE_AUTH;
243 #else
244                         rc = 1;
245 #endif
246                         goto return_results;
247
248                 } else {        /* look for krbname match */
249                         struct berval   krbval;
250
251                         krbval.bv_val = krbname;
252                         krbval.bv_len = strlen( krbname );
253
254                         if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
255                                 send_ldap_error( op, rs,
256                                     LDAP_INVALID_CREDENTIALS, NULL );
257 #ifdef LDAP_SYNCREPL
258                                 rc = LDAP_INVALID_CREDENTIALS;
259 #else
260                                 rc = 1;
261 #endif
262                                 goto return_results;
263                         }
264                 }
265                 rc = 0;
266                 break;
267
268         case LDAP_AUTH_KRBV42:
269                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
270                         "Kerberos bind step 2 not supported" );
271                 /* stop front end from sending result */
272                 rc = LDAP_UNWILLING_TO_PERFORM;
273                 goto return_results;
274 #endif
275
276         default:
277                 send_ldap_error( op, rs, LDAP_STRONG_AUTH_NOT_SUPPORTED,
278                     "authentication method not supported" );
279 #ifdef LDAP_SYNCREPL
280                 rc = LDAP_STRONG_AUTH_NOT_SUPPORTED;
281 #else
282                 rc = 1;
283 #endif
284                 goto return_results;
285         }
286
287 return_results:;
288         /* free entry and reader lock */
289         cache_return_entry_r( &li->li_cache, e );
290         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
291
292         /* front end will send result on success (rc==0) */
293         return( rc );
294 }
295