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.
34 static char copyright
[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
41 * ldap_compare - perform an ldap 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 * ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
50 ldap_compare( LDAP
*ld
, const char *dn
, const char *attr
, const char *value
)
55 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_compare\n", 0, 0, 0 );
57 bv
.bv_val
= (char *)value
;
58 bv
.bv_len
= ( value
== NULL
) ? 0 : strlen( value
);
60 if ( ldap_compare_ext( ld
, dn
, attr
, &bv
, NULL
, NULL
, &msgid
)
64 return( -1 ); /* error is in ld handle */
70 ldap_compare_ext( LDAP
*ld
, const char *dn
, const char *attr
,
71 const struct berval
*bvalue
, LDAPControl
**serverctrls
,
72 LDAPControl
**clientctrls
, int *msgidp
)
77 /* The compare request looks like this:
78 * CompareRequest ::= SEQUENCE {
79 * entry DistinguishedName,
82 * value AttributeValue
85 * and must be wrapped in an LDAPMessage.
88 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_compare_ext\n", 0, 0, 0 );
90 if ( !NSLDAPI_VALID_LDAP_POINTER( ld
)) {
91 return( LDAP_PARAM_ERROR
);
93 if ( attr
== NULL
|| bvalue
== NULL
|| bvalue
->bv_len
== 0
95 lderr
= LDAP_PARAM_ERROR
;
96 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
104 LDAP_MUTEX_LOCK( ld
, LDAP_MSGID_LOCK
);
105 *msgidp
= ++ld
->ld_msgid
;
106 LDAP_MUTEX_UNLOCK( ld
, LDAP_MSGID_LOCK
);
108 /* check the cache */
109 if ( ld
->ld_cache_on
&& ld
->ld_cache_compare
!= NULL
) {
110 LDAP_MUTEX_LOCK( ld
, LDAP_CACHE_LOCK
);
111 if ( (rc
= (ld
->ld_cache_compare
)( ld
, *msgidp
,
112 LDAP_REQ_COMPARE
, dn
, attr
, bvalue
)) != 0 ) {
114 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
115 return( LDAP_SUCCESS
);
117 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
120 /* create a message to send */
121 if (( lderr
= nsldapi_alloc_ber_with_options( ld
, &ber
))
126 if ( ber_printf( ber
, "{it{s{so}}", *msgidp
, LDAP_REQ_COMPARE
, dn
,
127 attr
, bvalue
->bv_val
, (int)bvalue
->bv_len
/* XXX lossy cast */ )
129 lderr
= LDAP_ENCODING_ERROR
;
130 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
135 if (( lderr
= nsldapi_put_controls( ld
, serverctrls
, 1, ber
))
141 /* send the message */
142 rc
= nsldapi_send_initial_request( ld
, *msgidp
, LDAP_REQ_COMPARE
,
145 return( rc
< 0 ? LDAP_GET_LDERRNO( ld
, NULL
, NULL
) : LDAP_SUCCESS
);
150 ldap_compare_s( LDAP
*ld
, const char *dn
, const char *attr
,
155 bv
.bv_val
= (char *)value
;
156 bv
.bv_len
= ( value
== NULL
) ? 0 : strlen( value
);
158 return( ldap_compare_ext_s( ld
, dn
, attr
, &bv
, NULL
, NULL
));
163 ldap_compare_ext_s( LDAP
*ld
, const char *dn
, const char *attr
,
164 const struct berval
*bvalue
, LDAPControl
**serverctrls
,
165 LDAPControl
**clientctrls
)
170 if (( err
= ldap_compare_ext( ld
, dn
, attr
, bvalue
, serverctrls
,
171 clientctrls
, &msgid
)) != LDAP_SUCCESS
) {
175 if ( ldap_result( ld
, msgid
, 1, (struct timeval
*)NULL
, &res
)
177 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
180 return( ldap_result2error( ld
, res
, 1 ) );