]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
Update BER decoding of PDU to use "o" (struct berval) instead of
[openldap] / servers / slapd / compare.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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
26 static int compare_entry(
27         Connection *conn,
28         Operation *op,
29         Entry *e,
30         AttributeAssertion *ava );
31
32 int
33 do_compare(
34     Connection  *conn,
35     Operation   *op
36 )
37 {
38         Entry *entry = NULL;
39         struct berval dn = { 0, NULL };
40         char *ndn = NULL;
41         struct berval desc;
42         struct berval value;
43         struct berval *nvalue;
44         AttributeAssertion ava;
45         Backend *be;
46         int rc = LDAP_SUCCESS;
47         const char *text = NULL;
48         int manageDSAit;
49
50         ava.aa_desc = NULL;
51         desc.bv_val = NULL;
52         value.bv_val = NULL;
53
54 #ifdef NEW_LOGGING
55         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
56                 "do_compare: conn %d\n", conn->c_connid ));
57 #else
58         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
59 #endif
60         /*
61          * Parse the compare request.  It looks like this:
62          *
63          *      CompareRequest := [APPLICATION 14] SEQUENCE {
64          *              entry   DistinguishedName,
65          *              ava     SEQUENCE {
66          *                      type    AttributeType,
67          *                      value   AttributeValue
68          *              }
69          *      }
70          */
71
72         if ( ber_scanf( op->o_ber, "{o" /*}*/, &dn ) == LBER_ERROR ) {
73 #ifdef NEW_LOGGING
74                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
75                         "do_compare: conn %d  ber_scanf failed\n", conn->c_connid ));
76 #else
77                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
78 #endif
79                 send_ldap_disconnect( conn, op,
80                         LDAP_PROTOCOL_ERROR, "decoding error" );
81                 return SLAPD_DISCONNECT;
82         }
83
84         if ( ber_scanf( op->o_ber, "{oo}", &desc, &value ) == LBER_ERROR ) {
85 #ifdef NEW_LOGGING
86                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
87                         "do_compare: conn %d  get ava failed\n", conn->c_connid ));
88 #else
89                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
90 #endif
91                 send_ldap_disconnect( conn, op,
92                         LDAP_PROTOCOL_ERROR, "decoding error" );
93                 rc = SLAPD_DISCONNECT;
94                 goto cleanup;
95         }
96
97         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
98 #ifdef NEW_LOGGING
99                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
100                         "do_compare: conn %d  ber_scanf failed\n", conn->c_connid ));
101 #else
102                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
103 #endif
104                 send_ldap_disconnect( conn, op,
105                         LDAP_PROTOCOL_ERROR, "decoding error" );
106                 rc = SLAPD_DISCONNECT;
107                 goto cleanup;
108         }
109
110         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
111 #ifdef NEW_LOGGING
112                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
113                         "do_compare: conn %d  get_ctrls failed\n", conn->c_connid ));
114 #else
115                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
116 #endif
117                 goto cleanup;
118         } 
119
120         ndn = ch_strdup( dn.bv_val );
121
122         if( dn_normalize( ndn ) == NULL ) {
123 #ifdef NEW_LOGGING
124                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
125                         "do_compare: conn %d  invalid dn (%s)\n",
126                         conn->c_connid, dn.bv_val ));
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_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &nvalue, &text );
143         if( rc != LDAP_SUCCESS ) {
144                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
145                 goto cleanup;
146         }
147
148         ava.aa_value = nvalue;
149
150         if( strcasecmp( ndn, LDAP_ROOT_DSE ) == 0 ) {
151 #ifdef NEW_LOGGING
152                 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
153                         "do_compare: conn %d  dn (%s) attr(%s) value (%s)\n",
154                         conn->c_connid, dn.bv_val, ava.aa_desc->ad_cname.bv_val,
155                         ava.aa_value->bv_val ));
156 #else
157                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
158                         dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
159 #endif
160
161                 Statslog( LDAP_DEBUG_STATS,
162                         "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
163                         op->o_connid, op->o_opid, dn.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         } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
179 #ifdef NEW_LOGGING
180                 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
181                         "do_compare: conn %d  dn (%s) attr(%s) value (%s)\n",
182                         conn->c_connid, dn.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                         dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
187 #endif
188
189                 Statslog( LDAP_DEBUG_STATS,
190                         "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
191                         op->o_connid, op->o_opid, dn.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         }
208
209         if( entry ) {
210                 rc = compare_entry( conn, op, entry, &ava );
211                 entry_free( entry );
212
213                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
214
215                 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
216                         rc = 0;
217                 }
218
219                 goto cleanup;
220         }
221
222         manageDSAit = get_manageDSAit( op );
223
224         /*
225          * We could be serving multiple database backends.  Select the
226          * appropriate one, or send a referral to our "referral server"
227          * if we don't hold it.
228          */
229         if ( (be = select_backend( ndn, manageDSAit, 0 )) == NULL ) {
230                 struct berval **ref = referral_rewrite( default_referral,
231                         NULL, dn.bv_val, LDAP_SCOPE_DEFAULT );
232
233                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
234                         NULL, NULL, ref ? ref : default_referral, NULL );
235
236                 ber_bvecfree( ref );
237                 rc = 0;
238                 goto cleanup;
239         }
240
241         /* check restrictions */
242         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
243         if( rc != LDAP_SUCCESS ) {
244                 send_ldap_result( conn, op, rc,
245                         NULL, text, NULL, NULL );
246                 goto cleanup;
247         }
248
249         /* check for referrals */
250         rc = backend_check_referrals( be, conn, op, dn.bv_val, ndn );
251         if ( rc != LDAP_SUCCESS ) {
252                 goto cleanup;
253         }
254
255 #ifdef NEW_LOGGING
256         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
257                 "do_compare: conn %d     dn (%s) attr(%s) value (%s)\n",
258                 conn->c_connid, dn.bv_val, ava.aa_desc->ad_cname.bv_val,
259                 ava.aa_value->bv_val ));
260 #else
261         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
262             dn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
263 #endif
264
265         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
266             op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname.bv_val, 0 );
267
268
269         /* deref suffix alias if appropriate */
270         ndn = suffix_alias( be, ndn );
271
272         if ( be->be_compare ) {
273                 (*be->be_compare)( be, conn, op, dn.bv_val, ndn, &ava );
274         } else {
275                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
276                         NULL, "operation not supported within namingContext", NULL, NULL );
277         }
278
279 cleanup:
280         free( dn.bv_val );
281         free( ndn );
282         free( desc.bv_val );
283         free( value.bv_val );
284
285         return rc;
286 }
287
288 static int compare_entry(
289         Connection *conn,
290         Operation *op,
291         Entry *e,
292         AttributeAssertion *ava )
293 {
294         int rc = LDAP_NO_SUCH_ATTRIBUTE;
295         Attribute *a;
296
297         if ( ! access_allowed( NULL, conn, op, e,
298                 ava->aa_desc, ava->aa_value, ACL_COMPARE ) )
299         {       
300                 return LDAP_INSUFFICIENT_ACCESS;
301         }
302
303         for(a = attrs_find( e->e_attrs, ava->aa_desc );
304                 a != NULL;
305                 a = attrs_find( a->a_next, ava->aa_desc ))
306         {
307                 rc = LDAP_COMPARE_FALSE;
308
309                 if ( value_find( ava->aa_desc, a->a_vals, ava->aa_value ) == 0 ) {
310                         rc = LDAP_COMPARE_TRUE;
311                         break;
312                 }
313         }
314
315         return rc;
316 }