Back to index

openldap  2.4.31
compare.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00016  * All rights reserved.
00017  */
00018 
00019 #include "portable.h"
00020 
00021 #include <stdio.h>
00022 
00023 #include <ac/socket.h>
00024 #include <ac/string.h>
00025 #include <ac/time.h>
00026 
00027 #include "ldap-int.h"
00028 #include "ldap_log.h"
00029 
00030 /* The compare request looks like this:
00031  *     CompareRequest ::= SEQUENCE {
00032  *            entry  DistinguishedName,
00033  *            ava    SEQUENCE {
00034  *                   type   AttributeType,
00035  *                   value  AttributeValue
00036  *            }
00037  *     }
00038  */
00039 
00040 /*
00041  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
00042  * of the entry to compare to and the attribute and value to compare (in
00043  * attr and value) are supplied.  The msgid of the response is returned.
00044  *
00045  * Example:
00046  *     struct berval bvalue = { "secret", sizeof("secret")-1 };
00047  *     rc = ldap_compare( ld, "c=us@cn=bob",
00048  *            "userPassword", &bvalue,
00049  *            sctrl, cctrl, &msgid )
00050  */
00051 int
00052 ldap_compare_ext(
00053        LDAP *ld,
00054        LDAP_CONST char *dn,
00055        LDAP_CONST char *attr,
00056        struct berval *bvalue,
00057        LDAPControl **sctrls,
00058        LDAPControl **cctrls,
00059        int    *msgidp )
00060 {
00061        int rc;
00062        BerElement    *ber;
00063        ber_int_t     id;
00064 
00065        Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
00066 
00067        assert( ld != NULL );
00068        assert( LDAP_VALID( ld ) );
00069        assert( dn != NULL );
00070        assert( attr != NULL );
00071        assert( msgidp != NULL );
00072 
00073        /* check client controls */
00074        rc = ldap_int_client_controls( ld, cctrls );
00075        if( rc != LDAP_SUCCESS ) return rc;
00076 
00077        /* create a message to send */
00078        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
00079               return( LDAP_NO_MEMORY );
00080        }
00081 
00082        LDAP_NEXT_MSGID(ld, id);
00083        rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
00084               id,
00085               LDAP_REQ_COMPARE, dn, attr, bvalue );
00086        if ( rc == -1 )
00087        {
00088               ld->ld_errno = LDAP_ENCODING_ERROR;
00089               ber_free( ber, 1 );
00090               return( ld->ld_errno );
00091        }
00092 
00093        /* Put Server Controls */
00094        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
00095               ber_free( ber, 1 );
00096               return ld->ld_errno;
00097        }
00098 
00099        if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
00100               ld->ld_errno = LDAP_ENCODING_ERROR;
00101               ber_free( ber, 1 );
00102               return( ld->ld_errno );
00103        }
00104 
00105 
00106        /* send the message */
00107        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
00108        return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
00109 }
00110 
00111 /*
00112  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
00113  * of the entry to compare to and the attribute and value to compare (in
00114  * attr and value) are supplied.  The msgid of the response is returned.
00115  *
00116  * Example:
00117  *     msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
00118  */
00119 int
00120 ldap_compare(
00121        LDAP *ld,
00122        LDAP_CONST char *dn,
00123        LDAP_CONST char *attr,
00124        LDAP_CONST char *value )
00125 {
00126        int msgid;
00127        struct berval bvalue;
00128 
00129        assert( value != NULL );
00130 
00131        bvalue.bv_val = (char *) value;
00132        bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
00133 
00134        return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
00135               ? msgid : -1;
00136 }
00137 
00138 int
00139 ldap_compare_ext_s(
00140        LDAP *ld,
00141        LDAP_CONST char *dn,
00142        LDAP_CONST char *attr,
00143        struct berval *bvalue,
00144        LDAPControl **sctrl,
00145        LDAPControl **cctrl )
00146 {
00147        int           rc;
00148        int           msgid;
00149        LDAPMessage   *res;
00150 
00151        rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
00152 
00153        if (  rc != LDAP_SUCCESS )
00154               return( rc );
00155 
00156        if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
00157               return( ld->ld_errno );
00158 
00159        return( ldap_result2error( ld, res, 1 ) );
00160 }
00161 
00162 int
00163 ldap_compare_s(
00164        LDAP *ld,
00165        LDAP_CONST char *dn,
00166        LDAP_CONST char *attr,
00167        LDAP_CONST char *value )
00168 {
00169        struct berval bvalue;
00170 
00171        assert( value != NULL );
00172 
00173        bvalue.bv_val = (char *) value;
00174        bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
00175 
00176        return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
00177 }