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