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;
60 bv.bv_len = strlen(dn);
61 bv.bv_val = (char *) dn;
63 for (i = 0; be->be_nsuffix[i]; i++) {
64 if (dn_issuffixbv (&bv, be->be_nsuffix[i]))
85 for (i = 0; be->be_nsuffix[i]; i++);
87 gi = (struct glueinfo *) ch_calloc (1, sizeof (glueinfo) +
88 (i-1) * sizeof (gluenode) );
96 be->be_glueflags = SLAP_GLUE_INSTANCE;
99 * For each of our suffixes, find the real backend that handles this
102 for (i = 0; be->be_nsuffix[i]; i++) {
103 for (j = 0; j < nbackends; j++) {
104 if (be == &backends[j])
107 for (k = 0; backends[j].be_nsuffix &&
108 backends[j].be_nsuffix[k]; k++) {
109 if (be->be_nsuffix[i]->bv_len !=
110 backends[j].be_nsuffix[k]->bv_len)
112 if (!strcmp (backends[j].be_nsuffix[k]->bv_val,
113 be->be_nsuffix[i]->bv_val)) {
119 gi->n[i].be = &backends[j];
120 gi->n[i].pdn = dn_parent (NULL,
121 be->be_nsuffix[i]->bv_val);
122 if (i < gi->nodes - 1)
123 gi->n[i].be->be_glueflags =
124 SLAP_GLUE_SUBORDINATE;
130 /* If we were invoked in tool mode, open all the underlying backends */
131 if (slapMode & SLAP_TOOL_MODE) {
132 for (i = 0; be->be_nsuffix[i]; i++) {
133 backend_startup (gi->n[i].be);
144 glueinfo *gi = (glueinfo *) be->be_private;
146 if (slapMode & SLAP_TOOL_MODE) {
148 for (i = 0; be->be_nsuffix[i]; i++) {
149 backend_shutdown (gi->n[i].be);
155 glue_back_db_destroy (
159 free (be->be_private);
178 be = glue_back_select (b0, ndn);
180 if (be && be->be_bind) {
181 conn->c_authz_backend = be;
182 rc = be->be_bind (be, conn, op, dn, ndn, method, cred, edn);
184 rc = LDAP_UNWILLING_TO_PERFORM;
185 send_ldap_result (conn, op, rc, NULL, "No bind target found",
191 typedef struct glue_state {
197 struct berval **refs;
211 struct berval *resdata,
212 struct berval *sasldata,
216 glue_state *gs = op->o_glue;
218 if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
220 if (gs->err == LDAP_SUCCESS && gs->matched) {
224 if (gs->err != LDAP_SUCCESS && matched) {
226 len = strlen (matched);
227 if (len > gs->matchlen) {
230 gs->matched = ch_strdup (matched);
238 for (i=0; ref[i]; i++);
242 new = ch_malloc ((i+1)*sizeof(struct berval *));
244 new = ch_realloc(gs->refs,
245 (j+i+1)*sizeof(struct berval *));
247 for (k=0; k<i; j++,k++) {
248 new[j] = ber_bvdup(ref[k]);
263 struct berval **refs,
268 glue_state *gs = op->o_glue;
270 gs->nentries += nentries;
271 glue_back_response (c, op, 0, 0, err, matched, text, refs,
272 NULL, NULL, NULL, ctrls);
287 const char *filterstr,
292 glueinfo *gi = (glueinfo *) b0->be_private;
294 int i, rc, t2limit = 0, s2limit = 0;
301 stoptime = slap_get_time () + tlimit;
304 case LDAP_SCOPE_BASE:
305 be = glue_back_select (b0, ndn);
307 if (be && be->be_search) {
308 rc = be->be_search (be, conn, op, dn, ndn, scope,
309 deref, slimit, tlimit, filter, filterstr,
312 rc = LDAP_UNWILLING_TO_PERFORM;
313 send_ldap_result (conn, op, rc, NULL,
314 "No search target found", NULL, NULL);
318 case LDAP_SCOPE_ONELEVEL:
319 case LDAP_SCOPE_SUBTREE:
321 op->o_sresult = glue_back_sresult;
322 op->o_response = glue_back_response;
323 bv.bv_len = strlen(ndn);
324 bv.bv_val = (char *) ndn;
327 * Execute in reverse order, most general first
329 for (i = gi->nodes-1; i >= 0; i--) {
330 if (!gi->n[i].be || !gi->n[i].be->be_search)
333 t2limit = stoptime - slap_get_time ();
338 s2limit = slimit - gs.nentries;
345 ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
347 ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
353 if (scope == LDAP_SCOPE_ONELEVEL &&
354 !strcmp (gi->n[i].pdn, ndn)) {
355 rc = be->be_search (be, conn, op,
357 b0->be_nsuffix[i]->bv_val,
358 LDAP_SCOPE_BASE, deref,
359 s2limit, t2limit, filter, filterstr,
361 } else if (scope == LDAP_SCOPE_SUBTREE &&
362 dn_issuffixbv (b0->be_nsuffix[i], &bv)) {
363 rc = be->be_search (be, conn, op,
365 b0->be_nsuffix[i]->bv_val,
367 s2limit, t2limit, filter, filterstr,
369 } else if (dn_issuffixbv (&bv, b0->be_nsuffix[i])) {
370 rc = be->be_search (be, conn, op,
371 dn, ndn, scope, deref,
372 s2limit, t2limit, filter, filterstr,
378 op->o_sresult = NULL;
379 op->o_response = NULL;
382 send_search_result (conn, op, gs.err, gs.matched, NULL, gs.refs,
388 ber_bvecfree(gs.refs);
399 AttributeAssertion *ava
405 be = glue_back_select (b0, ndn);
407 if (be && be->be_compare) {
408 rc = be->be_compare (be, conn, op, dn, ndn, ava);
410 rc = LDAP_UNWILLING_TO_PERFORM;
411 send_ldap_result (conn, op, rc, NULL, "No compare target found",
430 be = glue_back_select (b0, ndn);
432 if (be && be->be_modify) {
433 rc = be->be_modify (be, conn, op, dn, ndn, mod);
435 rc = LDAP_UNWILLING_TO_PERFORM;
436 send_ldap_result (conn, op, rc, NULL, "No modify target found",
457 be = glue_back_select (b0, ndn);
459 if (be && be->be_modrdn) {
460 rc = be->be_modrdn (be, conn, op, dn, ndn, newrdn, del, newsup);
462 rc = LDAP_UNWILLING_TO_PERFORM;
463 send_ldap_result (conn, op, rc, NULL, "No modrdn target found",
480 be = glue_back_select (b0, e->e_ndn);
482 if (be && be->be_add) {
483 rc = be->be_add (be, conn, op, e);
485 rc = LDAP_UNWILLING_TO_PERFORM;
486 send_ldap_result (conn, op, rc, NULL, "No add target found",
504 be = glue_back_select (b0, ndn);
506 if (be && be->be_delete) {
507 rc = be->be_delete (be, conn, op, dn, ndn);
509 rc = LDAP_UNWILLING_TO_PERFORM;
510 send_ldap_result (conn, op, rc, NULL, "No delete target found",
517 glue_back_release_rw (
528 be = glue_back_select (b0, e->e_ndn);
530 if (be && be->be_release) {
531 rc = be->be_release (be, conn, op, e, rw);
548 AttributeDescription * ad
554 be = glue_back_select (b0, ndn);
556 if (be && be->be_group) {
557 rc = be->be_group (be, conn, op, target, ndn, ondn, oc, ad);
559 rc = LDAP_UNWILLING_TO_PERFORM;
565 glue_back_attribute (
571 AttributeDescription *ad,
572 struct berval ***vals
578 be = glue_back_select (b0, ndn);
580 if (be && be->be_attribute) {
581 rc = be->be_attribute (be, conn, op, target, ndn, ad, vals);
583 rc = LDAP_UNWILLING_TO_PERFORM;
589 glue_back_referrals (
601 be = glue_back_select (b0, ndn);
603 if (be && be->be_chk_referrals) {
604 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 (
635 glueinfo *gi = (glueinfo *) b0->be_private;
640 if (!gi->n[i].be->be_entry_close)
642 rc = gi->n[i].be->be_entry_close (gi->n[i].be);
649 glue_tool_entry_first (
653 glueinfo *gi = (glueinfo *) b0->be_private;
656 /* If we're starting from scratch, start at the most general */
657 if (glueBack == -1) {
658 for (i = gi->nodes-1; i >= 0; i--) {
659 if (gi->n[i].be->be_entry_open &&
660 gi->n[i].be->be_entry_first)
666 if (gi->n[i].be->be_entry_open (gi->n[i].be, glueMode) != 0)
670 return gi->n[i].be->be_entry_first (gi->n[i].be);
674 glue_tool_entry_next (
678 glueinfo *gi = (glueinfo *) b0->be_private;
682 rc = gi->n[i].be->be_entry_next (gi->n[i].be);
684 /* If we ran out of entries in one database, move on to the next */
686 gi->n[i].be->be_entry_close (gi->n[i].be);
692 rc = glue_tool_entry_first (b0);
698 glue_tool_entry_get (
703 glueinfo *gi = (glueinfo *) b0->be_private;
706 return gi->n[i].be->be_entry_get (gi->n[i].be, id);
710 glue_tool_entry_put (
715 glueinfo *gi = (glueinfo *) b0->be_private;
719 be = glue_back_select (b0, e->e_ndn);
720 if (!be->be_entry_put)
725 rc = be->be_entry_open (be, glueMode);
729 } else if (be != gi->n[i].be) {
730 /* If this entry belongs in a different branch than the
731 * previous one, close the current database and open the
734 gi->n[i].be->be_entry_close (gi->n[i].be);
736 for (i = 0; b0->be_nsuffix[i]; i++)
737 if (gi->n[i].be == be)
739 rc = be->be_entry_open (be, glueMode);
744 return be->be_entry_put (be, e);
748 glue_tool_entry_reindex (
753 glueinfo *gi = (glueinfo *) b0->be_private;
756 if (!gi->n[i].be->be_entry_reindex)
759 return gi->n[i].be->be_entry_reindex (gi->n[i].be, id);
767 glueinfo *gi = (glueinfo *) b0->be_private;
770 /* just sync everyone */
771 for (i = 0; b0->be_nsuffix[i]; i++)
772 if (gi->n[i].be->be_sync)
773 gi->n[i].be->be_sync (gi->n[i].be);
778 glue_back_initialize (
788 bi->bi_db_config = 0;
789 bi->bi_db_open = glue_back_db_open;
790 bi->bi_db_close = glue_back_db_close;
791 bi->bi_db_destroy = glue_back_db_destroy;
793 bi->bi_op_bind = glue_back_bind;
794 bi->bi_op_unbind = 0;
795 bi->bi_op_search = glue_back_search;
796 bi->bi_op_compare = glue_back_compare;
797 bi->bi_op_modify = glue_back_modify;
798 bi->bi_op_modrdn = glue_back_modrdn;
799 bi->bi_op_add = glue_back_add;
800 bi->bi_op_delete = glue_back_delete;
801 bi->bi_op_abandon = 0;
805 bi->bi_entry_release_rw = glue_back_release_rw;
806 bi->bi_acl_group = glue_back_group;
807 bi->bi_acl_attribute = glue_back_attribute;
808 bi->bi_chk_referrals = glue_back_referrals;
811 * hooks for slap tools
813 bi->bi_tool_entry_open = glue_tool_entry_open;
814 bi->bi_tool_entry_close = glue_tool_entry_close;
815 bi->bi_tool_entry_first = glue_tool_entry_first;
816 bi->bi_tool_entry_next = glue_tool_entry_next;
817 bi->bi_tool_entry_get = glue_tool_entry_get;
818 bi->bi_tool_entry_put = glue_tool_entry_put;
819 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
820 bi->bi_tool_sync = glue_tool_sync;
822 bi->bi_connection_init = 0;
823 bi->bi_connection_destroy = 0;