]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/referral.c
Sync with HEAD
[openldap] / servers / slapd / back-ldbm / referral.c
1 /* referral.c - LDBM backend referral handler */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26
27 int
28 ldbm_back_referrals(
29     Operation   *op,
30     SlapReply   *rs )
31 {
32         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
33         Entry           *e, *matched;
34         int             rc = LDAP_SUCCESS;
35
36         if( op->o_tag == LDAP_REQ_SEARCH ) {
37                 /* let search take care of itself */
38                 return LDAP_SUCCESS;
39         }
40
41         if( get_manageDSAit( op ) ) {
42                 /* let op take care of DSA management */
43                 return LDAP_SUCCESS;
44         } 
45
46         /* grab giant lock for reading */
47         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
48
49         /* get entry with reader lock */
50         e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
51         if ( e == NULL ) {
52                 if ( matched != NULL ) {
53                         rs->sr_matched = ch_strdup( matched->e_dn );
54
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 );
58
59                         if ( is_entry_referral( matched ) ) {
60                                 rc = rs->sr_err = LDAP_OTHER;
61                                 rs->sr_ref = get_entry_referrals( op, matched );
62                         }
63
64                         cache_return_entry_r( &li->li_cache, matched );
65
66                 } else if ( default_referral != NULL ) {
67                         rc = rs->sr_err = LDAP_OTHER;
68                         rs->sr_ref = referral_rewrite( default_referral,
69                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
70                 }
71
72                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
73
74                 if ( rs->sr_ref != NULL ) {
75                         /* send referrals */
76                         rc = rs->sr_err = LDAP_REFERRAL;
77
78                 } else {
79                         rs->sr_text = rs->sr_matched ? "bad referral object" : "bad default referral";
80                 }
81
82                 if ( rc != LDAP_SUCCESS ) {
83                         send_ldap_result( op, rs );
84                 }
85
86                 if ( rs->sr_matched ) free( (char *)rs->sr_matched );
87                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
88                 rs->sr_text = NULL;
89                 rs->sr_ref = NULL;
90                 rs->sr_matched = NULL;
91
92                 return rc;
93         }
94
95         if ( is_entry_referral( e ) ) {
96                 /* entry is a referral */
97                 BerVarray refs = get_entry_referrals( op, e );
98                 rs->sr_ref = referral_rewrite(
99                         refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
100
101                 Debug( LDAP_DEBUG_TRACE,
102                         "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
103                         op->o_tag, op->o_req_dn.bv_val, e->e_dn );
104
105                 rs->sr_matched = e->e_name.bv_val;
106                 if( rs->sr_ref != NULL ) {
107                         rc = rs->sr_err = LDAP_REFERRAL;
108                         rs->sr_text = NULL;
109
110                 } else {
111                         rc = rs->sr_err = LDAP_OTHER;
112                         rs->sr_text = "bad referral object";
113                 }
114                 send_ldap_result( op, rs );
115
116                 if ( refs != NULL ) ber_bvarray_free( refs );
117                 rs->sr_err = rc;
118                 rs->sr_ref = NULL;
119                 rs->sr_text = NULL;
120                 rs->sr_matched = NULL;
121         }
122
123         cache_return_entry_r( &li->li_cache, e );
124         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
125
126         return rc;
127 }