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>
27 static int compare_entry(
31 AttributeAssertion *ava );
41 struct berval dn = { 0, NULL };
42 struct berval pdn = { 0, NULL };
43 struct berval ndn = { 0, NULL };
44 struct berval desc = { 0, NULL };
45 struct berval value = { 0, NULL };
46 AttributeAssertion ava = { NULL, { 0, NULL } };
48 int rc = LDAP_SUCCESS;
49 const char *text = NULL;
52 Slapi_PBlock *pb = op->o_pb;
57 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", conn->c_connid, 0, 0 );
59 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
62 * Parse the compare request. It looks like this:
64 * CompareRequest := [APPLICATION 14] SEQUENCE {
65 * entry DistinguishedName,
68 * value AttributeValue
73 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
75 LDAP_LOG( OPERATION, ERR,
76 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
78 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
80 send_ldap_disconnect( conn, op,
81 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", conn->c_connid, 0, 0 );
90 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
92 send_ldap_disconnect( conn, op,
93 LDAP_PROTOCOL_ERROR, "decoding error" );
94 rc = SLAPD_DISCONNECT;
98 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
100 LDAP_LOG( OPERATION, ERR,
101 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
103 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
105 send_ldap_disconnect( conn, op,
106 LDAP_PROTOCOL_ERROR, "decoding error" );
107 rc = SLAPD_DISCONNECT;
111 if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
113 LDAP_LOG( OPERATION, INFO,
114 "do_compare: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
116 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
121 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
122 if( rc != LDAP_SUCCESS ) {
124 LDAP_LOG( OPERATION, INFO,
125 "do_compare: conn %d invalid dn (%s)\n",
126 conn->c_connid, dn.bv_val, 0 );
128 Debug( LDAP_DEBUG_ANY,
129 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
131 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
132 "invalid DN", NULL, NULL );
136 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
137 if( rc != LDAP_SUCCESS ) {
138 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
142 rc = value_validate_normalize( ava.aa_desc, SLAP_MR_EQUALITY,
143 &value, &ava.aa_value, &text );
144 if( rc != LDAP_SUCCESS ) {
145 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
149 if( strcasecmp( ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
151 LDAP_LOG( OPERATION, ARGS,
152 "do_compare: dn (%s) attr(%s) value (%s)\n",
153 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
155 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
156 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
159 Statslog( LDAP_DEBUG_STATS,
160 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
161 op->o_connid, op->o_opid, pdn.bv_val,
162 ava.aa_desc->ad_cname.bv_val, 0 );
164 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
165 if( rc != LDAP_SUCCESS ) {
166 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
170 rc = root_dse_info( conn, &entry, &text );
171 if( rc != LDAP_SUCCESS ) {
172 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
178 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
180 LDAP_LOG( OPERATION, ARGS,
181 "do_compare: dn (%s) attr(%s) value (%s)\n",
182 pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
183 ava.aa_value.bv_val );
185 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
186 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
189 Statslog( LDAP_DEBUG_STATS,
190 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
191 op->o_connid, op->o_opid, pdn.bv_val,
192 ava.aa_desc->ad_cname.bv_val, 0 );
194 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
195 if( rc != LDAP_SUCCESS ) {
196 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
201 rc = schema_info( &entry, &text );
202 if( rc != LDAP_SUCCESS ) {
203 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
211 rc = compare_entry( conn, op, entry, &ava );
212 if( fentry) entry_free( fentry );
214 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
216 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
223 manageDSAit = get_manageDSAit( op );
226 * We could be serving multiple database backends. Select the
227 * appropriate one, or send a referral to our "referral server"
228 * if we don't hold it.
230 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
231 BerVarray ref = referral_rewrite( default_referral,
232 NULL, &pdn, LDAP_SCOPE_DEFAULT );
234 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
235 NULL, NULL, ref ? ref : default_referral, NULL );
237 ber_bvarray_free( ref );
242 /* check restrictions */
243 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
244 if( rc != LDAP_SUCCESS ) {
245 send_ldap_result( conn, op, rc,
246 NULL, text, NULL, NULL );
250 /* check for referrals */
251 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
252 if ( rc != LDAP_SUCCESS ) {
257 LDAP_LOG( OPERATION, ARGS,
258 "do_compare: dn (%s) attr(%s) value (%s)\n",
259 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
261 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
262 pdn.bv_val, 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, pdn.bv_val,
267 ava.aa_desc->ad_cname.bv_val, 0 );
270 /* deref suffix alias if appropriate */
271 suffix_alias( be, &ndn );
273 #if defined( LDAP_SLAPI )
274 slapi_x_backend_set_pb( pb, be );
275 slapi_x_connection_set_pb( pb, conn );
276 slapi_x_operation_set_pb( pb, op );
277 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
278 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
279 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
280 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
282 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
285 * A preoperation plugin failure will abort the
289 LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
290 "failed\n", 0, 0, 0);
292 Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
293 "failed.\n", 0, 0, 0);
295 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0)
299 #endif /* defined( LDAP_SLAPI ) */
301 if ( be->be_compare ) {
302 (*be->be_compare)( be, conn, op, &pdn, &ndn, &ava );
304 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
305 NULL, "operation not supported within namingContext",
309 #if defined( LDAP_SLAPI )
310 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) != 0 ) {
312 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
313 "failed\n", 0, 0, 0 );
315 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
316 "failed.\n", 0, 0, 0);
319 #endif /* defined( LDAP_SLAPI ) */
324 if ( ava.aa_value.bv_val ) free( ava.aa_value.bv_val );
329 static int compare_entry(
333 AttributeAssertion *ava )
335 int rc = LDAP_NO_SUCH_ATTRIBUTE;
338 if ( ! access_allowed( NULL, conn, op, e,
339 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
341 return LDAP_INSUFFICIENT_ACCESS;
344 for(a = attrs_find( e->e_attrs, ava->aa_desc );
346 a = attrs_find( a->a_next, ava->aa_desc ))
348 rc = LDAP_COMPARE_FALSE;
350 if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 ) {
351 rc = LDAP_COMPARE_TRUE;