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>
29 static int compare_entry(
32 AttributeAssertion *ava );
41 struct berval dn = { 0, NULL };
42 struct berval desc = { 0, NULL };
43 struct berval value = { 0, NULL };
44 AttributeAssertion ava = { NULL, { 0, NULL } };
48 Slapi_PBlock *pb = op->o_pb;
54 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
56 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
59 * Parse the compare request. It looks like this:
61 * CompareRequest := [APPLICATION 14] SEQUENCE {
62 * entry DistinguishedName,
65 * value AttributeValue
70 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
72 LDAP_LOG( OPERATION, ERR,
73 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
75 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
77 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
78 return SLAPD_DISCONNECT;
81 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
83 LDAP_LOG( OPERATION, ERR,
84 "do_compare: conn %d get ava failed\n", op->o_connid, 0, 0 );
86 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
88 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
89 return SLAPD_DISCONNECT;
92 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
94 LDAP_LOG( OPERATION, ERR,
95 "do_compare: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
97 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
99 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
100 return SLAPD_DISCONNECT;
103 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
105 LDAP_LOG( OPERATION, INFO,
106 "do_compare: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
108 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
113 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
114 if( rs->sr_err != LDAP_SUCCESS ) {
116 LDAP_LOG( OPERATION, INFO,
117 "do_compare: conn %d invalid dn (%s)\n",
118 op->o_connid, dn.bv_val, 0 );
120 Debug( LDAP_DEBUG_ANY,
121 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
123 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
127 rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
128 if( rs->sr_err != LDAP_SUCCESS ) {
129 send_ldap_result( op, rs );
133 rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
134 ava.aa_desc->ad_type->sat_equality,
135 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
136 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
137 if( rs->sr_err != LDAP_SUCCESS ) {
138 send_ldap_result( op, rs );
142 if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
144 LDAP_LOG( OPERATION, ARGS,
145 "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 );
148 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
149 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
152 Statslog( LDAP_DEBUG_STATS,
153 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
154 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
155 ava.aa_desc->ad_cname.bv_val, 0 );
157 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
158 send_ldap_result( op, rs );
162 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
163 if( rs->sr_err != LDAP_SUCCESS ) {
164 send_ldap_result( op, rs );
168 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
170 LDAP_LOG( OPERATION, ARGS,
171 "do_compare: dn (%s) attr(%s) value (%s)\n",
172 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val,
173 ava.aa_value.bv_val );
175 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
176 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
179 Statslog( LDAP_DEBUG_STATS,
180 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
181 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
182 ava.aa_desc->ad_cname.bv_val, 0 );
184 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
185 send_ldap_result( op, rs );
190 rs->sr_err = schema_info( &entry, &rs->sr_text );
191 if( rs->sr_err != LDAP_SUCCESS ) {
192 send_ldap_result( op, rs );
199 rs->sr_err = compare_entry( op, entry, &ava );
202 send_ldap_result( op, rs );
204 if( rs->sr_err == LDAP_COMPARE_TRUE || rs->sr_err == LDAP_COMPARE_FALSE ) {
211 manageDSAit = get_manageDSAit( op );
214 * We could be serving multiple database backends. Select the
215 * appropriate one, or send a referral to our "referral server"
216 * if we don't hold it.
218 if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
219 rs->sr_ref = referral_rewrite( default_referral,
220 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
222 rs->sr_err = LDAP_REFERRAL;
223 if (!rs->sr_ref) rs->sr_ref = default_referral;
224 send_ldap_result( op, rs );
226 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
231 /* check restrictions */
232 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
233 send_ldap_result( op, rs );
237 /* check for referrals */
238 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
243 LDAP_LOG( OPERATION, ARGS,
244 "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 );
247 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
248 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
251 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
252 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
253 ava.aa_desc->ad_cname.bv_val, 0 );
255 #if defined( LDAP_SLAPI )
256 slapi_x_pblock_set_operation( pb, op );
257 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
258 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
259 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
260 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
262 rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
263 if ( rs->sr_err != 0 ) {
265 * A preoperation plugin failure will abort the
269 LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
270 "failed\n", 0, 0, 0);
272 Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
273 "failed.\n", 0, 0, 0);
275 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0)
276 rs->sr_err = LDAP_OTHER;
279 #endif /* defined( LDAP_SLAPI ) */
281 if ( op->o_bd->be_compare ) {
283 op->o_bd->be_compare( op, rs );
285 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
286 "operation not supported within namingContext" );
289 #if defined( LDAP_SLAPI )
290 if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) != 0 ) {
292 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
293 "failed\n", 0, 0, 0 );
295 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
296 "failed.\n", 0, 0, 0);
299 #endif /* defined( LDAP_SLAPI ) */
302 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
303 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
304 if ( ava.aa_value.bv_val ) op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
309 static int compare_entry(
312 AttributeAssertion *ava )
314 int rc = LDAP_NO_SUCH_ATTRIBUTE;
317 if ( ! access_allowed( op, e,
318 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
320 return LDAP_INSUFFICIENT_ACCESS;
323 for(a = attrs_find( e->e_attrs, ava->aa_desc );
325 a = attrs_find( a->a_next, ava->aa_desc ))
327 rc = LDAP_COMPARE_FALSE;
329 if ( value_find_ex( ava->aa_desc,
330 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
331 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
333 &ava->aa_value, op->o_tmpmemctx ) == 0 )
335 rc = LDAP_COMPARE_TRUE;