]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
4e228a618c4030c87d01f790b1c9b080261567af
[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         Backend *be;
37         int rc = LDAP_SUCCESS;
38         char *text = NULL;
39 #ifdef SLAPD_SCHEMA_NOT_COMPAT
40         AttributeAssertion ava;
41         ava.aa_desc = NULL;
42 #else
43         Ava     ava;
44 #endif
45
46         desc.bv_val = NULL;
47         value.bv_val = NULL;
48
49         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
50
51         /*
52          * Parse the compare request.  It looks like this:
53          *
54          *      CompareRequest := [APPLICATION 14] SEQUENCE {
55          *              entry   DistinguishedName,
56          *              ava     SEQUENCE {
57          *                      type    AttributeType,
58          *                      value   AttributeValue
59          *              }
60          *      }
61          */
62
63         if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
64                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
65                 send_ldap_disconnect( conn, op,
66                         LDAP_PROTOCOL_ERROR, "decoding error" );
67                 return SLAPD_DISCONNECT;
68         }
69
70         if ( ber_scanf( op->o_ber, "{oo}", &desc, &value ) == LBER_ERROR ) {
71                 Debug( LDAP_DEBUG_ANY, "do_compare: get ava failed\n", 0, 0, 0 );
72                 send_ldap_disconnect( conn, op,
73                         LDAP_PROTOCOL_ERROR, "decoding error" );
74                 rc = SLAPD_DISCONNECT;
75                 goto cleanup;
76         }
77
78         if ( ber_scanf( op->o_ber, /*{*/ "}" ) == LBER_ERROR ) {
79                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
80                 send_ldap_disconnect( conn, op,
81                         LDAP_PROTOCOL_ERROR, "decoding error" );
82                 rc = SLAPD_DISCONNECT;
83                 goto cleanup;
84         }
85
86         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
87                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
88                 goto cleanup;
89         } 
90
91         ndn = ch_strdup( dn );
92
93         if( dn_normalize( ndn ) == NULL ) {
94                 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", dn, 0, 0 );
95                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
96                     "invalid DN", NULL, NULL );
97                 goto cleanup;
98         }
99
100 #ifdef SLAPD_SCHEMA_NOT_COMPAT
101         ava.aa_desc = NULL;
102         rc = slap_bv2ad( &desc, &ava.aa_desc, &text );
103         if( rc != LDAP_SUCCESS ) {
104                 send_ldap_result( conn, op, rc, NULL,
105                     text, NULL, NULL );
106                 goto cleanup;
107         }
108
109         rc = value_normalize( ava.aa_desc, SLAP_MR_EQUALITY, &value, &text );
110
111         if( rc != LDAP_SUCCESS ) {
112                 send_ldap_result( conn, op, rc, NULL,
113                     text, NULL, NULL );
114                 goto cleanup;
115         }
116
117         ava.aa_value = &value;
118
119         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
120             dn, ava.aa_desc->ad_cname, ava.aa_value->bv_val );
121
122         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
123             op->o_connid, op->o_opid, dn, ava.aa_desc->ad_cname, 0 );
124
125 #else
126         ava.ava_type = desc.bv_val;
127         ava.ava_value = value;
128         attr_normalize( ava.ava_type );
129         value_normalize( ava.ava_value.bv_val, attr_syntax( ava.ava_type ) );
130
131         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
132             dn, ava.ava_type, ava.ava_value.bv_val );
133
134         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
135             op->o_connid, op->o_opid, dn, ava.ava_type, 0 );
136 #endif
137
138
139         /*
140          * We could be serving multiple database backends.  Select the
141          * appropriate one, or send a referral to our "referral server"
142          * if we don't hold it.
143          */
144         if ( (be = select_backend( ndn )) == NULL ) {
145                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
146                         NULL, NULL, default_referral, NULL );
147                 rc = 1;
148                 goto cleanup;
149         }
150
151         /* make sure this backend recongizes critical controls */
152         rc = backend_check_controls( be, conn, op, &text ) ;
153
154         if( rc != LDAP_SUCCESS ) {
155                 send_ldap_result( conn, op, rc,
156                         NULL, text, NULL, NULL );
157                 goto cleanup;
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, "compare function not implemented", NULL, NULL );
168         }
169
170 cleanup:
171         free( dn );
172         free( ndn );
173         free( desc.bv_val );
174         free( value.bv_val );
175 #ifdef SLAPD_SCHEMA_NOT_COMPAT
176         if( ava.aa_desc != NULL ) {
177                 ad_free( ava.aa_desc, 1 );
178         }
179 #endif
180
181         return rc;
182 }