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