]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
Berkeley DB 4.2 support (DB 4.2 required by default)
[openldap] / servers / slapd / compare.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "ldap_pvt.h"
25 #include "slap.h"
26 #ifdef LDAP_SLAPI
27 #include "slapi.h"
28 #endif
29
30 static int compare_entry(
31         Operation *op,
32         Entry *e,
33         AttributeAssertion *ava );
34
35 int
36 do_compare(
37     Operation   *op,
38     SlapReply   *rs
39 )
40 {
41         Entry *entry = NULL;
42         struct berval dn = { 0, NULL };
43         struct berval desc = { 0, NULL };
44         struct berval value = { 0, NULL };
45         AttributeAssertion ava = { NULL, { 0, NULL } };
46         int manageDSAit;
47
48         ava.aa_desc = NULL;
49
50 #ifdef NEW_LOGGING
51         LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
52 #else
53         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
54 #endif
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 #ifdef NEW_LOGGING
69                 LDAP_LOG( OPERATION, ERR, 
70                         "do_compare: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
71 #else
72                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
73 #endif
74                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
75                 return SLAPD_DISCONNECT;
76         }
77
78         if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
79 #ifdef NEW_LOGGING
80                 LDAP_LOG( OPERATION, ERR, 
81                         "do_compare: conn %d  get ava failed\n", op->o_connid, 0, 0 );
82 #else
83                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
84 #endif
85                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
86                 return SLAPD_DISCONNECT;
87         }
88
89         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
90 #ifdef NEW_LOGGING
91                 LDAP_LOG( OPERATION, ERR, 
92                         "do_compare: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
93 #else
94                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
95 #endif
96                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
97                 return SLAPD_DISCONNECT;
98         }
99
100         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
101 #ifdef NEW_LOGGING
102                 LDAP_LOG( OPERATION, INFO, 
103                         "do_compare: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
104 #else
105                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
106 #endif
107                 goto cleanup;
108         } 
109
110         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
111         if( rs->sr_err != LDAP_SUCCESS ) {
112 #ifdef NEW_LOGGING
113                 LDAP_LOG( OPERATION, INFO, 
114                         "do_compare: conn %d  invalid dn (%s)\n",
115                         op->o_connid, dn.bv_val, 0 );
116 #else
117                 Debug( LDAP_DEBUG_ANY,
118                         "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
119 #endif
120                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
121                 goto cleanup;
122         }
123
124         rs->sr_err = slap_bv2ad( &desc, &ava.aa_desc, &rs->sr_text );
125         if( rs->sr_err != LDAP_SUCCESS ) {
126                 send_ldap_result( op, rs );
127                 goto cleanup;
128         }
129
130         rs->sr_err = asserted_value_validate_normalize( ava.aa_desc,
131                 ava.aa_desc->ad_type->sat_equality,
132                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
133                 &value, &ava.aa_value, &rs->sr_text, op->o_tmpmemctx );
134         if( rs->sr_err != LDAP_SUCCESS ) {
135                 send_ldap_result( op, rs );
136                 goto cleanup;
137         }
138
139         if( strcasecmp( op->o_req_ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
140 #ifdef NEW_LOGGING
141                 LDAP_LOG( OPERATION, ARGS, 
142                         "do_compare: dn (%s) attr(%s) value (%s)\n",
143                         op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
144 #else
145                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
146                         op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
147 #endif
148
149                 Statslog( LDAP_DEBUG_STATS,
150                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
151                         op->o_connid, op->o_opid, op->o_req_dn.bv_val,
152                         ava.aa_desc->ad_cname.bv_val, 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, &global_schemandn ) ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG( OPERATION, ARGS, 
168                         "do_compare: dn (%s) attr(%s) value (%s)\n",
169                         op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val,
170                         ava.aa_value.bv_val );
171 #else
172                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
173                         op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
174 #endif
175
176                 Statslog( LDAP_DEBUG_STATS,
177                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
178                         op->o_connid, op->o_opid, op->o_req_dn.bv_val,
179                         ava.aa_desc->ad_cname.bv_val, 0 );
180
181                 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
182                         send_ldap_result( op, rs );
183                         rs->sr_err = 0;
184                         goto cleanup;
185                 }
186
187                 rs->sr_err = schema_info( &entry, &rs->sr_text );
188                 if( rs->sr_err != LDAP_SUCCESS ) {
189                         send_ldap_result( op, rs );
190                         rs->sr_err = 0;
191                         goto cleanup;
192                 }
193         }
194
195         if( entry ) {
196                 rs->sr_err = compare_entry( op, entry, &ava );
197                 entry_free( entry );
198
199                 send_ldap_result( op, rs );
200
201                 if( rs->sr_err == LDAP_COMPARE_TRUE || rs->sr_err == LDAP_COMPARE_FALSE ) {
202                         rs->sr_err = 0;
203                 }
204
205                 goto cleanup;
206         }
207
208         manageDSAit = get_manageDSAit( op );
209
210         /*
211          * We could be serving multiple database backends.  Select the
212          * appropriate one, or send a referral to our "referral server"
213          * if we don't hold it.
214          */
215         if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
216                 rs->sr_ref = referral_rewrite( default_referral,
217                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
218
219                 rs->sr_err = LDAP_REFERRAL;
220                 if (!rs->sr_ref) rs->sr_ref = default_referral;
221                 send_ldap_result( op, rs );
222
223                 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
224                 rs->sr_err = 0;
225                 goto cleanup;
226         }
227
228         /* check restrictions */
229         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
230                 send_ldap_result( op, rs );
231                 goto cleanup;
232         }
233
234         /* check for referrals */
235         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
236                 goto cleanup;
237         }
238
239 #ifdef NEW_LOGGING
240         LDAP_LOG( OPERATION, ARGS, 
241                 "do_compare: dn (%s) attr(%s) value (%s)\n",
242                 op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
243 #else
244         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
245             op->o_req_dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
246 #endif
247
248         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
249             op->o_connid, op->o_opid, op->o_req_dn.bv_val,
250                 ava.aa_desc->ad_cname.bv_val, 0 );
251
252 #if defined( LDAP_SLAPI )
253 #define pb      op->o_pb
254         if ( pb ) {
255                 slapi_x_pblock_set_operation( pb, op );
256                 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
257                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
258                 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
259                 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
260
261                 rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
262                 if ( rs->sr_err < 0 ) {
263                         /*
264                          * A preoperation plugin failure will abort the
265                          * entire operation.
266                          */
267 #ifdef NEW_LOGGING
268                         LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
269                                         "failed\n", 0, 0, 0);
270 #else
271                         Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
272                                         "failed.\n", 0, 0, 0);
273 #endif
274                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 )  ||
275                                  rs->sr_err == LDAP_SUCCESS ) {
276                                 rs->sr_err = LDAP_OTHER;
277                         }
278                         goto cleanup;
279                 }
280         }
281 #endif /* defined( LDAP_SLAPI ) */
282
283         if ( op->o_bd->be_compare ) {
284                 op->orc_ava = &ava;
285                 op->o_bd->be_compare( op, rs );
286         } else {
287                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
288                         "operation not supported within namingContext" );
289         }
290
291 #if defined( LDAP_SLAPI )
292         if ( pb && doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 ) {
293 #ifdef NEW_LOGGING
294                 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
295                                 "failed\n", 0, 0, 0 );
296 #else
297                 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
298                                 "failed.\n", 0, 0, 0);
299 #endif
300         }
301 #endif /* defined( LDAP_SLAPI ) */
302
303 cleanup:
304         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
305         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
306         if ( ava.aa_value.bv_val ) op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
307
308         return rs->sr_err;
309 }
310
311 static int compare_entry(
312         Operation *op,
313         Entry *e,
314         AttributeAssertion *ava )
315 {
316         int rc = LDAP_NO_SUCH_ATTRIBUTE;
317         Attribute *a;
318
319         if ( ! access_allowed( op, e,
320                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
321         {       
322                 return LDAP_INSUFFICIENT_ACCESS;
323         }
324
325         for(a = attrs_find( e->e_attrs, ava->aa_desc );
326                 a != NULL;
327                 a = attrs_find( a->a_next, ava->aa_desc ))
328         {
329                 rc = LDAP_COMPARE_FALSE;
330
331                 if ( value_find_ex( ava->aa_desc,
332                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
333                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
334                         a->a_nvals,
335                         &ava->aa_value, op->o_tmpmemctx ) == 0 )
336                 {
337                         rc = LDAP_COMPARE_TRUE;
338                         break;
339                 }
340         }
341
342         return rc;
343 }