1 /* referral.c - BDB backend referral handler */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/string.h>
24 bdb_referrals( Operation *op, SlapReply *rs )
26 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
29 int rc = LDAP_SUCCESS;
34 if( op->o_tag == LDAP_REQ_SEARCH ) {
35 /* let search take care of itself */
39 if( get_manageDSAit( op ) ) {
40 /* let op take care of DSA management */
44 rc = LOCK_ID(bdb->bi_dbenv, &locker);
54 rc = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1, locker, &lock );
56 /* bdb_dn2entry() may legally leave ei == NULL
57 * if rc != 0 and rc != DB_NOTFOUND
68 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
69 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
71 case DB_LOCK_DEADLOCK:
72 case DB_LOCK_NOTGRANTED:
75 Debug( LDAP_DEBUG_TRACE,
76 LDAP_XSTRING(bdb_referrals)
77 ": dn2entry failed: %s (%d)\n",
78 db_strerror(rc), rc, 0 );
79 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
80 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
84 if ( rc == DB_NOTFOUND ) {
86 rs->sr_matched = NULL;
88 Debug( LDAP_DEBUG_TRACE,
89 LDAP_XSTRING(bdb_referrals)
90 ": tag=%lu target=\"%s\" matched=\"%s\"\n",
91 (unsigned long)op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
93 if( is_entry_referral( e ) ) {
94 BerVarray ref = get_entry_referrals( op, e );
96 rs->sr_ref = referral_rewrite( ref, NULL,
97 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
98 ber_bvarray_free( ref );
100 rs->sr_matched = ber_strdup_x(
101 e->e_name.bv_val, op->o_tmpmemctx );
105 bdb_cache_return_entry_r (bdb, e, &lock);
107 } else if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) && default_referral != NULL ) {
109 rs->sr_ref = referral_rewrite( default_referral,
110 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
113 if( rs->sr_ref != NULL ) {
115 rc = rs->sr_err = LDAP_REFERRAL;
116 send_ldap_result( op, rs );
117 ber_bvarray_free( rs->sr_ref );
119 } else if ( rc != LDAP_SUCCESS ) {
121 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
122 send_ldap_result( op, rs );
125 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
126 if (rs->sr_matched) {
127 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
128 rs->sr_matched = NULL;
133 if ( is_entry_referral( e ) ) {
134 /* entry is a referral */
135 BerVarray refs = get_entry_referrals( op, e );
136 rs->sr_ref = referral_rewrite(
137 refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
139 Debug( LDAP_DEBUG_TRACE,
140 LDAP_XSTRING(bdb_referrals)
141 ": tag=%lu target=\"%s\" matched=\"%s\"\n",
142 (unsigned long)op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
144 rs->sr_matched = e->e_name.bv_val;
145 if( rs->sr_ref != NULL ) {
146 rc = rs->sr_err = LDAP_REFERRAL;
147 send_ldap_result( op, rs );
148 ber_bvarray_free( rs->sr_ref );
151 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
155 rs->sr_matched = NULL;
156 ber_bvarray_free( refs );
159 bdb_cache_return_entry_r(bdb, e, &lock);
160 LOCK_ID_FREE ( bdb->bi_dbenv, locker );