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