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 bi_open as our main backend? */
481 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
482 on->on_info->oi_orig->bi_open )
485 /* Loop thru the bd_info's and make sure we only
486 * invoke their bi_open functions once each.
488 for ( j = 0; j<i; j++ ) {
489 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
490 gi->gi_n[j].gn_be->bd_info->bi_open ) {
495 /* OK, it's unique and non-NULL, call it. */
496 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
497 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
498 gi->gi_n[i].gn_be->bd_info );
499 /* Let backend.c take care of the rest of startup */
501 rc = backend_startup_one( gi->gi_n[i].gn_be );
504 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
505 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
507 } /* other case is impossible */
511 /* This function will only be called in tool mode */
517 static int glueClosed = 0;
520 if (glueClosed) return 0;
524 if (slapMode & SLAP_TOOL_MODE) {
525 rc = backend_shutdown( NULL );
531 glue_entry_release_rw (
542 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
543 op->o_bd = glue_back_select (&b2, &e->e_nname);
545 if ( op->o_bd->be_release ) {
546 rc = op->o_bd->be_release( op, e, rw );
549 /* FIXME: mimic be_entry_release_rw
550 * when no be_release() available */
560 glue_tool_entry_first (
564 slap_overinst *on = glue_tool_inst( b0->bd_info );
565 glueinfo *gi = on->on_bi.bi_private;
568 /* If we're starting from scratch, start at the most general */
570 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
573 for (i = gi->gi_nodes-1; i >= 0; i--) {
574 if (gi->gi_n[i].gn_be->be_entry_open &&
575 gi->gi_n[i].gn_be->be_entry_first) {
576 glueBack = gi->gi_n[i].gn_be;
582 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
583 glueBack->be_entry_open (glueBack, glueMode) != 0)
586 return glueBack->be_entry_first (glueBack);
590 glue_tool_entry_next (
594 slap_overinst *on = glue_tool_inst( b0->bd_info );
595 glueinfo *gi = on->on_bi.bi_private;
599 if (!glueBack || !glueBack->be_entry_next)
602 rc = glueBack->be_entry_next (glueBack);
604 /* If we ran out of entries in one database, move on to the next */
606 if ( glueBack && glueBack->be_entry_close )
607 glueBack->be_entry_close (glueBack);
608 for (i=0; i<gi->gi_nodes; i++) {
609 if (gi->gi_n[i].gn_be == glueBack)
616 glueBack = gi->gi_n[i-1].gn_be;
617 rc = glue_tool_entry_first (b0);
624 glue_tool_entry_get (
629 if (!glueBack || !glueBack->be_entry_get)
632 return glueBack->be_entry_get (glueBack, id);
636 glue_tool_entry_put (
646 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
647 be = glue_back_select (&b2, &e->e_nname);
648 if ( be == &b2 ) be = &toolDB;
650 if (!be->be_entry_put)
654 if ( be->be_entry_open ) {
655 rc = be->be_entry_open (be, glueMode);
660 } else if (be != glueBack) {
661 /* If this entry belongs in a different branch than the
662 * previous one, close the current database and open the
665 if ( glueBack->be_entry_close ) {
666 glueBack->be_entry_close (glueBack);
668 if ( be->be_entry_open ) {
669 rc = be->be_entry_open (be, glueMode);
676 return be->be_entry_put (be, e, text);
680 glue_tool_entry_reindex (
685 if (!glueBack || !glueBack->be_entry_reindex)
688 return glueBack->be_entry_reindex (glueBack, id);
696 slap_overinst *on = glue_tool_inst( b0->bd_info );
697 glueinfo *gi = on->on_bi.bi_private;
698 BackendInfo *bi = b0->bd_info;
701 /* just sync everyone */
702 for (i = 0; i<gi->gi_nodes; i++)
703 if (gi->gi_n[i].gn_be->be_sync)
704 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
705 b0->bd_info = on->on_info->oi_orig;
717 slap_overinst *on = (slap_overinst *)be->bd_info;
718 slap_overinfo *oi = on->on_info;
719 BackendInfo *bi = oi->oi_orig;
722 gi = ch_calloc( 1, sizeof(glueinfo));
723 on->on_bi.bi_private = gi;
724 dnParent( be->be_nsuffix, &gi->gi_pdn );
726 /* Currently the overlay framework doesn't handle these entry points
727 * but we need them....
729 oi->oi_bi.bi_open = glue_open;
730 oi->oi_bi.bi_close = glue_close;
732 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
734 /* Only advertise these if the root DB supports them */
735 if ( bi->bi_tool_entry_open )
736 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
737 if ( bi->bi_tool_entry_close )
738 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
739 if ( bi->bi_tool_entry_first )
740 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
741 if ( bi->bi_tool_entry_next )
742 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
743 if ( bi->bi_tool_entry_get )
744 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
745 if ( bi->bi_tool_entry_put )
746 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
747 if ( bi->bi_tool_entry_reindex )
748 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
749 if ( bi->bi_tool_sync )
750 oi->oi_bi.bi_tool_sync = glue_tool_sync;
752 /*FIXME : need to add support */
753 oi->oi_bi.bi_tool_dn2id_get = 0;
754 oi->oi_bi.bi_tool_id2entry_get = 0;
755 oi->oi_bi.bi_tool_entry_modify = 0;
757 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
767 slap_overinst *on = (slap_overinst *)be->bd_info;
768 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
771 return SLAP_CB_CONTINUE;
779 slap_overinst *on = (slap_overinst *)be->bd_info;
781 on->on_info->oi_bi.bi_db_close = NULL;
786 glue_sub_del( BackendDB *b0 )
791 /* Find the top backend for this subordinate */
793 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
799 if ( SLAP_GLUE_SUBORDINATE( be ))
801 if ( !SLAP_GLUE_INSTANCE( be ))
803 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
806 /* OK, got the right backend, find the overlay */
807 oi = (slap_overinfo *)be->bd_info;
808 for ( on=oi->oi_list; on; on=on->on_next ) {
809 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
812 assert( on != NULL );
813 gi = on->on_bi.bi_private;
814 for ( i=0; i < gi->gi_nodes; i++ ) {
815 if ( gi->gi_n[i].gn_be == b0 ) {
818 for (j=i+1; j < gi->gi_nodes; j++)
819 gi->gi_n[j-1] = gi->gi_n[j];
826 rc = LDAP_NO_SUCH_OBJECT;
831 typedef struct glue_Addrec {
832 struct glue_Addrec *ga_next;
836 /* List of added subordinates */
837 static glue_Addrec *ga_list;
839 /* Attach all the subordinate backends to their superior */
843 glue_Addrec *ga, *gnext = NULL;
846 /* For all the subordinate backends */
847 for ( ga=ga_list; ga != NULL; ga = gnext ) {
852 /* Find the top backend for this subordinate */
854 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
859 if ( SLAP_GLUE_SUBORDINATE( be ))
861 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
864 /* If it's not already configured, set up the overlay */
865 if ( !SLAP_GLUE_INSTANCE( be )) {
866 rc = overlay_config( be, glue.on_bi.bi_type );
870 /* Find the overlay instance */
871 oi = (slap_overinfo *)be->bd_info;
872 for ( on=oi->oi_list; on; on=on->on_next ) {
873 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
876 assert( on != NULL );
877 gi = on->on_bi.bi_private;
878 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
879 gi->gi_nodes * sizeof(gluenode));
880 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
881 dnParent( &ga->ga_be->be_nsuffix[0],
882 &gi->gi_n[gi->gi_nodes].gn_pdn );
884 on->on_bi.bi_private = gi;
888 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
889 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
890 rc = LDAP_NO_SUCH_OBJECT;
902 glue_sub_add( BackendDB *be, int advert, int online )
907 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
909 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
911 ga = ch_malloc( sizeof( glue_Addrec ));
912 ga->ga_next = ga_list;
917 rc = glue_sub_attach();
925 glue.on_bi.bi_type = "glue";
927 glue.on_bi.bi_db_init = glue_db_init;
928 glue.on_bi.bi_db_close = glue_db_close;
929 glue.on_bi.bi_db_destroy = glue_db_destroy;
931 glue.on_bi.bi_op_search = glue_op_search;
932 glue.on_bi.bi_op_modify = glue_op_func;
933 glue.on_bi.bi_op_modrdn = glue_op_func;
934 glue.on_bi.bi_op_add = glue_op_func;
935 glue.on_bi.bi_op_delete = glue_op_func;
937 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
938 glue.on_bi.bi_chk_controls = glue_chk_controls;
940 return overlay_register( &glue );