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 };
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 send_ldap_result( op, rs );
213 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
218 /* check restrictions */
219 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
220 send_ldap_result( op, rs );
224 /* check for referrals */
225 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
229 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
231 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
233 Statslog( LDAP_DEBUG_STATS, "%s CMP dn=\"%s\" attr=\"%s\"\n",
234 op->o_log_prefix, op->o_req_dn.bv_val,
235 ava.aa_desc->ad_cname.bv_val, 0, 0 );
237 #if defined( LDAP_SLAPI )
240 slapi_int_pblock_set_operation( pb, op );
241 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
242 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
243 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
244 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
246 rs->sr_err = slapi_int_call_plugins( op->o_bd,
247 SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
248 if ( rs->sr_err < 0 ) {
250 * A preoperation plugin failure will abort the
253 Debug(LDAP_DEBUG_TRACE,
254 "do_compare: compare preoperation plugin failed\n",
256 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
257 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
259 rs->sr_err = LDAP_OTHER;
264 #endif /* defined( LDAP_SLAPI ) */
267 if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
268 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
269 "entryDN compare not supported" );
271 } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
272 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
273 "subschemaSubentry compare not supported" );
275 } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
276 && op->o_bd->be_has_subordinates )
278 int rc, hasSubordinates = LDAP_SUCCESS;
280 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
281 if ( rc == 0 && entry ) {
282 rc = op->o_bd->be_has_subordinates( op, entry,
284 be_entry_release_r( op, entry );
290 asserted = bvmatch( &ava.aa_value, &slap_true_bv )
291 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
292 if ( hasSubordinates == asserted ) {
293 rs->sr_err = LDAP_COMPARE_TRUE;
295 rs->sr_err = LDAP_COMPARE_FALSE;
298 send_ldap_result( op, rs );
300 if( rc == 0 ) rs->sr_err = LDAP_SUCCESS;
302 } else if ( op->o_bd->be_compare ) {
303 op->o_bd->be_compare( op, rs );
306 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
307 "operation not supported within namingContext" );
310 #if defined( LDAP_SLAPI )
311 if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
312 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
314 Debug(LDAP_DEBUG_TRACE,
315 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
317 #endif /* defined( LDAP_SLAPI ) */
323 static int compare_entry(
326 AttributeAssertion *ava )
331 if ( ! access_allowed( op, e,
332 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
334 return LDAP_INSUFFICIENT_ACCESS;
337 a = attrs_find( e->e_attrs, ava->aa_desc );
338 if( a == NULL ) return LDAP_NO_SUCH_ATTRIBUTE;
340 rc = LDAP_COMPARE_FALSE;
341 for(a = attrs_find( e->e_attrs, ava->aa_desc );
343 a = attrs_find( a->a_next, ava->aa_desc ))
345 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
346 e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
348 rc = LDAP_INSUFFICIENT_ACCESS;
352 if ( value_find_ex( ava->aa_desc,
353 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
354 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
356 &ava->aa_value, op->o_tmpmemctx ) == 0 )
358 rc = LDAP_COMPARE_TRUE;