]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/referral.c
add referral check to functions elaborated by overlays
[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-2004 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
35         if( op->o_tag == LDAP_REQ_SEARCH ) {
36                 /* let search take care of itself */
37                 return LDAP_SUCCESS;
38         }
39
40         if( get_manageDSAit( op ) ) {
41                 /* let op take care of DSA management */
42                 return LDAP_SUCCESS;
43         } 
44
45         /* grab giant lock for reading */
46         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
47
48         /* get entry with reader lock */
49         e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
50         if ( e == NULL ) {
51                 if ( matched != NULL ) {
52                         rs->sr_matched = ch_strdup( matched->e_dn );
53
54 #ifdef NEW_LOGGING
55                         LDAP_LOG( BACK_LDBM, DETAIL1,
56                                 "ldbm_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
57                                 op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
58 #else
59                         Debug( LDAP_DEBUG_TRACE,
60                                 "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
61                                 op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
62 #endif
63
64                         if( is_entry_referral( matched ) ) {
65                                 rs->sr_err = LDAP_OTHER;
66                                 rs->sr_ref = get_entry_referrals( op, matched );
67                         }
68
69                         cache_return_entry_r( &li->li_cache, matched );
70
71                 } else if ( default_referral != NULL ) {
72                         rs->sr_err = LDAP_OTHER;
73                         rs->sr_ref = referral_rewrite( default_referral,
74                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
75                 }
76
77                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
78
79                 if( rs->sr_ref != NULL ) {
80                         /* send referrals */
81                         rs->sr_err = LDAP_REFERRAL;
82                         send_ldap_result( op, rs );
83                         ber_bvarray_free( rs->sr_ref );
84
85                 } else if ( rs->sr_err != LDAP_SUCCESS ) {
86                         rs->sr_text = rs->sr_matched ? "bad referral object" : "bad default referral";
87                         send_ldap_result( op, rs );
88                 }
89
90                 if ( rs->sr_matched ) free( (char *)rs->sr_matched );
91                 rs->sr_ref = NULL;
92                 rs->sr_matched = NULL;
93                 return rs->sr_err;
94         }
95
96         if ( is_entry_referral( e ) ) {
97                 /* entry is a referral */
98                 BerVarray refs = get_entry_referrals( op, e );
99                 rs->sr_ref = referral_rewrite(
100                         refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
101
102 #ifdef NEW_LOGGING
103                 LDAP_LOG( BACK_LDBM, DETAIL1,
104                         "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
105                         op->o_tag, op->o_req_dn.bv_val, e->e_dn );
106 #else
107                 Debug( LDAP_DEBUG_TRACE,
108                         "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
109                         op->o_tag, op->o_req_dn.bv_val, e->e_dn );
110 #endif
111
112                 rs->sr_matched = e->e_name.bv_val;
113                 if( rs->sr_ref != NULL ) {
114                         rs->sr_err = LDAP_REFERRAL;
115                         send_ldap_result( op, rs );
116
117                         ber_bvarray_free( rs->sr_ref );
118
119                 } else {
120                         send_ldap_error( op, rs, LDAP_OTHER,
121                                 "bad referral object" );
122                 }
123
124                 if( refs != NULL ) ber_bvarray_free( refs );
125                 rs->sr_ref = NULL;
126                 rs->sr_matched = NULL;
127         }
128
129         cache_return_entry_r( &li->li_cache, e );
130         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
131
132         return rs->sr_err;
133 }