]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
syntax and mr plugins
[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 = { 0 };
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, ENTRY, "do_compare: conn %d\n", conn->c_connid, 0, 0 );
54 #else
55         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
56 #endif
57         /*
58          * Parse the compare request.  It looks like this:
59          *
60          *      CompareRequest := [APPLICATION 14] SEQUENCE {
61          *              entry   DistinguishedName,
62          *              ava     SEQUENCE {
63          *                      type    AttributeType,
64          *                      value   AttributeValue
65          *              }
66          *      }
67          */
68
69         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
70 #ifdef NEW_LOGGING
71                 LDAP_LOG( OPERATION, ERR, 
72                         "do_compare: conn %d  ber_scanf failed\n", conn->c_connid, 0, 0 );
73 #else
74                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
75 #endif
76                 send_ldap_disconnect( conn, op,
77                         LDAP_PROTOCOL_ERROR, "decoding error" );
78                 return SLAPD_DISCONNECT;
79         }
80
81         if ( ber_scanf( op->o_ber, "{mm}", &desc, &value ) == LBER_ERROR ) {
82 #ifdef NEW_LOGGING
83                 LDAP_LOG( OPERATION, ERR, 
84                         "do_compare: conn %d  get ava failed\n", conn->c_connid, 0, 0 );
85 #else
86                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
87 #endif
88                 send_ldap_disconnect( conn, op,
89                         LDAP_PROTOCOL_ERROR, "decoding error" );
90                 rc = SLAPD_DISCONNECT;
91                 goto cleanup;
92         }
93
94         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
95 #ifdef NEW_LOGGING
96                 LDAP_LOG( OPERATION, ERR, 
97                         "do_compare: conn %d  ber_scanf failed\n", conn->c_connid, 0, 0 );
98 #else
99                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
100 #endif
101                 send_ldap_disconnect( conn, op,
102                         LDAP_PROTOCOL_ERROR, "decoding error" );
103                 rc = SLAPD_DISCONNECT;
104                 goto cleanup;
105         }
106
107         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
108 #ifdef NEW_LOGGING
109                 LDAP_LOG( OPERATION, INFO, 
110                         "do_compare: conn %d  get_ctrls failed\n", conn->c_connid, 0, 0 );
111 #else
112                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
113 #endif
114                 goto cleanup;
115         } 
116
117         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
118         if( rc != LDAP_SUCCESS ) {
119 #ifdef NEW_LOGGING
120                 LDAP_LOG( OPERATION, INFO, 
121                         "do_compare: conn %d  invalid dn (%s)\n",
122                         conn->c_connid, dn.bv_val, 0 );
123 #else
124                 Debug( LDAP_DEBUG_ANY,
125                         "do_compare: invalid dn (%s)\n", dn.bv_val, 0, 0 );
126 #endif
127                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
128                     "invalid DN", NULL, NULL );
129                 goto cleanup;
130         }
131
132         rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
133         if( rc != LDAP_SUCCESS ) {
134                 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
135                 goto cleanup;
136         }
137
138         rc = value_validate_normalize( ava.aa_desc, SLAP_MR_EQUALITY,
139                 &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, ARGS, 
148                         "do_compare: dn (%s) attr(%s) value (%s)\n",
149                         pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
150 #else
151                 Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
152                         pdn.bv_val, ava.aa_desc->ad_cname.bv_val, ava.aa_value.bv_val );
153 #endif
154
155                 Statslog( LDAP_DEBUG_STATS,
156                         "conn=%lu op=%lu CMP dn=\"%s\" attr=\"%s\"\n",
157                         op->o_connid, op->o_opid, pdn.bv_val,
158                         ava.aa_desc->ad_cname.bv_val, 0 );
159
160                 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
161                 if( rc != LDAP_SUCCESS ) {
162                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
163                         goto cleanup;
164                 }
165
166                 rc = root_dse_info( conn, &entry, &text );
167                 if( rc != LDAP_SUCCESS ) {
168                         send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
169                         goto cleanup;
170                 }
171
172 #ifdef SLAPD_SCHEMA_DN
173         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG( OPERATION, ARGS, 
176                         "do_compare: dn (%s) attr(%s) value (%s)\n",
177                         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=%lu op=%lu 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 #endif /* SLAPD_SCHEMA_DN */
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, 0 )) == NULL ) {
226                 BerVarray ref = referral_rewrite( default_referral,
227                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
228
229                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
230                         NULL, NULL, ref ? ref : default_referral, NULL );
231
232                 ber_bvarray_free( 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, &pdn, &ndn );
247         if ( rc != LDAP_SUCCESS ) {
248                 goto cleanup;
249         }
250
251 #ifdef NEW_LOGGING
252         LDAP_LOG( OPERATION, ARGS, 
253                 "do_compare: dn (%s) attr(%s) value (%s)\n",
254                 pdn.bv_val, ava.aa_desc->ad_cname.bv_val, 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=%lu op=%lu 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( pdn.bv_val );
278         free( ndn.bv_val );
279         if ( ava.aa_value.bv_val ) free( ava.aa_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, NULL ) )
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 }