]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
3683087a298dc7c5ac4cb7ab5e54c5f576d9a048
[openldap] / servers / slapd / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 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
34 static int compare_entry(
35         Operation *op,
36         Entry *e,
37         AttributeAssertion *ava );
38
39 int
40 do_compare(
41     Operation   *op,
42     SlapReply   *rs )
43 {
44         struct berval dn = BER_BVNULL;
45         struct berval desc = BER_BVNULL;
46         struct berval value = BER_BVNULL;
47 #ifdef LDAP_COMP_MATCH
48         AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
49 #else
50         AttributeAssertion ava = { NULL, BER_BVNULL };
51 #endif
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                 rs->sr_err = slap_bv2undef_ad( &desc, &ava.aa_desc,
103                                 &rs->sr_text,
104                                 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
105                 if( rs->sr_err != LDAP_SUCCESS ) {
106                         send_ldap_result( op, rs );
107                         goto cleanup;
108                 }
109         }
110
111         rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
112                 ava.aa_desc->ad_type->sat_equality,
113                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
114                 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
115         if( rs->sr_err != LDAP_SUCCESS ) {
116                 send_ldap_result( op, rs );
117                 goto cleanup;
118         }
119
120         op->orc_ava = &ava;
121
122         op->o_bd = frontendDB;
123         rs->sr_err = frontendDB->be_compare( op, rs );
124
125 cleanup:;
126         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
127         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
128         if ( !BER_BVISNULL( &ava.aa_value ) ) {
129                 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
130         }
131
132         return rs->sr_err;
133 }
134
135 int
136 fe_op_compare( Operation *op, SlapReply *rs )
137 {
138         Entry                   *entry = NULL;
139         AttributeAssertion      ava = *op->orc_ava;
140         BackendDB               *bd = op->o_bd;
141
142         if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
143                 Debug( LDAP_DEBUG_ARGS,
144                         "do_compare: dn (%s) attr (%s) value (%s)\n",
145                         op->o_req_dn.bv_val,
146                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
147
148                 Statslog( LDAP_DEBUG_STATS,
149                         "%s CMP dn=\"%s\" attr=\"%s\"\n",
150                         op->o_log_prefix, op->o_req_dn.bv_val,
151                         ava.aa_desc->ad_cname.bv_val, 0, 0 );
152
153                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
154                         send_ldap_result( op, rs );
155                         goto cleanup;
156                 }
157
158                 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
159                 if( rs->sr_err != LDAP_SUCCESS ) {
160                         send_ldap_result( op, rs );
161                         goto cleanup;
162                 }
163
164         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
165                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
166                         op->o_req_dn.bv_val,
167                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
168
169                 Statslog( LDAP_DEBUG_STATS,
170                         "%s CMP dn=\"%s\" attr=\"%s\"\n",
171                         op->o_log_prefix, op->o_req_dn.bv_val,
172                         ava.aa_desc->ad_cname.bv_val, 0, 0 );
173
174                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
175                         send_ldap_result( op, rs );
176                         rs->sr_err = 0;
177                         goto cleanup;
178                 }
179
180                 rs->sr_err = schema_info( &entry, &rs->sr_text );
181                 if( rs->sr_err != LDAP_SUCCESS ) {
182                         send_ldap_result( op, rs );
183                         rs->sr_err = 0;
184                         goto cleanup;
185                 }
186         }
187
188         if( entry ) {
189                 rs->sr_err = compare_entry( op, entry, &ava );
190                 entry_free( entry );
191
192                 send_ldap_result( op, rs );
193
194                 if( rs->sr_err == LDAP_COMPARE_TRUE ||
195                         rs->sr_err == LDAP_COMPARE_FALSE )
196                 {
197                         rs->sr_err = LDAP_SUCCESS;
198                 }
199
200                 goto cleanup;
201         }
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, 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 = bd;
216                 send_ldap_result( op, rs );
217
218                 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
219                 rs->sr_err = 0;
220                 goto cleanup;
221         }
222
223         /* check restrictions */
224         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
225                 send_ldap_result( op, rs );
226                 goto cleanup;
227         }
228
229         /* check for referrals */
230         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
231                 goto cleanup;
232         }
233
234         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
235             op->o_req_dn.bv_val,
236                 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
237
238         Statslog( LDAP_DEBUG_STATS, "%s CMP dn=\"%s\" attr=\"%s\"\n",
239                 op->o_log_prefix, op->o_req_dn.bv_val,
240                 ava.aa_desc->ad_cname.bv_val, 0, 0 );
241
242         op->orc_ava = &ava;
243
244         if ( SLAP_SHADOW(op->o_bd) && get_dontUseCopy(op) ) {
245                 /* don't use shadow copy */
246                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
247                         "copy not used" );
248
249         } else if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
250                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
251                         "entryDN compare not supported" );
252
253         } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
254                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
255                         "subschemaSubentry compare not supported" );
256
257 #ifndef SLAP_COMPARE_IN_FRONTEND
258         } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
259                 && op->o_bd->be_has_subordinates )
260         {
261                 int     rc, hasSubordinates = LDAP_SUCCESS;
262
263                 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
264                 if ( rc == 0 && entry ) {
265                         if ( ! access_allowed( op, entry,
266                                 ava.aa_desc, &ava.aa_value, ACL_COMPARE, NULL ) )
267                         {       
268                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
269                                 
270                         } else {
271                                 rc = rs->sr_err = op->o_bd->be_has_subordinates( op,
272                                                 entry, &hasSubordinates );
273                                 be_entry_release_r( op, entry );
274                         }
275                 }
276
277                 if ( rc == 0 ) {
278                         int     asserted;
279
280                         asserted = bvmatch( &ava.aa_value, &slap_true_bv )
281                                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
282                         if ( hasSubordinates == asserted ) {
283                                 rs->sr_err = LDAP_COMPARE_TRUE;
284
285                         } else {
286                                 rs->sr_err = LDAP_COMPARE_FALSE;
287                         }
288
289                 } else {
290                         /* return error only if "disclose"
291                          * is granted on the object */
292                         if ( backend_access( op, NULL, &op->o_req_ndn,
293                                         slap_schema.si_ad_entry,
294                                         NULL, ACL_DISCLOSE, NULL ) == LDAP_INSUFFICIENT_ACCESS )
295                         {
296                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
297                         }
298                 }
299
300                 send_ldap_result( op, rs );
301
302                 if ( rc == 0 ) {
303                         rs->sr_err = LDAP_SUCCESS;
304                 }
305
306         } else if ( op->o_bd->be_compare ) {
307                 rs->sr_err = op->o_bd->be_compare( op, rs );
308
309 #endif /* ! SLAP_COMPARE_IN_FRONTEND */
310         } else {
311                 rs->sr_err = SLAP_CB_CONTINUE;
312         }
313
314         if ( rs->sr_err == SLAP_CB_CONTINUE ) {
315                 /* do our best to compare that AVA
316                  * 
317                  * NOTE: this code is used only
318                  * if SLAP_COMPARE_IN_FRONTEND 
319                  * is #define'd (it's not by default)
320                  * or if op->o_bd->be_compare is NULL.
321                  * 
322                  * FIXME: one potential issue is that
323                  * if SLAP_COMPARE_IN_FRONTEND overlays
324                  * are not executed for compare. */
325                 BerVarray       vals = NULL;
326                 int             rc = LDAP_OTHER;
327
328                 rs->sr_err = backend_attribute( op, NULL, &op->o_req_ndn,
329                                 ava.aa_desc, &vals, ACL_COMPARE );
330                 switch ( rs->sr_err ) {
331                 default:
332                         /* return error only if "disclose"
333                          * is granted on the object */
334                         if ( backend_access( op, NULL, &op->o_req_ndn,
335                                         slap_schema.si_ad_entry,
336                                         NULL, ACL_DISCLOSE, NULL )
337                                         == LDAP_INSUFFICIENT_ACCESS )
338                         {
339                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
340                         }
341                         break;
342
343                 case LDAP_SUCCESS:
344                         if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
345                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
346                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
347                                 vals, &ava.aa_value, op->o_tmpmemctx ) == 0 )
348                         {
349                                 rs->sr_err = LDAP_COMPARE_TRUE;
350                                 break;
351
352                         } else {
353                                 rs->sr_err = LDAP_COMPARE_FALSE;
354                         }
355                         rc = LDAP_SUCCESS;
356                         break;
357                 }
358
359                 send_ldap_result( op, rs );
360
361                 if ( rc == 0 ) {
362                         rs->sr_err = LDAP_SUCCESS;
363                 }
364                 
365                 if ( vals ) {
366                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
367                 }
368         }
369
370 cleanup:;
371         op->o_bd = bd;
372         return rs->sr_err;
373 }
374
375 static int compare_entry(
376         Operation *op,
377         Entry *e,
378         AttributeAssertion *ava )
379 {
380         int rc = LDAP_COMPARE_FALSE;
381         Attribute *a;
382
383         if ( ! access_allowed( op, e,
384                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
385         {       
386                 rc = LDAP_INSUFFICIENT_ACCESS;
387                 goto done;
388         }
389
390         a = attrs_find( e->e_attrs, ava->aa_desc );
391         if( a == NULL ) {
392                 rc = LDAP_NO_SUCH_ATTRIBUTE;
393                 goto done;
394         }
395
396         for(a = attrs_find( e->e_attrs, ava->aa_desc );
397                 a != NULL;
398                 a = attrs_find( a->a_next, ava->aa_desc ))
399         {
400                 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
401                         e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
402                 {       
403                         rc = LDAP_INSUFFICIENT_ACCESS;
404                         break;
405                 }
406
407                 if ( value_find_ex( ava->aa_desc,
408                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
409                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
410                         a->a_nvals, &ava->aa_value, op->o_tmpmemctx ) == 0 )
411                 {
412                         rc = LDAP_COMPARE_TRUE;
413                         break;
414                 }
415         }
416
417 done:
418         if( rc != LDAP_COMPARE_TRUE && rc != LDAP_COMPARE_FALSE ) {
419                 if ( ! access_allowed( op, e,
420                         slap_schema.si_ad_entry, NULL, ACL_DISCLOSE, NULL ) )
421                 {
422                         rc = LDAP_NO_SUCH_OBJECT;
423                 }
424         }
425
426         return rc;
427 }