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);
157 b0->bd_info = on->on_info->oi_orig;
160 case LDAP_REQ_ADD: which = op_add; break;
161 case LDAP_REQ_DELETE: which = op_delete; break;
162 case LDAP_REQ_MODIFY: which = op_modify; break;
163 case LDAP_REQ_MODRDN: which = op_modrdn; break;
164 default: assert( 0 ); break;
167 func = &op->o_bd->bd_info->bi_op_bind;
169 rc = func[which]( op, rs );
171 rc = SLAP_CB_CONTINUE;
174 op->o_bd->bd_info = bi0;
179 glue_chk_referrals ( Operation *op, SlapReply *rs )
181 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
182 BackendDB *b0 = op->o_bd;
183 BackendInfo *bi0 = op->o_bd->bd_info;
186 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
187 b0->bd_info = on->on_info->oi_orig;
189 if ( op->o_bd->bd_info->bi_chk_referrals )
190 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
192 rc = SLAP_CB_CONTINUE;
195 op->o_bd->bd_info = bi0;
200 glue_chk_controls ( Operation *op, SlapReply *rs )
202 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
203 BackendDB *b0 = op->o_bd;
204 BackendInfo *bi0 = op->o_bd->bd_info;
205 int rc = SLAP_CB_CONTINUE;
207 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
208 b0->bd_info = on->on_info->oi_orig;
210 /* if the subordinate database has overlays, the bi_chk_controls()
211 * hook is actually over_aux_chk_controls(); in case it actually
212 * wraps a missing hok, we need to mimic the behavior
213 * of the frontend applied to that database */
214 if ( op->o_bd->bd_info->bi_chk_controls ) {
215 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
219 if ( rc == SLAP_CB_CONTINUE ) {
220 rc = backend_check_controls( op, rs );
224 op->o_bd->bd_info = bi0;
229 glue_op_search ( Operation *op, SlapReply *rs )
231 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
232 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
233 BackendDB *b0 = op->o_bd;
234 BackendDB *b1 = NULL, *btmp;
235 BackendInfo *bi0 = op->o_bd->bd_info;
238 glue_state gs = {0, 0, NULL, 0, NULL};
239 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
241 struct berval dn, ndn, *pdn;
245 cb.sc_next = op->o_callback;
247 stoptime = slap_get_time () + op->ors_tlimit;
249 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
250 b0->bd_info = on->on_info->oi_orig;
252 switch (op->ors_scope) {
253 case LDAP_SCOPE_BASE:
254 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
255 if (op->o_bd && op->o_bd->be_search) {
256 rs->sr_err = op->o_bd->be_search( op, rs );
260 case LDAP_SCOPE_ONELEVEL:
261 case LDAP_SCOPE_SUBTREE:
262 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
266 if (op->o_bd && op->o_bd->be_search) {
267 rs->sr_err = op->o_bd->be_search( op, rs );
269 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
270 "No search target found");
276 op->o_callback = &cb;
277 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
278 scope0 = op->ors_scope;
279 tlimit0 = op->ors_tlimit;
285 * Execute in reverse order, most general first
287 for (i = gi->gi_nodes; i >= 0; i--) {
288 if ( i == gi->gi_nodes ) {
292 btmp = gi->gi_n[i].gn_be;
293 pdn = &gi->gi_n[i].gn_pdn;
295 if (!btmp || !btmp->be_search)
297 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
299 if (tlimit0 != SLAP_NO_LIMIT) {
300 op->ors_tlimit = stoptime - slap_get_time ();
301 if (op->ors_tlimit <= 0) {
302 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
315 assert( op->o_bd->be_suffix != NULL );
316 assert( op->o_bd->be_nsuffix != NULL );
318 if (scope0 == LDAP_SCOPE_ONELEVEL &&
321 op->ors_scope = LDAP_SCOPE_BASE;
322 op->o_req_dn = op->o_bd->be_suffix[0];
323 op->o_req_ndn = op->o_bd->be_nsuffix[0];
324 rs->sr_err = op->o_bd->be_search(op, rs);
325 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
326 gs.err = LDAP_SUCCESS;
329 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
330 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
332 rs->sr_err = op->o_bd->be_search( op, rs );
334 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
335 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
337 op->o_req_dn = op->o_bd->be_suffix[0];
338 op->o_req_ndn = op->o_bd->be_nsuffix[0];
339 rs->sr_err = op->o_bd->be_search( op, rs );
340 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
341 gs.err = LDAP_SUCCESS;
346 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
347 rs->sr_err = op->o_bd->be_search( op, rs );
353 * Add errors that should result in dropping
356 case LDAP_SIZELIMIT_EXCEEDED:
357 case LDAP_TIMELIMIT_EXCEEDED:
358 case LDAP_ADMINLIMIT_EXCEEDED:
359 case LDAP_NO_SUCH_OBJECT:
360 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
361 case LDAP_X_CANNOT_CHAIN:
362 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
370 op->ors_scope = scope0;
371 op->ors_tlimit = tlimit0;
377 if ( op->o_abandon ) {
378 rs->sr_err = SLAPD_ABANDON;
380 op->o_callback = cb.sc_next;
382 rs->sr_matched = gs.matched;
383 rs->sr_ref = gs.refs;
385 send_ldap_result( op, rs );
389 op->o_bd->bd_info = bi0;
393 ber_bvarray_free(gs.refs);
397 static BackendDB toolDB;
400 glue_tool_entry_open (
405 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
407 /* We don't know which backend to talk to yet, so just
408 * remember the mode and move on...
414 toolDB.bd_info = oi->oi_orig;
420 glue_tool_entry_close (
427 if (!glueBack->be_entry_close)
429 rc = glueBack->be_entry_close (glueBack);
434 static slap_overinst *
439 slap_overinfo *oi = (slap_overinfo *)bi;
442 for ( on = oi->oi_list; on; on=on->on_next ) {
443 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
449 /* This function will only be called in tool mode */
455 slap_overinst *on = glue_tool_inst( bi );
456 glueinfo *gi = on->on_bi.bi_private;
457 static int glueOpened = 0;
458 int i, j, same, bsame = 0, rc = 0;
460 if (glueOpened) return 0;
464 /* If we were invoked in tool mode, open all the underlying backends */
465 if (slapMode & SLAP_TOOL_MODE) {
466 for (i = 0; i<gi->gi_nodes; i++) {
468 /* Same bi_open as our main backend? */
469 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
470 on->on_info->oi_orig->bi_open )
473 /* Loop thru the bd_info's and make sure we only
474 * invoke their bi_open functions once each.
476 for ( j = 0; j<i; j++ ) {
477 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
478 gi->gi_n[j].gn_be->bd_info->bi_open ) {
483 /* OK, it's unique and non-NULL, call it. */
484 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
485 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
486 gi->gi_n[i].gn_be->bd_info );
487 /* Let backend.c take care of the rest of startup */
489 rc = backend_startup_one( gi->gi_n[i].gn_be );
492 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
493 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
495 } /* other case is impossible */
499 /* This function will only be called in tool mode */
505 static int glueClosed = 0;
508 if (glueClosed) return 0;
512 if (slapMode & SLAP_TOOL_MODE) {
513 rc = backend_shutdown( NULL );
519 glue_entry_release_rw (
530 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
531 op->o_bd = glue_back_select (&b2, &e->e_nname);
533 if ( op->o_bd->be_release ) {
534 rc = op->o_bd->be_release( op, e, rw );
537 /* FIXME: mimic be_entry_release_rw
538 * when no be_release() available */
548 glue_tool_entry_first (
552 slap_overinst *on = glue_tool_inst( b0->bd_info );
553 glueinfo *gi = on->on_bi.bi_private;
556 /* If we're starting from scratch, start at the most general */
558 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
561 for (i = gi->gi_nodes-1; i >= 0; i--) {
562 if (gi->gi_n[i].gn_be->be_entry_open &&
563 gi->gi_n[i].gn_be->be_entry_first) {
564 glueBack = gi->gi_n[i].gn_be;
570 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
571 glueBack->be_entry_open (glueBack, glueMode) != 0)
574 return glueBack->be_entry_first (glueBack);
578 glue_tool_entry_next (
582 slap_overinst *on = glue_tool_inst( b0->bd_info );
583 glueinfo *gi = on->on_bi.bi_private;
587 if (!glueBack || !glueBack->be_entry_next)
590 rc = glueBack->be_entry_next (glueBack);
592 /* If we ran out of entries in one database, move on to the next */
594 if ( glueBack && glueBack->be_entry_close )
595 glueBack->be_entry_close (glueBack);
596 for (i=0; i<gi->gi_nodes; i++) {
597 if (gi->gi_n[i].gn_be == glueBack)
604 glueBack = gi->gi_n[i-1].gn_be;
605 rc = glue_tool_entry_first (b0);
612 glue_tool_entry_get (
617 if (!glueBack || !glueBack->be_entry_get)
620 return glueBack->be_entry_get (glueBack, id);
624 glue_tool_entry_put (
634 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
635 be = glue_back_select (&b2, &e->e_nname);
636 if ( be == &b2 ) be = &toolDB;
638 if (!be->be_entry_put)
642 if ( be->be_entry_open ) {
643 rc = be->be_entry_open (be, glueMode);
648 } else if (be != glueBack) {
649 /* If this entry belongs in a different branch than the
650 * previous one, close the current database and open the
653 if ( glueBack->be_entry_close ) {
654 glueBack->be_entry_close (glueBack);
656 if ( be->be_entry_open ) {
657 rc = be->be_entry_open (be, glueMode);
664 return be->be_entry_put (be, e, text);
668 glue_tool_entry_reindex (
673 if (!glueBack || !glueBack->be_entry_reindex)
676 return glueBack->be_entry_reindex (glueBack, id);
684 slap_overinst *on = glue_tool_inst( b0->bd_info );
685 glueinfo *gi = on->on_bi.bi_private;
686 BackendInfo *bi = b0->bd_info;
689 /* just sync everyone */
690 for (i = 0; i<gi->gi_nodes; i++)
691 if (gi->gi_n[i].gn_be->be_sync)
692 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
693 b0->bd_info = on->on_info->oi_orig;
705 slap_overinst *on = (slap_overinst *)be->bd_info;
706 slap_overinfo *oi = on->on_info;
707 BackendInfo *bi = oi->oi_orig;
710 gi = ch_calloc( 1, sizeof(glueinfo));
711 on->on_bi.bi_private = gi;
712 dnParent( be->be_nsuffix, &gi->gi_pdn );
714 /* Currently the overlay framework doesn't handle these entry points
715 * but we need them....
717 oi->oi_bi.bi_open = glue_open;
718 oi->oi_bi.bi_close = glue_close;
720 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
722 /* Only advertise these if the root DB supports them */
723 if ( bi->bi_tool_entry_open )
724 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
725 if ( bi->bi_tool_entry_close )
726 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
727 if ( bi->bi_tool_entry_first )
728 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
729 if ( bi->bi_tool_entry_next )
730 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
731 if ( bi->bi_tool_entry_get )
732 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
733 if ( bi->bi_tool_entry_put )
734 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
735 if ( bi->bi_tool_entry_reindex )
736 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
737 if ( bi->bi_tool_sync )
738 oi->oi_bi.bi_tool_sync = glue_tool_sync;
740 /*FIXME : need to add support */
741 oi->oi_bi.bi_tool_dn2id_get = 0;
742 oi->oi_bi.bi_tool_id2entry_get = 0;
743 oi->oi_bi.bi_tool_entry_modify = 0;
745 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
755 slap_overinst *on = (slap_overinst *)be->bd_info;
756 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
759 return SLAP_CB_CONTINUE;
767 slap_overinst *on = (slap_overinst *)be->bd_info;
769 on->on_info->oi_bi.bi_db_close = NULL;
774 glue_sub_del( BackendDB *b0 )
779 /* Find the top backend for this subordinate */
781 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
787 if ( SLAP_GLUE_SUBORDINATE( be ))
789 if ( !SLAP_GLUE_INSTANCE( be ))
791 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
794 /* OK, got the right backend, find the overlay */
795 oi = (slap_overinfo *)be->bd_info;
796 for ( on=oi->oi_list; on; on=on->on_next ) {
797 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
800 assert( on != NULL );
801 gi = on->on_bi.bi_private;
802 for ( i=0; i < gi->gi_nodes; i++ ) {
803 if ( gi->gi_n[i].gn_be == b0 ) {
806 for (j=i+1; j < gi->gi_nodes; j++)
807 gi->gi_n[j-1] = gi->gi_n[j];
814 rc = LDAP_NO_SUCH_OBJECT;
819 typedef struct glue_Addrec {
820 struct glue_Addrec *ga_next;
824 /* List of added subordinates */
825 static glue_Addrec *ga_list;
827 /* Attach all the subordinate backends to their superior */
831 glue_Addrec *ga, *gnext = NULL;
834 /* For all the subordinate backends */
835 for ( ga=ga_list; ga != NULL; ga = gnext ) {
840 /* Find the top backend for this subordinate */
842 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
847 if ( SLAP_GLUE_SUBORDINATE( be ))
849 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
852 /* If it's not already configured, set up the overlay */
853 if ( !SLAP_GLUE_INSTANCE( be )) {
854 rc = overlay_config( be, glue.on_bi.bi_type );
858 /* Find the overlay instance */
859 oi = (slap_overinfo *)be->bd_info;
860 for ( on=oi->oi_list; on; on=on->on_next ) {
861 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
864 assert( on != NULL );
865 gi = on->on_bi.bi_private;
866 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
867 gi->gi_nodes * sizeof(gluenode));
868 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
869 dnParent( &ga->ga_be->be_nsuffix[0],
870 &gi->gi_n[gi->gi_nodes].gn_pdn );
872 on->on_bi.bi_private = gi;
876 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
877 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
878 rc = LDAP_NO_SUCH_OBJECT;
890 glue_sub_add( BackendDB *be, int advert, int online )
895 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
897 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
899 ga = ch_malloc( sizeof( glue_Addrec ));
900 ga->ga_next = ga_list;
905 rc = glue_sub_attach();
913 glue.on_bi.bi_type = "glue";
915 glue.on_bi.bi_db_init = glue_db_init;
916 glue.on_bi.bi_db_close = glue_db_close;
917 glue.on_bi.bi_db_destroy = glue_db_destroy;
919 glue.on_bi.bi_op_search = glue_op_search;
920 glue.on_bi.bi_op_modify = glue_op_func;
921 glue.on_bi.bi_op_modrdn = glue_op_func;
922 glue.on_bi.bi_op_add = glue_op_func;
923 glue.on_bi.bi_op_delete = glue_op_func;
925 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
926 glue.on_bi.bi_chk_controls = glue_chk_controls;
928 return overlay_register( &glue );