]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/referral.c
cleanup bind
[openldap] / servers / slapd / back-ldbm / referral.c
1 /* referral.c - LDBM backend referral handler */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2000-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17
18 int
19 ldbm_back_referrals(
20     Backend     *be,
21     Connection  *conn,
22     Operation   *op,
23     struct berval *dn,
24     struct berval *ndn,
25         const char **text )
26 {
27         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
28         int rc = LDAP_SUCCESS;
29         Entry *e, *matched;
30
31         if( op->o_tag == LDAP_REQ_SEARCH ) {
32                 /* let search take care of itself */
33                 return rc;
34         }
35
36         if( get_manageDSAit( op ) ) {
37                 /* let op take care of DSA management */
38                 return rc;
39         } 
40
41         /* grab giant lock for reading */
42         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
43
44         /* get entry with reader lock */
45         e = dn2entry_r( be, ndn, &matched );
46         if ( e == NULL ) {
47                 char *matched_dn = NULL;
48                 BerVarray refs = NULL;
49
50                 if ( matched != NULL ) {
51                         matched_dn = ch_strdup( matched->e_dn );
52
53 #ifdef NEW_LOGGING
54                         LDAP_LOG( BACK_LDBM, DETAIL1,
55                                 "ldbm_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
56                                 op->o_tag, dn->bv_val, matched_dn );
57 #else
58                         Debug( LDAP_DEBUG_TRACE,
59                                 "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
60                                 op->o_tag, dn->bv_val, matched_dn );
61 #endif
62
63                         if( is_entry_referral( matched ) ) {
64                                 rc = LDAP_OTHER;
65                                 refs = get_entry_referrals( be, conn, op, matched );
66                         }
67
68                         cache_return_entry_r( &li->li_cache, matched );
69
70                 } else if ( default_referral != NULL ) {
71                         rc = LDAP_OTHER;
72                         refs = referral_rewrite( default_referral,
73                                 NULL, dn, LDAP_SCOPE_DEFAULT );
74                 }
75
76                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
77
78                 if( refs != NULL ) {
79                         /* send referrals */
80                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
81                                 matched_dn, NULL, refs, NULL );
82                         ber_bvarray_free( refs );
83
84                 } else if ( rc != LDAP_SUCCESS ) {
85                         send_ldap_result( conn, op, rc, matched_dn,
86                                 matched_dn ? "bad referral object" : "bad default referral",
87                                 NULL, NULL );
88                 }
89
90                 if ( matched_dn ) free( matched_dn );
91                 return rc;
92         }
93
94         if ( is_entry_referral( e ) ) {
95                 /* entry is a referral */
96                 BerVarray refs = get_entry_referrals( be, conn, op, e );
97                 BerVarray rrefs = referral_rewrite(
98                         refs, &e->e_name, dn, LDAP_SCOPE_DEFAULT );
99
100 #ifdef NEW_LOGGING
101                 LDAP_LOG( BACK_LDBM, DETAIL1,
102                         "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
103                         op->o_tag, dn->bv_val, e->e_dn );
104 #else
105                 Debug( LDAP_DEBUG_TRACE,
106                         "ldbm_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
107                         op->o_tag, dn->bv_val, e->e_dn );
108 #endif
109
110                 if( rrefs != NULL ) {
111                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
112                                 e->e_dn, NULL, rrefs, NULL );
113
114                         ber_bvarray_free( rrefs );
115
116                 } else {
117                         send_ldap_result( conn, op, rc = LDAP_OTHER, e->e_dn,
118                                 "bad referral object", NULL, NULL );
119                 }
120
121                 if( refs != NULL ) ber_bvarray_free( refs );
122         }
123
124         cache_return_entry_r( &li->li_cache, e );
125         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
126
127         return rc;
128 }