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