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