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