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;
53 Slapi_PBlock *pb = op->o_pb;
59 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", conn->c_connid, 0, 0 );
61 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
64 * Parse the compare request. It looks like this:
66 * CompareRequest := [APPLICATION 14] SEQUENCE {
67 * entry DistinguishedName,
70 * value AttributeValue
75 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
77 LDAP_LOG( OPERATION, ERR,
78 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
80 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
82 send_ldap_disconnect( conn, op,
83 LDAP_PROTOCOL_ERROR, "decoding error" );
84 return SLAPD_DISCONNECT;
87 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
89 LDAP_LOG( OPERATION, ERR,
90 "do_compare: conn %d get ava failed\n", conn->c_connid, 0, 0 );
92 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
94 send_ldap_disconnect( conn, op,
95 LDAP_PROTOCOL_ERROR, "decoding error" );
96 rc = SLAPD_DISCONNECT;
100 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
102 LDAP_LOG( OPERATION, ERR,
103 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
105 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
107 send_ldap_disconnect( conn, op,
108 LDAP_PROTOCOL_ERROR, "decoding error" );
109 rc = SLAPD_DISCONNECT;
113 if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
115 LDAP_LOG( OPERATION, INFO,
116 "do_compare: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
118 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
123 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
124 if( rc != LDAP_SUCCESS ) {
126 LDAP_LOG( OPERATION, INFO,
127 "do_compare: conn %d invalid dn (%s)\n",
128 conn->c_connid, dn.bv_val, 0 );
130 Debug( LDAP_DEBUG_ANY,
131 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
133 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
134 "invalid DN", NULL, NULL );
138 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
139 if( rc != LDAP_SUCCESS ) {
140 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
144 rc = value_validate_normalize( ava.aa_desc, SLAP_MR_EQUALITY,
145 &value, &ava.aa_value, &text );
146 if( rc != LDAP_SUCCESS ) {
147 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
151 if( strcasecmp( ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
153 LDAP_LOG( OPERATION, ARGS,
154 "do_compare: dn (%s) attr(%s) value (%s)\n",
155 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
157 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
158 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
161 Statslog( LDAP_DEBUG_STATS,
162 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
163 op->o_connid, op->o_opid, pdn.bv_val,
164 ava.aa_desc->ad_cname.bv_val, 0 );
166 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
167 if( rc != LDAP_SUCCESS ) {
168 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
172 rc = root_dse_info( conn, &entry, &text );
173 if( rc != LDAP_SUCCESS ) {
174 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
180 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
182 LDAP_LOG( OPERATION, ARGS,
183 "do_compare: dn (%s) attr(%s) value (%s)\n",
184 pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
185 ava.aa_value.bv_val );
187 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
188 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
191 Statslog( LDAP_DEBUG_STATS,
192 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
193 op->o_connid, op->o_opid, pdn.bv_val,
194 ava.aa_desc->ad_cname.bv_val, 0 );
196 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
197 if( rc != LDAP_SUCCESS ) {
198 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
203 rc = schema_info( &entry, &text );
204 if( rc != LDAP_SUCCESS ) {
205 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
213 rc = compare_entry( conn, op, entry, &ava );
214 if( fentry) entry_free( fentry );
216 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
218 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
225 manageDSAit = get_manageDSAit( op );
228 * We could be serving multiple database backends. Select the
229 * appropriate one, or send a referral to our "referral server"
230 * if we don't hold it.
232 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
233 BerVarray ref = referral_rewrite( default_referral,
234 NULL, &pdn, LDAP_SCOPE_DEFAULT );
236 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
237 NULL, NULL, ref ? ref : default_referral, NULL );
239 ber_bvarray_free( ref );
244 /* check restrictions */
245 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
246 if( rc != LDAP_SUCCESS ) {
247 send_ldap_result( conn, op, rc,
248 NULL, text, NULL, NULL );
252 /* check for referrals */
253 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
254 if ( rc != LDAP_SUCCESS ) {
259 LDAP_LOG( OPERATION, ARGS,
260 "do_compare: dn (%s) attr(%s) value (%s)\n",
261 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
263 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
264 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
267 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
268 op->o_connid, op->o_opid, pdn.bv_val,
269 ava.aa_desc->ad_cname.bv_val, 0 );
272 /* deref suffix alias if appropriate */
273 suffix_alias( be, &ndn );
275 #if defined( LDAP_SLAPI )
276 slapi_x_backend_set_pb( pb, be );
277 slapi_x_connection_set_pb( pb, conn );
278 slapi_x_operation_set_pb( pb, op );
279 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
280 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
281 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
282 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
284 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
287 * A preoperation plugin failure will abort the
291 LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
292 "failed\n", 0, 0, 0);
294 Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
295 "failed.\n", 0, 0, 0);
297 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0)
301 #endif /* defined( LDAP_SLAPI ) */
303 if ( be->be_compare ) {
304 (*be->be_compare)( be, conn, op, &pdn, &ndn, &ava );
306 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
307 NULL, "operation not supported within namingContext",
311 #if defined( LDAP_SLAPI )
312 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) != 0 ) {
314 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
315 "failed\n", 0, 0, 0 );
317 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
318 "failed.\n", 0, 0, 0);
321 #endif /* defined( LDAP_SLAPI ) */
326 if ( ava.aa_value.bv_val ) free( ava.aa_value.bv_val );
331 static int compare_entry(
335 AttributeAssertion *ava )
337 int rc = LDAP_NO_SUCH_ATTRIBUTE;
340 if ( ! access_allowed( NULL, conn, op, e,
341 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
343 return LDAP_INSUFFICIENT_ACCESS;
346 for(a = attrs_find( e->e_attrs, ava->aa_desc );
348 a = attrs_find( a->a_next, ava->aa_desc ))
350 rc = LDAP_COMPARE_FALSE;
352 if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 ) {
353 rc = LDAP_COMPARE_TRUE;