]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
BDB_INDEX code does no harm (but no good yet, not used by filters yet).
[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 int
27 do_compare(
28     Connection  *conn,
29     Operation   *op
30 )
31 {
32         char    *dn = NULL, *ndn=NULL;
33         struct berval desc;
34         struct berval value;
35         struct berval *nvalue;
36         AttributeAssertion ava;
37         Backend *be;
38         int rc = LDAP_SUCCESS;
39         const char *text = NULL;
40         int manageDSAit;
41
42         ava.aa_desc = NULL;
43         desc.bv_val = NULL;
44         value.bv_val = NULL;
45
46 #ifdef NEW_LOGGING
47         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
48                    "do_compare: conn %d\n", conn->c_connid ));
49 #else
50         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
51 #endif
52         /*
53          * Parse the compare request.  It looks like this:
54          *
55          *      CompareRequest := [APPLICATION 14] SEQUENCE {
56          *              entry   DistinguishedName,
57          *              ava     SEQUENCE {
58          *                      type    AttributeType,
59          *                      value   AttributeValue
60          *              }
61          *      }
62          */
63
64         if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
65 #ifdef NEW_LOGGING
66                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
67                            "do_compare: conn %d  ber_scanf failed\n", conn->c_connid ));
68 #else
69                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
70 #endif
71                 send_ldap_disconnect( conn, op,
72                         LDAP_PROTOCOL_ERROR, "decoding error" );
73                 return SLAPD_DISCONNECT;
74         }
75
76         if ( ber_scanf( op->o_ber, "{oo}", &desc, &value ) == LBER_ERROR ) {
77 #ifdef NEW_LOGGING
78                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
79                            "do_compare: conn %d  get ava failed\n", conn->c_connid ));
80 #else
81                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
82 #endif
83                 send_ldap_disconnect( conn, op,
84                         LDAP_PROTOCOL_ERROR, "decoding error" );
85                 rc = SLAPD_DISCONNECT;
86                 goto cleanup;
87         }
88
89         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
90 #ifdef NEW_LOGGING
91                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
92                            "do_compare: conn %d  ber_scanf failed\n", conn->c_connid ));
93 #else
94                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
95 #endif
96                 send_ldap_disconnect( conn, op,
97                         LDAP_PROTOCOL_ERROR, "decoding error" );
98                 rc = SLAPD_DISCONNECT;
99                 goto cleanup;
100         }
101
102         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
103 #ifdef NEW_LOGGING
104                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
105                            "do_compare: conn %d  get_ctrls failed\n", conn->c_connid ));
106 #else
107                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
108 #endif
109                 goto cleanup;
110         } 
111
112         ndn = ch_strdup( dn );
113
114         if( dn_normalize( ndn ) == NULL ) {
115 #ifdef NEW_LOGGING
116                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
117                            "do_compare: conn %d  invalid dn (%s)\n",
118                            conn->c_connid, dn ));
119 #else
120                 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", dn, 0, 0 );
121 #endif
122                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
123                     "invalid DN", NULL, NULL );
124                 goto cleanup;
125         }
126
127         if( *ndn == '\0' ) {
128 #ifdef NEW_LOGGING
129                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
130                            "do_compare: conn %d  compare to root DSE!\n",
131                            conn->c_connid ));
132 #else
133                 Debug( LDAP_DEBUG_ANY, "do_compare: root dse!\n", 0, 0, 0 );
134 #endif
135                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
136                         NULL, "compare upon the root DSE not supported", NULL, NULL );
137                 goto cleanup;
138         }
139
140         manageDSAit = get_manageDSAit( op );
141
142         /*
143          * We could be serving multiple database backends.  Select the
144          * appropriate one, or send a referral to our "referral server"
145          * if we don't hold it.
146          */
147         if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
148                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
149                         NULL, NULL, default_referral, NULL );
150                 rc = 1;
151                 goto cleanup;
152         }
153
154         /* check restrictions */
155         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
156         if( rc != LDAP_SUCCESS ) {
157                 send_ldap_result( conn, op, rc,
158                         NULL, text, NULL, NULL );
159                 goto cleanup;
160         }
161
162         /* check for referrals */
163         rc = backend_check_referrals( be, conn, op, dn, ndn );
164         if ( rc != LDAP_SUCCESS ) {
165                 goto cleanup;
166         }
167
168         rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
169         if( rc != LDAP_SUCCESS ) {
170                 send_ldap_result( conn, op, rc, NULL,
171                     text, NULL, NULL );
172                 goto cleanup;
173         }
174
175         if( !ava.aa_desc->ad_type->sat_equality ) {
176                 /* no equality matching rule */
177                 send_ldap_result( conn, op, rc = LDAP_INAPPROPRIATE_MATCHING, NULL,
178                     "no equality matching rule defined", NULL, NULL );
179                 goto cleanup;
180         }
181
182         rc = value_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &nvalue, &text );
183
184         if( rc != LDAP_SUCCESS ) {
185                 send_ldap_result( conn, op, rc, NULL,
186                     text, NULL, NULL );
187                 goto cleanup;
188         }
189
190         ava.aa_value = nvalue;
191
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, dn, 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             dn, ava.aa_desc->ad_cname->bv_val, ava.aa_value->bv_val );
200 #endif
201
202         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
203             op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname->bv_val, 0 );
204
205
206         /* deref suffix alias if appropriate */
207         ndn = suffix_alias( be, ndn );
208
209         if ( be->be_compare ) {
210                 (*be->be_compare)( be, conn, op, dn, ndn, &ava );
211         } else {
212                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
213                         NULL, "operation not supported within namingContext", NULL, NULL );
214         }
215
216 cleanup:
217         free( dn );
218         free( ndn );
219         free( desc.bv_val );
220         free( value.bv_val );
221         if( ava.aa_desc != NULL ) {
222                 ad_free( ava.aa_desc, 1 );
223         }
224
225         return rc;
226 }