]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/referral.c
cleanup & more
[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         Entry *matched = NULL;
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_r( op->o_bd, NULL, &op->o_req_ndn, &e, &matched, 0, locker, &lock );
47
48         switch(rc) {
49         case DB_NOTFOUND:
50                 rc = 0;
51         case 0:
52                 break;
53         case LDAP_BUSY:
54                 if (e != NULL) {
55                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
56                 }
57                 if (matched != NULL) {
58                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
59                 }
60                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
61                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
62                 return LDAP_BUSY;
63         case DB_LOCK_DEADLOCK:
64         case DB_LOCK_NOTGRANTED:
65                 goto dn2entry_retry;
66         default:
67 #ifdef NEW_LOGGING
68                 LDAP_LOG ( OPERATION, ERR, 
69                         "bdb_referrals: dn2entry failed: %s (%d)\n", 
70                         db_strerror(rc), rc, 0 );
71 #else
72                 Debug( LDAP_DEBUG_TRACE,
73                         "bdb_referrals: dn2entry failed: %s (%d)\n",
74                         db_strerror(rc), rc, 0 ); 
75 #endif
76                 if (e != NULL) {
77                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
78                 }
79                 if (matched != NULL) {
80                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
81                 }
82                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
83                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
84                 return rs->sr_err;
85         }
86
87         if ( e == NULL ) {
88                 if ( matched != NULL ) {
89                         rs->sr_matched = ch_strdup( matched->e_name.bv_val );
90
91 #ifdef NEW_LOGGING
92                 LDAP_LOG ( OPERATION, DETAIL1, 
93                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
94                         (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
95 #else
96                         Debug( LDAP_DEBUG_TRACE,
97                                 "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
98                                 (long) op->o_tag, op->o_req_dn.bv_val, rs->sr_matched );
99 #endif
100
101                         if( is_entry_referral( matched ) ) {
102                                 rc = LDAP_OTHER;
103                                 rs->sr_ref = get_entry_referrals( op, matched );
104                         }
105
106                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
107                         matched = NULL;
108                 } else if ( default_referral != NULL ) {
109                         rc = LDAP_OTHER;
110                         rs->sr_ref = referral_rewrite( default_referral,
111                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
112                 }
113
114                 if( rs->sr_ref != NULL ) {
115                         /* send referrals */
116                         rs->sr_err = LDAP_REFERRAL;
117                         send_ldap_result( op, rs );
118                         ber_bvarray_free( rs->sr_ref );
119                         rs->sr_ref = NULL;
120                 } else if ( rc != LDAP_SUCCESS ) {
121                         rs->sr_err = rc;
122                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
123                         send_ldap_result( op, rs );
124                 }
125
126                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
127                 free( (char *)rs->sr_matched );
128                 rs->sr_matched = NULL;
129                 return rc;
130         }
131
132         if ( is_entry_referral( e ) ) {
133                 /* entry is a referral */
134                 BerVarray refs = get_entry_referrals( op, e );
135                 rs->sr_ref = referral_rewrite(
136                         refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
137
138 #ifdef NEW_LOGGING
139                 LDAP_LOG ( OPERATION, DETAIL1, 
140                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
141                         (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
142 #else
143                 Debug( LDAP_DEBUG_TRACE,
144                         "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
145                         (long) op->o_tag, op->o_req_dn.bv_val, e->e_dn );
146 #endif
147
148                 rs->sr_matched = e->e_name.bv_val;
149                 if( rs->sr_ref != NULL ) {
150                         rs->sr_err = LDAP_REFERRAL;
151                         send_ldap_result( op, rs );
152                         ber_bvarray_free( rs->sr_ref );
153                         rs->sr_ref = NULL;
154                 } else {
155                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
156                         rc = rs->sr_err;
157                 }
158
159                 rs->sr_matched = NULL;
160                 ber_bvarray_free( refs );
161         }
162
163         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
164         LOCK_ID_FREE ( bdb->bi_dbenv, locker );
165         return rc;
166 }