2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by the Howard Chu for inclusion
17 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 /* This is an altered version */
22 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
24 * This work has been developed to fulfill the requirements
25 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
26 * to the OpenLDAP Foundation in the hope that it may be useful
27 * to the Open Source community, but WITHOUT ANY WARRANTY.
29 * Permission is granted to anyone to use this software for any purpose
30 * on any computer system, and to alter it and redistribute it, subject
31 * to the following restrictions:
33 * 1. The author and SysNet s.n.c. are not responsible for the consequences
34 * of use of this software, no matter how awful, even if they arise from
37 * 2. The origin of this software must not be misrepresented, either by
38 * explicit claim or by omission. Since few users ever read sources,
39 * credits should appear in the documentation.
41 * 3. Altered versions must be plainly marked as such, and must not be
42 * misrepresented as being the original software. Since few users
43 * ever read sources, credits should appear in the documentation.
44 * SysNet s.n.c. cannot be responsible for the consequences of the
47 * 4. This notice may not be removed or altered.
50 * This software is based on the backend back-ldap, implemented
51 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
52 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
53 * contributors. The contribution of the original software to the present
54 * implementation is acknowledged in this copyright statement.
56 * A special acknowledgement goes to Howard for the overall architecture
57 * (and for borrowing large pieces of code), and to Mark, who implemented
58 * from scratch the attribute/objectclass mapping.
60 * The original copyright statement follows.
62 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
64 * Permission is granted to anyone to use this software for any purpose
65 * on any computer system, and to alter it and redistribute it, subject
66 * to the following restrictions:
68 * 1. The author is not responsible for the consequences of use of this
69 * software, no matter how awful, even if they arise from flaws in it.
71 * 2. The origin of this software must not be misrepresented, either by
72 * explicit claim or by omission. Since few users ever read sources,
73 * credits should appear in the documentation.
75 * 3. Altered versions must be plainly marked as such, and must not be
76 * misrepresented as being the original software. Since few users
77 * ever read sources, credits should appear in the
80 * 4. This notice may not be removed or altered.
88 #include <ac/string.h>
89 #include <ac/socket.h>
92 #include "../back-ldap/back-ldap.h"
93 #include "back-meta.h"
96 meta_back_compare( Operation *op, SlapReply *rs )
98 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
100 struct metasingleconn *lsc;
101 char *match = NULL, *err = NULL;
102 struct berval mmatch = { 0, NULL };
103 int candidates = 0, last = 0, i, count = 0, rc;
104 int cres = LDAP_SUCCESS, rres = LDAP_SUCCESS;
108 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
109 &op->o_req_ndn, NULL );
111 send_ldap_result( op, rs );
115 if ( !meta_back_dobind( lc, op ) ) {
116 rs->sr_err = LDAP_OTHER;
117 send_ldap_result( op, rs );
121 msgid = ch_calloc( sizeof( int ), li->ntargets );
122 if ( msgid == NULL ) {
127 * start an asynchronous compare for each candidate target
129 dc.conn = op->o_conn;
131 dc.ctx = "compareDn";
133 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
134 struct berval mdn = { 0, NULL };
135 struct berval mapped_attr = op->oq_compare.rs_ava->aa_desc->ad_cname;
136 struct berval mapped_value = op->oq_compare.rs_ava->aa_value;
138 if ( lsc->candidate != META_CANDIDATE ) {
144 * Rewrite the compare dn, if needed
146 dc.rwmap = &li->targets[ i ]->rwmap;
148 switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
149 case LDAP_UNWILLING_TO_PERFORM:
158 * if attr is objectClass, try to remap the value
160 if ( op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_objectClass ) {
161 ldap_back_map( &li->targets[ i ]->rwmap.rwm_oc,
162 &op->oq_compare.rs_ava->aa_value,
163 &mapped_value, BACKLDAP_MAP );
165 if ( mapped_value.bv_val == NULL || mapped_value.bv_val[0] == '\0' ) {
169 * else try to remap the attribute
172 ldap_back_map( &li->targets[ i ]->rwmap.rwm_at,
173 &op->oq_compare.rs_ava->aa_desc->ad_cname,
174 &mapped_attr, BACKLDAP_MAP );
175 if ( mapped_attr.bv_val == NULL || mapped_attr.bv_val[0] == '\0' ) {
181 * the compare op is spawned across the targets and the first
182 * that returns determines the result; a constraint on unicity
183 * of the result ought to be enforced
185 msgid[ i ] = ldap_compare( lc->conns[ i ].ld, mdn.bv_val,
186 mapped_attr.bv_val, mapped_value.bv_val );
187 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
191 if ( mapped_attr.bv_val != op->oq_compare.rs_ava->aa_desc->ad_cname.bv_val ) {
192 free( mapped_attr.bv_val );
194 if ( mapped_value.bv_val != op->oq_compare.rs_ava->aa_value.bv_val ) {
195 free( mapped_value.bv_val );
198 if ( msgid[ i ] == -1 ) {
208 for ( rc = 0, count = 0; candidates > 0; ) {
211 * FIXME: should we check for abandon?
213 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
215 LDAPMessage *res = NULL;
217 if ( msgid[ i ] == -1 ) {
221 lrc = ldap_result( lsc->ld, msgid[ i ],
226 * FIXME: should we yield?
233 } else if ( lrc == LDAP_RES_COMPARE ) {
240 rs->sr_err = ldap_result2error( lsc->ld, res, 1 );
241 switch ( rs->sr_err ) {
242 case LDAP_COMPARE_TRUE:
243 case LDAP_COMPARE_FALSE:
246 * true or flase, got it;
247 * sending to cache ...
249 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
250 ( void )meta_dncache_update_entry( &li->cache, &op->o_req_ndn, i );
258 rres = ldap_back_map_result( rs );
263 ldap_get_option( lsc->ld,
264 LDAP_OPT_ERROR_STRING, &err );
266 if ( match != NULL ) {
269 ldap_get_option( lsc->ld,
270 LDAP_OPT_MATCHED_DN, &match );
292 * Rewrite the matched portion of the search base, if required
294 * FIXME: only the last one gets caught!
297 if ( match != NULL ) {
303 * the result of the compare is assigned to the res code
304 * that will be returned
309 * At least one compare failed with matched portion,
310 * and none was successful
312 } else if ( match != NULL && match[0] != '\0' ) {
313 struct berval matched;
315 matched.bv_val = match;
316 matched.bv_len = strlen( match );
318 dc.ctx = "matchedDn";
319 ldap_back_dn_massage( &dc, &matched, &mmatch );
323 rs->sr_matched = mmatch.bv_val;
324 send_ldap_result( op, rs );
325 rs->sr_matched = NULL;
327 if ( match != NULL ) {
328 if ( mmatch.bv_val != match ) {
329 free( mmatch.bv_val );