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 if (rs->sr_type == REP_SEARCH) {
191 rs->sr_err = send_search_entry( op, rs );
193 rs->sr_err = send_search_reference( op, rs );
195 op->o_callback = tmp;
199 if (rs->sr_err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
200 gs->err = rs->sr_err;
201 if (gs->err == LDAP_SUCCESS && gs->matched) {
202 ch_free (gs->matched);
206 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
208 len = strlen (rs->sr_matched);
209 if (len > gs->matchlen) {
211 ch_free (gs->matched);
212 gs->matched = ch_strdup (rs->sr_matched);
220 for (i=0; rs->sr_ref[i].bv_val; i++);
224 new = ch_malloc ((i+1)*sizeof(struct berval));
226 new = ch_realloc(gs->refs,
227 (j+i+1)*sizeof(struct berval));
229 for (k=0; k<i; j++,k++) {
230 ber_dupbv( &new[j], &rs->sr_ref[k] );
232 new[j].bv_val = NULL;
241 glue_back_search ( Operation *op, SlapReply *rs )
243 BackendDB *b0 = op->o_bd;
244 glueinfo *gi = (glueinfo *) b0->bd_info;
247 glue_state gs = {0, 0, NULL, 0, NULL, NULL};
248 slap_callback cb = { glue_back_response, NULL };
249 int scope0, slimit0, tlimit0;
250 struct berval dn, ndn;
254 gs.prevcb = op->o_callback;
256 if (op->ors_tlimit) {
257 stoptime = slap_get_time () + op->ors_tlimit;
260 switch (op->ors_scope) {
261 case LDAP_SCOPE_BASE:
262 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
264 if (op->o_bd && op->o_bd->be_search) {
265 rs->sr_err = op->o_bd->be_search( op, rs );
267 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
268 "No search target found");
272 case LDAP_SCOPE_ONELEVEL:
273 case LDAP_SCOPE_SUBTREE:
274 op->o_callback = &cb;
275 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
276 scope0 = op->ors_scope;
277 slimit0 = op->ors_slimit;
278 tlimit0 = op->ors_tlimit;
283 * Execute in reverse order, most general first
285 for (i = gi->nodes-1; i >= 0; i--) {
286 if (!gi->n[i].be || !gi->n[i].be->be_search)
289 op->ors_tlimit = stoptime - slap_get_time ();
290 if (op->ors_tlimit <= 0) {
291 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
296 op->ors_slimit = slimit0 - rs->sr_nentries;
297 if (op->ors_slimit <= 0) {
298 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
309 op->o_bd = gi->n[i].be;
310 if (scope0 == LDAP_SCOPE_ONELEVEL &&
311 dn_match(&gi->n[i].pdn, &ndn)) {
312 op->ors_scope = LDAP_SCOPE_BASE;
313 op->o_req_dn = op->o_bd->be_suffix[0];
314 op->o_req_ndn = op->o_bd->be_nsuffix[0];
315 rs->sr_err = op->o_bd->be_search(op, rs);
317 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
318 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn)) {
319 op->o_req_dn = op->o_bd->be_suffix[0];
320 op->o_req_ndn = op->o_bd->be_nsuffix[0];
321 rs->sr_err = op->o_bd->be_search( op, rs );
323 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
324 rs->sr_err = op->o_bd->be_search( op, rs );
330 * Add errors that should result in dropping
333 case LDAP_SIZELIMIT_EXCEEDED:
334 case LDAP_TIMELIMIT_EXCEEDED:
335 case LDAP_ADMINLIMIT_EXCEEDED:
343 op->ors_scope = scope0;
344 op->ors_slimit = slimit0;
345 op->ors_tlimit = tlimit0;
351 op->o_callback = gs.prevcb;
353 rs->sr_matched = gs.matched;
354 rs->sr_ref = gs.refs;
356 send_ldap_result( op, rs );
363 ber_bvarray_free(gs.refs);
369 glue_tool_entry_open (
374 /* We don't know which backend to talk to yet, so just
375 * remember the mode and move on...
385 glue_tool_entry_close (
392 if (!glueBack->be_entry_close)
394 rc = glueBack->be_entry_close (glueBack);
400 glue_tool_entry_first (
404 glueinfo *gi = (glueinfo *) b0->bd_info;
407 /* If we're starting from scratch, start at the most general */
409 for (i = gi->nodes-1; i >= 0; i--) {
410 if (gi->n[i].be->be_entry_open &&
411 gi->n[i].be->be_entry_first) {
412 glueBack = gi->n[i].be;
418 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
421 return glueBack->be_entry_first (glueBack);
425 glue_tool_entry_next (
429 glueinfo *gi = (glueinfo *) b0->bd_info;
433 if (!glueBack || !glueBack->be_entry_next)
436 rc = glueBack->be_entry_next (glueBack);
438 /* If we ran out of entries in one database, move on to the next */
440 glueBack->be_entry_close (glueBack);
441 for (i=0; i<gi->nodes; i++) {
442 if (gi->n[i].be == glueBack)
449 glueBack = gi->n[i-1].be;
450 rc = glue_tool_entry_first (b0);
457 glue_tool_entry_get (
462 if (!glueBack || !glueBack->be_entry_get)
465 return glueBack->be_entry_get (glueBack, id);
469 glue_tool_entry_put (
478 be = glue_back_select (b0, e->e_ndn);
479 if (!be->be_entry_put)
483 rc = be->be_entry_open (be, glueMode);
486 } else if (be != glueBack) {
487 /* If this entry belongs in a different branch than the
488 * previous one, close the current database and open the
491 glueBack->be_entry_close (glueBack);
492 rc = be->be_entry_open (be, glueMode);
497 return be->be_entry_put (be, e, text);
501 glue_tool_entry_reindex (
506 if (!glueBack || !glueBack->be_entry_reindex)
509 return glueBack->be_entry_reindex (glueBack, id);
517 glueinfo *gi = (glueinfo *) b0->bd_info;
520 /* just sync everyone */
521 for (i = 0; i<gi->nodes; i++)
522 if (gi->n[i].be->be_sync)
523 gi->n[i].be->be_sync (gi->n[i].be);
531 int cont = num_subordinates;
533 BackendInfo *bi = NULL;
536 /* While there are subordinate backends, search backwards through the
537 * backends and connect them to their superior.
539 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
540 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
541 /* The last database cannot be a subordinate of noone */
542 if (i == nBackendDB - 1) {
543 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
548 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
549 if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
552 /* We will only link it once */
553 if ( SLAP_GLUE_LINKED( be ) ) {
556 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
560 be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
562 /* We create a copy of the superior's be
563 * structure, pointing to all of its original
564 * information. Then we replace elements of
565 * the superior's info with our own. The copy
566 * is used whenever we have operations to pass
567 * down to the real database.
569 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
570 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
573 gi->bi = *b1->bd_info;
574 bi = (BackendInfo *)gi;
575 bi->bi_open = glue_back_open;
576 bi->bi_close = glue_back_close;
577 bi->bi_db_open = glue_back_db_open;
578 bi->bi_db_close = glue_back_db_close;
579 bi->bi_db_destroy = glue_back_db_destroy;
581 bi->bi_op_search = glue_back_search;
584 * hooks for slap tools
586 bi->bi_tool_entry_open = glue_tool_entry_open;
587 bi->bi_tool_entry_close = glue_tool_entry_close;
588 bi->bi_tool_entry_first = glue_tool_entry_first;
589 bi->bi_tool_entry_next = glue_tool_entry_next;
590 bi->bi_tool_entry_get = glue_tool_entry_get;
591 bi->bi_tool_entry_put = glue_tool_entry_put;
592 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
593 bi->bi_tool_sync = glue_tool_sync;
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 */