]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/backglue.c
ITS#5186 check for multiple suffixes at config time, not open time
[openldap] / servers / slapd / backglue.c
index 5cd6c42f80a16628ca101583de3d006acf1ccac3..f91f1959101e4d608971b63d8cbeec49eca26477 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
@@ -35,6 +35,7 @@
 
 #define SLAPD_TOOLS
 #include "slap.h"
+#include "config.h"
 
 typedef struct gluenode {
        BackendDB *gn_be;
@@ -65,7 +66,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])) {
@@ -78,13 +79,24 @@ glue_back_select (
 
 
 typedef struct glue_state {
+       char *matched;
+       BerVarray refs;
+       LDAPControl **ctrls;
        int err;
        int matchlen;
-       char *matched;
        int nrefs;
-       BerVarray refs;
+       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 )
 {
@@ -139,6 +151,55 @@ glue_op_response ( Operation *op, SlapReply *rs )
                        gs->nrefs = j;
                        gs->refs = new;
                }
+               if (rs->sr_ctrls) {
+                       int i, j, k;
+                       LDAPControl **newctrls;
+
+                       for (i=0; rs->sr_ctrls[i]; i++);
+
+                       j = gs->nctrls;
+                       if (!j) {
+                               newctrls = ch_malloc((i+1)*sizeof(LDAPControl *));
+                       } else {
+                               /* Forget old pagedResults response if we're sending
+                                * a new one now
+                                */
+                               if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
+                                       int newpage = 0;
+                                       for ( k=0; k<i; k++ ) {
+                                               if ( !strcmp(rs->sr_ctrls[k]->ldctl_oid,
+                                                       LDAP_CONTROL_PAGEDRESULTS )) {
+                                                       newpage = 1;
+                                                       break;
+                                               }
+                                       }
+                                       if ( newpage ) {
+                                               for ( k=0; k<j; k++ ) {
+                                                       if ( !strcmp(gs->ctrls[k]->ldctl_oid,
+                                                               LDAP_CONTROL_PAGEDRESULTS )) {
+                                                                       gs->ctrls[k]->ldctl_oid = NULL;
+                                                                       ldap_control_free( gs->ctrls[k] );
+                                                                       gs->ctrls[k] = gs->ctrls[--j];
+                                                                       gs->ctrls[j] = NULL;
+                                                                       break;
+                                                       }
+                                               }
+                                       }
+                               }
+                               newctrls = ch_realloc(gs->ctrls,
+                                       (j+i+1)*sizeof(LDAPControl *));
+                       }
+                       for (k=0; k<i; j++,k++) {
+                               newctrls[j] = ch_malloc(sizeof(LDAPControl));
+                               *newctrls[j] = *rs->sr_ctrls[k];
+                               if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ))
+                                       ber_dupbv( &newctrls[j]->ldctl_value,
+                                               &rs->sr_ctrls[k]->ldctl_value );
+                       }
+                       newctrls[j] = NULL;
+                       gs->nctrls = j;
+                       gs->ctrls = newctrls;
+               }
        }
        return 0;
 }
@@ -154,6 +215,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) {
@@ -161,6 +227,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;
        }
 
@@ -168,13 +235,25 @@ 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 )
+{
+       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 )
 {
@@ -184,6 +263,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 )
@@ -205,6 +287,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()
@@ -225,18 +310,48 @@ 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 )
 {
        slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
        glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
        BackendDB *b0 = op->o_bd;
-       BackendDB *b1 = NULL, *btmp;
+       BackendDB *btmp;
        BackendInfo *bi0 = op->o_bd->bd_info;
        int i;
-       long stoptime = 0;
-       glue_state gs = {0, 0, NULL, 0, NULL};
-       slap_callback cb = { NULL, glue_op_response, NULL, NULL };
+       long stoptime = 0, starttime;
+       glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
+       slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
        int scope0, tlimit0;
        struct berval dn, ndn, *pdn;
 
@@ -244,6 +359,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);
@@ -251,6 +367,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 );
@@ -260,29 +379,15 @@ 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;
                tlimit0 = op->ors_tlimit;
                dn = op->o_req_dn;
                ndn = op->o_req_ndn;
