17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
37c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
47c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
57c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
87c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
97c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
107c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
137c478bd9Sstevel@tonic-gate  * March 31, 1998.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
167c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
177c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
187c478bd9Sstevel@tonic-gate  * Rights Reserved.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * Contributor(s):
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
247c478bd9Sstevel@tonic-gate  *  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *  compare.c
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #if 0
31*1da57d55SToomas Soome #ifndef lint
327c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "ldap-int.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * ldap_compare - perform an ldap compare operation.  The dn
407c478bd9Sstevel@tonic-gate  * of the entry to compare to and the attribute and value to compare (in
417c478bd9Sstevel@tonic-gate  * attr and value) are supplied.  The msgid of the response is returned.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Example:
447c478bd9Sstevel@tonic-gate  *	ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate int
477c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_compare(LDAP * ld,const char * dn,const char * attr,const char * value)487c478bd9Sstevel@tonic-gate ldap_compare( LDAP *ld, const char *dn, const char *attr, const char *value )
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	int		msgid;
517c478bd9Sstevel@tonic-gate 	struct berval	bv;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	bv.bv_val = (char *)value;
567c478bd9Sstevel@tonic-gate 	bv.bv_len = ( value == NULL ) ? 0 : strlen( value );
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if ( ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &msgid )
597c478bd9Sstevel@tonic-gate 	    == LDAP_SUCCESS ) {
607c478bd9Sstevel@tonic-gate 		return( msgid );
617c478bd9Sstevel@tonic-gate 	} else {
627c478bd9Sstevel@tonic-gate 		return( -1 );	/* error is in ld handle */
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int
677c478bd9Sstevel@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)687c478bd9Sstevel@tonic-gate ldap_compare_ext( LDAP *ld, const char *dn, const char *attr,
697c478bd9Sstevel@tonic-gate     const struct berval *bvalue, LDAPControl **serverctrls,
707c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls, int *msgidp )
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	BerElement	*ber;
73*1da57d55SToomas Soome 	int		rc, lderr;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/* The compare request looks like this:
767c478bd9Sstevel@tonic-gate 	 *	CompareRequest ::= SEQUENCE {
777c478bd9Sstevel@tonic-gate 	 *		entry	DistinguishedName,
787c478bd9Sstevel@tonic-gate 	 *		ava	SEQUENCE {
797c478bd9Sstevel@tonic-gate 	 *			type	AttributeType,
807c478bd9Sstevel@tonic-gate 	 *			value	AttributeValue
817c478bd9Sstevel@tonic-gate 	 *		}
827c478bd9Sstevel@tonic-gate 	 *	}
837c478bd9Sstevel@tonic-gate 	 * and must be wrapped in an LDAPMessage.
847c478bd9Sstevel@tonic-gate 	 */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_compare_ext\n", 0, 0, 0 );
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
897c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	if ( attr == NULL || bvalue == NULL || bvalue->bv_len == 0
927c478bd9Sstevel@tonic-gate 	    || msgidp == NULL ) {
937c478bd9Sstevel@tonic-gate 		lderr = LDAP_PARAM_ERROR;
947c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
957c478bd9Sstevel@tonic-gate 		return( lderr );
967c478bd9Sstevel@tonic-gate 	}
97*1da57d55SToomas Soome 
987c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
997c478bd9Sstevel@tonic-gate 		dn = "";
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1037c478bd9Sstevel@tonic-gate 	*msgidp = ++ld->ld_msgid;
1047c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/* check the cache */
1077c478bd9Sstevel@tonic-gate 	if ( ld->ld_cache_on && ld->ld_cache_compare != NULL ) {
1087c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1097c478bd9Sstevel@tonic-gate 		if ( (rc = (ld->ld_cache_compare)( ld, *msgidp,
1107c478bd9Sstevel@tonic-gate 		    LDAP_REQ_COMPARE, dn, attr, bvalue )) != 0 ) {
1117c478bd9Sstevel@tonic-gate 			*msgidp = rc;
1127c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1137c478bd9Sstevel@tonic-gate 			return( LDAP_SUCCESS );
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* create a message to send */
1197c478bd9Sstevel@tonic-gate 	if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
1207c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
1217c478bd9Sstevel@tonic-gate 		return( lderr );
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{s{so}}", *msgidp, LDAP_REQ_COMPARE, dn,
1257c478bd9Sstevel@tonic-gate 	    attr, bvalue->bv_val, (int)bvalue->bv_len /* XXX lossy cast */ )
1267c478bd9Sstevel@tonic-gate 	    == -1 ) {
1277c478bd9Sstevel@tonic-gate 		lderr = LDAP_ENCODING_ERROR;
1287c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1297c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
1307c478bd9Sstevel@tonic-gate 		return( lderr );
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
1347c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
1357c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
1367c478bd9Sstevel@tonic-gate 		return( lderr );
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* send the message */
1407c478bd9Sstevel@tonic-gate 	rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_COMPARE,
1417c478bd9Sstevel@tonic-gate 		(char *)dn, ber );
1427c478bd9Sstevel@tonic-gate 	*msgidp = rc;
1437c478bd9Sstevel@tonic-gate 	return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate int
1477c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_compare_s(LDAP * ld,const char * dn,const char * attr,const char * value)1487c478bd9Sstevel@tonic-gate ldap_compare_s( LDAP *ld, const char *dn, const char *attr,
1497c478bd9Sstevel@tonic-gate     const char *value )
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	struct berval	bv;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	bv.bv_val = (char *)value;
1547c478bd9Sstevel@tonic-gate 	bv.bv_len = ( value == NULL ) ? 0 : strlen( value );
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	return( ldap_compare_ext_s( ld, dn, attr, &bv, NULL, NULL ));
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
1607c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_compare_ext_s(LDAP * ld,const char * dn,const char * attr,const struct berval * bvalue,LDAPControl ** serverctrls,LDAPControl ** clientctrls)1617c478bd9Sstevel@tonic-gate ldap_compare_ext_s( LDAP *ld, const char *dn, const char *attr,
1627c478bd9Sstevel@tonic-gate     const struct berval *bvalue, LDAPControl **serverctrls,
163*1da57d55SToomas Soome     LDAPControl **clientctrls )
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	int		err, msgid;
1667c478bd9Sstevel@tonic-gate 	LDAPMessage	*res;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (( err = ldap_compare_ext( ld, dn, attr, bvalue, serverctrls,
1697c478bd9Sstevel@tonic-gate 	    clientctrls, &msgid )) != LDAP_SUCCESS ) {
1707c478bd9Sstevel@tonic-gate 		return( err );
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res )
1747c478bd9Sstevel@tonic-gate 	    == -1 ) {
1757c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
1797c478bd9Sstevel@tonic-gate }
180