1 /* backglue.c - backend glue routines */
4 * Copyright 2001-2002 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 (dnIsSuffix(&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 {
173 slap_callback *prevcb;
187 struct berval *resdata,
188 struct berval *sasldata,
192 glue_state *gs = op->o_callback->sc_private;
194 if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
196 if (gs->err == LDAP_SUCCESS && gs->matched) {
200 if (gs->err != LDAP_SUCCESS && matched) {
202 len = strlen (matched);
203 if (len > gs->matchlen) {
206 gs->matched = ch_strdup (matched);
214 for (i=0; ref[i].bv_val; i++);
218 new = ch_malloc ((i+1)*sizeof(struct berval));
220 new = ch_realloc(gs->refs,
221 (j+i+1)*sizeof(struct berval));
223 for (k=0; k<i; j++,k++) {
224 ber_dupbv( &new[j], &ref[k] );
226 new[j].bv_val = NULL;
244 glue_state *gs = op->o_callback->sc_private;
246 gs->nentries += nentries;
247 glue_back_response (c, op, 0, 0, err, matched, text, refs,
248 NULL, NULL, NULL, ctrls);
252 glue_back_sendentry (
262 slap_callback *tmp = op->o_callback;
263 glue_state *gs = tmp->sc_private;
266 op->o_callback = gs->prevcb;
267 if (op->o_callback && op->o_callback->sc_sendentry) {
268 rc = op->o_callback->sc_sendentry(be, c, op, e, an, ao, ctrls);
270 rc = send_search_entry(be, c, op, e, an, ao, ctrls);
272 op->o_callback = tmp;
288 struct berval *filterstr,
289 AttributeName *attrs,
293 glueinfo *gi = (glueinfo *)b0->be_private;
295 int i, rc = 0, t2limit = 0, s2limit = 0;
300 cb.sc_response = glue_back_response;
301 cb.sc_sresult = glue_back_sresult;
302 cb.sc_sendentry = glue_back_sendentry;
305 gs.prevcb = op->o_callback;
308 stoptime = slap_get_time () + tlimit;
312 case LDAP_SCOPE_BASE:
313 be = glue_back_select (b0, ndn->bv_val);
315 if (be && be->be_search) {
316 rc = be->be_search (be, conn, op, dn, ndn, scope,
317 deref, slimit, tlimit, filter, filterstr,
320 rc = LDAP_UNWILLING_TO_PERFORM;
321 send_ldap_result (conn, op, rc, NULL,
322 "No search target found", NULL, NULL);
326 case LDAP_SCOPE_ONELEVEL:
327 case LDAP_SCOPE_SUBTREE:
328 op->o_callback = &cb;
329 rc = gs.err = LDAP_UNWILLING_TO_PERFORM;
332 * Execute in reverse order, most general first
334 for (i = gi->nodes-1; i >= 0; i--) {
335 if (!gi->n[i].be || !gi->n[i].be->be_search)
338 t2limit = stoptime - slap_get_time ();
340 rc = gs.err = LDAP_TIMELIMIT_EXCEEDED;
345 s2limit = slimit - gs.nentries;
347 rc = gs.err = LDAP_SIZELIMIT_EXCEEDED;
359 if (scope == LDAP_SCOPE_ONELEVEL &&
360 dn_match(&gi->n[i].pdn, ndn)) {
361 rc = be->be_search (be, conn, op,
362 &be->be_suffix[0], &be->be_nsuffix[0],
363 LDAP_SCOPE_BASE, deref,
364 s2limit, t2limit, filter, filterstr,
367 } else if (scope == LDAP_SCOPE_SUBTREE &&
368 dnIsSuffix(&be->be_nsuffix[0], ndn)) {
369 rc = be->be_search (be, conn, op,
370 &be->be_suffix[0], &be->be_nsuffix[0],
372 s2limit, t2limit, filter, filterstr,
375 } else if (dnIsSuffix(ndn, &be->be_nsuffix[0])) {
376 rc = be->be_search (be, conn, op, dn, ndn,
378 s2limit, t2limit, filter, filterstr,
384 op->o_callback = gs.prevcb;
386 send_search_result (conn, op, gs.err, gs.matched, NULL,
387 gs.refs, NULL, gs.nentries);
393 ber_bvarray_free(gs.refs);
412 be = glue_back_select (b0, ndn->bv_val);
414 if (be && be->be_bind) {
415 rc = be->be_bind (be, conn, op, dn, ndn, method, cred, edn);
417 if( rc == LDAP_SUCCESS ) {
418 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
419 if( conn->c_authz_backend == NULL ) {
420 conn->c_authz_backend = be;
422 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
425 rc = LDAP_UNWILLING_TO_PERFORM;
426 send_ldap_result (conn, op, rc, NULL, "No bind target found",
439 AttributeAssertion *ava
445 be = glue_back_select (b0, ndn->bv_val);
447 if (be && be->be_compare) {
448 rc = be->be_compare (be, conn, op, dn, ndn, ava);
450 rc = LDAP_UNWILLING_TO_PERFORM;
451 send_ldap_result (conn, op, rc, NULL, "No compare target found",
470 be = glue_back_select (b0, ndn->bv_val);
472 if (be && be->be_modify) {
473 rc = be->be_modify (be, conn, op, dn, ndn, mod);
475 rc = LDAP_UNWILLING_TO_PERFORM;
476 send_ldap_result (conn, op, rc, NULL,
477 "No modify target found", NULL, NULL);
489 struct berval *newrdn,
490 struct berval *nnewrdn,
492 struct berval *newsup,
493 struct berval *nnewsup
499 be = glue_back_select (b0, ndn->bv_val);
501 if (be && be->be_modrdn) {
502 rc = be->be_modrdn (be, conn, op, dn, ndn,
503 newrdn, nnewrdn, del, newsup, nnewsup );
505 rc = LDAP_UNWILLING_TO_PERFORM;
506 send_ldap_result (conn, op, rc, NULL,
507 "No modrdn target found", NULL, NULL);
523 be = glue_back_select (b0, e->e_ndn);
525 if (be && be->be_add) {
526 rc = be->be_add (be, conn, op, e);
528 rc = LDAP_UNWILLING_TO_PERFORM;
529 send_ldap_result (conn, op, rc, NULL, "No add target found",
547 be = glue_back_select (b0, ndn->bv_val);
549 if (be && be->be_delete) {
550 rc = be->be_delete (be, conn, op, dn, ndn);
552 rc = LDAP_UNWILLING_TO_PERFORM;
553 send_ldap_result (conn, op, rc, NULL, "No delete target found",
560 glue_back_release_rw (
571 be = glue_back_select (b0, e->e_ndn);
573 if (be && be->be_release) {
574 rc = be->be_release (be, conn, op, e, rw);
591 AttributeDescription * ad
597 be = glue_back_select (b0, ndn->bv_val);
599 if (be && be->be_group) {
600 rc = be->be_group (be, conn, op, target, ndn, ondn, oc, ad);
602 rc = LDAP_UNWILLING_TO_PERFORM;
608 glue_back_attribute (
614 AttributeDescription *ad,
621 be = glue_back_select (b0, ndn->bv_val);
623 if (be && be->be_attribute) {
624 rc = be->be_attribute (be, conn, op, target, ndn, ad, vals);
626 rc = LDAP_UNWILLING_TO_PERFORM;
632 glue_back_referrals (
644 be = glue_back_select (b0, ndn->bv_val);
646 if (be && be->be_chk_referrals) {
647 rc = be->be_chk_referrals (be, conn, op, dn, ndn, text);
655 glue_tool_entry_open (
660 /* We don't know which backend to talk to yet, so just
661 * remember the mode and move on...
671 glue_tool_entry_close (
678 if (!glueBack->be_entry_close)
680 rc = glueBack->be_entry_close (glueBack);
686 glue_tool_entry_first (
690 glueinfo *gi = (glueinfo *) b0->be_private;
693 /* If we're starting from scratch, start at the most general */
695 for (i = gi->nodes-1; i >= 0; i--) {
696 if (gi->n[i].be->be_entry_open &&
697 gi->n[i].be->be_entry_first) {
698 glueBack = gi->n[i].be;
704 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
707 return glueBack->be_entry_first (glueBack);
711 glue_tool_entry_next (
715 glueinfo *gi = (glueinfo *) b0->be_private;
719 if (!glueBack || !glueBack->be_entry_next)
722 rc = glueBack->be_entry_next (glueBack);
724 /* If we ran out of entries in one database, move on to the next */
726 glueBack->be_entry_close (glueBack);
727 for (i=0; i<gi->nodes; i++) {
728 if (gi->n[i].be == glueBack)
735 glueBack = gi->n[i-1].be;
736 rc = glue_tool_entry_first (b0);
743 glue_tool_entry_get (
748 if (!glueBack || !glueBack->be_entry_get)
751 return glueBack->be_entry_get (glueBack, id);
755 glue_tool_entry_put (
764 be = glue_back_select (b0, e->e_ndn);
765 if (!be->be_entry_put)
769 rc = be->be_entry_open (be, glueMode);
772 } else if (be != glueBack) {
773 /* If this entry belongs in a different branch than the
774 * previous one, close the current database and open the
777 glueBack->be_entry_close (glueBack);
778 rc = be->be_entry_open (be, glueMode);
783 return be->be_entry_put (be, e, text);
787 glue_tool_entry_reindex (
792 if (!glueBack || !glueBack->be_entry_reindex)
795 return glueBack->be_entry_reindex (glueBack, id);
803 glueinfo *gi = (glueinfo *) b0->be_private;
806 /* just sync everyone */
807 for (i = 0; i<gi->nodes; i++)
808 if (gi->n[i].be->be_sync)
809 gi->n[i].be->be_sync (gi->n[i].be);
817 int cont = num_subordinates;
819 BackendInfo *bi = NULL;
822 /* While there are subordinate backends, search backwards through the
823 * backends and connect them to their superior.
825 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
826 if (b1->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
827 /* The last database cannot be a subordinate of noone */
828 if (i == nBackendDB - 1) {
829 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
834 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
835 if (!(be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE)) {
838 /* We will only link it once */
839 if (be->be_flags & SLAP_BFLAG_GLUE_LINKED) {
842 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
846 be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
848 /* We create a copy of the superior's be
849 * structure, pointing to all of its original
850 * information. Then we replace elements of
851 * the superior's info with our own. The copy
852 * is used whenever we have operations to pass
853 * down to the real database.
855 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
856 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
857 gi->be = (BackendDB *)ch_malloc(
858 sizeof(BackendDB) + sizeof(BackendInfo));
859 bi = (BackendInfo *)(gi->be+1);
863 bi->bi_open = glue_back_open;
864 bi->bi_close = glue_back_close;
865 bi->bi_db_open = glue_back_db_open;
866 bi->bi_db_close = glue_back_db_close;
867 bi->bi_db_destroy = glue_back_db_destroy;
869 bi->bi_op_bind = glue_back_bind;
870 bi->bi_op_search = glue_back_search;
871 bi->bi_op_compare = glue_back_compare;
872 bi->bi_op_modify = glue_back_modify;
873 bi->bi_op_modrdn = glue_back_modrdn;
874 bi->bi_op_add = glue_back_add;
875 bi->bi_op_delete = glue_back_delete;
877 bi->bi_entry_release_rw = glue_back_release_rw;
878 bi->bi_acl_group = glue_back_group;
879 bi->bi_acl_attribute = glue_back_attribute;
880 bi->bi_chk_referrals = glue_back_referrals;
883 * hooks for slap tools
885 bi->bi_tool_entry_open = glue_tool_entry_open;
886 bi->bi_tool_entry_close = glue_tool_entry_close;
887 bi->bi_tool_entry_first = glue_tool_entry_first;
888 bi->bi_tool_entry_next = glue_tool_entry_next;
889 bi->bi_tool_entry_get = glue_tool_entry_get;
890 bi->bi_tool_entry_put = glue_tool_entry_put;
891 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
892 bi->bi_tool_sync = glue_tool_sync;
894 gi = (glueinfo *)ch_realloc(gi,
896 gi->nodes * sizeof(gluenode));
898 gi->n[gi->nodes].be = be;
899 dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn );
903 /* One more node for the master */
904 gi = (glueinfo *)ch_realloc(gi,
905 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
906 gi->n[gi->nodes].be = gi->be;
907 dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
913 /* If there are any unresolved subordinates left, something is wrong */