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