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 };
56 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
58 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
61 * Parse the compare request. It looks like this:
63 * CompareRequest := [APPLICATION 14] SEQUENCE {
64 * entry DistinguishedName,
67 * value AttributeValue
72 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
74 LDAP_LOG( OPERATION, ERR,
75 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
77 Debug( LDAP_DEBUG_ANY, "ber_scanf 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, "{mm}", &desc, &value ) == LBER_ERROR ) {
85 LDAP_LOG( OPERATION, ERR,
86 "do_compare: conn %d get ava failed\n", op->o_connid, 0, 0 );
88 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
90 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
91 return SLAPD_DISCONNECT;
94 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
96 LDAP_LOG( OPERATION, ERR,
97 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
99 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
101 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
102 return SLAPD_DISCONNECT;
105 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
107 LDAP_LOG( OPERATION, INFO,
108 "do_compare: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
110 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
115 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
117 if( rs->sr_err != LDAP_SUCCESS ) {
119 LDAP_LOG( OPERATION, INFO,
120 "do_compare: conn %d invalid dn (%s)\n",
121 op->o_connid, dn.bv_val, 0 );
123 Debug( LDAP_DEBUG_ANY,
124 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
126 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
130 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
131 if( rs->sr_err != LDAP_SUCCESS ) {
132 send_ldap_result( op, rs );
136 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
137 ava.aa_desc->ad_type->sat_equality,
138 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
139 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
140 if( rs->sr_err != LDAP_SUCCESS ) {
141 send_ldap_result( op, rs );
147 op->o_bd = frontendDB;
148 rs->sr_err = frontendDB->be_compare( op, rs );
151 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
152 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
153 if ( ava.aa_value.bv_val ) {
154 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
161 fe_op_compare( Operation *op, SlapReply *rs )
165 AttributeAssertion ava = *op->orc_ava;
167 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
169 LDAP_LOG( OPERATION, ARGS,
170 "do_compare: dn (%s) attr(%s) value (%s)\n",
172 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
174 Debug( LDAP_DEBUG_ARGS,
175 "do_compare: dn (%s) attr (%s) value (%s)\n",
177 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
180 Statslog( LDAP_DEBUG_STATS,
181 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
182 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
183 ava.aa_desc->ad_cname.bv_val, 0 );
185 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
186 send_ldap_result( op, rs );
190 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
191 if( rs->sr_err != LDAP_SUCCESS ) {
192 send_ldap_result( op, rs );
196 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
198 LDAP_LOG( OPERATION, ARGS,
199 "do_compare: dn (%s) attr(%s) value (%s)\n",
201 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
203 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
205 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
208 Statslog( LDAP_DEBUG_STATS,
209 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
210 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
211 ava.aa_desc->ad_cname.bv_val, 0 );
213 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
214 send_ldap_result( op, rs );
219 rs->sr_err = schema_info( &entry, &rs->sr_text );
220 if( rs->sr_err != LDAP_SUCCESS ) {
221 send_ldap_result( op, rs );
228 rs->sr_err = compare_entry( op, entry, &ava );
231 send_ldap_result( op, rs );
233 if( rs->sr_err == LDAP_COMPARE_TRUE ||
234 rs->sr_err == LDAP_COMPARE_FALSE )
236 rs->sr_err = LDAP_SUCCESS;
242 manageDSAit = get_manageDSAit( op );
245 * We could be serving multiple database backends. Select the
246 * appropriate one, or send a referral to our "referral server"
247 * if we don't hold it.
249 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
250 if ( op->o_bd == NULL ) {
251 rs->sr_ref = referral_rewrite( default_referral,
252 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
254 rs->sr_err = LDAP_REFERRAL;
255 if (!rs->sr_ref) rs->sr_ref = default_referral;
256 send_ldap_result( op, rs );
258 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
263 /* check restrictions */
264 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
265 send_ldap_result( op, rs );
269 /* check for referrals */
270 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
275 LDAP_LOG( OPERATION, ARGS,
276 "do_compare: dn (%s) attr(%s) value (%s)\n",
278 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
280 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
282 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
285 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
286 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
287 ava.aa_desc->ad_cname.bv_val, 0 );
289 #if defined( LDAP_SLAPI )
292 slapi_int_pblock_set_operation( pb, op );
293 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
294 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
295 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
296 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
298 rs->sr_err = slapi_int_call_plugins( op->o_bd,
299 SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
300 if ( rs->sr_err < 0 ) {
302 * A preoperation plugin failure will abort the
306 LDAP_LOG( OPERATION, INFO,
307 "do_compare: compare preoperation plugin failed\n",
310 Debug(LDAP_DEBUG_TRACE,
311 "do_compare: compare preoperation plugin failed\n",
314 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
315 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
317 rs->sr_err = LDAP_OTHER;
322 #endif /* defined( LDAP_SLAPI ) */
325 if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
326 && op->o_bd->be_has_subordinates )
328 int rc, hasSubordinates = LDAP_SUCCESS;
330 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL,
332 if ( rc == 0 && entry ) {
333 rc = op->o_bd->be_has_subordinates( op, entry,
335 be_entry_release_r( op, entry );
341 asserted = bvmatch( &ava.aa_value, &slap_true_bv )
342 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
343 if ( hasSubordinates == asserted ) {
344 rs->sr_err = LDAP_COMPARE_TRUE;
346 rs->sr_err = LDAP_COMPARE_FALSE;
349 send_ldap_result( op, rs );
351 if ( rs->sr_err == LDAP_COMPARE_TRUE ||
352 rs->sr_err == LDAP_COMPARE_FALSE )
354 rs->sr_err = LDAP_SUCCESS;
357 } else if ( op->o_bd->be_compare ) {
358 op->o_bd->be_compare( op, rs );
361 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
362 "operation not supported within namingContext" );
365 #if defined( LDAP_SLAPI )
366 if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
367 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
370 LDAP_LOG( OPERATION, INFO,
371 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
373 Debug(LDAP_DEBUG_TRACE,
374 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
377 #endif /* defined( LDAP_SLAPI ) */
383 static int compare_entry(
386 AttributeAssertion *ava )
388 int rc = LDAP_NO_SUCH_ATTRIBUTE;
391 if ( ! access_allowed( op, e,
392 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
394 return LDAP_INSUFFICIENT_ACCESS;
397 for(a = attrs_find( e->e_attrs, ava->aa_desc );
399 a = attrs_find( a->a_next, ava->aa_desc ))
401 rc = LDAP_COMPARE_FALSE;
403 if ( value_find_ex( ava->aa_desc,
404 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
405 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
407 &ava->aa_value, op->o_tmpmemctx ) == 0 )
409 rc = LDAP_COMPARE_TRUE;