2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2022 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_build_compare_req(
44 LDAP_CONST
char *attr
,
45 struct berval
*bvalue
,
53 /* create a message to send */
54 if ( (ber
= ldap_alloc_ber_with_options( ld
)) == NULL
) {
58 LDAP_NEXT_MSGID(ld
, *msgidp
);
59 rc
= ber_printf( ber
, "{it{s{sON}N}", /* '}' */
61 LDAP_REQ_COMPARE
, dn
, attr
, bvalue
);
64 ld
->ld_errno
= LDAP_ENCODING_ERROR
;
69 /* Put Server Controls */
70 if( ldap_int_put_controls( ld
, sctrls
, ber
) != LDAP_SUCCESS
) {
75 if( ber_printf( ber
, /*{*/ "N}" ) == -1 ) {
76 ld
->ld_errno
= LDAP_ENCODING_ERROR
;
85 * ldap_compare_ext - perform an ldap extended compare operation. The dn
86 * of the entry to compare to and the attribute and value to compare (in
87 * attr and value) are supplied. The msgid of the response is returned.
90 * struct berval bvalue = { "secret", sizeof("secret")-1 };
91 * rc = ldap_compare( ld, "c=us@cn=bob",
92 * "userPassword", &bvalue,
93 * sctrl, cctrl, &msgid )
99 LDAP_CONST
char *attr
,
100 struct berval
*bvalue
,
101 LDAPControl
**sctrls
,
102 LDAPControl
**cctrls
,
109 Debug0( LDAP_DEBUG_TRACE
, "ldap_compare\n" );
111 assert( ld
!= NULL
);
112 assert( LDAP_VALID( ld
) );
113 assert( dn
!= NULL
);
114 assert( attr
!= NULL
);
115 assert( msgidp
!= NULL
);
117 /* check client controls */
118 rc
= ldap_int_client_controls( ld
, cctrls
);
119 if( rc
!= LDAP_SUCCESS
) return rc
;
121 ber
= ldap_build_compare_req(
122 ld
, dn
, attr
, bvalue
, sctrls
, cctrls
, &id
);
126 /* send the message */
127 *msgidp
= ldap_send_initial_request( ld
, LDAP_REQ_COMPARE
, dn
, ber
, id
);
128 return ( *msgidp
< 0 ? ld
->ld_errno
: LDAP_SUCCESS
);
132 * ldap_compare_ext - perform an ldap extended compare operation. The dn
133 * of the entry to compare to and the attribute and value to compare (in
134 * attr and value) are supplied. The msgid of the response is returned.
137 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
143 LDAP_CONST
char *attr
,
144 LDAP_CONST
char *value
)
147 struct berval bvalue
;
149 assert( value
!= NULL
);
151 bvalue
.bv_val
= (char *) value
;
152 bvalue
.bv_len
= (value
== NULL
) ? 0 : strlen( value
);
154 return ldap_compare_ext( ld
, dn
, attr
, &bvalue
, NULL
, NULL
, &msgid
) == LDAP_SUCCESS
162 LDAP_CONST
char *attr
,
163 struct berval
*bvalue
,
165 LDAPControl
**cctrl
)
171 rc
= ldap_compare_ext( ld
, dn
, attr
, bvalue
, sctrl
, cctrl
, &msgid
);
173 if ( rc
!= LDAP_SUCCESS
)
176 if ( ldap_result( ld
, msgid
, LDAP_MSG_ALL
, (struct timeval
*) NULL
, &res
) == -1 || !res
)
177 return( ld
->ld_errno
);
179 return( ldap_result2error( ld
, res
, 1 ) );
186 LDAP_CONST
char *attr
,
187 LDAP_CONST
char *value
)
189 struct berval bvalue
;
191 assert( value
!= NULL
);
193 bvalue
.bv_val
= (char *) value
;
194 bvalue
.bv_len
= (value
== NULL
) ? 0 : strlen( value
);
196 return ldap_compare_ext_s( ld
, dn
, attr
, &bvalue
, NULL
, NULL
);