]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/bind.c
Fix prev commit, cleanup fakeroot entry before freeing
[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/string.h>
21 #include <ac/unistd.h>
22
23 #include "back-bdb.h"
24
25 int
26 bdb_bind( Operation *op, SlapReply *rs )
27 {
28         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
29         Entry           *e;
30         Attribute       *a;
31         EntryInfo       *ei;
32
33         AttributeDescription *password = slap_schema.si_ad_userPassword;
34
35         u_int32_t       locker;
36         DB_LOCK         lock;
37
38         Debug( LDAP_DEBUG_ARGS,
39                 "==> " LDAP_XSTRING(bdb_bind) ": dn: %s\n",
40                 op->o_req_dn.bv_val, 0, 0);
41
42         /* allow noauth binds */
43         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
44                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
45                 /* front end will send result */
46                 return LDAP_SUCCESS;
47         }
48
49         rs->sr_err = LOCK_ID(bdb->bi_dbenv, &locker);
50         switch(rs->sr_err) {
51         case 0:
52                 break;
53         default:
54                 rs->sr_text = "internal error";
55                 send_ldap_result( op, rs );
56                 return rs->sr_err;
57         }
58
59 dn2entry_retry:
60         /* get entry with reader lock */
61         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1,
62                 locker, &lock );
63
64         switch(rs->sr_err) {
65         case DB_NOTFOUND:
66         case 0:
67                 break;
68         case LDAP_BUSY:
69                 send_ldap_error( op, rs, LDAP_BUSY, "ldap_server_busy" );
70                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
71                 return LDAP_BUSY;
72         case DB_LOCK_DEADLOCK:
73         case DB_LOCK_NOTGRANTED:
74                 goto dn2entry_retry;
75         default:
76                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
77                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
78                 return rs->sr_err;
79         }
80
81         e = ei->bei_e;
82         if ( rs->sr_err == DB_NOTFOUND ) {
83                 if( e != NULL ) {
84                         bdb_cache_return_entry_r( bdb, e, &lock );
85                         e = NULL;
86                 }
87
88                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
89                 send_ldap_result( op, rs );
90
91                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
92
93                 return rs->sr_err;
94         }
95
96         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
97
98         /* check for deleted */
99         if ( is_entry_subentry( e ) ) {
100                 /* entry is an subentry, don't allow bind */
101                 Debug( LDAP_DEBUG_TRACE, "entry is subentry\n", 0,
102                         0, 0 );
103                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
104                 goto done;
105         }
106
107         if ( is_entry_alias( e ) ) {
108                 /* entry is an alias, don't allow bind */
109                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
110                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
111                 goto done;
112         }
113
114         if ( is_entry_referral( e ) ) {
115                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
116                         0, 0 );
117                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
118                 goto done;
119         }
120
121         switch ( op->oq_bind.rb_method ) {
122         case LDAP_AUTH_SIMPLE:
123                 a = attr_find( e->e_attrs, password );
124                 if ( a == NULL ) {
125                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
126                         goto done;
127                 }
128
129                 if ( slap_passwd_check( op, e, a, &op->oq_bind.rb_cred,
130                                         &rs->sr_text ) != 0 )
131                 {
132                         /* failure; stop front end from sending result */
133                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
134                         goto done;
135                 }
136                         
137                 rs->sr_err = 0;
138                 break;
139
140         default:
141                 assert( 0 ); /* should not be reachable */
142                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
143                 rs->sr_text = "authentication method not supported";
144         }
145
146 done:
147         /* free entry and reader lock */
148         if( e != NULL ) {
149                 bdb_cache_return_entry_r( bdb, e, &lock );
150         }
151
152         LOCK_ID_FREE(bdb->bi_dbenv, locker);
153
154         if ( rs->sr_err ) {
155                 send_ldap_result( op, rs );
156                 if ( rs->sr_ref ) {
157                         ber_bvarray_free( rs->sr_ref );
158                         rs->sr_ref = NULL;
159                 }
160         }
161         /* front end will send result on success (rs->sr_err==0) */
162         return rs->sr_err;
163 }