]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
0ad02a5b9feb736015ac7804de3b24e2068abbe6
[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 "slap.h"
25
26 int
27 do_compare(
28     Connection  *conn,
29     Operation   *op
30 )
31 {
32         char    *dn, *ndn;
33         Ava     ava;
34         Backend *be;
35         int rc = LDAP_SUCCESS;
36
37         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
38
39         if( op->o_bind_in_progress ) {
40                 Debug( LDAP_DEBUG_ANY, "do_compare: SASL bind in progress.\n",
41                         0, 0, 0 );
42                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
43                         NULL, "SASL bind in progress", NULL, NULL );
44                 return LDAP_SASL_BIND_IN_PROGRESS;
45         }
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{ao}}", &dn, &ava.ava_type,
60             &ava.ava_value ) == 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 -1;
65         }
66
67         if( dn_normalize( dn ) == NULL ) {
68                 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", dn, 0, 0 );
69                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
70                     "invalid DN", NULL, NULL );
71                 free( dn );
72                 ava_free( &ava, 0 );
73                 return rc;
74         }
75
76         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
77                 free( dn );
78                 ava_free( &ava, 0 );
79                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
80                 return rc;
81         } 
82
83         value_normalize( ava.ava_value.bv_val, attr_syntax( ava.ava_type ) );
84
85         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
86             dn, ava.ava_type, ava.ava_value.bv_val );
87
88         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d CMP dn=\"%s\" attr=\"%s\"\n",
89             op->o_connid, op->o_opid, dn, ava.ava_type, 0 );
90
91         ndn = ch_strdup( dn );
92         ldap_pvt_str2upper( ndn );
93
94         /*
95          * We could be serving multiple database backends.  Select the
96          * appropriate one, or send a referral to our "referral server"
97          * if we don't hold it.
98          */
99         if ( (be = select_backend( ndn )) == NULL ) {
100                 free( dn );
101                 free( ndn );
102                 ava_free( &ava, 0 );
103
104                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
105                         NULL, NULL, default_referral, NULL );
106                 return 1;
107         }
108
109         /* deref suffix alias if appropriate */
110         ndn = suffix_alias( be, ndn );
111
112         if ( be->be_compare ) {
113                 (*be->be_compare)( be, conn, op, dn, ndn, &ava );
114         } else {
115                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
116                         NULL, "Function not implemented", NULL, NULL );
117         }
118
119         free( dn );
120         free( ndn );
121         ava_free( &ava, 0 );
122
123         return rc;
124 }