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 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
54 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
56 * Parse the compare request. It looks like this:
58 * CompareRequest := [APPLICATION 14] SEQUENCE {
59 * entry DistinguishedName,
62 * value AttributeValue
67 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
68 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
69 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
70 return SLAPD_DISCONNECT;
73 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
74 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
75 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
76 return SLAPD_DISCONNECT;
79 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
80 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
81 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
82 return SLAPD_DISCONNECT;
85 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
86 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
90 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
92 if( rs->sr_err != LDAP_SUCCESS ) {
93 Debug( LDAP_DEBUG_ANY,
94 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
95 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
99 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
100 if( rs->sr_err != LDAP_SUCCESS ) {
101 send_ldap_result( op, rs );
105 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
106 ava.aa_desc->ad_type->sat_equality,
107 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
108 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
109 if( rs->sr_err != LDAP_SUCCESS ) {
110 send_ldap_result( op, rs );
116 op->o_bd = frontendDB;
117 rs->sr_err = frontendDB->be_compare( op, rs );
120 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
121 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
122 if ( ava.aa_value.bv_val ) {
123 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
130 fe_op_compare( Operation *op, SlapReply *rs )
134 AttributeAssertion ava = *op->orc_ava;
136 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
137 Debug( LDAP_DEBUG_ARGS,
138 "do_compare: dn (%s) attr (%s) value (%s)\n",
140 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
142 Statslog( LDAP_DEBUG_STATS,
143 "%s CMP dn=\"%s\" attr=\"%s\"\n",
144 op->o_log_prefix, op->o_req_dn.bv_val,
145 ava.aa_desc->ad_cname.bv_val, 0, 0 );
147 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
148 send_ldap_result( op, rs );
152 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
153 if( rs->sr_err != LDAP_SUCCESS ) {
154 send_ldap_result( op, rs );
158 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
159 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
161 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
163 Statslog( LDAP_DEBUG_STATS,
164 "%s CMP dn=\"%s\" attr=\"%s\"\n",
165 op->o_log_prefix, op->o_req_dn.bv_val,
166 ava.aa_desc->ad_cname.bv_val, 0, 0 );
168 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
169 send_ldap_result( op, rs );
174 rs->sr_err = schema_info( &entry, &rs->sr_text );
175 if( rs->sr_err != LDAP_SUCCESS ) {
176 send_ldap_result( op, rs );
183 rs->sr_err = compare_entry( op, entry, &ava );
186 send_ldap_result( op, rs );
188 if( rs->sr_err == LDAP_COMPARE_TRUE ||
189 rs->sr_err == LDAP_COMPARE_FALSE )
191 rs->sr_err = LDAP_SUCCESS;
197 manageDSAit = get_manageDSAit( op );
200 * We could be serving multiple database backends. Select the
201 * appropriate one, or send a referral to our "referral server"
202 * if we don't hold it.
204 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
205 if ( op->o_bd == NULL ) {
206 rs->sr_ref = referral_rewrite( default_referral,
207 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
209 rs->sr_err = LDAP_REFERRAL;
210 if (!rs->sr_ref) rs->sr_ref = default_referral;
211 op->o_bd = frontendDB;
212 send_ldap_result( op, rs );
215 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
220 /* check restrictions */
221 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
222 send_ldap_result( op, rs );
226 /* check for referrals */
227 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
231 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
233 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
235 Statslog( LDAP_DEBUG_STATS, "%s CMP dn=\"%s\" attr=\"%s\"\n",
236 op->o_log_prefix, op->o_req_dn.bv_val,
237 ava.aa_desc->ad_cname.bv_val, 0, 0 );
239 #if defined( LDAP_SLAPI )
242 slapi_int_pblock_set_operation( pb, op );
243 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
244 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
245 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
246 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
248 rs->sr_err = slapi_int_call_plugins( op->o_bd,
249 SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
250 if ( rs->sr_err < 0 ) {
252 * A preoperation plugin failure will abort the
255 Debug(LDAP_DEBUG_TRACE,
256 "do_compare: compare preoperation plugin failed\n",
258 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
259 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
261 rs->sr_err = LDAP_OTHER;
266 #endif /* defined( LDAP_SLAPI ) */
269 if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
270 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
271 "entryDN compare not supported" );
273 } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
274 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
275 "subschemaSubentry compare not supported" );
277 #ifndef SLAP_COMPARE_IN_FRONTEND
278 } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
279 && op->o_bd->be_has_subordinates )
281 int rc, hasSubordinates = LDAP_SUCCESS;
283 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
284 if ( rc == 0 && entry ) {
285 if ( ! access_allowed( op, entry,
286 ava.aa_desc, &ava.aa_value, ACL_COMPARE, NULL ) )
288 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
291 rc = rs->sr_err = op->o_bd->be_has_subordinates( op,
292 entry, &hasSubordinates );
293 be_entry_release_r( op, entry );
300 asserted = bvmatch( &ava.aa_value, &slap_true_bv )
301 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
302 if ( hasSubordinates == asserted ) {
303 rs->sr_err = LDAP_COMPARE_TRUE;
306 rs->sr_err = LDAP_COMPARE_FALSE;
310 #ifdef SLAP_ACL_HONOR_DISCLOSE
311 /* return error only if "disclose"
312 * is granted on the object */
313 if ( backend_access( op, NULL, &op->o_req_ndn,
314 slap_schema.si_ad_entry,
315 NULL, ACL_DISCLOSE, NULL ) == LDAP_INSUFFICIENT_ACCESS )
317 rs->sr_err = LDAP_NO_SUCH_OBJECT;
319 #endif /* SLAP_ACL_HONOR_DISCLOSE */
322 send_ldap_result( op, rs );
325 rs->sr_err = LDAP_SUCCESS;
328 } else if ( op->o_bd->be_compare ) {
329 rs->sr_err = op->o_bd->be_compare( op, rs );
331 #endif /* ! SLAP_COMPARE_IN_FRONTEND */
333 rs->sr_err = SLAP_CB_CONTINUE;
336 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
337 /* do our best to compare that AVA
339 * NOTE: this code is used only
340 * if SLAP_COMPARE_IN_FRONTEND
341 * is #define'd (it's not by default)
342 * or if op->o_bd->be_compare is NULL.
344 * FIXME: one potential issue is that
345 * if SLAP_COMPARE_IN_FRONTEND overlays
346 * are not executed for compare. */
347 BerVarray vals = NULL;
350 rs->sr_err = backend_attribute( op, NULL, &op->o_req_ndn,
351 ava.aa_desc, &vals, ACL_COMPARE );
352 switch ( rs->sr_err ) {
354 #ifdef SLAP_ACL_HONOR_DISCLOSE
355 /* return error only if "disclose"
356 * is granted on the object */
357 if ( backend_access( op, NULL, &op->o_req_ndn,
358 slap_schema.si_ad_entry,
359 NULL, ACL_DISCLOSE, NULL )
360 == LDAP_INSUFFICIENT_ACCESS )
362 rs->sr_err = LDAP_NO_SUCH_OBJECT;
364 #endif /* SLAP_ACL_HONOR_DISCLOSE */
368 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
369 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
370 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
371 vals, &ava.aa_value, op->o_tmpmemctx ) == 0 )
373 rs->sr_err = LDAP_COMPARE_TRUE;
377 rs->sr_err = LDAP_COMPARE_FALSE;
383 send_ldap_result( op, rs );
386 rs->sr_err = LDAP_SUCCESS;
390 ber_bvarray_free_x( vals, op->o_tmpmemctx );
394 #if defined( LDAP_SLAPI )
395 if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
396 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
398 Debug(LDAP_DEBUG_TRACE,
399 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
401 #endif /* defined( LDAP_SLAPI ) */
407 static int compare_entry(
410 AttributeAssertion *ava )
415 if ( ! access_allowed( op, e,
416 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
418 return LDAP_INSUFFICIENT_ACCESS;
421 a = attrs_find( e->e_attrs, ava->aa_desc );
422 if( a == NULL ) return LDAP_NO_SUCH_ATTRIBUTE;
424 rc = LDAP_COMPARE_FALSE;
425 for(a = attrs_find( e->e_attrs, ava->aa_desc );
427 a = attrs_find( a->a_next, ava->aa_desc ))
429 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
430 e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
432 rc = LDAP_INSUFFICIENT_ACCESS;
436 if ( value_find_ex( ava->aa_desc,
437 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
438 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
440 &ava->aa_value, op->o_tmpmemctx ) == 0 )
442 rc = LDAP_COMPARE_TRUE;