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