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 static slap_response glue_back_response;
55 /* Just like select_backend, but only for our backends */
62 glueinfo *gi = (glueinfo *) be->bd_info;
66 bv.bv_len = strlen(dn);
67 bv.bv_val = (char *) dn;
69 for (i = 0; i<gi->nodes; i++) {
70 if (dnIsSuffix(&bv, &gi->n[i].be->be_nsuffix[0])) {
77 /* This function will only be called in tool mode */
84 static int glueOpened = 0;
86 if (glueOpened) return 0;
90 /* If we were invoked in tool mode, open all the underlying backends */
91 if (slapMode & SLAP_TOOL_MODE) {
92 rc = backend_startup (NULL);
93 } /* other case is impossible */
97 /* This function will only be called in tool mode */
103 static int glueClosed = 0;
106 if (glueClosed) return 0;
110 if (slapMode & SLAP_TOOL_MODE) {
111 rc = backend_shutdown (NULL);
121 glueinfo *gi = (glueinfo *) be->bd_info;
122 static int glueOpened = 0;
125 if (glueOpened) return 0;
129 gi->bd.be_acl = be->be_acl;
131 if (gi->bd.bd_info->bi_db_open)
132 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
142 glueinfo *gi = (glueinfo *) be->bd_info;
143 static int glueClosed = 0;
145 if (glueClosed) return 0;
149 /* Close the master */
150 if (gi->bd.bd_info->bi_db_close)
151 gi->bd.bd_info->bi_db_close( &gi->bd );
157 glue_back_db_destroy (
161 glueinfo *gi = (glueinfo *) be->bd_info;
163 if (gi->bd.bd_info->bi_db_destroy)
164 gi->bd.bd_info->bi_db_destroy( &gi->bd );
169 typedef struct glue_state {
175 slap_callback *prevcb;
179 glue_back_response ( Operation *op, SlapReply *rs )
181 glue_state *gs = op->o_callback->sc_private;
182 slap_callback *tmp = op->o_callback;
184 switch(rs->sr_type) {
187 op->o_callback = gs->prevcb;
188 if (op->o_callback && op->o_callback->sc_response) {
189 rs->sr_err = op->o_callback->sc_response( op, rs );
190 } else rs->sr_err = SLAP_CB_CONTINUE;
191 op->o_callback = tmp;
195 if (rs->sr_err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
196 gs->err = rs->sr_err;
197 if (gs->err == LDAP_SUCCESS && gs->matched) {
198 ch_free (gs->matched);
202 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
204 len = strlen (rs->sr_matched);
205 if (len > gs->matchlen) {
207 ch_free (gs->matched);
208 gs->matched = ch_strdup (rs->sr_matched);
216 for (i=0; rs->sr_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], &rs->sr_ref[k] );
228 new[j].bv_val = NULL;
237 glue_back_search ( Operation *op, SlapReply *rs )
239 BackendDB *b0 = op->o_bd;
240 glueinfo *gi = (glueinfo *) b0->bd_info;
243 glue_state gs = {0, 0, NULL, 0, NULL, NULL};
244 slap_callback cb = { glue_back_response, NULL };
245 int scope0, slimit0, tlimit0;
246 struct berval dn, ndn;
250 gs.prevcb = op->o_callback;
252 if (op->ors_tlimit) {
253 stoptime = slap_get_time () + op->ors_tlimit;
256 switch (op->ors_scope) {
257 case LDAP_SCOPE_BASE:
258 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
260 if (op->o_bd && op->o_bd->be_search) {
261 rs->sr_err = op->o_bd->be_search( op, rs );
263 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
264 "No search target found");
268 case LDAP_SCOPE_ONELEVEL:
269 case LDAP_SCOPE_SUBTREE:
270 op->o_callback = &cb;
271 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
272 scope0 = op->ors_scope;
273 slimit0 = op->ors_slimit;
274 tlimit0 = op->ors_tlimit;
279 * Execute in reverse order, most general first
281 for (i = gi->nodes-1; i >= 0; i--) {
282 if (!gi->n[i].be || !gi->n[i].be->be_search)
285 op->ors_tlimit = stoptime - slap_get_time ();
286 if (op->ors_tlimit <= 0) {
287 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
292 op->ors_slimit = slimit0 - rs->sr_nentries;
293 if (op->ors_slimit <= 0) {
294 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
305 op->o_bd = gi->n[i].be;
306 if (scope0 == LDAP_SCOPE_ONELEVEL &&
307 dn_match(&gi->n[i].pdn, &ndn)) {
308 op->ors_scope = LDAP_SCOPE_BASE;
309 op->o_req_dn = op->o_bd->be_suffix[0];
310 op->o_req_ndn = op->o_bd->be_nsuffix[0];
311 rs->sr_err = op->o_bd->be_search(op, rs);
313 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
314 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn)) {
315 op->o_req_dn = op->o_bd->be_suffix[0];
316 op->o_req_ndn = op->o_bd->be_nsuffix[0];
317 rs->sr_err = op->o_bd->be_search( op, rs );
319 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
320 rs->sr_err = op->o_bd->be_search( op, rs );
326 * Add errors that should result in dropping
329 case LDAP_SIZELIMIT_EXCEEDED:
330 case LDAP_TIMELIMIT_EXCEEDED:
331 case LDAP_ADMINLIMIT_EXCEEDED:
339 op->ors_scope = scope0;
340 op->ors_slimit = slimit0;
341 op->ors_tlimit = tlimit0;
347 op->o_callback = gs.prevcb;
349 rs->sr_matched = gs.matched;
350 rs->sr_ref = gs.refs;
352 send_ldap_result( op, rs );
359 ber_bvarray_free(gs.refs);
365 glue_tool_entry_open (
370 /* We don't know which backend to talk to yet, so just
371 * remember the mode and move on...
381 glue_tool_entry_close (
388 if (!glueBack->be_entry_close)
390 rc = glueBack->be_entry_close (glueBack);
396 glue_tool_entry_first (
400 glueinfo *gi = (glueinfo *) b0->bd_info;
403 /* If we're starting from scratch, start at the most general */
405 for (i = gi->nodes-1; i >= 0; i--) {
406 if (gi->n[i].be->be_entry_open &&
407 gi->n[i].be->be_entry_first) {
408 glueBack = gi->n[i].be;
414 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
417 return glueBack->be_entry_first (glueBack);
421 glue_tool_entry_next (
425 glueinfo *gi = (glueinfo *) b0->bd_info;
429 if (!glueBack || !glueBack->be_entry_next)
432 rc = glueBack->be_entry_next (glueBack);
434 /* If we ran out of entries in one database, move on to the next */
436 glueBack->be_entry_close (glueBack);
437 for (i=0; i<gi->nodes; i++) {
438 if (gi->n[i].be == glueBack)
445 glueBack = gi->n[i-1].be;
446 rc = glue_tool_entry_first (b0);
453 glue_tool_entry_get (
458 if (!glueBack || !glueBack->be_entry_get)
461 return glueBack->be_entry_get (glueBack, id);
465 glue_tool_entry_put (
474 be = glue_back_select (b0, e->e_ndn);
475 if (!be->be_entry_put)
479 rc = be->be_entry_open (be, glueMode);
482 } else if (be != glueBack) {
483 /* If this entry belongs in a different branch than the
484 * previous one, close the current database and open the
487 glueBack->be_entry_close (glueBack);
488 rc = be->be_entry_open (be, glueMode);
493 return be->be_entry_put (be, e, text);
497 glue_tool_entry_reindex (
502 if (!glueBack || !glueBack->be_entry_reindex)
505 return glueBack->be_entry_reindex (glueBack, id);
513 glueinfo *gi = (glueinfo *) b0->bd_info;
516 /* just sync everyone */
517 for (i = 0; i<gi->nodes; i++)
518 if (gi->n[i].be->be_sync)
519 gi->n[i].be->be_sync (gi->n[i].be);
527 int cont = num_subordinates;
529 BackendInfo *bi = NULL;
532 /* While there are subordinate backends, search backwards through the
533 * backends and connect them to their superior.
535 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
536 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
537 /* The last database cannot be a subordinate of noone */
538 if (i == nBackendDB - 1) {
539 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
544 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
545 if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
548 /* We will only link it once */
549 if ( SLAP_GLUE_LINKED( be ) ) {
552 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
556 be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
558 /* We create a copy of the superior's be
559 * structure, pointing to all of its original
560 * information. Then we replace elements of
561 * the superior's info with our own. The copy
562 * is used whenever we have operations to pass
563 * down to the real database.
565 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
566 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
569 gi->bi = *b1->bd_info;
570 bi = (BackendInfo *)gi;
571 bi->bi_open = glue_back_open;
572 bi->bi_close = glue_back_close;
573 bi->bi_db_open = glue_back_db_open;
574 bi->bi_db_close = glue_back_db_close;
575 bi->bi_db_destroy = glue_back_db_destroy;
577 bi->bi_op_search = glue_back_search;
580 * hooks for slap tools
582 bi->bi_tool_entry_open = glue_tool_entry_open;
583 bi->bi_tool_entry_close = glue_tool_entry_close;
584 bi->bi_tool_entry_first = glue_tool_entry_first;
585 bi->bi_tool_entry_next = glue_tool_entry_next;
586 bi->bi_tool_entry_get = glue_tool_entry_get;
587 bi->bi_tool_entry_put = glue_tool_entry_put;
588 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
589 bi->bi_tool_sync = glue_tool_sync;
590 /* FIXME : will support later */
591 bi->bi_tool_dn2id_get = 0;
592 bi->bi_tool_id2entry_get = 0;
593 bi->bi_tool_entry_modify = 0;
595 gi = (glueinfo *)ch_realloc(gi,
597 gi->nodes * sizeof(gluenode));
599 gi->n[gi->nodes].be = be;
600 dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn );
604 /* One more node for the master */
605 gi = (glueinfo *)ch_realloc(gi,
606 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
607 gi->n[gi->nodes].be = &gi->bd;
608 dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
610 b1->bd_info = (BackendInfo *)gi;
613 /* If there are any unresolved subordinates left, something is wrong */