]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
ITS#3549 acknowledge CANCEL requests
[openldap] / servers / slapd / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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 "slap.h"
33 #ifdef LDAP_SLAPI
34 #include "slapi/slapi.h"
35 #endif
36
37 static int compare_entry(
38         Operation *op,
39         Entry *e,
40         AttributeAssertion *ava );
41
42 int
43 do_compare(
44     Operation   *op,
45     SlapReply   *rs )
46 {
47         struct berval dn = BER_BVNULL;
48         struct berval desc = BER_BVNULL;
49         struct berval value = BER_BVNULL;
50         AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
51
52         ava.aa_desc = NULL;
53
54         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
55         /*
56          * Parse the compare request.  It looks like this:
57          *
58          *      CompareRequest := [APPLICATION 14] SEQUENCE {
59          *              entry   DistinguishedName,
60          *              ava     SEQUENCE {
61          *                      type    AttributeType,
62          *                      value   AttributeValue
63          *              }
64          *      }
65          */
66
67         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
68                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
69                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
70                 return SLAPD_DISCONNECT;
71         }
72
73         if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
74                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
75                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
76                 return SLAPD_DISCONNECT;
77         }
78
79         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
80                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
81                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
82                 return SLAPD_DISCONNECT;
83         }
84
85         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
86                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
87                 goto cleanup;
88         } 
89
90         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
91                 op->o_tmpmemctx );
92         if( rs->sr_err != LDAP_SUCCESS ) {
93                 Debug( LDAP_DEBUG_ANY,
94                         "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
95                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
96                 goto cleanup;
97         }
98
99         rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
100         if( rs->sr_err != LDAP_SUCCESS ) {
101                 send_ldap_result( op, rs );
102                 goto cleanup;
103         }
104
105         rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
106                 ava.aa_desc->ad_type->sat_equality,
107                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
108                 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
109         if( rs->sr_err != LDAP_SUCCESS ) {
110                 send_ldap_result( op, rs );
111                 goto cleanup;
112         }
113
114         op->orc_ava = &ava;
115
116         op->o_bd = frontendDB;
117         rs->sr_err = frontendDB->be_compare( op, rs );
118
119 cleanup:;
120         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
121         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
122         if ( ava.aa_value.bv_val ) {
123                 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
124         }
125
126         return rs->sr_err;
127 }
128
129 int
130 fe_op_compare( Operation *op, SlapReply *rs )
131 {
132         Entry *entry = NULL;
133         int manageDSAit;
134         AttributeAssertion ava = *op->orc_ava;
135
136         if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
137                 Debug( LDAP_DEBUG_ARGS,
138                         "do_compare: dn (%s) attr (%s) value (%s)\n",
139                         op->o_req_dn.bv_val,
140                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
141
142                 Statslog( LDAP_DEBUG_STATS,
143                         "%s CMP dn=\"%s\" attr=\"%s\"\n",
144                         op->o_log_prefix, op->o_req_dn.bv_val,
145                         ava.aa_desc->ad_cname.bv_val, 0, 0 );
146
147                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
148                         send_ldap_result( op, rs );
149                         goto cleanup;
150                 }
151
152                 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
153                 if( rs->sr_err != LDAP_SUCCESS ) {
154                         send_ldap_result( op, rs );
155                         goto cleanup;
156                 }
157
158         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
159                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
160                         op->o_req_dn.bv_val,
161                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
162
163                 Statslog( LDAP_DEBUG_STATS,
164                         "%s CMP dn=\"%s\" attr=\"%s\"\n",
165                         op->o_log_prefix, op->o_req_dn.bv_val,
166                         ava.aa_desc->ad_cname.bv_val, 0, 0 );
167
168                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
169                         send_ldap_result( op, rs );
170                         rs->sr_err = 0;
171                         goto cleanup;
172                 }
173
174                 rs->sr_err = schema_info( &entry, &rs->sr_text );
175                 if( rs->sr_err != LDAP_SUCCESS ) {
176                         send_ldap_result( op, rs );
177                         rs->sr_err = 0;
178                         goto cleanup;
179                 }
180         }
181
182         if( entry ) {
183                 rs->sr_err = compare_entry( op, entry, &ava );
184                 entry_free( entry );
185
186                 send_ldap_result( op, rs );
187
188                 if( rs->sr_err == LDAP_COMPARE_TRUE ||
189                         rs->sr_err == LDAP_COMPARE_FALSE )
190                 {
191                         rs->sr_err = LDAP_SUCCESS;
192                 }
193
194                 goto cleanup;
195         }
196
197         manageDSAit = get_manageDSAit( op );
198
199         /*
200          * We could be serving multiple database backends.  Select the
201          * appropriate one, or send a referral to our "referral server"
202          * if we don't hold it.
203          */
204         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
205         if ( op->o_bd == NULL ) {
206                 rs->sr_ref = referral_rewrite( default_referral,
207                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
208
209                 rs->sr_err = LDAP_REFERRAL;
210                 if (!rs->sr_ref) rs->sr_ref = default_referral;
211                 op->o_bd = frontendDB;
212                 send_ldap_result( op, rs );
213                 op->o_bd = NULL;
214
215                 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
216                 rs->sr_err = 0;
217                 goto cleanup;
218         }
219
220         /* check restrictions */
221         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
222                 send_ldap_result( op, rs );
223                 goto cleanup;
224         }
225
226         /* check for referrals */
227         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
228                 goto cleanup;
229         }
230
231         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
232             op->o_req_dn.bv_val,
233                 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
234
235         Statslog( LDAP_DEBUG_STATS, "%s CMP dn=\"%s\" attr=\"%s\"\n",
236                 op->o_log_prefix, op->o_req_dn.bv_val,
237                 ava.aa_desc->ad_cname.bv_val, 0, 0 );
238
239 #if defined( LDAP_SLAPI )
240 #define pb      op->o_pb
241         if ( pb ) {
242                 slapi_int_pblock_set_operation( pb, op );
243                 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
244                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
245                 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
246                 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
247
248                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
249                         SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
250                 if ( rs->sr_err < 0 ) {
251                         /*
252                          * A preoperation plugin failure will abort the
253                          * entire operation.
254                          */
255                         Debug(LDAP_DEBUG_TRACE,
256                                 "do_compare: compare preoperation plugin failed\n",
257                                 0, 0, 0);
258                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
259                                 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
260                         {
261                                 rs->sr_err = LDAP_OTHER;
262                         }
263                         goto cleanup;
264                 }
265         }
266 #endif /* defined( LDAP_SLAPI ) */
267
268         op->orc_ava = &ava;
269         if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
270                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
271                         "entryDN compare not supported" );
272
273         } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
274                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
275                         "subschemaSubentry compare not supported" );
276
277 #ifndef SLAP_COMPARE_IN_FRONTEND
278         } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
279                 && op->o_bd->be_has_subordinates )
280         {
281                 int     rc, hasSubordinates = LDAP_SUCCESS;
282
283                 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
284                 if ( rc == 0 && entry ) {
285                         if ( ! access_allowed( op, entry,
286                                 ava.aa_desc, &ava.aa_value, ACL_COMPARE, NULL ) )
287                         {       
288                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
289                                 
290                         } else {
291                                 rc = rs->sr_err = op->o_bd->be_has_subordinates( op,
292                                                 entry, &hasSubordinates );
293                                 be_entry_release_r( op, entry );
294                         }
295                 }
296
297                 if ( rc == 0 ) {
298                         int     asserted;
299
300                         asserted = bvmatch( &ava.aa_value, &slap_true_bv )
301                                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
302                         if ( hasSubordinates == asserted ) {
303                                 rs->sr_err = LDAP_COMPARE_TRUE;
304
305                         } else {
306                                 rs->sr_err = LDAP_COMPARE_FALSE;
307                         }
308
309                 } else {
310 #ifdef SLAP_ACL_HONOR_DISCLOSE
311                         /* return error only if "disclose"
312                          * is granted on the object */
313                         if ( backend_access( op, NULL, &op->o_req_ndn,
314                                         slap_schema.si_ad_entry,
315                                         NULL, ACL_DISCLOSE, NULL ) == LDAP_INSUFFICIENT_ACCESS )
316                         {
317                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
318                         }
319 #endif /* SLAP_ACL_HONOR_DISCLOSE */
320                 }
321
322                 send_ldap_result( op, rs );
323
324                 if ( rc == 0 ) {
325                         rs->sr_err = LDAP_SUCCESS;
326                 }
327
328         } else if ( op->o_bd->be_compare ) {
329                 rs->sr_err = op->o_bd->be_compare( op, rs );
330
331 #endif /* ! SLAP_COMPARE_IN_FRONTEND */
332         } else {
333                 rs->sr_err = SLAP_CB_CONTINUE;
334         }
335
336         if ( rs->sr_err == SLAP_CB_CONTINUE ) {
337                 /* do our best to compare that AVA
338                  * 
339                  * NOTE: this code is used only
340                  * if SLAP_COMPARE_IN_FRONTEND 
341                  * is #define'd (it's not by default)
342                  * or if op->o_bd->be_compare is NULL.
343                  * 
344                  * FIXME: one potential issue is that
345                  * if SLAP_COMPARE_IN_FRONTEND overlays
346                  * are not executed for compare. */
347                 BerVarray       vals = NULL;
348                 int             rc = LDAP_OTHER;
349
350                 rs->sr_err = backend_attribute( op, NULL, &op->o_req_ndn,
351                                 ava.aa_desc, &vals, ACL_COMPARE );
352                 switch ( rs->sr_err ) {
353                 default:
354 #ifdef SLAP_ACL_HONOR_DISCLOSE
355                         /* return error only if "disclose"
356                          * is granted on the object */
357                         if ( backend_access( op, NULL, &op->o_req_ndn,
358                                         slap_schema.si_ad_entry,
359                                         NULL, ACL_DISCLOSE, NULL )
360                                         == LDAP_INSUFFICIENT_ACCESS )
361                         {
362                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
363                         }
364 #endif /* SLAP_ACL_HONOR_DISCLOSE */
365                         break;
366
367                 case LDAP_SUCCESS:
368                         if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
369                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
370                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
371                                 vals, &ava.aa_value, op->o_tmpmemctx ) == 0 )
372                         {
373                                 rs->sr_err = LDAP_COMPARE_TRUE;
374                                 break;
375
376                         } else {
377                                 rs->sr_err = LDAP_COMPARE_FALSE;
378                         }
379                         rc = LDAP_SUCCESS;
380                         break;
381                 }
382
383                 send_ldap_result( op, rs );
384
385                 if ( rc == 0 ) {
386                         rs->sr_err = LDAP_SUCCESS;
387                 }
388                 
389                 if ( vals ) {
390                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
391                 }
392         }
393
394 #if defined( LDAP_SLAPI )
395         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
396                 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
397         {
398                 Debug(LDAP_DEBUG_TRACE,
399                         "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
400         }
401 #endif /* defined( LDAP_SLAPI ) */
402
403 cleanup:;
404         return rs->sr_err;
405 }
406
407 static int compare_entry(
408         Operation *op,
409         Entry *e,
410         AttributeAssertion *ava )
411 {
412         int rc;
413         Attribute *a;
414
415         if ( ! access_allowed( op, e,
416                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
417         {       
418                 return LDAP_INSUFFICIENT_ACCESS;
419         }
420
421         a = attrs_find( e->e_attrs, ava->aa_desc );
422         if( a == NULL ) return LDAP_NO_SUCH_ATTRIBUTE;
423
424         rc = LDAP_COMPARE_FALSE;
425         for(a = attrs_find( e->e_attrs, ava->aa_desc );
426                 a != NULL;
427                 a = attrs_find( a->a_next, ava->aa_desc ))
428         {
429                 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
430                         e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
431                 {       
432                         rc = LDAP_INSUFFICIENT_ACCESS;
433                         break;
434                 }
435
436                 if ( value_find_ex( ava->aa_desc,
437                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
438                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
439                         a->a_nvals,
440                         &ava->aa_value, op->o_tmpmemctx ) == 0 )
441                 {
442                         rc = LDAP_COMPARE_TRUE;
443                         break;
444                 }
445         }
446
447         return rc;
448 }