Back to index

lightning-sunbird  0.9+nobinonly
compare.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1990 Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 /*
00042  *  compare.c
00043  */
00044 
00045 #if 0
00046 #ifndef lint 
00047 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
00048 #endif
00049 #endif
00050 
00051 #include "ldap-int.h"
00052 
00053 /*
00054  * ldap_compare - perform an ldap compare operation.  The dn
00055  * of the entry to compare to and the attribute and value to compare (in
00056  * attr and value) are supplied.  The msgid of the response is returned.
00057  *
00058  * Example:
00059  *     ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
00060  */
00061 int
00062 LDAP_CALL
00063 ldap_compare( LDAP *ld, const char *dn, const char *attr, const char *value )
00064 {
00065        int           msgid;
00066        struct berval bv;
00067 
00068        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
00069 
00070        bv.bv_val = (char *)value;
00071        bv.bv_len = ( value == NULL ) ? 0 : strlen( value );
00072 
00073        if ( ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &msgid )
00074            == LDAP_SUCCESS ) {
00075               return( msgid );
00076        } else {
00077               return( -1 ); /* error is in ld handle */
00078        }
00079 }
00080 
00081 int
00082 LDAP_CALL
00083 ldap_compare_ext( LDAP *ld, const char *dn, const char *attr,
00084     const struct berval *bvalue, LDAPControl **serverctrls,
00085     LDAPControl **clientctrls, int *msgidp )
00086 {
00087        BerElement    *ber;
00088        int           rc, lderr; 
00089 
00090        /* The compare request looks like this:
00091         *     CompareRequest ::= SEQUENCE {
00092         *            entry  DistinguishedName,
00093         *            ava    SEQUENCE {
00094         *                   type   AttributeType,
00095         *                   value  AttributeValue
00096         *            }
00097         *     }
00098         * and must be wrapped in an LDAPMessage.
00099         */
00100 
00101        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_compare_ext\n", 0, 0, 0 );
00102 
00103        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00104               return( LDAP_PARAM_ERROR );
00105        }
00106        if ( attr == NULL || bvalue == NULL || bvalue->bv_len == 0
00107            || msgidp == NULL ) {
00108               lderr = LDAP_PARAM_ERROR;
00109               LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00110               return( lderr );
00111        }
00112               
00113        if ( dn == NULL ) {
00114               dn = "";
00115        }
00116 
00117        LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
00118        *msgidp = ++ld->ld_msgid;
00119        LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
00120 
00121        /* check the cache */
00122        if ( ld->ld_cache_on && ld->ld_cache_compare != NULL ) {
00123               LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
00124               if ( (rc = (ld->ld_cache_compare)( ld, *msgidp,
00125                   LDAP_REQ_COMPARE, dn, attr, bvalue )) != 0 ) {
00126                      *msgidp = rc;
00127                      LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00128                      return( LDAP_SUCCESS );
00129               }
00130               LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00131        }
00132 
00133        /* create a message to send */
00134        if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
00135            != LDAP_SUCCESS ) {
00136               return( lderr );
00137        }
00138 
00139        if ( ber_printf( ber, "{it{s{so}}", *msgidp, LDAP_REQ_COMPARE, dn,
00140            attr, bvalue->bv_val, (int)bvalue->bv_len /* XXX lossy cast */ )
00141            == -1 ) {
00142               lderr = LDAP_ENCODING_ERROR;
00143               LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00144               ber_free( ber, 1 );
00145               return( lderr );
00146        }
00147 
00148        if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
00149            != LDAP_SUCCESS ) {
00150               ber_free( ber, 1 );
00151               return( lderr );
00152        }
00153 
00154        /* send the message */
00155        rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_COMPARE,
00156               (char *)dn, ber );
00157        *msgidp = rc;
00158        return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
00159 }
00160 
00161 int
00162 LDAP_CALL
00163 ldap_compare_s( LDAP *ld, const char *dn, const char *attr,
00164     const char *value )
00165 {
00166        struct berval bv;
00167 
00168        bv.bv_val = (char *)value;
00169        bv.bv_len = ( value == NULL ) ? 0 : strlen( value );
00170 
00171        return( ldap_compare_ext_s( ld, dn, attr, &bv, NULL, NULL ));
00172 }
00173 
00174 int
00175 LDAP_CALL
00176 ldap_compare_ext_s( LDAP *ld, const char *dn, const char *attr,
00177     const struct berval *bvalue, LDAPControl **serverctrls,
00178     LDAPControl **clientctrls ) 
00179 {
00180        int           err, msgid;
00181        LDAPMessage   *res;
00182 
00183        if (( err = ldap_compare_ext( ld, dn, attr, bvalue, serverctrls,
00184            clientctrls, &msgid )) != LDAP_SUCCESS ) {
00185               return( err );
00186        }
00187 
00188        if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res )
00189            == -1 ) {
00190               return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00191        }
00192 
00193        return( ldap_result2error( ld, res, 1 ) );
00194 }