-               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 ) {
@@ -294,10 +399,21 @@ glue_op_search ( Operation *op, SlapReply *rs )
                        }
                        if (!btmp || !btmp->be_search)
                                continue;
-                       if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
+                       if (!dnIsSuffix(&btmp->be_nsuffix[0], &b0->be_nsuffix[0]))
+                               continue;
+                       if (get_no_subordinate_glue(op) && btmp != b0)
+                               continue;
+                       /* If we remembered which backend we were on before,
+                        * skip down to it now
+                        */
+                       if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
+                               op->o_conn->c_pagedresults_state.ps_be &&
+                               op->o_conn->c_pagedresults_state.ps_be != btmp )
                                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;
@@ -325,18 +441,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;
                                }
@@ -344,7 +463,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 ) {
@@ -361,7 +480,41 @@ glue_op_search ( Operation *op, SlapReply *rs )
                        case LDAP_X_CANNOT_CHAIN:
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
                                goto end_of_loop;
-                       
+
+                       case LDAP_SUCCESS:
+                               if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
+                                       PagedResultsState *ps = op->o_pagedresults_state;
+
+                                       /* Assume this backend can be forgotten now */
+                                       op->o_conn->c_pagedresults_state.ps_be = NULL;
+
+                                       /* If we have a full page, exit the loop. We may
+                                        * need to remember this backend so we can continue
+                                        * from here on a subsequent request.
+                                        */
+                                       if ( rs->sr_nentries >= ps->ps_size ) {
+                                               /* Don't bother to remember the first backend.
+                                                * Only remember the last one if there's more state left.
+                                                */
+                                               if ( op->o_bd != b0 &&
+                                                       ( op->o_conn->c_pagedresults_state.ps_cookie ||
+                                                       op->o_bd != gi->gi_n[0].gn_be ))
+                                                       op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
+                                               goto end_of_loop;
+                                       }
+
+                                       /* This backend has run out of entries, but more responses
+                                        * can fit in the page. Fake a reset of the state so the
+                                        * next backend will start up properly. Only back-[bh]db
+                                        * and back-sql look at this state info.
+                                        */
+                                       if ( ps->ps_cookieval.bv_len == sizeof( PagedResultsCookie )) {
+                                               ps->ps_cookie = 0;
+                                               memset( ps->ps_cookieval.bv_val, 0,
+                                                       sizeof( PagedResultsCookie ));
+                                       }
+                               }
+                               
                        default:
                                break;
                        }
@@ -369,6 +522,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;
 
@@ -381,6 +535,7 @@ end_of_loop:;
                rs->sr_err = gs.err;
                rs->sr_matched = gs.matched;
                rs->sr_ref = gs.refs;
+               rs->sr_ctrls = gs.ctrls;
 
                send_ldap_result( op, rs );
        }
@@ -391,6 +546,14 @@ end_of_loop:;
                free (gs.matched);
        if (gs.refs)
                ber_bvarray_free(gs.refs);
+       if (gs.ctrls) {
+               for (i = gs.nctrls; --i >= 0; ) {
+                       if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
+                               free(gs.ctrls[i]->ldctl_value.bv_val);
+                       free(gs.ctrls[i]);
+               }
+               free(gs.ctrls);
+       }
        return rs->sr_err;
 }
 
@@ -486,7 +649,7 @@ glue_open (
                                        gi->gi_n[i].gn_be->bd_info );
                        /* Let backend.c take care of the rest of startup */
                        if ( !rc )
-                               rc = backend_startup_one( gi->gi_n[i].gn_be );
+                               rc = backend_startup_one( gi->gi_n[i].gn_be, NULL );
                        if ( rc ) break;
                }
                if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
