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 = op_bind;
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;
171 default: assert( 0 ); break;
174 func = &op->o_bd->bd_info->bi_op_bind;
176 rc = func[which]( op, rs );
178 rc = SLAP_CB_CONTINUE;
181 op->o_bd->bd_info = bi0;
186 glue_chk_referrals ( Operation *op, SlapReply *rs )
188 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
189 BackendDB *b0 = op->o_bd;
190 BackendInfo *bi0 = op->o_bd->bd_info;
193 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
194 b0->bd_info = on->on_info->oi_orig;
196 if ( op->o_bd->bd_info->bi_chk_referrals )
197 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
199 rc = SLAP_CB_CONTINUE;
202 op->o_bd->bd_info = bi0;
207 glue_chk_controls ( Operation *op, SlapReply *rs )
209 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
210 BackendDB *b0 = op->o_bd;
211 BackendInfo *bi0 = op->o_bd->bd_info;
212 int rc = SLAP_CB_CONTINUE;
214 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
215 b0->bd_info = on->on_info->oi_orig;
217 /* if the subordinate database has overlays, the bi_chk_controls()
218 * hook is actually over_aux_chk_controls(); in case it actually
219 * wraps a missing hok, we need to mimic the behavior
220 * of the frontend applied to that database */
221 if ( op->o_bd->bd_info->bi_chk_controls ) {
222 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
226 if ( rc == SLAP_CB_CONTINUE ) {
227 rc = backend_check_controls( op, rs );
231 op->o_bd->bd_info = bi0;
236 glue_op_search ( Operation *op, SlapReply *rs )
238 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
239 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
240 BackendDB *b0 = op->o_bd;
241 BackendDB *b1 = NULL, *btmp;
242 BackendInfo *bi0 = op->o_bd->bd_info;
245 glue_state gs = {0, 0, 0, NULL, 0, NULL};
246 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
247 int scope0, slimit0, tlimit0;
248 struct berval dn, ndn, *pdn;
252 cb.sc_next = op->o_callback;
254 stoptime = slap_get_time () + op->ors_tlimit;
256 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
257 b0->bd_info = on->on_info->oi_orig;
259 switch (op->ors_scope) {
260 case LDAP_SCOPE_BASE:
261 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
262 if (op->o_bd && op->o_bd->be_search) {
263 rs->sr_err = op->o_bd->be_search( op, rs );
267 case LDAP_SCOPE_ONELEVEL:
268 case LDAP_SCOPE_SUBTREE:
269 #ifdef LDAP_SCOPE_SUBORDINATE
270 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
275 if (op->o_bd && op->o_bd->be_search) {
276 rs->sr_err = op->o_bd->be_search( op, rs );
278 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
279 "No search target found");
285 op->o_callback = &cb;
286 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
287 scope0 = op->ors_scope;
288 slimit0 = gs.slimit = op->ors_slimit;
289 tlimit0 = op->ors_tlimit;
295 * Execute in reverse order, most general first
297 for (i = gi->gi_nodes; i >= 0; i--) {
298 if ( i == gi->gi_nodes ) {
302 btmp = gi->gi_n[i].gn_be;
303 pdn = &gi->gi_n[i].gn_pdn;
305 if (!btmp || !btmp->be_search)
307 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
309 if (tlimit0 != SLAP_NO_LIMIT) {
310 op->ors_tlimit = stoptime - slap_get_time ();
311 if (op->ors_tlimit <= 0) {
312 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
316 if (slimit0 != SLAP_NO_LIMIT) {
317 op->ors_slimit = slimit0 - rs->sr_nentries;
318 if (op->ors_slimit < 0) {
319 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
332 assert( op->o_bd->be_suffix != NULL );
333 assert( op->o_bd->be_nsuffix != NULL );
335 if (scope0 == LDAP_SCOPE_ONELEVEL &&
338 op->ors_scope = LDAP_SCOPE_BASE;
339 op->o_req_dn = op->o_bd->be_suffix[0];
340 op->o_req_ndn = op->o_bd->be_nsuffix[0];
341 rs->sr_err = op->o_bd->be_search(op, rs);
343 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
344 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
346 rs->sr_err = op->o_bd->be_search( op, rs );
348 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
349 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
351 op->o_req_dn = op->o_bd->be_suffix[0];
352 op->o_req_ndn = op->o_bd->be_nsuffix[0];
353 rs->sr_err = op->o_bd->be_search( op, rs );
354 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
355 gs.err = LDAP_SUCCESS;
358 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
359 rs->sr_err = op->o_bd->be_search( op, rs );
365 * Add errors that should result in dropping
368 case LDAP_SIZELIMIT_EXCEEDED:
369 case LDAP_TIMELIMIT_EXCEEDED:
370 case LDAP_ADMINLIMIT_EXCEEDED:
371 case LDAP_NO_SUCH_OBJECT:
372 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
373 case LDAP_X_CANNOT_CHAIN:
374 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
382 op->ors_scope = scope0;
383 op->ors_slimit = slimit0;
384 op->ors_tlimit = tlimit0;
390 if ( op->o_abandon ) {
391 rs->sr_err = SLAPD_ABANDON;
393 op->o_callback = cb.sc_next;
395 rs->sr_matched = gs.matched;
396 rs->sr_ref = gs.refs;
398 send_ldap_result( op, rs );
402 op->o_bd->bd_info = bi0;
406 ber_bvarray_free(gs.refs);
410 static BackendDB toolDB;
413 glue_tool_entry_open (
418 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
420 /* We don't know which backend to talk to yet, so just
421 * remember the mode and move on...
427 toolDB.bd_info = oi->oi_orig;
433 glue_tool_entry_close (
440 if (!glueBack->be_entry_close)
442 rc = glueBack->be_entry_close (glueBack);
447 static slap_overinst *
452 slap_overinfo *oi = (slap_overinfo *)bi;
455 for ( on = oi->oi_list; on; on=on->on_next ) {
456 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
462 /* This function will only be called in tool mode */
468 slap_overinst *on = glue_tool_inst( bi );
469 glueinfo *gi = on->on_bi.bi_private;
470 static int glueOpened = 0;
471 int i, j, same, bsame = 0, rc = 0;
473 if (glueOpened) return 0;
477 /* If we were invoked in tool mode, open all the underlying backends */
478 if (slapMode & SLAP_TOOL_MODE) {
479 for (i = 0; i<gi->gi_nodes; i++) {
481 /* Same bi_open as our main backend? */
482 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
483 on->on_info->oi_orig->bi_open )
486 /* Loop thru the bd_info's and make sure we only
487 * invoke their bi_open functions once each.
489 for ( j = 0; j<i; j++ ) {
490 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
491 gi->gi_n[j].gn_be->bd_info->bi_open ) {
496 /* OK, it's unique and non-NULL, call it. */
497 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
498 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
499 gi->gi_n[i].gn_be->bd_info );
500 /* Let backend.c take care of the rest of startup */
502 rc = backend_startup_one( gi->gi_n[i].gn_be );
505 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
506 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
508 } /* other case is impossible */
512 /* This function will only be called in tool mode */
518 static int glueClosed = 0;
521 if (glueClosed) return 0;
525 if (slapMode & SLAP_TOOL_MODE) {
526 rc = backend_shutdown( NULL );
532 glue_entry_release_rw (
543 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
544 op->o_bd = glue_back_select (&b2, &e->e_nname);
546 if ( op->o_bd->be_release ) {
547 rc = op->o_bd->be_release( op, e, rw );
550 /* FIXME: mimic be_entry_release_rw
551 * when no be_release() available */
561 glue_tool_entry_first (
565 slap_overinst *on = glue_tool_inst( b0->bd_info );
566 glueinfo *gi = on->on_bi.bi_private;
569 /* If we're starting from scratch, start at the most general */
571 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
574 for (i = gi->gi_nodes-1; i >= 0; i--) {
575 if (gi->gi_n[i].gn_be->be_entry_open &&
576 gi->gi_n[i].gn_be->be_entry_first) {
577 glueBack = gi->gi_n[i].gn_be;
583 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
584 glueBack->be_entry_open (glueBack, glueMode) != 0)
587 return glueBack->be_entry_first (glueBack);
591 glue_tool_entry_next (
595 slap_overinst *on = glue_tool_inst( b0->bd_info );
596 glueinfo *gi = on->on_bi.bi_private;
600 if (!glueBack || !glueBack->be_entry_next)
603 rc = glueBack->be_entry_next (glueBack);
605 /* If we ran out of entries in one database, move on to the next */
607 if ( glueBack && glueBack->be_entry_close )
608 glueBack->be_entry_close (glueBack);
609 for (i=0; i<gi->gi_nodes; i++) {
610 if (gi->gi_n[i].gn_be == glueBack)
617 glueBack = gi->gi_n[i-1].gn_be;
618 rc = glue_tool_entry_first (b0);
625 glue_tool_entry_get (
630 if (!glueBack || !glueBack->be_entry_get)
633 return glueBack->be_entry_get (glueBack, id);
637 glue_tool_entry_put (
647 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
648 be = glue_back_select (&b2, &e->e_nname);
649 if ( be == &b2 ) be = &toolDB;
651 if (!be->be_entry_put)
655 if ( be->be_entry_open ) {
656 rc = be->be_entry_open (be, glueMode);
661 } else if (be != glueBack) {
662 /* If this entry belongs in a different branch than the
663 * previous one, close the current database and open the
666 if ( glueBack->be_entry_close ) {
667 glueBack->be_entry_close (glueBack);
669 if ( be->be_entry_open ) {
670 rc = be->be_entry_open (be, glueMode);
677 return be->be_entry_put (be, e, text);
681 glue_tool_entry_reindex (
686 if (!glueBack || !glueBack->be_entry_reindex)
689 return glueBack->be_entry_reindex (glueBack, id);
697 slap_overinst *on = glue_tool_inst( b0->bd_info );
698 glueinfo *gi = on->on_bi.bi_private;
699 BackendInfo *bi = b0->bd_info;
702 /* just sync everyone */
703 for (i = 0; i<gi->gi_nodes; i++)
704 if (gi->gi_n[i].gn_be->be_sync)
705 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
706 b0->bd_info = on->on_info->oi_orig;
718 slap_overinst *on = (slap_overinst *)be->bd_info;
719 slap_overinfo *oi = on->on_info;
720 BackendInfo *bi = oi->oi_orig;
723 gi = ch_calloc( 1, sizeof(glueinfo));
724 on->on_bi.bi_private = gi;
725 dnParent( be->be_nsuffix, &gi->gi_pdn );
727 /* Currently the overlay framework doesn't handle these entry points
728 * but we need them....
730 oi->oi_bi.bi_open = glue_open;
731 oi->oi_bi.bi_close = glue_close;
733 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
735 /* Only advertise these if the root DB supports them */
736 if ( bi->bi_tool_entry_open )
737 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
738 if ( bi->bi_tool_entry_close )
739 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
740 if ( bi->bi_tool_entry_first )
741 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
742 if ( bi->bi_tool_entry_next )
743 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
744 if ( bi->bi_tool_entry_get )
745 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
746 if ( bi->bi_tool_entry_put )
747 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
748 if ( bi->bi_tool_entry_reindex )
749 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
750 if ( bi->bi_tool_sync )
751 oi->oi_bi.bi_tool_sync = glue_tool_sync;
753 /*FIXME : need to add support */
754 oi->oi_bi.bi_tool_dn2id_get = 0;
755 oi->oi_bi.bi_tool_id2entry_get = 0;
756 oi->oi_bi.bi_tool_entry_modify = 0;
758 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
768 slap_overinst *on = (slap_overinst *)be->bd_info;
769 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
772 return SLAP_CB_CONTINUE;
780 slap_overinst *on = (slap_overinst *)be->bd_info;
782 on->on_info->oi_bi.bi_db_close = NULL;
787 glue_sub_del( BackendDB *b0 )
792 /* Find the top backend for this subordinate */
794 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
800 if ( SLAP_GLUE_SUBORDINATE( be ))
802 if ( !SLAP_GLUE_INSTANCE( be ))
804 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
807 /* OK, got the right backend, find the overlay */
808 oi = (slap_overinfo *)be->bd_info;
809 for ( on=oi->oi_list; on; on=on->on_next ) {
810 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
813 assert( on != NULL );
814 gi = on->on_bi.bi_private;
815 for ( i=0; i < gi->gi_nodes; i++ ) {
816 if ( gi->gi_n[i].gn_be == b0 ) {
819 for (j=i+1; j < gi->gi_nodes; j++)
820 gi->gi_n[j-1] = gi->gi_n[j];
827 rc = LDAP_NO_SUCH_OBJECT;
832 typedef struct glue_Addrec {
833 struct glue_Addrec *ga_next;
837 /* List of added subordinates */
838 static glue_Addrec *ga_list;
840 /* Attach all the subordinate backends to their superior */
844 glue_Addrec *ga, *gnext = NULL;
847 /* For all the subordinate backends */
848 for ( ga=ga_list; ga != NULL; ga = gnext ) {
853 /* Find the top backend for this subordinate */
855 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
860 if ( SLAP_GLUE_SUBORDINATE( be ))
862 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
865 /* If it's not already configured, set up the overlay */
866 if ( !SLAP_GLUE_INSTANCE( be )) {
867 rc = overlay_config( be, glue.on_bi.bi_type );
871 /* Find the overlay instance */
872 oi = (slap_overinfo *)be->bd_info;
873 for ( on=oi->oi_list; on; on=on->on_next ) {
874 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
877 assert( on != NULL );
878 gi = on->on_bi.bi_private;
879 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
880 gi->gi_nodes * sizeof(gluenode));
881 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
882 dnParent( &ga->ga_be->be_nsuffix[0],
883 &gi->gi_n[gi->gi_nodes].gn_pdn );
885 on->on_bi.bi_private = gi;
889 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
890 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
891 rc = LDAP_NO_SUCH_OBJECT;
903 glue_sub_add( BackendDB *be, int advert, int online )
908 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
910 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
912 ga = ch_malloc( sizeof( glue_Addrec ));
913 ga->ga_next = ga_list;
918 rc = glue_sub_attach();
926 glue.on_bi.bi_type = "glue";
928 glue.on_bi.bi_db_init = glue_db_init;
929 glue.on_bi.bi_db_close = glue_db_close;
930 glue.on_bi.bi_db_destroy = glue_db_destroy;
932 glue.on_bi.bi_op_search = glue_op_search;
933 glue.on_bi.bi_op_modify = glue_op_func;
934 glue.on_bi.bi_op_modrdn = glue_op_func;
935 glue.on_bi.bi_op_add = glue_op_func;
936 glue.on_bi.bi_op_delete = glue_op_func;
938 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
939 glue.on_bi.bi_chk_controls = glue_chk_controls;
941 return overlay_register( &glue );