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(
33 AttributeAssertion *ava );
43 struct berval dn = { 0, NULL };
44 struct berval pdn = { 0, NULL };
45 struct berval ndn = { 0, NULL };
46 struct berval desc = { 0, NULL };
47 struct berval value = { 0, NULL };
48 AttributeAssertion ava = { NULL, { 0, NULL } };
50 int rc = LDAP_SUCCESS;
51 const char *text = NULL;
55 Slapi_PBlock *pb = op->o_pb;
61 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", conn->c_connid, 0, 0 );
63 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
66 * Parse the compare request. It looks like this:
68 * CompareRequest := [APPLICATION 14] SEQUENCE {
69 * entry DistinguishedName,
72 * value AttributeValue
77 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
79 LDAP_LOG( OPERATION, ERR,
80 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
82 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
84 send_ldap_disconnect( conn, op,
85 LDAP_PROTOCOL_ERROR, "decoding error" );
86 return SLAPD_DISCONNECT;
89 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
91 LDAP_LOG( OPERATION, ERR,
92 "do_compare: conn %d get ava failed\n", conn->c_connid, 0, 0 );
94 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
96 send_ldap_disconnect( conn, op,
97 LDAP_PROTOCOL_ERROR, "decoding error" );
98 rc = SLAPD_DISCONNECT;
102 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
104 LDAP_LOG( OPERATION, ERR,
105 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
107 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
109 send_ldap_disconnect( conn, op,
110 LDAP_PROTOCOL_ERROR, "decoding error" );
111 rc = SLAPD_DISCONNECT;
115 if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
117 LDAP_LOG( OPERATION, INFO,
118 "do_compare: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
120 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
125 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
126 if( rc != LDAP_SUCCESS ) {
128 LDAP_LOG( OPERATION, INFO,
129 "do_compare: conn %d invalid dn (%s)\n",
130 conn->c_connid, dn.bv_val, 0 );
132 Debug( LDAP_DEBUG_ANY,
133 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
135 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
136 "invalid DN", NULL, NULL );
140 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
141 if( rc != LDAP_SUCCESS ) {
142 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
147 rc = asserted_value_validate_normalize( ava.aa_desc,
148 ava.aa_desc->ad_type->sat_equality,
149 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
150 &value, &ava.aa_value, &text );
152 rc = value_validate_normalize( ava.aa_desc, SLAP_MR_EQUALITY,
153 &value, &ava.aa_value, &text );
155 if( rc != LDAP_SUCCESS ) {
156 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
160 if( strcasecmp( ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
162 LDAP_LOG( OPERATION, ARGS,
163 "do_compare: dn (%s) attr(%s) value (%s)\n",
164 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
166 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
167 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
170 Statslog( LDAP_DEBUG_STATS,
171 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
172 op->o_connid, op->o_opid, pdn.bv_val,
173 ava.aa_desc->ad_cname.bv_val, 0 );
175 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
176 if( rc != LDAP_SUCCESS ) {
177 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
181 rc = root_dse_info( conn, &entry, &text );
182 if( rc != LDAP_SUCCESS ) {
183 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
189 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
191 LDAP_LOG( OPERATION, ARGS,
192 "do_compare: dn (%s) attr(%s) value (%s)\n",
193 pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
194 ava.aa_value.bv_val );
196 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
197 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
200 Statslog( LDAP_DEBUG_STATS,
201 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
202 op->o_connid, op->o_opid, pdn.bv_val,
203 ava.aa_desc->ad_cname.bv_val, 0 );
205 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
206 if( rc != LDAP_SUCCESS ) {
207 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
212 rc = schema_info( &entry, &text );
213 if( rc != LDAP_SUCCESS ) {
214 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
222 rc = compare_entry( conn, op, entry, &ava );
223 if( fentry) entry_free( fentry );
225 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
227 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
234 manageDSAit = get_manageDSAit( op );
237 * We could be serving multiple database backends. Select the
238 * appropriate one, or send a referral to our "referral server"
239 * if we don't hold it.
241 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
242 BerVarray ref = referral_rewrite( default_referral,
243 NULL, &pdn, LDAP_SCOPE_DEFAULT );
245 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
246 NULL, NULL, ref ? ref : default_referral, NULL );
248 ber_bvarray_free( ref );
253 /* check restrictions */
254 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
255 if( rc != LDAP_SUCCESS ) {
256 send_ldap_result( conn, op, rc,
257 NULL, text, NULL, NULL );
261 /* check for referrals */
262 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
263 if ( rc != LDAP_SUCCESS ) {
268 LDAP_LOG( OPERATION, ARGS,
269 "do_compare: dn (%s) attr(%s) value (%s)\n",
270 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
272 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
273 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
276 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
277 op->o_connid, op->o_opid, pdn.bv_val,
278 ava.aa_desc->ad_cname.bv_val, 0 );
280 #if defined( LDAP_SLAPI )
281 slapi_x_backend_set_pb( pb, be );
282 slapi_x_connection_set_pb( pb, conn );
283 slapi_x_operation_set_pb( pb, op );
284 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
285 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
286 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
287 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
289 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
292 * A preoperation plugin failure will abort the
296 LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
297 "failed\n", 0, 0, 0);
299 Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
300 "failed.\n", 0, 0, 0);
302 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0)
306 #endif /* defined( LDAP_SLAPI ) */
308 if ( be->be_compare ) {
309 (*be->be_compare)( be, conn, op, &pdn, &ndn, &ava );
311 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
312 NULL, "operation not supported within namingContext",
316 #if defined( LDAP_SLAPI )
317 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) != 0 ) {
319 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
320 "failed\n", 0, 0, 0 );
322 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
323 "failed.\n", 0, 0, 0);
326 #endif /* defined( LDAP_SLAPI ) */
331 if ( ava.aa_value.bv_val ) free( ava.aa_value.bv_val );
336 static int compare_entry(
340 AttributeAssertion *ava )
342 int rc = LDAP_NO_SUCH_ATTRIBUTE;
345 if ( ! access_allowed( NULL, conn, op, e,
346 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
348 return LDAP_INSUFFICIENT_ACCESS;
351 for(a = attrs_find( e->e_attrs, ava->aa_desc );
353 a = attrs_find( a->a_next, ava->aa_desc ))
355 rc = LDAP_COMPARE_FALSE;
358 if ( value_find_ex( ava->aa_desc,
359 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
360 a->a_nvals ? a->a_nvals : a->a_vals,
361 &ava->aa_value ) == 0 )
363 if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 )
366 rc = LDAP_COMPARE_TRUE;