2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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>
39 struct berval dn = BER_BVNULL;
40 struct berval desc = BER_BVNULL;
41 struct berval value = BER_BVNULL;
42 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
44 Debug( LDAP_DEBUG_TRACE, "%s do_compare\n",
45 op->o_log_prefix, 0, 0 );
47 * Parse the compare request. It looks like this:
49 * CompareRequest := [APPLICATION 14] SEQUENCE {
50 * entry DistinguishedName,
53 * value AttributeValue
58 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
59 Debug( LDAP_DEBUG_ANY, "%s do_compare: ber_scanf failed\n",
60 op->o_log_prefix, 0, 0 );
61 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
62 return SLAPD_DISCONNECT;
65 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
66 Debug( LDAP_DEBUG_ANY, "%s do_compare: get ava failed\n",
67 op->o_log_prefix, 0, 0 );
68 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
69 return SLAPD_DISCONNECT;
72 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
73 Debug( LDAP_DEBUG_ANY, "%s do_compare: ber_scanf failed\n",
74 op->o_log_prefix, 0, 0 );
75 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
76 return SLAPD_DISCONNECT;
79 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
80 Debug( LDAP_DEBUG_ANY, "%s do_compare: get_ctrls failed\n",
81 op->o_log_prefix, 0, 0 );
85 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
87 if( rs->sr_err != LDAP_SUCCESS ) {
88 Debug( LDAP_DEBUG_ANY, "%s do_compare: invalid dn (%s)\n",
89 op->o_log_prefix, dn.bv_val, 0 );
90 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
94 Statslog( LDAP_DEBUG_STATS,
95 "%s CMP dn=\"%s\" attr=\"%s\"\n",
96 op->o_log_prefix, op->o_req_dn.bv_val,
99 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
100 if( rs->sr_err != LDAP_SUCCESS ) {
101 rs->sr_err = slap_bv2undef_ad( &desc, &ava.aa_desc,
103 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
104 if( rs->sr_err != LDAP_SUCCESS ) {
105 send_ldap_result( op, rs );
110 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
111 ava.aa_desc->ad_type->sat_equality,
112 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
113 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
114 if( rs->sr_err != LDAP_SUCCESS ) {
115 send_ldap_result( op, rs );
121 Debug( LDAP_DEBUG_ARGS,
122 "do_compare: dn (%s) attr (%s) value (%s)\n",
124 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
126 op->o_bd = frontendDB;
127 rs->sr_err = frontendDB->be_compare( op, rs );
128 if ( rs->sr_err == SLAPD_ASYNCOP ) {
134 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
135 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
136 if ( !BER_BVISNULL( &ava.aa_value ) ) {
137 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
144 fe_op_compare( Operation *op, SlapReply *rs )
147 AttributeAssertion *ava = op->orc_ava;
148 BackendDB *bd = op->o_bd;
150 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 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 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
164 send_ldap_result( op, rs );
169 rs->sr_err = schema_info( &entry, &rs->sr_text );
170 if( rs->sr_err != LDAP_SUCCESS ) {
171 send_ldap_result( op, rs );
178 rs->sr_err = slap_compare_entry( op, entry, ava );
181 send_ldap_result( op, rs );
183 if( rs->sr_err == LDAP_COMPARE_TRUE ||
184 rs->sr_err == LDAP_COMPARE_FALSE )
186 rs->sr_err = LDAP_SUCCESS;
193 * We could be serving multiple database backends. Select the
194 * appropriate one, or send a referral to our "referral server"
195 * if we don't hold it.
197 op->o_bd = select_backend( &op->o_req_ndn, 0 );
198 if ( op->o_bd == NULL ) {
199 rs->sr_ref = referral_rewrite( default_referral,
200 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
202 rs->sr_err = LDAP_REFERRAL;
203 if (!rs->sr_ref) rs->sr_ref = default_referral;
205 send_ldap_result( op, rs );
207 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
212 /* check restrictions */
213 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
214 send_ldap_result( op, rs );
218 /* check for referrals */
219 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
223 if ( SLAP_SHADOW(op->o_bd) && get_dontUseCopy(op) ) {
224 /* don't use shadow copy */
225 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
228 } else if ( ava->aa_desc == slap_schema.si_ad_entryDN ) {
229 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
230 "entryDN compare not supported" );
232 } else if ( ava->aa_desc == slap_schema.si_ad_subschemaSubentry ) {
233 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
234 "subschemaSubentry compare not supported" );
236 #ifndef SLAP_COMPARE_IN_FRONTEND
237 } else if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates
238 && op->o_bd->be_has_subordinates )
240 int rc, hasSubordinates = LDAP_SUCCESS;
242 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
243 if ( rc == 0 && entry ) {
244 if ( ! access_allowed( op, entry,
245 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
247 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
250 rc = rs->sr_err = op->o_bd->be_has_subordinates( op,
251 entry, &hasSubordinates );
252 be_entry_release_r( op, entry );
259 asserted = bvmatch( &ava->aa_value, &slap_true_bv )
260 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
261 if ( hasSubordinates == asserted ) {
262 rs->sr_err = LDAP_COMPARE_TRUE;
265 rs->sr_err = LDAP_COMPARE_FALSE;
269 /* return error only if "disclose"
270 * is granted on the object */
271 if ( backend_access( op, NULL, &op->o_req_ndn,
272 slap_schema.si_ad_entry,
273 NULL, ACL_DISCLOSE, NULL ) == LDAP_INSUFFICIENT_ACCESS )
275 rs->sr_err = LDAP_NO_SUCH_OBJECT;
279 send_ldap_result( op, rs );
282 rs->sr_err = LDAP_SUCCESS;
285 } else if ( op->o_bd->be_compare ) {
286 rs->sr_err = op->o_bd->be_compare( op, rs );
288 #endif /* ! SLAP_COMPARE_IN_FRONTEND */
290 rs->sr_err = SLAP_CB_CONTINUE;
293 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
294 /* do our best to compare that AVA
296 * NOTE: this code is used only
297 * if SLAP_COMPARE_IN_FRONTEND
298 * is #define'd (it's not by default)
299 * or if op->o_bd->be_compare is NULL.
301 * FIXME: one potential issue is that
302 * if SLAP_COMPARE_IN_FRONTEND overlays
303 * are not executed for compare. */
304 BerVarray vals = NULL;
307 rs->sr_err = backend_attribute( op, NULL, &op->o_req_ndn,
308 ava->aa_desc, &vals, ACL_COMPARE );
309 switch ( rs->sr_err ) {
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 )
316 == LDAP_INSUFFICIENT_ACCESS )
318 rs->sr_err = LDAP_NO_SUCH_OBJECT;
323 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
324 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
325 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
326 vals, &ava->aa_value, op->o_tmpmemctx ) == 0 )
328 rs->sr_err = LDAP_COMPARE_TRUE;
332 rs->sr_err = LDAP_COMPARE_FALSE;
338 send_ldap_result( op, rs );
341 rs->sr_err = LDAP_SUCCESS;
345 ber_bvarray_free_x( vals, op->o_tmpmemctx );
354 int slap_compare_entry(
357 AttributeAssertion *ava )
359 int rc = LDAP_COMPARE_FALSE;
362 if ( ! access_allowed( op, e,
363 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
365 rc = LDAP_INSUFFICIENT_ACCESS;
369 if ( get_assert( op ) &&
370 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
372 rc = LDAP_ASSERTION_FAILED;
376 a = attrs_find( e->e_attrs, ava->aa_desc );
378 rc = LDAP_NO_SUCH_ATTRIBUTE;
384 a = attrs_find( a->a_next, ava->aa_desc ))
386 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
387 e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
389 rc = LDAP_INSUFFICIENT_ACCESS;
393 if ( attr_valfind( a,
394 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
395 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
396 &ava->aa_value, NULL, op->o_tmpmemctx ) == 0 )
398 rc = LDAP_COMPARE_TRUE;
404 if( rc != LDAP_COMPARE_TRUE && rc != LDAP_COMPARE_FALSE ) {
405 if ( ! access_allowed( op, e,
406 slap_schema.si_ad_entry, NULL, ACL_DISCLOSE, NULL ) )
408 rc = LDAP_NO_SUCH_OBJECT;