]> git.sur5r.net Git - openldap/blob - servers/ldapd/compare.c
88ac1604f7e4fd2205d1a81b850eed4a7801a39d
[openldap] / servers / ldapd / compare.c
1 /*
2  * Copyright (c) 1990 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 <stdio.h>
14 #include <quipu/commonarg.h>
15 #include <quipu/attrvalue.h>
16 #include <quipu/ds_error.h>
17 #include <quipu/compare.h>
18 #include <quipu/dap2.h>
19 #include <quipu/dua.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include "lber.h"
23 #include "ldap.h"
24 #include "common.h"
25
26 #ifdef COMPAT20
27 extern int      ldap_compat;
28 #define COMPTAG (ldap_compat == 20 ? OLD_LDAP_RES_COMPARE : LDAP_RES_COMPARE)
29 #else
30 #define COMPTAG LDAP_RES_COMPARE
31 #endif
32
33 int
34 do_compare( 
35     Sockbuf     *clientsb,
36     struct msg  *m,
37     BerElement  *ber
38 )
39 {
40         char                    *dn, *attr, *value;
41         int                     rc;
42         struct ds_compare_arg   ca;
43         AttributeType           type;
44         static CommonArgs       common = default_common_args;
45         extern short            ldap_dn_syntax;
46
47         Debug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
48
49         /*
50          * Parse the compare request.  It looks like this:
51          *      CompareRequest := [APPLICATION 14] SEQUENCE {
52          *              entry   DistinguishedName,
53          *              ava     SEQUENCE {
54          *                      type    AttributeType,
55          *                      value   AttributeValue
56          *              }
57          *      }
58          */
59
60 #if ISODEPACKAGE == IC
61 #if ICRELEASE > 2
62         DAS_CompareArgument_INIT ( &ca );
63 #endif
64 #endif
65
66         if ( ber_scanf( ber, "{a{aa}}", &dn, &attr, &value ) == LBER_ERROR ) {
67                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
68                 send_ldap_msgresult( clientsb, COMPTAG, m,
69                     LDAP_PROTOCOL_ERROR, NULL, "" );
70                 return( 0 );
71         }
72
73         Debug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s) value (%s)\n",
74             dn, attr, value );
75
76         ca.cma_object = ldap_str2dn( dn );
77         free( dn );
78         if ( ca.cma_object == NULLDN ) {
79                 Debug( LDAP_DEBUG_ANY, "ldap_str2dn failed\n", 0, 0, 0 );
80                 send_ldap_msgresult( clientsb, COMPTAG, m,
81                     LDAP_INVALID_DN_SYNTAX, NULL, "" );
82                 return( 0 );
83         }
84
85         type = str2AttrT( attr );
86         if ( type == NULLAttrT ) {
87                 Debug( LDAP_DEBUG_ANY, "str2AttrT failed\n", 0, 0, 0 );
88                 send_ldap_msgresult( clientsb, COMPTAG, m,
89                     LDAP_UNDEFINED_TYPE, NULL, attr );
90                 free( attr );
91                 return( 0 );
92         }
93         free( attr );
94         ca.cma_purported.ava_type = type;
95
96         ca.cma_purported.ava_value = ldap_str2AttrV( value, type->oa_syntax );
97         free( value );
98         if ( ca.cma_purported.ava_value == NULLAttrV ) {
99                 Debug( LDAP_DEBUG_ANY, "str2AttrV failed\n", 0, 0, 0 );
100                 send_ldap_msgresult( clientsb, COMPTAG, m,
101                     LDAP_INVALID_SYNTAX, NULL, "" );
102                 return( 0 );
103         }
104
105         ca.cma_common = common; /* struct copy */
106
107         rc = initiate_dap_operation( OP_COMPARE, m, &ca );
108
109         dn_free( ca.cma_object );
110         AttrV_free( ca.cma_purported.ava_value );
111
112         if ( rc != 0 ) {
113                 send_ldap_msgresult( clientsb, COMPTAG, m, rc, NULL, "" );
114                 return( 0 );
115         }
116
117         return( 1 );
118 }
119
120 void
121 compare_result( 
122     Sockbuf                     *sb,
123     struct msg                  *m,
124     struct ds_compare_result    *cr
125 )
126 {
127         send_ldap_msgresult( sb, COMPTAG, m, cr->cmr_matched ?
128             LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE, NULL, "" );
129
130         return;
131 }