2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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>
35 #include "slapi/slapi.h"
38 static int compare_entry(
41 AttributeAssertion *ava );
48 struct berval dn = BER_BVNULL;
49 struct berval desc = BER_BVNULL;
50 struct berval value = BER_BVNULL;
51 AttributeAssertion ava = { NULL, BER_BVNULL };
55 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
57 * Parse the compare request. It looks like this:
59 * CompareRequest := [APPLICATION 14] SEQUENCE {
60 * entry DistinguishedName,
63 * value AttributeValue
68 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
69 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
70 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
71 return SLAPD_DISCONNECT;
74 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
75 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
76 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
77 return SLAPD_DISCONNECT;
80 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
81 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
82 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
83 return SLAPD_DISCONNECT;
86 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
87 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
91 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
93 if( rs->sr_err != LDAP_SUCCESS ) {
94 Debug( LDAP_DEBUG_ANY,
95 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
96 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
100 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
101 if( rs->sr_err != LDAP_SUCCESS ) {
102 send_ldap_result( op, rs );
106 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
107 ava.aa_desc->ad_type->sat_equality,
108 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
109 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
110 if( rs->sr_err != LDAP_SUCCESS ) {
111 send_ldap_result( op, rs );
117 op->o_bd = frontendDB;
118 rs->sr_err = frontendDB->be_compare( op, rs );
121 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
122 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
123 if ( ava.aa_value.bv_val ) {
124 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
131 fe_op_compare( Operation *op, SlapReply *rs )
135 AttributeAssertion ava = *op->orc_ava;
137 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
138 Debug( LDAP_DEBUG_ARGS,
139 "do_compare: dn (%s) attr (%s) value (%s)\n",
141 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
143 Statslog( LDAP_DEBUG_STATS,
144 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
145 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
146 ava.aa_desc->ad_cname.bv_val, 0 );
148 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
149 send_ldap_result( op, rs );
153 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
154 if( rs->sr_err != LDAP_SUCCESS ) {
155 send_ldap_result( op, rs );
159 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
160 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
162 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
164 Statslog( LDAP_DEBUG_STATS,
165 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
166 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
167 ava.aa_desc->ad_cname.bv_val, 0 );
169 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
170 send_ldap_result( op, rs );
175 rs->sr_err = schema_info( &entry, &rs->sr_text );
176 if( rs->sr_err != LDAP_SUCCESS ) {
177 send_ldap_result( op, rs );
184 rs->sr_err = compare_entry( op, entry, &ava );
187 send_ldap_result( op, rs );
189 if( rs->sr_err == LDAP_COMPARE_TRUE ||
190 rs->sr_err == LDAP_COMPARE_FALSE )
192 rs->sr_err = LDAP_SUCCESS;
198 manageDSAit = get_manageDSAit( op );
201 * We could be serving multiple database backends. Select the
202 * appropriate one, or send a referral to our "referral server"
203 * if we don't hold it.
205 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
206 if ( op->o_bd == NULL ) {
207 rs->sr_ref = referral_rewrite( default_referral,
208 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
210 rs->sr_err = LDAP_REFERRAL;
211 if (!rs->sr_ref) rs->sr_ref = default_referral;
212 send_ldap_result( op, rs );
214 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
219 /* check restrictions */
220 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
221 send_ldap_result( op, rs );
225 /* check for referrals */
226 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
230 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
232 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
234 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
235 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
236 ava.aa_desc->ad_cname.bv_val, 0 );
238 #if defined( LDAP_SLAPI )
241 slapi_int_pblock_set_operation( pb, op );
242 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
243 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
244 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
245 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
247 rs->sr_err = slapi_int_call_plugins( op->o_bd,
248 SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
249 if ( rs->sr_err < 0 ) {
251 * A preoperation plugin failure will abort the
254 Debug(LDAP_DEBUG_TRACE,
255 "do_compare: compare preoperation plugin failed\n",
257 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
258 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
260 rs->sr_err = LDAP_OTHER;
265 #endif /* defined( LDAP_SLAPI ) */
268 if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
269 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
270 "entryDN compare not supported" );
272 } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
273 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
274 "subschemaSubentry compare not supported" );
276 } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
277 && op->o_bd->be_has_subordinates )
279 int rc, hasSubordinates = LDAP_SUCCESS;
281 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
282 if ( rc == 0 && entry ) {
283 rc = op->o_bd->be_has_subordinates( op, entry,
285 be_entry_release_r( op, entry );
291 asserted = bvmatch( &ava.aa_value, &slap_true_bv )
292 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
293 if ( hasSubordinates == asserted ) {
294 rs->sr_err = LDAP_COMPARE_TRUE;
296 rs->sr_err = LDAP_COMPARE_FALSE;
299 send_ldap_result( op, rs );
301 if( rc == 0 ) rs->sr_err = LDAP_SUCCESS;
303 } else if ( op->o_bd->be_compare ) {
304 op->o_bd->be_compare( op, rs );
307 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
308 "operation not supported within namingContext" );
311 #if defined( LDAP_SLAPI )
312 if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
313 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
315 Debug(LDAP_DEBUG_TRACE,
316 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
318 #endif /* defined( LDAP_SLAPI ) */
324 static int compare_entry(
327 AttributeAssertion *ava )
329 int rc = LDAP_NO_SUCH_ATTRIBUTE;
332 if ( ! access_allowed( op, e,
333 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
335 return LDAP_INSUFFICIENT_ACCESS;
338 for(a = attrs_find( e->e_attrs, ava->aa_desc );
340 a = attrs_find( a->a_next, ava->aa_desc ))
342 rc = LDAP_COMPARE_FALSE;
344 if ( value_find_ex( ava->aa_desc,
345 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
346 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
348 &ava->aa_value, op->o_tmpmemctx ) == 0 )
350 rc = LDAP_COMPARE_TRUE;