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