1*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate /*
4*0Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
5*0Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
6*0Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
7*0Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
10*0Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11*0Sstevel@tonic-gate * implied. See the License for the specific language governing
12*0Sstevel@tonic-gate * rights and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
15*0Sstevel@tonic-gate * March 31, 1998.
16*0Sstevel@tonic-gate *
17*0Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
18*0Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
19*0Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20*0Sstevel@tonic-gate * Rights Reserved.
21*0Sstevel@tonic-gate *
22*0Sstevel@tonic-gate * Contributor(s):
23*0Sstevel@tonic-gate */
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan.
26*0Sstevel@tonic-gate * All rights reserved.
27*0Sstevel@tonic-gate */
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate * compare.c
30*0Sstevel@tonic-gate */
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #if 0
33*0Sstevel@tonic-gate #ifndef lint
34*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
35*0Sstevel@tonic-gate #endif
36*0Sstevel@tonic-gate #endif
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #include "ldap-int.h"
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate * ldap_compare - perform an ldap compare operation. The dn
42*0Sstevel@tonic-gate * of the entry to compare to and the attribute and value to compare (in
43*0Sstevel@tonic-gate * attr and value) are supplied. The msgid of the response is returned.
44*0Sstevel@tonic-gate *
45*0Sstevel@tonic-gate * Example:
46*0Sstevel@tonic-gate * ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
47*0Sstevel@tonic-gate */
48*0Sstevel@tonic-gate int
49*0Sstevel@tonic-gate LDAP_CALL
ldap_compare(LDAP * ld,const char * dn,const char * attr,const char * value)50*0Sstevel@tonic-gate ldap_compare( LDAP *ld, const char *dn, const char *attr, const char *value )
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate int msgid;
53*0Sstevel@tonic-gate struct berval bv;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate bv.bv_val = (char *)value;
58*0Sstevel@tonic-gate bv.bv_len = ( value == NULL ) ? 0 : strlen( value );
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate if ( ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &msgid )
61*0Sstevel@tonic-gate == LDAP_SUCCESS ) {
62*0Sstevel@tonic-gate return( msgid );
63*0Sstevel@tonic-gate } else {
64*0Sstevel@tonic-gate return( -1 ); /* error is in ld handle */
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate int
69*0Sstevel@tonic-gate LDAP_CALL
ldap_compare_ext(LDAP * ld,const char * dn,const char * attr,const struct berval * bvalue,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)70*0Sstevel@tonic-gate ldap_compare_ext( LDAP *ld, const char *dn, const char *attr,
71*0Sstevel@tonic-gate const struct berval *bvalue, LDAPControl **serverctrls,
72*0Sstevel@tonic-gate LDAPControl **clientctrls, int *msgidp )
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate BerElement *ber;
75*0Sstevel@tonic-gate int rc, lderr;
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate /* The compare request looks like this:
78*0Sstevel@tonic-gate * CompareRequest ::= SEQUENCE {
79*0Sstevel@tonic-gate * entry DistinguishedName,
80*0Sstevel@tonic-gate * ava SEQUENCE {
81*0Sstevel@tonic-gate * type AttributeType,
82*0Sstevel@tonic-gate * value AttributeValue
83*0Sstevel@tonic-gate * }
84*0Sstevel@tonic-gate * }
85*0Sstevel@tonic-gate * and must be wrapped in an LDAPMessage.
86*0Sstevel@tonic-gate */
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_compare_ext\n", 0, 0, 0 );
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
91*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate if ( attr == NULL || bvalue == NULL || bvalue->bv_len == 0
94*0Sstevel@tonic-gate || msgidp == NULL ) {
95*0Sstevel@tonic-gate lderr = LDAP_PARAM_ERROR;
96*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
97*0Sstevel@tonic-gate return( lderr );
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate if ( dn == NULL ) {
101*0Sstevel@tonic-gate dn = "";
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
105*0Sstevel@tonic-gate *msgidp = ++ld->ld_msgid;
106*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate /* check the cache */
109*0Sstevel@tonic-gate if ( ld->ld_cache_on && ld->ld_cache_compare != NULL ) {
110*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
111*0Sstevel@tonic-gate if ( (rc = (ld->ld_cache_compare)( ld, *msgidp,
112*0Sstevel@tonic-gate LDAP_REQ_COMPARE, dn, attr, bvalue )) != 0 ) {
113*0Sstevel@tonic-gate *msgidp = rc;
114*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
115*0Sstevel@tonic-gate return( LDAP_SUCCESS );
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate /* create a message to send */
121*0Sstevel@tonic-gate if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
122*0Sstevel@tonic-gate != LDAP_SUCCESS ) {
123*0Sstevel@tonic-gate return( lderr );
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate if ( ber_printf( ber, "{it{s{so}}", *msgidp, LDAP_REQ_COMPARE, dn,
127*0Sstevel@tonic-gate attr, bvalue->bv_val, (int)bvalue->bv_len /* XXX lossy cast */ )
128*0Sstevel@tonic-gate == -1 ) {
129*0Sstevel@tonic-gate lderr = LDAP_ENCODING_ERROR;
130*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
131*0Sstevel@tonic-gate ber_free( ber, 1 );
132*0Sstevel@tonic-gate return( lderr );
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
136*0Sstevel@tonic-gate != LDAP_SUCCESS ) {
137*0Sstevel@tonic-gate ber_free( ber, 1 );
138*0Sstevel@tonic-gate return( lderr );
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate /* send the message */
142*0Sstevel@tonic-gate rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_COMPARE,
143*0Sstevel@tonic-gate (char *)dn, ber );
144*0Sstevel@tonic-gate *msgidp = rc;
145*0Sstevel@tonic-gate return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate int
149*0Sstevel@tonic-gate LDAP_CALL
ldap_compare_s(LDAP * ld,const char * dn,const char * attr,const char * value)150*0Sstevel@tonic-gate ldap_compare_s( LDAP *ld, const char *dn, const char *attr,
151*0Sstevel@tonic-gate const char *value )
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate struct berval bv;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate bv.bv_val = (char *)value;
156*0Sstevel@tonic-gate bv.bv_len = ( value == NULL ) ? 0 : strlen( value );
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate return( ldap_compare_ext_s( ld, dn, attr, &bv, NULL, NULL ));
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate int
162*0Sstevel@tonic-gate LDAP_CALL
ldap_compare_ext_s(LDAP * ld,const char * dn,const char * attr,const struct berval * bvalue,LDAPControl ** serverctrls,LDAPControl ** clientctrls)163*0Sstevel@tonic-gate ldap_compare_ext_s( LDAP *ld, const char *dn, const char *attr,
164*0Sstevel@tonic-gate const struct berval *bvalue, LDAPControl **serverctrls,
165*0Sstevel@tonic-gate LDAPControl **clientctrls )
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate int err, msgid;
168*0Sstevel@tonic-gate LDAPMessage *res;
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate if (( err = ldap_compare_ext( ld, dn, attr, bvalue, serverctrls,
171*0Sstevel@tonic-gate clientctrls, &msgid )) != LDAP_SUCCESS ) {
172*0Sstevel@tonic-gate return( err );
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res )
176*0Sstevel@tonic-gate == -1 ) {
177*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate return( ldap_result2error( ld, res, 1 ) );
181*0Sstevel@tonic-gate }
182