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