Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-bdb / referral.c
blob048d549b4100a7b069be17616b8d4b83169c19e9
1 /* referral.c - BDB backend referral handler */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/referral.c,v 1.42.2.6 2008/04/16 16:41:17 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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>.
17 #include "portable.h"
18 #include <stdio.h>
19 #include <ac/string.h>
21 #include "back-bdb.h"
23 int
24 bdb_referrals( Operation *op, SlapReply *rs )
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;
31 BDB_LOCKER locker;
32 DB_LOCK lock;
34 if( op->o_tag == LDAP_REQ_SEARCH ) {
35 /* let search take care of itself */
36 return rc;
39 if( get_manageDSAit( op ) ) {
40 /* let op take care of DSA management */
41 return rc;
44 rc = LOCK_ID(bdb->bi_dbenv, &locker);
45 switch(rc) {
46 case 0:
47 break;
48 default:
49 return LDAP_OTHER;
52 dn2entry_retry:
53 /* get entry */
54 rc = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1, locker, &lock );
56 /* bdb_dn2entry() may legally leave ei == NULL
57 * if rc != 0 and rc != DB_NOTFOUND
59 if ( ei ) {
60 e = ei->bei_e;
63 switch(rc) {
64 case DB_NOTFOUND:
65 case 0:
66 break;
67 case LDAP_BUSY:
68 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
69 rs->sr_text = "ldap server busy";
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 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
80 rs->sr_text = "internal error";
81 return LDAP_OTHER;
84 if ( rc == DB_NOTFOUND ) {
85 rc = LDAP_SUCCESS;
86 rs->sr_matched = NULL;
87 if ( e != NULL ) {
88 Debug( LDAP_DEBUG_TRACE,
89 LDAP_XSTRING(bdb_referrals)
90 ": tag=%lu target=\"%s\" matched=\"%s\"\n",
91 (unsigned long)op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
93 if( is_entry_referral( e ) ) {
94 BerVarray ref = get_entry_referrals( op, e );
95 rc = LDAP_OTHER;
96 rs->sr_ref = referral_rewrite( ref, &e->e_name,
97 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
98 ber_bvarray_free( ref );
99 if ( rs->sr_ref ) {
100 rs->sr_matched = ber_strdup_x(
101 e->e_name.bv_val, op->o_tmpmemctx );
105 bdb_cache_return_entry_r (bdb, e, &lock);
106 e = NULL;
109 if( rs->sr_ref != NULL ) {
110 /* send referrals */
111 rc = rs->sr_err = LDAP_REFERRAL;
112 send_ldap_result( op, rs );
113 ber_bvarray_free( rs->sr_ref );
114 rs->sr_ref = NULL;
115 } else if ( rc != LDAP_SUCCESS ) {
116 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
119 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
120 if (rs->sr_matched) {
121 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
122 rs->sr_matched = NULL;
124 return rc;
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 );
133 Debug( LDAP_DEBUG_TRACE,
134 LDAP_XSTRING(bdb_referrals)
135 ": tag=%lu target=\"%s\" matched=\"%s\"\n",
136 (unsigned long)op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
138 rs->sr_matched = e->e_name.bv_val;
139 if( rs->sr_ref != NULL ) {
140 rc = rs->sr_err = LDAP_REFERRAL;
141 send_ldap_result( op, rs );
142 ber_bvarray_free( rs->sr_ref );
143 rs->sr_ref = NULL;
144 } else {
145 rc = LDAP_OTHER;
146 rs->sr_text = "bad referral object";
149 rs->sr_matched = NULL;
150 ber_bvarray_free( refs );
153 bdb_cache_return_entry_r(bdb, e, &lock);
154 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
155 return rc;