Back to index

openldap  2.4.31
compare.cpp
Go to the documentation of this file.
00001 /* compare.cpp - ndb backend compare routine */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2008-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Howard Chu for inclusion
00018  * in OpenLDAP Software. This work was sponsored by MySQL.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 #include <ac/string.h>
00025 
00026 #include "back-ndb.h"
00027 
00028 int
00029 ndb_back_compare( Operation *op, SlapReply *rs )
00030 {
00031        struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
00032        Entry         e = {0};
00033        Attribute     *a;
00034        int           manageDSAit = get_manageDSAit( op );
00035 
00036        NdbArgs NA;
00037        NdbRdns rdns;
00038        struct berval matched;
00039 
00040        /* Get our NDB handle */
00041        rs->sr_err = ndb_thread_handle( op, &NA.ndb );
00042 
00043        rdns.nr_num = 0;
00044        NA.rdns = &rdns;
00045        e.e_name = op->o_req_dn;
00046        e.e_nname = op->o_req_ndn;
00047        NA.e = &e;
00048 
00049 dn2entry_retry:
00050        NA.txn = NA.ndb->startTransaction();
00051        rs->sr_text = NULL;
00052        if( !NA.txn ) {
00053               Debug( LDAP_DEBUG_TRACE,
00054                      LDAP_XSTRING(ndb_compare) ": startTransaction failed: %s (%d)\n",
00055                      NA.ndb->getNdbError().message, NA.ndb->getNdbError().code, 0 );
00056               rs->sr_err = LDAP_OTHER;
00057               rs->sr_text = "internal error";
00058               goto return_results;
00059        }
00060 
00061        NA.ocs = NULL;
00062        /* get entry */
00063        rs->sr_err = ndb_entry_get_info( op, &NA, 0, &matched );
00064        switch( rs->sr_err ) {
00065        case 0:
00066               break;
00067        case LDAP_NO_SUCH_OBJECT:
00068               rs->sr_matched = matched.bv_val;
00069               if ( NA.ocs )
00070                      ndb_check_referral( op, rs, &NA );
00071               goto return_results;
00072        case LDAP_BUSY:
00073               rs->sr_text = "ldap server busy";
00074               goto return_results;
00075 #if 0
00076        case DB_LOCK_DEADLOCK:
00077        case DB_LOCK_NOTGRANTED:
00078               goto dn2entry_retry;
00079 #endif
00080        default:
00081               rs->sr_err = LDAP_OTHER;
00082               rs->sr_text = "internal error";
00083               goto return_results;
00084        }
00085 
00086        rs->sr_err = ndb_entry_get_data( op, &NA, 0 );
00087        ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
00088        if (!manageDSAit && is_entry_referral( &e ) ) {
00089               /* return referral only if "disclose" is granted on the object */
00090               if ( !access_allowed( op, &e, slap_schema.si_ad_entry,
00091                      NULL, ACL_DISCLOSE, NULL ) )
00092               {
00093                      rs->sr_err = LDAP_NO_SUCH_OBJECT;
00094               } else {
00095                      /* entry is a referral, don't allow compare */
00096                      rs->sr_ref = get_entry_referrals( op, &e );
00097                      rs->sr_err = LDAP_REFERRAL;
00098                      rs->sr_matched = e.e_name.bv_val;
00099                      rs->sr_flags |= REP_REF_MUSTBEFREED;
00100               }
00101 
00102               Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0, 0, 0 );
00103               goto return_results;
00104        }
00105 
00106        if ( get_assert( op ) &&
00107               ( test_filter( op, &e, (Filter *)get_assertion( op )) != LDAP_COMPARE_TRUE ))
00108        {
00109               if ( !access_allowed( op, &e, slap_schema.si_ad_entry,
00110                      NULL, ACL_DISCLOSE, NULL ) )
00111               {
00112                      rs->sr_err = LDAP_NO_SUCH_OBJECT;
00113               } else {
00114                      rs->sr_err = LDAP_ASSERTION_FAILED;
00115               }
00116               goto return_results;
00117        }
00118 
00119        if ( !access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
00120               &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL ) )
00121        {
00122               /* return error only if "disclose"
00123                * is granted on the object */
00124               if ( !access_allowed( op, &e, slap_schema.si_ad_entry,
00125                                    NULL, ACL_DISCLOSE, NULL ) )
00126               {
00127                      rs->sr_err = LDAP_NO_SUCH_OBJECT;
00128               } else {
00129                      rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
00130               }
00131               goto return_results;
00132        }
00133 
00134        rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
00135 
00136        for ( a = attrs_find( e.e_attrs, op->oq_compare.rs_ava->aa_desc );
00137               a != NULL;
00138               a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
00139        {
00140               rs->sr_err = LDAP_COMPARE_FALSE;
00141 
00142               if ( attr_valfind( a,
00143                      SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
00144                             SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
00145                      &op->oq_compare.rs_ava->aa_value, NULL,
00146                      op->o_tmpmemctx ) == 0 )
00147               {
00148                      rs->sr_err = LDAP_COMPARE_TRUE;
00149                      break;
00150               }
00151        }
00152 
00153 return_results:
00154        NA.txn->close();
00155        if ( e.e_attrs ) {
00156               attrs_free( e.e_attrs );
00157               e.e_attrs = NULL;
00158        }
00159        send_ldap_result( op, rs );
00160 
00161        switch ( rs->sr_err ) {
00162        case LDAP_COMPARE_FALSE:
00163        case LDAP_COMPARE_TRUE:
00164               rs->sr_err = LDAP_SUCCESS;
00165               break;
00166        }
00167 
00168        return rs->sr_err;
00169 }