2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
18 /* Portions Copyright (C) The Internet Society (1997)
19 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
22 /* The compare request looks like this:
23 * CompareRequest ::= SEQUENCE {
24 * entry DistinguishedName,
27 * value AttributeValue
36 #include <ac/socket.h>
37 #include <ac/string.h>
44 * ldap_compare_ext - perform an ldap extended compare operation. The dn
45 * of the entry to compare to and the attribute and value to compare (in
46 * attr and value) are supplied. The msgid of the response is returned.
49 * struct berval bvalue = { "secret", sizeof("secret")-1 };
50 * rc = ldap_compare( ld, "c=us@cn=bob",
51 * "userPassword", &bvalue,
52 * sctrl, cctrl, &msgid )
58 LDAP_CONST char *attr,
59 struct berval *bvalue,
69 LDAP_LOG ( OPERATION, ENTRY, "ldap_compare\n", 0, 0, 0 );
71 Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
75 assert( LDAP_VALID( ld ) );
77 assert( attr != NULL );
78 assert( msgidp != NULL );
80 /* check client controls */
81 rc = ldap_int_client_controls( ld, cctrls );
82 if( rc != LDAP_SUCCESS ) return rc;
84 /* create a message to send */
85 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
86 return( LDAP_NO_MEMORY );
89 LDAP_NEXT_MSGID(ld, id);
90 rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
92 LDAP_REQ_COMPARE, dn, attr, bvalue );
95 ld->ld_errno = LDAP_ENCODING_ERROR;
97 return( ld->ld_errno );
100 /* Put Server Controls */
101 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
106 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
107 ld->ld_errno = LDAP_ENCODING_ERROR;
109 return( ld->ld_errno );
113 /* send the message */
114 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
115 return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
119 * ldap_compare_ext - perform an ldap extended compare operation. The dn
120 * of the entry to compare to and the attribute and value to compare (in
121 * attr and value) are supplied. The msgid of the response is returned.
124 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
130 LDAP_CONST char *attr,
131 LDAP_CONST char *value )
134 struct berval bvalue;
136 assert( value != NULL );
138 bvalue.bv_val = (char *) value;
139 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
141 return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
149 LDAP_CONST char *attr,
150 struct berval *bvalue,
152 LDAPControl **cctrl )
158 rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
160 if ( rc != LDAP_SUCCESS )
163 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
164 return( ld->ld_errno );
166 return( ldap_result2error( ld, res, 1 ) );
173 LDAP_CONST char *attr,
174 LDAP_CONST char *value )
176 struct berval bvalue;
178 assert( value != NULL );
180 bvalue.bv_val = (char *) value;
181 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
183 return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );