]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/referral.c
Mem context tweaks for bdb_dn2idl
[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                 rs->sr_matched = NULL;
79                 if ( e != NULL ) {
80 #ifdef NEW_LOGGING
81                         LDAP_LOG ( OPERATION, DETAIL1, 
82                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
83                         (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
84 #else
85                         Debug( LDAP_DEBUG_TRACE,
86                                 "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
87                                 (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
88 #endif
89
90                         if( is_entry_referral( e ) ) {
91                                 rc = LDAP_OTHER;
92                                 rs->sr_ref = get_entry_referrals( op, e );
93                                 if ( rs->sr_ref ) {
94                                         rs->sr_matched = ber_strdup_x(
95                                         e->e_name.bv_val, op->o_tmpmemctx );
96                                 }
97                         }
98
99                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
100                         e = NULL;
101                 } else if ( default_referral != NULL ) {
102                         rc = LDAP_OTHER;
103                         rs->sr_ref = referral_rewrite( default_referral,
104                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
105                 }
106
107                 if( rs->sr_ref != NULL ) {
108                         /* send referrals */
109                         rs->sr_err = LDAP_REFERRAL;
110                         send_ldap_result( op, rs );
111                         ber_bvarray_free( rs->sr_ref );
112                         rs->sr_ref = NULL;
113                 } else if ( rc != LDAP_SUCCESS ) {
114                         rs->sr_err = rc;
115                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
116                         send_ldap_result( op, rs );
117                 }
118
119                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
120                 if (rs->sr_matched) {
121                         sl_free( (char *)rs->sr_matched, op->o_tmpmemctx );
122                         rs->sr_matched = NULL;
123                 }
124                 return rc;
125         }
126
127         if ( is_entry_referral( e ) ) {
128                 /* entry is a referral */
129                 BerVarray refs = get_entry_referrals( op, e );
130                 rs->sr_ref = referral_rewrite(
131                         refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
132
133 #ifdef NEW_LOGGING
134                 LDAP_LOG ( OPERATION, DETAIL1, 
135                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
136                         (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
137 #else
138                 Debug( LDAP_DEBUG_TRACE,
139                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
140                         (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
141 #endif
142
143                 rs->sr_matched = e->e_name.bv_val;
144                 if( rs->sr_ref != NULL ) {
145                         rs->sr_err = LDAP_REFERRAL;
146                         send_ldap_result( op, rs );
147                         ber_bvarray_free( rs->sr_ref );
148                         rs->sr_ref = NULL;
149                 } else {
150                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
151                         rc = rs->sr_err;
152                 }
153
154                 rs->sr_matched = NULL;
155                 ber_bvarray_free( refs );
156         }
157
158         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
159         LOCK_ID_FREE ( bdb->bi_dbenv, locker );
160         return rc;
161 }