]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/referral.c
Use EntryInfo navigation for search scope checks
[openldap] / servers / slapd / back-bdb / referral.c
1 /* referral.c - BDB 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 #include <stdio.h>
10 #include <ac/string.h>
11
12 #include "back-bdb.h"
13 #include "external.h"
14
15 int
16 bdb_referrals( Operation *op, SlapReply *rs )
17 {
18         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
19         Entry *e = NULL;
20         EntryInfo *ei;
21         int rc = LDAP_SUCCESS;
22
23         u_int32_t       locker;
24         DB_LOCK         lock;
25
26         if( op->o_tag == LDAP_REQ_SEARCH ) {
27                 /* let search take care of itself */
28                 return rc;
29         }
30
31         if( get_manageDSAit( op ) ) {
32                 /* let op take care of DSA management */
33                 return rc;
34         } 
35
36         rc = LOCK_ID(bdb->bi_dbenv, &locker);
37         switch(rc) {
38         case 0:
39                 break;
40         default:
41                 return LDAP_OTHER;
42         }
43
44 dn2entry_retry:
45         /* get entry */
46         rc = bdb_dn2entry( op->o_bd, NULL, &op->o_req_ndn, &ei, 1, locker,
47                 &lock, op->o_tmpmemctx );
48
49         e = ei->bei_e;
50         switch(rc) {
51         case DB_NOTFOUND:
52         case 0:
53                 break;
54         case LDAP_BUSY:
55                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
56                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
57                 return LDAP_BUSY;
58         case DB_LOCK_DEADLOCK:
59         case DB_LOCK_NOTGRANTED:
60                 goto dn2entry_retry;
61         default:
62 #ifdef NEW_LOGGING
63                 LDAP_LOG ( OPERATION, ERR, 
64                         "bdb_referrals: dn2entry failed: %s (%d)\n", 
65                         db_strerror(rc), rc, 0 );
66 #else
67                 Debug( LDAP_DEBUG_TRACE,
68                         "bdb_referrals: dn2entry failed: %s (%d)\n",
69                         db_strerror(rc), rc, 0 ); 
70 #endif
71                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
72                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
73                 return rs->sr_err;
74         }
75
76         if ( rc == DB_NOTFOUND ) {
77                 rc = 0;
78                 if ( e != NULL ) {
79                         rs->sr_matched = ch_strdup( e->e_name.bv_val );
80
81 #ifdef NEW_LOGGING
82                 LDAP_LOG ( OPERATION, DETAIL1, 
83                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
84                         (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
85 #else
86                         Debug( LDAP_DEBUG_TRACE,
87                                 "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
88                                 (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
89 #endif
90
91                         if( is_entry_referral( e ) ) {
92                                 rc = LDAP_OTHER;
93                                 rs->sr_ref = get_entry_referrals( op, e );
94                         }
95
96                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
97                         e = NULL;
98                 } else if ( default_referral != NULL ) {
99                         rc = LDAP_OTHER;
100                         rs->sr_ref = referral_rewrite( default_referral,
101                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
102                 }
103
104                 if( rs->sr_ref != NULL ) {
105                         /* send referrals */
106                         rs->sr_err = LDAP_REFERRAL;
107                         send_ldap_result( op, rs );
108                         ber_bvarray_free( rs->sr_ref );
109                         rs->sr_ref = NULL;
110                 } else if ( rc != LDAP_SUCCESS ) {
111                         rs->sr_err = rc;
112                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
113                         send_ldap_result( op, rs );
114                 }
115
116                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
117                 free( (char *)rs->sr_matched );
118                 rs->sr_matched = NULL;
119                 return rc;
120         }
121
122         if ( is_entry_referral( e ) ) {
123                 /* entry is a referral */
124                 BerVarray refs = get_entry_referrals( op, e );
125                 rs->sr_ref = referral_rewrite(
126                         refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
127
128 #ifdef NEW_LOGGING
129                 LDAP_LOG ( OPERATION, DETAIL1, 
130                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
131                         (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
132 #else
133                 Debug( LDAP_DEBUG_TRACE,
134                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
135                         (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
136 #endif
137
138                 rs->sr_matched = e->e_name.bv_val;
139                 if( rs->sr_ref != NULL ) {
140                         rs->sr_err = LDAP_REFERRAL;
141                         send_ldap_result( op, rs );
142                         ber_bvarray_free( rs->sr_ref );
143                         rs->sr_ref = NULL;
144                 } else {
145                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
146                         rc = rs->sr_err;
147                 }
148
149                 rs->sr_matched = NULL;
150                 ber_bvarray_free( refs );
151         }
152
153         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
154         LOCK_ID_FREE ( bdb->bi_dbenv, locker );
155         return rc;
156 }