]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
trim use of uninitialized data; please review
[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
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                 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 ( !BER_BVISNULL( &ava.aa_value ) ) {
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                         "%s CMP dn=\"%s\" attr=\"%s\"\n",
145                         op->o_log_prefix, op->o_req_dn.bv_val,
146                         ava.aa_desc->ad_cname.bv_val, 0, 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                         "%s CMP dn=\"%s\" attr=\"%s\"\n",
166                         op->o_log_prefix, op->o_req_dn.bv_val,
167                         ava.aa_desc->ad_cname.bv_val, 0, 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                 op->o_bd = frontendDB;
213                 send_ldap_result( op, rs );
214                 op->o_bd = NULL;
215
216                 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
217                 rs->sr_err = 0;
218                 goto cleanup;
219         }
220
221         /* check restrictions */
222         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
223                 send_ldap_result( op, rs );
224                 goto cleanup;
225         }
226
227         /* check for referrals */
228         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
229                 goto cleanup;
230         }
231
232         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
233             op->o_req_dn.bv_val,
234                 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
235
236         Statslog( LDAP_DEBUG_STATS, "%s CMP dn=\"%s\" attr=\"%s\"\n",
237                 op->o_log_prefix, op->o_req_dn.bv_val,
238                 ava.aa_desc->ad_cname.bv_val, 0, 0 );
239
240         op->orc_ava = &ava;
241         if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
242                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
243                         "entryDN compare not supported" );
244
245         } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
246                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
247                         "subschemaSubentry compare not supported" );
248
249 #ifndef SLAP_COMPARE_IN_FRONTEND
250         } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
251                 && op->o_bd->be_has_subordinates )
252         {
253                 int     rc, hasSubordinates = LDAP_SUCCESS;
254
255                 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
256                 if ( rc == 0 && entry ) {
257                         if ( ! access_allowed( op, entry,
258                                 ava.aa_desc, &ava.aa_value, ACL_COMPARE, NULL ) )
259                         {       
260                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
261                                 
262                         } else {
263                                 rc = rs->sr_err = op->o_bd->be_has_subordinates( op,
264                                                 entry, &hasSubordinates );
265                                 be_entry_release_r( op, entry );
266                         }
267                 }
268
269                 if ( rc == 0 ) {
270                         int     asserted;
271
272                         asserted = bvmatch( &ava.aa_value, &slap_true_bv )
273                                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
274                         if ( hasSubordinates == asserted ) {
275                                 rs->sr_err = LDAP_COMPARE_TRUE;
276
277                         } else {
278                                 rs->sr_err = LDAP_COMPARE_FALSE;
279                         }
280
281                 } else {
282 #ifdef SLAP_ACL_HONOR_DISCLOSE
283                         /* return error only if "disclose"
284                          * is granted on the object */
285                         if ( backend_access( op, NULL, &op->o_req_ndn,
286                                         slap_schema.si_ad_entry,
287                                         NULL, ACL_DISCLOSE, NULL ) == LDAP_INSUFFICIENT_ACCESS )
288                         {
289                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
290                         }
291 #endif /* SLAP_ACL_HONOR_DISCLOSE */
292                 }
293
294                 send_ldap_result( op, rs );
295
296                 if ( rc == 0 ) {
297                         rs->sr_err = LDAP_SUCCESS;
298                 }
299
300         } else if ( op->o_bd->be_compare ) {
301                 rs->sr_err = op->o_bd->be_compare( op, rs );
302
303 #endif /* ! SLAP_COMPARE_IN_FRONTEND */
304         } else {
305                 rs->sr_err = SLAP_CB_CONTINUE;
306         }
307
308         if ( rs->sr_err == SLAP_CB_CONTINUE ) {
309                 /* do our best to compare that AVA
310                  * 
311                  * NOTE: this code is used only
312                  * if SLAP_COMPARE_IN_FRONTEND 
313                  * is #define'd (it's not by default)
314                  * or if op->o_bd->be_compare is NULL.
315                  * 
316                  * FIXME: one potential issue is that
317                  * if SLAP_COMPARE_IN_FRONTEND overlays
318                  * are not executed for compare. */
319                 BerVarray       vals = NULL;
320                 int             rc = LDAP_OTHER;
321
322                 rs->sr_err = backend_attribute( op, NULL, &op->o_req_ndn,
323                                 ava.aa_desc, &vals, ACL_COMPARE );
324                 switch ( rs->sr_err ) {
325                 default:
326 #ifdef SLAP_ACL_HONOR_DISCLOSE
327                         /* return error only if "disclose"
328                          * is granted on the object */
329                         if ( backend_access( op, NULL, &op->o_req_ndn,
330                                         slap_schema.si_ad_entry,
331                                         NULL, ACL_DISCLOSE, NULL )
332                                         == LDAP_INSUFFICIENT_ACCESS )
333                         {
334                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
335                         }
336 #endif /* SLAP_ACL_HONOR_DISCLOSE */
337                         break;
338
339                 case LDAP_SUCCESS:
340                         if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
341                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
342                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
343                                 vals, &ava.aa_value, op->o_tmpmemctx ) == 0 )
344                         {
345                                 rs->sr_err = LDAP_COMPARE_TRUE;
346                                 break;
347
348                         } else {
349                                 rs->sr_err = LDAP_COMPARE_FALSE;
350                         }
351                         rc = LDAP_SUCCESS;
352                         break;
353                 }
354
355                 send_ldap_result( op, rs );
356
357                 if ( rc == 0 ) {
358                         rs->sr_err = LDAP_SUCCESS;
359                 }
360                 
361                 if ( vals ) {
362                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
363                 }
364         }
365
366 cleanup:;
367         return rs->sr_err;
368 }
369
370 static int compare_entry(
371         Operation *op,
372         Entry *e,
373         AttributeAssertion *ava )
374 {
375         int rc = LDAP_COMPARE_FALSE;
376         Attribute *a;
377
378         if ( ! access_allowed( op, e,
379                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
380         {       
381                 rc = LDAP_INSUFFICIENT_ACCESS;
382                 goto done;
383         }
384
385         a = attrs_find( e->e_attrs, ava->aa_desc );
386         if( a == NULL ) {
387                 rc = LDAP_NO_SUCH_ATTRIBUTE;
388                 goto done;
389         }
390
391         for(a = attrs_find( e->e_attrs, ava->aa_desc );
392                 a != NULL;
393                 a = attrs_find( a->a_next, ava->aa_desc ))
394         {
395                 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
396                         e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
397                 {       
398                         rc = LDAP_INSUFFICIENT_ACCESS;
399                         break;
400                 }
401
402                 if ( value_find_ex( ava->aa_desc,
403                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
404                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
405                         a->a_nvals, &ava->aa_value, op->o_tmpmemctx ) == 0 )
406                 {
407                         rc = LDAP_COMPARE_TRUE;
408                         break;
409                 }
410         }
411
412 done:
413 #ifdef LDAP_ACL_HONOR_DISCLOSE
414         if( rc != LDAP_COMPARE_TRUE && rc != LDAP_COMPARE_FALSE ) {
415                 if ( ! access_allowed( op, e,
416                         slap_schema.si_ad_entry, NULL, ACL_DISCLOSE, NULL ) )
417                 {
418                         rc = LDAP_NO_SUCH_OBJECT;
419                 }
420         }
421 #endif
422
423         return rc;
424 }