]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/bind.c
Remove broken MSVC build from REL_ENG branch.
[openldap] / servers / slapd / back-bdb / bind.c
1 /* bind.c - bdb backend bind routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2003 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 #include <ac/krb.h>
21 #include <ac/string.h>
22 #include <ac/unistd.h>
23
24 #include "back-bdb.h"
25 #include "external.h"
26
27 int
28 bdb_bind( Operation *op, SlapReply *rs )
29 {
30         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
31         Entry           *e;
32         Attribute       *a;
33         EntryInfo       *ei;
34 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
35         char            krbname[MAX_K_NAME_SZ + 1];
36         AttributeDescription *krbattr = slap_schema.si_ad_krbName;
37         struct berval   krbval;
38         AUTH_DAT        ad;
39 #endif
40
41         AttributeDescription *password = slap_schema.si_ad_userPassword;
42
43         u_int32_t       locker;
44         DB_LOCK         lock;
45
46 #ifdef NEW_LOGGING
47         LDAP_LOG ( OPERATION, ARGS, "==> bdb_bind: dn: %s\n", op->o_req_dn.bv_val, 0, 0 );
48 #else
49         Debug( LDAP_DEBUG_ARGS, "==> bdb_bind: dn: %s\n", op->o_req_dn.bv_val, 0, 0);
50 #endif
51
52         /* allow noauth binds */
53         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
54                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
55                 /* front end will send result */
56                 return LDAP_SUCCESS;
57         }
58
59         rs->sr_err = LOCK_ID(bdb->bi_dbenv, &locker);
60         switch(rs->sr_err) {
61         case 0:
62                 break;
63         default:
64                 rs->sr_text = "internal error";
65                 send_ldap_result( op, rs );
66                 return rs->sr_err;
67         }
68
69 dn2entry_retry:
70         /* get entry with reader lock */
71         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1,
72                 locker, &lock );
73
74         switch(rs->sr_err) {
75         case DB_NOTFOUND:
76         case 0:
77                 break;
78         case LDAP_BUSY:
79                 send_ldap_error( op, rs, LDAP_BUSY, "ldap_server_busy" );
80                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
81                 return LDAP_BUSY;
82         case DB_LOCK_DEADLOCK:
83         case DB_LOCK_NOTGRANTED:
84                 goto dn2entry_retry;
85         default:
86                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
87                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
88                 return rs->sr_err;
89         }
90
91         e = ei->bei_e;
92         if ( rs->sr_err == DB_NOTFOUND ) {
93                 if( e != NULL ) {
94                         rs->sr_ref = is_entry_referral( e )
95                                 ? get_entry_referrals( op, e )
96                                 : NULL;
97                         if (rs->sr_ref)
98                                 rs->sr_matched = ch_strdup( e->e_name.bv_val );
99
100                         bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
101                         e = NULL;
102                 } else {
103                         rs->sr_ref = referral_rewrite( default_referral,
104                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
105                 }
106
107                 if ( rs->sr_ref != NULL ) {
108                         rs->sr_err = LDAP_REFERRAL;
109                         send_ldap_result( op, rs );
110                         free( (char *)rs->sr_matched );
111                         ber_bvarray_free( rs->sr_ref );
112                         rs->sr_ref = NULL;
113                         rs->sr_matched = NULL;
114                 } else {
115                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
116                         send_ldap_result( op, rs );
117                 }
118
119                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
120
121                 return rs->sr_err;
122         }
123
124         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
125
126         /* check for deleted */
127 #ifdef BDB_SUBENTRIES
128         if ( is_entry_subentry( e ) ) {
129                 /* entry is an subentry, don't allow bind */
130 #ifdef NEW_LOGGING
131                 LDAP_LOG ( OPERATION, DETAIL1, 
132                         "bdb_bind: entry is subentry\n", 0, 0, 0 );
133 #else
134                 Debug( LDAP_DEBUG_TRACE, "entry is subentry\n", 0,
135                         0, 0 );
136 #endif
137
138                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
139                 send_ldap_result( op, rs );
140
141                 goto done;
142         }
143 #endif
144
145 #ifdef BDB_ALIASES
146         if ( is_entry_alias( e ) ) {
147                 /* entry is an alias, don't allow bind */
148 #ifdef NEW_LOGGING
149                 LDAP_LOG ( OPERATION, DETAIL1, "bdb_bind: entry is alias\n", 0, 0, 0 );
150 #else
151                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
152                         0, 0 );
153 #endif
154
155                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM, "entry is alias");
156
157                 goto done;
158         }
159 #endif
160
161         if ( is_entry_referral( e ) ) {
162                 /* entry is a referral, don't allow bind */
163                 rs->sr_ref = get_entry_referrals( op, e );
164
165 #ifdef NEW_LOGGING
166                 LDAP_LOG ( OPERATION, DETAIL1, 
167                         "bdb_bind: entry is referral\n", 0, 0, 0 );
168 #else
169                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
170                         0, 0 );
171 #endif
172
173                 if( rs->sr_ref != NULL ) {
174                         rs->sr_err = LDAP_REFERRAL;
175                         rs->sr_matched = e->e_name.bv_val;
176                         send_ldap_result( op, rs );
177                         ber_bvarray_free( rs->sr_ref );
178                         rs->sr_ref = NULL;
179                         rs->sr_matched = NULL;
180                 } else {
181                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
182                         send_ldap_result( op, rs );
183                 }
184
185                 goto done;
186         }
187
188         switch ( op->oq_bind.rb_method ) {
189         case LDAP_AUTH_SIMPLE:
190                 rs->sr_err = access_allowed( op, e,
191                         password, NULL, ACL_AUTH, NULL );
192                 if ( ! rs->sr_err ) {
193                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
194                         send_ldap_result( op, rs );
195                         goto done;
196                 }
197
198                 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
199                         rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
200                         send_ldap_result( op, rs );
201                         goto done;
202                 }
203
204                 if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 ) {
205                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
206                         send_ldap_result( op, rs );
207                         goto done;
208                 }
209
210                 rs->sr_err = 0;
211                 break;
212
213 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
214         case LDAP_AUTH_KRBV41:
215                 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad ) != LDAP_SUCCESS ) {
216                         rs->sr_err = LDAP_INVALID_CREDENTIALS,
217                         send_ldap_result( op );
218                         goto done;
219                 }
220
221                 rs->sr_err = access_allowed( op, e,
222                         krbattr, NULL, ACL_AUTH, NULL );
223                 if ( ! rs->sr_err ) {
224                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS,
225                         send_ldap_result( op );
226                         goto done;
227                 }
228
229                 krbval.bv_len = 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                                 rs->sr_err = 0;
238                                 break;
239                         }
240                         rs->sr_err = LDAP_INAPPROPRIATE_AUTH,
241                         send_ldap_result( op );
242                         goto done;
243
244                 } else {        /* look for krbname match */
245                         krbval.bv_val = krbname;
246
247                         if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
248                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
249                                 send_ldap_result( op );
250                                 goto done;
251                         }
252                 }
253                 rs->sr_err = 0;
254                 break;
255
256         case LDAP_AUTH_KRBV42:
257                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
258                         "Kerberos bind step 2 not supported" );
259                 goto done;
260 #endif
261
262         default:
263                 send_ldap_error( op, rs, LDAP_STRONG_AUTH_NOT_SUPPORTED,
264                         "authentication method not supported" );
265                 goto done;
266         }
267
268 done:
269         /* free entry and reader lock */
270         if( e != NULL ) {
271                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
272         }
273
274         LOCK_ID_FREE(bdb->bi_dbenv, locker);
275
276         /* front end will send result on success (rs->sr_err==0) */
277         return rs->sr_err;
278 }