openldap/servers/slapd/back-bdb/referral.c

163 lines
4.0 KiB
C
Raw Normal View History

2000-09-28 07:25:15 +08:00
/* referral.c - BDB backend referral handler */
/* $OpenLDAP$ */
2003-11-29 05:08:20 +08:00
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
2006-01-04 07:11:52 +08:00
* Copyright 2000-2006 The OpenLDAP Foundation.
2003-11-29 05:08:20 +08:00
* All rights reserved.
*
* 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>.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/string.h>
#include "back-bdb.h"
int
bdb_referrals( Operation *op, SlapReply *rs )
{
struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
2002-04-30 00:42:41 +08:00
Entry *e = NULL;
2003-04-17 00:23:36 +08:00
EntryInfo *ei;
int rc = LDAP_SUCCESS;
u_int32_t locker;
DB_LOCK lock;
if( op->o_tag == LDAP_REQ_SEARCH ) {
/* let search take care of itself */
return rc;
}
if( get_manageDSAit( op ) ) {
/* let op take care of DSA management */
return rc;
}
rc = LOCK_ID(bdb->bi_dbenv, &locker);
switch(rc) {
case 0:
break;
default:
return LDAP_OTHER;
}
dn2entry_retry:
/* get entry */
rc = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1, locker, &lock );
2005-03-16 07:01:06 +08:00
/* bdb_dn2entry() may legally leave ei == NULL
* if rc != 0 and rc != DB_NOTFOUND
*/
if ( ei ) {
e = ei->bei_e;
}
switch(rc) {
case DB_NOTFOUND:
case 0:
break;
2002-04-20 05:41:32 +08:00
case LDAP_BUSY:
send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
LOCK_ID_FREE ( bdb->bi_dbenv, locker );
2002-04-20 05:41:32 +08:00
return LDAP_BUSY;
case DB_LOCK_DEADLOCK:
case DB_LOCK_NOTGRANTED:
goto dn2entry_retry;
default:
2000-09-28 07:25:15 +08:00
Debug( LDAP_DEBUG_TRACE,
2004-11-17 22:53:03 +08:00
LDAP_XSTRING(bdb_referrals)
": dn2entry failed: %s (%d)\n",
2000-09-28 07:25:15 +08:00
db_strerror(rc), rc, 0 );
send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
LOCK_ID_FREE ( bdb->bi_dbenv, locker );
return rs->sr_err;
}
2003-04-17 00:23:36 +08:00
if ( rc == DB_NOTFOUND ) {
rc = 0;
2003-04-22 02:28:38 +08:00
rs->sr_matched = NULL;
2003-04-17 00:23:36 +08:00
if ( e != NULL ) {
Debug( LDAP_DEBUG_TRACE,
2004-11-17 22:53:03 +08:00
LDAP_XSTRING(bdb_referrals)
": op=%ld target=\"%s\" matched=\"%s\"\n",
2003-05-15 03:30:25 +08:00
(long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
2003-04-17 00:23:36 +08:00
if( is_entry_referral( e ) ) {
2006-05-16 08:57:46 +08:00
BerVarray ref = get_entry_referrals( op, e );
rc = LDAP_OTHER;
2006-05-16 08:57:46 +08:00
rs->sr_ref = referral_rewrite( ref, NULL,
&op->o_req_dn, LDAP_SCOPE_DEFAULT );
ber_bvarray_free( ref );
2003-04-22 02:28:38 +08:00
if ( rs->sr_ref ) {
rs->sr_matched = ber_strdup_x(
e->e_name.bv_val, op->o_tmpmemctx );
}
}
2003-04-17 00:23:36 +08:00
bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
e = NULL;
2006-01-07 00:04:07 +08:00
} else if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) && default_referral != NULL ) {
rc = LDAP_OTHER;
rs->sr_ref = referral_rewrite( default_referral,
NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
}
if( rs->sr_ref != NULL ) {
/* send referrals */
2005-01-11 08:38:04 +08:00
rc = rs->sr_err = LDAP_REFERRAL;
send_ldap_result( op, rs );
ber_bvarray_free( rs->sr_ref );
rs->sr_ref = NULL;
} else if ( rc != LDAP_SUCCESS ) {
rs->sr_err = rc;
rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
send_ldap_result( op, rs );
}
LOCK_ID_FREE ( bdb->bi_dbenv, locker );
2003-04-22 02:28:38 +08:00
if (rs->sr_matched) {
op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
2003-04-22 02:28:38 +08:00
rs->sr_matched = NULL;
}
return rc;
}
if ( is_entry_referral( e ) ) {
/* entry is a referral */
BerVarray refs = get_entry_referrals( op, e );
rs->sr_ref = referral_rewrite(
refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
Debug( LDAP_DEBUG_TRACE,
2004-11-17 22:53:03 +08:00
LDAP_XSTRING(bdb_referrals)
": op=%ld target=\"%s\" matched=\"%s\"\n",
2003-05-15 03:30:25 +08:00
(long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
rs->sr_matched = e->e_name.bv_val;
if( rs->sr_ref != NULL ) {
2004-04-06 01:31:27 +08:00
rc = rs->sr_err = LDAP_REFERRAL;
send_ldap_result( op, rs );
ber_bvarray_free( rs->sr_ref );
rs->sr_ref = NULL;
} else {
send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
rc = rs->sr_err;
}
rs->sr_matched = NULL;
ber_bvarray_free( refs );
}
bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
LOCK_ID_FREE ( bdb->bi_dbenv, locker );
return rc;
}