]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/bind.c
3f4c558c852905eeb648b584d7a1aa13802200f7
[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-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 #include <ac/krb.h>
21 #include <ac/string.h>
22 #include <ac/unistd.h>
23
24 #include "back-bdb.h"
25
26 int
27 bdb_bind( Operation *op, SlapReply *rs )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
30         Entry           *e;
31         Attribute       *a;
32         EntryInfo       *ei;
33 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
34         char            krbname[MAX_K_NAME_SZ + 1];
35         AttributeDescription *krbattr = slap_schema.si_ad_krbName;
36         struct berval   krbval;
37         AUTH_DAT        ad;
38 #endif
39
40         AttributeDescription *password = slap_schema.si_ad_userPassword;
41
42         u_int32_t       locker;
43         DB_LOCK         lock;
44
45         Debug( LDAP_DEBUG_ARGS,
46                 "==> bdb_bind: dn: %s\n", op->o_req_dn.bv_val, 0, 0);
47
48         /* allow noauth binds */
49         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
50                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
51                 /* front end will send result */
52                 return LDAP_SUCCESS;
53         }
54
55         rs->sr_err = LOCK_ID(bdb->bi_dbenv, &locker);
56         switch(rs->sr_err) {
57         case 0:
58                 break;
59         default:
60                 rs->sr_text = "internal error";
61                 send_ldap_result( op, rs );
62                 return rs->sr_err;
63         }
64
65 dn2entry_retry:
66         /* get entry with reader lock */
67         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1,
68                 locker, &lock );
69
70         switch(rs->sr_err) {
71         case DB_NOTFOUND:
72         case 0:
73                 break;
74         case LDAP_BUSY:
75                 send_ldap_error( op, rs, LDAP_BUSY, "ldap_server_busy" );
76                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
77                 return LDAP_BUSY;
78         case DB_LOCK_DEADLOCK:
79         case DB_LOCK_NOTGRANTED:
80                 goto dn2entry_retry;
81         default:
82                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
83                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
84                 return rs->sr_err;
85         }
86
87         e = ei->bei_e;
88         if ( rs->sr_err == DB_NOTFOUND ) {
89                 if( e != NULL ) {
90                         bdb_cache_return_entry_r( bdb->bi_dbenv,
91                                 &bdb->bi_cache, e, &lock );
92                         e = NULL;
93                 }
94
95                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
96                 send_ldap_result( op, rs );
97
98                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
99
100                 return rs->sr_err;
101         }
102
103         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
104
105         /* check for deleted */
106 #ifdef BDB_SUBENTRIES
107         if ( is_entry_subentry( e ) ) {
108                 /* entry is an subentry, don't allow bind */
109                 Debug( LDAP_DEBUG_TRACE, "entry is subentry\n", 0,
110                         0, 0 );
111                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
112                 goto done;
113         }
114 #endif
115
116         if ( is_entry_alias( e ) ) {
117                 /* entry is an alias, don't allow bind */
118                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
119                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
120                 goto done;
121         }
122
123         if ( is_entry_referral( e ) ) {
124                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
125                         0, 0 );
126                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
127                 goto done;
128         }
129
130         switch ( op->oq_bind.rb_method ) {
131         case LDAP_AUTH_SIMPLE:
132                 rs->sr_err = access_allowed( op, e,
133                         password, NULL, ACL_AUTH, NULL );
134                 if ( ! rs->sr_err ) {
135                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
136                         goto done;
137                 }
138
139                 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
140                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
141                         goto done;
142                 }
143
144                 if ( slap_passwd_check( op->o_conn,
145                         a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 )
146                 {
147                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
148                         goto done;
149                 }
150
151                 rs->sr_err = 0;
152                 break;
153
154 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
155         case LDAP_AUTH_KRBV41:
156                 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad )
157                         != LDAP_SUCCESS )
158                 {
159                         rs->sr_err = LDAP_INVALID_CREDENTIALS,
160                         goto done;
161                 }
162
163                 rs->sr_err = access_allowed( op, e,
164                         krbattr, NULL, ACL_AUTH, NULL );
165                 if ( ! rs->sr_err ) {
166                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS,
167                         goto done;
168                 }
169
170                 krbval.bv_len = sprintf( krbname, "%s%s%s@%s", ad.pname,
171                         *ad.pinst ? "." : "", ad.pinst, ad.prealm );
172
173                 if ( (a = attr_find( e->e_attrs, krbattr )) == NULL ) {
174                         /*
175                          * no krbname values present: check against DN
176                          */
177                         if ( strcasecmp( op->o_req_dn.bv_val, krbname ) == 0 ) {
178                                 rs->sr_err = 0;
179                                 break;
180                         }
181                         rs->sr_err = LDAP_INAPPROPRIATE_AUTH,
182                         goto done;
183
184                 } else {        /* look for krbname match */
185                         krbval.bv_val = krbname;
186
187                         if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
188                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
189                                 goto done;
190                         }
191                 }
192                 rs->sr_err = 0;
193                 break;
194 #endif
195
196         default:
197                 assert( 0 ); /* should not be unreachable */
198                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
199                 rs->sr_text = "authentication method not supported";
200         }
201
202 done:
203         /* free entry and reader lock */
204         if( e != NULL ) {
205                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
206         }
207
208         LOCK_ID_FREE(bdb->bi_dbenv, locker);
209
210         if ( rs->sr_err ) {
211                 send_ldap_result( op, rs );
212                 if ( rs->sr_ref ) {
213                         ber_bvarray_free( rs->sr_ref );
214                         rs->sr_ref = NULL;
215                 }
216         }
217         /* front end will send result on success (rs->sr_err==0) */
218         return rs->sr_err;
219 }