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