1 #pragma ident "%Z%%M% %I% %E% SMI"
4 * The contents of this file are subject to the Netscape Public
5 * License Version 1.1 (the "License"); you may not use this file
6 * except in compliance with the License. You may obtain a copy of
7 * the License at http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS
10 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * rights and limitations under the License.
14 * The Original Code is Mozilla Communicator client code, released
17 * The Initial Developer of the Original Code is Netscape
18 * Communications Corporation. Portions created by Netscape are
19 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25 * Copyright (c) 1990 Regents of the University of Michigan.
26 * All rights reserved.
33 static char copyright
[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
39 * ldap_compare - perform an ldap compare operation. The dn
40 * of the entry to compare to and the attribute and value to compare (in
41 * attr and value) are supplied. The msgid of the response is returned.
44 * ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
48 ldap_compare( LDAP
*ld
, const char *dn
, const char *attr
, const char *value
)
53 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_compare\n", 0, 0, 0 );
55 bv
.bv_val
= (char *)value
;
56 bv
.bv_len
= ( value
== NULL
) ? 0 : strlen( value
);
58 if ( ldap_compare_ext( ld
, dn
, attr
, &bv
, NULL
, NULL
, &msgid
)
62 return( -1 ); /* error is in ld handle */
68 ldap_compare_ext( LDAP
*ld
, const char *dn
, const char *attr
,
69 const struct berval
*bvalue
, LDAPControl
**serverctrls
,
70 LDAPControl
**clientctrls
, int *msgidp
)
75 /* The compare request looks like this:
76 * CompareRequest ::= SEQUENCE {
77 * entry DistinguishedName,
80 * value AttributeValue
83 * and must be wrapped in an LDAPMessage.
86 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_compare_ext\n", 0, 0, 0 );
88 if ( !NSLDAPI_VALID_LDAP_POINTER( ld
)) {
89 return( LDAP_PARAM_ERROR
);
91 if ( attr
== NULL
|| bvalue
== NULL
|| bvalue
->bv_len
== 0
93 lderr
= LDAP_PARAM_ERROR
;
94 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
102 LDAP_MUTEX_LOCK( ld
, LDAP_MSGID_LOCK
);
103 *msgidp
= ++ld
->ld_msgid
;
104 LDAP_MUTEX_UNLOCK( ld
, LDAP_MSGID_LOCK
);
106 /* check the cache */
107 if ( ld
->ld_cache_on
&& ld
->ld_cache_compare
!= NULL
) {
108 LDAP_MUTEX_LOCK( ld
, LDAP_CACHE_LOCK
);
109 if ( (rc
= (ld
->ld_cache_compare
)( ld
, *msgidp
,
110 LDAP_REQ_COMPARE
, dn
, attr
, bvalue
)) != 0 ) {
112 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
113 return( LDAP_SUCCESS
);
115 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
118 /* create a message to send */
119 if (( lderr
= nsldapi_alloc_ber_with_options( ld
, &ber
))
124 if ( ber_printf( ber
, "{it{s{so}}", *msgidp
, LDAP_REQ_COMPARE
, dn
,
125 attr
, bvalue
->bv_val
, (int)bvalue
->bv_len
/* XXX lossy cast */ )
127 lderr
= LDAP_ENCODING_ERROR
;
128 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
133 if (( lderr
= nsldapi_put_controls( ld
, serverctrls
, 1, ber
))
139 /* send the message */
140 rc
= nsldapi_send_initial_request( ld
, *msgidp
, LDAP_REQ_COMPARE
,
143 return( rc
< 0 ? LDAP_GET_LDERRNO( ld
, NULL
, NULL
) : LDAP_SUCCESS
);
148 ldap_compare_s( LDAP
*ld
, const char *dn
, const char *attr
,
153 bv
.bv_val
= (char *)value
;
154 bv
.bv_len
= ( value
== NULL
) ? 0 : strlen( value
);
156 return( ldap_compare_ext_s( ld
, dn
, attr
, &bv
, NULL
, NULL
));
161 ldap_compare_ext_s( LDAP
*ld
, const char *dn
, const char *attr
,
162 const struct berval
*bvalue
, LDAPControl
**serverctrls
,
163 LDAPControl
**clientctrls
)
168 if (( err
= ldap_compare_ext( ld
, dn
, attr
, bvalue
, serverctrls
,
169 clientctrls
, &msgid
)) != LDAP_SUCCESS
) {
173 if ( ldap_result( ld
, msgid
, 1, NULL
, &res
)
175 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
178 return( ldap_result2error( ld
, res
, 1 ) );