1 1.3 christos /* $NetBSD: compare.c,v 1.4 2025/09/05 21:16:31 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.4 christos * Copyright 1999-2024 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.3 christos __RCSID("$NetBSD: compare.c,v 1.4 2025/09/05 21:16:31 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.3 christos Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n" ); 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.3 christos "could not get connection handle - exiting\n" ); 53 1.1 lukem 54 1.1 lukem rs->sr_text = ( rs->sr_err == LDAP_OTHER ) 55 1.1 lukem ? "SQL-backend error" : NULL; 56 1.1 lukem goto return_results; 57 1.1 lukem } 58 1.1 lukem 59 1.1 lukem anlist[ 0 ].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname; 60 1.1 lukem anlist[ 0 ].an_desc = op->oq_compare.rs_ava->aa_desc; 61 1.1 lukem BER_BVZERO( &anlist[ 1 ].an_name ); 62 1.1 lukem 63 1.1 lukem /* 64 1.1 lukem * Get the entry 65 1.1 lukem */ 66 1.1 lukem bsi.bsi_e = &e; 67 1.1 lukem rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE, 68 1.1 lukem (time_t)(-1), NULL, dbh, op, rs, anlist, 69 1.1 lukem ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) ); 70 1.1 lukem switch ( rc ) { 71 1.1 lukem case LDAP_SUCCESS: 72 1.1 lukem break; 73 1.1 lukem 74 1.1 lukem case LDAP_REFERRAL: 75 1.1 lukem if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) && 76 1.1 lukem dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) ) 77 1.1 lukem { 78 1.1 lukem rs->sr_err = LDAP_SUCCESS; 79 1.1 lukem rs->sr_text = NULL; 80 1.1 lukem rs->sr_matched = NULL; 81 1.1 lukem if ( rs->sr_ref ) { 82 1.1 lukem ber_bvarray_free( rs->sr_ref ); 83 1.1 lukem rs->sr_ref = NULL; 84 1.1 lukem } 85 1.1 lukem break; 86 1.1 lukem } 87 1.1 lukem /* fallthru */ 88 1.1 lukem 89 1.1 lukem default: 90 1.1 lukem Debug( LDAP_DEBUG_TRACE, "backsql_compare(): " 91 1.3 christos "could not retrieve compareDN ID - no such entry\n" ); 92 1.1 lukem goto return_results; 93 1.1 lukem } 94 1.1 lukem 95 1.1 lukem if ( get_assert( op ) && 96 1.1 lukem ( test_filter( op, &e, get_assertion( op ) ) 97 1.1 lukem != LDAP_COMPARE_TRUE ) ) 98 1.1 lukem { 99 1.1 lukem rs->sr_err = LDAP_ASSERTION_FAILED; 100 1.1 lukem goto return_results; 101 1.1 lukem } 102 1.1 lukem 103 1.1 lukem if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) { 104 1.2 christos SlapReply nrs = { REP_SEARCH }; 105 1.1 lukem Attribute **ap; 106 1.1 lukem 107 1.1 lukem for ( ap = &e.e_attrs; *ap; ap = &(*ap)->a_next ) 108 1.1 lukem ; 109 1.1 lukem 110 1.1 lukem nrs.sr_attrs = anlist; 111 1.1 lukem nrs.sr_entry = &e; 112 1.1 lukem nrs.sr_attr_flags = SLAP_OPATTRS_NO; 113 1.1 lukem nrs.sr_operational_attrs = NULL; 114 1.1 lukem 115 1.1 lukem rs->sr_err = backsql_operational( op, &nrs ); 116 1.1 lukem if ( rs->sr_err != LDAP_SUCCESS ) { 117 1.1 lukem goto return_results; 118 1.1 lukem } 119 1.1 lukem 120 1.1 lukem *ap = nrs.sr_operational_attrs; 121 1.1 lukem } 122 1.1 lukem 123 1.1 lukem if ( ! access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc, 124 1.1 lukem &op->oq_compare.rs_ava->aa_value, 125 1.1 lukem ACL_COMPARE, NULL ) ) 126 1.1 lukem { 127 1.1 lukem rs->sr_err = LDAP_INSUFFICIENT_ACCESS; 128 1.1 lukem goto return_results; 129 1.1 lukem } 130 1.1 lukem 131 1.1 lukem rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE; 132 1.1 lukem for ( a = attrs_find( e.e_attrs, op->oq_compare.rs_ava->aa_desc ); 133 1.1 lukem a != NULL; 134 1.1 lukem a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) ) 135 1.1 lukem { 136 1.1 lukem rs->sr_err = LDAP_COMPARE_FALSE; 137 1.1 lukem if ( attr_valfind( a, 138 1.1 lukem SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH | 139 1.1 lukem SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH, 140 1.1 lukem &op->oq_compare.rs_ava->aa_value, NULL, 141 1.1 lukem op->o_tmpmemctx ) == 0 ) 142 1.1 lukem { 143 1.1 lukem rs->sr_err = LDAP_COMPARE_TRUE; 144 1.1 lukem break; 145 1.1 lukem } 146 1.1 lukem } 147 1.1 lukem 148 1.1 lukem return_results:; 149 1.1 lukem switch ( rs->sr_err ) { 150 1.1 lukem case LDAP_COMPARE_TRUE: 151 1.1 lukem case LDAP_COMPARE_FALSE: 152 1.1 lukem break; 153 1.1 lukem 154 1.1 lukem default: 155 1.1 lukem if ( !BER_BVISNULL( &e.e_nname ) && 156 1.1 lukem ! access_allowed( op, &e, 157 1.1 lukem slap_schema.si_ad_entry, NULL, 158 1.1 lukem ACL_DISCLOSE, NULL ) ) 159 1.1 lukem { 160 1.1 lukem rs->sr_err = LDAP_NO_SUCH_OBJECT; 161 1.1 lukem rs->sr_text = NULL; 162 1.1 lukem } 163 1.1 lukem break; 164 1.1 lukem } 165 1.1 lukem 166 1.1 lukem send_ldap_result( op, rs ); 167 1.1 lukem 168 1.1 lukem if ( rs->sr_matched ) { 169 1.1 lukem rs->sr_matched = NULL; 170 1.1 lukem } 171 1.1 lukem 172 1.1 lukem if ( rs->sr_ref ) { 173 1.1 lukem ber_bvarray_free( rs->sr_ref ); 174 1.1 lukem rs->sr_ref = NULL; 175 1.1 lukem } 176 1.1 lukem 177 1.1 lukem if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) { 178 1.1 lukem (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx ); 179 1.1 lukem } 180 1.1 lukem 181 1.1 lukem if ( !BER_BVISNULL( &e.e_nname ) ) { 182 1.1 lukem backsql_entry_clean( op, &e ); 183 1.1 lukem } 184 1.1 lukem 185 1.1 lukem if ( bsi.bsi_attrs != NULL ) { 186 1.1 lukem op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx ); 187 1.1 lukem } 188 1.1 lukem 189 1.3 christos Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n" ); 190 1.1 lukem switch ( rs->sr_err ) { 191 1.1 lukem case LDAP_COMPARE_TRUE: 192 1.1 lukem case LDAP_COMPARE_FALSE: 193 1.1 lukem return LDAP_SUCCESS; 194 1.1 lukem 195 1.1 lukem default: 196 1.1 lukem return rs->sr_err; 197 1.1 lukem } 198 1.1 lukem } 199 1.1 lukem 200