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);
183 b0->bd_info = on->on_info->oi_orig;
186 case LDAP_REQ_ADD: which = op_add; break;
187 case LDAP_REQ_DELETE: which = op_delete; break;
188 case LDAP_REQ_MODIFY: which = op_modify; break;
189 case LDAP_REQ_MODRDN: which = op_modrdn; break;
190 default: assert( 0 ); break;
193 func = &op->o_bd->bd_info->bi_op_bind;
195 rc = func[which]( op, rs );
197 rc = SLAP_CB_CONTINUE;
200 op->o_bd->bd_info = bi0;
205 glue_chk_referrals ( Operation *op, SlapReply *rs )
207 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
208 BackendDB *b0 = op->o_bd;
209 BackendInfo *bi0 = op->o_bd->bd_info;
212 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
213 b0->bd_info = on->on_info->oi_orig;
215 if ( op->o_bd->bd_info->bi_chk_referrals )
216 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
218 rc = SLAP_CB_CONTINUE;
221 op->o_bd->bd_info = bi0;
226 glue_chk_controls ( Operation *op, SlapReply *rs )
228 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
229 BackendDB *b0 = op->o_bd;
230 BackendInfo *bi0 = op->o_bd->bd_info;
231 int rc = SLAP_CB_CONTINUE;
233 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
234 b0->bd_info = on->on_info->oi_orig;
236 /* if the subordinate database has overlays, the bi_chk_controls()
237 * hook is actually over_aux_chk_controls(); in case it actually
238 * wraps a missing hok, we need to mimic the behavior
239 * of the frontend applied to that database */
240 if ( op->o_bd->bd_info->bi_chk_controls ) {
241 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
245 if ( rc == SLAP_CB_CONTINUE ) {
246 rc = backend_check_controls( op, rs );
250 op->o_bd->bd_info = bi0;
255 glue_op_search ( Operation *op, SlapReply *rs )
257 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
258 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
259 BackendDB *b0 = op->o_bd;
260 BackendDB *b1 = NULL, *btmp;
261 BackendInfo *bi0 = op->o_bd->bd_info;
264 glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
265 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
267 struct berval dn, ndn, *pdn;
271 cb.sc_next = op->o_callback;
273 stoptime = slap_get_time () + op->ors_tlimit;
275 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
276 b0->bd_info = on->on_info->oi_orig;
278 switch (op->ors_scope) {
279 case LDAP_SCOPE_BASE:
280 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
281 if (op->o_bd && op->o_bd->be_search) {
282 rs->sr_err = op->o_bd->be_search( op, rs );
286 case LDAP_SCOPE_ONELEVEL:
287 case LDAP_SCOPE_SUBTREE:
288 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
292 if (op->o_bd && op->o_bd->be_search) {
293 rs->sr_err = op->o_bd->be_search( op, rs );
295 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
296 "No search target found");
302 op->o_callback = &cb;
303 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
304 scope0 = op->ors_scope;
305 tlimit0 = op->ors_tlimit;
311 * Execute in reverse order, most general first
313 for (i = gi->gi_nodes; i >= 0; i--) {
314 if ( i == gi->gi_nodes ) {
318 btmp = gi->gi_n[i].gn_be;
319 pdn = &gi->gi_n[i].gn_pdn;
321 if (!btmp || !btmp->be_search)
323 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
325 if (tlimit0 != SLAP_NO_LIMIT) {
326 op->ors_tlimit = stoptime - slap_get_time ();
327 if (op->ors_tlimit <= 0) {
328 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
341 assert( op->o_bd->be_suffix != NULL );
342 assert( op->o_bd->be_nsuffix != NULL );
344 if (scope0 == LDAP_SCOPE_ONELEVEL &&
347 op->ors_scope = LDAP_SCOPE_BASE;
348 op->o_req_dn = op->o_bd->be_suffix[0];
349 op->o_req_ndn = op->o_bd->be_nsuffix[0];
350 rs->sr_err = op->o_bd->be_search(op, rs);
351 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
352 gs.err = LDAP_SUCCESS;
355 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
356 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
358 rs->sr_err = op->o_bd->be_search( op, rs );
360 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
361 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
363 op->o_req_dn = op->o_bd->be_suffix[0];
364 op->o_req_ndn = op->o_bd->be_nsuffix[0];
365 rs->sr_err = op->o_bd->be_search( op, rs );
366 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
367 gs.err = LDAP_SUCCESS;
372 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
373 rs->sr_err = op->o_bd->be_search( op, rs );
379 * Add errors that should result in dropping
382 case LDAP_SIZELIMIT_EXCEEDED:
383 case LDAP_TIMELIMIT_EXCEEDED:
384 case LDAP_ADMINLIMIT_EXCEEDED:
385 case LDAP_NO_SUCH_OBJECT:
386 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
387 case LDAP_X_CANNOT_CHAIN:
388 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
396 op->ors_scope = scope0;
397 op->ors_tlimit = tlimit0;
403 if ( op->o_abandon ) {
404 rs->sr_err = SLAPD_ABANDON;
406 op->o_callback = cb.sc_next;
408 rs->sr_matched = gs.matched;
409 rs->sr_ref = gs.refs;
410 rs->sr_ctrls = gs.ctrls;
412 send_ldap_result( op, rs );
416 op->o_bd->bd_info = bi0;
420 ber_bvarray_free(gs.refs);
422 for (i = gs.nctrls; --i >= 0; ) {
423 if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
424 free(gs.ctrls[i]->ldctl_value.bv_val);
432 static BackendDB toolDB;
435 glue_tool_entry_open (
440 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
442 /* We don't know which backend to talk to yet, so just
443 * remember the mode and move on...
449 toolDB.bd_info = oi->oi_orig;
455 glue_tool_entry_close (
462 if (!glueBack->be_entry_close)
464 rc = glueBack->be_entry_close (glueBack);
469 static slap_overinst *
474 slap_overinfo *oi = (slap_overinfo *)bi;
477 for ( on = oi->oi_list; on; on=on->on_next ) {
478 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
484 /* This function will only be called in tool mode */
490 slap_overinst *on = glue_tool_inst( bi );
491 glueinfo *gi = on->on_bi.bi_private;
492 static int glueOpened = 0;
493 int i, j, same, bsame = 0, rc = 0;
495 if (glueOpened) return 0;
499 /* If we were invoked in tool mode, open all the underlying backends */
500 if (slapMode & SLAP_TOOL_MODE) {
501 for (i = 0; i<gi->gi_nodes; i++) {
503 /* Same bi_open as our main backend? */
504 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
505 on->on_info->oi_orig->bi_open )
508 /* Loop thru the bd_info's and make sure we only
509 * invoke their bi_open functions once each.
511 for ( j = 0; j<i; j++ ) {
512 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
513 gi->gi_n[j].gn_be->bd_info->bi_open ) {
518 /* OK, it's unique and non-NULL, call it. */
519 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
520 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
521 gi->gi_n[i].gn_be->bd_info );
522 /* Let backend.c take care of the rest of startup */
524 rc = backend_startup_one( gi->gi_n[i].gn_be );
527 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
528 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
530 } /* other case is impossible */
534 /* This function will only be called in tool mode */
540 static int glueClosed = 0;
543 if (glueClosed) return 0;
547 if (slapMode & SLAP_TOOL_MODE) {
548 rc = backend_shutdown( NULL );
554 glue_entry_release_rw (
565 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
566 op->o_bd = glue_back_select (&b2, &e->e_nname);
568 if ( op->o_bd->be_release ) {
569 rc = op->o_bd->be_release( op, e, rw );
572 /* FIXME: mimic be_entry_release_rw
573 * when no be_release() available */
583 glue_tool_entry_first (
587 slap_overinst *on = glue_tool_inst( b0->bd_info );
588 glueinfo *gi = on->on_bi.bi_private;
591 /* If we're starting from scratch, start at the most general */
593 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
596 for (i = gi->gi_nodes-1; i >= 0; i--) {
597 if (gi->gi_n[i].gn_be->be_entry_open &&
598 gi->gi_n[i].gn_be->be_entry_first) {
599 glueBack = gi->gi_n[i].gn_be;
605 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
606 glueBack->be_entry_open (glueBack, glueMode) != 0)
609 return glueBack->be_entry_first (glueBack);
613 glue_tool_entry_next (
617 slap_overinst *on = glue_tool_inst( b0->bd_info );
618 glueinfo *gi = on->on_bi.bi_private;
622 if (!glueBack || !glueBack->be_entry_next)
625 rc = glueBack->be_entry_next (glueBack);
627 /* If we ran out of entries in one database, move on to the next */
629 if ( glueBack && glueBack->be_entry_close )
630 glueBack->be_entry_close (glueBack);
631 for (i=0; i<gi->gi_nodes; i++) {
632 if (gi->gi_n[i].gn_be == glueBack)
639 glueBack = gi->gi_n[i-1].gn_be;
640 rc = glue_tool_entry_first (b0);
647 glue_tool_entry_get (
652 if (!glueBack || !glueBack->be_entry_get)
655 return glueBack->be_entry_get (glueBack, id);
659 glue_tool_entry_put (
669 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
670 be = glue_back_select (&b2, &e->e_nname);
671 if ( be == &b2 ) be = &toolDB;
673 if (!be->be_entry_put)
677 if ( be->be_entry_open ) {
678 rc = be->be_entry_open (be, glueMode);
683 } else if (be != glueBack) {
684 /* If this entry belongs in a different branch than the
685 * previous one, close the current database and open the
688 if ( glueBack->be_entry_close ) {
689 glueBack->be_entry_close (glueBack);
691 if ( be->be_entry_open ) {
692 rc = be->be_entry_open (be, glueMode);
699 return be->be_entry_put (be, e, text);
703 glue_tool_entry_reindex (
708 if (!glueBack || !glueBack->be_entry_reindex)
711 return glueBack->be_entry_reindex (glueBack, id);
719 slap_overinst *on = glue_tool_inst( b0->bd_info );
720 glueinfo *gi = on->on_bi.bi_private;
721 BackendInfo *bi = b0->bd_info;
724 /* just sync everyone */
725 for (i = 0; i<gi->gi_nodes; i++)
726 if (gi->gi_n[i].gn_be->be_sync)
727 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
728 b0->bd_info = on->on_info->oi_orig;
740 slap_overinst *on = (slap_overinst *)be->bd_info;
741 slap_overinfo *oi = on->on_info;
742 BackendInfo *bi = oi->oi_orig;
745 gi = ch_calloc( 1, sizeof(glueinfo));
746 on->on_bi.bi_private = gi;
747 dnParent( be->be_nsuffix, &gi->gi_pdn );
749 /* Currently the overlay framework doesn't handle these entry points
750 * but we need them....
752 oi->oi_bi.bi_open = glue_open;
753 oi->oi_bi.bi_close = glue_close;
755 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
757 /* Only advertise these if the root DB supports them */
758 if ( bi->bi_tool_entry_open )
759 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
760 if ( bi->bi_tool_entry_close )
761 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
762 if ( bi->bi_tool_entry_first )
763 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
764 if ( bi->bi_tool_entry_next )
765 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
766 if ( bi->bi_tool_entry_get )
767 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
768 if ( bi->bi_tool_entry_put )
769 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
770 if ( bi->bi_tool_entry_reindex )
771 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
772 if ( bi->bi_tool_sync )
773 oi->oi_bi.bi_tool_sync = glue_tool_sync;
775 /*FIXME : need to add support */
776 oi->oi_bi.bi_tool_dn2id_get = 0;
777 oi->oi_bi.bi_tool_id2entry_get = 0;
778 oi->oi_bi.bi_tool_entry_modify = 0;
780 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
790 slap_overinst *on = (slap_overinst *)be->bd_info;
791 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
794 return SLAP_CB_CONTINUE;
802 slap_overinst *on = (slap_overinst *)be->bd_info;
804 on->on_info->oi_bi.bi_db_close = NULL;
809 glue_sub_del( BackendDB *b0 )
814 /* Find the top backend for this subordinate */
816 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
822 if ( SLAP_GLUE_SUBORDINATE( be ))
824 if ( !SLAP_GLUE_INSTANCE( be ))
826 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
829 /* OK, got the right backend, find the overlay */
830 oi = (slap_overinfo *)be->bd_info;
831 for ( on=oi->oi_list; on; on=on->on_next ) {
832 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
835 assert( on != NULL );
836 gi = on->on_bi.bi_private;
837 for ( i=0; i < gi->gi_nodes; i++ ) {
838 if ( gi->gi_n[i].gn_be == b0 ) {
841 for (j=i+1; j < gi->gi_nodes; j++)
842 gi->gi_n[j-1] = gi->gi_n[j];
849 rc = LDAP_NO_SUCH_OBJECT;
854 typedef struct glue_Addrec {
855 struct glue_Addrec *ga_next;
859 /* List of added subordinates */
860 static glue_Addrec *ga_list;
862 /* Attach all the subordinate backends to their superior */
866 glue_Addrec *ga, *gnext = NULL;
869 /* For all the subordinate backends */
870 for ( ga=ga_list; ga != NULL; ga = gnext ) {
875 /* Find the top backend for this subordinate */
877 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
882 if ( SLAP_GLUE_SUBORDINATE( be ))
884 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
887 /* If it's not already configured, set up the overlay */
888 if ( !SLAP_GLUE_INSTANCE( be )) {
889 rc = overlay_config( be, glue.on_bi.bi_type );
893 /* Find the overlay instance */
894 oi = (slap_overinfo *)be->bd_info;
895 for ( on=oi->oi_list; on; on=on->on_next ) {
896 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
899 assert( on != NULL );
900 gi = on->on_bi.bi_private;
901 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
902 gi->gi_nodes * sizeof(gluenode));
903 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
904 dnParent( &ga->ga_be->be_nsuffix[0],
905 &gi->gi_n[gi->gi_nodes].gn_pdn );
907 on->on_bi.bi_private = gi;
911 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
912 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
913 rc = LDAP_NO_SUCH_OBJECT;
925 glue_sub_add( BackendDB *be, int advert, int online )
930 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
932 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
934 ga = ch_malloc( sizeof( glue_Addrec ));
935 ga->ga_next = ga_list;
940 rc = glue_sub_attach();
948 glue.on_bi.bi_type = "glue";
950 glue.on_bi.bi_db_init = glue_db_init;
951 glue.on_bi.bi_db_close = glue_db_close;
952 glue.on_bi.bi_db_destroy = glue_db_destroy;
954 glue.on_bi.bi_op_search = glue_op_search;
955 glue.on_bi.bi_op_modify = glue_op_func;
956 glue.on_bi.bi_op_modrdn = glue_op_func;
957 glue.on_bi.bi_op_add = glue_op_func;
958 glue.on_bi.bi_op_delete = glue_op_func;
960 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
961 glue.on_bi.bi_chk_controls = glue_chk_controls;
963 return overlay_register( &glue );