]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
Fix handling of non-critical controls for backends which
[openldap] / libraries / libldap / compare.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  compare.c
11  */
12
13 /* The compare request looks like this:
14  *      CompareRequest ::= SEQUENCE {
15  *              entry   DistinguishedName,
16  *              ava     SEQUENCE {
17  *                      type    AttributeType,
18  *                      value   AttributeValue
19  *              }
20  *      }
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "ldap-int.h"
32 #include "ldap_log.h"
33
34 /*
35  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
36  * of the entry to compare to and the attribute and value to compare (in
37  * attr and value) are supplied.  The msgid of the response is returned.
38  *
39  * Example:
40  *      struct berval bvalue = { "secret", sizeof("secret")-1 };
41  *      rc = ldap_compare( ld, "c=us@cn=bob",
42  *              "userPassword", &bvalue,
43  *              sctrl, cctrl, &msgid )
44  */
45 int
46 ldap_compare_ext(
47         LDAP *ld,
48         LDAP_CONST char *dn,
49         LDAP_CONST char *attr,
50         struct berval *bvalue,
51         LDAPControl **sctrls,
52         LDAPControl **cctrls,
53         int     *msgidp )
54 {
55         int rc;
56         BerElement      *ber;
57
58 #ifdef NEW_LOGGING
59         LDAP_LOG ( OPERATION, ENTRY, "ldap_compare\n", 0, 0, 0 );
60 #else
61         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
62 #endif
63
64         assert( ld != NULL );
65         assert( LDAP_VALID( ld ) );
66         assert( dn != NULL );
67         assert( attr != NULL );
68         assert( msgidp != NULL );
69
70         /* check client controls */
71         rc = ldap_int_client_controls( ld, cctrls );
72         if( rc != LDAP_SUCCESS ) return rc;
73
74         /* create a message to send */
75         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
76                 return( LDAP_NO_MEMORY );
77         }
78
79         if ( ber_printf( ber, "{it{s{sON}N}", /* '}' */
80                 ++ld->ld_msgid,
81                 LDAP_REQ_COMPARE, dn, attr, bvalue ) == -1 )
82         {
83                 ld->ld_errno = LDAP_ENCODING_ERROR;
84                 ber_free( ber, 1 );
85                 return( ld->ld_errno );
86         }
87
88         /* Put Server Controls */
89         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
90                 ber_free( ber, 1 );
91                 return ld->ld_errno;
92         }
93
94         if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
95                 ld->ld_errno = LDAP_ENCODING_ERROR;
96                 ber_free( ber, 1 );
97                 return( ld->ld_errno );
98         }
99
100 #ifndef LDAP_NOCACHE
101         if ( ld->ld_cache != NULL ) {
102                 if ( ldap_check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) {
103                         ber_free( ber, 1 );
104                         ld->ld_errno = LDAP_SUCCESS;
105                         *msgidp = ld->ld_msgid;
106                         return( ld->ld_errno );
107                 }
108                 ldap_add_request_to_cache( ld, LDAP_REQ_COMPARE, ber );
109         }
110 #endif /* LDAP_NOCACHE */
111
112         /* send the message */
113         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber );
114         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
115 }
116
117 /*
118  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
119  * of the entry to compare to and the attribute and value to compare (in
120  * attr and value) are supplied.  The msgid of the response is returned.
121  *
122  * Example:
123  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
124  */
125 int
126 ldap_compare(
127         LDAP *ld,
128         LDAP_CONST char *dn,
129         LDAP_CONST char *attr,
130         LDAP_CONST char *value )
131 {
132         int msgid;
133         struct berval bvalue;
134
135         assert( value != NULL );
136
137         bvalue.bv_val = (char *) value;
138         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
139
140         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
141                 ? msgid : -1;
142 }
143
144 int
145 ldap_compare_ext_s(
146         LDAP *ld,
147         LDAP_CONST char *dn,
148         LDAP_CONST char *attr,
149         struct berval *bvalue,
150         LDAPControl **sctrl,
151         LDAPControl **cctrl )
152 {
153         int             rc;
154         int             msgid;
155         LDAPMessage     *res;
156
157         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
158
159         if (  rc != LDAP_SUCCESS )
160                 return( rc );
161
162         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
163                 return( ld->ld_errno );
164
165         return( ldap_result2error( ld, res, 1 ) );
166 }
167
168 int
169 ldap_compare_s(
170         LDAP *ld,
171         LDAP_CONST char *dn,
172         LDAP_CONST char *attr,
173         LDAP_CONST char *value )
174 {
175         struct berval bvalue;
176
177         assert( value != NULL );
178
179         bvalue.bv_val = (char *) value;
180         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
181
182         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
183 }