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