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 {
205 struct berval *resdata,
206 struct berval *sasldata,
210 glue_state *gs = op->o_glue;
212 if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
214 if (gs->err == LDAP_SUCCESS && gs->matched) {
218 if (gs->err != LDAP_SUCCESS && matched) {
220 len = strlen (matched);
221 if (len > gs->matchlen) {
224 gs->matched = ch_strdup (matched);
237 struct berval **refs,
242 glue_state *gs = op->o_glue;
244 gs->nentries += nentries;
245 glue_back_response (c, op, 0, 0, err, matched, text, refs,
246 NULL, NULL, NULL, ctrls);
261 const char *filterstr,
266 glueinfo *gi = (glueinfo *) b0->be_private;
268 int i, rc, t2limit = 0, s2limit = 0;
275 stoptime = slap_get_time () + tlimit;
278 case LDAP_SCOPE_BASE:
279 be = glue_back_select (b0, ndn);
281 if (be && be->be_search) {
282 rc = be->be_search (be, conn, op, dn, ndn, scope,
283 deref, slimit, tlimit, filter, filterstr,
286 rc = LDAP_UNWILLING_TO_PERFORM;
287 send_ldap_result (conn, op, rc, NULL,
288 "No search target found", NULL, NULL);
292 case LDAP_SCOPE_ONELEVEL:
294 op->o_sresult = glue_back_sresult;
295 op->o_response = glue_back_response;
298 * Execute in reverse order, most general first
300 for (i = gi->nodes-1; i >= 0; i--) {
301 if (!gi->n[i].be->be_search)
304 t2limit = stoptime - slap_get_time ();
309 s2limit = slimit - gs.nentries;
316 ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
318 ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
323 if (!strcmp (gi->n[i].pdn, ndn)) {
325 rc = be->be_search (be, conn, op,
327 b0->be_nsuffix[i]->bv_val,
328 LDAP_SCOPE_BASE, deref,
329 s2limit, t2limit, filter, filterstr,
331 } else if (dn_issuffix (ndn, b0->be_nsuffix[i]->bv_val)) {
333 rc = be->be_search (be, conn, op,
334 dn, ndn, scope, deref,
335 s2limit, t2limit, filter, filterstr,
341 case LDAP_SCOPE_SUBTREE:
343 op->o_sresult = glue_back_sresult;
344 op->o_response = glue_back_response;
347 * Execute in reverse order, most general first
349 for (i = gi->nodes-1; i >= 0; i--) {
350 if (!gi->n[i].be->be_search)
353 t2limit = stoptime - slap_get_time ();
358 s2limit = slimit - gs.nentries;
365 ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
367 ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
372 if (dn_issuffix (ndn, b0->be_nsuffix[i]->bv_val)) {
374 rc = be->be_search (be, conn, op,
375 dn, ndn, scope, deref,
376 s2limit, t2limit, filter, filterstr,
378 } else if (dn_issuffix (b0->be_nsuffix[i]->bv_val, ndn)) {
380 rc = be->be_search (be, conn, op,
382 b0->be_nsuffix[i]->bv_val,
384 s2limit, t2limit, filter, filterstr,
390 op->o_sresult = NULL;
391 op->o_response = NULL;
394 send_search_result (conn, op, gs.err, gs.matched, NULL, NULL,
409 AttributeAssertion *ava
415 be = glue_back_select (b0, ndn);
417 if (be && be->be_compare) {
418 rc = be->be_compare (be, conn, op, dn, ndn, ava);
420 rc = LDAP_UNWILLING_TO_PERFORM;
421 send_ldap_result (conn, op, rc, NULL, "No compare target found",
440 be = glue_back_select (b0, ndn);
442 if (be && be->be_modify) {
443 rc = be->be_modify (be, conn, op, dn, ndn, mod);
445 rc = LDAP_UNWILLING_TO_PERFORM;
446 send_ldap_result (conn, op, rc, NULL, "No modify target found",
467 be = glue_back_select (b0, ndn);
469 if (be && be->be_modrdn) {
470 rc = be->be_modrdn (be, conn, op, dn, ndn, newrdn, del, newsup);
472 rc = LDAP_UNWILLING_TO_PERFORM;
473 send_ldap_result (conn, op, rc, NULL, "No modrdn target found",
490 be = glue_back_select (b0, e->e_ndn);
492 if (be && be->be_add) {
493 rc = be->be_add (be, conn, op, e);
495 rc = LDAP_UNWILLING_TO_PERFORM;
496 send_ldap_result (conn, op, rc, NULL, "No add target found",
514 be = glue_back_select (b0, ndn);
516 if (be && be->be_delete) {
517 rc = be->be_delete (be, conn, op, dn, ndn);
519 rc = LDAP_UNWILLING_TO_PERFORM;
520 send_ldap_result (conn, op, rc, NULL, "No delete target found",
527 glue_back_release_rw (
538 be = glue_back_select (b0, e->e_ndn);
540 if (be && be->be_release) {
541 rc = be->be_release (be, conn, op, e, rw);
558 AttributeDescription * ad
564 be = glue_back_select (b0, ndn);
566 if (be && be->be_group) {
567 rc = be->be_group (be, conn, op, target, ndn, ondn, oc, ad);
569 rc = LDAP_UNWILLING_TO_PERFORM;
575 glue_back_attribute (
581 AttributeDescription *ad,
582 struct berval ***vals
588 be = glue_back_select (b0, ndn);
590 if (be && be->be_attribute) {
591 rc = be->be_attribute (be, conn, op, target, ndn, ad, vals);
593 rc = LDAP_UNWILLING_TO_PERFORM;
599 glue_back_referrals (
611 be = glue_back_select (b0, ndn);
613 if (be && be->be_chk_referrals) {
614 rc = be->be_chk_referrals (be, conn, op, dn, ndn, text);
625 glue_tool_entry_open (
630 /* We don't know which backend to talk to yet, so just
631 * remember the mode and move on...
641 glue_tool_entry_close (
645 glueinfo *gi = (glueinfo *) b0->be_private;
650 if (!gi->n[i].be->be_entry_close)
652 rc = gi->n[i].be->be_entry_close (gi->n[i].be);
659 glue_tool_entry_first (
663 glueinfo *gi = (glueinfo *) b0->be_private;
666 /* If we're starting from scratch, start at the most general */
667 if (glueBack == -1) {
668 for (i = gi->nodes-1; i >= 0; i--) {
669 if (gi->n[i].be->be_entry_open &&
670 gi->n[i].be->be_entry_first)
676 if (gi->n[i].be->be_entry_open (gi->n[i].be, glueMode) != 0)
680 return gi->n[i].be->be_entry_first (gi->n[i].be);
684 glue_tool_entry_next (
688 glueinfo *gi = (glueinfo *) b0->be_private;
692 rc = gi->n[i].be->be_entry_next (gi->n[i].be);
694 /* If we ran out of entries in one database, move on to the next */
696 gi->n[i].be->be_entry_close (gi->n[i].be);
702 rc = glue_tool_entry_first (b0);
708 glue_tool_entry_get (
713 glueinfo *gi = (glueinfo *) b0->be_private;
716 return gi->n[i].be->be_entry_get (gi->n[i].be, id);
720 glue_tool_entry_put (
725 glueinfo *gi = (glueinfo *) b0->be_private;
729 be = glue_back_select (b0, e->e_ndn);
730 if (!be->be_entry_put)
735 rc = be->be_entry_open (be, glueMode);
739 } else if (be != gi->n[i].be) {
740 /* If this entry belongs in a different branch than the
741 * previous one, close the current database and open the
744 gi->n[i].be->be_entry_close (gi->n[i].be);
746 for (i = 0; b0->be_nsuffix[i]; i++)
747 if (gi->n[i].be == be)
749 rc = be->be_entry_open (be, glueMode);
754 return be->be_entry_put (be, e);
758 glue_tool_entry_reindex (
763 glueinfo *gi = (glueinfo *) b0->be_private;
766 if (!gi->n[i].be->be_entry_reindex)
769 return gi->n[i].be->be_entry_reindex (gi->n[i].be, id);
777 glueinfo *gi = (glueinfo *) b0->be_private;
780 /* just sync everyone */
781 for (i = 0; b0->be_nsuffix[i]; i++)
782 if (gi->n[i].be->be_sync)
783 gi->n[i].be->be_sync (gi->n[i].be);
788 glue_back_initialize (
798 bi->bi_db_config = 0;
799 bi->bi_db_open = glue_back_db_open;
800 bi->bi_db_close = glue_back_db_close;
801 bi->bi_db_destroy = glue_back_db_destroy;
803 bi->bi_op_bind = glue_back_bind;
804 bi->bi_op_unbind = 0;
805 bi->bi_op_search = glue_back_search;
806 bi->bi_op_compare = glue_back_compare;
807 bi->bi_op_modify = glue_back_modify;
808 bi->bi_op_modrdn = glue_back_modrdn;
809 bi->bi_op_add = glue_back_add;
810 bi->bi_op_delete = glue_back_delete;
811 bi->bi_op_abandon = 0;
815 bi->bi_entry_release_rw = glue_back_release_rw;
816 bi->bi_acl_group = glue_back_group;
817 bi->bi_acl_attribute = glue_back_attribute;
818 bi->bi_chk_referrals = glue_back_referrals;
821 * hooks for slap tools
823 bi->bi_tool_entry_open = glue_tool_entry_open;
824 bi->bi_tool_entry_close = glue_tool_entry_close;
825 bi->bi_tool_entry_first = glue_tool_entry_first;
826 bi->bi_tool_entry_next = glue_tool_entry_next;
827 bi->bi_tool_entry_get = glue_tool_entry_get;
828 bi->bi_tool_entry_put = glue_tool_entry_put;
829 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
830 bi->bi_tool_sync = glue_tool_sync;
832 bi->bi_connection_init = 0;
833 bi->bi_connection_destroy = 0;