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