]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
cb291b073d45ccdb0f8665a587f7eff95503a15c
[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
41         ava.aa_desc = NULL;
42         desc.bv_val = NULL;
43         value.bv_val = NULL;
44
45         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
46
47         /*
48          * Parse the compare request.  It looks like this:
49          *
50          *      CompareRequest := [APPLICATION 14] SEQUENCE {
51          *              entry   DistinguishedName,
52          *              ava     SEQUENCE {
53          *                      type    AttributeType,
54          *                      value   AttributeValue
55          *              }
56          *      }
57          */
58
59         if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
60                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
61                 send_ldap_disconnect( conn, op,
62                         LDAP_PROTOCOL_ERROR, "decoding error" );
63                 return SLAPD_DISCONNECT;
64         }
65
66         if ( ber_scanf( op->o_ber, "{oo}", &desc, &value ) == LBER_ERROR ) {
67                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
68                 send_ldap_disconnect( conn, op,
69                         LDAP_PROTOCOL_ERROR, "decoding error" );
70                 rc = SLAPD_DISCONNECT;
71                 goto cleanup;
72         }
73
74         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
75                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
76                 send_ldap_disconnect( conn, op,
77                         LDAP_PROTOCOL_ERROR, "decoding error" );
78                 rc = SLAPD_DISCONNECT;
79                 goto cleanup;
80         }
81
82         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
83                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
84                 goto cleanup;
85         } 
86
87         ndn = ch_strdup( dn );
88
89         if( dn_normalize( ndn ) == NULL ) {
90                 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", dn, 0, 0 );
91                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
92                     "invalid DN", NULL, NULL );
93                 goto cleanup;
94         }
95
96         if( ndn == '\0' ) {
97                 Debug( LDAP_DEBUG_ANY, "do_compare: root dse!\n", 0, 0, 0 );
98                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
99                         NULL, "compare upon the root DSE not supported", NULL, NULL );
100                 goto cleanup;
101         }
102
103         /*
104          * We could be serving multiple database backends.  Select the
105          * appropriate one, or send a referral to our "referral server"
106          * if we don't hold it.
107          */
108         if ( (be = select_backend( ndn )) == NULL ) {
109                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
110                         NULL, NULL, default_referral, NULL );
111                 rc = 1;
112                 goto cleanup;
113         }
114
115         /* check restrictions */
116         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
117         if( rc != LDAP_SUCCESS ) {
118                 send_ldap_result( conn, op, rc,
119                         NULL, text, NULL, NULL );
120                 goto cleanup;
121         }
122
123         /* check for referrals */
124         rc = backend_check_referrals( be, conn, op, dn, ndn );
125         if ( rc != LDAP_SUCCESS ) {
126                 goto cleanup;
127         }
128
129         rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
130         if( rc != LDAP_SUCCESS ) {
131                 send_ldap_result( conn, op, rc, NULL,
132                     text, NULL, NULL );
133                 goto cleanup;
134         }
135
136         if( !ava.aa_desc->ad_type->sat_equality ) {
137                 /* no equality matching rule */
138                 send_ldap_result( conn, op, rc = LDAP_INAPPROPRIATE_MATCHING, NULL,
139                     "no equality matching rule defined", NULL, NULL );
140                 goto cleanup;
141         }
142
143         rc = value_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &nvalue, &text );
144
145         if( rc != LDAP_SUCCESS ) {
146                 send_ldap_result( conn, op, rc, NULL,
147                     text, NULL, NULL );
148                 goto cleanup;
149         }
150
151         ava.aa_value = nvalue;
152
153         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
154             dn, ava.aa_desc->ad_cname->bv_val, ava.aa_value->bv_val );
155
156         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
157             op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname->bv_val, 0 );
158
159
160         /* deref suffix alias if appropriate */
161         ndn = suffix_alias( be, ndn );
162
163         if ( be->be_compare ) {
164                 (*be->be_compare)( be, conn, op, dn, ndn, &ava );
165         } else {
166                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
167                         NULL, "operation not supported within namingContext", NULL, NULL );
168         }
169
170 cleanup:
171         free( dn );
172         free( ndn );
173         free( desc.bv_val );
174         free( value.bv_val );
175         if( ava.aa_desc != NULL ) {
176                 ad_free( ava.aa_desc, 1 );
177         }
178
179         return rc;
180 }