2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/string.h>
28 #include <ac/socket.h>
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
35 meta_back_compare( Operation *op, SlapReply *rs )
37 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
39 struct metasingleconn *lsc;
42 struct berval mmatch = BER_BVNULL;
53 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
54 &op->o_req_ndn, NULL );
56 send_ldap_result( op, rs );
60 if ( !meta_back_dobind( lc, op ) ) {
61 rs->sr_err = LDAP_UNAVAILABLE;
62 send_ldap_result( op, rs );
66 msgid = ch_calloc( sizeof( int ), li->ntargets );
67 if ( msgid == NULL ) {
72 * start an asynchronous compare for each candidate target
78 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
79 struct berval mdn = BER_BVNULL;
80 struct berval mapped_attr = op->orc_ava->aa_desc->ad_cname;
81 struct berval mapped_value = op->orc_ava->aa_value;
83 if ( lsc->msc_candidate != META_CANDIDATE ) {
89 * Rewrite the compare dn, if needed
91 dc.rwmap = &li->targets[ i ]->mt_rwmap;
93 switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
94 case LDAP_UNWILLING_TO_PERFORM:
103 * if attr is objectClass, try to remap the value
105 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
106 ldap_back_map( &li->targets[ i ]->mt_rwmap.rwm_oc,
107 &op->orc_ava->aa_value,
108 &mapped_value, BACKLDAP_MAP );
110 if ( mapped_value.bv_val == NULL || mapped_value.bv_val[0] == '\0' ) {
114 * else try to remap the attribute
117 ldap_back_map( &li->targets[ i ]->mt_rwmap.rwm_at,
118 &op->orc_ava->aa_desc->ad_cname,
119 &mapped_attr, BACKLDAP_MAP );
120 if ( mapped_attr.bv_val == NULL || mapped_attr.bv_val[0] == '\0' ) {
124 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
126 dc.ctx = "compareAttrDN";
128 switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
130 case LDAP_UNWILLING_TO_PERFORM:
141 * the compare op is spawned across the targets and the first
142 * that returns determines the result; a constraint on unicity
143 * of the result ought to be enforced
145 rc = ldap_compare_ext( lc->mc_conns[ i ].msc_ld, mdn.bv_val,
146 mapped_attr.bv_val, &mapped_value,
147 NULL, NULL, &msgid[ i ] );
149 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
154 if ( mapped_attr.bv_val != op->orc_ava->aa_desc->ad_cname.bv_val ) {
155 free( mapped_attr.bv_val );
156 BER_BVZERO( &mapped_attr );
159 if ( mapped_value.bv_val != op->orc_ava->aa_value.bv_val ) {
160 free( mapped_value.bv_val );
161 BER_BVZERO( &mapped_value );
164 if ( rc != LDAP_SUCCESS ) {
165 /* FIXME: what should we do with the error? */
175 for ( rc = 0, count = 0; candidates > 0; ) {
178 * FIXME: should we check for abandon?
180 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); lsc++, i++ ) {
182 LDAPMessage *res = NULL;
184 if ( msgid[ i ] == -1 ) {
188 lrc = ldap_result( lsc->msc_ld, msgid[ i ],
193 * FIXME: should we yield?
200 } else if ( lrc == LDAP_RES_COMPARE ) {
207 rc = ldap_parse_result( lsc->msc_ld, res,
209 NULL, NULL, NULL, NULL, 1 );
210 if ( rc != LDAP_SUCCESS ) {
216 switch ( rs->sr_err ) {
217 case LDAP_COMPARE_TRUE:
218 case LDAP_COMPARE_FALSE:
221 * true or flase, got it;
222 * sending to cache ...
224 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
225 ( void )meta_dncache_update_entry( &li->cache, &op->o_req_ndn, i );
233 rres = slap_map_api2result( rs );
238 ldap_get_option( lsc->msc_ld,
239 LDAP_OPT_ERROR_STRING, &err );
241 if ( match != NULL ) {
244 ldap_get_option( lsc->msc_ld,
245 LDAP_OPT_MATCHED_DN, &match );
267 * Rewrite the matched portion of the search base, if required
269 * FIXME: only the last one gets caught!
272 if ( match != NULL ) {
278 * the result of the compare is assigned to the res code
279 * that will be returned
284 * At least one compare failed with matched portion,
285 * and none was successful
287 } else if ( match != NULL && match[0] != '\0' ) {
288 struct berval matched;
290 matched.bv_val = match;
291 matched.bv_len = strlen( match );
293 dc.ctx = "matchedDN";
294 ldap_back_dn_massage( &dc, &matched, &mmatch );
297 if ( rres != LDAP_SUCCESS ) {
300 rs->sr_matched = mmatch.bv_val;
301 send_ldap_result( op, rs );
302 rs->sr_matched = NULL;
304 if ( match != NULL ) {
305 if ( mmatch.bv_val != match ) {
306 free( mmatch.bv_val );