3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
21 #include <ac/socket.h>
22 #include <ac/string.h>
30 static int compare_entry(
33 AttributeAssertion *ava );
42 struct berval dn = { 0, NULL };
43 struct berval desc = { 0, NULL };
44 struct berval value = { 0, NULL };
45 AttributeAssertion ava = { NULL, { 0, NULL } };
51 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
53 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 ) {
69 LDAP_LOG( OPERATION, ERR,
70 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
72 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
74 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
75 return SLAPD_DISCONNECT;
78 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
80 LDAP_LOG( OPERATION, ERR,
81 "do_compare: conn %d get ava failed\n", op->o_connid, 0, 0 );
83 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
85 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
86 return SLAPD_DISCONNECT;
89 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
91 LDAP_LOG( OPERATION, ERR,
92 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
94 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
96 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
97 return SLAPD_DISCONNECT;
100 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
102 LDAP_LOG( OPERATION, INFO,
103 "do_compare: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
105 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
110 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
111 if( rs->sr_err != LDAP_SUCCESS ) {
113 LDAP_LOG( OPERATION, INFO,
114 "do_compare: conn %d invalid dn (%s)\n",
115 op->o_connid, dn.bv_val, 0 );
117 Debug( LDAP_DEBUG_ANY,
118 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
120 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
124 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
125 if( rs->sr_err != LDAP_SUCCESS ) {
126 send_ldap_result( op, rs );
130 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
131 ava.aa_desc->ad_type->sat_equality,
132 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
133 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
134 if( rs->sr_err != LDAP_SUCCESS ) {
135 send_ldap_result( op, rs );
139 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
141 LDAP_LOG( OPERATION, ARGS,
142 "do_compare: dn (%s) attr(%s) value (%s)\n",
143 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
145 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
146 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
149 Statslog( LDAP_DEBUG_STATS,
150 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
151 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
152 ava.aa_desc->ad_cname.bv_val, 0 );
154 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
155 send_ldap_result( op, rs );
159 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
160 if( rs->sr_err != LDAP_SUCCESS ) {
161 send_ldap_result( op, rs );
165 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
167 LDAP_LOG( OPERATION, ARGS,
168 "do_compare: dn (%s) attr(%s) value (%s)\n",
169 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val,
170 ava.aa_value.bv_val );
172 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
173 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
176 Statslog( LDAP_DEBUG_STATS,
177 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
178 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
179 ava.aa_desc->ad_cname.bv_val, 0 );
181 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
182 send_ldap_result( op, rs );
187 rs->sr_err = schema_info( &entry, &rs->sr_text );
188 if( rs->sr_err != LDAP_SUCCESS ) {
189 send_ldap_result( op, rs );
196 rs->sr_err = compare_entry( op, entry, &ava );
199 send_ldap_result( op, rs );
201 if( rs->sr_err == LDAP_COMPARE_TRUE || rs->sr_err == LDAP_COMPARE_FALSE ) {
208 manageDSAit = get_manageDSAit( op );
211 * We could be serving multiple database backends. Select the
212 * appropriate one, or send a referral to our "referral server"
213 * if we don't hold it.
215 if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
216 rs->sr_ref = referral_rewrite( default_referral,
217 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
219 rs->sr_err = LDAP_REFERRAL;
220 if (!rs->sr_ref) rs->sr_ref = default_referral;
221 send_ldap_result( op, rs );
223 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
228 /* check restrictions */
229 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
230 send_ldap_result( op, rs );
234 /* check for referrals */
235 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
240 LDAP_LOG( OPERATION, ARGS,
241 "do_compare: dn (%s) attr(%s) value (%s)\n",
242 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
244 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
245 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
248 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
249 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
250 ava.aa_desc->ad_cname.bv_val, 0 );
252 #if defined( LDAP_SLAPI )
255 slapi_x_pblock_set_operation( pb, op );
256 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
257 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
258 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
259 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
261 rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
262 if ( rs->sr_err < 0 ) {
264 * A preoperation plugin failure will abort the
268 LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
269 "failed\n", 0, 0, 0);
271 Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
272 "failed.\n", 0, 0, 0);
274 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
275 rs->sr_err == LDAP_SUCCESS ) {
276 rs->sr_err = LDAP_OTHER;
281 #endif /* defined( LDAP_SLAPI ) */
283 if ( op->o_bd->be_compare ) {
285 op->o_bd->be_compare( op, rs );
287 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
288 "operation not supported within namingContext" );
291 #if defined( LDAP_SLAPI )
292 if ( pb && doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 ) {
294 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
295 "failed\n", 0, 0, 0 );
297 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
298 "failed.\n", 0, 0, 0);
301 #endif /* defined( LDAP_SLAPI ) */
304 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
305 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
306 if ( ava.aa_value.bv_val ) op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
311 static int compare_entry(
314 AttributeAssertion *ava )
316 int rc = LDAP_NO_SUCH_ATTRIBUTE;
319 if ( ! access_allowed( op, e,
320 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
322 return LDAP_INSUFFICIENT_ACCESS;
325 for(a = attrs_find( e->e_attrs, ava->aa_desc );
327 a = attrs_find( a->a_next, ava->aa_desc ))
329 rc = LDAP_COMPARE_FALSE;
331 if ( value_find_ex( ava->aa_desc,
332 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
333 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
335 &ava->aa_value, op->o_tmpmemctx ) == 0 )
337 rc = LDAP_COMPARE_TRUE;