]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/backglue.c
fix previous commit (ITS#4861)
[openldap] / servers / slapd / backglue.c
index 7b14360eff665a2af33f6402bfee67446fcc9fc9..f0ab5ca02dadc5bbd6231aee7ada458dc32fa8d7 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2001-2006 The OpenLDAP Foundation.
+ * Copyright 2001-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -65,7 +65,7 @@ glue_back_select (
        glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
        int i;
 
-       for (i = 0; i<gi->gi_nodes; i++) {
+       for (i = gi->gi_nodes-1; i >= 0; i--) {
                assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
 
                if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
@@ -87,6 +87,15 @@ typedef struct glue_state {
        int nctrls;
 } glue_state;
 
+static int
+glue_op_cleanup( Operation *op, SlapReply *rs )
+{
+       /* This is not a final result */
+       if (rs->sr_type == REP_RESULT )
+               rs->sr_type = REP_GLUE_RESULT;
+       return SLAP_CB_CONTINUE;
+}
+
 static int
 glue_op_response ( Operation *op, SlapReply *rs )
 {
@@ -180,6 +189,11 @@ glue_op_func ( Operation *op, SlapReply *rs )
        int rc;
 
        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
+
+       /* If we're on the master backend, let overlay framework handle it */
+       if ( op->o_bd == b0 )
+               return SLAP_CB_CONTINUE;
+
        b0->bd_info = on->on_info->oi_orig;
 
        switch(op->o_tag) {
@@ -187,6 +201,7 @@ glue_op_func ( Operation *op, SlapReply *rs )
        case LDAP_REQ_DELETE: which = op_delete; break;
        case LDAP_REQ_MODIFY: which = op_modify; break;
        case LDAP_REQ_MODRDN: which = op_modrdn; break;
+       case LDAP_REQ_EXTENDED: which = op_extended; break;
        default: assert( 0 ); break;
        }
 
@@ -194,13 +209,26 @@ glue_op_func ( Operation *op, SlapReply *rs )
        if ( func[which] )
                rc = func[which]( op, rs );
        else
-               rc = SLAP_CB_CONTINUE;
+               rc = SLAP_CB_BYPASS;
 
        op->o_bd = b0;
        op->o_bd->bd_info = bi0;
        return rc;
 }
 
+static int
+glue_response ( Operation *op, SlapReply *rs )
+{
+       slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
+       BackendDB *be = op->o_bd;
+       be = glue_back_select (op->o_bd, &op->o_req_ndn);
+
+       /* If we're on the master backend, let overlay framework handle it.
+        * Otherwise, bail out.
+        */
+       return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
+}
+
 static int
 glue_chk_referrals ( Operation *op, SlapReply *rs )
 {
@@ -210,6 +238,9 @@ glue_chk_referrals ( Operation *op, SlapReply *rs )
        int rc;
 
        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
+       if ( op->o_bd == b0 )
+               return SLAP_CB_CONTINUE;
+
        b0->bd_info = on->on_info->oi_orig;
 
        if ( op->o_bd->bd_info->bi_chk_referrals )
@@ -231,6 +262,9 @@ glue_chk_controls ( Operation *op, SlapReply *rs )
        int rc = SLAP_CB_CONTINUE;
 
        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
+       if ( op->o_bd == b0 )
+               return SLAP_CB_CONTINUE;
+
        b0->bd_info = on->on_info->oi_orig;
 
        /* if the subordinate database has overlays, the bi_chk_controls()
@@ -251,6 +285,36 @@ glue_chk_controls ( Operation *op, SlapReply *rs )
        return rc;
 }
 
+/* ITS#4615 - overlays configured above the glue overlay should be
+ * invoked for the entire glued tree. Overlays configured below the
+ * glue overlay should only be invoked on the master backend.
+ * So, if we're searching on any subordinates, we need to force the
+ * current overlay chain to stop processing, without stopping the
+ * overall callback flow.
+ */
+static int
+glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
+       slap_overinst *on )
+{
+       /* Process any overlays on the master backend */
+       if ( op->o_bd == b0 && on->on_next ) {
+               BackendInfo *bi = op->o_bd->bd_info;
+               int rc = SLAP_CB_CONTINUE;
+               for ( on=on->on_next; on; on=on->on_next ) {
+                       op->o_bd->bd_info = (BackendInfo *)on;
+                       if ( on->on_bi.bi_op_search ) {
+                               rc = on->on_bi.bi_op_search( op, rs );
+                               if ( rc != SLAP_CB_CONTINUE )
+                                       break;
+                       }
+               }
+               op->o_bd->bd_info = bi;
+               if ( rc != SLAP_CB_CONTINUE )
+                       return rc;
+       }
+       return op->o_bd->be_search( op, rs );
+}
+
 static int
 glue_op_search ( Operation *op, SlapReply *rs )
 {
@@ -260,9 +324,9 @@ glue_op_search ( Operation *op, SlapReply *rs )
        BackendDB *b1 = NULL, *btmp;
        BackendInfo *bi0 = op->o_bd->bd_info;
        int i;
-       long stoptime = 0;
+       long stoptime = 0, starttime;
        glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
-       slap_callback cb = { NULL, glue_op_response, NULL, NULL };
+       slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
        int scope0, tlimit0;
        struct berval dn, ndn, *pdn;
 
@@ -270,6 +334,7 @@ glue_op_search ( Operation *op, SlapReply *rs )
 
        cb.sc_next = op->o_callback;
 
+       starttime = op->o_time;
        stoptime = slap_get_time () + op->ors_tlimit;
 
        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
@@ -277,6 +342,9 @@ glue_op_search ( Operation *op, SlapReply *rs )
 
        switch (op->ors_scope) {
        case LDAP_SCOPE_BASE:
+               if ( op->o_bd == b0 )
+                       return SLAP_CB_CONTINUE;
+
                rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                if (op->o_bd && op->o_bd->be_search) {
                        rs->sr_err = op->o_bd->be_search( op, rs );
@@ -286,19 +354,6 @@ glue_op_search ( Operation *op, SlapReply *rs )
        case LDAP_SCOPE_ONELEVEL:
        case LDAP_SCOPE_SUBTREE:
        case LDAP_SCOPE_SUBORDINATE: /* FIXME */
-
-#if 0
-               if ( op->o_sync ) {
-                       if (op->o_bd && op->o_bd->be_search) {
-                               rs->sr_err = op->o_bd->be_search( op, rs );
-                       } else {
-                               send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
-                                               "No search target found");
-                       }
-                       return rs->sr_err;
-               }
-#endif
-
                op->o_callback = &cb;
                rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
                scope0 = op->ors_scope;
@@ -308,7 +363,7 @@ glue_op_search ( Operation *op, SlapReply *rs )
                b1 = op->o_bd;
 
                /*
-                * Execute in reverse order, most general first 
+                * Execute in reverse order, most specific first 
                 */
                for (i = gi->gi_nodes; i >= 0; i--) {
                        if ( i == gi->gi_nodes ) {
@@ -323,7 +378,8 @@ glue_op_search ( Operation *op, SlapReply *rs )
                        if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
                                continue;
                        if (tlimit0 != SLAP_NO_LIMIT) {
-                               op->ors_tlimit = stoptime - slap_get_time ();
+                               op->o_time = slap_get_time();
+                               op->ors_tlimit = stoptime - op->o_time;
                                if (op->ors_tlimit <= 0) {
                                        rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
                                        break;
@@ -351,18 +407,21 @@ glue_op_search ( Operation *op, SlapReply *rs )
                                if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
                                        gs.err = LDAP_SUCCESS;
                                }
+                               op->ors_scope = LDAP_SCOPE_ONELEVEL;
+                               op->o_req_dn = dn;
+                               op->o_req_ndn = ndn;
 
                        } else if (scope0 == LDAP_SCOPE_SUBTREE &&
                                dn_match(&op->o_bd->be_nsuffix[0], &ndn))
                        {
-                               rs->sr_err = op->o_bd->be_search( op, rs );
+                               rs->sr_err = glue_sub_search( op, rs, b0, on );
 
                        } else if (scope0 == LDAP_SCOPE_SUBTREE &&
                                dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
                        {
                                op->o_req_dn = op->o_bd->be_suffix[0];
                                op->o_req_ndn = op->o_bd->be_nsuffix[0];
-                               rs->sr_err = op->o_bd->be_search( op, rs );
+                               rs->sr_err = glue_sub_search( op, rs, b0, on );
                                if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
                                        gs.err = LDAP_SUCCESS;
                                }
@@ -370,7 +429,7 @@ glue_op_search ( Operation *op, SlapReply *rs )
                                op->o_req_ndn = ndn;
 
                        } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
-                               rs->sr_err = op->o_bd->be_search( op, rs );
+                               rs->sr_err = glue_sub_search( op, rs, b0, on );
                        }
 
                        switch ( gs.err ) {
@@ -395,6 +454,7 @@ glue_op_search ( Operation *op, SlapReply *rs )
 end_of_loop:;
                op->ors_scope = scope0;
                op->ors_tlimit = tlimit0;
+               op->o_time = starttime;
                op->o_req_dn = dn;
                op->o_req_ndn = ndn;
 
@@ -742,6 +802,13 @@ glue_db_init(
        BackendInfo     *bi = oi->oi_orig;
        glueinfo *gi;
 
+       if ( SLAP_GLUE_SUBORDINATE( be )) {
+               Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
+                       "cannot have glue overlay!\n",
+                       be->be_suffix[0].bv_val, 0, 0 );
+               return LDAP_OTHER;
+       }
+
        gi = ch_calloc( 1, sizeof(glueinfo));
        on->on_bi.bi_private = gi;
        dnParent( be->be_nsuffix, &gi->gi_pdn );
@@ -801,7 +868,7 @@ glue_db_close(
 {
        slap_overinst   *on = (slap_overinst *)be->bd_info;
 
-       on->on_info->oi_bi.bi_db_close = NULL;
+       on->on_info->oi_bi.bi_db_close = 0;
        return 0;
 }
 
@@ -927,6 +994,12 @@ glue_sub_add( BackendDB *be, int advert, int online )
        glue_Addrec *ga;
        int rc = 0;
 
+       if ( overlay_is_inst( be, "glue" )) {
+               Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
+                       "cannot be a subordinate!\n",
+                       be->be_suffix[0].bv_val, 0, 0 );
+               return LDAP_OTHER;
+       }
        SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
        if ( advert )
                SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
@@ -956,9 +1029,11 @@ glue_sub_init()
        glue.on_bi.bi_op_modrdn = glue_op_func;
        glue.on_bi.bi_op_add = glue_op_func;
        glue.on_bi.bi_op_delete = glue_op_func;
+       glue.on_bi.bi_extended = glue_op_func;
 
        glue.on_bi.bi_chk_referrals = glue_chk_referrals;
        glue.on_bi.bi_chk_controls = glue_chk_controls;
+       glue.on_response = glue_response;
 
        return overlay_register( &glue );
 }