]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
67651aa87792551a012faff9a55288bc61a60d2d
[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         Connection *conn,
31         Operation *op,
32         Entry *e,
33         AttributeAssertion *ava );
34
35 int
36 do_compare(
37     Connection  *conn,
38     Operation   *op
39 )
40 {
41         Entry *entry = NULL;
42         Entry *fentry = NULL;
43         struct berval dn = { 0, NULL };
44         struct berval pdn = { 0, NULL };
45         struct berval ndn = { 0, NULL };
46         struct berval desc = { 0, NULL };
47         struct berval value = { 0, NULL };
48         AttributeAssertion ava = { NULL, { 0, NULL } };
49         Backend *be;
50         int rc = LDAP_SUCCESS;
51         const char *text = NULL;
52         int manageDSAit;
53
54 #ifdef LDAP_SLAPI
55         Slapi_PBlock *pb = op->o_pb;
56 #endif
57
58         ava.aa_desc = NULL;
59
60 #ifdef NEW_LOGGING
61         LDAP_LOG( OPERATION, ENTRY, "do_compare: conn %d\n", conn->c_connid, 0, 0 );
62 #else
63         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
64 #endif
65         /*
66          * Parse the compare request.  It looks like this:
67          *
68          *      CompareRequest := [APPLICATION 14] SEQUENCE {
69          *              entry   DistinguishedName,
70          *              ava     SEQUENCE {
71          *                      type    AttributeType,
72          *                      value   AttributeValue
73          *              }
74          *      }
75          */
76
77         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
78 #ifdef NEW_LOGGING
79                 LDAP_LOG( OPERATION, ERR, 
80                         "do_compare: conn %d  ber_scanf failed\n", conn->c_connid, 0, 0 );
81 #else
82                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
83 #endif
84                 send_ldap_disconnect( conn, op,
85                         LDAP_PROTOCOL_ERROR, "decoding error" );
86                 return SLAPD_DISCONNECT;
87         }
88
89         if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
90 #ifdef NEW_LOGGING
91                 LDAP_LOG( OPERATION, ERR, 
92                         "do_compare: conn %d  get ava failed\n", conn->c_connid, 0, 0 );
93 #else
94                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
95 #endif
96                 send_ldap_disconnect( conn, op,
97                         LDAP_PROTOCOL_ERROR, "decoding error" );
98                 rc = SLAPD_DISCONNECT;
99                 goto cleanup;
100         }
101
102         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
103 #ifdef NEW_LOGGING
104                 LDAP_LOG( OPERATION, ERR, 
105                         "do_compare: conn %d  ber_scanf failed\n", conn->c_connid, 0, 0 );
106 #else
107                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
108 #endif
109                 send_ldap_disconnect( conn, op,
110                         LDAP_PROTOCOL_ERROR, "decoding error" );
111                 rc = SLAPD_DISCONNECT;
112                 goto cleanup;
113         }
114
115         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
116 #ifdef NEW_LOGGING
117                 LDAP_LOG( OPERATION, INFO, 
118                         "do_compare: conn %d  get_ctrls failed\n", conn->c_connid, 0, 0 );
119 #else
120                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
121 #endif
122                 goto cleanup;
123         } 
124
125         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
126         if( rc != LDAP_SUCCESS ) {
127 #ifdef NEW_LOGGING
128                 LDAP_LOG( OPERATION, INFO, 
129                         "do_compare: conn %d  invalid dn (%s)\n",
130                         conn->c_connid, dn.bv_val, 0 );
131 #else
132                 Debug( LDAP_DEBUG_ANY,
133                         "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
134 #endif
135                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
136                     "invalid DN", NULL, NULL );
137                 goto cleanup;
138         }
139
140         rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
141         if( rc != LDAP_SUCCESS ) {
142                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
143                 goto cleanup;
144         }
145
146         rc = value_validate_normalize( ava.aa_desc, SLAP_MR_EQUALITY,
147                 &value, &ava.aa_value, &text );
148         if( rc != LDAP_SUCCESS ) {
149                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
150                 goto cleanup;
151         }
152
153         if( strcasecmp( ndn.bv_val, LDAP_ROOT_DSE ) == 0 ) {
154 #ifdef NEW_LOGGING
155                 LDAP_LOG( OPERATION, ARGS, 
156                         "do_compare: dn (%s) attr(%s) value (%s)\n",
157                         pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
158 #else
159                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
160                         pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
161 #endif
162
163                 Statslog( LDAP_DEBUG_STATS,
164                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
165                         op->o_connid, op->o_opid, pdn.bv_val,
166                         ava.aa_desc->ad_cname.bv_val, 0 );
167
168                 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
169                 if( rc != LDAP_SUCCESS ) {
170                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
171                         goto cleanup;
172                 }
173
174                 rc = root_dse_info( conn, &entry, &text );
175                 if( rc != LDAP_SUCCESS ) {
176                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
177                         goto cleanup;
178                 }
179
180                 fentry = entry;
181
182         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
183 #ifdef NEW_LOGGING
184                 LDAP_LOG( OPERATION, ARGS, 
185                         "do_compare: dn (%s) attr(%s) value (%s)\n",
186                         pdn.bv_val, ava.aa_desc->ad_cname.bv_val,
187                         ava.aa_value.bv_val );
188 #else
189                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
190                         pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
191 #endif
192
193                 Statslog( LDAP_DEBUG_STATS,
194                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
195                         op->o_connid, op->o_opid, pdn.bv_val,
196                         ava.aa_desc->ad_cname.bv_val, 0 );
197
198                 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
199                 if( rc != LDAP_SUCCESS ) {
200                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
201                         rc = 0;
202                         goto cleanup;
203                 }
204
205                 rc = schema_info( &entry, &text );
206                 if( rc != LDAP_SUCCESS ) {
207                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
208                         rc = 0;
209                         goto cleanup;
210                 }
211                 fentry = entry;
212         }
213
214         if( entry ) {
215                 rc = compare_entry( conn, op, entry, &ava );
216                 if( fentry) entry_free( fentry );
217
218                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
219
220                 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
221                         rc = 0;
222                 }
223
224                 goto cleanup;
225         }
226
227         manageDSAit = get_manageDSAit( op );
228
229         /*
230          * We could be serving multiple database backends.  Select the
231          * appropriate one, or send a referral to our "referral server"
232          * if we don't hold it.
233          */
234         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
235                 BerVarray ref = referral_rewrite( default_referral,
236                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
237
238                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
239                         NULL, NULL, ref ? ref : default_referral, NULL );
240
241                 ber_bvarray_free( ref );
242                 rc = 0;
243                 goto cleanup;
244         }
245
246         /* check restrictions */
247         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
248         if( rc != LDAP_SUCCESS ) {
249                 send_ldap_result( conn, op, rc,
250                         NULL, text, NULL, NULL );
251                 goto cleanup;
252         }
253
254         /* check for referrals */
255         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
256         if ( rc != LDAP_SUCCESS ) {
257                 goto cleanup;
258         }
259
260 #ifdef NEW_LOGGING
261         LDAP_LOG( OPERATION, ARGS, 
262                 "do_compare: dn (%s) attr(%s) value (%s)\n",
263                 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
264 #else
265         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
266             pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
267 #endif
268
269         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
270             op->o_connid, op->o_opid, pdn.bv_val,
271                 ava.aa_desc->ad_cname.bv_val, 0 );
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 }