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 glueinfo {
44 /* Just like select_backend, but only for our backends */
51 struct glueinfo *gi = (struct glueinfo *) be->be_private;
54 for (i = 0; be->be_nsuffix[i]; i++) {
55 if (dn_issuffix (dn, be->be_nsuffix[i]->bv_val))
66 static int glueOpened = 0;
71 /* Make sure all backends get started when we got selected
74 if (slapMode == SLAP_TOOL_MODE)
75 backend_startup (NULL);
84 static int glueClosed = 0;
89 if (slapMode == SLAP_TOOL_MODE)
90 backend_shutdown (NULL);
109 for (i = 0; be->be_suffix[i]; i++);
111 gi = (struct glueinfo *) ch_calloc (i, sizeof (struct glueinfo));
119 * For each of our suffixes, find the real backend that handles this
122 for (i = 0; be->be_nsuffix[i]; i++) {
123 for (j = 0; j < nbackends; j++) {
124 if (be == &backends[j])
127 for (k = 0; backends[j].be_nsuffix &&
128 backends[j].be_nsuffix[k]; k++) {
129 if (be->be_nsuffix[i]->bv_len !=
130 backends[j].be_nsuffix[k]->bv_len)
132 if (!strcmp (backends[j].be_nsuffix[k]->bv_val,
133 be->be_nsuffix[i]->bv_val)) {
139 gi[i].be = &backends[j];
140 gi[i].pdn = dn_parent (NULL,
141 be->be_nsuffix[i]->bv_val);
150 glue_back_db_destroy (
154 free (be->be_private);
173 be = glue_back_select (b0, ndn);
175 if (be && be->be_bind) {
176 conn->c_authz_backend = be;
177 rc = be->be_bind (be, conn, op, dn, ndn, method, cred, edn);
179 rc = LDAP_UNWILLING_TO_PERFORM;
180 send_ldap_result (conn, op, rc, NULL, "No bind target found",
186 typedef struct glue_state {
204 struct berval *resdata,
205 struct berval *sasldata,
209 glue_state *gs = op->o_glue;
211 if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
213 if (gs->err == LDAP_SUCCESS && gs->matched) {
217 if (gs->err != LDAP_SUCCESS && matched) {
219 len = strlen (matched);
220 if (len > gs->matchlen) {
223 gs->matched = ch_strdup (matched);
236 struct berval **refs,
241 glue_state *gs = op->o_glue;
243 gs->nentries += nentries;
244 glue_back_response (c, op, 0, 0, err, matched, text, refs,
245 NULL, NULL, NULL, ctrls);
260 const char *filterstr,
265 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
267 int i, rc, t2limit = 0, s2limit = 0;
274 stoptime = slap_get_time () + tlimit;
277 case LDAP_SCOPE_BASE:
278 be = glue_back_select (b0, ndn);
280 if (be && be->be_search) {
281 rc = be->be_search (be, conn, op, dn, ndn, scope,
282 deref, slimit, tlimit, filter, filterstr,
285 rc = LDAP_UNWILLING_TO_PERFORM;
286 send_ldap_result (conn, op, rc, NULL,
287 "No search target found", NULL, NULL);
291 case LDAP_SCOPE_ONELEVEL:
293 op->o_sresult = glue_back_sresult;
294 op->o_response = glue_back_response;
297 * Execute in reverse order, most general first
299 for (i = 0; b0->be_nsuffix[i]; i++);
300 for (--i; i >= 0; i--) {
301 if (!gi[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[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 = 0; b0->be_nsuffix[i]; i++);
350 for (--i; i >= 0; i--) {
351 if (!gi[i].be->be_search)
354 t2limit = stoptime - slap_get_time ();
359 s2limit = slimit - gs.nentries;
366 ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
368 ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
373 if (dn_issuffix (ndn, b0->be_nsuffix[i]->bv_val)) {
375 rc = be->be_search (be, conn, op,
376 dn, ndn, scope, deref,
377 s2limit, t2limit, filter, filterstr,
379 } else if (dn_issuffix (b0->be_nsuffix[i]->bv_val, ndn)) {
381 rc = be->be_search (be, conn, op,
383 b0->be_nsuffix[i]->bv_val,
385 s2limit, t2limit, filter, filterstr,
391 op->o_sresult = NULL;
392 op->o_response = NULL;
395 send_search_result (conn, op, gs.err, gs.matched, NULL, NULL,
410 AttributeAssertion *ava
416 be = glue_back_select (b0, ndn);
418 if (be && be->be_compare) {
419 rc = be->be_compare (be, conn, op, dn, ndn, ava);
421 rc = LDAP_UNWILLING_TO_PERFORM;
422 send_ldap_result (conn, op, rc, NULL, "No compare target found",
441 be = glue_back_select (b0, ndn);
443 if (be && be->be_modify) {
444 rc = be->be_modify (be, conn, op, dn, ndn, mod);
446 rc = LDAP_UNWILLING_TO_PERFORM;
447 send_ldap_result (conn, op, rc, NULL, "No modify target found",
468 be = glue_back_select (b0, ndn);
470 if (be && be->be_modrdn) {
471 rc = be->be_modrdn (be, conn, op, dn, ndn, newrdn, del, newsup);
473 rc = LDAP_UNWILLING_TO_PERFORM;
474 send_ldap_result (conn, op, rc, NULL, "No modrdn target found",
491 be = glue_back_select (b0, e->e_ndn);
493 if (be && be->be_add) {
494 rc = be->be_add (be, conn, op, e);
496 rc = LDAP_UNWILLING_TO_PERFORM;
497 send_ldap_result (conn, op, rc, NULL, "No add target found",
515 be = glue_back_select (b0, ndn);
517 if (be && be->be_delete) {
518 rc = be->be_delete (be, conn, op, dn, ndn);
520 rc = LDAP_UNWILLING_TO_PERFORM;
521 send_ldap_result (conn, op, rc, NULL, "No delete target found",
536 AttributeDescription * ad
542 be = glue_back_select (b0, ndn);
544 if (be && be->be_group) {
545 rc = be->be_group (be, conn, op, target, ndn, ondn, oc, ad);
547 rc = LDAP_UNWILLING_TO_PERFORM;
553 glue_back_attribute (
559 AttributeDescription *ad,
560 struct berval ***vals
566 be = glue_back_select (b0, ndn);
568 if (be && be->be_attribute) {
569 rc = be->be_attribute (be, conn, op, target, ndn, ad, vals);
571 rc = LDAP_UNWILLING_TO_PERFORM;
577 glue_back_referrals (
589 be = glue_back_select (b0, ndn);
591 if (be && be->be_chk_referrals) {
592 rc = be->be_chk_referrals (be, conn, op, dn, ndn, text);
603 glue_tool_entry_open (
610 /* We don't know which backend to talk to yet, so just
611 * remember the mode and move on...
621 glue_tool_entry_close (
625 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
630 if (!gi[i].be->be_entry_close)
632 rc = gi[i].be->be_entry_close (gi[i].be);
639 glue_tool_entry_first (
643 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
646 /* If we're starting from scratch, start at the most general */
647 if (glueBack == -1) {
648 for (i = 0; b0->be_nsuffix[i]; i++);
649 for (--i; i >= 0; i--) {
650 if (gi[i].be->be_entry_open &&
651 gi[i].be->be_entry_first)
657 if (gi[i].be->be_entry_open (gi[i].be, glueMode) != 0)
661 return gi[i].be->be_entry_first (gi[i].be);
665 glue_tool_entry_next (
669 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
673 rc = gi[i].be->be_entry_next (gi[i].be);
675 /* If we ran out of entries in one database, move on to the next */
677 gi[i].be->be_entry_close (gi[i].be);
682 rc = glue_tool_entry_first (b0);
688 glue_tool_entry_get (
693 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
695 return gi[glueBack].be->be_entry_get (gi[glueBack].be, id);
699 glue_tool_entry_put (
704 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
708 be = glue_back_select (b0, e->e_ndn);
709 if (!be->be_entry_put)
714 rc = be->be_entry_open (be, glueMode);
718 } else if (be != gi[i].be) {
719 /* If this entry belongs in a different branch than the
720 * previous one, close the current database and open the
723 gi[i].be->be_entry_close (gi[i].be);
725 for (i = 0; b0->be_nsuffix[i]; i++)
728 rc = be->be_entry_open (be, glueMode);
733 return be->be_entry_put (be, e);
737 glue_tool_entry_reindex (
742 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
745 if (!gi[i].be->be_entry_reindex)
748 return gi[i].be->be_entry_reindex (gi[i].be, id);
756 struct glueinfo *gi = (struct glueinfo *) b0->be_private;
759 /* just sync everyone */
760 for (i = 0; b0->be_nsuffix[i]; i++)
761 if (gi[i].be->be_sync)
762 gi[i].be->be_sync (gi[i].be);
767 glue_back_initialize (
771 bi->bi_open = glue_back_open;
773 bi->bi_close = glue_back_close;
777 bi->bi_db_config = 0;
778 bi->bi_db_open = glue_back_db_open;
780 bi->bi_db_destroy = glue_back_db_destroy;
782 bi->bi_op_bind = glue_back_bind;
783 bi->bi_op_unbind = 0;
784 bi->bi_op_search = glue_back_search;
785 bi->bi_op_compare = glue_back_compare;
786 bi->bi_op_modify = glue_back_modify;
787 bi->bi_op_modrdn = glue_back_modrdn;
788 bi->bi_op_add = glue_back_add;
789 bi->bi_op_delete = glue_back_delete;
790 bi->bi_op_abandon = 0;
794 bi->bi_acl_group = glue_back_group;
795 bi->bi_acl_attribute = glue_back_attribute;
796 bi->bi_chk_referrals = glue_back_referrals;
799 * hooks for slap tools
801 bi->bi_tool_entry_open = glue_tool_entry_open;
802 bi->bi_tool_entry_close = glue_tool_entry_close;
803 bi->bi_tool_entry_first = glue_tool_entry_first;
804 bi->bi_tool_entry_next = glue_tool_entry_next;
805 bi->bi_tool_entry_get = glue_tool_entry_get;
806 bi->bi_tool_entry_put = glue_tool_entry_put;
807 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
808 bi->bi_tool_sync = glue_tool_sync;
810 bi->bi_connection_init = 0;
811 bi->bi_connection_destroy = 0;