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