]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
fix negative counters; prepare for imrpved count of sent data
[openldap] / servers / slapd / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31
32 #include "ldap_pvt.h"
33 #include "slap.h"
34 #ifdef LDAP_SLAPI
35 #include "slapi/slapi.h"
36 #endif
37
38 static int compare_entry(
39         Operation *op,
40         Entry *e,
41         AttributeAssertion *ava );
42
43 int
44 do_compare(
45     Operation   *op,
46     SlapReply   *rs )
47 {
48         struct berval dn = BER_BVNULL;
49         struct berval desc = BER_BVNULL;
50         struct berval value = BER_BVNULL;
51         AttributeAssertion ava = { NULL, BER_BVNULL };
52
53         ava.aa_desc = NULL;
54
55         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
56         /*
57          * Parse the compare request.  It looks like this:
58          *
59          *      CompareRequest := [APPLICATION 14] SEQUENCE {
60          *              entry   DistinguishedName,
61          *              ava     SEQUENCE {
62          *                      type    AttributeType,
63          *                      value   AttributeValue
64          *              }
65          *      }
66          */
67
68         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
69                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
70                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
71                 return SLAPD_DISCONNECT;
72         }
73
74         if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
75                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
76                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
77                 return SLAPD_DISCONNECT;
78         }
79
80         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
81                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
82                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
83                 return SLAPD_DISCONNECT;
84         }
85
86         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
87                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
88                 goto cleanup;
89         } 
90
91         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
92                 op->o_tmpmemctx );
93         if( rs->sr_err != LDAP_SUCCESS ) {
94                 Debug( LDAP_DEBUG_ANY,
95                         "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
96                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
97                 goto cleanup;
98         }
99
100         rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
101         if( rs->sr_err != LDAP_SUCCESS ) {
102                 send_ldap_result( op, rs );
103                 goto cleanup;
104         }
105
106         rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
107                 ava.aa_desc->ad_type->sat_equality,
108                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
109                 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
110         if( rs->sr_err != LDAP_SUCCESS ) {
111                 send_ldap_result( op, rs );
112                 goto cleanup;
113         }
114
115         op->orc_ava = &ava;
116
117         op->o_bd = frontendDB;
118         rs->sr_err = frontendDB->be_compare( op, rs );
119
120 cleanup:;
121         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
122         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
123         if ( ava.aa_value.bv_val ) {
124                 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
125         }
126
127         return rs->sr_err;
128 }
129
130 int
131 fe_op_compare( Operation *op, SlapReply *rs )
132 {
133         Entry *entry = NULL;
134         int manageDSAit;
135         AttributeAssertion ava = *op->orc_ava;
136
137         if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
138                 Debug( LDAP_DEBUG_ARGS,
139                         "do_compare: dn (%s) attr (%s) value (%s)\n",
140                         op->o_req_dn.bv_val,
141                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
142
143                 Statslog( LDAP_DEBUG_STATS,
144                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
145                         op->o_connid, op->o_opid, op->o_req_dn.bv_val,
146                         ava.aa_desc->ad_cname.bv_val, 0 );
147
148                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
149                         send_ldap_result( op, rs );
150                         goto cleanup;
151                 }
152
153                 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
154                 if( rs->sr_err != LDAP_SUCCESS ) {
155                         send_ldap_result( op, rs );
156                         goto cleanup;
157                 }
158
159         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
160                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
161                         op->o_req_dn.bv_val,
162                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
163
164                 Statslog( LDAP_DEBUG_STATS,
165                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
166                         op->o_connid, op->o_opid, op->o_req_dn.bv_val,
167                         ava.aa_desc->ad_cname.bv_val, 0 );
168
169                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
170                         send_ldap_result( op, rs );
171                         rs->sr_err = 0;
172                         goto cleanup;
173                 }
174
175                 rs->sr_err = schema_info( &entry, &rs->sr_text );
176                 if( rs->sr_err != LDAP_SUCCESS ) {
177                         send_ldap_result( op, rs );
178                         rs->sr_err = 0;
179                         goto cleanup;
180                 }
181         }
182
183         if( entry ) {
184                 rs->sr_err = compare_entry( op, entry, &ava );
185                 entry_free( entry );
186
187                 send_ldap_result( op, rs );
188
189                 if( rs->sr_err == LDAP_COMPARE_TRUE ||
190                         rs->sr_err == LDAP_COMPARE_FALSE )
191                 {
192                         rs->sr_err = LDAP_SUCCESS;
193                 }
194
195                 goto cleanup;
196         }
197
198         manageDSAit = get_manageDSAit( op );
199
200         /*
201          * We could be serving multiple database backends.  Select the
202          * appropriate one, or send a referral to our "referral server"
203          * if we don't hold it.
204          */
205         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
206         if ( op->o_bd == NULL ) {
207                 rs->sr_ref = referral_rewrite( default_referral,
208                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
209
210                 rs->sr_err = LDAP_REFERRAL;
211                 if (!rs->sr_ref) rs->sr_ref = default_referral;
212                 send_ldap_result( op, rs );
213
214                 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
215                 rs->sr_err = 0;
216                 goto cleanup;
217         }
218
219         /* check restrictions */
220         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
221                 send_ldap_result( op, rs );
222                 goto cleanup;
223         }
224
225         /* check for referrals */
226         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
227                 goto cleanup;
228         }
229
230         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
231             op->o_req_dn.bv_val,
232                 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
233
234         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
235             op->o_connid, op->o_opid, op->o_req_dn.bv_val,
236                 ava.aa_desc->ad_cname.bv_val, 0 );
237
238 #if defined( LDAP_SLAPI )
239 #define pb      op->o_pb
240         if ( pb ) {
241                 slapi_int_pblock_set_operation( pb, op );
242                 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
243                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
244                 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
245                 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
246
247                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
248                         SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
249                 if ( rs->sr_err < 0 ) {
250                         /*
251                          * A preoperation plugin failure will abort the
252                          * entire operation.
253                          */
254                         Debug(LDAP_DEBUG_TRACE,
255                                 "do_compare: compare preoperation plugin failed\n",
256                                 0, 0, 0);
257                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
258                                 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
259                         {
260                                 rs->sr_err = LDAP_OTHER;
261                         }
262                         goto cleanup;
263                 }
264         }
265 #endif /* defined( LDAP_SLAPI ) */
266
267         op->orc_ava = &ava;
268         if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
269                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
270                         "entryDN compare not supported" );
271
272         } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
273                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
274                         "subschemaSubentry compare not supported" );
275
276         } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
277                 && op->o_bd->be_has_subordinates )
278         {
279                 int     rc, hasSubordinates = LDAP_SUCCESS;
280
281                 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
282                 if ( rc == 0 && entry ) {
283                         rc = op->o_bd->be_has_subordinates( op, entry,
284                                 &hasSubordinates );
285                         be_entry_release_r( op, entry );
286                 }
287
288                 if ( rc == 0 ) {
289                         int     asserted;
290
291                         asserted = bvmatch( &ava.aa_value, &slap_true_bv )
292                                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
293                         if ( hasSubordinates == asserted ) {
294                                 rs->sr_err = LDAP_COMPARE_TRUE;
295                         } else {
296                                 rs->sr_err = LDAP_COMPARE_FALSE;
297                         }
298                 }
299                 send_ldap_result( op, rs );
300
301                 if( rc == 0 ) rs->sr_err = LDAP_SUCCESS;
302
303         } else if ( op->o_bd->be_compare ) {
304                 op->o_bd->be_compare( op, rs );
305
306         } else {
307                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
308                         "operation not supported within namingContext" );
309         }
310
311 #if defined( LDAP_SLAPI )
312         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
313                 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
314         {
315                 Debug(LDAP_DEBUG_TRACE,
316                         "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
317         }
318 #endif /* defined( LDAP_SLAPI ) */
319
320 cleanup:;
321         return rs->sr_err;
322 }
323
324 static int compare_entry(
325         Operation *op,
326         Entry *e,
327         AttributeAssertion *ava )
328 {
329         int rc = LDAP_NO_SUCH_ATTRIBUTE;
330         Attribute *a;
331
332         if ( ! access_allowed( op, e,
333                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
334         {       
335                 return LDAP_INSUFFICIENT_ACCESS;
336         }
337
338         for(a = attrs_find( e->e_attrs, ava->aa_desc );
339                 a != NULL;
340                 a = attrs_find( a->a_next, ava->aa_desc ))
341         {
342                 rc = LDAP_COMPARE_FALSE;
343
344                 if ( value_find_ex( ava->aa_desc,
345                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
346                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
347                         a->a_nvals,
348                         &ava->aa_value, op->o_tmpmemctx ) == 0 )
349                 {
350                         rc = LDAP_COMPARE_TRUE;
351                         break;
352                 }
353         }
354
355         return rc;
356 }