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 case REP_INTERMEDIATE:
97 return SLAP_CB_CONTINUE;
100 if (rs->sr_err == LDAP_SUCCESS ||
101 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
102 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
103 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
104 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
105 gs->err != LDAP_SUCCESS)
106 gs->err = rs->sr_err;
107 if (gs->err == LDAP_SUCCESS && gs->matched) {
108 ch_free (gs->matched);
112 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
114 len = strlen (rs->sr_matched);
115 if (len > gs->matchlen) {
117 ch_free (gs->matched);
118 gs->matched = ch_strdup (rs->sr_matched);
126 for (i=0; rs->sr_ref[i].bv_val; i++);
130 new = ch_malloc ((i+1)*sizeof(struct berval));
132 new = ch_realloc(gs->refs,
133 (j+i+1)*sizeof(struct berval));
135 for (k=0; k<i; j++,k++) {
136 ber_dupbv( &new[j], &rs->sr_ref[k] );
138 new[j].bv_val = NULL;
147 glue_op_func ( Operation *op, SlapReply *rs )
149 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
150 BackendDB *b0 = op->o_bd;
151 BackendInfo *bi0 = op->o_bd->bd_info;
153 slap_operation_t which = op_bind;
156 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
158 /* If we're on the master backend, let overlay framework handle it */
159 if ( op->o_bd == b0 )
160 return SLAP_CB_CONTINUE;
162 b0->bd_info = on->on_info->oi_orig;
165 case LDAP_REQ_ADD: which = op_add; break;
166 case LDAP_REQ_DELETE: which = op_delete; break;
167 case LDAP_REQ_MODIFY: which = op_modify; break;
168 case LDAP_REQ_MODRDN: which = op_modrdn; break;
169 default: assert( 0 ); break;
172 func = &op->o_bd->bd_info->bi_op_bind;
174 rc = func[which]( op, rs );
176 rc = SLAP_CB_CONTINUE;
179 op->o_bd->bd_info = bi0;
184 glue_chk_referrals ( Operation *op, SlapReply *rs )
186 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
187 BackendDB *b0 = op->o_bd;
188 BackendInfo *bi0 = op->o_bd->bd_info;
191 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
192 if ( op->o_bd == b0 )
193 return SLAP_CB_CONTINUE;
195 b0->bd_info = on->on_info->oi_orig;
197 if ( op->o_bd->bd_info->bi_chk_referrals )
198 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
200 rc = SLAP_CB_CONTINUE;
203 op->o_bd->bd_info = bi0;
208 glue_chk_controls ( Operation *op, SlapReply *rs )
210 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
211 BackendDB *b0 = op->o_bd;
212 BackendInfo *bi0 = op->o_bd->bd_info;
213 int rc = SLAP_CB_CONTINUE;
215 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
216 if ( op->o_bd == b0 )
217 return SLAP_CB_CONTINUE;
219 b0->bd_info = on->on_info->oi_orig;
221 /* if the subordinate database has overlays, the bi_chk_controls()
222 * hook is actually over_aux_chk_controls(); in case it actually
223 * wraps a missing hok, we need to mimic the behavior
224 * of the frontend applied to that database */
225 if ( op->o_bd->bd_info->bi_chk_controls ) {
226 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
230 if ( rc == SLAP_CB_CONTINUE ) {
231 rc = backend_check_controls( op, rs );
235 op->o_bd->bd_info = bi0;
240 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
243 /* Process any overlays on the master backend */
244 if ( op->o_bd == b0 && on->on_next ) {
245 BackendInfo *bi = op->o_bd->bd_info;
247 for ( on=on->on_next; on; on=on->on_next ) {
248 op->o_bd->bd_info = (BackendInfo *)on;
249 rc = on->on_bi.bi_op_search( op, rs );
250 if ( rc != SLAP_CB_CONTINUE )
253 op->o_bd->bd_info = bi;
254 if ( rc != SLAP_CB_CONTINUE )
257 return op->o_bd->be_search( op, rs );
261 glue_op_search ( Operation *op, SlapReply *rs )
263 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
264 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
265 BackendDB *b0 = op->o_bd;
266 BackendDB *b1 = NULL, *btmp;
267 BackendInfo *bi0 = op->o_bd->bd_info;
270 glue_state gs = {0, 0, NULL, 0, NULL};
271 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
273 struct berval dn, ndn, *pdn;
277 cb.sc_next = op->o_callback;
279 stoptime = slap_get_time () + op->ors_tlimit;
281 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
282 b0->bd_info = on->on_info->oi_orig;
284 switch (op->ors_scope) {
285 case LDAP_SCOPE_BASE:
286 if ( op->o_bd == b0 )
287 return SLAP_CB_CONTINUE;
289 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
290 if (op->o_bd && op->o_bd->be_search) {
291 rs->sr_err = op->o_bd->be_search( op, rs );
295 case LDAP_SCOPE_ONELEVEL:
296 case LDAP_SCOPE_SUBTREE:
297 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
298 op->o_callback = &cb;
299 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
300 scope0 = op->ors_scope;
301 tlimit0 = op->ors_tlimit;
307 * Execute in reverse order, most general first
309 for (i = gi->gi_nodes; i >= 0; i--) {
310 if ( i == gi->gi_nodes ) {
314 btmp = gi->gi_n[i].gn_be;
315 pdn = &gi->gi_n[i].gn_pdn;
317 if (!btmp || !btmp->be_search)
319 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
321 if (tlimit0 != SLAP_NO_LIMIT) {
322 op->ors_tlimit = stoptime - slap_get_time ();
323 if (op->ors_tlimit <= 0) {
324 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
337 assert( op->o_bd->be_suffix != NULL );
338 assert( op->o_bd->be_nsuffix != NULL );
340 if (scope0 == LDAP_SCOPE_ONELEVEL &&
343 op->ors_scope = LDAP_SCOPE_BASE;
344 op->o_req_dn = op->o_bd->be_suffix[0];
345 op->o_req_ndn = op->o_bd->be_nsuffix[0];
346 rs->sr_err = op->o_bd->be_search(op, rs);
347 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
348 gs.err = LDAP_SUCCESS;
351 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
352 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
354 rs->sr_err = glue_sub_search( op, rs, b0, on );
356 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
357 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
359 op->o_req_dn = op->o_bd->be_suffix[0];
360 op->o_req_ndn = op->o_bd->be_nsuffix[0];
361 rs->sr_err = glue_sub_search( op, rs, b0, on );
362 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
363 gs.err = LDAP_SUCCESS;
368 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
369 rs->sr_err = glue_sub_search( op, rs, b0, on );
375 * Add errors that should result in dropping
378 case LDAP_SIZELIMIT_EXCEEDED:
379 case LDAP_TIMELIMIT_EXCEEDED:
380 case LDAP_ADMINLIMIT_EXCEEDED:
381 case LDAP_NO_SUCH_OBJECT:
382 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
383 case LDAP_X_CANNOT_CHAIN:
384 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
392 op->ors_scope = scope0;
393 op->ors_tlimit = tlimit0;
399 if ( op->o_abandon ) {
400 rs->sr_err = SLAPD_ABANDON;
402 op->o_callback = cb.sc_next;
404 rs->sr_matched = gs.matched;
405 rs->sr_ref = gs.refs;
407 send_ldap_result( op, rs );
411 op->o_bd->bd_info = bi0;
415 ber_bvarray_free(gs.refs);
419 static BackendDB toolDB;
422 glue_tool_entry_open (
427 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
429 /* We don't know which backend to talk to yet, so just
430 * remember the mode and move on...
436 toolDB.bd_info = oi->oi_orig;
442 glue_tool_entry_close (
449 if (!glueBack->be_entry_close)
451 rc = glueBack->be_entry_close (glueBack);
456 static slap_overinst *
461 slap_overinfo *oi = (slap_overinfo *)bi;
464 for ( on = oi->oi_list; on; on=on->on_next ) {
465 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
471 /* This function will only be called in tool mode */
477 slap_overinst *on = glue_tool_inst( bi );
478 glueinfo *gi = on->on_bi.bi_private;
479 static int glueOpened = 0;
480 int i, j, same, bsame = 0, rc = 0;
482 if (glueOpened) return 0;
486 /* If we were invoked in tool mode, open all the underlying backends */
487 if (slapMode & SLAP_TOOL_MODE) {
488 for (i = 0; i<gi->gi_nodes; i++) {
490 /* Same bi_open as our main backend? */
491 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
492 on->on_info->oi_orig->bi_open )
495 /* Loop thru the bd_info's and make sure we only
496 * invoke their bi_open functions once each.
498 for ( j = 0; j<i; j++ ) {
499 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
500 gi->gi_n[j].gn_be->bd_info->bi_open ) {
505 /* OK, it's unique and non-NULL, call it. */
506 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
507 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
508 gi->gi_n[i].gn_be->bd_info );
509 /* Let backend.c take care of the rest of startup */
511 rc = backend_startup_one( gi->gi_n[i].gn_be );
514 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
515 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
517 } /* other case is impossible */
521 /* This function will only be called in tool mode */
527 static int glueClosed = 0;
530 if (glueClosed) return 0;
534 if (slapMode & SLAP_TOOL_MODE) {
535 rc = backend_shutdown( NULL );
541 glue_entry_release_rw (
552 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
553 op->o_bd = glue_back_select (&b2, &e->e_nname);
555 if ( op->o_bd->be_release ) {
556 rc = op->o_bd->be_release( op, e, rw );
559 /* FIXME: mimic be_entry_release_rw
560 * when no be_release() available */
570 glue_tool_entry_first (
574 slap_overinst *on = glue_tool_inst( b0->bd_info );
575 glueinfo *gi = on->on_bi.bi_private;
578 /* If we're starting from scratch, start at the most general */
580 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
583 for (i = gi->gi_nodes-1; i >= 0; i--) {
584 if (gi->gi_n[i].gn_be->be_entry_open &&
585 gi->gi_n[i].gn_be->be_entry_first) {
586 glueBack = gi->gi_n[i].gn_be;
592 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
593 glueBack->be_entry_open (glueBack, glueMode) != 0)
596 return glueBack->be_entry_first (glueBack);
600 glue_tool_entry_next (
604 slap_overinst *on = glue_tool_inst( b0->bd_info );
605 glueinfo *gi = on->on_bi.bi_private;
609 if (!glueBack || !glueBack->be_entry_next)
612 rc = glueBack->be_entry_next (glueBack);
614 /* If we ran out of entries in one database, move on to the next */
616 if ( glueBack && glueBack->be_entry_close )
617 glueBack->be_entry_close (glueBack);
618 for (i=0; i<gi->gi_nodes; i++) {
619 if (gi->gi_n[i].gn_be == glueBack)
626 glueBack = gi->gi_n[i-1].gn_be;
627 rc = glue_tool_entry_first (b0);
634 glue_tool_entry_get (
639 if (!glueBack || !glueBack->be_entry_get)
642 return glueBack->be_entry_get (glueBack, id);
646 glue_tool_entry_put (
656 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
657 be = glue_back_select (&b2, &e->e_nname);
658 if ( be == &b2 ) be = &toolDB;
660 if (!be->be_entry_put)
664 if ( be->be_entry_open ) {
665 rc = be->be_entry_open (be, glueMode);
670 } else if (be != glueBack) {
671 /* If this entry belongs in a different branch than the
672 * previous one, close the current database and open the
675 if ( glueBack->be_entry_close ) {
676 glueBack->be_entry_close (glueBack);
678 if ( be->be_entry_open ) {
679 rc = be->be_entry_open (be, glueMode);
686 return be->be_entry_put (be, e, text);
690 glue_tool_entry_reindex (
695 if (!glueBack || !glueBack->be_entry_reindex)
698 return glueBack->be_entry_reindex (glueBack, id);
706 slap_overinst *on = glue_tool_inst( b0->bd_info );
707 glueinfo *gi = on->on_bi.bi_private;
708 BackendInfo *bi = b0->bd_info;
711 /* just sync everyone */
712 for (i = 0; i<gi->gi_nodes; i++)
713 if (gi->gi_n[i].gn_be->be_sync)
714 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
715 b0->bd_info = on->on_info->oi_orig;
727 slap_overinst *on = (slap_overinst *)be->bd_info;
728 slap_overinfo *oi = on->on_info;
729 BackendInfo *bi = oi->oi_orig;
732 gi = ch_calloc( 1, sizeof(glueinfo));
733 on->on_bi.bi_private = gi;
734 dnParent( be->be_nsuffix, &gi->gi_pdn );
736 /* Currently the overlay framework doesn't handle these entry points
737 * but we need them....
739 oi->oi_bi.bi_open = glue_open;
740 oi->oi_bi.bi_close = glue_close;
742 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
744 /* Only advertise these if the root DB supports them */
745 if ( bi->bi_tool_entry_open )
746 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
747 if ( bi->bi_tool_entry_close )
748 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
749 if ( bi->bi_tool_entry_first )
750 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
751 if ( bi->bi_tool_entry_next )
752 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
753 if ( bi->bi_tool_entry_get )
754 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
755 if ( bi->bi_tool_entry_put )
756 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
757 if ( bi->bi_tool_entry_reindex )
758 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
759 if ( bi->bi_tool_sync )
760 oi->oi_bi.bi_tool_sync = glue_tool_sync;
762 /*FIXME : need to add support */
763 oi->oi_bi.bi_tool_dn2id_get = 0;
764 oi->oi_bi.bi_tool_id2entry_get = 0;
765 oi->oi_bi.bi_tool_entry_modify = 0;
767 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
777 slap_overinst *on = (slap_overinst *)be->bd_info;
778 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
781 return SLAP_CB_CONTINUE;
789 slap_overinst *on = (slap_overinst *)be->bd_info;
791 on->on_info->oi_bi.bi_db_close = 0;
796 glue_sub_del( BackendDB *b0 )
801 /* Find the top backend for this subordinate */
803 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
809 if ( SLAP_GLUE_SUBORDINATE( be ))
811 if ( !SLAP_GLUE_INSTANCE( be ))
813 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
816 /* OK, got the right backend, find the overlay */
817 oi = (slap_overinfo *)be->bd_info;
818 for ( on=oi->oi_list; on; on=on->on_next ) {
819 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
822 assert( on != NULL );
823 gi = on->on_bi.bi_private;
824 for ( i=0; i < gi->gi_nodes; i++ ) {
825 if ( gi->gi_n[i].gn_be == b0 ) {
828 for (j=i+1; j < gi->gi_nodes; j++)
829 gi->gi_n[j-1] = gi->gi_n[j];
836 rc = LDAP_NO_SUCH_OBJECT;
841 typedef struct glue_Addrec {
842 struct glue_Addrec *ga_next;
846 /* List of added subordinates */
847 static glue_Addrec *ga_list;
849 /* Attach all the subordinate backends to their superior */
853 glue_Addrec *ga, *gnext = NULL;
856 /* For all the subordinate backends */
857 for ( ga=ga_list; ga != NULL; ga = gnext ) {
862 /* Find the top backend for this subordinate */
864 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
869 if ( SLAP_GLUE_SUBORDINATE( be ))
871 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
874 /* If it's not already configured, set up the overlay */
875 if ( !SLAP_GLUE_INSTANCE( be )) {
876 rc = overlay_config( be, glue.on_bi.bi_type );
880 /* Find the overlay instance */
881 oi = (slap_overinfo *)be->bd_info;
882 for ( on=oi->oi_list; on; on=on->on_next ) {
883 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
886 assert( on != NULL );
887 gi = on->on_bi.bi_private;
888 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
889 gi->gi_nodes * sizeof(gluenode));
890 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
891 dnParent( &ga->ga_be->be_nsuffix[0],
892 &gi->gi_n[gi->gi_nodes].gn_pdn );
894 on->on_bi.bi_private = gi;
898 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
899 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
900 rc = LDAP_NO_SUCH_OBJECT;
912 glue_sub_add( BackendDB *be, int advert, int online )
917 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
919 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
921 ga = ch_malloc( sizeof( glue_Addrec ));
922 ga->ga_next = ga_list;
927 rc = glue_sub_attach();
935 glue.on_bi.bi_type = "glue";
937 glue.on_bi.bi_db_init = glue_db_init;
938 glue.on_bi.bi_db_close = glue_db_close;
939 glue.on_bi.bi_db_destroy = glue_db_destroy;
941 glue.on_bi.bi_op_search = glue_op_search;
942 glue.on_bi.bi_op_modify = glue_op_func;
943 glue.on_bi.bi_op_modrdn = glue_op_func;
944 glue.on_bi.bi_op_add = glue_op_func;
945 glue.on_bi.bi_op_delete = glue_op_func;
947 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
948 glue.on_bi.bi_chk_controls = glue_chk_controls;
950 return overlay_register( &glue );