3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
13 /* The compare request looks like this:
14 * CompareRequest ::= SEQUENCE {
15 * entry DistinguishedName,
18 * value AttributeValue
27 #include <ac/socket.h>
28 #include <ac/string.h>
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.
39 * struct berval bvalue = { "secret", sizeof("secret")-1 };
40 * rc = ldap_compare( ld, "c=us@cn=bob",
41 * "userPassword", &bvalue,
42 * sctrl, cctrl, &msgid )
48 LDAP_CONST char *attr,
49 struct berval *bvalue,
58 LDAP_LOG (( "compare", LDAP_LEVEL_ENTRY, "ldap_compare\n" ));
60 Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
64 assert( LDAP_VALID( ld ) );
66 assert( attr != NULL );
67 assert( msgidp != NULL );
69 /* check client controls */
70 rc = ldap_int_client_controls( ld, cctrls );
71 if( rc != LDAP_SUCCESS ) return rc;
73 /* create a message to send */
74 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
75 return( LDAP_NO_MEMORY );
78 if ( ber_printf( ber, "{it{s{sON}N}", /* '}' */
80 LDAP_REQ_COMPARE, dn, attr, bvalue ) == -1 )
82 ld->ld_errno = LDAP_ENCODING_ERROR;
84 return( ld->ld_errno );
87 /* Put Server Controls */
88 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
93 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
94 ld->ld_errno = LDAP_ENCODING_ERROR;
96 return( ld->ld_errno );
100 if ( ld->ld_cache != NULL ) {
101 if ( ldap_check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) {
103 ld->ld_errno = LDAP_SUCCESS;
104 *msgidp = ld->ld_msgid;
105 return( ld->ld_errno );
107 ldap_add_request_to_cache( ld, LDAP_REQ_COMPARE, ber );
109 #endif /* LDAP_NOCACHE */
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 );
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.
122 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
128 LDAP_CONST char *attr,
129 LDAP_CONST char *value )
132 struct berval bvalue;
134 assert( value != NULL );
136 bvalue.bv_val = (char *) value;
137 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
139 return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
147 LDAP_CONST char *attr,
148 struct berval *bvalue,
150 LDAPControl **cctrl )
156 rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
158 if ( rc != LDAP_SUCCESS )
161 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
162 return( ld->ld_errno );
164 return( ldap_result2error( ld, res, 1 ) );
171 LDAP_CONST char *attr,
172 LDAP_CONST char *value )
174 struct berval bvalue;
176 assert( value != NULL );
178 bvalue.bv_val = (char *) value;
179 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
181 return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );