]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
now I remember why I introduced the 'has_ldapinfo_dn_ru' flag
[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 {
49         Entry *entry = NULL;
50         struct berval dn = { 0, NULL };
51         struct berval desc = { 0, NULL };
52         struct berval value = { 0, NULL };
53         AttributeAssertion ava = { NULL, { 0, NULL } };
54         int manageDSAit;
55
56         ava.aa_desc = NULL;
57
58 #ifdef NEW_LOGGING
59         LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", op->o_connid, 0, 0 );
60 #else
61         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
62 #endif
63         /*
64          * Parse the compare request.  It looks like this:
65          *
66          *      CompareRequest := [APPLICATION 14] SEQUENCE {
67          *              entry   DistinguishedName,
68          *              ava     SEQUENCE {
69          *                      type    AttributeType,
70          *                      value   AttributeValue
71          *              }
72          *      }
73          */
74
75         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
76 #ifdef NEW_LOGGING
77                 LDAP_LOG( OPERATION, ERR, 
78                         "do_compare: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
79 #else
80                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
81 #endif
82                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
83                 return SLAPD_DISCONNECT;
84         }
85
86         if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
87 #ifdef NEW_LOGGING
88                 LDAP_LOG( OPERATION, ERR, 
89                         "do_compare: conn %d  get ava failed\n", op->o_connid, 0, 0 );
90 #else
91                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
92 #endif
93                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
94                 return SLAPD_DISCONNECT;
95         }
96
97         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
98 #ifdef NEW_LOGGING
99                 LDAP_LOG( OPERATION, ERR, 
100                         "do_compare: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
101 #else
102                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
103 #endif
104                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
105                 return SLAPD_DISCONNECT;
106         }
107
108         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
109 #ifdef NEW_LOGGING
110                 LDAP_LOG( OPERATION, INFO, 
111                         "do_compare: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
112 #else
113                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
114 #endif
115                 goto cleanup;
116         } 
117
118         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, 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, 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 #define pb      op->o_pb
262         if ( pb ) {
263                 slapi_int_pblock_set_operation( pb, op );
264                 slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void *)dn.bv_val );
265                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
266                 slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, (void *)desc.bv_val );
267                 slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&value );
268
269                 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_COMPARE_FN, pb );
270                 if ( rs->sr_err < 0 ) {
271                         /*
272                          * A preoperation plugin failure will abort the
273                          * entire operation.
274                          */
275 #ifdef NEW_LOGGING
276                         LDAP_LOG( OPERATION, INFO, "do_compare: compare preoperation plugin "
277                                         "failed\n", 0, 0, 0);
278 #else
279                         Debug(LDAP_DEBUG_TRACE, "do_compare: compare preoperation plugin "
280                                         "failed.\n", 0, 0, 0);
281 #endif
282                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 )  ||
283                                  rs->sr_err == LDAP_SUCCESS ) {
284                                 rs->sr_err = LDAP_OTHER;
285                         }
286                         goto cleanup;
287                 }
288         }
289 #endif /* defined( LDAP_SLAPI ) */
290
291         if ( op->o_bd->be_compare ) {
292                 op->orc_ava = &ava;
293                 op->o_bd->be_compare( op, rs );
294         } else {
295                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
296                         "operation not supported within namingContext" );
297         }
298
299 #if defined( LDAP_SLAPI )
300         if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_COMPARE_FN, pb ) < 0 ) {
301 #ifdef NEW_LOGGING
302                 LDAP_LOG( OPERATION, INFO, "do_compare: compare postoperation plugins "
303                                 "failed\n", 0, 0, 0 );
304 #else
305                 Debug(LDAP_DEBUG_TRACE, "do_compare: compare postoperation plugins "
306                                 "failed.\n", 0, 0, 0);
307 #endif
308         }
309 #endif /* defined( LDAP_SLAPI ) */
310
311 cleanup:
312         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
313         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
314         if ( ava.aa_value.bv_val ) op->o_tmpfree( ava.aa_value.bv_val, op->o_tmpmemctx );
315
316         return rs->sr_err;
317 }
318
319 static int compare_entry(
320         Operation *op,
321         Entry *e,
322         AttributeAssertion *ava )
323 {
324         int rc = LDAP_NO_SUCH_ATTRIBUTE;
325         Attribute *a;
326
327         if ( ! access_allowed( op, e,
328                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
329         {       
330                 return LDAP_INSUFFICIENT_ACCESS;
331         }
332
333         for(a = attrs_find( e->e_attrs, ava->aa_desc );
334                 a != NULL;
335                 a = attrs_find( a->a_next, ava->aa_desc ))
336         {
337                 rc = LDAP_COMPARE_FALSE;
338
339                 if ( value_find_ex( ava->aa_desc,
340                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
341                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
342                         a->a_nvals,
343                         &ava->aa_value, op->o_tmpmemctx ) == 0 )
344                 {
345                         rc = LDAP_COMPARE_TRUE;
346                         break;
347                 }
348         }
349
350         return rc;
351 }