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