1 /* backglue.c - backend glue */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2005 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 {
90 glue_op_response ( Operation *op, SlapReply *rs )
92 glue_state *gs = op->o_callback->sc_private;
96 if ( gs->slimit != SLAP_NO_LIMIT
97 && rs->sr_nentries >= gs->slimit )
99 rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
104 return SLAP_CB_CONTINUE;
107 if (rs->sr_err == LDAP_SUCCESS ||
108 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
109 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
110 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
111 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
112 gs->err != LDAP_SUCCESS)
113 gs->err = rs->sr_err;
114 if (gs->err == LDAP_SUCCESS && gs->matched) {
115 ch_free (gs->matched);
119 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
121 len = strlen (rs->sr_matched);
122 if (len > gs->matchlen) {
124 ch_free (gs->matched);
125 gs->matched = ch_strdup (rs->sr_matched);
133 for (i=0; rs->sr_ref[i].bv_val; i++);
137 new = ch_malloc ((i+1)*sizeof(struct berval));
139 new = ch_realloc(gs->refs,
140 (j+i+1)*sizeof(struct berval));
142 for (k=0; k<i; j++,k++) {
143 ber_dupbv( &new[j], &rs->sr_ref[k] );
145 new[j].bv_val = NULL;
154 glue_op_func ( Operation *op, SlapReply *rs )
156 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
157 BackendDB *b0 = op->o_bd;
158 BackendInfo *bi0 = op->o_bd->bd_info;
160 slap_operation_t which;
163 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
164 b0->bd_info = on->on_info->oi_orig;
167 case LDAP_REQ_ADD: which = op_add; break;
168 case LDAP_REQ_DELETE: which = op_delete; break;
169 case LDAP_REQ_MODIFY: which = op_modify; break;
170 case LDAP_REQ_MODRDN: which = op_modrdn; break;
173 func = &op->o_bd->bd_info->bi_op_bind;
175 rc = func[which]( op, rs );
177 rc = SLAP_CB_CONTINUE;
180 op->o_bd->bd_info = bi0;
185 glue_chk_referrals ( Operation *op, SlapReply *rs )
187 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
188 BackendDB *b0 = op->o_bd;
189 BackendInfo *bi0 = op->o_bd->bd_info;
192 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
193 b0->bd_info = on->on_info->oi_orig;
195 if ( op->o_bd->bd_info->bi_chk_referrals )
196 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
198 rc = SLAP_CB_CONTINUE;
201 op->o_bd->bd_info = bi0;
206 glue_chk_controls ( Operation *op, SlapReply *rs )
208 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
209 BackendDB *b0 = op->o_bd;
210 BackendInfo *bi0 = op->o_bd->bd_info;
211 int rc = SLAP_CB_CONTINUE;
213 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
214 b0->bd_info = on->on_info->oi_orig;
216 /* if the subordinate database has overlays, the bi_chk_controls()
217 * hook is actually over_aux_chk_controls(); in case it actually
218 * wraps a missing hok, we need to mimic the behavior
219 * of the frontend applied to that database */
220 if ( op->o_bd->bd_info->bi_chk_controls ) {
221 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
225 if ( rc == SLAP_CB_CONTINUE ) {
226 rc = backend_check_controls( op, rs );
230 op->o_bd->bd_info = bi0;
235 glue_op_search ( Operation *op, SlapReply *rs )
237 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
238 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
239 BackendDB *b0 = op->o_bd;
240 BackendDB *b1 = NULL, *btmp;
241 BackendInfo *bi0 = op->o_bd->bd_info;
244 glue_state gs = {0, 0, 0, NULL, 0, NULL};
245 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
246 int scope0, slimit0, tlimit0;
247 struct berval dn, ndn, *pdn;
251 cb.sc_next = op->o_callback;
253 stoptime = slap_get_time () + op->ors_tlimit;
255 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
256 b0->bd_info = on->on_info->oi_orig;
258 switch (op->ors_scope) {
259 case LDAP_SCOPE_BASE:
260 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
261 if (op->o_bd && op->o_bd->be_search) {
262 rs->sr_err = op->o_bd->be_search( op, rs );
266 case LDAP_SCOPE_ONELEVEL:
267 case LDAP_SCOPE_SUBTREE:
268 #ifdef LDAP_SCOPE_SUBORDINATE
269 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
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");
284 op->o_callback = &cb;
285 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
286 scope0 = op->ors_scope;
287 slimit0 = gs.slimit = op->ors_slimit;
288 tlimit0 = op->ors_tlimit;
294 * Execute in reverse order, most general first
296 for (i = gi->gi_nodes; i >= 0; i--) {
297 if ( i == gi->gi_nodes ) {
301 btmp = gi->gi_n[i].gn_be;
302 pdn = &gi->gi_n[i].gn_pdn;
304 if (!btmp || !btmp->be_search)
306 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
308 if (tlimit0 != SLAP_NO_LIMIT) {
309 op->ors_tlimit = stoptime - slap_get_time ();
310 if (op->ors_tlimit <= 0) {
311 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
315 if (slimit0 != SLAP_NO_LIMIT) {
316 op->ors_slimit = slimit0 - rs->sr_nentries;
317 if (op->ors_slimit < 0) {
318 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
331 assert( op->o_bd->be_suffix != NULL );
332 assert( op->o_bd->be_nsuffix != NULL );
334 if (scope0 == LDAP_SCOPE_ONELEVEL &&
337 op->ors_scope = LDAP_SCOPE_BASE;
338 op->o_req_dn = op->o_bd->be_suffix[0];
339 op->o_req_ndn = op->o_bd->be_nsuffix[0];
340 rs->sr_err = op->o_bd->be_search(op, rs);
342 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
343 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
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 );
353 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
354 gs.err = LDAP_SUCCESS;
357 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
358 rs->sr_err = op->o_bd->be_search( op, rs );
364 * Add errors that should result in dropping
367 case LDAP_SIZELIMIT_EXCEEDED:
368 case LDAP_TIMELIMIT_EXCEEDED:
369 case LDAP_ADMINLIMIT_EXCEEDED:
370 case LDAP_NO_SUCH_OBJECT:
371 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
372 case LDAP_CANNOT_CHAIN:
373 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
381 op->ors_scope = scope0;
382 op->ors_slimit = slimit0;
383 op->ors_tlimit = tlimit0;
389 if ( op->o_abandon ) {
390 rs->sr_err = SLAPD_ABANDON;
392 op->o_callback = cb.sc_next;
394 rs->sr_matched = gs.matched;
395 rs->sr_ref = gs.refs;
397 send_ldap_result( op, rs );
401 op->o_bd->bd_info = bi0;
405 ber_bvarray_free(gs.refs);
409 static BackendDB toolDB;
412 glue_tool_entry_open (
417 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
419 /* We don't know which backend to talk to yet, so just
420 * remember the mode and move on...
426 toolDB.bd_info = oi->oi_orig;
432 glue_tool_entry_close (
439 if (!glueBack->be_entry_close)
441 rc = glueBack->be_entry_close (glueBack);
446 static slap_overinst *
451 slap_overinfo *oi = (slap_overinfo *)bi;
454 for ( on = oi->oi_list; on; on=on->on_next ) {
455 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
461 /* This function will only be called in tool mode */
467 slap_overinst *on = glue_tool_inst( bi );
468 glueinfo *gi = on->on_bi.bi_private;
469 static int glueOpened = 0;
470 int i, j, same, bsame = 0, rc = 0;
472 if (glueOpened) return 0;
476 /* If we were invoked in tool mode, open all the underlying backends */
477 if (slapMode & SLAP_TOOL_MODE) {
478 for (i = 0; i<gi->gi_nodes; i++) {
480 /* Same type as our main backend? */
481 if ( gi->gi_n[i].gn_be->bd_info == on->on_info->oi_orig )
484 /* Loop thru the bd_info's and make sure we only
485 * invoke their bi_open functions once each.
487 for ( j = 0; j<i; j++ ) {
488 if ( gi->gi_n[i].gn_be->bd_info ==
489 gi->gi_n[j].gn_be->bd_info ) {
494 /* OK, it's unique and non-NULL, call it. */
495 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
496 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
497 gi->gi_n[i].gn_be->bd_info );
498 /* Let backend.c take care of the rest of startup */
500 rc = backend_startup_one( gi->gi_n[i].gn_be );
503 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
504 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
506 } /* other case is impossible */
510 /* This function will only be called in tool mode */
516 static int glueClosed = 0;
519 if (glueClosed) return 0;
523 if (slapMode & SLAP_TOOL_MODE) {
524 rc = backend_shutdown( NULL );
530 glue_entry_release_rw (
541 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
542 op->o_bd = glue_back_select (&b2, &e->e_nname);
544 if ( op->o_bd->be_release ) {
545 rc = op->o_bd->be_release( op, e, rw );
548 /* FIXME: mimic be_entry_release_rw
549 * when no be_release() available */
559 glue_tool_entry_first (
563 slap_overinst *on = glue_tool_inst( b0->bd_info );
564 glueinfo *gi = on->on_bi.bi_private;
567 /* If we're starting from scratch, start at the most general */
569 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
572 for (i = gi->gi_nodes-1; i >= 0; i--) {
573 if (gi->gi_n[i].gn_be->be_entry_open &&
574 gi->gi_n[i].gn_be->be_entry_first) {
575 glueBack = gi->gi_n[i].gn_be;
581 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
582 glueBack->be_entry_open (glueBack, glueMode) != 0)
585 return glueBack->be_entry_first (glueBack);
589 glue_tool_entry_next (
593 slap_overinst *on = glue_tool_inst( b0->bd_info );
594 glueinfo *gi = on->on_bi.bi_private;
598 if (!glueBack || !glueBack->be_entry_next)
601 rc = glueBack->be_entry_next (glueBack);
603 /* If we ran out of entries in one database, move on to the next */
605 if ( glueBack && glueBack->be_entry_close )
606 glueBack->be_entry_close (glueBack);
607 for (i=0; i<gi->gi_nodes; i++) {
608 if (gi->gi_n[i].gn_be == glueBack)
615 glueBack = gi->gi_n[i-1].gn_be;
616 rc = glue_tool_entry_first (b0);
623 glue_tool_entry_get (
628 if (!glueBack || !glueBack->be_entry_get)
631 return glueBack->be_entry_get (glueBack, id);
635 glue_tool_entry_put (
645 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
646 be = glue_back_select (&b2, &e->e_nname);
647 if ( be == &b2 ) be = &toolDB;
649 if (!be->be_entry_put)
653 if ( be->be_entry_open ) {
654 rc = be->be_entry_open (be, glueMode);
659 } else if (be != glueBack) {
660 /* If this entry belongs in a different branch than the
661 * previous one, close the current database and open the
664 if ( glueBack->be_entry_close ) {
665 glueBack->be_entry_close (glueBack);
667 if ( be->be_entry_open ) {
668 rc = be->be_entry_open (be, glueMode);
675 return be->be_entry_put (be, e, text);
679 glue_tool_entry_reindex (
684 if (!glueBack || !glueBack->be_entry_reindex)
687 return glueBack->be_entry_reindex (glueBack, id);
695 slap_overinst *on = glue_tool_inst( b0->bd_info );
696 glueinfo *gi = on->on_bi.bi_private;
697 BackendInfo *bi = b0->bd_info;
700 /* just sync everyone */
701 for (i = 0; i<gi->gi_nodes; i++)
702 if (gi->gi_n[i].gn_be->be_sync)
703 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
704 b0->bd_info = on->on_info->oi_orig;
716 slap_overinst *on = (slap_overinst *)be->bd_info;
717 slap_overinfo *oi = on->on_info;
720 gi = ch_calloc( 1, sizeof(glueinfo));
721 on->on_bi.bi_private = gi;
722 dnParent( be->be_nsuffix, &gi->gi_pdn );
724 /* Currently the overlay framework doesn't handle these entry points
725 * but we need them....
727 oi->oi_bi.bi_open = glue_open;
728 oi->oi_bi.bi_close = glue_close;
730 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
732 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
733 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
734 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
735 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
736 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
737 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
738 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
739 oi->oi_bi.bi_tool_sync = glue_tool_sync;
741 /*FIXME : need to add support */
742 oi->oi_bi.bi_tool_dn2id_get = 0;
743 oi->oi_bi.bi_tool_id2entry_get = 0;
744 oi->oi_bi.bi_tool_entry_modify = 0;
746 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
756 slap_overinst *on = (slap_overinst *)be->bd_info;
757 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
760 return SLAP_CB_CONTINUE;
768 slap_overinst *on = (slap_overinst *)be->bd_info;
770 on->on_info->oi_bi.bi_db_close = NULL;
775 glue_sub_del( BackendDB *b0 )
780 /* Find the top backend for this subordinate */
782 while ( be=LDAP_STAILQ_NEXT( be, be_next )) {
788 if ( SLAP_GLUE_SUBORDINATE( be ))
790 if ( !SLAP_GLUE_INSTANCE( be ))
792 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
795 /* OK, got the right backend, find the overlay */
796 oi = (slap_overinfo *)be->bd_info;
797 for ( on=oi->oi_list; on; on=on->on_next ) {
798 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
801 assert( on != NULL );
802 gi = on->on_bi.bi_private;
803 for ( i=0; i < gi->gi_nodes; i++ ) {
804 if ( gi->gi_n[i].gn_be == b0 ) {
807 for (j=i+1; j < gi->gi_nodes; j++)
808 gi->gi_n[j-1] = gi->gi_n[j];
815 rc = LDAP_NO_SUCH_OBJECT;
820 typedef struct glue_Addrec {
821 struct glue_Addrec *ga_next;
825 /* List of added subordinates */
826 static glue_Addrec *ga_list;
828 /* Attach all the subordinate backends to their superior */
832 glue_Addrec *ga, *gnext = NULL;
835 /* For all the subordinate backends */
836 for ( ga=ga_list; ga != NULL; ga = gnext ) {
841 /* Find the top backend for this subordinate */
843 while ( be=LDAP_STAILQ_NEXT( be, be_next )) {
848 if ( SLAP_GLUE_SUBORDINATE( be ))
850 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
853 /* If it's not already configured, set up the overlay */
854 if ( !SLAP_GLUE_INSTANCE( be )) {
855 rc = overlay_config( be, glue.on_bi.bi_type );
859 /* Find the overlay instance */
860 oi = (slap_overinfo *)be->bd_info;
861 for ( on=oi->oi_list; on; on=on->on_next ) {
862 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
865 assert( on != NULL );
866 gi = on->on_bi.bi_private;
867 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
868 gi->gi_nodes * sizeof(gluenode));
869 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
870 dnParent( &ga->ga_be->be_nsuffix[0],
871 &gi->gi_n[gi->gi_nodes].gn_pdn );
873 on->on_bi.bi_private = gi;
877 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
878 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
879 rc = LDAP_NO_SUCH_OBJECT;
891 glue_sub_add( BackendDB *be, int advert, int online )
896 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
898 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
900 ga = ch_malloc( sizeof( glue_Addrec ));
904 glue_Addrec *g2 = ga_list;
906 for ( ; g2 && g2->ga_next; g2=g2->ga_next );
913 rc = glue_sub_attach();
923 glue.on_bi.bi_type = "glue";
925 glue.on_bi.bi_db_init = glue_db_init;
926 glue.on_bi.bi_db_close = glue_db_close;
927 glue.on_bi.bi_db_destroy = glue_db_destroy;
929 glue.on_bi.bi_op_search = glue_op_search;
930 glue.on_bi.bi_op_modify = glue_op_func;
931 glue.on_bi.bi_op_modrdn = glue_op_func;
932 glue.on_bi.bi_op_add = glue_op_func;
933 glue.on_bi.bi_op_delete = glue_op_func;
935 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
936 glue.on_bi.bi_chk_controls = glue_chk_controls;
938 rc = overlay_register( &glue );
941 return glue_sub_attach();