3 * Copyright 1998-2002 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 );
40 struct berval dn = { 0, NULL };
41 struct berval pdn = { 0, NULL };
42 struct berval ndn = { 0, NULL };
43 struct berval desc = { 0, NULL };
44 struct berval value = { 0, NULL };
45 AttributeAssertion ava = { 0 };
47 int rc = LDAP_SUCCESS;
48 const char *text = NULL;
54 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", conn->c_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", conn->c_connid, 0, 0 );
75 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
77 send_ldap_disconnect( conn, op,
78 LDAP_PROTOCOL_ERROR, "decoding error" );
79 return SLAPD_DISCONNECT;
82 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
84 LDAP_LOG( OPERATION, ERR,
85 "do_compare: conn %d get ava failed\n", conn->c_connid, 0, 0 );
87 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
89 send_ldap_disconnect( conn, op,
90 LDAP_PROTOCOL_ERROR, "decoding error" );
91 rc = SLAPD_DISCONNECT;
95 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
97 LDAP_LOG( OPERATION, ERR,
98 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
100 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
102 send_ldap_disconnect( conn, op,
103 LDAP_PROTOCOL_ERROR, "decoding error" );
104 rc = SLAPD_DISCONNECT;
108 if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
110 LDAP_LOG( OPERATION, INFO,
111 "do_compare: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
113 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
118 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
119 if( rc != LDAP_SUCCESS ) {
121 LDAP_LOG( OPERATION, INFO,
122 "do_compare: conn %d invalid dn (%s)\n",
123 conn->c_connid, dn.bv_val, 0 );
125 Debug( LDAP_DEBUG_ANY,
126 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
128 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
129 "invalid DN", NULL, NULL );
133 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
134 if( rc != LDAP_SUCCESS ) {
135 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
139 rc = value_validate_normalize( ava.aa_desc, SLAP_MR_EQUALITY,
140 &value, &ava.aa_value, &text );
141 if( rc != LDAP_SUCCESS ) {
142 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
146 if( strcasecmp( ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
148 LDAP_LOG( OPERATION, ARGS,
149 "do_compare: dn (%s) attr(%s) value (%s)\n",
150 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
152 Debug( LDAP_DEBUG_ARGS, "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 );
156 Statslog( LDAP_DEBUG_STATS,
157 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
158 op->o_connid, op->o_opid, pdn.bv_val,
159 ava.aa_desc->ad_cname.bv_val, 0 );
161 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
162 if( rc != LDAP_SUCCESS ) {
163 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
167 rc = root_dse_info( conn, &entry, &text );
168 if( rc != LDAP_SUCCESS ) {
169 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
175 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
177 LDAP_LOG( OPERATION, ARGS,
178 "do_compare: dn (%s) attr(%s) value (%s)\n",
179 pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
180 ava.aa_value.bv_val );
182 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
183 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
186 Statslog( LDAP_DEBUG_STATS,
187 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
188 op->o_connid, op->o_opid, pdn.bv_val,
189 ava.aa_desc->ad_cname.bv_val, 0 );
191 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
192 if( rc != LDAP_SUCCESS ) {
193 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
198 rc = schema_info( &entry, &text );
199 if( rc != LDAP_SUCCESS ) {
200 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
208 rc = compare_entry( conn, op, entry, &ava );
209 if( fentry) entry_free( fentry );
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 BerVarray 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 );
234 ber_bvarray_free( ref );
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, ARGS,
255 "do_compare: dn (%s) attr(%s) value (%s)\n",
256 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
258 Debug( LDAP_DEBUG_ARGS, "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 );
262 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
263 op->o_connid, op->o_opid, pdn.bv_val,
264 ava.aa_desc->ad_cname.bv_val, 0 );
267 /* deref suffix alias if appropriate */
268 suffix_alias( be, &ndn );
270 if ( be->be_compare ) {
271 (*be->be_compare)( be, conn, op, &pdn, &ndn, &ava );
273 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
274 NULL, "operation not supported within namingContext",
281 if ( ava.aa_value.bv_val ) free( ava.aa_value.bv_val );
286 static int compare_entry(
290 AttributeAssertion *ava )
292 int rc = LDAP_NO_SUCH_ATTRIBUTE;
295 if ( ! access_allowed( NULL, conn, op, e,
296 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
298 return LDAP_INSUFFICIENT_ACCESS;
301 for(a = attrs_find( e->e_attrs, ava->aa_desc );
303 a = attrs_find( a->a_next, ava->aa_desc ))
305 rc = LDAP_COMPARE_FALSE;
307 if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 ) {
308 rc = LDAP_COMPARE_TRUE;