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 };
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, "{o" /*}*/, &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.bv_val );
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.bv_val ));
128 Debug( LDAP_DEBUG_ANY,
129 "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
131 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
132 "invalid DN", NULL, NULL );
136 rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
137 if( rc != LDAP_SUCCESS ) {
138 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
142 rc = value_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &nvalue, &text );
143 if( rc != LDAP_SUCCESS ) {
144 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
148 ava.aa_value = nvalue;
150 if( strcasecmp( ndn, LDAP_ROOT_DSE ) == 0 ) {
152 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
153 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
154 conn->c_connid, dn.bv_val, ava.aa_desc->ad_cname.bv_val,
155 ava.aa_value->bv_val ));
157 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
158 dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
161 Statslog( LDAP_DEBUG_STATS,
162 "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
163 op->o_connid, op->o_opid, dn.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 );
178 } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
180 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
181 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
182 conn->c_connid, dn.bv_val, ava.aa_desc->ad_cname.bv_val,
183 ava.aa_value->bv_val ));
185 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
186 dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
189 Statslog( LDAP_DEBUG_STATS,
190 "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
191 op->o_connid, op->o_opid, dn.bv_val,
192 ava.aa_desc->ad_cname.bv_val, 0 );
194 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
195 if( rc != LDAP_SUCCESS ) {
196 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
201 rc = schema_info( &entry, &text );
202 if( rc != LDAP_SUCCESS ) {
203 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
210 rc = compare_entry( conn, op, entry, &ava );
213 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
215 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
222 manageDSAit = get_manageDSAit( op );
225 * We could be serving multiple database backends. Select the
226 * appropriate one, or send a referral to our "referral server"
227 * if we don't hold it.
229 if ( (be = select_backend( ndn, manageDSAit, 0 )) == NULL ) {
230 struct berval **ref = referral_rewrite( default_referral,
231 NULL, dn.bv_val, LDAP_SCOPE_DEFAULT );
233 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
234 NULL, NULL, ref ? ref : default_referral, NULL );
241 /* check restrictions */
242 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
243 if( rc != LDAP_SUCCESS ) {
244 send_ldap_result( conn, op, rc,
245 NULL, text, NULL, NULL );
249 /* check for referrals */
250 rc = backend_check_referrals( be, conn, op, dn.bv_val, ndn );
251 if ( rc != LDAP_SUCCESS ) {
256 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
257 "do_compare: conn %d dn (%s) attr(%s) value (%s)\n",
258 conn->c_connid, dn.bv_val, ava.aa_desc->ad_cname.bv_val,
259 ava.aa_value->bv_val ));
261 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
262 dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
265 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
266 op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname.bv_val, 0 );
269 /* deref suffix alias if appropriate */
270 ndn = suffix_alias( be, ndn );
272 if ( be->be_compare ) {
273 (*be->be_compare)( be, conn, op, dn.bv_val, ndn, &ava );
275 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
276 NULL, "operation not supported within namingContext", NULL, NULL );
283 free( value.bv_val );
288 static int compare_entry(
292 AttributeAssertion *ava )
294 int rc = LDAP_NO_SUCH_ATTRIBUTE;
297 if ( ! access_allowed( NULL, conn, op, e,
298 ava->aa_desc, ava->aa_value, ACL_COMPARE ) )
300 return LDAP_INSUFFICIENT_ACCESS;
303 for(a = attrs_find( e->e_attrs, ava->aa_desc );
305 a = attrs_find( a->a_next, ava->aa_desc ))
307 rc = LDAP_COMPARE_FALSE;
309 if ( value_find( ava->aa_desc, a->a_vals, ava->aa_value ) == 0 ) {
310 rc = LDAP_COMPARE_TRUE;