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