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 (dn_issuffixbv (&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,
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 dn_issuffixbv (be->be_nsuffix[0], &bv)) {
338 rc = be->be_search (be, conn, op,
340 be->be_nsuffix[0]->bv_val,
342 s2limit, t2limit, filter, filterstr,
344 } else if (dn_issuffixbv (&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);
614 glue_tool_entry_open (
619 /* We don't know which backend to talk to yet, so just
620 * remember the mode and move on...
630 glue_tool_entry_close (
637 if (!glueBack->be_entry_close)
639 rc = glueBack->be_entry_close (glueBack);
645 glue_tool_entry_first (
649 glueinfo *gi = (glueinfo *) b0->be_private;
652 /* If we're starting from scratch, start at the most general */
654 for (i = gi->nodes-1; i >= 0; i--) {
655 if (gi->n[i].be->be_entry_open &&
656 gi->n[i].be->be_entry_first) {
657 glueBack = gi->n[i].be;
663 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
666 return glueBack->be_entry_first (glueBack);
670 glue_tool_entry_next (
674 glueinfo *gi = (glueinfo *) b0->be_private;
678 rc = glueBack->be_entry_next (glueBack);
680 /* If we ran out of entries in one database, move on to the next */
682 glueBack->be_entry_close (glueBack);
683 for (i=0; i<gi->nodes; i++) {
684 if (gi->n[i].be == glueBack)
691 glueBack = gi->n[i-1].be;
692 rc = glue_tool_entry_first (b0);
699 glue_tool_entry_get (
704 return glueBack->be_entry_get (glueBack, id);
708 glue_tool_entry_put (
716 be = glue_back_select (b0, e->e_ndn);
717 if (!be->be_entry_put)
721 rc = be->be_entry_open (be, glueMode);
724 } else if (be != glueBack) {
725 /* If this entry belongs in a different branch than the
726 * previous one, close the current database and open the
729 glueBack->be_entry_close (glueBack);
730 rc = be->be_entry_open (be, glueMode);
735 return be->be_entry_put (be, e);
739 glue_tool_entry_reindex (
744 if (!glueBack->be_entry_reindex)
747 return glueBack->be_entry_reindex (glueBack, id);
755 glueinfo *gi = (glueinfo *) b0->be_private;
758 /* just sync everyone */
759 for (i = 0; b0->be_nsuffix[i]; i++)
760 if (gi->n[i].be->be_sync)
761 gi->n[i].be->be_sync (gi->n[i].be);
765 extern int num_subs; /* config.c */
776 /* While there are subordinate backends, search backwards through the
777 * backends and connect them to their superior.
779 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
780 if (b1->be_glueflags & SLAP_GLUE_SUBORDINATE) {
781 /* The last database cannot be a subordinate of noone */
782 if (i == nBackendDB - 1)
783 b1->be_glueflags ^= SLAP_GLUE_SUBORDINATE;
787 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
788 if (!(be->be_glueflags & SLAP_GLUE_SUBORDINATE))
790 /* We will only link it once */
791 if (be->be_glueflags & SLAP_GLUE_LINKED)
793 if (!dn_issuffixbv(be->be_nsuffix[0],
797 be->be_glueflags |= SLAP_GLUE_LINKED;
799 /* We create a copy of the superior's be
800 * structure, pointing to all of its original
801 * information. Then we replace elements of
802 * the superior's info with our own. The copy
803 * is used whenever we have operations to pass
804 * down to the real database.
806 b1->be_glueflags |= SLAP_GLUE_INSTANCE;
807 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
808 gi->be = (BackendDB *)ch_malloc(sizeof(BackendDB) + sizeof(BackendInfo));
809 bi = (BackendInfo *)(gi->be+1);
813 bi->bi_open = glue_back_open;
814 bi->bi_close = glue_back_close;
815 bi->bi_db_open = glue_back_db_open;
816 bi->bi_db_close = glue_back_db_close;
817 bi->bi_db_destroy = glue_back_db_destroy;
819 bi->bi_op_bind = glue_back_bind;
820 bi->bi_op_search = glue_back_search;
821 bi->bi_op_compare = glue_back_compare;
822 bi->bi_op_modify = glue_back_modify;
823 bi->bi_op_modrdn = glue_back_modrdn;
824 bi->bi_op_add = glue_back_add;
825 bi->bi_op_delete = glue_back_delete;
827 bi->bi_entry_release_rw = glue_back_release_rw;
828 bi->bi_acl_group = glue_back_group;
829 bi->bi_acl_attribute = glue_back_attribute;
830 bi->bi_chk_referrals = glue_back_referrals;
833 * hooks for slap tools
835 bi->bi_tool_entry_open = glue_tool_entry_open;
836 bi->bi_tool_entry_close = glue_tool_entry_close;
837 bi->bi_tool_entry_first = glue_tool_entry_first;
838 bi->bi_tool_entry_next = glue_tool_entry_next;
839 bi->bi_tool_entry_get = glue_tool_entry_get;
840 bi->bi_tool_entry_put = glue_tool_entry_put;
841 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
842 bi->bi_tool_sync = glue_tool_sync;
844 gi = (glueinfo *)ch_realloc(gi,
846 gi->nodes * sizeof(gluenode));
848 gi->n[gi->nodes].be = be;
849 gi->n[gi->nodes].pdn = dn_parent(NULL,
850 be->be_nsuffix[0]->bv_val);
854 /* One more node for the master */
855 gi = (glueinfo *)ch_realloc(gi,
856 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
857 gi->n[gi->nodes].be = gi->be;
858 gi->n[gi->nodes].pdn = dn_parent(NULL,
859 b1->be_nsuffix[0]->bv_val);
865 /* If there are any unresolved subordinates left, something is wrong */