]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ndb/bind.cpp
fix previous commit (ITS#6661)
[openldap] / servers / slapd / back-ndb / bind.cpp
1 /* bind.cpp - ndb backend bind routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2008-2010 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 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Howard Chu for inclusion
18  * in OpenLDAP Software. This work was sponsored by MySQL.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/unistd.h>
26
27 #include "back-ndb.h"
28
29 extern "C" int
30 ndb_back_bind( Operation *op, SlapReply *rs )
31 {
32         struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
33         Entry           e = {0};
34         Attribute       *a;
35
36         AttributeDescription *password = slap_schema.si_ad_userPassword;
37
38         NdbArgs NA;
39
40         Debug( LDAP_DEBUG_ARGS,
41                 "==> " LDAP_XSTRING(ndb_back_bind) ": dn: %s\n",
42                 op->o_req_dn.bv_val, 0, 0);
43
44         /* allow noauth binds */
45         rs->sr_err = be_rootdn_bind( op, NULL );
46         switch ( rs->sr_err ) {
47         case SLAP_CB_CONTINUE:
48                 break;
49
50         case LDAP_INVALID_CREDENTIALS:
51                 send_ldap_result( op, rs );
52                 /* fallthru */
53
54         case LDAP_SUCCESS:
55                 /* frontend will send result */
56                 return rs->sr_err;
57         }
58
59         /* Get our NDB handle */
60         rs->sr_err = ndb_thread_handle( op, &NA.ndb );
61
62         e.e_name = op->o_req_dn;
63         e.e_nname = op->o_req_ndn;
64         NA.e = &e;
65
66 dn2entry_retry:
67         NA.txn = NA.ndb->startTransaction();
68         rs->sr_text = NULL;
69         if( !NA.txn ) {
70                 Debug( LDAP_DEBUG_TRACE,
71                         LDAP_XSTRING(ndb_back_bind) ": startTransaction failed: %s (%d)\n",
72                         NA.ndb->getNdbError().message, NA.ndb->getNdbError().code, 0 );
73                 rs->sr_err = LDAP_OTHER;
74                 rs->sr_text = "internal error";
75                 goto done;
76         }
77
78         /* get entry */
79         {
80                 NdbRdns rdns;
81                 rdns.nr_num = 0;
82                 NA.rdns = &rdns;
83                 NA.ocs = NULL;
84                 rs->sr_err = ndb_entry_get_info( op, &NA, 0, NULL );
85         }
86         switch(rs->sr_err) {
87         case 0:
88                 break;
89         case LDAP_NO_SUCH_OBJECT:
90                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
91                 goto done;
92         case LDAP_BUSY:
93                 rs->sr_text = "ldap_server_busy";
94                 goto done;
95 #if 0
96         case DB_LOCK_DEADLOCK:
97         case DB_LOCK_NOTGRANTED:
98                 goto dn2entry_retry;
99 #endif
100         default:
101                 rs->sr_err = LDAP_OTHER;
102                 rs->sr_text = "internal error";
103                 goto done;
104         }
105
106         rs->sr_err = ndb_entry_get_data( op, &NA, 0 );
107         ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
108         ber_dupbv( &op->oq_bind.rb_edn, &e.e_name );
109
110         /* check for deleted */
111         if ( is_entry_subentry( &e ) ) {
112                 /* entry is an subentry, don't allow bind */
113                 Debug( LDAP_DEBUG_TRACE, "entry is subentry\n", 0,
114                         0, 0 );
115                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
116                 goto done;
117         }
118
119         if ( is_entry_alias( &e ) ) {
120                 /* entry is an alias, don't allow bind */
121                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
122                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
123                 goto done;
124         }
125
126         if ( is_entry_referral( &e ) ) {
127                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
128                         0, 0 );
129                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
130                 goto done;
131         }
132
133         switch ( op->oq_bind.rb_method ) {
134         case LDAP_AUTH_SIMPLE:
135                 a = attr_find( e.e_attrs, password );
136                 if ( a == NULL ) {
137                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
138                         goto done;
139                 }
140
141                 if ( slap_passwd_check( op, &e, a, &op->oq_bind.rb_cred,
142                                         &rs->sr_text ) != 0 )
143                 {
144                         /* failure; stop front end from sending result */
145                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
146                         goto done;
147                 }
148                         
149                 rs->sr_err = 0;
150                 break;
151
152         default:
153                 assert( 0 ); /* should not be reachable */
154                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
155                 rs->sr_text = "authentication method not supported";
156         }
157
158 done:
159         NA.txn->close();
160         if ( e.e_attrs ) {
161                 attrs_free( e.e_attrs );
162                 e.e_attrs = NULL;
163         }
164         if ( rs->sr_err ) {
165                 send_ldap_result( op, rs );
166         }
167         /* front end will send result on success (rs->sr_err==0) */
168         return rs->sr_err;
169 }