2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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,
68 Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
71 assert( LDAP_VALID( ld ) );
73 assert( attr != NULL );
74 assert( msgidp != NULL );
76 /* check client controls */
77 rc = ldap_int_client_controls( ld, cctrls );
78 if( rc != LDAP_SUCCESS ) return rc;
80 /* create a message to send */
81 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
82 return( LDAP_NO_MEMORY );
85 LDAP_NEXT_MSGID(ld, id);
86 rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
88 LDAP_REQ_COMPARE, dn, attr, bvalue );
91 ld->ld_errno = LDAP_ENCODING_ERROR;
93 return( ld->ld_errno );
96 /* Put Server Controls */
97 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
102 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
103 ld->ld_errno = LDAP_ENCODING_ERROR;
105 return( ld->ld_errno );
109 /* send the message */
110 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
111 return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
115 * ldap_compare_ext - perform an ldap extended compare operation. The dn
116 * of the entry to compare to and the attribute and value to compare (in
117 * attr and value) are supplied. The msgid of the response is returned.
120 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
126 LDAP_CONST char *attr,
127 LDAP_CONST char *value )
130 struct berval bvalue;
132 assert( value != NULL );
134 bvalue.bv_val = (char *) value;
135 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
137 return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
145 LDAP_CONST char *attr,
146 struct berval *bvalue,
148 LDAPControl **cctrl )
154 rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
156 if ( rc != LDAP_SUCCESS )
159 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
160 return( ld->ld_errno );
162 return( ldap_result2error( ld, res, 1 ) );
169 LDAP_CONST char *attr,
170 LDAP_CONST char *value )
172 struct berval bvalue;
174 assert( value != NULL );
176 bvalue.bv_val = (char *) value;
177 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
179 return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );