]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/bind.c
286c268b06934e370276de3be93427f532c782ab
[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 #ifdef LDAP_DEVEL
43         /* allow noauth binds */
44         switch ( be_rootdn_bind( op, rs ) ) {
45         case SLAP_CB_CONTINUE:
46                 break;
47
48         default:
49                 /* in case of success, frontend will send result;
50                  * otherwise, be_rootdn_bind() did */
51                 return rs->sr_err;
52         }
53
54 #else /* traditional */
55         /* allow noauth binds */
56         switch ( be_rootdn_bind( op, NULL ) ) {
57         case LDAP_SUCCESS:
58                 /* frontend will send result */
59                 return rs->sr_err;
60
61         default:
62                 /* give the database a chanche */
63                 break;
64         }
65 #endif /* traditional */
66
67         rs->sr_err = LOCK_ID(bdb->bi_dbenv, &locker);
68         switch(rs->sr_err) {
69         case 0:
70                 break;
71         default:
72                 rs->sr_text = "internal error";
73                 send_ldap_result( op, rs );
74                 return rs->sr_err;
75         }
76
77 dn2entry_retry:
78         /* get entry with reader lock */
79         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1,
80                 locker, &lock );
81
82         switch(rs->sr_err) {
83         case DB_NOTFOUND:
84         case 0:
85                 break;
86         case LDAP_BUSY:
87                 send_ldap_error( op, rs, LDAP_BUSY, "ldap_server_busy" );
88                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
89                 return LDAP_BUSY;
90         case DB_LOCK_DEADLOCK:
91         case DB_LOCK_NOTGRANTED:
92                 goto dn2entry_retry;
93         default:
94                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
95                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
96                 return rs->sr_err;
97         }
98
99         e = ei->bei_e;
100         if ( rs->sr_err == DB_NOTFOUND ) {
101                 if( e != NULL ) {
102                         bdb_cache_return_entry_r( bdb, e, &lock );
103                         e = NULL;
104                 }
105
106                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
107                 send_ldap_result( op, rs );
108
109                 LOCK_ID_FREE(bdb->bi_dbenv, locker);
110
111                 return rs->sr_err;
112         }
113
114         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
115
116         /* check for deleted */
117         if ( is_entry_subentry( e ) ) {
118                 /* entry is an subentry, don't allow bind */
119                 Debug( LDAP_DEBUG_TRACE, "entry is subentry\n", 0,
120                         0, 0 );
121                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
122                 goto done;
123         }
124
125         if ( is_entry_alias( e ) ) {
126                 /* entry is an alias, don't allow bind */
127                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
128                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
129                 goto done;
130         }
131
132         if ( is_entry_referral( e ) ) {
133                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
134                         0, 0 );
135                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
136                 goto done;
137         }
138
139         switch ( op->oq_bind.rb_method ) {
140         case LDAP_AUTH_SIMPLE:
141                 a = attr_find( e->e_attrs, password );
142                 if ( a == NULL ) {
143                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
144                         goto done;
145                 }
146
147                 if ( slap_passwd_check( op, e, a, &op->oq_bind.rb_cred,
148                                         &rs->sr_text ) != 0 )
149                 {
150                         /* failure; stop front end from sending result */
151                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
152                         goto done;
153                 }
154                         
155                 rs->sr_err = 0;
156                 break;
157
158         default:
159                 assert( 0 ); /* should not be reachable */
160                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
161                 rs->sr_text = "authentication method not supported";
162         }
163
164 done:
165         /* free entry and reader lock */
166         if( e != NULL ) {
167                 bdb_cache_return_entry_r( bdb, e, &lock );
168         }
169
170         LOCK_ID_FREE(bdb->bi_dbenv, locker);
171
172         if ( rs->sr_err ) {
173                 send_ldap_result( op, rs );
174                 if ( rs->sr_ref ) {
175                         ber_bvarray_free( rs->sr_ref );
176                         rs->sr_ref = NULL;
177                 }
178         }
179         /* front end will send result on success (rs->sr_err==0) */
180         return rs->sr_err;
181 }