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