]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
Replace existing SLAP_MR_ matching flags with:
[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         char    *dn = NULL, *ndn=NULL;
40         struct berval desc;
41         struct berval value;
42         struct berval *nvalue;
43         AttributeAssertion ava;
44         Backend *be;
45         int rc = LDAP_SUCCESS;
46         const char *text = NULL;
47         int manageDSAit;
48
49         ava.aa_desc = NULL;
50         desc.bv_val = NULL;
51         value.bv_val = 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, "{a" /*}*/, &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         ndn = ch_strdup( dn );
120
121         if( dn_normalize( ndn ) == NULL ) {
122 #ifdef NEW_LOGGING
123                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
124                            "do_compare: conn %d  invalid dn (%s)\n",
125                            conn->c_connid, dn ));
126 #else
127                 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", dn, 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 = slap_bv2ad( &desc, &ava.aa_desc, &text );
135         if( rc != LDAP_SUCCESS ) {
136                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
137                 goto cleanup;
138         }
139
140         rc = value_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &nvalue, &text );
141         if( rc != LDAP_SUCCESS ) {
142                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
143                 goto cleanup;
144         }
145
146         ava.aa_value = nvalue;
147
148         if( strcasecmp( ndn, LDAP_ROOT_DSE ) == 0 ) {
149 #ifdef NEW_LOGGING
150                 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
151                         "do_compare: conn %d  dn (%s) attr(%s) value (%s)\n",
152                         conn->c_connid, dn, ava.aa_desc->ad_cname.bv_val,
153                         ava.aa_value->bv_val ));
154 #else
155                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
156                         dn, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
157 #endif
158
159                 Statslog( LDAP_DEBUG_STATS,
160                         "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
161                         op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname.bv_val, 0 );
162
163                 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
164                 if( rc != LDAP_SUCCESS ) {
165                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
166                         goto cleanup;
167                 }
168
169                 rc = root_dse_info( conn, &entry, &text );
170                 if( rc != LDAP_SUCCESS ) {
171                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
172                         goto cleanup;
173                 }
174
175         } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
176 #ifdef NEW_LOGGING
177                 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
178                         "do_compare: conn %d  dn (%s) attr(%s) value (%s)\n",
179                         conn->c_connid, dn, ava.aa_desc->ad_cname->bv_val,
180                         ava.aa_value->bv_val ));
181 #else
182                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
183                         dn, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
184 #endif
185
186                 Statslog( LDAP_DEBUG_STATS,
187                         "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
188                         op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname.bv_val, 0 );
189
190                 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
191                 if( rc != LDAP_SUCCESS ) {
192                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
193                         rc = 0;
194                         goto cleanup;
195                 }
196
197                 rc = schema_info( &entry, &text );
198                 if( rc != LDAP_SUCCESS ) {
199                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
200                         rc = 0;
201                         goto cleanup;
202                 }
203         }
204
205         if( entry ) {
206                 rc = compare_entry( conn, op, entry, &ava );
207                 entry_free( entry );
208
209                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
210
211                 if( rc == LDAP_COMPARE_TRUE || rc == LDAP_COMPARE_FALSE ) {
212                         rc = 0;
213                 }
214
215                 goto cleanup;
216         }
217
218         manageDSAit = get_manageDSAit( op );
219
220         /*
221          * We could be serving multiple database backends.  Select the
222          * appropriate one, or send a referral to our "referral server"
223          * if we don't hold it.
224          */
225         if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
226                 struct berval **ref = referral_rewrite( default_referral,
227                         NULL, dn, LDAP_SCOPE_DEFAULT );
228
229                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
230                         NULL, NULL, ref ? ref : default_referral, NULL );
231
232                 ber_bvecfree( ref );
233                 rc = 0;
234                 goto cleanup;
235         }
236
237         /* check restrictions */
238         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
239         if( rc != LDAP_SUCCESS ) {
240                 send_ldap_result( conn, op, rc,
241                         NULL, text, NULL, NULL );
242                 goto cleanup;
243         }
244
245         /* check for referrals */
246         rc = backend_check_referrals( be, conn, op, dn, ndn );
247         if ( rc != LDAP_SUCCESS ) {
248                 goto cleanup;
249         }
250
251 #ifdef NEW_LOGGING
252         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
253                    "do_compare: conn %d  dn (%s) attr(%s) value (%s)\n",
254                    conn->c_connid, dn, ava.aa_desc->ad_cname.bv_val,
255                    ava.aa_value->bv_val ));
256 #else
257         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
258             dn, ava.aa_desc->ad_cname.bv_val, ava.aa_value->bv_val );
259 #endif
260
261         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
262             op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname.bv_val, 0 );
263
264
265         /* deref suffix alias if appropriate */
266         ndn = suffix_alias( be, ndn );
267
268         if ( be->be_compare ) {
269                 (*be->be_compare)( be, conn, op, dn, ndn, &ava );
270         } else {
271                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
272                         NULL, "operation not supported within namingContext", NULL, NULL );
273         }
274
275 cleanup:
276         free( dn );
277         free( ndn );
278         free( desc.bv_val );
279         free( value.bv_val );
280
281         return rc;
282 }
283
284 static int compare_entry(
285         Connection *conn,
286         Operation *op,
287         Entry *e,
288         AttributeAssertion *ava )
289 {
290         int rc = LDAP_NO_SUCH_ATTRIBUTE;
291         Attribute *a;
292
293         if ( ! access_allowed( NULL, conn, op, e,
294                 ava->aa_desc, ava->aa_value, ACL_COMPARE ) )
295         {       
296                 return LDAP_INSUFFICIENT_ACCESS;
297         }
298
299         for(a = attrs_find( e->e_attrs, ava->aa_desc );
300                 a != NULL;
301                 a = attrs_find( a->a_next, ava->aa_desc ))
302         {
303                 rc = LDAP_COMPARE_FALSE;
304
305                 if ( value_find( ava->aa_desc, a->a_vals, ava->aa_value ) == 0 ) {
306                         rc = LDAP_COMPARE_TRUE;
307                         break;
308                 }
309         }
310
311         return rc;
312 }