]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
ITS#3353 consolidate slapd globals into a single struct
[openldap] / servers / slapd / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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 };
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( SLAPD_GLOBAL(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 = SLAPD_GLOBAL(default_referral);
211                 send_ldap_result( op, rs );
212
213                 if (rs->sr_ref != SLAPD_GLOBAL(default_referral)) ber_bvarray_free( rs->sr_ref );
214                 rs->sr_err = 0;
215                 goto cleanup;
216         }
217
218         /* check restrictions */
219         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
220                 send_ldap_result( op, rs );
221                 goto cleanup;
222         }
223
224         /* check for referrals */
225         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
226                 goto cleanup;
227         }
228
229         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
230             op->o_req_dn.bv_val,
231                 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
232
233         Statslog( LDAP_DEBUG_STATS, "%s CMP dn=\"%s\" attr=\"%s\"\n",
234                 op->o_log_prefix, op->o_req_dn.bv_val,
235                 ava.aa_desc->ad_cname.bv_val, 0, 0 );
236
237 #if defined( LDAP_SLAPI )
238 #define pb      op->o_pb
239         if ( pb ) {
240                 slapi_int_pblock_set_operation( pb, op );
241                 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
242                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
243                 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
244                 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
245
246                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
247                         SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
248                 if ( rs->sr_err < 0 ) {
249                         /*
250                          * A preoperation plugin failure will abort the
251                          * entire operation.
252                          */
253                         Debug(LDAP_DEBUG_TRACE,
254                                 "do_compare: compare preoperation plugin failed\n",
255                                 0, 0, 0);
256                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
257                                 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
258                         {
259                                 rs->sr_err = LDAP_OTHER;
260                         }
261                         goto cleanup;
262                 }
263         }
264 #endif /* defined( LDAP_SLAPI ) */
265
266         op->orc_ava = &ava;
267         if ( ava.aa_desc == slap_schema.si_ad_entryDN ) {
268                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
269                         "entryDN compare not supported" );
270
271         } else if ( ava.aa_desc == slap_schema.si_ad_subschemaSubentry ) {
272                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
273                         "subschemaSubentry compare not supported" );
274
275         } else if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
276                 && op->o_bd->be_has_subordinates )
277         {
278                 int     rc, hasSubordinates = LDAP_SUCCESS;
279
280                 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &entry );
281                 if ( rc == 0 && entry ) {
282                         rc = op->o_bd->be_has_subordinates( op, entry,
283                                 &hasSubordinates );
284                         be_entry_release_r( op, entry );
285                 }
286
287                 if ( rc == 0 ) {
288                         int     asserted;
289
290                         asserted = bvmatch( &ava.aa_value, &slap_true_bv )
291                                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
292                         if ( hasSubordinates == asserted ) {
293                                 rs->sr_err = LDAP_COMPARE_TRUE;
294                         } else {
295                                 rs->sr_err = LDAP_COMPARE_FALSE;
296                         }
297                 }
298                 send_ldap_result( op, rs );
299
300                 if( rc == 0 ) rs->sr_err = LDAP_SUCCESS;
301
302         } else if ( op->o_bd->be_compare ) {
303                 op->o_bd->be_compare( op, rs );
304
305         } else {
306                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
307                         "operation not supported within namingContext" );
308         }
309
310 #if defined( LDAP_SLAPI )
311         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
312                 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
313         {
314                 Debug(LDAP_DEBUG_TRACE,
315                         "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
316         }
317 #endif /* defined( LDAP_SLAPI ) */
318
319 cleanup:;
320         return rs->sr_err;
321 }
322
323 static int compare_entry(
324         Operation *op,
325         Entry *e,
326         AttributeAssertion *ava )
327 {
328         int rc;
329         Attribute *a;
330
331         if ( ! access_allowed( op, e,
332                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
333         {       
334                 return LDAP_INSUFFICIENT_ACCESS;
335         }
336
337         a = attrs_find( e->e_attrs, ava->aa_desc );
338         if( a == NULL ) return LDAP_NO_SUCH_ATTRIBUTE;
339
340         rc = LDAP_COMPARE_FALSE;
341         for(a = attrs_find( e->e_attrs, ava->aa_desc );
342                 a != NULL;
343                 a = attrs_find( a->a_next, ava->aa_desc ))
344         {
345                 if (( ava->aa_desc != a->a_desc ) && ! access_allowed( op,
346                         e, a->a_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
347                 {       
348                         rc = LDAP_INSUFFICIENT_ACCESS;
349                         break;
350                 }
351
352                 if ( value_find_ex( ava->aa_desc,
353                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
354                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
355                         a->a_nvals,
356                         &ava->aa_value, op->o_tmpmemctx ) == 0 )
357                 {
358                         rc = LDAP_COMPARE_TRUE;
359                         break;
360                 }
361         }
362
363         return rc;
364 }