]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
Cleanup result handling
[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
69                         rs->sr_ref = is_entry_referral( matched )
70                                 ? get_entry_referrals( op, matched )
71                                 : NULL;
72
73                         cache_return_entry_r( &li->li_cache, matched );
74
75                 } else {
76                         rs->sr_ref = referral_rewrite( default_referral,
77                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
78                 }
79
80                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
81
82                 /* allow noauth binds */
83                 rc = 1;
84                 if ( rs->sr_ref != NULL ) {
85                         rs->sr_err = LDAP_REFERRAL;
86                 } else {
87                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
88                 }
89                 send_ldap_result( op, rs );
90
91                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
92                 if ( rs->sr_matched ) free( (char *)rs->sr_matched );
93                 rs->sr_ref = NULL;
94                 rs->sr_matched = NULL;
95                 return rs->sr_err;
96         }
97
98         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
99
100         /* check for deleted */
101 #ifdef LDBM_SUBENTRIES
102         if ( is_entry_subentry( e ) ) {
103                 /* entry is an subentry, don't allow bind */
104 #ifdef NEW_LOGGING
105                 LDAP_LOG ( OPERATION, DETAIL1,
106                                 "bdb_bind: entry is subentry\n", 0, 0, 0 );
107 #else
108                 Debug( LDAP_DEBUG_TRACE,
109                                 "entry is subentry\n", 0, 0, 0 );
110 #endif
111                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
112                 send_ldap_result( op, rs );
113                 rc = LDAP_INVALID_CREDENTIALS;
114                 goto return_results;
115         }
116 #endif
117
118         if ( is_entry_alias( e ) ) {
119                 /* entry is an alias, don't allow bind */
120 #ifdef NEW_LOGGING
121                 LDAP_LOG( BACK_LDBM, INFO, 
122                         "ldbm_back_bind: entry (%s) is an alias.\n", e->e_name.bv_val, 0, 0 );
123 #else
124                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
125                     0, 0 );
126 #endif
127
128
129                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
130                     "entry is alias" );
131
132                 rc = LDAP_ALIAS_PROBLEM;
133                 goto return_results;
134         }
135
136         if ( is_entry_referral( e ) ) {
137                 /* entry is a referral, don't allow bind */
138                 rs->sr_ref = get_entry_referrals( op, e );
139
140 #ifdef NEW_LOGGING
141                 LDAP_LOG( BACK_LDBM, INFO, 
142                            "ldbm_back_bind: entry(%s) is a referral.\n", e->e_dn, 0, 0 );
143 #else
144                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
145                     0, 0 );
146 #endif
147
148
149                 if( rs->sr_ref != NULL ) {
150                         rs->sr_err = LDAP_REFERRAL;
151                         rs->sr_matched = e->e_name.bv_val;
152
153                 } else {
154                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
155                 }
156                 send_ldap_result( op, rs );
157
158                 ber_bvarray_free( rs->sr_ref );
159                 rs->sr_matched = NULL;
160                 rs->sr_ref = NULL;
161                 rc = rs->sr_err;
162                 goto return_results;
163         }
164
165         switch ( op->oq_bind.rb_method ) {
166         case LDAP_AUTH_SIMPLE:
167                 if ( ! access_allowed( op, e,
168                         password, NULL, ACL_AUTH, NULL ) )
169                 {
170                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
171                         rc = LDAP_INSUFFICIENT_ACCESS;
172                         goto return_results;
173                 }
174
175                 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
176                         send_ldap_error( op, rs, LDAP_INAPPROPRIATE_AUTH, NULL );
177
178                         /* stop front end from sending result */
179                         rc = LDAP_INAPPROPRIATE_AUTH;
180                         goto return_results;
181                 }
182
183                 if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 ) {
184                         send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
185                         /* stop front end from sending result */
186                         rc = LDAP_INVALID_CREDENTIALS;
187                         goto return_results;
188                 }
189
190                 rc = 0;
191                 break;
192
193 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
194         case LDAP_AUTH_KRBV41:
195                 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad ) != LDAP_SUCCESS ) {
196                         send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
197                         rc = LDAP_INVALID_CREDENTIALS;
198                         goto return_results;
199                 }
200
201                 if ( ! access_allowed( op, e,
202                         krbattr, NULL, ACL_AUTH, NULL ) )
203                 {
204                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
205                                 NULL );
206                         rc = LDAP_INSUFFICIENT_ACCESS;
207                         goto return_results;
208                 }
209
210                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
211                     : "", ad.pinst, ad.prealm );
212
213                 if ( (a = attr_find( e->e_attrs, krbattr )) == NULL ) {
214                         /*
215                          * no krbname values present:  check against DN
216                          */
217                         if ( strcasecmp( op->o_req_dn.bv_val, krbname ) == 0 ) {
218                                 rc = 0;
219                                 break;
220                         }
221                         send_ldap_error( op, rs, LDAP_INAPPROPRIATE_AUTH, NULL );
222                         rc = LDAP_INAPPROPRIATE_AUTH;
223                         goto return_results;
224
225                 } else {        /* look for krbname match */
226                         struct berval   krbval;
227
228                         krbval.bv_val = krbname;
229                         krbval.bv_len = strlen( krbname );
230
231                         if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
232                                 send_ldap_error( op, rs,
233                                     LDAP_INVALID_CREDENTIALS, NULL );
234                                 rc = LDAP_INVALID_CREDENTIALS;
235                                 goto return_results;
236                         }
237                 }
238                 rc = 0;
239                 break;
240
241         case LDAP_AUTH_KRBV42:
242                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
243                         "Kerberos bind step 2 not supported" );
244                 /* stop front end from sending result */
245                 rc = LDAP_UNWILLING_TO_PERFORM;
246                 goto return_results;
247 #endif
248
249         default:
250                 send_ldap_error( op, rs, LDAP_STRONG_AUTH_NOT_SUPPORTED,
251                     "authentication method not supported" );
252                 rc = LDAP_STRONG_AUTH_NOT_SUPPORTED;
253                 goto return_results;
254         }
255
256 return_results:;
257         /* free entry and reader lock */
258         cache_return_entry_r( &li->li_cache, e );
259         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
260
261         /* front end will send result on success (rc==0) */
262         return( rc );
263 }
264