Home | History | Annotate | Line # | Download | only in back-sql
compare.c revision 1.2
      1  1.2  christos /*	$NetBSD: compare.c,v 1.2 2020/08/11 13:15:42 christos Exp $	*/
      2  1.2  christos 
      3  1.2  christos /* $OpenLDAP$ */
      4  1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5  1.1     lukem  *
      6  1.2  christos  * Copyright 1999-2020 The OpenLDAP Foundation.
      7  1.1     lukem  * Portions Copyright 1999 Dmitry Kovalev.
      8  1.1     lukem  * Portions Copyright 2002 Pierangelo Masarati.
      9  1.1     lukem  * All rights reserved.
     10  1.1     lukem  *
     11  1.1     lukem  * Redistribution and use in source and binary forms, with or without
     12  1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     13  1.1     lukem  * Public License.
     14  1.1     lukem  *
     15  1.1     lukem  * A copy of this license is available in the file LICENSE in the
     16  1.1     lukem  * top-level directory of the distribution or, alternatively, at
     17  1.1     lukem  * <http://www.OpenLDAP.org/license.html>.
     18  1.1     lukem  */
     19  1.1     lukem /* ACKNOWLEDGEMENTS:
     20  1.1     lukem  * This work was initially developed by Dmitry Kovalev for inclusion
     21  1.1     lukem  * by OpenLDAP Software.  Additional significant contributors include
     22  1.1     lukem  * Pierangelo Masarati.
     23  1.1     lukem  */
     24  1.1     lukem 
     25  1.2  christos #include <sys/cdefs.h>
     26  1.2  christos __RCSID("$NetBSD: compare.c,v 1.2 2020/08/11 13:15:42 christos Exp $");
     27  1.2  christos 
     28  1.1     lukem #include "portable.h"
     29  1.1     lukem 
     30  1.1     lukem #include <stdio.h>
     31  1.1     lukem #include <sys/types.h>
     32  1.1     lukem 
     33  1.1     lukem #include "slap.h"
     34  1.1     lukem #include "proto-sql.h"
     35  1.1     lukem 
     36  1.1     lukem int
     37  1.1     lukem backsql_compare( Operation *op, SlapReply *rs )
     38  1.1     lukem {
     39  1.1     lukem 	SQLHDBC			dbh = SQL_NULL_HDBC;
     40  1.1     lukem 	Entry			e = { 0 };
     41  1.1     lukem 	Attribute		*a = NULL;
     42  1.1     lukem 	backsql_srch_info	bsi = { 0 };
     43  1.1     lukem 	int			rc;
     44  1.1     lukem 	int			manageDSAit = get_manageDSAit( op );
     45  1.1     lukem 	AttributeName		anlist[2];
     46  1.1     lukem 
     47  1.1     lukem  	Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
     48  1.1     lukem 
     49  1.1     lukem 	rs->sr_err = backsql_get_db_conn( op, &dbh );
     50  1.1     lukem 	if ( rs->sr_err != LDAP_SUCCESS ) {
     51  1.1     lukem      		Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
     52  1.1     lukem 			"could not get connection handle - exiting\n",
     53  1.1     lukem 			0, 0, 0 );
     54  1.1     lukem 
     55  1.1     lukem 		rs->sr_text = ( rs->sr_err == LDAP_OTHER )
     56  1.1     lukem 			? "SQL-backend error" : NULL;
     57  1.1     lukem 		goto return_results;
     58  1.1     lukem 	}
     59  1.1     lukem 
     60  1.1     lukem 	anlist[ 0 ].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
     61  1.1     lukem 	anlist[ 0 ].an_desc = op->oq_compare.rs_ava->aa_desc;
     62  1.1     lukem 	BER_BVZERO( &anlist[ 1 ].an_name );
     63  1.1     lukem 
     64  1.1     lukem 	/*
     65  1.1     lukem 	 * Get the entry
     66  1.1     lukem 	 */
     67  1.1     lukem 	bsi.bsi_e = &e;
     68  1.1     lukem 	rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
     69  1.1     lukem 			(time_t)(-1), NULL, dbh, op, rs, anlist,
     70  1.1     lukem 			( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
     71  1.1     lukem 	switch ( rc ) {
     72  1.1     lukem 	case LDAP_SUCCESS:
     73  1.1     lukem 		break;
     74  1.1     lukem 
     75  1.1     lukem 	case LDAP_REFERRAL:
     76  1.1     lukem 		if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
     77  1.1     lukem 				dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
     78  1.1     lukem 		{
     79  1.1     lukem 			rs->sr_err = LDAP_SUCCESS;
     80  1.1     lukem 			rs->sr_text = NULL;
     81  1.1     lukem 			rs->sr_matched = NULL;
     82  1.1     lukem 			if ( rs->sr_ref ) {
     83  1.1     lukem 				ber_bvarray_free( rs->sr_ref );
     84  1.1     lukem 				rs->sr_ref = NULL;
     85  1.1     lukem 			}
     86  1.1     lukem 			break;
     87  1.1     lukem 		}
     88  1.1     lukem 		/* fallthru */
     89  1.1     lukem 
     90  1.1     lukem 	default:
     91  1.1     lukem 		Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
     92  1.1     lukem 			"could not retrieve compareDN ID - no such entry\n",
     93  1.1     lukem 			0, 0, 0 );
     94  1.1     lukem 		goto return_results;
     95  1.1     lukem 	}
     96  1.1     lukem 
     97  1.1     lukem 	if ( get_assert( op ) &&
     98  1.1     lukem 			( test_filter( op, &e, get_assertion( op ) )
     99  1.1     lukem 			  != LDAP_COMPARE_TRUE ) )
    100  1.1     lukem 	{
    101  1.1     lukem 		rs->sr_err = LDAP_ASSERTION_FAILED;
    102  1.1     lukem 		goto return_results;
    103  1.1     lukem 	}
    104  1.1     lukem 
    105  1.1     lukem 	if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
    106  1.2  christos 		SlapReply	nrs = { REP_SEARCH };
    107  1.1     lukem 		Attribute	**ap;
    108  1.1     lukem 
    109  1.1     lukem 		for ( ap = &e.e_attrs; *ap; ap = &(*ap)->a_next )
    110  1.1     lukem 			;
    111  1.1     lukem 
    112  1.1     lukem 		nrs.sr_attrs = anlist;
    113  1.1     lukem 		nrs.sr_entry = &e;
    114  1.1     lukem 		nrs.sr_attr_flags = SLAP_OPATTRS_NO;
    115  1.1     lukem 		nrs.sr_operational_attrs = NULL;
    116  1.1     lukem 
    117  1.1     lukem 		rs->sr_err = backsql_operational( op, &nrs );
    118  1.1     lukem 		if ( rs->sr_err != LDAP_SUCCESS ) {
    119  1.1     lukem 			goto return_results;
    120  1.1     lukem 		}
    121  1.1     lukem 
    122  1.1     lukem 		*ap = nrs.sr_operational_attrs;
    123  1.1     lukem 	}
    124  1.1     lukem 
    125  1.1     lukem 	if ( ! access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
    126  1.1     lukem 				&op->oq_compare.rs_ava->aa_value,
    127  1.1     lukem 				ACL_COMPARE, NULL ) )
    128  1.1     lukem 	{
    129  1.1     lukem 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    130  1.1     lukem 		goto return_results;
    131  1.1     lukem 	}
    132  1.1     lukem 
    133  1.1     lukem 	rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
    134  1.1     lukem 	for ( a = attrs_find( e.e_attrs, op->oq_compare.rs_ava->aa_desc );
    135  1.1     lukem 			a != NULL;
    136  1.1     lukem 			a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
    137  1.1     lukem 	{
    138  1.1     lukem 		rs->sr_err = LDAP_COMPARE_FALSE;
    139  1.1     lukem 		if ( attr_valfind( a,
    140  1.1     lukem 					SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
    141  1.1     lukem 					SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
    142  1.1     lukem 					&op->oq_compare.rs_ava->aa_value, NULL,
    143  1.1     lukem 					op->o_tmpmemctx ) == 0 )
    144  1.1     lukem 		{
    145  1.1     lukem 			rs->sr_err = LDAP_COMPARE_TRUE;
    146  1.1     lukem 			break;
    147  1.1     lukem 		}
    148  1.1     lukem 	}
    149  1.1     lukem 
    150  1.1     lukem return_results:;
    151  1.1     lukem 	switch ( rs->sr_err ) {
    152  1.1     lukem 	case LDAP_COMPARE_TRUE:
    153  1.1     lukem 	case LDAP_COMPARE_FALSE:
    154  1.1     lukem 		break;
    155  1.1     lukem 
    156  1.1     lukem 	default:
    157  1.1     lukem 		if ( !BER_BVISNULL( &e.e_nname ) &&
    158  1.1     lukem 				! access_allowed( op, &e,
    159  1.1     lukem 					slap_schema.si_ad_entry, NULL,
    160  1.1     lukem 					ACL_DISCLOSE, NULL ) )
    161  1.1     lukem 		{
    162  1.1     lukem 			rs->sr_err = LDAP_NO_SUCH_OBJECT;
    163  1.1     lukem 			rs->sr_text = NULL;
    164  1.1     lukem 		}
    165  1.1     lukem 		break;
    166  1.1     lukem 	}
    167  1.1     lukem 
    168  1.1     lukem 	send_ldap_result( op, rs );
    169  1.1     lukem 
    170  1.1     lukem 	if ( rs->sr_matched ) {
    171  1.1     lukem 		rs->sr_matched = NULL;
    172  1.1     lukem 	}
    173  1.1     lukem 
    174  1.1     lukem 	if ( rs->sr_ref ) {
    175  1.1     lukem 		ber_bvarray_free( rs->sr_ref );
    176  1.1     lukem 		rs->sr_ref = NULL;
    177  1.1     lukem 	}
    178  1.1     lukem 
    179  1.1     lukem 	if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
    180  1.1     lukem 		(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
    181  1.1     lukem 	}
    182  1.1     lukem 
    183  1.1     lukem 	if ( !BER_BVISNULL( &e.e_nname ) ) {
    184  1.1     lukem 		backsql_entry_clean( op, &e );
    185  1.1     lukem 	}
    186  1.1     lukem 
    187  1.1     lukem 	if ( bsi.bsi_attrs != NULL ) {
    188  1.1     lukem 		op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
    189  1.1     lukem 	}
    190  1.1     lukem 
    191  1.1     lukem 	Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
    192  1.1     lukem 	switch ( rs->sr_err ) {
    193  1.1     lukem 	case LDAP_COMPARE_TRUE:
    194  1.1     lukem 	case LDAP_COMPARE_FALSE:
    195  1.1     lukem 		return LDAP_SUCCESS;
    196  1.1     lukem 
    197  1.1     lukem 	default:
    198  1.1     lukem 		return rs->sr_err;
    199  1.1     lukem 	}
    200  1.1     lukem }
    201  1.1     lukem 
    202