]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/referral.c
Unify use of BDB lockers
[openldap] / servers / slapd / back-bdb / referral.c
index ea9dcfd86d533bd9119b18c53b98591f000017d3..d31056eebbc91bd0e28ea1e6ed1983e532ceaad7 100644 (file)
@@ -1,8 +1,17 @@
 /* referral.c - BDB backend referral handler */
 /* $OpenLDAP$ */
-/*
- * Copyright 2000-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2000-2007 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
@@ -10,7 +19,6 @@
 #include <ac/string.h>
 
 #include "back-bdb.h"
-#include "external.h"
 
 int
 bdb_referrals( Operation *op, SlapReply *rs )
@@ -43,10 +51,15 @@ bdb_referrals( Operation *op, SlapReply *rs )
 
 dn2entry_retry:
        /* get entry */
-       rc = bdb_dn2entry( op->o_bd, NULL, &op->o_req_ndn, &ei, 1, locker,
-               &lock, op->o_tmpmemctx );
+       rc = bdb_dn2entry( op, NULL, &op->o_req_ndn, &ei, 1, locker, &lock );
+
+       /* bdb_dn2entry() may legally leave ei == NULL
+        * if rc != 0 and rc != DB_NOTFOUND
+        */
+       if ( ei ) {
+               e = ei->bei_e;
+       }
 
-       e = ei->bei_e;
        switch(rc) {
        case DB_NOTFOUND:
        case 0:
@@ -59,15 +72,10 @@ dn2entry_retry:
        case DB_LOCK_NOTGRANTED:
                goto dn2entry_retry;
        default:
-#ifdef NEW_LOGGING
-               LDAP_LOG ( OPERATION, ERR, 
-                       "bdb_referrals: dn2entry failed: %s (%d)\n", 
-                       db_strerror(rc), rc, 0 );
-#else
                Debug( LDAP_DEBUG_TRACE,
-                       "bdb_referrals: dn2entry failed: %s (%d)\n",
+                       LDAP_XSTRING(bdb_referrals)
+                       ": dn2entry failed: %s (%d)\n",
                        db_strerror(rc), rc, 0 ); 
-#endif
                send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
                LOCK_ID_FREE ( bdb->bi_dbenv, locker );
                return rs->sr_err;
@@ -77,28 +85,26 @@ dn2entry_retry:
                rc = 0;
                rs->sr_matched = NULL;
                if ( e != NULL ) {
-#ifdef NEW_LOGGING
-                       LDAP_LOG ( OPERATION, DETAIL1, 
-                       "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
-                       (long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
-#else
                        Debug( LDAP_DEBUG_TRACE,
-                               "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
+                               LDAP_XSTRING(bdb_referrals)
+                               ": op=%ld target=\"%s\" matched=\"%s\"\n",
                                (long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
-#endif
 
                        if( is_entry_referral( e ) ) {
+                               BerVarray ref = get_entry_referrals( op, e );
                                rc = LDAP_OTHER;
-                               rs->sr_ref = get_entry_referrals( op, e );
+                               rs->sr_ref = referral_rewrite( ref, NULL,
+                                       &op->o_req_dn, LDAP_SCOPE_DEFAULT );
+                               ber_bvarray_free( ref );
                                if ( rs->sr_ref ) {
                                        rs->sr_matched = ber_strdup_x(
                                        e->e_name.bv_val, op->o_tmpmemctx );
                                }
                        }
 
-                       bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
+                       bdb_cache_return_entry_r (bdb, e, &lock);
                        e = NULL;
-               } else if ( default_referral != NULL ) {
+               } else if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) && default_referral != NULL ) {
                        rc = LDAP_OTHER;
                        rs->sr_ref = referral_rewrite( default_referral,
                                NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
@@ -106,7 +112,7 @@ dn2entry_retry:
 
                if( rs->sr_ref != NULL ) {
                        /* send referrals */
-                       rs->sr_err = LDAP_REFERRAL;
+                       rc = rs->sr_err = LDAP_REFERRAL;
                        send_ldap_result( op, rs );
                        ber_bvarray_free( rs->sr_ref );
                        rs->sr_ref = NULL;
@@ -118,7 +124,7 @@ dn2entry_retry:
 
                LOCK_ID_FREE ( bdb->bi_dbenv, locker );
                if (rs->sr_matched) {
-                       sl_free( (char *)rs->sr_matched, op->o_tmpmemctx );
+                       op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
                        rs->sr_matched = NULL;
                }
                return rc;
@@ -130,19 +136,14 @@ dn2entry_retry:
                rs->sr_ref = referral_rewrite(
                        refs, &e->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
 
-#ifdef NEW_LOGGING
-               LDAP_LOG ( OPERATION, DETAIL1, 
-                       "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
-                       (long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
-#else
                Debug( LDAP_DEBUG_TRACE,
-                       "bdb_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
+                       LDAP_XSTRING(bdb_referrals)
+                       ": op=%ld target=\"%s\" matched=\"%s\"\n",
                        (long) op->o_tag, op->o_req_dn.bv_val, e->e_name.bv_val );
-#endif
 
                rs->sr_matched = e->e_name.bv_val;
                if( rs->sr_ref != NULL ) {
-                       rs->sr_err = LDAP_REFERRAL;
+                       rc = rs->sr_err = LDAP_REFERRAL;
                        send_ldap_result( op, rs );
                        ber_bvarray_free( rs->sr_ref );
                        rs->sr_ref = NULL;
@@ -155,7 +156,7 @@ dn2entry_retry:
                ber_bvarray_free( refs );
        }
 
-       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
+       bdb_cache_return_entry_r(bdb, e, &lock);
        LOCK_ID_FREE ( bdb->bi_dbenv, locker );
        return rc;
 }