]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/referral.c
Sync with HEAD
[openldap] / servers / slapd / back-bdb / referral.c
1 /* referral.c - BDB backend referral handler */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 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 #include <stdio.h>
19 #include <ac/string.h>
20
21 #include "back-bdb.h"
22
23 int
24 bdb_referrals( Operation *op, SlapReply *rs )
25 {
26         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
27         Entry *e = NULL;
28         EntryInfo *ei;
29         int rc = LDAP_SUCCESS;
30
31         u_int32_t       locker;
32         DB_LOCK         lock;
33
34         if( op->o_tag == LDAP_REQ_SEARCH ) {
35                 /* let search take care of itself */
36                 return rc;
37         }
38
39         if( get_manageDSAit( op ) ) {
40                 /* let op take care of DSA management */
41                 return rc;
42         } 
43
44         rc = LOCK_ID(bdb->bi_dbenv, &locker);
45         switch(rc) {
46         case 0:
47                 break;
48         default:
49                 return LDAP_OTHER;
50         }
51
52 dn2entry_retry:
53         /* get entry */
54         rc = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1, locker, &lock );
55
56         e = ei->bei_e;
57         switch(rc) {
58         case DB_NOTFOUND:
59         case 0:
60                 break;
61         case LDAP_BUSY:
62                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
63                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
64                 return LDAP_BUSY;
65         case DB_LOCK_DEADLOCK:
66         case DB_LOCK_NOTGRANTED:
67                 goto dn2entry_retry;
68         default:
69                 Debug( LDAP_DEBUG_TRACE,
70                         LDAP_XSTRING(bdb_referrals)
71                         ": dn2entry failed: %s (%d)\n",
72                         db_strerror(rc), rc, 0 ); 
73                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
74                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
75                 return rs->sr_err;
76         }
77
78         if ( rc == DB_NOTFOUND ) {
79                 rc = 0;
80                 rs->sr_matched = NULL;
81                 if ( e != NULL ) {
82                         Debug( LDAP_DEBUG_TRACE,
83                                 LDAP_XSTRING(bdb_referrals)
84                                 ": op=%ld target=\"%s\" matched=\"%s\"\n",
85                                 (long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
86
87                         if( is_entry_referral( e ) ) {
88                                 rc = LDAP_OTHER;
89                                 rs->sr_ref = get_entry_referrals( op, e );
90                                 if ( rs->sr_ref ) {
91                                         rs->sr_matched = ber_strdup_x(
92                                         e->e_name.bv_val, op->o_tmpmemctx );
93                                 }
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                         rc = 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                 if (rs->sr_matched) {
118                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
119                         rs->sr_matched = NULL;
120                 }
121                 return rc;
122         }
123
124         if ( is_entry_referral( e ) ) {
125                 /* entry is a referral */
126                 BerVarray refs = get_entry_referrals( op, e );
127                 rs->sr_ref = referral_rewrite(
128                         refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
129
130                 Debug( LDAP_DEBUG_TRACE,
131                         LDAP_XSTRING(bdb_referrals)
132                         ": op=%ld target=\"%s\" matched=\"%s\"\n",
133                         (long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
134
135                 rs->sr_matched = e->e_name.bv_val;
136                 if( rs->sr_ref != NULL ) {
137                         rc = rs->sr_err = LDAP_REFERRAL;
138                         send_ldap_result( op, rs );
139                         ber_bvarray_free( rs->sr_ref );
140                         rs->sr_ref = NULL;
141                 } else {
142                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
143                         rc = rs->sr_err;
144                 }
145
146                 rs->sr_matched = NULL;
147                 ber_bvarray_free( refs );
148         }
149
150         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
151         LOCK_ID_FREE ( bdb->bi_dbenv, locker );
152         return rc;
153 }