1 /* backglue.c - backend glue routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
18 * Functions to glue a bunch of other backends into a single tree.
19 * All of the glued backends must share a common suffix. E.g., you
20 * can glue o=foo and ou=bar,o=foo but you can't glue o=foo and o=bar.
22 * This uses the backend structures and routines extensively, but is
23 * not an actual backend of its own. To use it you must add a "subordinate"
24 * keyword to the configuration of other backends. Subordinates will
25 * automatically be connected to their parent backend.
27 * The purpose of these functions is to allow you to split a single database
28 * into pieces (for load balancing purposes, whatever) but still be able
29 * to treat it as a single database after it's been split. As such, each
30 * of the glued backends should have identical rootdn and rootpw.
32 * If you need more elaborate configuration, you probably should be using
41 #include <ac/string.h>
42 #include <ac/socket.h>
47 typedef struct gluenode {
52 typedef struct glueinfo {
60 static BackendDB *glueBack;
62 static slap_response glue_back_response;
64 /* Just like select_backend, but only for our backends */
71 glueinfo *gi = (glueinfo *) be->bd_info;
75 bv.bv_len = strlen(dn);
76 bv.bv_val = (char *) dn;
78 for (i = 0; i<gi->gi_nodes; i++) {
79 assert( gi->gi_n[i].gn_be->be_nsuffix );
81 if (dnIsSuffix(&bv, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
82 return gi->gi_n[i].gn_be;
88 /* This function will only be called in tool mode */
95 static int glueOpened = 0;
97 if (glueOpened) return 0;
101 /* If we were invoked in tool mode, open all the underlying backends */
102 if (slapMode & SLAP_TOOL_MODE) {
103 rc = backend_startup (NULL);
104 } /* other case is impossible */
108 /* This function will only be called in tool mode */
114 static int glueClosed = 0;
117 if (glueClosed) return 0;
121 if (slapMode & SLAP_TOOL_MODE) {
122 rc = backend_shutdown (NULL);
132 glueinfo *gi = (glueinfo *) be->bd_info;
133 static int glueOpened = 0;
136 if (glueOpened) return 0;
140 gi->gi_bd.be_acl = be->be_acl;
141 gi->gi_bd.be_pending_csn_list = be->be_pending_csn_list;
142 gi->gi_bd.be_context_csn = be->be_context_csn;
144 if (gi->gi_bd.bd_info->bi_db_open)
145 rc = gi->gi_bd.bd_info->bi_db_open(&gi->gi_bd);
155 glueinfo *gi = (glueinfo *) be->bd_info;
156 static int glueClosed = 0;
158 if (glueClosed) return 0;
162 /* Close the master */
163 if (gi->gi_bd.bd_info->bi_db_close)
164 gi->gi_bd.bd_info->bi_db_close( &gi->gi_bd );
170 glue_back_db_destroy (
174 glueinfo *gi = (glueinfo *) be->bd_info;
176 if (gi->gi_bd.bd_info->bi_db_destroy)
177 gi->gi_bd.bd_info->bi_db_destroy( &gi->gi_bd );
182 typedef struct glue_state {
192 glue_back_response ( Operation *op, SlapReply *rs )
194 glue_state *gs = op->o_callback->sc_private;
196 switch(rs->sr_type) {
198 if ( gs->slimit != SLAP_NO_LIMIT
199 && rs->sr_nentries >= gs->slimit )
201 rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
206 return SLAP_CB_CONTINUE;
209 if (rs->sr_err == LDAP_SUCCESS ||
210 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
211 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
212 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
213 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
214 gs->err != LDAP_SUCCESS)
215 gs->err = rs->sr_err;
216 if (gs->err == LDAP_SUCCESS && gs->matched) {
217 ch_free (gs->matched);
221 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
223 len = strlen (rs->sr_matched);
224 if (len > gs->matchlen) {
226 ch_free (gs->matched);
227 gs->matched = ch_strdup (rs->sr_matched);
235 for (i=0; rs->sr_ref[i].bv_val; i++);
239 new = ch_malloc ((i+1)*sizeof(struct berval));
241 new = ch_realloc(gs->refs,
242 (j+i+1)*sizeof(struct berval));
244 for (k=0; k<i; j++,k++) {
245 ber_dupbv( &new[j], &rs->sr_ref[k] );
247 new[j].bv_val = NULL;
256 glue_back_search ( Operation *op, SlapReply *rs )
258 BackendDB *b0 = op->o_bd;
259 BackendDB *b1 = NULL;
260 glueinfo *gi = (glueinfo *) b0->bd_info;
263 glue_state gs = {0, 0, 0, NULL, 0, NULL};
264 slap_callback cb = { NULL, glue_back_response, NULL, NULL };
265 int scope0, slimit0, tlimit0;
266 struct berval dn, ndn;
270 cb.sc_next = op->o_callback;
272 stoptime = slap_get_time () + op->ors_tlimit;
274 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
276 switch (op->ors_scope) {
277 case LDAP_SCOPE_BASE:
278 if (op->o_bd && op->o_bd->be_search) {
279 rs->sr_err = op->o_bd->be_search( op, rs );
281 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
282 "No search target found");
286 case LDAP_SCOPE_ONELEVEL:
287 case LDAP_SCOPE_SUBTREE:
288 #ifdef LDAP_SCOPE_SUBORDINATE
289 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
292 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
293 if (op->o_bd && op->o_bd->be_search) {
294 rs->sr_err = op->o_bd->be_search( op, rs );
296 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
297 "No search target found");
302 op->o_callback = &cb;
303 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
304 scope0 = op->ors_scope;
305 slimit0 = gs.slimit = op->ors_slimit;
306 tlimit0 = op->ors_tlimit;
312 * Execute in reverse order, most general first
314 for (i = gi->gi_nodes-1; i >= 0; i--) {
315 if (!gi->gi_n[i].gn_be || !gi->gi_n[i].gn_be->be_search)
317 if (!dnIsSuffix(&gi->gi_n[i].gn_be->be_nsuffix[0], &b1->be_nsuffix[0]))
319 if (tlimit0 != SLAP_NO_LIMIT) {
320 op->ors_tlimit = stoptime - slap_get_time ();
321 if (op->ors_tlimit <= 0) {
322 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
326 if (slimit0 != SLAP_NO_LIMIT) {
327 op->ors_slimit = slimit0 - rs->sr_nentries;
328 if (op->ors_slimit < 0) {
329 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
340 op->o_bd = gi->gi_n[i].gn_be;
342 assert( op->o_bd->be_suffix );
343 assert( op->o_bd->be_nsuffix );
345 if (scope0 == LDAP_SCOPE_ONELEVEL &&
346 dn_match(&gi->gi_n[i].gn_pdn, &ndn))
348 op->ors_scope = LDAP_SCOPE_BASE;
349 op->o_req_dn = op->o_bd->be_suffix[0];
350 op->o_req_ndn = op->o_bd->be_nsuffix[0];
351 rs->sr_err = op->o_bd->be_search(op, rs);
353 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
354 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
356 rs->sr_err = op->o_bd->be_search( op, rs );
358 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
359 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
361 op->o_req_dn = op->o_bd->be_suffix[0];
362 op->o_req_ndn = op->o_bd->be_nsuffix[0];
363 rs->sr_err = op->o_bd->be_search( op, rs );
364 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
365 gs.err = LDAP_SUCCESS;
368 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
369 rs->sr_err = op->o_bd->be_search( op, rs );
375 * Add errors that should result in dropping
378 case LDAP_SIZELIMIT_EXCEEDED:
379 case LDAP_TIMELIMIT_EXCEEDED:
380 case LDAP_ADMINLIMIT_EXCEEDED:
381 case LDAP_NO_SUCH_OBJECT:
389 op->ors_scope = scope0;
390 op->ors_slimit = slimit0;
391 op->ors_tlimit = tlimit0;
397 if ( !op->o_abandon ) {
398 op->o_callback = cb.sc_next;
400 rs->sr_matched = gs.matched;
401 rs->sr_ref = gs.refs;
403 send_ldap_result( op, rs );
410 ber_bvarray_free(gs.refs);
416 glue_tool_entry_open (
421 /* We don't know which backend to talk to yet, so just
422 * remember the mode and move on...
432 glue_tool_entry_close (
439 if (!glueBack->be_entry_close)
441 rc = glueBack->be_entry_close (glueBack);
447 glue_tool_entry_first (
451 glueinfo *gi = (glueinfo *) b0->bd_info;
454 /* If we're starting from scratch, start at the most general */
456 for (i = gi->gi_nodes-1; i >= 0; i--) {
457 if (gi->gi_n[i].gn_be->be_entry_open &&
458 gi->gi_n[i].gn_be->be_entry_first) {
459 glueBack = gi->gi_n[i].gn_be;
465 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
466 glueBack->be_entry_open (glueBack, glueMode) != 0)
469 return glueBack->be_entry_first (glueBack);
473 glue_tool_entry_next (
477 glueinfo *gi = (glueinfo *) b0->bd_info;
481 if (!glueBack || !glueBack->be_entry_next)
484 rc = glueBack->be_entry_next (glueBack);
486 /* If we ran out of entries in one database, move on to the next */
488 if ( glueBack && glueBack->be_entry_close )
489 glueBack->be_entry_close (glueBack);
490 for (i=0; i<gi->gi_nodes; i++) {
491 if (gi->gi_n[i].gn_be == glueBack)
498 glueBack = gi->gi_n[i-1].gn_be;
499 rc = glue_tool_entry_first (b0);
506 glue_tool_entry_get (
511 if (!glueBack || !glueBack->be_entry_get)
514 return glueBack->be_entry_get (glueBack, id);
518 glue_tool_entry_put (
527 be = glue_back_select (b0, e->e_ndn);
528 if (!be->be_entry_put)
532 rc = be->be_entry_open (be, glueMode);
535 } else if (be != glueBack) {
536 /* If this entry belongs in a different branch than the
537 * previous one, close the current database and open the
540 glueBack->be_entry_close (glueBack);
541 rc = be->be_entry_open (be, glueMode);
546 return be->be_entry_put (be, e, text);
550 glue_tool_entry_reindex (
555 if (!glueBack || !glueBack->be_entry_reindex)
558 return glueBack->be_entry_reindex (glueBack, id);
566 glueinfo *gi = (glueinfo *) b0->bd_info;
569 /* just sync everyone */
570 for (i = 0; i<gi->gi_nodes; i++)
571 if (gi->gi_n[i].gn_be->be_sync)
572 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
580 int cont = num_subordinates;
582 BackendInfo *bi = NULL;
585 /* While there are subordinate backends, search backwards through the
586 * backends and connect them to their superior.
588 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
589 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
590 /* The last database cannot be a subordinate of noone */
591 if (i == nBackendDB - 1) {
592 SLAP_DBFLAGS(b1) ^= SLAP_DBFLAG_GLUE_SUBORDINATE;
597 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
598 if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
601 /* We will only link it once */
602 if ( SLAP_GLUE_LINKED( be ) ) {
605 assert( be->be_nsuffix );
606 assert( b1->be_nsuffix );
607 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
611 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_GLUE_LINKED;
613 /* We create a copy of the superior's be
614 * structure, pointing to all of its original
615 * information. Then we replace elements of
616 * the superior's info with our own. The copy
617 * is used whenever we have operations to pass
618 * down to the real database.
620 SLAP_DBFLAGS(b1) |= SLAP_DBFLAG_GLUE_INSTANCE;
621 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
624 gi->gi_bi = *b1->bd_info;
625 bi = (BackendInfo *)gi;
626 bi->bi_open = glue_back_open;
627 bi->bi_close = glue_back_close;
628 bi->bi_db_open = glue_back_db_open;
629 bi->bi_db_close = glue_back_db_close;
630 bi->bi_db_destroy = glue_back_db_destroy;
632 bi->bi_op_search = glue_back_search;
635 * hooks for slap tools
637 bi->bi_tool_entry_open = glue_tool_entry_open;
638 bi->bi_tool_entry_close = glue_tool_entry_close;
639 bi->bi_tool_entry_first = glue_tool_entry_first;
640 bi->bi_tool_entry_next = glue_tool_entry_next;
641 bi->bi_tool_entry_get = glue_tool_entry_get;
642 bi->bi_tool_entry_put = glue_tool_entry_put;
643 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
644 bi->bi_tool_sync = glue_tool_sync;
645 /* FIXME : will support later */
646 bi->bi_tool_dn2id_get = 0;
647 bi->bi_tool_id2entry_get = 0;
648 bi->bi_tool_entry_modify = 0;
650 gi = (glueinfo *)ch_realloc(gi,
652 gi->gi_nodes * sizeof(gluenode));
654 gi->gi_n[gi->gi_nodes].gn_be = be;
655 dnParent( &be->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
659 /* One more node for the master */
660 gi = (glueinfo *)ch_realloc(gi,
661 sizeof(glueinfo) + gi->gi_nodes * sizeof(gluenode));
662 gi->gi_n[gi->gi_nodes].gn_be = &gi->gi_bd;
663 dnParent( &b1->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
665 b1->bd_info = (BackendInfo *)gi;
668 /* If there are any unresolved subordinates left, something is wrong */