]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
Referrals should not (except in special cases) be
[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 "ldap_pvt.h"
33 #include "slap.h"
34 #ifdef LDAP_SLAPI
35 #include "slapi/slapi.h"
36 #endif
37
38 static int compare_entry(
39         Operation *op,
40         Entry *e,
41         AttributeAssertion *ava );
42
43 int
44 do_compare(
45     Operation   *op,
46     SlapReply   *rs )
47 {
48         struct berval dn = BER_BVNULL;
49         struct berval desc = BER_BVNULL;
50         struct berval value = BER_BVNULL;
51         AttributeAssertion ava = { NULL, BER_BVNULL };
52
53         ava.aa_desc = NULL;
54
55 #ifdef NEW_LOGGING
56         LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
57 #else
58         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
59 #endif
60         /*
61          * Parse the compare request.  It looks like this:
62          *
63          *      CompareRequest := [APPLICATION 14] SEQUENCE {
64          *              entry   DistinguishedName,
65          *              ava     SEQUENCE {
66          *                      type    AttributeType,
67          *                      value   AttributeValue
68          *              }
69          *      }
70          */
71
72         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
73 #ifdef NEW_LOGGING
74                 LDAP_LOG( OPERATION, ERR, 
75                         "do_compare: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
76 #else
77                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
78 #endif
79                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
80                 return SLAPD_DISCONNECT;
81         }
82
83         if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
84 #ifdef NEW_LOGGING
85                 LDAP_LOG( OPERATION, ERR, 
86                         "do_compare: conn %d  get ava failed\n", op->o_connid, 0, 0 );
87 #else
88                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
89 #endif
90                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
91                 return SLAPD_DISCONNECT;
92         }
93
94         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
95 #ifdef NEW_LOGGING
96                 LDAP_LOG( OPERATION, ERR, 
97                         "do_compare: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
98 #else
99                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
100 #endif
101                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
102                 return SLAPD_DISCONNECT;
103         }
104
105         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
106 #ifdef NEW_LOGGING
107                 LDAP_LOG( OPERATION, INFO, 
108                         "do_compare: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
109 #else
110                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
111 #endif
112                 goto cleanup;
113         } 
114
115         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
116                 op->o_tmpmemctx );
117         if( rs->sr_err != LDAP_SUCCESS ) {
118 #ifdef NEW_LOGGING
119                 LDAP_LOG( OPERATION, INFO, 
120                         "do_compare: conn %d  invalid dn (%s)\n",
121                         op->o_connid, dn.bv_val, 0 );
122 #else
123                 Debug( LDAP_DEBUG_ANY,
124                         "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
125 #endif
126                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
127                 goto cleanup;
128         }
129
130         rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
131         if( rs->sr_err != LDAP_SUCCESS ) {
132                 send_ldap_result( op, rs );
133                 goto cleanup;
134         }
135
136         rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
137                 ava.aa_desc->ad_type->sat_equality,
138                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
139                 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
140         if( rs->sr_err != LDAP_SUCCESS ) {
141                 send_ldap_result( op, rs );
142                 goto cleanup;
143         }
144
145         op->orc_ava = &ava;
146
147         op->o_bd = frontendDB;
148         rs->sr_err = frontendDB->be_compare( op, rs );
149
150 cleanup:;
151         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
152         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
153         if ( ava.aa_value.bv_val ) {
154                 op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
155         }
156
157         return rs->sr_err;
158 }
159
160 int
161 fe_op_compare( Operation *op, SlapReply *rs )
162 {
163         Entry *entry = NULL;
164         int manageDSAit;
165         AttributeAssertion ava = *op->orc_ava;
166
167         if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
168 #ifdef NEW_LOGGING
169                 LDAP_LOG( OPERATION, ARGS, 
170                         "do_compare: dn (%s) attr(%s) value (%s)\n",
171                         op->o_req_dn.bv_val,
172                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
173 #else
174                 Debug( LDAP_DEBUG_ARGS,
175                         "do_compare: dn (%s) attr (%s) value (%s)\n",
176                         op->o_req_dn.bv_val,
177                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
178 #endif
179
180                 Statslog( LDAP_DEBUG_STATS,
181                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
182                         op->o_connid, op->o_opid, op->o_req_dn.bv_val,
183                         ava.aa_desc->ad_cname.bv_val, 0 );
184
185                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
186                         send_ldap_result( op, rs );
187                         goto cleanup;
188                 }
189
190                 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
191                 if( rs->sr_err != LDAP_SUCCESS ) {
192                         send_ldap_result( op, rs );
193                         goto cleanup;
194                 }
195
196         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
197 #ifdef NEW_LOGGING
198                 LDAP_LOG( OPERATION, ARGS, 
199                         "do_compare: dn (%s) attr(%s) value (%s)\n",
200                         op->o_req_dn.bv_val,
201                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
202 #else
203                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
204                         op->o_req_dn.bv_val,
205                         ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
206 #endif
207
208                 Statslog( LDAP_DEBUG_STATS,
209                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
210                         op->o_connid, op->o_opid, op->o_req_dn.bv_val,
211                         ava.aa_desc->ad_cname.bv_val, 0 );
212
213                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
214                         send_ldap_result( op, rs );
215                         rs->sr_err = 0;
216                         goto cleanup;
217                 }
218
219                 rs->sr_err = schema_info( &entry, &rs->sr_text );
220                 if( rs->sr_err != LDAP_SUCCESS ) {
221                         send_ldap_result( op, rs );
222                         rs->sr_err = 0;
223                         goto cleanup;
224                 }
225         }
226
227         if( entry ) {
228                 rs->sr_err = compare_entry( op, entry, &ava );
229                 entry_free( entry );
230
231                 send_ldap_result( op, rs );
232
233                 if( rs->sr_err == LDAP_COMPARE_TRUE ||
234                         rs->sr_err == LDAP_COMPARE_FALSE )
235                 {
236                         rs->sr_err = LDAP_SUCCESS;
237                 }
238
239                 goto cleanup;
240         }
241
242         manageDSAit = get_manageDSAit( op );
243
244         /*
245          * We could be serving multiple database backends.  Select the
246          * appropriate one, or send a referral to our "referral server"
247          * if we don't hold it.
248          */
249         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
250         if ( op->o_bd == NULL ) {
251                 rs->sr_ref = referral_rewrite( default_referral,
252                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
253
254                 rs->sr_err = LDAP_REFERRAL;
255                 if (!rs->sr_ref) rs->sr_ref = default_referral;
256                 send_ldap_result( op, rs );
257
258                 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
259                 rs->sr_err = 0;
260                 goto cleanup;
261         }
262
263         /* check restrictions */
264         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
265                 send_ldap_result( op, rs );
266                 goto cleanup;
267         }
268
269         /* check for referrals */
270         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
271                 goto cleanup;
272         }
273
274 #ifdef NEW_LOGGING
275         LDAP_LOG( OPERATION, ARGS, 
276                 "do_compare: dn (%s) attr(%s) value (%s)\n",
277                 op->o_req_dn.bv_val,
278                 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
279 #else
280         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
281             op->o_req_dn.bv_val,
282                 ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
283 #endif
284
285         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
286             op->o_connid, op->o_opid, op->o_req_dn.bv_val,
287                 ava.aa_desc->ad_cname.bv_val, 0 );
288
289 #if defined( LDAP_SLAPI )
290 #define pb      op->o_pb
291         if ( pb ) {
292                 slapi_int_pblock_set_operation( pb, op );
293                 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)op->o_req_dn.bv_val );
294                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
295                 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)ava.aa_desc->ad_cname.bv_val );
296                 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&ava.aa_value );
297
298                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
299                         SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
300                 if ( rs->sr_err < 0 ) {
301                         /*
302                          * A preoperation plugin failure will abort the
303                          * entire operation.
304                          */
305 #ifdef NEW_LOGGING
306                         LDAP_LOG( OPERATION, INFO,
307                                 "do_compare: compare preoperation plugin failed\n",
308                                 0, 0, 0);
309 #else
310                         Debug(LDAP_DEBUG_TRACE,
311                                 "do_compare: compare preoperation plugin failed\n",
312                                 0, 0, 0);
313 #endif
314                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
315                                 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
316                         {
317                                 rs->sr_err = LDAP_OTHER;
318                         }
319                         goto cleanup;
320                 }
321         }
322 #endif /* defined( LDAP_SLAPI ) */
323
324         op->orc_ava = &ava;
325         if ( ava.aa_desc == slap_schema.si_ad_hasSubordinates
326                         && op->o_bd->be_has_subordinates )
327         {
328                 int     rc, hasSubordinates = LDAP_SUCCESS;
329
330                 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL,
331                                 0, &entry );
332                 if ( rc == 0 && entry ) {
333                         rc = op->o_bd->be_has_subordinates( op, entry,
334                                         &hasSubordinates );
335                         be_entry_release_r( op, entry );
336                 }
337
338                 if ( rc == 0 ) {
339                         int     asserted;
340
341                         asserted = bvmatch( &ava.aa_value, &slap_true_bv )
342                                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
343                         if ( hasSubordinates == asserted ) {
344                                 rs->sr_err = LDAP_COMPARE_TRUE;
345                         } else {
346                                 rs->sr_err = LDAP_COMPARE_FALSE;
347                         }
348                 }
349                 send_ldap_result( op, rs );
350
351                 if ( rs->sr_err == LDAP_COMPARE_TRUE ||
352                                 rs->sr_err == LDAP_COMPARE_FALSE )
353                 {
354                         rs->sr_err = LDAP_SUCCESS;
355                 }
356         
357         } else if ( op->o_bd->be_compare ) {
358                 op->o_bd->be_compare( op, rs );
359
360         } else {
361                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
362                         "operation not supported within namingContext" );
363         }
364
365 #if defined( LDAP_SLAPI )
366         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
367                 SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 )
368         {
369 #ifdef NEW_LOGGING
370                 LDAP_LOG( OPERATION, INFO,
371                         "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
372 #else
373                 Debug(LDAP_DEBUG_TRACE,
374                         "do_compare: compare postoperation plugins failed\n", 0, 0, 0 );
375 #endif
376         }
377 #endif /* defined( LDAP_SLAPI ) */
378
379 cleanup:;
380         return rs->sr_err;
381 }
382
383 static int compare_entry(
384         Operation *op,
385         Entry *e,
386         AttributeAssertion *ava )
387 {
388         int rc = LDAP_NO_SUCH_ATTRIBUTE;
389         Attribute *a;
390
391         if ( ! access_allowed( op, e,
392                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
393         {       
394                 return LDAP_INSUFFICIENT_ACCESS;
395         }
396
397         for(a = attrs_find( e->e_attrs, ava->aa_desc );
398                 a != NULL;
399                 a = attrs_find( a->a_next, ava->aa_desc ))
400         {
401                 rc = LDAP_COMPARE_FALSE;
402
403                 if ( value_find_ex( ava->aa_desc,
404                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
405                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
406                         a->a_nvals,
407                         &ava->aa_value, op->o_tmpmemctx ) == 0 )
408                 {
409                         rc = LDAP_COMPARE_TRUE;
410                         break;
411                 }
412         }
413
414         return rc;
415 }