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