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 );
49 struct berval dn = BER_BVNULL;
50 struct berval desc = BER_BVNULL;
51 struct berval value = BER_BVNULL;
52 AttributeAssertion ava = { NULL, BER_BVNULL };
58 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
60 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
63 * Parse the compare request. It looks like this:
65 * CompareRequest := [APPLICATION 14] SEQUENCE {
66 * entry DistinguishedName,
69 * value AttributeValue
74 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
76 LDAP_LOG( OPERATION, ERR,
77 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
79 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 ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
87 LDAP_LOG( OPERATION, ERR,
88 "do_compare: conn %d get ava failed\n", op->o_connid, 0, 0 );
90 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
92 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
93 return SLAPD_DISCONNECT;
96 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
98 LDAP_LOG( OPERATION, ERR,
99 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
101 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
103 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
104 return SLAPD_DISCONNECT;
107 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
109 LDAP_LOG( OPERATION, INFO,
110 "do_compare: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
112 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
117 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
119 if( rs->sr_err != LDAP_SUCCESS ) {
121 LDAP_LOG( OPERATION, INFO,
122 "do_compare: conn %d invalid dn (%s)\n",
123 op->o_connid, dn.bv_val, 0 );
125 Debug( LDAP_DEBUG_ANY,
126 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
128 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
132 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
133 if( rs->sr_err != LDAP_SUCCESS ) {
134 send_ldap_result( op, rs );
138 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
139 ava.aa_desc->ad_type->sat_equality,
140 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
141 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
142 if( rs->sr_err != LDAP_SUCCESS ) {
143 send_ldap_result( op, rs );
147 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
149 LDAP_LOG( OPERATION, ARGS,
150 "do_compare: dn (%s) attr(%s) value (%s)\n",
152 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
154 Debug( LDAP_DEBUG_ARGS,
155 "do_compare: dn (%s) attr (%s) value (%s)\n",
157 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
160 Statslog( LDAP_DEBUG_STATS,
161 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
162 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
163 ava.aa_desc->ad_cname.bv_val, 0 );
165 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
166 send_ldap_result( op, rs );
170 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
171 if( rs->sr_err != LDAP_SUCCESS ) {
172 send_ldap_result( op, rs );
176 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
178 LDAP_LOG( OPERATION, ARGS,
179 "do_compare: dn (%s) attr(%s) value (%s)\n",
181 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
183 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
185 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
188 Statslog( LDAP_DEBUG_STATS,
189 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
190 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
191 ava.aa_desc->ad_cname.bv_val, 0 );
193 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
194 send_ldap_result( op, rs );
199 rs->sr_err = schema_info( &entry, &rs->sr_text );
200 if( rs->sr_err != LDAP_SUCCESS ) {
201 send_ldap_result( op, rs );
208 rs->sr_err = compare_entry( op, entry, &ava );
211 send_ldap_result( op, rs );
213 if( rs->sr_err == LDAP_COMPARE_TRUE ||
214 rs->sr_err == LDAP_COMPARE_FALSE )
216 rs->sr_err = LDAP_SUCCESS;
222 manageDSAit = get_manageDSAit( op );
225 * We could be serving multiple database backends. Select the
226 * appropriate one, or send a referral to our "referral server"
227 * if we don't hold it.
229 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
230 if ( op->o_bd == NULL ) {
231 rs->sr_ref = referral_rewrite( default_referral,
232 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
234 rs->sr_err = LDAP_REFERRAL;
235 if (!rs->sr_ref) rs->sr_ref = default_referral;
236 send_ldap_result( op, rs );
238 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
243 /* check restrictions */
244 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
245 send_ldap_result( op, rs );
249 /* check for referrals */
250 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
255 LDAP_LOG( OPERATION, ARGS,
256 "do_compare: dn (%s) attr(%s) value (%s)\n",
258 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
260 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
262 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
265 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
266 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
267 ava.aa_desc->ad_cname.bv_val, 0 );
269 #if defined( LDAP_SLAPI )
272 slapi_int_pblock_set_operation( pb, op );
273 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
274 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
275 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
276 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
278 rs->sr_err = slapi_int_call_plugins( op->o_bd,
279 SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
280 if ( rs->sr_err < 0 ) {
282 * A preoperation plugin failure will abort the
286 LDAP_LOG( OPERATION, INFO,
287 "do_compare: compare preoperation plugin failed\n",
290 Debug(LDAP_DEBUG_TRACE,
291 "do_compare: compare preoperation plugin failed\n",
294 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
295 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
297 rs->sr_err = LDAP_OTHER;
302 #endif /* defined( LDAP_SLAPI ) */
304 if ( op->o_bd->be_compare ) {
306 op->o_bd->be_compare( op, rs );
308 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
309 "operation not supported within namingContext" );
312 #if defined( LDAP_SLAPI )
313 if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
314 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
317 LDAP_LOG( OPERATION, INFO,
318 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
320 Debug(LDAP_DEBUG_TRACE,
321 "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
324 #endif /* defined( LDAP_SLAPI ) */
327 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
328 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
329 if ( ava.aa_value.bv_val ) {
330 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
335 static int compare_entry(
338 AttributeAssertion *ava )
340 int rc = LDAP_NO_SUCH_ATTRIBUTE;
343 if ( ! access_allowed( op, e,
344 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
346 return LDAP_INSUFFICIENT_ACCESS;
349 for(a = attrs_find( e->e_attrs, ava->aa_desc );
351 a = attrs_find( a->a_next, ava->aa_desc ))
353 rc = LDAP_COMPARE_FALSE;
355 if ( value_find_ex( ava->aa_desc,
356 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
357 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
359 &ava->aa_value, op->o_tmpmemctx ) == 0 )
361 rc = LDAP_COMPARE_TRUE;