]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ndb/compare.cpp
MySQL NDB Cluster backend (experimental)
[openldap] / servers / slapd / back-ndb / compare.cpp
1 /* compare.cpp - ndb backend compare routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2008 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
26 #include "back-ndb.h"
27
28 int
29 ndb_back_compare( Operation *op, SlapReply *rs )
30 {
31         struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
32         Entry           e = {0};
33         Attribute       *a;
34         int             manageDSAit = get_manageDSAit( op );
35
36         NdbArgs NA;
37         NdbRdns rdns;
38         struct berval matched;
39
40         /* Get our NDB handle */
41         rs->sr_err = ndb_thread_handle( op, &NA.ndb );
42
43         rdns.nr_num = 0;
44         NA.rdns = &rdns;
45         e.e_name = op->o_req_dn;
46         e.e_nname = op->o_req_ndn;
47
48 dn2entry_retry:
49         NA.txn = NA.ndb->startTransaction();
50         rs->sr_text = NULL;
51         if( !NA.txn ) {
52                 Debug( LDAP_DEBUG_TRACE,
53                         LDAP_XSTRING(ndb_compare) ": startTransaction failed: %s (%d)\n",
54                         NA.ndb->getNdbError().message, NA.ndb->getNdbError().code, 0 );
55                 rs->sr_err = LDAP_OTHER;
56                 rs->sr_text = "internal error";
57                 goto return_results;
58         }
59
60         NA.ocs = NULL;
61         /* get entry */
62         rs->sr_err = ndb_entry_get_info( op->o_bd, &NA, 0, &matched );
63         switch( rs->sr_err ) {
64         case 0:
65                 break;
66         case LDAP_NO_SUCH_OBJECT:
67                 rs->sr_matched = matched.bv_val;
68                 goto return_results;
69         case LDAP_BUSY:
70                 rs->sr_text = "ldap server busy";
71                 goto return_results;
72 #if 0
73         case DB_LOCK_DEADLOCK:
74         case DB_LOCK_NOTGRANTED:
75                 goto dn2entry_retry;
76 #endif
77         default:
78                 rs->sr_err = LDAP_OTHER;
79                 rs->sr_text = "internal error";
80                 goto return_results;
81         }
82
83         rs->sr_err = ndb_entry_get_data( op->o_bd, &NA, 0 );
84         ber_bvarray_free( NA.ocs );
85         if (!manageDSAit && is_entry_referral( &e ) ) {
86                 /* return referral only if "disclose" is granted on the object */
87                 if ( !access_allowed( op, &e, slap_schema.si_ad_entry,
88                         NULL, ACL_DISCLOSE, NULL ) )
89                 {
90                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
91                 } else {
92                         /* entry is a referral, don't allow compare */
93                         rs->sr_ref = get_entry_referrals( op, &e );
94                         rs->sr_err = LDAP_REFERRAL;
95                         rs->sr_matched = e.e_name.bv_val;
96                 }
97
98                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0, 0, 0 );
99                 goto return_results;
100         }
101
102         if ( get_assert( op ) &&
103                 ( test_filter( op, &e, (Filter *)get_assertion( op )) != LDAP_COMPARE_TRUE ))
104         {
105                 if ( !access_allowed( op, &e, slap_schema.si_ad_entry,
106                         NULL, ACL_DISCLOSE, NULL ) )
107                 {
108                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
109                 } else {
110                         rs->sr_err = LDAP_ASSERTION_FAILED;
111                 }
112                 goto return_results;
113         }
114
115         if ( !access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
116                 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL ) )
117         {
118                 /* return error only if "disclose"
119                  * is granted on the object */
120                 if ( !access_allowed( op, &e, slap_schema.si_ad_entry,
121                                         NULL, ACL_DISCLOSE, NULL ) )
122                 {
123                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
124                 } else {
125                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
126                 }
127                 goto return_results;
128         }
129
130         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
131
132         for ( a = attrs_find( e.e_attrs, op->oq_compare.rs_ava->aa_desc );
133                 a != NULL;
134                 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
135         {
136                 rs->sr_err = LDAP_COMPARE_FALSE;
137
138                 if ( attr_valfind( a,
139                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
140                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
141                         &op->oq_compare.rs_ava->aa_value, NULL,
142                         op->o_tmpmemctx ) == 0 )
143                 {
144                         rs->sr_err = LDAP_COMPARE_TRUE;
145                         break;
146                 }
147         }
148
149 return_results:
150         NA.txn->close();
151         if ( e.e_attrs ) {
152                 attrs_free( e.e_attrs );
153                 e.e_attrs = NULL;
154         }
155         send_ldap_result( op, rs );
156
157         ber_bvarray_free( rs->sr_ref );
158         rs->sr_ref = NULL;
159
160         switch ( rs->sr_err ) {
161         case LDAP_COMPARE_FALSE:
162         case LDAP_COMPARE_TRUE:
163                 rs->sr_err = LDAP_SUCCESS;
164                 break;
165         }
166
167         return rs->sr_err;
168 }