3 * Copyright 1998-2000 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>
26 static int compare_entry(
30 AttributeAssertion *ava );
39 struct berval dn = { 0, NULL };
40 struct berval pdn = { 0, NULL };
41 struct berval ndn = { 0, NULL };
42 struct berval desc = { 0, NULL };
43 struct berval value = { 0, NULL };
44 struct berval *nvalue;
45 AttributeAssertion ava;
47 int rc = LDAP_SUCCESS;
48 const char *text = NULL;
54 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
55 "do_compare: conn %d\n", conn->c_connid ));
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, "{o" /*}*/, &dn ) == LBER_ERROR ) {
73 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
74 "do_compare: conn %d ber_scanf failed\n", conn->c_connid ));
76 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
78 send_ldap_disconnect( conn, op,
79 LDAP_PROTOCOL_ERROR, "decoding error" );
80 return SLAPD_DISCONNECT;
83 if ( ber_scanf( op->o_ber, "{oo}", &desc, &value ) == LBER_ERROR ) {
85 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
86 "do_compare: conn %d get ava failed\n", conn->c_connid ));
88 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
90 send_ldap_disconnect( conn, op,
91 LDAP_PROTOCOL_ERROR, "decoding error" );
92 rc = SLAPD_DISCONNECT;
96 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
98 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
99 "do_compare: conn %d ber_scanf failed\n", conn->c_connid ));
101 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
103 send_ldap_disconnect( conn, op,
104 LDAP_PROTOCOL_ERROR, "decoding error" );
105 rc = SLAPD_DISCONNECT;
109 if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
111 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
112 "do_compare: conn %d get_ctrls failed\n", conn->c_connid ));
114 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
119 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
120 if( rc != LDAP_SUCCESS ) {
122 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
123 "do_compare: conn %d invalid dn (%s)\n",
124 conn->c_connid, dn.bv_val ));
126 Debug( LDAP_DEBUG_ANY,
127 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
129 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
130 "invalid DN", NULL, NULL );
134 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
135 if( rc != LDAP_SUCCESS ) {
136 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
140 rc = value_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &nvalue, &text );
141 if( rc != LDAP_SUCCESS ) {
142 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
146 ava.aa_value = nvalue;
148 if( strcasecmp( ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
150 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
151 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
152 conn->c_connid, pdn.bv_val,
153 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=%ld op=%d 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 );
176 } else if ( strcasecmp( ndn.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
178 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
179 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
180 conn->c_connid, pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
181 ava.aa_value->bv_val ));
183 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
184 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
187 Statslog( LDAP_DEBUG_STATS,
188 "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
189 op->o_connid, op->o_opid, pdn.bv_val,
190 ava.aa_desc->ad_cname.bv_val, 0 );
192 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
193 if( rc != LDAP_SUCCESS ) {
194 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
199 rc = schema_info( &entry, &text );
200 if( rc != LDAP_SUCCESS ) {
201 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
208 rc = compare_entry( conn, op, entry, &ava );
211 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
213 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
220 manageDSAit = get_manageDSAit( op );
223 * We could be serving multiple database backends. Select the
224 * appropriate one, or send a referral to our "referral server"
225 * if we don't hold it.
227 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
228 struct berval **ref = referral_rewrite( default_referral,
229 NULL, &pdn, LDAP_SCOPE_DEFAULT );
231 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
232 NULL, NULL, ref ? ref : default_referral, NULL );
239 /* check restrictions */
240 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
241 if( rc != LDAP_SUCCESS ) {
242 send_ldap_result( conn, op, rc,
243 NULL, text, NULL, NULL );
247 /* check for referrals */
248 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
249 if ( rc != LDAP_SUCCESS ) {
254 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
255 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
256 conn->c_connid, pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
257 ava.aa_value->bv_val ));
259 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
260 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
263 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
264 op->o_connid, op->o_opid, pdn.bv_val,
265 ava.aa_desc->ad_cname.bv_val, 0 );
268 /* deref suffix alias if appropriate */
269 suffix_alias( be, &ndn );
271 if ( be->be_compare ) {
272 (*be->be_compare)( be, conn, op, &pdn, &ndn, &ava );
274 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
275 NULL, "operation not supported within namingContext",
284 free( value.bv_val );
289 static int compare_entry(
293 AttributeAssertion *ava )
295 int rc = LDAP_NO_SUCH_ATTRIBUTE;
298 if ( ! access_allowed( NULL, conn, op, e,
299 ava->aa_desc, ava->aa_value, ACL_COMPARE ) )
301 return LDAP_INSUFFICIENT_ACCESS;
304 for(a = attrs_find( e->e_attrs, ava->aa_desc );
306 a = attrs_find( a->a_next, ava->aa_desc ))
308 rc = LDAP_COMPARE_FALSE;
310 if ( value_find( ava->aa_desc, a->a_vals, ava->aa_value ) == 0 ) {
311 rc = LDAP_COMPARE_TRUE;