2003-11-27 14:35:14 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2005-01-02 04:49:32 +08:00
|
|
|
* Copyright 1999-2005 The OpenLDAP Foundation.
|
2003-12-09 01:41:40 +08:00
|
|
|
* Portions Copyright 2001-2003 Pierangelo Masarati.
|
|
|
|
* Portions Copyright 1999-2003 Howard Chu.
|
2003-11-27 14:35:14 +08:00
|
|
|
* All rights reserved.
|
2001-05-12 08:51:28 +08:00
|
|
|
*
|
2003-11-27 14:35:14 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by the Howard Chu for inclusion
|
|
|
|
* in OpenLDAP Software and subsequently enhanced by Pierangelo
|
|
|
|
* Masarati.
|
|
|
|
*/
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "../back-ldap/back-ldap.h"
|
|
|
|
#include "back-meta.h"
|
|
|
|
|
|
|
|
int
|
2003-04-04 05:35:54 +08:00
|
|
|
meta_back_compare( Operation *op, SlapReply *rs )
|
2001-05-12 08:51:28 +08:00
|
|
|
{
|
2005-01-08 17:20:54 +08:00
|
|
|
struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
|
|
|
|
struct metaconn *lc;
|
|
|
|
struct metasingleconn *lsc;
|
|
|
|
char *match = NULL,
|
|
|
|
*err = NULL;
|
|
|
|
struct berval mmatch = BER_BVNULL;
|
2005-04-16 10:25:41 +08:00
|
|
|
int ncandidates = 0,
|
2005-01-08 17:20:54 +08:00
|
|
|
last = 0,
|
|
|
|
i,
|
|
|
|
count = 0,
|
|
|
|
rc,
|
|
|
|
cres = LDAP_SUCCESS,
|
|
|
|
rres = LDAP_SUCCESS,
|
|
|
|
*msgid;
|
|
|
|
dncookie dc;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2005-04-22 06:22:43 +08:00
|
|
|
SlapReply *candidates = meta_back_candidates_get( op );
|
2005-04-16 10:25:41 +08:00
|
|
|
|
|
|
|
lc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
|
2005-02-05 23:55:27 +08:00
|
|
|
if ( !lc || !meta_back_dobind( lc, op, LDAP_BACK_SENDERR ) ) {
|
|
|
|
return rs->sr_err;
|
2003-04-04 05:35:54 +08:00
|
|
|
}
|
|
|
|
|
2005-04-16 10:25:41 +08:00
|
|
|
msgid = ch_calloc( sizeof( int ), li->mi_ntargets );
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( msgid == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start an asynchronous compare for each candidate target
|
|
|
|
*/
|
2003-04-08 01:04:14 +08:00
|
|
|
dc.conn = op->o_conn;
|
|
|
|
dc.rs = rs;
|
2004-03-11 05:11:14 +08:00
|
|
|
dc.ctx = "compareDN";
|
2003-04-08 01:04:14 +08:00
|
|
|
|
2005-04-21 02:52:10 +08:00
|
|
|
for ( i = 0, lsc = &lc->mc_conns[ 0 ]; !META_LAST( lsc ); ++i, ++lsc ) {
|
2004-04-07 12:11:43 +08:00
|
|
|
struct berval mdn = BER_BVNULL;
|
2005-01-08 17:20:54 +08:00
|
|
|
struct berval mapped_attr = op->orc_ava->aa_desc->ad_cname;
|
|
|
|
struct berval mapped_value = op->orc_ava->aa_value;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2005-04-22 06:22:43 +08:00
|
|
|
if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
|
2003-01-24 06:07:33 +08:00
|
|
|
msgid[ i ] = -1;
|
2001-05-12 08:51:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewrite the compare dn, if needed
|
|
|
|
*/
|
2005-04-16 10:25:41 +08:00
|
|
|
dc.rwmap = &li->mi_targets[ i ]->mt_rwmap;
|
2003-04-08 01:04:14 +08:00
|
|
|
|
|
|
|
switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
|
|
|
|
case LDAP_UNWILLING_TO_PERFORM:
|
|
|
|
rc = 1;
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
default:
|
2001-05-12 08:51:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if attr is objectClass, try to remap the value
|
|
|
|
*/
|
2005-01-08 17:20:54 +08:00
|
|
|
if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
|
2005-04-16 10:25:41 +08:00
|
|
|
ldap_back_map( &li->mi_targets[ i ]->mt_rwmap.rwm_oc,
|
2005-01-08 17:20:54 +08:00
|
|
|
&op->orc_ava->aa_value,
|
2003-04-04 05:35:54 +08:00
|
|
|
&mapped_value, BACKLDAP_MAP );
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2005-04-16 10:25:41 +08:00
|
|
|
if ( BER_BVISNULL( &mapped_value ) || mapped_value.bv_val[0] == '\0' ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* else try to remap the attribute
|
|
|
|
*/
|
|
|
|
} else {
|
2005-04-16 10:25:41 +08:00
|
|
|
ldap_back_map( &li->mi_targets[ i ]->mt_rwmap.rwm_at,
|
2005-01-08 17:20:54 +08:00
|
|
|
&op->orc_ava->aa_desc->ad_cname,
|
2003-04-04 05:35:54 +08:00
|
|
|
&mapped_attr, BACKLDAP_MAP );
|
2005-04-16 10:25:41 +08:00
|
|
|
if ( BER_BVISNULL( &mapped_attr ) || mapped_attr.bv_val[0] == '\0' ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
2004-12-31 01:45:07 +08:00
|
|
|
|
2005-01-08 17:20:54 +08:00
|
|
|
if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
|
2004-12-31 01:45:07 +08:00
|
|
|
{
|
|
|
|
dc.ctx = "compareAttrDN";
|
|
|
|
|
2005-01-08 17:20:54 +08:00
|
|
|
switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
|
2004-12-31 01:45:07 +08:00
|
|
|
{
|
|
|
|
case LDAP_UNWILLING_TO_PERFORM:
|
|
|
|
rc = 1;
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the compare op is spawned across the targets and the first
|
|
|
|
* that returns determines the result; a constraint on unicity
|
|
|
|
* of the result ought to be enforced
|
|
|
|
*/
|
2005-01-08 17:20:54 +08:00
|
|
|
rc = ldap_compare_ext( lc->mc_conns[ i ].msc_ld, mdn.bv_val,
|
|
|
|
mapped_attr.bv_val, &mapped_value,
|
2005-01-26 17:52:12 +08:00
|
|
|
op->o_ctrls, NULL, &msgid[ i ] );
|
2004-12-31 01:45:07 +08:00
|
|
|
|
2003-04-08 01:04:14 +08:00
|
|
|
if ( mdn.bv_val != op->o_req_dn.bv_val ) {
|
|
|
|
free( mdn.bv_val );
|
2005-01-08 17:20:54 +08:00
|
|
|
BER_BVZERO( &mdn );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2004-12-31 01:45:07 +08:00
|
|
|
|
2005-01-08 17:20:54 +08:00
|
|
|
if ( mapped_attr.bv_val != op->orc_ava->aa_desc->ad_cname.bv_val ) {
|
2002-01-04 00:29:38 +08:00
|
|
|
free( mapped_attr.bv_val );
|
2005-01-08 17:20:54 +08:00
|
|
|
BER_BVZERO( &mapped_attr );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
2004-12-31 01:45:07 +08:00
|
|
|
|
2005-01-08 17:20:54 +08:00
|
|
|
if ( mapped_value.bv_val != op->orc_ava->aa_value.bv_val ) {
|
2002-01-04 00:29:38 +08:00
|
|
|
free( mapped_value.bv_val );
|
2005-01-08 17:20:54 +08:00
|
|
|
BER_BVZERO( &mapped_value );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2005-01-08 17:20:54 +08:00
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
|
|
|
/* FIXME: what should we do with the error? */
|
2003-01-24 06:07:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-04-16 10:25:41 +08:00
|
|
|
++ncandidates;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wait for replies
|
|
|
|
*/
|
2005-04-16 10:25:41 +08:00
|
|
|
for ( rc = 0, count = 0; ncandidates > 0; ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: should we check for abandon?
|
|
|
|
*/
|
2005-04-21 02:52:10 +08:00
|
|
|
for ( i = 0, lsc = &lc->mc_conns[ 0 ]; !META_LAST( lsc ); lsc++, i++ ) {
|
2003-04-04 05:35:54 +08:00
|
|
|
int lrc;
|
|
|
|
LDAPMessage *res = NULL;
|
2005-04-21 11:17:31 +08:00
|
|
|
struct timeval tv = { 0 };
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2003-01-24 06:07:33 +08:00
|
|
|
if ( msgid[ i ] == -1 ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-01-08 17:20:54 +08:00
|
|
|
lrc = ldap_result( lsc->msc_ld, msgid[ i ],
|
2005-04-21 11:17:31 +08:00
|
|
|
0, &tv, &res );
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
if ( lrc == 0 ) {
|
2005-04-21 11:17:31 +08:00
|
|
|
assert( res == NULL );
|
2001-05-12 08:51:28 +08:00
|
|
|
continue;
|
2003-04-04 05:35:54 +08:00
|
|
|
|
2005-04-21 11:17:31 +08:00
|
|
|
} else if ( lrc == -1 ) {
|
|
|
|
/* we do not retry in this case;
|
|
|
|
* only for unique operations... */
|
|
|
|
ldap_get_option( lsc->msc_ld,
|
|
|
|
LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
|
|
|
|
rres = slap_map_api2result( rs );
|
|
|
|
rres = rc;
|
|
|
|
rc = -1;
|
|
|
|
goto finish;
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
} else if ( lrc == LDAP_RES_COMPARE ) {
|
|
|
|
if ( count > 0 ) {
|
2002-08-01 06:49:02 +08:00
|
|
|
rres = LDAP_OTHER;
|
2001-05-12 08:51:28 +08:00
|
|
|
rc = -1;
|
|
|
|
goto finish;
|
|
|
|
}
|
2005-01-08 17:20:54 +08:00
|
|
|
|
|
|
|
rc = ldap_parse_result( lsc->msc_ld, res,
|
|
|
|
&rs->sr_err,
|
|
|
|
NULL, NULL, NULL, NULL, 1 );
|
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
|
|
|
rres = rc;
|
|
|
|
rc = -1;
|
|
|
|
goto finish;
|
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2003-04-06 00:53:57 +08:00
|
|
|
switch ( rs->sr_err ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
case LDAP_COMPARE_TRUE:
|
|
|
|
case LDAP_COMPARE_FALSE:
|
|
|
|
|
|
|
|
/*
|
2005-04-21 11:17:31 +08:00
|
|
|
* true or false, got it;
|
2001-05-12 08:51:28 +08:00
|
|
|
* sending to cache ...
|
|
|
|
*/
|
2005-04-16 10:25:41 +08:00
|
|
|
if ( li->mi_cache.ttl != META_DNCACHE_DISABLED ) {
|
|
|
|
( void )meta_dncache_update_entry( &li->mi_cache, &op->o_req_ndn, i );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-04-06 01:32:59 +08:00
|
|
|
rres = slap_map_api2result( rs );
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
if ( err != NULL ) {
|
|
|
|
free( err );
|
|
|
|
}
|
2005-01-08 17:20:54 +08:00
|
|
|
ldap_get_option( lsc->msc_ld,
|
2001-05-12 08:51:28 +08:00
|
|
|
LDAP_OPT_ERROR_STRING, &err );
|
|
|
|
|
|
|
|
if ( match != NULL ) {
|
|
|
|
free( match );
|
|
|
|
}
|
2005-01-08 17:20:54 +08:00
|
|
|
ldap_get_option( lsc->msc_ld,
|
2001-05-12 08:51:28 +08:00
|
|
|
LDAP_OPT_MATCHED_DN, &match );
|
|
|
|
|
|
|
|
last = i;
|
|
|
|
break;
|
|
|
|
}
|
2003-01-24 06:07:33 +08:00
|
|
|
msgid[ i ] = -1;
|
2005-04-16 10:25:41 +08:00
|
|
|
--ncandidates;
|
2003-01-24 06:07:33 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
} else {
|
2003-01-24 06:07:33 +08:00
|
|
|
msgid[ i ] = -1;
|
2005-04-16 10:25:41 +08:00
|
|
|
--ncandidates;
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( res ) {
|
|
|
|
ldap_msgfree( res );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-03 16:55:17 +08:00
|
|
|
finish:;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewrite the matched portion of the search base, if required
|
|
|
|
*
|
|
|
|
* FIXME: only the last one gets caught!
|
|
|
|
*/
|
|
|
|
if ( count == 1 ) {
|
|
|
|
if ( match != NULL ) {
|
|
|
|
free( match );
|
|
|
|
match = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the result of the compare is assigned to the res code
|
|
|
|
* that will be returned
|
|
|
|
*/
|
|
|
|
rres = cres;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At least one compare failed with matched portion,
|
|
|
|
* and none was successful
|
|
|
|
*/
|
2003-04-08 01:04:14 +08:00
|
|
|
} else if ( match != NULL && match[0] != '\0' ) {
|
|
|
|
struct berval matched;
|
|
|
|
|
2005-04-16 10:25:41 +08:00
|
|
|
ber_str2bv( match, 0, 0, &matched );
|
2003-04-08 01:04:14 +08:00
|
|
|
|
2004-03-11 05:11:14 +08:00
|
|
|
dc.ctx = "matchedDN";
|
2003-04-08 01:04:14 +08:00
|
|
|
ldap_back_dn_massage( &dc, &matched, &mmatch );
|
2001-05-14 07:44:22 +08:00
|
|
|
}
|
|
|
|
|
2004-04-03 20:15:33 +08:00
|
|
|
if ( rres != LDAP_SUCCESS ) {
|
|
|
|
rs->sr_err = rres;
|
|
|
|
}
|
2003-04-08 01:04:14 +08:00
|
|
|
rs->sr_matched = mmatch.bv_val;
|
2003-04-04 05:35:54 +08:00
|
|
|
send_ldap_result( op, rs );
|
|
|
|
rs->sr_matched = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
if ( match != NULL ) {
|
2003-04-08 01:04:14 +08:00
|
|
|
if ( mmatch.bv_val != match ) {
|
|
|
|
free( mmatch.bv_val );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
free( match );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( msgid ) {
|
|
|
|
free( msgid );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|