1 /* backglue.c - backend glue routines */
4 * Copyright 2001 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 * Functions to glue a bunch of other backends into a single tree.
10 * All of the glued backends must share a common suffix. E.g., you
11 * can glue o=foo and ou=bar,o=foo but you can't glue o=foo and o=bar.
13 * This uses the backend structures and routines extensively, but is
14 * not an actual backend of its own. To use it you must add a "subordinate"
15 * keyword to the configuration of other backends. Subordinates will
16 * automatically be connected to their parent backend.
18 * The purpose of these functions is to allow you to split a single database
19 * into pieces (for load balancing purposes, whatever) but still be able
20 * to treat it as a single database after it's been split. As such, each
21 * of the glued backends should have identical rootdn and rootpw.
23 * If you need more elaborate configuration, you probably should be using
32 #include <ac/socket.h>
37 typedef struct gluenode {
42 typedef struct glueinfo {
49 static BackendDB *glueBack;
51 /* Just like select_backend, but only for our backends */
58 glueinfo *gi = (glueinfo *) be->be_private;
62 bv.bv_len = strlen(dn);
63 bv.bv_val = (char *) dn;
65 for (i = 0; i<gi->nodes; i++) {
66 if (dnIsSuffix(&bv, gi->n[i].be->be_nsuffix[0])) {
73 /* This function will only be called in tool mode */
80 static int glueOpened = 0;
82 if (glueOpened) return 0;
86 /* If we were invoked in tool mode, open all the underlying backends */
87 if (slapMode & SLAP_TOOL_MODE) {
88 rc = backend_startup (NULL);
89 } /* other case is impossible */
93 /* This function will only be called in tool mode */
99 static int glueClosed = 0;
102 if (glueClosed) return 0;
106 if (slapMode & SLAP_TOOL_MODE) {
107 rc = backend_shutdown (NULL);
117 glueinfo *gi = (glueinfo *)be->be_private;
118 static int glueOpened = 0;
121 if (glueOpened) return 0;
125 gi->be->be_acl = be->be_acl;
127 if (gi->be->bd_info->bi_db_open)
128 rc = gi->be->bd_info->bi_db_open(gi->be);
138 glueinfo *gi = (glueinfo *)be->be_private;
139 static int glueClosed = 0;
141 if (glueClosed) return 0;
145 /* Close the master */
146 if (gi->be->bd_info->bi_db_close)
147 gi->be->bd_info->bi_db_close( gi->be );
153 glue_back_db_destroy (
157 glueinfo *gi = (glueinfo *)be->be_private;
159 if (gi->be->bd_info->bi_db_destroy)
160 gi->be->bd_info->bi_db_destroy( gi->be );
166 typedef struct glue_state {
172 struct berval **refs;
186 struct berval *resdata,
187 struct berval *sasldata,
191 glue_state *gs = op->o_glue;
193 if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
195 if (gs->err == LDAP_SUCCESS && gs->matched) {
199 if (gs->err != LDAP_SUCCESS && matched) {
201 len = strlen (matched);
202 if (len > gs->matchlen) {
205 gs->matched = ch_strdup (matched);
213 for (i=0; ref[i]; i++);
217 new = ch_malloc ((i+1)*sizeof(struct berval *));
219 new = ch_realloc(gs->refs,
220 (j+i+1)*sizeof(struct berval *));
222 for (k=0; k<i; j++,k++) {
223 new[j] = ber_bvdup(ref[k]);
238 struct berval **refs,
243 glue_state *gs = op->o_glue;
245 gs->nentries += nentries;
246 glue_back_response (c, op, 0, 0, err, matched, text, refs,
247 NULL, NULL, NULL, ctrls);
262 const char *filterstr,
267 glueinfo *gi = (glueinfo *)b0->be_private;
269 int i, rc, t2limit = 0, s2limit = 0;
276 stoptime = slap_get_time () + tlimit;
279 case LDAP_SCOPE_BASE:
280 be = glue_back_select (b0, ndn);
282 if (be && be->be_search) {
283 rc = be->be_search (be, conn, op, dn, ndn, scope,
284 deref, slimit, tlimit, filter, filterstr,
287 rc = LDAP_UNWILLING_TO_PERFORM;
288 send_ldap_result (conn, op, rc, NULL,
289 "No search target found", NULL, NULL);
293 case LDAP_SCOPE_ONELEVEL:
294 case LDAP_SCOPE_SUBTREE:
296 op->o_sresult = glue_back_sresult;
297 op->o_response = glue_back_response;
298 bv.bv_len = strlen(ndn);
299 bv.bv_val = (char *) ndn;
302 * Execute in reverse order, most general first
304 for (i = gi->nodes-1; i >= 0; i--) {
305 if (!gi->n[i].be || !gi->n[i].be->be_search)
308 t2limit = stoptime - slap_get_time ();
313 s2limit = slimit - gs.nentries;
320 ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
322 ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
328 if (scope == LDAP_SCOPE_ONELEVEL &&
329 !strcmp (gi->n[i].pdn, ndn)) {
330 rc = be->be_search (be, conn, op,
331 be->be_suffix[0]->bv_val,
332 be->be_nsuffix[0]->bv_val,
333 LDAP_SCOPE_BASE, deref,
334 s2limit, t2limit, filter, filterstr,
336 } else if (scope == LDAP_SCOPE_SUBTREE &&
337 dnIsSuffix(be->be_nsuffix[0], &bv)) {
338 rc = be->be_search (be, conn, op,
339 be->be_suffix[0]->bv_val,
340 be->be_nsuffix[0]->bv_val,
342 s2limit, t2limit, filter, filterstr,
344 } else if (dnIsSuffix(&bv, be->be_nsuffix[0])) {
345 rc = be->be_search (be, conn, op,
346 dn, ndn, scope, deref,
347 s2limit, t2limit, filter, filterstr,
353 op->o_sresult = NULL;
354 op->o_response = NULL;
357 send_search_result (conn, op, gs.err, gs.matched, NULL, gs.refs,
363 ber_bvecfree(gs.refs);
382 be = glue_back_select (b0, ndn);
384 if (be && be->be_bind) {
385 conn->c_authz_backend = be;
386 rc = be->be_bind (be, conn, op, dn, ndn, method, cred, edn);
388 rc = LDAP_UNWILLING_TO_PERFORM;
389 send_ldap_result (conn, op, rc, NULL, "No bind target found",
402 AttributeAssertion *ava
408 be = glue_back_select (b0, ndn);
410 if (be && be->be_compare) {
411 rc = be->be_compare (be, conn, op, dn, ndn, ava);
413 rc = LDAP_UNWILLING_TO_PERFORM;
414 send_ldap_result (conn, op, rc, NULL, "No compare target found",
433 be = glue_back_select (b0, ndn);
435 if (be && be->be_modify) {
436 rc = be->be_modify (be, conn, op, dn, ndn, mod);
438 rc = LDAP_UNWILLING_TO_PERFORM;
439 send_ldap_result (conn, op, rc, NULL, "No modify target found",
460 be = glue_back_select (b0, ndn);
462 if (be && be->be_modrdn) {
463 rc = be->be_modrdn (be, conn, op, dn, ndn, newrdn, del, newsup);
465 rc = LDAP_UNWILLING_TO_PERFORM;
466 send_ldap_result (conn, op, rc, NULL, "No modrdn target found",
483 be = glue_back_select (b0, e->e_ndn);
485 if (be && be->be_add) {
486 rc = be->be_add (be, conn, op, e);
488 rc = LDAP_UNWILLING_TO_PERFORM;
489 send_ldap_result (conn, op, rc, NULL, "No add target found",
507 be = glue_back_select (b0, ndn);
509 if (be && be->be_delete) {
510 rc = be->be_delete (be, conn, op, dn, ndn);
512 rc = LDAP_UNWILLING_TO_PERFORM;
513 send_ldap_result (conn, op, rc, NULL, "No delete target found",
520 glue_back_release_rw (
531 be = glue_back_select (b0, e->e_ndn);
533 if (be && be->be_release) {
534 rc = be->be_release (be, conn, op, e, rw);
551 AttributeDescription * ad
557 be = glue_back_select (b0, ndn);
559 if (be && be->be_group) {
560 rc = be->be_group (be, conn, op, target, ndn, ondn, oc, ad);
562 rc = LDAP_UNWILLING_TO_PERFORM;
568 glue_back_attribute (
574 AttributeDescription *ad,
575 struct berval ***vals
581 be = glue_back_select (b0, ndn);
583 if (be && be->be_attribute) {
584 rc = be->be_attribute (be, conn, op, target, ndn, ad, vals);
586 rc = LDAP_UNWILLING_TO_PERFORM;
592 glue_back_referrals (
604 be = glue_back_select (b0, ndn);
606 if (be && be->be_chk_referrals) {
607 rc = be->be_chk_referrals (be, conn, op, dn, ndn, text);
615 glue_tool_entry_open (
620 /* We don't know which backend to talk to yet, so just
621 * remember the mode and move on...
631 glue_tool_entry_close (
638 if (!glueBack->be_entry_close)
640 rc = glueBack->be_entry_close (glueBack);
646 glue_tool_entry_first (
650 glueinfo *gi = (glueinfo *) b0->be_private;
653 /* If we're starting from scratch, start at the most general */
655 for (i = gi->nodes-1; i >= 0; i--) {
656 if (gi->n[i].be->be_entry_open &&
657 gi->n[i].be->be_entry_first) {
658 glueBack = gi->n[i].be;
664 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
667 return glueBack->be_entry_first (glueBack);
671 glue_tool_entry_next (
675 glueinfo *gi = (glueinfo *) b0->be_private;
679 if (!glueBack || !glueBack->be_entry_next)
682 rc = glueBack->be_entry_next (glueBack);
684 /* If we ran out of entries in one database, move on to the next */
686 glueBack->be_entry_close (glueBack);
687 for (i=0; i<gi->nodes; i++) {
688 if (gi->n[i].be == glueBack)
695 glueBack = gi->n[i-1].be;
696 rc = glue_tool_entry_first (b0);
703 glue_tool_entry_get (
708 if (!glueBack || !glueBack->be_entry_get)
711 return glueBack->be_entry_get (glueBack, id);
715 glue_tool_entry_put (
723 be = glue_back_select (b0, e->e_ndn);
724 if (!be->be_entry_put)
728 rc = be->be_entry_open (be, glueMode);
731 } else if (be != glueBack) {
732 /* If this entry belongs in a different branch than the
733 * previous one, close the current database and open the
736 glueBack->be_entry_close (glueBack);
737 rc = be->be_entry_open (be, glueMode);
742 return be->be_entry_put (be, e);
746 glue_tool_entry_reindex (
751 if (!glueBack || !glueBack->be_entry_reindex)
754 return glueBack->be_entry_reindex (glueBack, id);
762 glueinfo *gi = (glueinfo *) b0->be_private;
765 /* just sync everyone */
766 for (i = 0; b0->be_nsuffix[i]; i++)
767 if (gi->n[i].be->be_sync)
768 gi->n[i].be->be_sync (gi->n[i].be);
781 /* While there are subordinate backends, search backwards through the
782 * backends and connect them to their superior.
784 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
785 if (b1->be_glueflags & SLAP_GLUE_SUBORDINATE) {
786 /* The last database cannot be a subordinate of noone */
787 if (i == nBackendDB - 1)
788 b1->be_glueflags ^= SLAP_GLUE_SUBORDINATE;
792 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
793 if (!(be->be_glueflags & SLAP_GLUE_SUBORDINATE))
795 /* We will only link it once */
796 if (be->be_glueflags & SLAP_GLUE_LINKED)
798 if (!dnIsSuffix(be->be_nsuffix[0],
802 be->be_glueflags |= SLAP_GLUE_LINKED;
804 /* We create a copy of the superior's be
805 * structure, pointing to all of its original
806 * information. Then we replace elements of
807 * the superior's info with our own. The copy
808 * is used whenever we have operations to pass
809 * down to the real database.
811 b1->be_glueflags |= SLAP_GLUE_INSTANCE;
812 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
813 gi->be = (BackendDB *)ch_malloc(sizeof(BackendDB) + sizeof(BackendInfo));
814 bi = (BackendInfo *)(gi->be+1);
818 bi->bi_open = glue_back_open;
819 bi->bi_close = glue_back_close;
820 bi->bi_db_open = glue_back_db_open;
821 bi->bi_db_close = glue_back_db_close;
822 bi->bi_db_destroy = glue_back_db_destroy;
824 bi->bi_op_bind = glue_back_bind;
825 bi->bi_op_search = glue_back_search;
826 bi->bi_op_compare = glue_back_compare;
827 bi->bi_op_modify = glue_back_modify;
828 bi->bi_op_modrdn = glue_back_modrdn;
829 bi->bi_op_add = glue_back_add;
830 bi->bi_op_delete = glue_back_delete;
832 bi->bi_entry_release_rw = glue_back_release_rw;
833 bi->bi_acl_group = glue_back_group;
834 bi->bi_acl_attribute = glue_back_attribute;
835 bi->bi_chk_referrals = glue_back_referrals;
838 * hooks for slap tools
840 bi->bi_tool_entry_open = glue_tool_entry_open;
841 bi->bi_tool_entry_close = glue_tool_entry_close;
842 bi->bi_tool_entry_first = glue_tool_entry_first;
843 bi->bi_tool_entry_next = glue_tool_entry_next;
844 bi->bi_tool_entry_get = glue_tool_entry_get;
845 bi->bi_tool_entry_put = glue_tool_entry_put;
846 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
847 bi->bi_tool_sync = glue_tool_sync;
849 gi = (glueinfo *)ch_realloc(gi,
851 gi->nodes * sizeof(gluenode));
853 gi->n[gi->nodes].be = be;
854 gi->n[gi->nodes].pdn = dn_parent(NULL,
855 be->be_nsuffix[0]->bv_val);
859 /* One more node for the master */
860 gi = (glueinfo *)ch_realloc(gi,
861 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
862 gi->n[gi->nodes].be = gi->be;
863 gi->n[gi->nodes].pdn = dn_parent(NULL,
864 b1->be_nsuffix[0]->bv_val);
870 /* If there are any unresolved subordinates left, something is wrong */