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 1999-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1999 Dmitry Kovalev.
00006  * Portions Copyright 2002 Pierangelo Masarati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Dmitry Kovalev for inclusion
00019  * by OpenLDAP Software.  Additional significant contributors include
00020  * Pierangelo Masarati.
00021  */
00022 
00023 #include "portable.h"
00024 
00025 #include <stdio.h>
00026 #include <sys/types.h>
00027 
00028 #include "slap.h"
00029 #include "proto-sql.h"
00030 
00031 int
00032 backsql_compare( Operation *op, SlapReply *rs )
00033 {
00034        SQLHDBC                     dbh = SQL_NULL_HDBC;
00035        Entry                e = { 0 };
00036        Attribute            *a = NULL;
00037        backsql_srch_info    bsi = { 0 };
00038        int                  rc;
00039        int                  manageDSAit = get_manageDSAit( op );
00040        AttributeName        anlist[2];
00041 
00042        Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
00043 
00044        rs->sr_err = backsql_get_db_conn( op, &dbh );
00045        if ( rs->sr_err != LDAP_SUCCESS ) {
00046               Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
00047                      "could not get connection handle - exiting\n",
00048                      0, 0, 0 );
00049 
00050               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
00051                      ? "SQL-backend error" : NULL;
00052               goto return_results;
00053        }
00054 
00055        anlist[ 0 ].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
00056        anlist[ 0 ].an_desc = op->oq_compare.rs_ava->aa_desc;
00057        BER_BVZERO( &anlist[ 1 ].an_name );
00058 
00059        /*
00060         * Get the entry
00061         */
00062        bsi.bsi_e = &e;
00063        rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
00064                      (time_t)(-1), NULL, dbh, op, rs, anlist,
00065                      ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
00066        switch ( rc ) {
00067        case LDAP_SUCCESS:
00068               break;
00069 
00070        case LDAP_REFERRAL:
00071               if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
00072                             dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
00073               {
00074                      rs->sr_err = LDAP_SUCCESS;
00075                      rs->sr_text = NULL;
00076                      rs->sr_matched = NULL;
00077                      if ( rs->sr_ref ) {
00078                             ber_bvarray_free( rs->sr_ref );
00079                             rs->sr_ref = NULL;
00080                      }
00081                      break;
00082               }
00083               /* fallthru */
00084 
00085        default:
00086               Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
00087                      "could not retrieve compareDN ID - no such entry\n", 
00088                      0, 0, 0 );
00089               goto return_results;
00090        }
00091 
00092        if ( get_assert( op ) &&
00093                      ( test_filter( op, &e, get_assertion( op ) )
00094                        != LDAP_COMPARE_TRUE ) )
00095        {
00096               rs->sr_err = LDAP_ASSERTION_FAILED;
00097               goto return_results;
00098        }
00099 
00100        if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
00101               SlapReply     nrs = { REP_SEARCH };
00102               Attribute     **ap;
00103 
00104               for ( ap = &e.e_attrs; *ap; ap = &(*ap)->a_next )
00105                      ;
00106 
00107               nrs.sr_attrs = anlist;
00108               nrs.sr_entry = &e;
00109               nrs.sr_attr_flags = SLAP_OPATTRS_NO;
00110               nrs.sr_operational_attrs = NULL;
00111 
00112               rs->sr_err = backsql_operational( op, &nrs );
00113               if ( rs->sr_err != LDAP_SUCCESS ) {
00114                      goto return_results;
00115               }
00116               
00117               *ap = nrs.sr_operational_attrs;
00118        }
00119 
00120        if ( ! access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
00121                             &op->oq_compare.rs_ava->aa_value,
00122                             ACL_COMPARE, NULL ) )
00123        {
00124               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
00125               goto return_results;
00126        }
00127 
00128        rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
00129        for ( a = attrs_find( e.e_attrs, op->oq_compare.rs_ava->aa_desc );
00130                      a != NULL;
00131                      a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
00132        {
00133               rs->sr_err = LDAP_COMPARE_FALSE;
00134               if ( attr_valfind( a,
00135                                    SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
00136                                    SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
00137                                    &op->oq_compare.rs_ava->aa_value, NULL,
00138                                    op->o_tmpmemctx ) == 0 )
00139               {
00140                      rs->sr_err = LDAP_COMPARE_TRUE;
00141                      break;
00142               }
00143        }
00144 
00145 return_results:;
00146        switch ( rs->sr_err ) {
00147        case LDAP_COMPARE_TRUE:
00148        case LDAP_COMPARE_FALSE:
00149               break;
00150 
00151        default:
00152               if ( !BER_BVISNULL( &e.e_nname ) &&
00153                             ! access_allowed( op, &e,
00154                                    slap_schema.si_ad_entry, NULL,
00155                                    ACL_DISCLOSE, NULL ) )
00156               {
00157                      rs->sr_err = LDAP_NO_SUCH_OBJECT;
00158                      rs->sr_text = NULL;
00159               }
00160               break;
00161        }
00162 
00163        send_ldap_result( op, rs );
00164 
00165        if ( rs->sr_matched ) {
00166               rs->sr_matched = NULL;
00167        }
00168 
00169        if ( rs->sr_ref ) {
00170               ber_bvarray_free( rs->sr_ref );
00171               rs->sr_ref = NULL;
00172        }
00173 
00174        if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
00175               (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
00176        }
00177 
00178        if ( !BER_BVISNULL( &e.e_nname ) ) {
00179               backsql_entry_clean( op, &e );
00180        }
00181 
00182        if ( bsi.bsi_attrs != NULL ) {
00183               op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
00184        }
00185 
00186        Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
00187        switch ( rs->sr_err ) {
00188        case LDAP_COMPARE_TRUE:
00189        case LDAP_COMPARE_FALSE:
00190               return LDAP_SUCCESS;
00191 
00192        default:
00193               return rs->sr_err;
00194        }
00195 }
00196