1 /* backglue.c - backend glue routines */
4 * Copyright 2001-2003 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/string.h>
33 #include <ac/socket.h>
38 typedef struct gluenode {
43 typedef struct glueinfo {
51 static BackendDB *glueBack;
53 /* Just like select_backend, but only for our backends */
60 glueinfo *gi = (glueinfo *) be->bd_info;
64 bv.bv_len = strlen(dn);
65 bv.bv_val = (char *) dn;
67 for (i = 0; i<gi->nodes; i++) {
68 if (dnIsSuffix(&bv, &gi->n[i].be->be_nsuffix[0])) {
75 /* This function will only be called in tool mode */
82 static int glueOpened = 0;
84 if (glueOpened) return 0;
88 /* If we were invoked in tool mode, open all the underlying backends */
89 if (slapMode & SLAP_TOOL_MODE) {
90 rc = backend_startup (NULL);
91 } /* other case is impossible */
95 /* This function will only be called in tool mode */
101 static int glueClosed = 0;
104 if (glueClosed) return 0;
108 if (slapMode & SLAP_TOOL_MODE) {
109 rc = backend_shutdown (NULL);
119 glueinfo *gi = (glueinfo *) be->bd_info;
120 static int glueOpened = 0;
123 if (glueOpened) return 0;
127 gi->bd.be_acl = be->be_acl;
129 if (gi->bd.bd_info->bi_db_open)
130 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
140 glueinfo *gi = (glueinfo *) be->bd_info;
141 static int glueClosed = 0;
143 if (glueClosed) return 0;
147 /* Close the master */
148 if (gi->bd.bd_info->bi_db_close)
149 gi->bd.bd_info->bi_db_close( &gi->bd );
155 glue_back_db_destroy (
159 glueinfo *gi = (glueinfo *) be->bd_info;
161 if (gi->bd.bd_info->bi_db_destroy)
162 gi->bd.bd_info->bi_db_destroy( &gi->bd );
167 typedef struct glue_state {
174 slap_callback *prevcb;
188 struct berval *resdata,
189 struct berval *sasldata,
193 glue_state *gs = op->o_callback->sc_private;
195 if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
197 if (gs->err == LDAP_SUCCESS && gs->matched) {
202 if (gs->err != LDAP_SUCCESS && matched) {
204 len = strlen (matched);
205 if (len > gs->matchlen) {
208 gs->matched = ch_strdup (matched);
216 for (i=0; ref[i].bv_val; i++);
220 new = ch_malloc ((i+1)*sizeof(struct berval));
222 new = ch_realloc(gs->refs,
223 (j+i+1)*sizeof(struct berval));
225 for (k=0; k<i; j++,k++) {
226 ber_dupbv( &new[j], &ref[k] );
228 new[j].bv_val = NULL;
246 glue_state *gs = op->o_callback->sc_private;
248 gs->nentries += nentries;
249 glue_back_response (c, op, 0, 0, err, matched, text, refs,
250 NULL, NULL, NULL, ctrls);
254 glue_back_sendentry (
264 slap_callback *tmp = op->o_callback;
265 glue_state *gs = tmp->sc_private;
268 op->o_callback = gs->prevcb;
269 if (op->o_callback && op->o_callback->sc_sendentry) {
270 rc = op->o_callback->sc_sendentry(be, c, op, e, an, ao, ctrls);
272 rc = send_search_entry(be, c, op, e, an, ao, ctrls);
274 op->o_callback = tmp;
279 glue_back_sendreference (
289 slap_callback *tmp = op->o_callback;
290 glue_state *gs = tmp->sc_private;
293 op->o_callback = gs->prevcb;
294 if (op->o_callback && op->o_callback->sc_sendreference) {
295 rc = op->o_callback->sc_sendreference( be, c, op, e, bv, ctrls, v2 );
297 rc = send_search_reference( be, c, op, e, bv, ctrls, v2 );
299 op->o_callback = tmp;
315 struct berval *filterstr,
316 AttributeName *attrs,
320 glueinfo *gi = (glueinfo *) b0->bd_info;
322 int i, rc = 0, t2limit = 0, s2limit = 0;
324 glue_state gs = {0, 0, 0, NULL, 0, NULL, NULL};
327 cb.sc_response = glue_back_response;
328 cb.sc_sresult = glue_back_sresult;
329 cb.sc_sendentry = glue_back_sendentry;
332 gs.prevcb = op->o_callback;
335 stoptime = slap_get_time () + tlimit;
339 case LDAP_SCOPE_BASE:
340 be = glue_back_select (b0, ndn->bv_val);
342 if (be && be->be_search) {
343 rc = be->be_search (be, conn, op, dn, ndn, scope,
344 deref, slimit, tlimit, filter, filterstr,
347 rc = LDAP_UNWILLING_TO_PERFORM;
348 send_ldap_result (conn, op, rc, NULL,
349 "No search target found", NULL, NULL);
353 case LDAP_SCOPE_ONELEVEL:
354 case LDAP_SCOPE_SUBTREE:
355 op->o_callback = &cb;
356 rc = gs.err = LDAP_UNWILLING_TO_PERFORM;
359 * Execute in reverse order, most general first
361 for (i = gi->nodes-1; i >= 0; i--) {
362 if (!gi->n[i].be || !gi->n[i].be->be_search)
365 t2limit = stoptime - slap_get_time ();
367 rc = gs.err = LDAP_TIMELIMIT_EXCEEDED;
372 s2limit = slimit - gs.nentries;
374 rc = gs.err = LDAP_SIZELIMIT_EXCEEDED;
386 if (scope == LDAP_SCOPE_ONELEVEL &&
387 dn_match(&gi->n[i].pdn, ndn)) {
388 rc = be->be_search (be, conn, op,
389 &be->be_suffix[0], &be->be_nsuffix[0],
390 LDAP_SCOPE_BASE, deref,
391 s2limit, t2limit, filter, filterstr,
394 } else if (scope == LDAP_SCOPE_SUBTREE &&
395 dnIsSuffix(&be->be_nsuffix[0], ndn)) {
396 rc = be->be_search (be, conn, op,
397 &be->be_suffix[0], &be->be_nsuffix[0],
399 s2limit, t2limit, filter, filterstr,
402 } else if (dnIsSuffix(ndn, &be->be_nsuffix[0])) {
403 rc = be->be_search (be, conn, op, dn, ndn,
405 s2limit, t2limit, filter, filterstr,
412 * Add errors that should result in dropping
415 case LDAP_SIZELIMIT_EXCEEDED:
416 case LDAP_TIMELIMIT_EXCEEDED:
417 case LDAP_ADMINLIMIT_EXCEEDED:
427 op->o_callback = gs.prevcb;
429 send_search_result (conn, op, gs.err, gs.matched, NULL,
430 gs.refs, NULL, gs.nentries);
436 ber_bvarray_free(gs.refs);
442 glue_tool_entry_open (
447 /* We don't know which backend to talk to yet, so just
448 * remember the mode and move on...
458 glue_tool_entry_close (
465 if (!glueBack->be_entry_close)
467 rc = glueBack->be_entry_close (glueBack);
473 glue_tool_entry_first (
477 glueinfo *gi = (glueinfo *) b0->bd_info;
480 /* If we're starting from scratch, start at the most general */
482 for (i = gi->nodes-1; i >= 0; i--) {
483 if (gi->n[i].be->be_entry_open &&
484 gi->n[i].be->be_entry_first) {
485 glueBack = gi->n[i].be;
491 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
494 return glueBack->be_entry_first (glueBack);
498 glue_tool_entry_next (
502 glueinfo *gi = (glueinfo *) b0->bd_info;
506 if (!glueBack || !glueBack->be_entry_next)
509 rc = glueBack->be_entry_next (glueBack);
511 /* If we ran out of entries in one database, move on to the next */
513 glueBack->be_entry_close (glueBack);
514 for (i=0; i<gi->nodes; i++) {
515 if (gi->n[i].be == glueBack)
522 glueBack = gi->n[i-1].be;
523 rc = glue_tool_entry_first (b0);
530 glue_tool_entry_get (
535 if (!glueBack || !glueBack->be_entry_get)
538 return glueBack->be_entry_get (glueBack, id);
542 glue_tool_entry_put (
551 be = glue_back_select (b0, e->e_ndn);
552 if (!be->be_entry_put)
556 rc = be->be_entry_open (be, glueMode);
559 } else if (be != glueBack) {
560 /* If this entry belongs in a different branch than the
561 * previous one, close the current database and open the
564 glueBack->be_entry_close (glueBack);
565 rc = be->be_entry_open (be, glueMode);
570 return be->be_entry_put (be, e, text);
574 glue_tool_entry_reindex (
579 if (!glueBack || !glueBack->be_entry_reindex)
582 return glueBack->be_entry_reindex (glueBack, id);
590 glueinfo *gi = (glueinfo *) b0->bd_info;
593 /* just sync everyone */
594 for (i = 0; i<gi->nodes; i++)
595 if (gi->n[i].be->be_sync)
596 gi->n[i].be->be_sync (gi->n[i].be);
604 int cont = num_subordinates;
606 BackendInfo *bi = NULL;
609 /* While there are subordinate backends, search backwards through the
610 * backends and connect them to their superior.
612 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
613 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
614 /* The last database cannot be a subordinate of noone */
615 if (i == nBackendDB - 1) {
616 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
621 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
622 if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
625 /* We will only link it once */
626 if ( SLAP_GLUE_LINKED( be ) ) {
629 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
633 be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
635 /* We create a copy of the superior's be
636 * structure, pointing to all of its original
637 * information. Then we replace elements of
638 * the superior's info with our own. The copy
639 * is used whenever we have operations to pass
640 * down to the real database.
642 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
643 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
646 gi->bi = *b1->bd_info;
647 bi = (BackendInfo *)gi;
648 bi->bi_open = glue_back_open;
649 bi->bi_close = glue_back_close;
650 bi->bi_db_open = glue_back_db_open;
651 bi->bi_db_close = glue_back_db_close;
652 bi->bi_db_destroy = glue_back_db_destroy;
654 bi->bi_op_search = glue_back_search;
657 * hooks for slap tools
659 bi->bi_tool_entry_open = glue_tool_entry_open;
660 bi->bi_tool_entry_close = glue_tool_entry_close;
661 bi->bi_tool_entry_first = glue_tool_entry_first;
662 bi->bi_tool_entry_next = glue_tool_entry_next;
663 bi->bi_tool_entry_get = glue_tool_entry_get;
664 bi->bi_tool_entry_put = glue_tool_entry_put;
665 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
666 bi->bi_tool_sync = glue_tool_sync;
668 gi = (glueinfo *)ch_realloc(gi,
670 gi->nodes * sizeof(gluenode));
672 gi->n[gi->nodes].be = be;
673 dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn );
677 /* One more node for the master */
678 gi = (glueinfo *)ch_realloc(gi,
679 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
680 gi->n[gi->nodes].be = &gi->bd;
681 dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
686 /* If there are any unresolved subordinates left, something is wrong */