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;
141 gi->bd.be_pending_csn_list = be->be_pending_csn_list;
143 if (gi->bd.bd_info->bi_db_open)
144 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
154 glueinfo *gi = (glueinfo *) be->bd_info;
155 static int glueClosed = 0;
157 if (glueClosed) return 0;
161 /* Close the master */
162 if (gi->bd.bd_info->bi_db_close)
163 gi->bd.bd_info->bi_db_close( &gi->bd );
169 glue_back_db_destroy (
173 glueinfo *gi = (glueinfo *) be->bd_info;
175 if (gi->bd.bd_info->bi_db_destroy)
176 gi->bd.bd_info->bi_db_destroy( &gi->bd );
181 typedef struct glue_state {
191 glue_back_response ( Operation *op, SlapReply *rs )
193 glue_state *gs = op->o_callback->sc_private;
195 switch(rs->sr_type) {
197 if ( gs->slimit != -1 && rs->sr_nentries >= gs->slimit ) {
198 rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
203 return SLAP_CB_CONTINUE;
206 if (rs->sr_err == LDAP_SUCCESS ||
207 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
208 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
209 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
210 gs->err != LDAP_SUCCESS)
211 gs->err = rs->sr_err;
212 if (gs->err == LDAP_SUCCESS && gs->matched) {
213 ch_free (gs->matched);
217 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
219 len = strlen (rs->sr_matched);
220 if (len > gs->matchlen) {
222 ch_free (gs->matched);
223 gs->matched = ch_strdup (rs->sr_matched);
231 for (i=0; rs->sr_ref[i].bv_val; i++);
235 new = ch_malloc ((i+1)*sizeof(struct berval));
237 new = ch_realloc(gs->refs,
238 (j+i+1)*sizeof(struct berval));
240 for (k=0; k<i; j++,k++) {
241 ber_dupbv( &new[j], &rs->sr_ref[k] );
243 new[j].bv_val = NULL;
252 glue_back_search ( Operation *op, SlapReply *rs )
254 BackendDB *b0 = op->o_bd;
255 glueinfo *gi = (glueinfo *) b0->bd_info;
258 glue_state gs = {0, 0, 0, NULL, 0, NULL};
259 slap_callback cb = { NULL, glue_back_response, NULL, NULL };
260 int scope0, slimit0, tlimit0;
261 struct berval dn, ndn;
265 cb.sc_next = op->o_callback;
267 stoptime = slap_get_time () + op->ors_tlimit;
269 switch (op->ors_scope) {
270 case LDAP_SCOPE_BASE:
271 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
273 if (op->o_bd && op->o_bd->be_search) {
274 rs->sr_err = op->o_bd->be_search( op, rs );
276 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
277 "No search target found");
281 case LDAP_SCOPE_ONELEVEL:
282 case LDAP_SCOPE_SUBTREE:
283 #ifdef LDAP_SCOPE_SUBORDINATE
284 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
287 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
288 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
290 if (op->o_bd && op->o_bd->be_search) {
291 rs->sr_err = op->o_bd->be_search( op, rs );
293 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
294 "No search target found");
299 op->o_callback = &cb;
300 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
301 scope0 = op->ors_scope;
302 slimit0 = gs.slimit = op->ors_slimit;
303 tlimit0 = op->ors_tlimit;
308 * Execute in reverse order, most general first
310 for (i = gi->nodes-1; i >= 0; i--) {
311 if (!gi->n[i].be || !gi->n[i].be->be_search)
314 op->ors_tlimit = stoptime - slap_get_time ();
315 if (op->ors_tlimit <= 0) {
316 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
321 op->ors_slimit = slimit0 - rs->sr_nentries;
322 if (op->ors_slimit < 0) {
323 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
334 op->o_bd = gi->n[i].be;
336 assert( op->o_bd->be_suffix );
337 assert( op->o_bd->be_nsuffix );
339 if (scope0 == LDAP_SCOPE_ONELEVEL &&
340 dn_match(&gi->n[i].pdn, &ndn))
342 op->ors_scope = LDAP_SCOPE_BASE;
343 op->o_req_dn = op->o_bd->be_suffix[0];
344 op->o_req_ndn = op->o_bd->be_nsuffix[0];
345 rs->sr_err = op->o_bd->be_search(op, rs);
347 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
348 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
350 op->o_req_dn = op->o_bd->be_suffix[0];
351 op->o_req_ndn = op->o_bd->be_nsuffix[0];
352 rs->sr_err = op->o_bd->be_search( op, rs );
354 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
355 rs->sr_err = op->o_bd->be_search( op, rs );
361 * Add errors that should result in dropping
364 case LDAP_SIZELIMIT_EXCEEDED:
365 case LDAP_TIMELIMIT_EXCEEDED:
366 case LDAP_ADMINLIMIT_EXCEEDED:
374 op->ors_scope = scope0;
375 op->ors_slimit = slimit0;
376 op->ors_tlimit = tlimit0;
382 if ( !op->o_abandon ) {
383 op->o_callback = cb.sc_next;
385 rs->sr_matched = gs.matched;
386 rs->sr_ref = gs.refs;
388 send_ldap_result( op, rs );
395 ber_bvarray_free(gs.refs);
401 glue_tool_entry_open (
406 /* We don't know which backend to talk to yet, so just
407 * remember the mode and move on...
417 glue_tool_entry_close (
424 if (!glueBack->be_entry_close)
426 rc = glueBack->be_entry_close (glueBack);
432 glue_tool_entry_first (
436 glueinfo *gi = (glueinfo *) b0->bd_info;
439 /* If we're starting from scratch, start at the most general */
441 for (i = gi->nodes-1; i >= 0; i--) {
442 if (gi->n[i].be->be_entry_open &&
443 gi->n[i].be->be_entry_first) {
444 glueBack = gi->n[i].be;
450 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
451 glueBack->be_entry_open (glueBack, glueMode) != 0)
454 return glueBack->be_entry_first (glueBack);
458 glue_tool_entry_next (
462 glueinfo *gi = (glueinfo *) b0->bd_info;
466 if (!glueBack || !glueBack->be_entry_next)
469 rc = glueBack->be_entry_next (glueBack);
471 /* If we ran out of entries in one database, move on to the next */
473 if ( glueBack && glueBack->be_entry_close )
474 glueBack->be_entry_close (glueBack);
475 for (i=0; i<gi->nodes; i++) {
476 if (gi->n[i].be == glueBack)
483 glueBack = gi->n[i-1].be;
484 rc = glue_tool_entry_first (b0);
491 glue_tool_entry_get (
496 if (!glueBack || !glueBack->be_entry_get)
499 return glueBack->be_entry_get (glueBack, id);
503 glue_tool_entry_put (
512 be = glue_back_select (b0, e->e_ndn);
513 if (!be->be_entry_put)
517 rc = be->be_entry_open (be, glueMode);
520 } else if (be != glueBack) {
521 /* If this entry belongs in a different branch than the
522 * previous one, close the current database and open the
525 glueBack->be_entry_close (glueBack);
526 rc = be->be_entry_open (be, glueMode);
531 return be->be_entry_put (be, e, text);
535 glue_tool_entry_reindex (
540 if (!glueBack || !glueBack->be_entry_reindex)
543 return glueBack->be_entry_reindex (glueBack, id);
551 glueinfo *gi = (glueinfo *) b0->bd_info;
554 /* just sync everyone */
555 for (i = 0; i<gi->nodes; i++)
556 if (gi->n[i].be->be_sync)
557 gi->n[i].be->be_sync (gi->n[i].be);
565 int cont = num_subordinates;
567 BackendInfo *bi = NULL;
570 /* While there are subordinate backends, search backwards through the
571 * backends and connect them to their superior.
573 for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
574 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
575 /* The last database cannot be a subordinate of noone */
576 if (i == nBackendDB - 1) {
577 SLAP_DBFLAGS(b1) ^= SLAP_DBFLAG_GLUE_SUBORDINATE;
582 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
583 if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
586 /* We will only link it once */
587 if ( SLAP_GLUE_LINKED( be ) ) {
590 assert( be->be_nsuffix );
591 assert( b1->be_nsuffix );
592 if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
596 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_GLUE_LINKED;
598 /* We create a copy of the superior's be
599 * structure, pointing to all of its original
600 * information. Then we replace elements of
601 * the superior's info with our own. The copy
602 * is used whenever we have operations to pass
603 * down to the real database.
605 SLAP_DBFLAGS(b1) |= SLAP_DBFLAG_GLUE_INSTANCE;
606 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
609 gi->bi = *b1->bd_info;
610 bi = (BackendInfo *)gi;
611 bi->bi_open = glue_back_open;
612 bi->bi_close = glue_back_close;
613 bi->bi_db_open = glue_back_db_open;
614 bi->bi_db_close = glue_back_db_close;
615 bi->bi_db_destroy = glue_back_db_destroy;
617 bi->bi_op_search = glue_back_search;
620 * hooks for slap tools
622 bi->bi_tool_entry_open = glue_tool_entry_open;
623 bi->bi_tool_entry_close = glue_tool_entry_close;
624 bi->bi_tool_entry_first = glue_tool_entry_first;
625 bi->bi_tool_entry_next = glue_tool_entry_next;
626 bi->bi_tool_entry_get = glue_tool_entry_get;
627 bi->bi_tool_entry_put = glue_tool_entry_put;
628 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
629 bi->bi_tool_sync = glue_tool_sync;
630 /* FIXME : will support later */
631 bi->bi_tool_dn2id_get = 0;
632 bi->bi_tool_id2entry_get = 0;
633 bi->bi_tool_entry_modify = 0;
635 gi = (glueinfo *)ch_realloc(gi,
637 gi->nodes * sizeof(gluenode));
639 gi->n[gi->nodes].be = be;
640 dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn );
644 /* One more node for the master */
645 gi = (glueinfo *)ch_realloc(gi,
646 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
647 gi->n[gi->nodes].be = &gi->bd;
648 dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
650 b1->bd_info = (BackendInfo *)gi;
653 /* If there are any unresolved subordinates left, something is wrong */