1 /* referral.c - BDB backend referral handler */
4 * Copyright 2000-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
16 bdb_referrals( Operation *op, SlapReply *rs )
18 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
20 Entry *matched = NULL;
21 int rc = LDAP_SUCCESS;
26 if( op->o_tag == LDAP_REQ_SEARCH ) {
27 /* let search take care of itself */
31 if( get_manageDSAit( op ) ) {
32 /* let op take care of DSA management */
36 rc = LOCK_ID(bdb->bi_dbenv, &locker);
46 rc = bdb_dn2entry_r( op->o_bd, NULL, &op->o_req_ndn, &e, &matched, 0, locker, &lock );
55 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
57 if (matched != NULL) {
58 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
60 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
61 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
63 case DB_LOCK_DEADLOCK:
64 case DB_LOCK_NOTGRANTED:
68 LDAP_LOG ( OPERATION, ERR,
69 "bdb_referrals: dn2entry failed: %s (%d)\n",
70 db_strerror(rc), rc, 0 );
72 Debug( LDAP_DEBUG_TRACE,
73 "bdb_referrals: dn2entry failed: %s (%d)\n",
74 db_strerror(rc), rc, 0 );
77 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
79 if (matched != NULL) {
80 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
82 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
83 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
88 if ( matched != NULL ) {
89 rs->sr_matched = ch_strdup( matched->e_name.bv_val );
92 LDAP_LOG ( OPERATION, DETAIL1,
93 "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
94 (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
96 Debug( LDAP_DEBUG_TRACE,
97 "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
98 (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
101 if( is_entry_referral( matched ) ) {
103 rs->sr_ref = get_entry_referrals( op, matched );
106 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
108 } else if ( default_referral != NULL ) {
110 rs->sr_ref = referral_rewrite( default_referral,
111 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
114 if( rs->sr_ref != NULL ) {
116 rs->sr_err = LDAP_REFERRAL;
117 send_ldap_result( op, rs );
118 ber_bvarray_free( rs->sr_ref );
120 } else if ( rc != LDAP_SUCCESS ) {
122 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
123 send_ldap_result( op, rs );
126 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
127 free( (char *)rs->sr_matched );
128 rs->sr_matched = NULL;
132 if ( is_entry_referral( e ) ) {
133 /* entry is a referral */
134 BerVarray refs = get_entry_referrals( op, e );
135 rs->sr_ref = referral_rewrite(
136 refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
139 LDAP_LOG ( OPERATION, DETAIL1,
140 "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
141 (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
143 Debug( LDAP_DEBUG_TRACE,
144 "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
145 (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
148 rs->sr_matched = e->e_name.bv_val;
149 if( rs->sr_ref != NULL ) {
150 rs->sr_err = LDAP_REFERRAL;
151 send_ldap_result( op, rs );
152 ber_bvarray_free( rs->sr_ref );
155 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
159 rs->sr_matched = NULL;
160 ber_bvarray_free( refs );
163 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
164 LOCK_ID_FREE ( bdb->bi_dbenv, locker );