]> git.sur5r.net Git - openldap/blob - servers/slapd/compare.c
Add multimaster replication support (ITS#170) based upon
[openldap] / servers / slapd / compare.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/socket.h>
18
19 #include "slap.h"
20
21 int
22 do_compare(
23     Connection  *conn,
24     Operation   *op
25 )
26 {
27         char    *ndn;
28         Ava     ava;
29         Backend *be;
30         int rc = LDAP_SUCCESS;
31
32         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
33
34         if( op->o_bind_in_progress ) {
35                 Debug( LDAP_DEBUG_ANY, "do_compare: SASL bind in progress.\n",
36                         0, 0, 0 );
37                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
38                         NULL, "SASL bind in progress", NULL, NULL );
39                 return LDAP_SASL_BIND_IN_PROGRESS;
40         }
41
42         /*
43          * Parse the compare request.  It looks like this:
44          *
45          *      CompareRequest := [APPLICATION 14] SEQUENCE {
46          *              entry   DistinguishedName,
47          *              ava     SEQUENCE {
48          *                      type    AttributeType,
49          *                      value   AttributeValue
50          *              }
51          *      }
52          */
53
54         if ( ber_scanf( op->o_ber, "{a{ao}}", &ndn, &ava.ava_type,
55             &ava.ava_value ) == LBER_ERROR ) {
56                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
57                 send_ldap_disconnect( conn, op,
58                         LDAP_PROTOCOL_ERROR, "decoding error" );
59                 return -1;
60         }
61
62         if( dn_normalize_case( ndn ) == NULL ) {
63                 Debug( LDAP_DEBUG_ANY, "do_compare: invalid dn (%s)\n", ndn, 0, 0 );
64                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
65                     "invalid DN", NULL, NULL );
66                 free( ndn );
67                 ava_free( &ava, 0 );
68                 return rc;
69         }
70
71         if( ( rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
72                 free( ndn );
73                 ava_free( &ava, 0 );
74                 Debug( LDAP_DEBUG_ANY, "do_compare: get_ctrls failed\n", 0, 0, 0 );
75                 return rc;
76         } 
77
78         value_normalize( ava.ava_value.bv_val, attr_syntax( ava.ava_type ) );
79
80         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
81             ndn, ava.ava_type, ava.ava_value.bv_val );
82
83         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d CMP dn=\"%s\" attr=\"%s\"\n",
84             op->o_connid, op->o_opid, ndn, ava.ava_type, 0 );
85
86         /*
87          * We could be serving multiple database backends.  Select the
88          * appropriate one, or send a referral to our "referral server"
89          * if we don't hold it.
90          */
91         if ( (be = select_backend( ndn )) == NULL ) {
92                 free( ndn );
93                 ava_free( &ava, 0 );
94
95                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
96                         NULL, NULL, default_referral, NULL );
97                 return 1;
98         }
99
100         /* deref suffix alias if appropriate */
101         ndn = suffix_alias( be, ndn );
102
103         if ( be->be_compare ) {
104                 (*be->be_compare)( be, conn, op, ndn, &ava );
105         } else {
106                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
107                         NULL, "Function not implemented", NULL, NULL );
108         }
109
110         free( ndn );
111         ava_free( &ava, 0 );
112
113         return rc;
114 }