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;
240 glue_back_search ( Operation *op, SlapReply *rs )
242 BackendDB *b0 = op->o_bd;
243 glueinfo *gi = (glueinfo *) b0->bd_info;
246 glue_state gs = {0, 0, NULL, 0, NULL, NULL};
247 slap_callback cb = { glue_back_response };
248 int scope0, slimit0, tlimit0;
249 struct berval dn, ndn;
253 gs.prevcb = op->o_callback;
255 if (op->ors_tlimit) {
256 stoptime = slap_get_time () + op->ors_tlimit;
259 switch (op->ors_scope) {
260 case LDAP_SCOPE_BASE:
261 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
263 if (op->o_bd && op->o_bd->be_search) {
264 rs->sr_err = op->o_bd->be_search( op, rs );
266 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
267 "No search target found");
271 case LDAP_SCOPE_ONELEVEL:
272 case LDAP_SCOPE_SUBTREE:
273 op->o_callback = &cb;
274 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
275 scope0 = op->ors_scope;
276 slimit0 = op->ors_slimit;
277 tlimit0 = op->ors_tlimit;
282 * Execute in reverse order, most general first
284 for (i = gi->nodes-1; i >= 0; i--) {
285 if (!gi->n[i].be || !gi->n[i].be->be_search)
288 op->ors_tlimit = stoptime - slap_get_time ();
289 if (op->ors_tlimit <= 0) {
290 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
295 op->ors_slimit = slimit0 - rs->sr_nentries;
296 if (op->ors_slimit <= 0) {
297 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
308 op->o_bd = gi->n[i].be;
309 if (scope0 == LDAP_SCOPE_ONELEVEL &&
310 dn_match(&gi->n[i].pdn, &ndn)) {
311 op->ors_scope = LDAP_SCOPE_BASE;
312 op->o_req_dn = op->o_bd->be_suffix[0];
313 op->o_req_ndn = op->o_bd->be_nsuffix[0];
314 rs->sr_err = op->o_bd->be_search(op, rs);
316 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
317 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn)) {
318 op->o_req_dn = op->o_bd->be_suffix[0];
319 op->o_req_ndn = op->o_bd->be_nsuffix[0];
320 rs->sr_err = op->o_bd->be_search( op, rs );
322 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
323 rs->sr_err = op->o_bd->be_search( op, rs );
329 * Add errors that should result in dropping
332 case LDAP_SIZELIMIT_EXCEEDED:
333 case LDAP_TIMELIMIT_EXCEEDED:
334 case LDAP_ADMINLIMIT_EXCEEDED:
342 op->ors_scope = scope0;
343 op->ors_slimit = slimit0;
344 op->ors_tlimit = tlimit0;
350 op->o_callback = gs.prevcb;
352 rs->sr_matched = gs.matched;
353 rs->sr_ref = gs.refs;
355 send_ldap_result( op, rs );
362 ber_bvarray_free(gs.refs);
368 glue_tool_entry_open (
373 /* We don't know which backend to talk to yet, so just
374 * remember the mode and move on...
384 glue_tool_entry_close (
391 if (!glueBack->be_entry_close)
393 rc = glueBack->be_entry_close (glueBack);
399 glue_tool_entry_first (
403 glueinfo *gi = (glueinfo *) b0->bd_info;
406 /* If we're starting from scratch, start at the most general */
408 for (i = gi->nodes-1; i >= 0; i--) {
409 if (gi->n[i].be->be_entry_open &&
410 gi->n[i].be->be_entry_first) {
411 glueBack = gi->n[i].be;
417 if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
420 return glueBack->be_entry_first (glueBack);
424 glue_tool_entry_next (
428 glueinfo *gi = (glueinfo *) b0->bd_info;
432 if (!glueBack || !glueBack->be_entry_next)
435 rc = glueBack->be_entry_next (glueBack);
437 /* If we ran out of entries in one database, move on to the next */
439 glueBack->be_entry_close (glueBack);
440 for (i=0; i<gi->nodes; i++) {
441 if (gi->n[i].be == glueBack)
448 glueBack = gi->n[i-1].be;
449 rc = glue_tool_entry_first (b0);
456 glue_tool_entry_get (
461 if (!glueBack || !glueBack->be_entry_get)
464 return glueBack->be_entry_get (glueBack, id);
468 glue_tool_entry_put (
477 be = glue_back_select (b0, e->e_ndn);
478 if (!be->be_entry_put)
482 rc = be->be_entry_open (be, glueMode);
485 } else if (be != glueBack) {
486 /* If this entry belongs in a different branch than the
487 * previous one, close the current database and open the
490 glueBack->be_entry_close (glueBack);
491 rc = be->be_entry_open (be, glueMode);
496 return be->be_entry_put (be, e, text);
500 glue_tool_entry_reindex (
505 if (!glueBack || !glueBack->be_entry_reindex)
508 return glueBack->be_entry_reindex (glueBack, id);
516 glueinfo *gi = (glueinfo *) b0->bd_info;
519 /* just sync everyone */
520 for (i = 0; i<gi->nodes; i++)
521 if (gi->n[i].be->be_sync)
522 gi->n[i].be->be_sync (gi->n[i].be);
530 int cont = num_subordinates;
532 BackendInfo *bi = NULL;
535 /* While there are subordinate backends, search backwards through the
536 * backends and connect them to their superior.
538 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
539 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
540 /* The last database cannot be a subordinate of noone */
541 if (i == nBackendDB - 1) {
542 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
547 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
548 if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
551 /* We will only link it once */
552 if ( SLAP_GLUE_LINKED( be ) ) {
555 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
559 be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
561 /* We create a copy of the superior's be
562 * structure, pointing to all of its original
563 * information. Then we replace elements of
564 * the superior's info with our own. The copy
565 * is used whenever we have operations to pass
566 * down to the real database.
568 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
569 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
572 gi->bi = *b1->bd_info;
573 bi = (BackendInfo *)gi;
574 bi->bi_open = glue_back_open;
575 bi->bi_close = glue_back_close;
576 bi->bi_db_open = glue_back_db_open;
577 bi->bi_db_close = glue_back_db_close;
578 bi->bi_db_destroy = glue_back_db_destroy;
580 bi->bi_op_search = glue_back_search;
583 * hooks for slap tools
585 bi->bi_tool_entry_open = glue_tool_entry_open;
586 bi->bi_tool_entry_close = glue_tool_entry_close;
587 bi->bi_tool_entry_first = glue_tool_entry_first;
588 bi->bi_tool_entry_next = glue_tool_entry_next;
589 bi->bi_tool_entry_get = glue_tool_entry_get;
590 bi->bi_tool_entry_put = glue_tool_entry_put;
591 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
592 bi->bi_tool_sync = glue_tool_sync;
594 gi = (glueinfo *)ch_realloc(gi,
596 gi->nodes * sizeof(gluenode));
598 gi->n[gi->nodes].be = be;
599 dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn );
603 /* One more node for the master */
604 gi = (glueinfo *)ch_realloc(gi,
605 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
606 gi->n[gi->nodes].be = &gi->bd;
607 dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
609 b1->bd_info = (BackendInfo *)gi;
612 /* If there are any unresolved subordinates left, something is wrong */