2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 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;
40 char *match = NULL, *err = NULL;
41 struct berval mmatch = BER_BVNULL;
42 int candidates = 0, last = 0, i, count = 0, rc;
43 int cres = LDAP_SUCCESS, rres = LDAP_SUCCESS;
47 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
48 &op->o_req_ndn, NULL );
50 send_ldap_result( op, rs );
54 if ( !meta_back_dobind( lc, op ) ) {
55 rs->sr_err = LDAP_OTHER;
56 send_ldap_result( op, rs );
60 msgid = ch_calloc( sizeof( int ), li->ntargets );
61 if ( msgid == NULL ) {
66 * start an asynchronous compare for each candidate target
72 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
73 struct berval mdn = BER_BVNULL;
74 struct berval mapped_attr = op->oq_compare.rs_ava->aa_desc->ad_cname;
75 struct berval mapped_value = op->oq_compare.rs_ava->aa_value;
77 if ( lsc->candidate != META_CANDIDATE ) {
83 * Rewrite the compare dn, if needed
85 dc.rwmap = &li->targets[ i ]->rwmap;
87 switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
88 case LDAP_UNWILLING_TO_PERFORM:
97 * if attr is objectClass, try to remap the value
99 if ( op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_objectClass ) {
100 ldap_back_map( &li->targets[ i ]->rwmap.rwm_oc,
101 &op->oq_compare.rs_ava->aa_value,
102 &mapped_value, BACKLDAP_MAP );
104 if ( mapped_value.bv_val == NULL || mapped_value.bv_val[0] == '\0' ) {
108 * else try to remap the attribute
111 ldap_back_map( &li->targets[ i ]->rwmap.rwm_at,
112 &op->oq_compare.rs_ava->aa_desc->ad_cname,
113 &mapped_attr, BACKLDAP_MAP );
114 if ( mapped_attr.bv_val == NULL || mapped_attr.bv_val[0] == '\0' ) {
120 * the compare op is spawned across the targets and the first
121 * that returns determines the result; a constraint on unicity
122 * of the result ought to be enforced
124 msgid[ i ] = ldap_compare( lc->conns[ i ].ld, mdn.bv_val,
125 mapped_attr.bv_val, mapped_value.bv_val );
126 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
130 if ( mapped_attr.bv_val != op->oq_compare.rs_ava->aa_desc->ad_cname.bv_val ) {
131 free( mapped_attr.bv_val );
133 if ( mapped_value.bv_val != op->oq_compare.rs_ava->aa_value.bv_val ) {
134 free( mapped_value.bv_val );
137 if ( msgid[ i ] == -1 ) {
147 for ( rc = 0, count = 0; candidates > 0; ) {
150 * FIXME: should we check for abandon?
152 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
154 LDAPMessage *res = NULL;
156 if ( msgid[ i ] == -1 ) {
160 lrc = ldap_result( lsc->ld, msgid[ i ],
165 * FIXME: should we yield?
172 } else if ( lrc == LDAP_RES_COMPARE ) {
179 rs->sr_err = ldap_result2error( lsc->ld, res, 1 );
180 switch ( rs->sr_err ) {
181 case LDAP_COMPARE_TRUE:
182 case LDAP_COMPARE_FALSE:
185 * true or flase, got it;
186 * sending to cache ...
188 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
189 ( void )meta_dncache_update_entry( &li->cache, &op->o_req_ndn, i );
197 rres = slap_map_api2result( rs );
202 ldap_get_option( lsc->ld,
203 LDAP_OPT_ERROR_STRING, &err );
205 if ( match != NULL ) {
208 ldap_get_option( lsc->ld,
209 LDAP_OPT_MATCHED_DN, &match );
231 * Rewrite the matched portion of the search base, if required
233 * FIXME: only the last one gets caught!
236 if ( match != NULL ) {
242 * the result of the compare is assigned to the res code
243 * that will be returned
248 * At least one compare failed with matched portion,
249 * and none was successful
251 } else if ( match != NULL && match[0] != '\0' ) {
252 struct berval matched;
254 matched.bv_val = match;
255 matched.bv_len = strlen( match );
257 dc.ctx = "matchedDN";
258 ldap_back_dn_massage( &dc, &matched, &mmatch );
261 if ( rres != LDAP_SUCCESS ) {
264 rs->sr_matched = mmatch.bv_val;
265 send_ldap_result( op, rs );
266 rs->sr_matched = NULL;
268 if ( match != NULL ) {
269 if ( mmatch.bv_val != match ) {
270 free( mmatch.bv_val );