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