| 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 2001-2003 Pierangelo Masarati. |
| 6 |
* Portions Copyright 1999-2003 Howard Chu. |
| 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 the Howard Chu for inclusion |
| 19 |
* in OpenLDAP Software and subsequently enhanced by Pierangelo |
| 20 |
* Masarati. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "portable.h" |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
#include <ac/string.h> |
| 28 |
#include <ac/socket.h> |
| 29 |
|
| 30 |
#include "slap.h" |
| 31 |
#include "../back-ldap/back-ldap.h" |
| 32 |
#include "back-meta.h" |
| 33 |
|
| 34 |
int |
| 35 |
meta_back_compare( Operation *op, SlapReply *rs ) |
| 36 |
{ |
| 37 |
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private; |
| 38 |
metatarget_t *mt; |
| 39 |
metaconn_t *mc; |
| 40 |
int rc = 0; |
| 41 |
int candidate = -1; |
| 42 |
struct berval mdn = BER_BVNULL; |
| 43 |
dncookie dc; |
| 44 |
struct berval mapped_attr = op->orc_ava->aa_desc->ad_cname; |
| 45 |
struct berval mapped_value = op->orc_ava->aa_value; |
| 46 |
int msgid; |
| 47 |
ldap_back_send_t retrying = LDAP_BACK_RETRYING; |
| 48 |
LDAPControl **ctrls = NULL; |
| 49 |
SlapReply *candidates = NULL; |
| 50 |
|
| 51 |
candidates = meta_back_candidates_get( op ); |
| 52 |
mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR, candidates ); |
| 53 |
if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR, candidates ) ) { |
| 54 |
op->o_tmpfree( candidates, op->o_tmpmemctx ); |
| 55 |
return rs->sr_err; |
| 56 |
} |
| 57 |
|
| 58 |
assert( mc->mc_conns[ candidate ].msc_ld != NULL ); |
| 59 |
|
| 60 |
/* |
| 61 |
* Rewrite the modify dn, if needed |
| 62 |
*/ |
| 63 |
mt = mi->mi_targets[ candidate ]; |
| 64 |
dc.target = mt; |
| 65 |
dc.conn = op->o_conn; |
| 66 |
dc.rs = rs; |
| 67 |
dc.ctx = "compareDN"; |
| 68 |
|
| 69 |
switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) { |
| 70 |
case LDAP_UNWILLING_TO_PERFORM: |
| 71 |
rc = 1; |
| 72 |
goto cleanup; |
| 73 |
|
| 74 |
default: |
| 75 |
break; |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* if attr is objectClass, try to remap the value |
| 80 |
*/ |
| 81 |
if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) { |
| 82 |
ldap_back_map( &mt->mt_rwmap.rwm_oc, |
| 83 |
&op->orc_ava->aa_value, |
| 84 |
&mapped_value, BACKLDAP_MAP ); |
| 85 |
|
| 86 |
if ( BER_BVISNULL( &mapped_value ) || BER_BVISEMPTY( &mapped_value ) ) { |
| 87 |
goto cleanup; |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
* else try to remap the attribute |
| 92 |
*/ |
| 93 |
} else { |
| 94 |
ldap_back_map( &mt->mt_rwmap.rwm_at, |
| 95 |
&op->orc_ava->aa_desc->ad_cname, |
| 96 |
&mapped_attr, BACKLDAP_MAP ); |
| 97 |
if ( BER_BVISNULL( &mapped_attr ) || BER_BVISEMPTY( &mapped_attr ) ) { |
| 98 |
goto cleanup; |
| 99 |
} |
| 100 |
|
| 101 |
if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) |
| 102 |
{ |
| 103 |
dc.ctx = "compareAttrDN"; |
| 104 |
|
| 105 |
switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) ) |
| 106 |
{ |
| 107 |
case LDAP_UNWILLING_TO_PERFORM: |
| 108 |
rc = 1; |
| 109 |
goto cleanup; |
| 110 |
|
| 111 |
default: |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
retry:; |
| 118 |
ctrls = op->o_ctrls; |
| 119 |
rc = meta_back_controls_add( op, rs, mc, candidate, &ctrls ); |
| 120 |
if ( rc != LDAP_SUCCESS ) { |
| 121 |
send_ldap_result( op, rs ); |
| 122 |
goto cleanup; |
| 123 |
} |
| 124 |
|
| 125 |
rs->sr_err = ldap_compare_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val, |
| 126 |
mapped_attr.bv_val, &mapped_value, |
| 127 |
ctrls, NULL, &msgid ); |
| 128 |
|
| 129 |
rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid, |
| 130 |
mt->mt_timeout[ SLAP_OP_COMPARE ], ( LDAP_BACK_SENDRESULT | retrying ) ); |
| 131 |
if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) { |
| 132 |
retrying &= ~LDAP_BACK_RETRYING; |
| 133 |
if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR, candidates ) ) { |
| 134 |
/* if the identity changed, there might be need to re-authz */ |
| 135 |
(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls ); |
| 136 |
goto retry; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
cleanup:; |
| 141 |
(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls ); |
| 142 |
|
| 143 |
if ( mdn.bv_val != op->o_req_dn.bv_val ) { |
| 144 |
free( mdn.bv_val ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( op->orc_ava->aa_value.bv_val != mapped_value.bv_val ) { |
| 148 |
free( mapped_value.bv_val ); |
| 149 |
} |
| 150 |
|
| 151 |
if ( mc ) { |
| 152 |
meta_back_release_conn( mi, mc ); |
| 153 |
} |
| 154 |
|
| 155 |
op->o_tmpfree( candidates, op->o_tmpmemctx ); |
| 156 |
return rs->sr_err; |
| 157 |
} |
| 158 |
|