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