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 );
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 AttributeAssertion ava = { 0 };
46 int rc = LDAP_SUCCESS;
47 const char *text = NULL;
53 LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", conn->c_connid, 0, 0 );
55 Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
58 * Parse the compare request. It looks like this:
60 * CompareRequest := [APPLICATION 14] SEQUENCE {
61 * entry DistinguishedName,
64 * value AttributeValue
69 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
71 LDAP_LOG( OPERATION, ERR,
72 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
74 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
76 send_ldap_disconnect( conn, op,
77 LDAP_PROTOCOL_ERROR, "decoding error" );
78 return SLAPD_DISCONNECT;
81 if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
83 LDAP_LOG( OPERATION, ERR,
84 "do_compare: conn %d get ava failed\n", conn->c_connid, 0, 0 );
86 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
88 send_ldap_disconnect( conn, op,
89 LDAP_PROTOCOL_ERROR, "decoding error" );
90 rc = SLAPD_DISCONNECT;
94 if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
96 LDAP_LOG( OPERATION, ERR,
97 "do_compare: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
99 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
101 send_ldap_disconnect( conn, op,
102 LDAP_PROTOCOL_ERROR, "decoding error" );
103 rc = SLAPD_DISCONNECT;
107 if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
109 LDAP_LOG( OPERATION, INFO,
110 "do_compare: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
112 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
117 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
118 if( rc != LDAP_SUCCESS ) {
120 LDAP_LOG( OPERATION, INFO,
121 "do_compare: conn %d invalid dn (%s)\n",
122 conn->c_connid, dn.bv_val, 0 );
124 Debug( LDAP_DEBUG_ANY,
125 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
127 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
128 "invalid DN", NULL, NULL );
132 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
133 if( rc != LDAP_SUCCESS ) {
134 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
138 rc = value_validate_normalize( ava.aa_desc, SLAP_MR_EQUALITY,
139 &value, &ava.aa_value, &text );
140 if( rc != LDAP_SUCCESS ) {
141 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
145 if( strcasecmp( ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
147 LDAP_LOG( OPERATION, ARGS,
148 "do_compare: dn (%s) attr(%s) value (%s)\n",
149 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
151 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
152 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
155 Statslog( LDAP_DEBUG_STATS,
156 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
157 op->o_connid, op->o_opid, pdn.bv_val,
158 ava.aa_desc->ad_cname.bv_val, 0 );
160 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
161 if( rc != LDAP_SUCCESS ) {
162 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
166 rc = root_dse_info( conn, &entry, &text );
167 if( rc != LDAP_SUCCESS ) {
168 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
172 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
174 LDAP_LOG( OPERATION, ARGS,
175 "do_compare: dn (%s) attr(%s) value (%s)\n",
176 pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
177 ava.aa_value.bv_val );
179 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
180 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
183 Statslog( LDAP_DEBUG_STATS,
184 "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
185 op->o_connid, op->o_opid, pdn.bv_val,
186 ava.aa_desc->ad_cname.bv_val, 0 );
188 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
189 if( rc != LDAP_SUCCESS ) {
190 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
195 rc = schema_info( &entry, &text );
196 if( rc != LDAP_SUCCESS ) {
197 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
204 rc = compare_entry( conn, op, entry, &ava );
207 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
209 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
216 manageDSAit = get_manageDSAit( op );
219 * We could be serving multiple database backends. Select the
220 * appropriate one, or send a referral to our "referral server"
221 * if we don't hold it.
223 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
224 BerVarray ref = referral_rewrite( default_referral,
225 NULL, &pdn, LDAP_SCOPE_DEFAULT );
227 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
228 NULL, NULL, ref ? ref : default_referral, NULL );
230 ber_bvarray_free( ref );
235 /* check restrictions */
236 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
237 if( rc != LDAP_SUCCESS ) {
238 send_ldap_result( conn, op, rc,
239 NULL, text, NULL, NULL );
243 /* check for referrals */
244 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
245 if ( rc != LDAP_SUCCESS ) {
250 LDAP_LOG( OPERATION, ARGS,
251 "do_compare: dn (%s) attr(%s) value (%s)\n",
252 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
254 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
255 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
258 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
259 op->o_connid, op->o_opid, pdn.bv_val,
260 ava.aa_desc->ad_cname.bv_val, 0 );
263 /* deref suffix alias if appropriate */
264 suffix_alias( be, &ndn );
266 if ( be->be_compare ) {
267 (*be->be_compare)( be, conn, op, &pdn, &ndn, &ava );
269 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
270 NULL, "operation not supported within namingContext",
277 if ( ava.aa_value.bv_val ) free( ava.aa_value.bv_val );
282 static int compare_entry(
286 AttributeAssertion *ava )
288 int rc = LDAP_NO_SUCH_ATTRIBUTE;
291 if ( ! access_allowed( NULL, conn, op, e,
292 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
294 return LDAP_INSUFFICIENT_ACCESS;
297 for(a = attrs_find( e->e_attrs, ava->aa_desc );
299 a = attrs_find( a->a_next, ava->aa_desc ))
301 rc = LDAP_COMPARE_FALSE;
303 if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 ) {
304 rc = LDAP_COMPARE_TRUE;