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 {
50 static BackendDB *glueBack;
52 /* Just like select_backend, but only for our backends */
59 glueinfo *gi = (glueinfo *) be->bd_info;
63 bv.bv_len = strlen(dn);
64 bv.bv_val = (char *) dn;
66 for (i = 0; i<gi->nodes; i++) {
67 if (dnIsSuffix(&bv, &gi->n[i].be->be_nsuffix[0])) {
74 /* This function will only be called in tool mode */
81 static int glueOpened = 0;
83 if (glueOpened) return 0;
87 /* If we were invoked in tool mode, open all the underlying backends */
88 if (slapMode & SLAP_TOOL_MODE) {
89 rc = backend_startup (NULL);
90 } /* other case is impossible */
94 /* This function will only be called in tool mode */
100 static int glueClosed = 0;
103 if (glueClosed) return 0;
107 if (slapMode & SLAP_TOOL_MODE) {
108 rc = backend_shutdown (NULL);
118 glueinfo *gi = (glueinfo *) be->bd_info;
119 static int glueOpened = 0;
122 if (glueOpened) return 0;
126 gi->bd.be_acl = be->be_acl;
128 if (gi->bd.bd_info->bi_db_open)
129 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
139 glueinfo *gi = (glueinfo *) be->bd_info;
140 static int glueClosed = 0;
142 if (glueClosed) return 0;
146 /* Close the master */
147 if (gi->bd.bd_info->bi_db_close)
148 gi->bd.bd_info->bi_db_close( &gi->bd );
154 glue_back_db_destroy (
158 glueinfo *gi = (glueinfo *) be->bd_info;
160 if (gi->bd.bd_info->bi_db_destroy)
161 gi->bd.bd_info->bi_db_destroy( &gi->bd );
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) {
201 if (gs->err != LDAP_SUCCESS && matched) {
203 len = strlen (matched);
204 if (len > gs->matchlen) {
207 gs->matched = ch_strdup (matched);
215 for (i=0; ref[i].bv_val; i++);
219 new = ch_malloc ((i+1)*sizeof(struct berval));
221 new = ch_realloc(gs->refs,
222 (j+i+1)*sizeof(struct berval));
224 for (k=0; k<i; j++,k++) {
225 ber_dupbv( &new[j], &ref[k] );
227 new[j].bv_val = NULL;
245 glue_state *gs = op->o_callback->sc_private;
247 gs->nentries += nentries;
248 glue_back_response (c, op, 0, 0, err, matched, text, refs,
249 NULL, NULL, NULL, ctrls);
253 glue_back_sendentry (
263 slap_callback *tmp = op->o_callback;
264 glue_state *gs = tmp->sc_private;
267 op->o_callback = gs->prevcb;
268 if (op->o_callback && op->o_callback->sc_sendentry) {
269 rc = op->o_callback->sc_sendentry(be, c, op, e, an, ao, ctrls);
271 rc = send_search_entry(be, c, op, e, an, ao, ctrls);
273 op->o_callback = tmp;
289 struct berval *filterstr,
290 AttributeName *attrs,
294 glueinfo *gi = (glueinfo *) b0->bd_info;
296 int i, rc = 0, t2limit = 0, s2limit = 0;
301 cb.sc_response = glue_back_response;
302 cb.sc_sresult = glue_back_sresult;
303 cb.sc_sendentry = glue_back_sendentry;
306 gs.prevcb = op->o_callback;
309 stoptime = slap_get_time () + tlimit;
313 case LDAP_SCOPE_BASE:
314 be = glue_back_select (b0, ndn->bv_val);
316 if (be && be->be_search) {
317 rc = be->be_search (be, conn, op, dn, ndn, scope,
318 deref, slimit, tlimit, filter, filterstr,
321 rc = LDAP_UNWILLING_TO_PERFORM;
322 send_ldap_result (conn, op, rc, NULL,
323 "No search target found", NULL, NULL);
327 case LDAP_SCOPE_ONELEVEL:
328 case LDAP_SCOPE_SUBTREE:
329 op->o_callback = &cb;
330 rc = gs.err = LDAP_UNWILLING_TO_PERFORM;
333 * Execute in reverse order, most general first
335 for (i = gi->nodes-1; i >= 0; i--) {
336 if (!gi->n[i].be || !gi->n[i].be->be_search)
339 t2limit = stoptime - slap_get_time ();
341 rc = gs.err = LDAP_TIMELIMIT_EXCEEDED;
346 s2limit = slimit - gs.nentries;
348 rc = gs.err = LDAP_SIZELIMIT_EXCEEDED;
360 if (scope == LDAP_SCOPE_ONELEVEL &&
361 dn_match(&gi->n[i].pdn, ndn)) {
362 rc = be->be_search (be, conn, op,
363 &be->be_suffix[0], &be->be_nsuffix[0],
364 LDAP_SCOPE_BASE, deref,
365 s2limit, t2limit, filter, filterstr,
368 } else if (scope == LDAP_SCOPE_SUBTREE &&
369 dnIsSuffix(&be->be_nsuffix[0], ndn)) {
370 rc = be->be_search (be, conn, op,
371 &be->be_suffix[0], &be->be_nsuffix[0],
373 s2limit, t2limit, filter, filterstr,
376 } else if (dnIsSuffix(ndn, &be->be_nsuffix[0])) {
377 rc = be->be_search (be, conn, op, dn, ndn,
379 s2limit, t2limit, filter, filterstr,
385 op->o_callback = gs.prevcb;
387 send_search_result (conn, op, gs.err, gs.matched, NULL,
388 gs.refs, NULL, gs.nentries);
394 ber_bvarray_free(gs.refs);
400 glue_tool_entry_open (
405 /* We don't know which backend to talk to yet, so just
406 * remember the mode and move on...
416 glue_tool_entry_close (
423 if (!glueBack->be_entry_close)
425 rc = glueBack->be_entry_close (glueBack);
431 glue_tool_entry_first (
435 glueinfo *gi = (glueinfo *) b0->bd_info;
438 /* If we're starting from scratch, start at the most general */
440 for (i = gi->nodes-1; i >= 0; i--) {
441 if (gi->n[i].be->be_entry_open &&
442 gi->n[i].be->be_entry_first) {
443 glueBack = gi->n[i].be;
449 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
452 return glueBack->be_entry_first (glueBack);
456 glue_tool_entry_next (
460 glueinfo *gi = (glueinfo *) b0->bd_info;
464 if (!glueBack || !glueBack->be_entry_next)
467 rc = glueBack->be_entry_next (glueBack);
469 /* If we ran out of entries in one database, move on to the next */
471 glueBack->be_entry_close (glueBack);
472 for (i=0; i<gi->nodes; i++) {
473 if (gi->n[i].be == glueBack)
480 glueBack = gi->n[i-1].be;
481 rc = glue_tool_entry_first (b0);
488 glue_tool_entry_get (
493 if (!glueBack || !glueBack->be_entry_get)
496 return glueBack->be_entry_get (glueBack, id);
500 glue_tool_entry_put (
509 be = glue_back_select (b0, e->e_ndn);
510 if (!be->be_entry_put)
514 rc = be->be_entry_open (be, glueMode);
517 } else if (be != glueBack) {
518 /* If this entry belongs in a different branch than the
519 * previous one, close the current database and open the
522 glueBack->be_entry_close (glueBack);
523 rc = be->be_entry_open (be, glueMode);
528 return be->be_entry_put (be, e, text);
532 glue_tool_entry_reindex (
537 if (!glueBack || !glueBack->be_entry_reindex)
540 return glueBack->be_entry_reindex (glueBack, id);
548 glueinfo *gi = (glueinfo *) b0->bd_info;
551 /* just sync everyone */
552 for (i = 0; i<gi->nodes; i++)
553 if (gi->n[i].be->be_sync)
554 gi->n[i].be->be_sync (gi->n[i].be);
562 int cont = num_subordinates;
564 BackendInfo *bi = NULL;
567 /* While there are subordinate backends, search backwards through the
568 * backends and connect them to their superior.
570 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
571 if (b1->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
572 /* The last database cannot be a subordinate of noone */
573 if (i == nBackendDB - 1) {
574 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
579 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
580 if (!(be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE)) {
583 /* We will only link it once */
584 if (be->be_flags & SLAP_BFLAG_GLUE_LINKED) {
587 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
591 be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
593 /* We create a copy of the superior's be
594 * structure, pointing to all of its original
595 * information. Then we replace elements of
596 * the superior's info with our own. The copy
597 * is used whenever we have operations to pass
598 * down to the real database.
600 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
601 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
604 gi->bi = *b1->bd_info;
605 bi = (BackendInfo *)gi;
606 bi->bi_open = glue_back_open;
607 bi->bi_close = glue_back_close;
608 bi->bi_db_open = glue_back_db_open;
609 bi->bi_db_close = glue_back_db_close;
610 bi->bi_db_destroy = glue_back_db_destroy;
612 bi->bi_op_search = glue_back_search;
615 * hooks for slap tools
617 bi->bi_tool_entry_open = glue_tool_entry_open;
618 bi->bi_tool_entry_close = glue_tool_entry_close;
619 bi->bi_tool_entry_first = glue_tool_entry_first;
620 bi->bi_tool_entry_next = glue_tool_entry_next;
621 bi->bi_tool_entry_get = glue_tool_entry_get;
622 bi->bi_tool_entry_put = glue_tool_entry_put;
623 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
624 bi->bi_tool_sync = glue_tool_sync;
626 gi = (glueinfo *)ch_realloc(gi,
628 gi->nodes * sizeof(gluenode));
630 gi->n[gi->nodes].be = be;
631 dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn );
635 /* One more node for the master */
636 gi = (glueinfo *)ch_realloc(gi,
637 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
638 gi->n[gi->nodes].be = &gi->bd;
639 dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
644 /* If there are any unresolved subordinates left, something is wrong */