@@ -515,6 +678,28 @@ glue_close (
        return rc;
 }
 
+static int
+glue_entry_get_rw (
+       Operation               *op,
+       struct berval   *dn,
+       ObjectClass             *oc,
+       AttributeDescription    *ad,
+       int     rw,
+       Entry   **e )
+{
+       int rc;
+       BackendDB *b0 = op->o_bd;
+       op->o_bd = glue_back_select( b0, dn );
+
+       if ( op->o_bd->be_fetch ) {
+               rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
+       } else {
+               rc = LDAP_UNWILLING_TO_PERFORM;
+       }
+       op->o_bd =b0;
+       return rc;
+}
+
 static int
 glue_entry_release_rw (
        Operation *op,
@@ -522,13 +707,10 @@ glue_entry_release_rw (
        int rw
 )
 {
-       BackendDB *b0, b2;
+       BackendDB *b0 = op->o_bd;
        int rc = -1;
 
-       b0 = op->o_bd;
-       b2 = *op->o_bd;
-       b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
-       op->o_bd = glue_back_select (&b2, &e->e_nname);
+       op->o_bd = glue_back_select (b0, &e->e_nname);
 
        if ( op->o_bd->be_release ) {
                rc = op->o_bd->be_release( op, e, rw );
@@ -667,13 +849,14 @@ glue_tool_entry_put (
 static int
 glue_tool_entry_reindex (
        BackendDB *b0,
-       ID id
+       ID id,
+       AttributeDescription **adv
 )
 {
        if (!glueBack || !glueBack->be_entry_reindex)
                return -1;
 
-       return glueBack->be_entry_reindex (glueBack, id);
+       return glueBack->be_entry_reindex (glueBack, id, adv);
 }
 
 static int
@@ -699,7 +882,8 @@ glue_tool_sync (
 
 static int
 glue_db_init(
-       BackendDB *be
+       BackendDB *be,
+       ConfigReply *cr
 )
 {
        slap_overinst   *on = (slap_overinst *)be->bd_info;
@@ -707,6 +891,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 );
@@ -717,8 +908,6 @@ glue_db_init(
        oi->oi_bi.bi_open = glue_open;
        oi->oi_bi.bi_close = glue_close;
 
-       oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
-
        /* Only advertise these if the root DB supports them */
        if ( bi->bi_tool_entry_open )
                oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
@@ -739,7 +928,6 @@ glue_db_init(
 
        /*FIXME : need to add support */
        oi->oi_bi.bi_tool_dn2id_get = 0;
-       oi->oi_bi.bi_tool_id2entry_get = 0;
        oi->oi_bi.bi_tool_entry_modify = 0;
 
        SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
@@ -749,7 +937,8 @@ glue_db_init(
 
 static int
 glue_db_destroy (
-       BackendDB *be
+       BackendDB *be,
+       ConfigReply *cr
 )
 {
        slap_overinst   *on = (slap_overinst *)be->bd_info;
@@ -761,7 +950,8 @@ glue_db_destroy (
 
 static int
 glue_db_close( 
-       BackendDB *be
+       BackendDB *be,
+       ConfigReply *cr
 )
 {
        slap_overinst   *on = (slap_overinst *)be->bd_info;
@@ -851,7 +1041,7 @@ glue_sub_attach()
 
                        /* If it's not already configured, set up the overlay */
                        if ( !SLAP_GLUE_INSTANCE( be )) {
-                               rc = overlay_config( be, glue.on_bi.bi_type );
+                               rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL );
                                if ( rc )
                                        break;
                        }
@@ -892,6 +1082,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;
@@ -921,9 +1117,14 @@ 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_bi.bi_entry_get_rw = glue_entry_get_rw;
+       glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
+
+       glue.on_response = glue_response;
 
        return overlay_register( &glue );
 }