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 {
91 glue_op_response ( Operation *op, SlapReply *rs )
93 glue_state *gs = op->o_callback->sc_private;
98 case REP_INTERMEDIATE:
99 return SLAP_CB_CONTINUE;
102 if (rs->sr_err == LDAP_SUCCESS ||
103 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
104 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
105 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
106 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
107 gs->err != LDAP_SUCCESS)
108 gs->err = rs->sr_err;
109 if (gs->err == LDAP_SUCCESS && gs->matched) {
110 ch_free (gs->matched);
114 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
116 len = strlen (rs->sr_matched);
117 if (len > gs->matchlen) {
119 ch_free (gs->matched);
120 gs->matched = ch_strdup (rs->sr_matched);
128 for (i=0; rs->sr_ref[i].bv_val; i++);
132 new = ch_malloc ((i+1)*sizeof(struct berval));
134 new = ch_realloc(gs->refs,
135 (j+i+1)*sizeof(struct berval));
137 for (k=0; k<i; j++,k++) {
138 ber_dupbv( &new[j], &rs->sr_ref[k] );
140 new[j].bv_val = NULL;
146 LDAPControl **newctrls;
148 for (i=0; rs->sr_ctrls[i]; i++);
152 newctrls = ch_malloc((i+1)*sizeof(LDAPControl *));
154 newctrls = ch_realloc(gs->ctrls,
155 (j+i+1)*sizeof(LDAPControl *));
157 for (k=0; k<i; j++,k++) {
158 newctrls[j] = ch_malloc(sizeof(LDAPControl));
159 *newctrls[j] = *rs->sr_ctrls[k];
160 if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ))
161 ber_dupbv( &newctrls[j]->ldctl_value,
162 &rs->sr_ctrls[k]->ldctl_value );
166 gs->ctrls = newctrls;
173 glue_op_func ( Operation *op, SlapReply *rs )
175 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
176 BackendDB *b0 = op->o_bd;
177 BackendInfo *bi0 = op->o_bd->bd_info;
179 slap_operation_t which = op_bind;
182 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
184 /* If we're on the master backend, let overlay framework handle it */
185 if ( op->o_bd == b0 )
186 return SLAP_CB_CONTINUE;
188 b0->bd_info = on->on_info->oi_orig;
191 case LDAP_REQ_ADD: which = op_add; break;
192 case LDAP_REQ_DELETE: which = op_delete; break;
193 case LDAP_REQ_MODIFY: which = op_modify; break;
194 case LDAP_REQ_MODRDN: which = op_modrdn; break;
195 default: assert( 0 ); break;
198 func = &op->o_bd->bd_info->bi_op_bind;
200 rc = func[which]( op, rs );
202 rc = SLAP_CB_CONTINUE;
205 op->o_bd->bd_info = bi0;
210 glue_chk_referrals ( Operation *op, SlapReply *rs )
212 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
213 BackendDB *b0 = op->o_bd;
214 BackendInfo *bi0 = op->o_bd->bd_info;
217 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
218 if ( op->o_bd == b0 )
219 return SLAP_CB_CONTINUE;
221 b0->bd_info = on->on_info->oi_orig;
223 if ( op->o_bd->bd_info->bi_chk_referrals )
224 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
226 rc = SLAP_CB_CONTINUE;
229 op->o_bd->bd_info = bi0;
234 glue_chk_controls ( Operation *op, SlapReply *rs )
236 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
237 BackendDB *b0 = op->o_bd;
238 BackendInfo *bi0 = op->o_bd->bd_info;
239 int rc = SLAP_CB_CONTINUE;
241 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
242 if ( op->o_bd == b0 )
243 return SLAP_CB_CONTINUE;
245 b0->bd_info = on->on_info->oi_orig;
247 /* if the subordinate database has overlays, the bi_chk_controls()
248 * hook is actually over_aux_chk_controls(); in case it actually
249 * wraps a missing hok, we need to mimic the behavior
250 * of the frontend applied to that database */
251 if ( op->o_bd->bd_info->bi_chk_controls ) {
252 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
256 if ( rc == SLAP_CB_CONTINUE ) {
257 rc = backend_check_controls( op, rs );
261 op->o_bd->bd_info = bi0;
266 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
269 /* Process any overlays on the master backend */
270 if ( op->o_bd == b0 && on->on_next ) {
271 BackendInfo *bi = op->o_bd->bd_info;
272 int rc = SLAP_CB_CONTINUE;
273 for ( on=on->on_next; on; on=on->on_next ) {
274 op->o_bd->bd_info = (BackendInfo *)on;
275 if ( on->on_bi.bi_op_search ) {
276 rc = on->on_bi.bi_op_search( op, rs );
277 if ( rc != SLAP_CB_CONTINUE )
281 op->o_bd->bd_info = bi;
282 if ( rc != SLAP_CB_CONTINUE )
285 return op->o_bd->be_search( op, rs );
289 glue_op_search ( Operation *op, SlapReply *rs )
291 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
292 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
293 BackendDB *b0 = op->o_bd;
294 BackendDB *b1 = NULL, *btmp;
295 BackendInfo *bi0 = op->o_bd->bd_info;
298 glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
299 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
301 struct berval dn, ndn, *pdn;
305 cb.sc_next = op->o_callback;
307 stoptime = slap_get_time () + op->ors_tlimit;
309 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
310 b0->bd_info = on->on_info->oi_orig;
312 switch (op->ors_scope) {
313 case LDAP_SCOPE_BASE:
314 if ( op->o_bd == b0 )
315 return SLAP_CB_CONTINUE;
317 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
318 if (op->o_bd && op->o_bd->be_search) {
319 rs->sr_err = op->o_bd->be_search( op, rs );
323 case LDAP_SCOPE_ONELEVEL:
324 case LDAP_SCOPE_SUBTREE:
325 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
326 op->o_callback = &cb;
327 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
328 scope0 = op->ors_scope;
329 tlimit0 = op->ors_tlimit;
335 * Execute in reverse order, most general first
337 for (i = gi->gi_nodes; i >= 0; i--) {
338 if ( i == gi->gi_nodes ) {
342 btmp = gi->gi_n[i].gn_be;
343 pdn = &gi->gi_n[i].gn_pdn;
345 if (!btmp || !btmp->be_search)
347 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
349 if (tlimit0 != SLAP_NO_LIMIT) {
350 op->ors_tlimit = stoptime - slap_get_time ();
351 if (op->ors_tlimit <= 0) {
352 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
365 assert( op->o_bd->be_suffix != NULL );
366 assert( op->o_bd->be_nsuffix != NULL );
368 if (scope0 == LDAP_SCOPE_ONELEVEL &&
371 op->ors_scope = LDAP_SCOPE_BASE;
372 op->o_req_dn = op->o_bd->be_suffix[0];
373 op->o_req_ndn = op->o_bd->be_nsuffix[0];
374 rs->sr_err = op->o_bd->be_search(op, rs);
375 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
376 gs.err = LDAP_SUCCESS;
379 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
380 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
382 rs->sr_err = glue_sub_search( op, rs, b0, on );
384 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
385 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
387 op->o_req_dn = op->o_bd->be_suffix[0];
388 op->o_req_ndn = op->o_bd->be_nsuffix[0];
389 rs->sr_err = glue_sub_search( op, rs, b0, on );
390 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
391 gs.err = LDAP_SUCCESS;
396 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
397 rs->sr_err = glue_sub_search( op, rs, b0, on );
403 * Add errors that should result in dropping
406 case LDAP_SIZELIMIT_EXCEEDED:
407 case LDAP_TIMELIMIT_EXCEEDED:
408 case LDAP_ADMINLIMIT_EXCEEDED:
409 case LDAP_NO_SUCH_OBJECT:
410 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
411 case LDAP_X_CANNOT_CHAIN:
412 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
420 op->ors_scope = scope0;
421 op->ors_tlimit = tlimit0;
427 if ( op->o_abandon ) {
428 rs->sr_err = SLAPD_ABANDON;
430 op->o_callback = cb.sc_next;
432 rs->sr_matched = gs.matched;
433 rs->sr_ref = gs.refs;
434 rs->sr_ctrls = gs.ctrls;
436 send_ldap_result( op, rs );
440 op->o_bd->bd_info = bi0;
444 ber_bvarray_free(gs.refs);
446 for (i = gs.nctrls; --i >= 0; ) {
447 if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
448 free(gs.ctrls[i]->ldctl_value.bv_val);
456 static BackendDB toolDB;
459 glue_tool_entry_open (
464 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
466 /* We don't know which backend to talk to yet, so just
467 * remember the mode and move on...
473 toolDB.bd_info = oi->oi_orig;
479 glue_tool_entry_close (
486 if (!glueBack->be_entry_close)
488 rc = glueBack->be_entry_close (glueBack);
493 static slap_overinst *
498 slap_overinfo *oi = (slap_overinfo *)bi;
501 for ( on = oi->oi_list; on; on=on->on_next ) {
502 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
508 /* This function will only be called in tool mode */
514 slap_overinst *on = glue_tool_inst( bi );
515 glueinfo *gi = on->on_bi.bi_private;
516 static int glueOpened = 0;
517 int i, j, same, bsame = 0, rc = 0;
519 if (glueOpened) return 0;
523 /* If we were invoked in tool mode, open all the underlying backends */
524 if (slapMode & SLAP_TOOL_MODE) {
525 for (i = 0; i<gi->gi_nodes; i++) {
527 /* Same bi_open as our main backend? */
528 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
529 on->on_info->oi_orig->bi_open )
532 /* Loop thru the bd_info's and make sure we only
533 * invoke their bi_open functions once each.
535 for ( j = 0; j<i; j++ ) {
536 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
537 gi->gi_n[j].gn_be->bd_info->bi_open ) {
542 /* OK, it's unique and non-NULL, call it. */
543 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
544 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
545 gi->gi_n[i].gn_be->bd_info );
546 /* Let backend.c take care of the rest of startup */
548 rc = backend_startup_one( gi->gi_n[i].gn_be );
551 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
552 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
554 } /* other case is impossible */
558 /* This function will only be called in tool mode */
564 static int glueClosed = 0;
567 if (glueClosed) return 0;
571 if (slapMode & SLAP_TOOL_MODE) {
572 rc = backend_shutdown( NULL );
578 glue_entry_release_rw (
589 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
590 op->o_bd = glue_back_select (&b2, &e->e_nname);
592 if ( op->o_bd->be_release ) {
593 rc = op->o_bd->be_release( op, e, rw );
596 /* FIXME: mimic be_entry_release_rw
597 * when no be_release() available */
607 glue_tool_entry_first (
611 slap_overinst *on = glue_tool_inst( b0->bd_info );
612 glueinfo *gi = on->on_bi.bi_private;
615 /* If we're starting from scratch, start at the most general */
617 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
620 for (i = gi->gi_nodes-1; i >= 0; i--) {
621 if (gi->gi_n[i].gn_be->be_entry_open &&
622 gi->gi_n[i].gn_be->be_entry_first) {
623 glueBack = gi->gi_n[i].gn_be;
629 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
630 glueBack->be_entry_open (glueBack, glueMode) != 0)
633 return glueBack->be_entry_first (glueBack);
637 glue_tool_entry_next (
641 slap_overinst *on = glue_tool_inst( b0->bd_info );
642 glueinfo *gi = on->on_bi.bi_private;
646 if (!glueBack || !glueBack->be_entry_next)
649 rc = glueBack->be_entry_next (glueBack);
651 /* If we ran out of entries in one database, move on to the next */
653 if ( glueBack && glueBack->be_entry_close )
654 glueBack->be_entry_close (glueBack);
655 for (i=0; i<gi->gi_nodes; i++) {
656 if (gi->gi_n[i].gn_be == glueBack)
663 glueBack = gi->gi_n[i-1].gn_be;
664 rc = glue_tool_entry_first (b0);
671 glue_tool_entry_get (
676 if (!glueBack || !glueBack->be_entry_get)
679 return glueBack->be_entry_get (glueBack, id);
683 glue_tool_entry_put (
693 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
694 be = glue_back_select (&b2, &e->e_nname);
695 if ( be == &b2 ) be = &toolDB;
697 if (!be->be_entry_put)
701 if ( be->be_entry_open ) {
702 rc = be->be_entry_open (be, glueMode);
707 } else if (be != glueBack) {
708 /* If this entry belongs in a different branch than the
709 * previous one, close the current database and open the
712 if ( glueBack->be_entry_close ) {
713 glueBack->be_entry_close (glueBack);
715 if ( be->be_entry_open ) {
716 rc = be->be_entry_open (be, glueMode);
723 return be->be_entry_put (be, e, text);
727 glue_tool_entry_reindex (
732 if (!glueBack || !glueBack->be_entry_reindex)
735 return glueBack->be_entry_reindex (glueBack, id);
743 slap_overinst *on = glue_tool_inst( b0->bd_info );
744 glueinfo *gi = on->on_bi.bi_private;
745 BackendInfo *bi = b0->bd_info;
748 /* just sync everyone */
749 for (i = 0; i<gi->gi_nodes; i++)
750 if (gi->gi_n[i].gn_be->be_sync)
751 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
752 b0->bd_info = on->on_info->oi_orig;
764 slap_overinst *on = (slap_overinst *)be->bd_info;
765 slap_overinfo *oi = on->on_info;
766 BackendInfo *bi = oi->oi_orig;
769 gi = ch_calloc( 1, sizeof(glueinfo));
770 on->on_bi.bi_private = gi;
771 dnParent( be->be_nsuffix, &gi->gi_pdn );
773 /* Currently the overlay framework doesn't handle these entry points
774 * but we need them....
776 oi->oi_bi.bi_open = glue_open;
777 oi->oi_bi.bi_close = glue_close;
779 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
781 /* Only advertise these if the root DB supports them */
782 if ( bi->bi_tool_entry_open )
783 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
784 if ( bi->bi_tool_entry_close )
785 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
786 if ( bi->bi_tool_entry_first )
787 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
788 if ( bi->bi_tool_entry_next )
789 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
790 if ( bi->bi_tool_entry_get )
791 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
792 if ( bi->bi_tool_entry_put )
793 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
794 if ( bi->bi_tool_entry_reindex )
795 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
796 if ( bi->bi_tool_sync )
797 oi->oi_bi.bi_tool_sync = glue_tool_sync;
799 /*FIXME : need to add support */
800 oi->oi_bi.bi_tool_dn2id_get = 0;
801 oi->oi_bi.bi_tool_id2entry_get = 0;
802 oi->oi_bi.bi_tool_entry_modify = 0;
804 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
814 slap_overinst *on = (slap_overinst *)be->bd_info;
815 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
818 return SLAP_CB_CONTINUE;
826 slap_overinst *on = (slap_overinst *)be->bd_info;
828 on->on_info->oi_bi.bi_db_close = 0;
833 glue_sub_del( BackendDB *b0 )
838 /* Find the top backend for this subordinate */
840 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
846 if ( SLAP_GLUE_SUBORDINATE( be ))
848 if ( !SLAP_GLUE_INSTANCE( be ))
850 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
853 /* OK, got the right backend, find the overlay */
854 oi = (slap_overinfo *)be->bd_info;
855 for ( on=oi->oi_list; on; on=on->on_next ) {
856 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
859 assert( on != NULL );
860 gi = on->on_bi.bi_private;
861 for ( i=0; i < gi->gi_nodes; i++ ) {
862 if ( gi->gi_n[i].gn_be == b0 ) {
865 for (j=i+1; j < gi->gi_nodes; j++)
866 gi->gi_n[j-1] = gi->gi_n[j];
873 rc = LDAP_NO_SUCH_OBJECT;
878 typedef struct glue_Addrec {
879 struct glue_Addrec *ga_next;
883 /* List of added subordinates */
884 static glue_Addrec *ga_list;
886 /* Attach all the subordinate backends to their superior */
890 glue_Addrec *ga, *gnext = NULL;
893 /* For all the subordinate backends */
894 for ( ga=ga_list; ga != NULL; ga = gnext ) {
899 /* Find the top backend for this subordinate */
901 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
906 if ( SLAP_GLUE_SUBORDINATE( be ))
908 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
911 /* If it's not already configured, set up the overlay */
912 if ( !SLAP_GLUE_INSTANCE( be )) {
913 rc = overlay_config( be, glue.on_bi.bi_type );
917 /* Find the overlay instance */
918 oi = (slap_overinfo *)be->bd_info;
919 for ( on=oi->oi_list; on; on=on->on_next ) {
920 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
923 assert( on != NULL );
924 gi = on->on_bi.bi_private;
925 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
926 gi->gi_nodes * sizeof(gluenode));
927 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
928 dnParent( &ga->ga_be->be_nsuffix[0],
929 &gi->gi_n[gi->gi_nodes].gn_pdn );
931 on->on_bi.bi_private = gi;
935 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
936 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
937 rc = LDAP_NO_SUCH_OBJECT;
949 glue_sub_add( BackendDB *be, int advert, int online )
954 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
956 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
958 ga = ch_malloc( sizeof( glue_Addrec ));
959 ga->ga_next = ga_list;
964 rc = glue_sub_attach();
972 glue.on_bi.bi_type = "glue";
974 glue.on_bi.bi_db_init = glue_db_init;
975 glue.on_bi.bi_db_close = glue_db_close;
976 glue.on_bi.bi_db_destroy = glue_db_destroy;
978 glue.on_bi.bi_op_search = glue_op_search;
979 glue.on_bi.bi_op_modify = glue_op_func;
980 glue.on_bi.bi_op_modrdn = glue_op_func;
981 glue.on_bi.bi_op_add = glue_op_func;
982 glue.on_bi.bi_op_delete = glue_op_func;
984 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
985 glue.on_bi.bi_chk_controls = glue_chk_controls;
987 return overlay_register( &glue );