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 } };
49 Slapi_PBlock *pb = op->o_pb;
55 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
57 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
60 * Parse the compare request. It looks like this:
62 * CompareRequest := [APPLICATION 14] SEQUENCE {
63 * entry DistinguishedName,
66 * value AttributeValue
71 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
73 LDAP_LOG( OPERATION, ERR,
74 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
76 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
78 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
79 return SLAPD_DISCONNECT;
82 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
84 LDAP_LOG( OPERATION, ERR,
85 "do_compare: conn %d get ava failed\n", op->o_connid, 0, 0 );
87 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
89 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
90 return SLAPD_DISCONNECT;
93 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
95 LDAP_LOG( OPERATION, ERR,
96 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
98 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
100 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
101 return SLAPD_DISCONNECT;
104 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
106 LDAP_LOG( OPERATION, INFO,
107 "do_compare: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
109 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
114 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
115 if( rs->sr_err != LDAP_SUCCESS ) {
117 LDAP_LOG( OPERATION, INFO,
118 "do_compare: conn %d invalid dn (%s)\n",
119 op->o_connid, dn.bv_val, 0 );
121 Debug( LDAP_DEBUG_ANY,
122 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
124 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
128 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
129 if( rs->sr_err != LDAP_SUCCESS ) {
130 send_ldap_result( op, rs );
134 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
135 ava.aa_desc->ad_type->sat_equality,
136 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
137 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
138 if( rs->sr_err != LDAP_SUCCESS ) {
139 send_ldap_result( op, rs );
143 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
145 LDAP_LOG( OPERATION, ARGS,
146 "do_compare: dn (%s) attr(%s) value (%s)\n",
147 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
149 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
150 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
153 Statslog( LDAP_DEBUG_STATS,
154 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
155 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
156 ava.aa_desc->ad_cname.bv_val, 0 );
158 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
159 send_ldap_result( op, rs );
163 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
164 if( rs->sr_err != LDAP_SUCCESS ) {
165 send_ldap_result( op, rs );
169 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
171 LDAP_LOG( OPERATION, ARGS,
172 "do_compare: dn (%s) attr(%s) value (%s)\n",
173 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val,
174 ava.aa_value.bv_val );
176 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
177 op->o_req_dn.bv_val, 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 );
191 rs->sr_err = schema_info( &entry, &rs->sr_text );
192 if( rs->sr_err != LDAP_SUCCESS ) {
193 send_ldap_result( op, rs );
200 rs->sr_err = compare_entry( op, entry, &ava );
203 send_ldap_result( op, rs );
205 if( rs->sr_err == LDAP_COMPARE_TRUE || rs->sr_err == LDAP_COMPARE_FALSE ) {
212 manageDSAit = get_manageDSAit( op );
215 * We could be serving multiple database backends. Select the
216 * appropriate one, or send a referral to our "referral server"
217 * if we don't hold it.
219 if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
220 rs->sr_ref = referral_rewrite( default_referral,
221 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
223 rs->sr_err = LDAP_REFERRAL;
224 if (!rs->sr_ref) rs->sr_ref = default_referral;
225 send_ldap_result( op, rs );
227 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
232 /* check restrictions */
233 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
234 send_ldap_result( op, rs );
238 /* check for referrals */
239 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
244 LDAP_LOG( OPERATION, ARGS,
245 "do_compare: dn (%s) attr(%s) value (%s)\n",
246 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
248 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
249 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
252 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
253 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
254 ava.aa_desc->ad_cname.bv_val, 0 );
256 #if defined( LDAP_SLAPI )
257 slapi_x_pblock_set_operation( pb, op );
258 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
259 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
260 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
261 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
263 rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
264 if ( rs->sr_err < 0 ) {
266 * A preoperation plugin failure will abort the
270 LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
271 "failed\n", 0, 0, 0);
273 Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
274 "failed.\n", 0, 0, 0);
276 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
277 rs->sr_err == LDAP_SUCCESS ) {
278 rs->sr_err = LDAP_OTHER;
282 #endif /* defined( LDAP_SLAPI ) */
284 if ( op->o_bd->be_compare ) {
286 op->o_bd->be_compare( op, rs );
288 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
289 "operation not supported within namingContext" );
292 #if defined( LDAP_SLAPI )
293 if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 ) {
295 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
296 "failed\n", 0, 0, 0 );
298 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
299 "failed.\n", 0, 0, 0);
302 #endif /* defined( LDAP_SLAPI ) */
305 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
306 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
307 if ( ava.aa_value.bv_val ) op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
312 static int compare_entry(
315 AttributeAssertion *ava )
317 int rc = LDAP_NO_SUCH_ATTRIBUTE;
320 if ( ! access_allowed( op, e,
321 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
323 return LDAP_INSUFFICIENT_ACCESS;
326 for(a = attrs_find( e->e_attrs, ava->aa_desc );
328 a = attrs_find( a->a_next, ava->aa_desc ))
330 rc = LDAP_COMPARE_FALSE;
332 if ( value_find_ex( ava->aa_desc,
333 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
334 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
336 &ava->aa_value, op->o_tmpmemctx ) == 0 )
338 rc = LDAP_COMPARE_TRUE;