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