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