2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "slapi/slapi.h"
37 static int compare_entry(
40 AttributeAssertion *ava );
47 struct berval dn = BER_BVNULL;
48 struct berval desc = BER_BVNULL;
49 struct berval value = BER_BVNULL;
50 #ifdef LDAP_COMP_MATCH
51 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
53 AttributeAssertion ava = { NULL, BER_BVNULL };
58 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
60 * Parse the compare request. It looks like this:
62 * CompareRequest := [APPLICATION 14] SEQUENCE {
63 * entry DistinguishedName,
66 * value AttributeValue
71 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
72 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
73 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
74 return SLAPD_DISCONNECT;
77 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
78 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
79 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
80 return SLAPD_DISCONNECT;
83 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
84 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
85 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
86 return SLAPD_DISCONNECT;
89 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
90 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
94 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
96 if( rs->sr_err != LDAP_SUCCESS ) {
97 Debug( LDAP_DEBUG_ANY,
98 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
99 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
103 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
104 if( rs->sr_err != LDAP_SUCCESS ) {
105 send_ldap_result( op, rs );
109 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
110 ava.aa_desc->ad_type->sat_equality,
111 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
112 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
113 if( rs->sr_err != LDAP_SUCCESS ) {
114 send_ldap_result( op, rs );
120 op->o_bd = frontendDB;
121 rs->sr_err = frontendDB->be_compare( op, rs );
124 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
125 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
126 if ( ava.aa_value.bv_val ) {
127 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
134 fe_op_compare( Operation *op, SlapReply *rs )
138 AttributeAssertion ava = *op->orc_ava;
140 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
141 Debug( LDAP_DEBUG_ARGS,
142 "do_compare: dn (%s) attr (%s) value (%s)\n",
144 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
146 Statslog( LDAP_DEBUG_STATS,
147 "%s CMP dn=\"%s\" attr=\"%s\"\n",
148 op->o_log_prefix, op->o_req_dn.bv_val,
149 ava.aa_desc->ad_cname.bv_val, 0, 0 );
151 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
152 send_ldap_result( op, rs );
156 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
157 if( rs->sr_err != LDAP_SUCCESS ) {
158 send_ldap_result( op, rs );
162 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
163 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
165 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
167 Statslog( LDAP_DEBUG_STATS,
168 "%s CMP dn=\"%s\" attr=\"%s\"\n",
169 op->o_log_prefix, op->o_req_dn.bv_val,
170 ava.aa_desc->ad_cname.bv_val, 0, 0 );
172 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
173 send_ldap_result( op, rs );
178 rs->sr_err = schema_info( &entry, &rs->sr_text );
179 if( rs->sr_err != LDAP_SUCCESS ) {
180 send_ldap_result( op, rs );
187 rs->sr_err = compare_entry( op, entry, &ava );
190 send_ldap_result( op, rs );
192 if( rs->sr_err == LDAP_COMPARE_TRUE ||
193 rs->sr_err == LDAP_COMPARE_FALSE )
195 rs->sr_err = LDAP_SUCCESS;
201 manageDSAit = get_manageDSAit( op );
204 * We could be serving multiple database backends. Select the
205 * appropriate one, or send a referral to our "referral server"
206 * if we don't hold it.
208 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
209 if ( op->o_bd == NULL ) {
210 rs->sr_ref = referral_rewrite( default_referral,
211 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
213 rs->sr_err = LDAP_REFERRAL;
214 if (!rs->sr_ref) rs->sr_ref = default_referral;
215 op->o_bd = frontendDB;
216 send_ldap_result( op, rs );
219 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
224 /* check restrictions */
225 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
226 send_ldap_result( op, rs );
230 /* check for referrals */
231 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
235 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
237 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
239 Statslog( LDAP_DEBUG_STATS, "%s CMP dn=\"%s\" attr=\"%s\"\n",
240 op->o_log_prefix, op->o_req_dn.bv_val,
241 ava.aa_desc->ad_cname.bv_val, 0, 0 );
243 #if defined( LDAP_SLAPI )
246 slapi_int_pblock_set_operation( pb, op );
247 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
248 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
249 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
250 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
252 rs->sr_err = slapi_int_call_plugins( op->o_bd,
253 SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
254 if ( rs->sr_err < 0 ) {
256 * A preoperation plugin failure will abort the
259 Debug(LDAP_DEBUG_TRACE,
260 "do_compare: compare preoperation plugin failed\n",
262 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
263 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
265 rs->sr_err = LDAP_OTHER;
270 #endif /* defined( LDAP_SLAPI ) */
273 if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
274 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
275 "entryDN compare not supported" );
277 } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
278 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
279 "subschemaSubentry compare not supported" );
281 #ifndef SLAP_COMPARE_IN_FRONTEND
282 } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
283 && op->o_bd->be_has_subordinates )
285 int rc, hasSubordinates = LDAP_SUCCESS;
287 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
288 if ( rc == 0 && entry ) {
289 if ( ! access_allowed( op, entry,
290 ava.aa_desc, &ava.aa_value, ACL_COMPARE, NULL ) )
292 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
295 rc = rs->sr_err = op->o_bd->be_has_subordinates( op,
296 entry, &hasSubordinates );
297 be_entry_release_r( op, entry );
304 asserted = bvmatch( &ava.aa_value, &slap_true_bv )
305 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
306 if ( hasSubordinates == asserted ) {
307 rs->sr_err = LDAP_COMPARE_TRUE;
310 rs->sr_err = LDAP_COMPARE_FALSE;
314 #ifdef SLAP_ACL_HONOR_DISCLOSE
315 /* return error only if "disclose"
316 * is granted on the object */
317 if ( backend_access( op, NULL, &op->o_req_ndn,
318 slap_schema.si_ad_entry,
319 NULL, ACL_DISCLOSE, NULL ) == LDAP_INSUFFICIENT_ACCESS )
321 rs->sr_err = LDAP_NO_SUCH_OBJECT;
323 #endif /* SLAP_ACL_HONOR_DISCLOSE */
326 send_ldap_result( op, rs );
329 rs->sr_err = LDAP_SUCCESS;
332 } else if ( op->o_bd->be_compare ) {
333 rs->sr_err = op->o_bd->be_compare( op, rs );
335 #endif /* ! SLAP_COMPARE_IN_FRONTEND */
337 rs->sr_err = SLAP_CB_CONTINUE;
340 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
341 /* do our best to compare that AVA
343 * NOTE: this code is used only
344 * if SLAP_COMPARE_IN_FRONTEND
345 * is #define'd (it's not by default)
346 * or if op->o_bd->be_compare is NULL.
348 * FIXME: one potential issue is that
349 * if SLAP_COMPARE_IN_FRONTEND overlays
350 * are not executed for compare. */
351 BerVarray vals = NULL;
354 rs->sr_err = backend_attribute( op, NULL, &op->o_req_ndn,
355 ava.aa_desc, &vals, ACL_COMPARE );
356 switch ( rs->sr_err ) {
358 #ifdef SLAP_ACL_HONOR_DISCLOSE
359 /* return error only if "disclose"
360 * is granted on the object */
361 if ( backend_access( op, NULL, &op->o_req_ndn,
362 slap_schema.si_ad_entry,
363 NULL, ACL_DISCLOSE, NULL )
364 == LDAP_INSUFFICIENT_ACCESS )
366 rs->sr_err = LDAP_NO_SUCH_OBJECT;
368 #endif /* SLAP_ACL_HONOR_DISCLOSE */
372 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
373 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
374 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
375 vals, &ava.aa_value, op->o_tmpmemctx ) == 0 )
377 rs->sr_err = LDAP_COMPARE_TRUE;
381 rs->sr_err = LDAP_COMPARE_FALSE;
387 send_ldap_result( op, rs );
390 rs->sr_err = LDAP_SUCCESS;
394 ber_bvarray_free_x( vals, op->o_tmpmemctx );
398 #if defined( LDAP_SLAPI )
399 if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
400 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
402 Debug(LDAP_DEBUG_TRACE,
403 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
405 #endif /* defined( LDAP_SLAPI ) */
411 static int compare_entry(
414 AttributeAssertion *ava )
416 int rc = LDAP_COMPARE_FALSE;
419 if ( ! access_allowed( op, e,
420 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
422 rc = LDAP_INSUFFICIENT_ACCESS;
426 a = attrs_find( e->e_attrs, ava->aa_desc );
428 rc = LDAP_NO_SUCH_ATTRIBUTE;
432 for(a = attrs_find( e->e_attrs, ava->aa_desc );
434 a = attrs_find( a->a_next, ava->aa_desc ))
436 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
437 e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
439 rc = LDAP_INSUFFICIENT_ACCESS;
443 if ( value_find_ex( ava->aa_desc,
444 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
445 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
446 a->a_nvals, &ava->aa_value, op->o_tmpmemctx ) == 0 )
448 rc = LDAP_COMPARE_TRUE;
454 #ifdef LDAP_ACL_HONOR_DISCLOSE
455 if( rc != LDAP_COMPARE_TRUE && rc != LDAP_COMPARE_FALSE ) {
456 if ( ! access_allowed( op, e,
457 slap_schema.si_ad_entry, NULL, ACL_DISCLOSE, NULL ) )
459 rc = LDAP_NO_SUCH_OBJECT;