1 /* backglue.c - backend glue */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2006 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 * The purpose of these functions is to allow you to split a single database
23 * into pieces (for load balancing purposes, whatever) but still be able
24 * to treat it as a single database after it's been split. As such, each
25 * of the glued backends should have identical rootdn.
33 #include <ac/string.h>
34 #include <ac/socket.h>
39 typedef struct gluenode {
44 typedef struct glueinfo {
50 static slap_overinst glue;
53 static BackendDB *glueBack;
55 static slap_response glue_op_response;
57 /* Just like select_backend, but only for our backends */
64 slap_overinst *on = (slap_overinst *)be->bd_info;
65 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
68 for (i = 0; i<gi->gi_nodes; i++) {
69 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
71 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
72 return gi->gi_n[i].gn_be;
75 be->bd_info = on->on_info->oi_orig;
80 typedef struct glue_state {
89 glue_op_response ( Operation *op, SlapReply *rs )
91 glue_state *gs = op->o_callback->sc_private;
96 return SLAP_CB_CONTINUE;
99 if (rs->sr_err == LDAP_SUCCESS ||
100 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
101 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
102 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
103 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
104 gs->err != LDAP_SUCCESS)
105 gs->err = rs->sr_err;
106 if (gs->err == LDAP_SUCCESS && gs->matched) {
107 ch_free (gs->matched);
111 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
113 len = strlen (rs->sr_matched);
114 if (len > gs->matchlen) {
116 ch_free (gs->matched);
117 gs->matched = ch_strdup (rs->sr_matched);
125 for (i=0; rs->sr_ref[i].bv_val; i++);
129 new = ch_malloc ((i+1)*sizeof(struct berval));
131 new = ch_realloc(gs->refs,
132 (j+i+1)*sizeof(struct berval));
134 for (k=0; k<i; j++,k++) {
135 ber_dupbv( &new[j], &rs->sr_ref[k] );
137 new[j].bv_val = NULL;
146 glue_op_func ( Operation *op, SlapReply *rs )
148 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
149 BackendDB *b0 = op->o_bd;
150 BackendInfo *bi0 = op->o_bd->bd_info;
152 slap_operation_t which = op_bind;
155 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
156 b0->bd_info = on->on_info->oi_orig;
159 case LDAP_REQ_ADD: which = op_add; break;
160 case LDAP_REQ_DELETE: which = op_delete; break;
161 case LDAP_REQ_MODIFY: which = op_modify; break;
162 case LDAP_REQ_MODRDN: which = op_modrdn; break;
163 default: assert( 0 ); break;
166 func = &op->o_bd->bd_info->bi_op_bind;
168 rc = func[which]( op, rs );
170 rc = SLAP_CB_CONTINUE;
173 op->o_bd->bd_info = bi0;
178 glue_chk_referrals ( Operation *op, SlapReply *rs )
180 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
181 BackendDB *b0 = op->o_bd;
182 BackendInfo *bi0 = op->o_bd->bd_info;
185 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
186 b0->bd_info = on->on_info->oi_orig;
188 if ( op->o_bd->bd_info->bi_chk_referrals )
189 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
191 rc = SLAP_CB_CONTINUE;
194 op->o_bd->bd_info = bi0;
199 glue_chk_controls ( Operation *op, SlapReply *rs )
201 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
202 BackendDB *b0 = op->o_bd;
203 BackendInfo *bi0 = op->o_bd->bd_info;
204 int rc = SLAP_CB_CONTINUE;
206 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
207 b0->bd_info = on->on_info->oi_orig;
209 /* if the subordinate database has overlays, the bi_chk_controls()
210 * hook is actually over_aux_chk_controls(); in case it actually
211 * wraps a missing hok, we need to mimic the behavior
212 * of the frontend applied to that database */
213 if ( op->o_bd->bd_info->bi_chk_controls ) {
214 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
218 if ( rc == SLAP_CB_CONTINUE ) {
219 rc = backend_check_controls( op, rs );
223 op->o_bd->bd_info = bi0;
228 glue_op_search ( Operation *op, SlapReply *rs )
230 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
231 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
232 BackendDB *b0 = op->o_bd;
233 BackendDB *b1 = NULL, *btmp;
234 BackendInfo *bi0 = op->o_bd->bd_info;
237 glue_state gs = {0, 0, NULL, 0, NULL};
238 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
240 struct berval dn, ndn, *pdn;
244 cb.sc_next = op->o_callback;
246 stoptime = slap_get_time () + op->ors_tlimit;
248 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
249 b0->bd_info = on->on_info->oi_orig;
251 switch (op->ors_scope) {
252 case LDAP_SCOPE_BASE:
253 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
254 if (op->o_bd && op->o_bd->be_search) {
255 rs->sr_err = op->o_bd->be_search( op, rs );
259 case LDAP_SCOPE_ONELEVEL:
260 case LDAP_SCOPE_SUBTREE:
261 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
265 if (op->o_bd && op->o_bd->be_search) {
266 rs->sr_err = op->o_bd->be_search( op, rs );
268 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
269 "No search target found");
275 op->o_callback = &cb;
276 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
277 scope0 = op->ors_scope;
278 tlimit0 = op->ors_tlimit;
284 * Execute in reverse order, most general first
286 for (i = gi->gi_nodes; i >= 0; i--) {
287 if ( i == gi->gi_nodes ) {
291 btmp = gi->gi_n[i].gn_be;
292 pdn = &gi->gi_n[i].gn_pdn;
294 if (!btmp || !btmp->be_search)
296 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
298 if (tlimit0 != SLAP_NO_LIMIT) {
299 op->ors_tlimit = stoptime - slap_get_time ();
300 if (op->ors_tlimit <= 0) {
301 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
314 assert( op->o_bd->be_suffix != NULL );
315 assert( op->o_bd->be_nsuffix != NULL );
317 if (scope0 == LDAP_SCOPE_ONELEVEL &&
320 op->ors_scope = LDAP_SCOPE_BASE;
321 op->o_req_dn = op->o_bd->be_suffix[0];
322 op->o_req_ndn = op->o_bd->be_nsuffix[0];
323 rs->sr_err = op->o_bd->be_search(op, rs);
324 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
325 gs.err = LDAP_SUCCESS;
328 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
329 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
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 );
339 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
340 gs.err = LDAP_SUCCESS;
345 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
346 rs->sr_err = op->o_bd->be_search( op, rs );
352 * Add errors that should result in dropping
355 case LDAP_SIZELIMIT_EXCEEDED:
356 case LDAP_TIMELIMIT_EXCEEDED:
357 case LDAP_ADMINLIMIT_EXCEEDED:
358 case LDAP_NO_SUCH_OBJECT:
359 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
360 case LDAP_X_CANNOT_CHAIN:
361 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
369 op->ors_scope = scope0;
370 op->ors_tlimit = tlimit0;
376 if ( op->o_abandon ) {
377 rs->sr_err = SLAPD_ABANDON;
379 op->o_callback = cb.sc_next;
381 rs->sr_matched = gs.matched;
382 rs->sr_ref = gs.refs;
384 send_ldap_result( op, rs );
388 op->o_bd->bd_info = bi0;
392 ber_bvarray_free(gs.refs);
396 static BackendDB toolDB;
399 glue_tool_entry_open (
404 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
406 /* We don't know which backend to talk to yet, so just
407 * remember the mode and move on...
413 toolDB.bd_info = oi->oi_orig;
419 glue_tool_entry_close (
426 if (!glueBack->be_entry_close)
428 rc = glueBack->be_entry_close (glueBack);
433 static slap_overinst *
438 slap_overinfo *oi = (slap_overinfo *)bi;
441 for ( on = oi->oi_list; on; on=on->on_next ) {
442 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
448 /* This function will only be called in tool mode */
454 slap_overinst *on = glue_tool_inst( bi );
455 glueinfo *gi = on->on_bi.bi_private;
456 static int glueOpened = 0;
457 int i, j, same, bsame = 0, rc = 0;
459 if (glueOpened) return 0;
463 /* If we were invoked in tool mode, open all the underlying backends */
464 if (slapMode & SLAP_TOOL_MODE) {
465 for (i = 0; i<gi->gi_nodes; i++) {
467 /* Same bi_open as our main backend? */
468 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
469 on->on_info->oi_orig->bi_open )
472 /* Loop thru the bd_info's and make sure we only
473 * invoke their bi_open functions once each.
475 for ( j = 0; j<i; j++ ) {
476 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
477 gi->gi_n[j].gn_be->bd_info->bi_open ) {
482 /* OK, it's unique and non-NULL, call it. */
483 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
484 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
485 gi->gi_n[i].gn_be->bd_info );
486 /* Let backend.c take care of the rest of startup */
488 rc = backend_startup_one( gi->gi_n[i].gn_be );
491 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
492 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
494 } /* other case is impossible */
498 /* This function will only be called in tool mode */
504 static int glueClosed = 0;
507 if (glueClosed) return 0;
511 if (slapMode & SLAP_TOOL_MODE) {
512 rc = backend_shutdown( NULL );
518 glue_entry_release_rw (
529 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
530 op->o_bd = glue_back_select (&b2, &e->e_nname);
532 if ( op->o_bd->be_release ) {
533 rc = op->o_bd->be_release( op, e, rw );
536 /* FIXME: mimic be_entry_release_rw
537 * when no be_release() available */
547 glue_tool_entry_first (
551 slap_overinst *on = glue_tool_inst( b0->bd_info );
552 glueinfo *gi = on->on_bi.bi_private;
555 /* If we're starting from scratch, start at the most general */
557 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
560 for (i = gi->gi_nodes-1; i >= 0; i--) {
561 if (gi->gi_n[i].gn_be->be_entry_open &&
562 gi->gi_n[i].gn_be->be_entry_first) {
563 glueBack = gi->gi_n[i].gn_be;
569 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
570 glueBack->be_entry_open (glueBack, glueMode) != 0)
573 return glueBack->be_entry_first (glueBack);
577 glue_tool_entry_next (
581 slap_overinst *on = glue_tool_inst( b0->bd_info );
582 glueinfo *gi = on->on_bi.bi_private;
586 if (!glueBack || !glueBack->be_entry_next)
589 rc = glueBack->be_entry_next (glueBack);
591 /* If we ran out of entries in one database, move on to the next */
593 if ( glueBack && glueBack->be_entry_close )
594 glueBack->be_entry_close (glueBack);
595 for (i=0; i<gi->gi_nodes; i++) {
596 if (gi->gi_n[i].gn_be == glueBack)
603 glueBack = gi->gi_n[i-1].gn_be;
604 rc = glue_tool_entry_first (b0);
611 glue_tool_entry_get (
616 if (!glueBack || !glueBack->be_entry_get)
619 return glueBack->be_entry_get (glueBack, id);
623 glue_tool_entry_put (
633 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
634 be = glue_back_select (&b2, &e->e_nname);
635 if ( be == &b2 ) be = &toolDB;
637 if (!be->be_entry_put)
641 if ( be->be_entry_open ) {
642 rc = be->be_entry_open (be, glueMode);
647 } else if (be != glueBack) {
648 /* If this entry belongs in a different branch than the
649 * previous one, close the current database and open the
652 if ( glueBack->be_entry_close ) {
653 glueBack->be_entry_close (glueBack);
655 if ( be->be_entry_open ) {
656 rc = be->be_entry_open (be, glueMode);
663 return be->be_entry_put (be, e, text);
667 glue_tool_entry_reindex (
672 if (!glueBack || !glueBack->be_entry_reindex)
675 return glueBack->be_entry_reindex (glueBack, id);
683 slap_overinst *on = glue_tool_inst( b0->bd_info );
684 glueinfo *gi = on->on_bi.bi_private;
685 BackendInfo *bi = b0->bd_info;
688 /* just sync everyone */
689 for (i = 0; i<gi->gi_nodes; i++)
690 if (gi->gi_n[i].gn_be->be_sync)
691 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
692 b0->bd_info = on->on_info->oi_orig;
704 slap_overinst *on = (slap_overinst *)be->bd_info;
705 slap_overinfo *oi = on->on_info;
706 BackendInfo *bi = oi->oi_orig;
709 gi = ch_calloc( 1, sizeof(glueinfo));
710 on->on_bi.bi_private = gi;
711 dnParent( be->be_nsuffix, &gi->gi_pdn );
713 /* Currently the overlay framework doesn't handle these entry points
714 * but we need them....
716 oi->oi_bi.bi_open = glue_open;
717 oi->oi_bi.bi_close = glue_close;
719 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
721 /* Only advertise these if the root DB supports them */
722 if ( bi->bi_tool_entry_open )
723 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
724 if ( bi->bi_tool_entry_close )
725 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
726 if ( bi->bi_tool_entry_first )
727 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
728 if ( bi->bi_tool_entry_next )
729 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
730 if ( bi->bi_tool_entry_get )
731 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
732 if ( bi->bi_tool_entry_put )
733 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
734 if ( bi->bi_tool_entry_reindex )
735 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
736 if ( bi->bi_tool_sync )
737 oi->oi_bi.bi_tool_sync = glue_tool_sync;
739 /*FIXME : need to add support */
740 oi->oi_bi.bi_tool_dn2id_get = 0;
741 oi->oi_bi.bi_tool_id2entry_get = 0;
742 oi->oi_bi.bi_tool_entry_modify = 0;
744 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
754 slap_overinst *on = (slap_overinst *)be->bd_info;
755 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
758 return SLAP_CB_CONTINUE;
766 slap_overinst *on = (slap_overinst *)be->bd_info;
768 on->on_info->oi_bi.bi_db_close = NULL;
773 glue_sub_del( BackendDB *b0 )
778 /* Find the top backend for this subordinate */
780 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
786 if ( SLAP_GLUE_SUBORDINATE( be ))
788 if ( !SLAP_GLUE_INSTANCE( be ))
790 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
793 /* OK, got the right backend, find the overlay */
794 oi = (slap_overinfo *)be->bd_info;
795 for ( on=oi->oi_list; on; on=on->on_next ) {
796 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
799 assert( on != NULL );
800 gi = on->on_bi.bi_private;
801 for ( i=0; i < gi->gi_nodes; i++ ) {
802 if ( gi->gi_n[i].gn_be == b0 ) {
805 for (j=i+1; j < gi->gi_nodes; j++)
806 gi->gi_n[j-1] = gi->gi_n[j];
813 rc = LDAP_NO_SUCH_OBJECT;
818 typedef struct glue_Addrec {
819 struct glue_Addrec *ga_next;
823 /* List of added subordinates */
824 static glue_Addrec *ga_list;
826 /* Attach all the subordinate backends to their superior */
830 glue_Addrec *ga, *gnext = NULL;
833 /* For all the subordinate backends */
834 for ( ga=ga_list; ga != NULL; ga = gnext ) {
839 /* Find the top backend for this subordinate */
841 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
846 if ( SLAP_GLUE_SUBORDINATE( be ))
848 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
851 /* If it's not already configured, set up the overlay */
852 if ( !SLAP_GLUE_INSTANCE( be )) {
853 rc = overlay_config( be, glue.on_bi.bi_type );
857 /* Find the overlay instance */
858 oi = (slap_overinfo *)be->bd_info;
859 for ( on=oi->oi_list; on; on=on->on_next ) {
860 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
863 assert( on != NULL );
864 gi = on->on_bi.bi_private;
865 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
866 gi->gi_nodes * sizeof(gluenode));
867 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
868 dnParent( &ga->ga_be->be_nsuffix[0],
869 &gi->gi_n[gi->gi_nodes].gn_pdn );
871 on->on_bi.bi_private = gi;
875 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
876 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
877 rc = LDAP_NO_SUCH_OBJECT;
889 glue_sub_add( BackendDB *be, int advert, int online )
894 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
896 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
898 ga = ch_malloc( sizeof( glue_Addrec ));
899 ga->ga_next = ga_list;
904 rc = glue_sub_attach();
912 glue.on_bi.bi_type = "glue";
914 glue.on_bi.bi_db_init = glue_db_init;
915 glue.on_bi.bi_db_close = glue_db_close;
916 glue.on_bi.bi_db_destroy = glue_db_destroy;
918 glue.on_bi.bi_op_search = glue_op_search;
919 glue.on_bi.bi_op_modify = glue_op_func;
920 glue.on_bi.bi_op_modrdn = glue_op_func;
921 glue.on_bi.bi_op_add = glue_op_func;
922 glue.on_bi.bi_op_delete = glue_op_func;
924 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
925 glue.on_bi.bi_chk_controls = glue_chk_controls;
927 return overlay_register( &glue );