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