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, LDAP_BACK_SENDERR );
55 if ( !lc || !meta_back_dobind( lc, op, LDAP_BACK_SENDERR ) ) {
59 msgid = ch_calloc( sizeof( int ), li->ntargets );
60 if ( msgid == NULL ) {
65 * start an asynchronous compare for each candidate target
71 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
72 struct berval mdn = BER_BVNULL;
73 struct berval mapped_attr = op->orc_ava->aa_desc->ad_cname;
74 struct berval mapped_value = op->orc_ava->aa_value;
76 if ( lsc->msc_candidate != META_CANDIDATE ) {
82 * Rewrite the compare dn, if needed
84 dc.rwmap = &li->targets[ i ]->mt_rwmap;
86 switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
87 case LDAP_UNWILLING_TO_PERFORM:
96 * if attr is objectClass, try to remap the value
98 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
99 ldap_back_map( &li->targets[ i ]->mt_rwmap.rwm_oc,
100 &op->orc_ava->aa_value,
101 &mapped_value, BACKLDAP_MAP );
103 if ( mapped_value.bv_val == NULL || mapped_value.bv_val[0] == '\0' ) {
107 * else try to remap the attribute
110 ldap_back_map( &li->targets[ i ]->mt_rwmap.rwm_at,
111 &op->orc_ava->aa_desc->ad_cname,
112 &mapped_attr, BACKLDAP_MAP );
113 if ( mapped_attr.bv_val == NULL || mapped_attr.bv_val[0] == '\0' ) {
117 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
119 dc.ctx = "compareAttrDN";
121 switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
123 case LDAP_UNWILLING_TO_PERFORM:
134 * the compare op is spawned across the targets and the first
135 * that returns determines the result; a constraint on unicity
136 * of the result ought to be enforced
138 rc = ldap_compare_ext( lc->mc_conns[ i ].msc_ld, mdn.bv_val,
139 mapped_attr.bv_val, &mapped_value,
140 op->o_ctrls, NULL, &msgid[ i ] );
142 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
147 if ( mapped_attr.bv_val != op->orc_ava->aa_desc->ad_cname.bv_val ) {
148 free( mapped_attr.bv_val );
149 BER_BVZERO( &mapped_attr );
152 if ( mapped_value.bv_val != op->orc_ava->aa_value.bv_val ) {
153 free( mapped_value.bv_val );
154 BER_BVZERO( &mapped_value );
157 if ( rc != LDAP_SUCCESS ) {
158 /* FIXME: what should we do with the error? */
168 for ( rc = 0, count = 0; candidates > 0; ) {
171 * FIXME: should we check for abandon?
173 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); lsc++, i++ ) {
175 LDAPMessage *res = NULL;
177 if ( msgid[ i ] == -1 ) {
181 lrc = ldap_result( lsc->msc_ld, msgid[ i ],
186 * FIXME: should we yield?
193 } else if ( lrc == LDAP_RES_COMPARE ) {
200 rc = ldap_parse_result( lsc->msc_ld, res,
202 NULL, NULL, NULL, NULL, 1 );
203 if ( rc != LDAP_SUCCESS ) {
209 switch ( rs->sr_err ) {
210 case LDAP_COMPARE_TRUE:
211 case LDAP_COMPARE_FALSE:
214 * true or flase, got it;
215 * sending to cache ...
217 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
218 ( void )meta_dncache_update_entry( &li->cache, &op->o_req_ndn, i );
226 rres = slap_map_api2result( rs );
231 ldap_get_option( lsc->msc_ld,
232 LDAP_OPT_ERROR_STRING, &err );
234 if ( match != NULL ) {
237 ldap_get_option( lsc->msc_ld,
238 LDAP_OPT_MATCHED_DN, &match );
260 * Rewrite the matched portion of the search base, if required
262 * FIXME: only the last one gets caught!
265 if ( match != NULL ) {
271 * the result of the compare is assigned to the res code
272 * that will be returned
277 * At least one compare failed with matched portion,
278 * and none was successful
280 } else if ( match != NULL && match[0] != '\0' ) {
281 struct berval matched;
283 matched.bv_val = match;
284 matched.bv_len = strlen( match );
286 dc.ctx = "matchedDN";
287 ldap_back_dn_massage( &dc, &matched, &mmatch );
290 if ( rres != LDAP_SUCCESS ) {
293 rs->sr_matched = mmatch.bv_val;
294 send_ldap_result( op, rs );
295 rs->sr_matched = NULL;
297 if ( match != NULL ) {
298 if ( mmatch.bv_val != match ) {
299 free( mmatch.bv_val );