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