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 * The only configuration items that are needed for this backend are
14 * the suffixes, and they should be identical to suffixes of other
15 * backends that are being configured. The suffixes must be listed
16 * in order from longest to shortest, (most-specific to least-specific)
17 * in order for the selection to work. Every backend that is being glued
18 * must be fully configured as usual.
20 * The purpose of this backend is to allow you to split a single database
21 * into pieces (for load balancing purposes, whatever) but still be able
22 * to treat it as a single database after it's been split. As such, each
23 * of the glued backends should have identical rootdn and rootpw.
25 * If you need more elaborate configuration, you probably should be using
34 #include <ac/socket.h>
39 typedef struct gluenode {
44 typedef struct glueinfo {
49 /* Just like select_backend, but only for our backends */
56 glueinfo *gi = (glueinfo *) be->be_private;
59 for (i = 0; be->be_nsuffix[i]; i++) {
60 if (dn_issuffix (dn, be->be_nsuffix[i]->bv_val))
81 for (i = 0; be->be_nsuffix[i]; i++);
83 gi = (struct glueinfo *) ch_calloc (1, sizeof (glueinfo) +
84 (i-1) * sizeof (gluenode) );
92 be->be_glueflags = SLAP_GLUE_INSTANCE;
95 * For each of our suffixes, find the real backend that handles this
98 for (i = 0; be->be_nsuffix[i]; i++) {
99 for (j = 0; j < nbackends; j++) {
100 if (be == &backends[j])
103 for (k = 0; backends[j].be_nsuffix &&
104 backends[j].be_nsuffix[k]; k++) {
105 if (be->be_nsuffix[i]->bv_len !=
106 backends[j].be_nsuffix[k]->bv_len)
108 if (!strcmp (backends[j].be_nsuffix[k]->bv_val,
109 be->be_nsuffix[i]->bv_val)) {
115 gi->n[i].be = &backends[j];
116 gi->n[i].pdn = dn_parent (NULL,
117 be->be_nsuffix[i]->bv_val);
118 if (i < gi->nodes - 1)
119 gi->n[i].be->be_glueflags =
120 SLAP_GLUE_SUBORDINATE;
126 /* If we were invoked in tool mode, open all the underlying backends */
127 if (slapMode & SLAP_TOOL_MODE) {
128 for (i = 0; be->be_nsuffix[i]; i++) {
129 backend_startup (gi->n[i].be);
140 glueinfo *gi = (glueinfo *) be->be_private;
142 if (slapMode & SLAP_TOOL_MODE) {
144 for (i = 0; be->be_nsuffix[i]; i++) {
145 backend_shutdown (gi->n[i].be);
151 glue_back_db_destroy (
155 free (be->be_private);
174 be = glue_back_select (b0, ndn);
176 if (be && be->be_bind) {
177 conn->c_authz_backend = be;
178 rc = be->be_bind (be, conn, op, dn, ndn, method, cred, edn);
180 rc = LDAP_UNWILLING_TO_PERFORM;
181 send_ldap_result (conn, op, rc, NULL, "No bind target found",
187 typedef struct glue_state {
193 struct berval **refs;
207 struct berval *resdata,
208 struct berval *sasldata,
212 glue_state *gs = op->o_glue;
214 if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
216 if (gs->err == LDAP_SUCCESS && gs->matched) {
220 if (gs->err != LDAP_SUCCESS && matched) {
222 len = strlen (matched);
223 if (len > gs->matchlen) {
226 gs->matched = ch_strdup (matched);
234 for (i=0; ref[i]; i++);
238 new = ch_malloc ((i+1)*sizeof(struct berval *));
241 new = ch_realloc(gs->refs,
242 (j+i+1)*sizeof(struct berval *));
244 for (k=0; k<i; j++,k++) {
245 new[j] = ber_bvdup(ref[k]);
260 struct berval **refs,
265 glue_state *gs = op->o_glue;
267 gs->nentries += nentries;
268 glue_back_response (c, op, 0, 0, err, matched, text, refs,
269 NULL, NULL, NULL, ctrls);
284 const char *filterstr,
289 glueinfo *gi = (glueinfo *) b0->be_private;
291 int i, rc, t2limit = 0, s2limit = 0;
297 stoptime = slap_get_time () + tlimit;
300 case LDAP_SCOPE_BASE:
301 be = glue_back_select (b0, ndn);
303 if (be && be->be_search) {
304 rc = be->be_search (be, conn, op, dn, ndn, scope,
305 deref, slimit, tlimit, filter, filterstr,
308 rc = LDAP_UNWILLING_TO_PERFORM;
309 send_ldap_result (conn, op, rc, NULL,
310 "No search target found", NULL, NULL);
314 case LDAP_SCOPE_ONELEVEL:
316 op->o_sresult = glue_back_sresult;
317 op->o_response = glue_back_response;
320 * Execute in reverse order, most general first
322 for (i = gi->nodes-1; i >= 0; i--) {
323 if (!gi->n[i].be->be_search)
326 t2limit = stoptime - slap_get_time ();
331 s2limit = slimit - gs.nentries;
338 ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
340 ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
345 if (!strcmp (gi->n[i].pdn, ndn)) {
347 rc = be->be_search (be, conn, op,
349 b0->be_nsuffix[i]->bv_val,
350 LDAP_SCOPE_BASE, deref,
351 s2limit, t2limit, filter, filterstr,
353 } else if (dn_issuffix (ndn, b0->be_nsuffix[i]->bv_val)) {
355 rc = be->be_search (be, conn, op,
356 dn, ndn, scope, deref,
357 s2limit, t2limit, filter, filterstr,
363 case LDAP_SCOPE_SUBTREE:
365 op->o_sresult = glue_back_sresult;
366 op->o_response = glue_back_response;
369 * Execute in reverse order, most general first
371 for (i = gi->nodes-1; i >= 0; i--) {
372 if (!gi->n[i].be->be_search)
375 t2limit = stoptime - slap_get_time ();
380 s2limit = slimit - gs.nentries;
387 ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
389 ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
394 if (dn_issuffix (ndn, b0->be_nsuffix[i]->bv_val)) {
396 rc = be->be_search (be, conn, op,
397 dn, ndn, scope, deref,
398 s2limit, t2limit, filter, filterstr,
400 } else if (dn_issuffix (b0->be_nsuffix[i]->bv_val, ndn)) {
402 rc = be->be_search (be, conn, op,
404 b0->be_nsuffix[i]->bv_val,
406 s2limit, t2limit, filter, filterstr,
412 op->o_sresult = NULL;
413 op->o_response = NULL;
416 send_search_result (conn, op, gs.err, gs.matched, NULL, gs.refs,
422 ber_bvecfree(gs.refs);
433 AttributeAssertion *ava
439 be = glue_back_select (b0, ndn);
441 if (be && be->be_compare) {
442 rc = be->be_compare (be, conn, op, dn, ndn, ava);
444 rc = LDAP_UNWILLING_TO_PERFORM;
445 send_ldap_result (conn, op, rc, NULL, "No compare target found",
464 be = glue_back_select (b0, ndn);
466 if (be && be->be_modify) {
467 rc = be->be_modify (be, conn, op, dn, ndn, mod);
469 rc = LDAP_UNWILLING_TO_PERFORM;
470 send_ldap_result (conn, op, rc, NULL, "No modify target found",
491 be = glue_back_select (b0, ndn);
493 if (be && be->be_modrdn) {
494 rc = be->be_modrdn (be, conn, op, dn, ndn, newrdn, del, newsup);
496 rc = LDAP_UNWILLING_TO_PERFORM;
497 send_ldap_result (conn, op, rc, NULL, "No modrdn target found",
514 be = glue_back_select (b0, e->e_ndn);
516 if (be && be->be_add) {
517 rc = be->be_add (be, conn, op, e);
519 rc = LDAP_UNWILLING_TO_PERFORM;
520 send_ldap_result (conn, op, rc, NULL, "No add target found",
538 be = glue_back_select (b0, ndn);
540 if (be && be->be_delete) {
541 rc = be->be_delete (be, conn, op, dn, ndn);
543 rc = LDAP_UNWILLING_TO_PERFORM;
544 send_ldap_result (conn, op, rc, NULL, "No delete target found",
551 glue_back_release_rw (
562 be = glue_back_select (b0, e->e_ndn);
564 if (be && be->be_release) {
565 rc = be->be_release (be, conn, op, e, rw);
582 AttributeDescription * ad
588 be = glue_back_select (b0, ndn);
590 if (be && be->be_group) {
591 rc = be->be_group (be, conn, op, target, ndn, ondn, oc, ad);
593 rc = LDAP_UNWILLING_TO_PERFORM;
599 glue_back_attribute (
605 AttributeDescription *ad,
606 struct berval ***vals
612 be = glue_back_select (b0, ndn);
614 if (be && be->be_attribute) {
615 rc = be->be_attribute (be, conn, op, target, ndn, ad, vals);
617 rc = LDAP_UNWILLING_TO_PERFORM;
623 glue_back_referrals (
635 be = glue_back_select (b0, ndn);
637 if (be && be->be_chk_referrals) {
638 rc = be->be_chk_referrals (be, conn, op, dn, ndn, text);
649 glue_tool_entry_open (
654 /* We don't know which backend to talk to yet, so just
655 * remember the mode and move on...
665 glue_tool_entry_close (
669 glueinfo *gi = (glueinfo *) b0->be_private;
674 if (!gi->n[i].be->be_entry_close)
676 rc = gi->n[i].be->be_entry_close (gi->n[i].be);
683 glue_tool_entry_first (
687 glueinfo *gi = (glueinfo *) b0->be_private;
690 /* If we're starting from scratch, start at the most general */
691 if (glueBack == -1) {
692 for (i = gi->nodes-1; i >= 0; i--) {
693 if (gi->n[i].be->be_entry_open &&
694 gi->n[i].be->be_entry_first)
700 if (gi->n[i].be->be_entry_open (gi->n[i].be, glueMode) != 0)
704 return gi->n[i].be->be_entry_first (gi->n[i].be);
708 glue_tool_entry_next (
712 glueinfo *gi = (glueinfo *) b0->be_private;
716 rc = gi->n[i].be->be_entry_next (gi->n[i].be);
718 /* If we ran out of entries in one database, move on to the next */
720 gi->n[i].be->be_entry_close (gi->n[i].be);
726 rc = glue_tool_entry_first (b0);
732 glue_tool_entry_get (
737 glueinfo *gi = (glueinfo *) b0->be_private;
740 return gi->n[i].be->be_entry_get (gi->n[i].be, id);
744 glue_tool_entry_put (
749 glueinfo *gi = (glueinfo *) b0->be_private;
753 be = glue_back_select (b0, e->e_ndn);
754 if (!be->be_entry_put)
759 rc = be->be_entry_open (be, glueMode);
763 } else if (be != gi->n[i].be) {
764 /* If this entry belongs in a different branch than the
765 * previous one, close the current database and open the
768 gi->n[i].be->be_entry_close (gi->n[i].be);
770 for (i = 0; b0->be_nsuffix[i]; i++)
771 if (gi->n[i].be == be)
773 rc = be->be_entry_open (be, glueMode);
778 return be->be_entry_put (be, e);
782 glue_tool_entry_reindex (
787 glueinfo *gi = (glueinfo *) b0->be_private;
790 if (!gi->n[i].be->be_entry_reindex)
793 return gi->n[i].be->be_entry_reindex (gi->n[i].be, id);
801 glueinfo *gi = (glueinfo *) b0->be_private;
804 /* just sync everyone */
805 for (i = 0; b0->be_nsuffix[i]; i++)
806 if (gi->n[i].be->be_sync)
807 gi->n[i].be->be_sync (gi->n[i].be);
812 glue_back_initialize (
822 bi->bi_db_config = 0;
823 bi->bi_db_open = glue_back_db_open;
824 bi->bi_db_close = glue_back_db_close;
825 bi->bi_db_destroy = glue_back_db_destroy;
827 bi->bi_op_bind = glue_back_bind;
828 bi->bi_op_unbind = 0;
829 bi->bi_op_search = glue_back_search;
830 bi->bi_op_compare = glue_back_compare;
831 bi->bi_op_modify = glue_back_modify;
832 bi->bi_op_modrdn = glue_back_modrdn;
833 bi->bi_op_add = glue_back_add;
834 bi->bi_op_delete = glue_back_delete;
835 bi->bi_op_abandon = 0;
839 bi->bi_entry_release_rw = glue_back_release_rw;
840 bi->bi_acl_group = glue_back_group;
841 bi->bi_acl_attribute = glue_back_attribute;
842 bi->bi_chk_referrals = glue_back_referrals;
845 * hooks for slap tools
847 bi->bi_tool_entry_open = glue_tool_entry_open;
848 bi->bi_tool_entry_close = glue_tool_entry_close;
849 bi->bi_tool_entry_first = glue_tool_entry_first;
850 bi->bi_tool_entry_next = glue_tool_entry_next;
851 bi->bi_tool_entry_get = glue_tool_entry_get;
852 bi->bi_tool_entry_put = glue_tool_entry_put;
853 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
854 bi->bi_tool_sync = glue_tool_sync;
856 bi->bi_connection_init = 0;
857 bi->bi_connection_destroy = 0;