1 /* referral.c - LDBM backend referral handler */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
25 #include "back-ldbm.h"
32 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
34 int rc = LDAP_SUCCESS;
36 if( op->o_tag == LDAP_REQ_SEARCH ) {
37 /* let search take care of itself */
41 if( get_manageDSAit( op ) ) {
42 /* let op take care of DSA management */
46 /* grab giant lock for reading */
47 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
49 /* get entry with reader lock */
50 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
52 if ( matched != NULL ) {
53 rs->sr_matched = ch_strdup( matched->e_dn );
55 Debug( LDAP_DEBUG_TRACE,
56 "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
57 op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
59 if ( is_entry_referral( matched ) ) {
60 rc = rs->sr_err = LDAP_OTHER;
61 rs->sr_ref = get_entry_referrals( op, matched );
64 cache_return_entry_r( &li->li_cache, matched );
66 } else if ( default_referral != NULL ) {
67 rs->sr_ref = referral_rewrite( default_referral,
68 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
71 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
73 if ( rs->sr_ref != NULL ) {
75 rc = rs->sr_err = LDAP_REFERRAL;
78 rs->sr_text = rs->sr_matched ? "bad referral object" : "bad default referral";
81 send_ldap_result( op, rs );
83 if ( rs->sr_matched ) free( (char *)rs->sr_matched );
84 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
87 rs->sr_matched = NULL;
92 if ( is_entry_referral( e ) ) {
93 /* entry is a referral */
94 BerVarray refs = get_entry_referrals( op, e );
95 rs->sr_ref = referral_rewrite(
96 refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
98 Debug( LDAP_DEBUG_TRACE,
99 "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
100 op->o_tag, op->o_req_dn.bv_val, e->e_dn );
102 rs->sr_matched = e->e_name.bv_val;
103 if( rs->sr_ref != NULL ) {
104 rc = rs->sr_err = LDAP_REFERRAL;
108 rc = rs->sr_err = LDAP_OTHER;
109 rs->sr_text = "bad referral object";
111 send_ldap_result( op, rs );
113 if ( refs != NULL ) ber_bvarray_free( refs );
117 rs->sr_matched = NULL;
120 cache_return_entry_r( &li->li_cache, e );
121 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);