2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 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.
23 #include <ac/socket.h>
24 #include <ac/string.h>
30 /* The compare request looks like this:
31 * CompareRequest ::= SEQUENCE {
32 * entry DistinguishedName,
35 * value AttributeValue
41 * ldap_compare_ext - perform an ldap extended compare operation. The dn
42 * of the entry to compare to and the attribute and value to compare (in
43 * attr and value) are supplied. The msgid of the response is returned.
46 * struct berval bvalue = { "secret", sizeof("secret")-1 };
47 * rc = ldap_compare( ld, "c=us@cn=bob",
48 * "userPassword", &bvalue,
49 * sctrl, cctrl, &msgid )
55 LDAP_CONST char *attr,
56 struct berval *bvalue,
65 Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
68 assert( LDAP_VALID( ld ) );
70 assert( attr != NULL );
71 assert( msgidp != NULL );
73 /* check client controls */
74 rc = ldap_int_client_controls( ld, cctrls );
75 if( rc != LDAP_SUCCESS ) return rc;
77 /* create a message to send */
78 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
79 return( LDAP_NO_MEMORY );
82 LDAP_NEXT_MSGID(ld, id);
83 rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
85 LDAP_REQ_COMPARE, dn, attr, bvalue );
88 ld->ld_errno = LDAP_ENCODING_ERROR;
90 return( ld->ld_errno );
93 /* Put Server Controls */
94 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
99 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
100 ld->ld_errno = LDAP_ENCODING_ERROR;
102 return( ld->ld_errno );
106 /* send the message */
107 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
108 return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
112 * ldap_compare_ext - perform an ldap extended compare operation. The dn
113 * of the entry to compare to and the attribute and value to compare (in
114 * attr and value) are supplied. The msgid of the response is returned.
117 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
123 LDAP_CONST char *attr,
124 LDAP_CONST char *value )
127 struct berval bvalue;
129 assert( value != NULL );
131 bvalue.bv_val = (char *) value;
132 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
134 return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
142 LDAP_CONST char *attr,
143 struct berval *bvalue,
145 LDAPControl **cctrl )
151 rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
153 if ( rc != LDAP_SUCCESS )
156 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
157 return( ld->ld_errno );
159 return( ldap_result2error( ld, res, 1 ) );
166 LDAP_CONST char *attr,
167 LDAP_CONST char *value )
169 struct berval bvalue;
171 assert( value != NULL );
173 bvalue.bv_val = (char *) value;
174 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
176 return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );