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->nodes; i++) {
79 assert( gi->n[i].be->be_nsuffix );
81 if (dnIsSuffix(&bv, &gi->n[i].be->be_nsuffix[0])) {
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->bd.be_acl = be->be_acl;
142 if (gi->bd.bd_info->bi_db_open)
143 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
153 glueinfo *gi = (glueinfo *) be->bd_info;
154 static int glueClosed = 0;
156 if (glueClosed) return 0;
160 /* Close the master */
161 if (gi->bd.bd_info->bi_db_close)
162 gi->bd.bd_info->bi_db_close( &gi->bd );
168 glue_back_db_destroy (
172 glueinfo *gi = (glueinfo *) be->bd_info;
174 if (gi->bd.bd_info->bi_db_destroy)
175 gi->bd.bd_info->bi_db_destroy( &gi->bd );
180 typedef struct glue_state {
190 glue_back_response ( Operation *op, SlapReply *rs )
192 glue_state *gs = op->o_callback->sc_private;
194 switch(rs->sr_type) {
196 if ( gs->slimit && rs->sr_nentries >= gs->slimit ) {
197 rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
202 return SLAP_CB_CONTINUE;
205 if (rs->sr_err == LDAP_SUCCESS ||
206 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
207 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
208 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
209 gs->err != LDAP_SUCCESS)
210 gs->err = rs->sr_err;
211 if (gs->err == LDAP_SUCCESS && gs->matched) {
212 ch_free (gs->matched);
216 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
218 len = strlen (rs->sr_matched);
219 if (len > gs->matchlen) {
221 ch_free (gs->matched);
222 gs->matched = ch_strdup (rs->sr_matched);
230 for (i=0; rs->sr_ref[i].bv_val; i++);
234 new = ch_malloc ((i+1)*sizeof(struct berval));
236 new = ch_realloc(gs->refs,
237 (j+i+1)*sizeof(struct berval));
239 for (k=0; k<i; j++,k++) {
240 ber_dupbv( &new[j], &rs->sr_ref[k] );
242 new[j].bv_val = NULL;
251 glue_back_search ( Operation *op, SlapReply *rs )
253 BackendDB *b0 = op->o_bd;
254 glueinfo *gi = (glueinfo *) b0->bd_info;
257 glue_state gs = {0, 0, 0, NULL, 0, NULL};
258 slap_callback cb = { NULL, glue_back_response, NULL, NULL };
259 int scope0, slimit0, tlimit0;
260 struct berval dn, ndn;
264 cb.sc_next = op->o_callback;
266 if (op->ors_tlimit) {
267 stoptime = slap_get_time () + op->ors_tlimit;
270 switch (op->ors_scope) {
271 case LDAP_SCOPE_BASE:
272 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
274 if (op->o_bd && op->o_bd->be_search) {
275 rs->sr_err = op->o_bd->be_search( op, rs );
277 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
278 "No search target found");
282 case LDAP_SCOPE_ONELEVEL:
283 case LDAP_SCOPE_SUBTREE:
284 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
285 op->o_callback = &cb;
286 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
287 scope0 = op->ors_scope;
288 slimit0 = gs.slimit = op->ors_slimit;
289 tlimit0 = op->ors_tlimit;
294 * Execute in reverse order, most general first
296 for (i = gi->nodes-1; i >= 0; i--) {
297 if (!gi->n[i].be || !gi->n[i].be->be_search)
300 op->ors_tlimit = stoptime - slap_get_time ();
301 if (op->ors_tlimit <= 0) {
302 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
307 op->ors_slimit = slimit0 - rs->sr_nentries;
308 if (op->ors_slimit < 0) {
309 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
320 op->o_bd = gi->n[i].be;
322 assert( op->o_bd->be_suffix );
323 assert( op->o_bd->be_nsuffix );
325 if (scope0 == LDAP_SCOPE_ONELEVEL &&
326 dn_match(&gi->n[i].pdn, &ndn))
328 op->ors_scope = LDAP_SCOPE_BASE;
329 op->o_req_dn = op->o_bd->be_suffix[0];
330 op->o_req_ndn = op->o_bd->be_nsuffix[0];
331 rs->sr_err = op->o_bd->be_search(op, rs);
333 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
334 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
336 op->o_req_dn = op->o_bd->be_suffix[0];
337 op->o_req_ndn = op->o_bd->be_nsuffix[0];
338 rs->sr_err = op->o_bd->be_search( op, rs );
340 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
341 rs->sr_err = op->o_bd->be_search( op, rs );
347 * Add errors that should result in dropping
350 case LDAP_SIZELIMIT_EXCEEDED:
351 case LDAP_TIMELIMIT_EXCEEDED:
352 case LDAP_ADMINLIMIT_EXCEEDED:
360 op->ors_scope = scope0;
361 op->ors_slimit = slimit0;
362 op->ors_tlimit = tlimit0;
368 op->o_callback = cb.sc_next;
370 rs->sr_matched = gs.matched;
371 rs->sr_ref = gs.refs;
373 send_ldap_result( op, rs );
380 ber_bvarray_free(gs.refs);
386 glue_tool_entry_open (
391 /* We don't know which backend to talk to yet, so just
392 * remember the mode and move on...
402 glue_tool_entry_close (
409 if (!glueBack->be_entry_close)
411 rc = glueBack->be_entry_close (glueBack);
417 glue_tool_entry_first (
421 glueinfo *gi = (glueinfo *) b0->bd_info;
424 /* If we're starting from scratch, start at the most general */
426 for (i = gi->nodes-1; i >= 0; i--) {
427 if (gi->n[i].be->be_entry_open &&
428 gi->n[i].be->be_entry_first) {
429 glueBack = gi->n[i].be;
435 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
436 glueBack->be_entry_open (glueBack, glueMode) != 0)
439 return glueBack->be_entry_first (glueBack);
443 glue_tool_entry_next (
447 glueinfo *gi = (glueinfo *) b0->bd_info;
451 if (!glueBack || !glueBack->be_entry_next)
454 rc = glueBack->be_entry_next (glueBack);
456 /* If we ran out of entries in one database, move on to the next */
458 if ( glueBack && glueBack->be_entry_close )
459 glueBack->be_entry_close (glueBack);
460 for (i=0; i<gi->nodes; i++) {
461 if (gi->n[i].be == glueBack)
468 glueBack = gi->n[i-1].be;
469 rc = glue_tool_entry_first (b0);
476 glue_tool_entry_get (
481 if (!glueBack || !glueBack->be_entry_get)
484 return glueBack->be_entry_get (glueBack, id);
488 glue_tool_entry_put (
497 be = glue_back_select (b0, e->e_ndn);
498 if (!be->be_entry_put)
502 rc = be->be_entry_open (be, glueMode);
505 } else if (be != glueBack) {
506 /* If this entry belongs in a different branch than the
507 * previous one, close the current database and open the
510 glueBack->be_entry_close (glueBack);
511 rc = be->be_entry_open (be, glueMode);
516 return be->be_entry_put (be, e, text);
520 glue_tool_entry_reindex (
525 if (!glueBack || !glueBack->be_entry_reindex)
528 return glueBack->be_entry_reindex (glueBack, id);
536 glueinfo *gi = (glueinfo *) b0->bd_info;
539 /* just sync everyone */
540 for (i = 0; i<gi->nodes; i++)
541 if (gi->n[i].be->be_sync)
542 gi->n[i].be->be_sync (gi->n[i].be);
550 int cont = num_subordinates;
552 BackendInfo *bi = NULL;
555 /* While there are subordinate backends, search backwards through the
556 * backends and connect them to their superior.
558 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
559 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
560 /* The last database cannot be a subordinate of noone */
561 if (i == nBackendDB - 1) {
562 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
567 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
568 if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
571 /* We will only link it once */
572 if ( SLAP_GLUE_LINKED( be ) ) {
575 assert( be->be_nsuffix );
576 assert( b1->be_nsuffix );
577 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
581 be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
583 /* We create a copy of the superior's be
584 * structure, pointing to all of its original
585 * information. Then we replace elements of
586 * the superior's info with our own. The copy
587 * is used whenever we have operations to pass
588 * down to the real database.
590 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
591 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
594 gi->bi = *b1->bd_info;
595 bi = (BackendInfo *)gi;
596 bi->bi_open = glue_back_open;
597 bi->bi_close = glue_back_close;
598 bi->bi_db_open = glue_back_db_open;
599 bi->bi_db_close = glue_back_db_close;
600 bi->bi_db_destroy = glue_back_db_destroy;
602 bi->bi_op_search = glue_back_search;
605 * hooks for slap tools
607 bi->bi_tool_entry_open = glue_tool_entry_open;
608 bi->bi_tool_entry_close = glue_tool_entry_close;
609 bi->bi_tool_entry_first = glue_tool_entry_first;
610 bi->bi_tool_entry_next = glue_tool_entry_next;
611 bi->bi_tool_entry_get = glue_tool_entry_get;
612 bi->bi_tool_entry_put = glue_tool_entry_put;
613 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
614 bi->bi_tool_sync = glue_tool_sync;
615 /* FIXME : will support later */
616 bi->bi_tool_dn2id_get = 0;
617 bi->bi_tool_id2entry_get = 0;
618 bi->bi_tool_entry_modify = 0;
620 gi = (glueinfo *)ch_realloc(gi,
622 gi->nodes * sizeof(gluenode));
624 gi->n[gi->nodes].be = be;
625 dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn );
629 /* One more node for the master */
630 gi = (glueinfo *)ch_realloc(gi,
631 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
632 gi->n[gi->nodes].be = &gi->bd;
633 dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
635 b1->bd_info = (BackendInfo *)gi;
638 /* If there are any unresolved subordinates left, something is wrong */