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 );
40 char *dn = NULL, *ndn=NULL;
43 struct berval *nvalue;
44 AttributeAssertion ava;
46 int rc = LDAP_SUCCESS;
47 const char *text = NULL;
55 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
56 "do_compare: conn %d\n", conn->c_connid ));
58 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
61 * Parse the compare request. It looks like this:
63 * CompareRequest := [APPLICATION 14] SEQUENCE {
64 * entry DistinguishedName,
67 * value AttributeValue
72 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
74 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
75 "do_compare: conn %d ber_scanf failed\n", conn->c_connid ));
77 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
79 send_ldap_disconnect( conn, op,
80 LDAP_PROTOCOL_ERROR, "decoding error" );
81 return SLAPD_DISCONNECT;
84 if ( ber_scanf( op->o_ber, "{oo}", &desc, &value ) == LBER_ERROR ) {
86 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
87 "do_compare: conn %d get ava failed\n", conn->c_connid ));
89 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
91 send_ldap_disconnect( conn, op,
92 LDAP_PROTOCOL_ERROR, "decoding error" );
93 rc = SLAPD_DISCONNECT;
97 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
99 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
100 "do_compare: conn %d ber_scanf failed\n", conn->c_connid ));
102 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
104 send_ldap_disconnect( conn, op,
105 LDAP_PROTOCOL_ERROR, "decoding error" );
106 rc = SLAPD_DISCONNECT;
110 if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
112 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
113 "do_compare: conn %d get_ctrls failed\n", conn->c_connid ));
115 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
120 ndn = ch_strdup( dn );
122 if( dn_normalize( ndn ) == NULL ) {
124 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
125 "do_compare: conn %d invalid dn (%s)\n",
126 conn->c_connid, dn ));
128 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", dn, 0, 0 );
130 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
131 "invalid DN", NULL, NULL );
135 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
136 if( rc != LDAP_SUCCESS ) {
137 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
141 rc = value_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &nvalue, &text );
142 if( rc != LDAP_SUCCESS ) {
143 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
147 ava.aa_value = nvalue;
149 if( strcasecmp( ndn, LDAP_ROOT_DSE ) == 0 ) {
151 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
152 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
153 conn->c_connid, dn, ava.aa_desc->ad_cname.bv_val,
154 ava.aa_value->bv_val ));
156 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
157 dn, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
160 Statslog( LDAP_DEBUG_STATS,
161 "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
162 op->o_connid, op->o_opid, dn, 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, 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, dn, 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 dn, 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, dn, 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 );
207 rc = compare_entry( conn, op, entry, &ava );
210 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
212 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
219 manageDSAit = get_manageDSAit( op );
222 * We could be serving multiple database backends. Select the
223 * appropriate one, or send a referral to our "referral server"
224 * if we don't hold it.
226 if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
227 struct berval **ref = referral_rewrite( default_referral,
228 NULL, dn, LDAP_SCOPE_DEFAULT );
230 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
231 NULL, NULL, ref ? ref : default_referral, NULL );
238 /* check restrictions */
239 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
240 if( rc != LDAP_SUCCESS ) {
241 send_ldap_result( conn, op, rc,
242 NULL, text, NULL, NULL );
246 /* check for referrals */
247 rc = backend_check_referrals( be, conn, op, dn, ndn );
248 if ( rc != LDAP_SUCCESS ) {
253 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
254 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
255 conn->c_connid, dn, ava.aa_desc->ad_cname.bv_val,
256 ava.aa_value->bv_val ));
258 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
259 dn, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
262 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
263 op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname.bv_val, 0 );
266 /* deref suffix alias if appropriate */
267 ndn = suffix_alias( be, ndn );
269 if ( be->be_compare ) {
270 (*be->be_compare)( be, conn, op, dn, ndn, &ava );
272 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
273 NULL, "operation not supported within namingContext", NULL, NULL );
280 free( value.bv_val );
285 static int compare_entry(
289 AttributeAssertion *ava )
291 int rc = LDAP_NO_SUCH_ATTRIBUTE;
294 if ( ! access_allowed( NULL, conn, op, e,
295 ava->aa_desc, ava->aa_value, ACL_COMPARE ) )
297 return LDAP_INSUFFICIENT_ACCESS;
300 for(a = attrs_find( e->e_attrs, ava->aa_desc );
302 a = attrs_find( a->a_next, ava->aa_desc ))
304 rc = LDAP_COMPARE_FALSE;
306 if ( value_find_ex( ava->aa_desc,
307 SLAP_MR_VALUE_IS_IN_MR_SYNTAX,
308 a->a_vals, ava->aa_value ) == 0 )
310 rc = LDAP_COMPARE_TRUE;