]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
ITS#889: quick fix
[openldap] / libraries / libldap / compare.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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
33 /*
34  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
35  * of the entry to compare to and the attribute and value to compare (in
36  * attr and value) are supplied.  The msgid of the response is returned.
37  *
38  * Example:
39  *      struct berval bvalue = { "secret", strlen("secret") };
40  *      rc = ldap_compare( ld, "c=us@cn=bob",
41  *              "userPassword", &bvalue,
42  *              sctrl, cctrl, &msgid )
43  */
44 int
45 ldap_compare_ext(
46         LDAP *ld,
47         LDAP_CONST char *dn,
48         LDAP_CONST char *attr,
49         struct berval *bvalue,
50         LDAPControl **sctrls,
51         LDAPControl **cctrls,
52         int     *msgidp )
53 {
54         BerElement      *ber;
55
56         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
57
58         assert( ld != NULL );
59         assert( LDAP_VALID( ld ) );
60         assert( dn != NULL );
61         assert( attr != NULL );
62         assert( msgidp != NULL );
63
64         /* create a message to send */
65         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
66                 return( LDAP_NO_MEMORY );
67         }
68
69         if ( ber_printf( ber, "{it{s{sON}N}", /* '}' */
70                 ++ld->ld_msgid,
71                 LDAP_REQ_COMPARE, dn, attr, bvalue ) == -1 )
72         {
73                 ld->ld_errno = LDAP_ENCODING_ERROR;
74                 ber_free( ber, 1 );
75                 return( ld->ld_errno );
76         }
77
78         /* Put Server Controls */
79         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
80                 ber_free( ber, 1 );
81                 return ld->ld_errno;
82         }
83
84         if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
85                 ld->ld_errno = LDAP_ENCODING_ERROR;
86                 ber_free( ber, 1 );
87                 return( ld->ld_errno );
88         }
89
90 #ifndef LDAP_NOCACHE
91         if ( ld->ld_cache != NULL ) {
92                 if ( ldap_check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) {
93                         ber_free( ber, 1 );
94                         ld->ld_errno = LDAP_SUCCESS;
95                         *msgidp = ld->ld_msgid;
96                         return( ld->ld_errno );
97                 }
98                 ldap_add_request_to_cache( ld, LDAP_REQ_COMPARE, ber );
99         }
100 #endif /* LDAP_NOCACHE */
101
102         /* send the message */
103         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber );
104         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
105 }
106
107 /*
108  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
109  * of the entry to compare to and the attribute and value to compare (in
110  * attr and value) are supplied.  The msgid of the response is returned.
111  *
112  * Example:
113  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
114  */
115 int
116 ldap_compare(
117         LDAP *ld,
118         LDAP_CONST char *dn,
119         LDAP_CONST char *attr,
120         LDAP_CONST char *value )
121 {
122         int msgid;
123         struct berval bvalue;
124
125         bvalue.bv_val = (char *) value;
126         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
127
128         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
129                 ? msgid : -1;
130 }
131
132 int
133 ldap_compare_ext_s(
134         LDAP *ld,
135         LDAP_CONST char *dn,
136         LDAP_CONST char *attr,
137         struct berval *bvalue,
138         LDAPControl **sctrl,
139         LDAPControl **cctrl )
140 {
141         int             rc;
142         int             msgid;
143         LDAPMessage     *res;
144
145         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
146
147         if (  rc != LDAP_SUCCESS )
148                 return( rc );
149
150         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
151                 return( ld->ld_errno );
152
153         return( ldap_result2error( ld, res, 1 ) );
154 }
155
156 int
157 ldap_compare_s(
158         LDAP *ld,
159         LDAP_CONST char *dn,
160         LDAP_CONST char *attr,
161         LDAP_CONST char *value )
162 {
163         struct berval bvalue;
164
165         bvalue.bv_val = (char *) value;
166         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
167
168         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
169 }