]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
Reorder error detection based upon precedence
[openldap] / servers / slapd / compare.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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         Backend *be;
37         int rc = LDAP_SUCCESS;
38 #ifdef SLAPD_SCHEMA_NOT_COMPAT
39         char *text = NULL;
40         AttributeAssertion ava;
41
42         ava.aa_desc = NULL;
43 #else
44         Ava     ava;
45 #endif
46
47         desc.bv_val = NULL;
48         value.bv_val = NULL;
49
50         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
51
52         if( op->o_bind_in_progress ) {
53                 Debug( LDAP_DEBUG_ANY, "do_compare: SASL bind in progress.\n",
54                         0, 0, 0 );
55                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
56                         NULL, "SASL bind in progress", NULL, NULL );
57                 return LDAP_SASL_BIND_IN_PROGRESS;
58         }
59
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                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
74                 send_ldap_disconnect( conn, op,
75                         LDAP_PROTOCOL_ERROR, "decoding error" );
76                 return SLAPD_DISCONNECT;
77         }
78
79         if ( ber_scanf( op->o_ber, "{oo}", &desc, &value ) == LBER_ERROR ) {
80                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
81                 send_ldap_disconnect( conn, op,
82                         LDAP_PROTOCOL_ERROR, "decoding error" );
83                 rc = SLAPD_DISCONNECT;
84                 goto cleanup;
85         }
86
87         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
88                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
89                 send_ldap_disconnect( conn, op,
90                         LDAP_PROTOCOL_ERROR, "decoding error" );
91                 rc = SLAPD_DISCONNECT;
92                 goto cleanup;
93         }
94
95         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
96                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
97                 goto cleanup;
98         } 
99
100         ndn = ch_strdup( dn );
101
102         if( dn_normalize( ndn ) == NULL ) {
103                 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", dn, 0, 0 );
104                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
105                     "invalid DN", NULL, NULL );
106                 goto cleanup;
107         }
108
109 #ifdef SLAPD_SCHEMA_NOT_COMPAT
110         rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
111         if( rc != LDAP_SUCCESS ) {
112                 send_ldap_result( conn, op, rc, NULL,
113                     text, NULL, NULL );
114                 goto cleanup;
115         }
116         ava.aa_value = &value;
117
118         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
119             dn, ava.aa_desc->ad_cname, ava.aa_value->bv_val );
120
121         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
122             op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname, 0 );
123
124 #else
125         ava.ava_type = desc.bv_val;
126         ava.ava_value = value;
127         attr_normalize( ava.ava_type );
128         value_normalize( ava.ava_value.bv_val, attr_syntax( ava.ava_type ) );
129
130         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
131             dn, ava.ava_type, ava.ava_value.bv_val );
132
133         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
134             op->o_connid, op->o_opid, dn, ava.ava_type, 0 );
135 #endif
136
137
138         /*
139          * We could be serving multiple database backends.  Select the
140          * appropriate one, or send a referral to our "referral server"
141          * if we don't hold it.
142          */
143         if ( (be = select_backend( ndn )) == NULL ) {
144                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
145                         NULL, NULL, default_referral, NULL );
146                 rc = 1;
147                 goto cleanup;
148         }
149
150         /* make sure this backend recongizes critical controls */
151         rc = backend_check_controls( be, conn, op ) ;
152
153         if( rc != LDAP_SUCCESS ) {
154                 send_ldap_result( conn, op, rc,
155                         NULL, NULL, NULL, NULL );
156                 goto cleanup;
157         }
158
159         /* deref suffix alias if appropriate */
160         ndn = suffix_alias( be, ndn );
161
162         if ( be->be_compare ) {
163                 (*be->be_compare)( be, conn, op, dn, ndn, &ava );
164         } else {
165                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
166                         NULL, "Function not implemented", NULL, NULL );
167         }
168
169 cleanup:
170         free( dn );
171         free( ndn );
172         free( desc.bv_val );
173         free( value.bv_val );
174 #ifdef SLAPD_SCHEMA_NOT_COMPAT
175         if( ava.aa_desc != NULL ) {
176                 ad_free( ava.aa_desc, 1 );
177         }
178 #endif
179
180         return rc;
181 }