1 /* backglue.c - backend glue */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2010 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>
40 typedef struct gluenode {
45 typedef struct glueinfo {
51 static slap_overinst glue;
54 static BackendDB *glueBack;
55 static BackendDB glueBackDone;
56 #define GLUEBACK_DONE (&glueBackDone)
58 static slap_response glue_op_response;
60 /* Just like select_backend, but only for our backends */
67 slap_overinst *on = (slap_overinst *)be->bd_info;
68 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
71 for (i = gi->gi_nodes-1; i >= 0; i--) {
72 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
74 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
75 return gi->gi_n[i].gn_be;
78 be->bd_info = on->on_info->oi_orig;
83 typedef struct glue_state {
94 glue_op_cleanup( Operation *op, SlapReply *rs )
96 /* This is not a final result */
97 if (rs->sr_type == REP_RESULT )
98 rs->sr_type = REP_GLUE_RESULT;
99 return SLAP_CB_CONTINUE;
103 glue_op_response ( Operation *op, SlapReply *rs )
105 glue_state *gs = op->o_callback->sc_private;
107 switch(rs->sr_type) {
110 case REP_INTERMEDIATE:
111 return SLAP_CB_CONTINUE;
114 if (rs->sr_err == LDAP_SUCCESS ||
115 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
116 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
117 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
118 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
119 gs->err != LDAP_SUCCESS)
120 gs->err = rs->sr_err;
121 if (gs->err == LDAP_SUCCESS && gs->matched) {
122 ch_free (gs->matched);
126 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
128 len = strlen (rs->sr_matched);
129 if (len > gs->matchlen) {
131 ch_free (gs->matched);
132 gs->matched = ch_strdup (rs->sr_matched);
140 for (i=0; rs->sr_ref[i].bv_val; i++);
144 new = ch_malloc ((i+1)*sizeof(struct berval));
146 new = ch_realloc(gs->refs,
147 (j+i+1)*sizeof(struct berval));
149 for (k=0; k<i; j++,k++) {
150 ber_dupbv( &new[j], &rs->sr_ref[k] );
152 new[j].bv_val = NULL;
158 LDAPControl **newctrls;
160 for (i=0; rs->sr_ctrls[i]; i++);
164 newctrls = ch_malloc((i+1)*sizeof(LDAPControl *));
166 /* Forget old pagedResults response if we're sending
169 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
171 for ( k=0; k<i; k++ ) {
172 if ( !strcmp(rs->sr_ctrls[k]->ldctl_oid,
173 LDAP_CONTROL_PAGEDRESULTS )) {
179 for ( k=0; k<j; k++ ) {
180 if ( !strcmp(gs->ctrls[k]->ldctl_oid,
181 LDAP_CONTROL_PAGEDRESULTS )) {
182 gs->ctrls[k]->ldctl_oid = NULL;
183 ldap_control_free( gs->ctrls[k] );
184 gs->ctrls[k] = gs->ctrls[--j];
191 newctrls = ch_realloc(gs->ctrls,
192 (j+i+1)*sizeof(LDAPControl *));
194 for (k=0; k<i; j++,k++) {
195 newctrls[j] = ch_malloc(sizeof(LDAPControl));
196 *newctrls[j] = *rs->sr_ctrls[k];
197 if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ))
198 ber_dupbv( &newctrls[j]->ldctl_value,
199 &rs->sr_ctrls[k]->ldctl_value );
203 gs->ctrls = newctrls;
210 glue_op_func ( 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;
216 slap_operation_t which = op_bind;
219 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
221 /* If we're on the master backend, let overlay framework handle it */
222 if ( op->o_bd == b0 )
223 return SLAP_CB_CONTINUE;
225 b0->bd_info = on->on_info->oi_orig;
228 case LDAP_REQ_ADD: which = op_add; break;
229 case LDAP_REQ_DELETE: which = op_delete; break;
230 case LDAP_REQ_MODIFY: which = op_modify; break;
231 case LDAP_REQ_MODRDN: which = op_modrdn; break;
232 case LDAP_REQ_EXTENDED: which = op_extended; break;
233 default: assert( 0 ); break;
236 func = &op->o_bd->bd_info->bi_op_bind;
238 rc = func[which]( op, rs );
243 op->o_bd->bd_info = bi0;
248 glue_response ( Operation *op, SlapReply *rs )
250 BackendDB *be = op->o_bd;
251 be = glue_back_select (op->o_bd, &op->o_req_ndn);
253 /* If we're on the master backend, let overlay framework handle it.
254 * Otherwise, bail out.
256 return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
260 glue_chk_referrals ( Operation *op, SlapReply *rs )
262 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
263 BackendDB *b0 = op->o_bd;
264 BackendInfo *bi0 = op->o_bd->bd_info;
267 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
268 if ( op->o_bd == b0 )
269 return SLAP_CB_CONTINUE;
271 b0->bd_info = on->on_info->oi_orig;
273 if ( op->o_bd->bd_info->bi_chk_referrals )
274 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
276 rc = SLAP_CB_CONTINUE;
279 op->o_bd->bd_info = bi0;
284 glue_chk_controls ( Operation *op, SlapReply *rs )
286 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
287 BackendDB *b0 = op->o_bd;
288 BackendInfo *bi0 = op->o_bd->bd_info;
289 int rc = SLAP_CB_CONTINUE;
291 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
292 if ( op->o_bd == b0 )
293 return SLAP_CB_CONTINUE;
295 b0->bd_info = on->on_info->oi_orig;
297 /* if the subordinate database has overlays, the bi_chk_controls()
298 * hook is actually over_aux_chk_controls(); in case it actually
299 * wraps a missing hok, we need to mimic the behavior
300 * of the frontend applied to that database */
301 if ( op->o_bd->bd_info->bi_chk_controls ) {
302 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
306 if ( rc == SLAP_CB_CONTINUE ) {
307 rc = backend_check_controls( op, rs );
311 op->o_bd->bd_info = bi0;
315 /* ITS#4615 - overlays configured above the glue overlay should be
316 * invoked for the entire glued tree. Overlays configured below the
317 * glue overlay should only be invoked on the master backend.
318 * So, if we're searching on any subordinates, we need to force the
319 * current overlay chain to stop processing, without stopping the
320 * overall callback flow.
323 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
326 /* Process any overlays on the master backend */
327 if ( op->o_bd == b0 && on->on_next ) {
328 BackendInfo *bi = op->o_bd->bd_info;
329 int rc = SLAP_CB_CONTINUE;
330 for ( on=on->on_next; on; on=on->on_next ) {
331 op->o_bd->bd_info = (BackendInfo *)on;
332 if ( on->on_bi.bi_op_search ) {
333 rc = on->on_bi.bi_op_search( op, rs );
334 if ( rc != SLAP_CB_CONTINUE )
338 op->o_bd->bd_info = bi;
339 if ( rc != SLAP_CB_CONTINUE )
342 return op->o_bd->be_search( op, rs );
346 glue_op_search ( Operation *op, SlapReply *rs )
348 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
349 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
350 BackendDB *b0 = op->o_bd;
351 BackendDB *b1 = NULL, *btmp;
352 BackendInfo *bi0 = op->o_bd->bd_info;
354 long stoptime = 0, starttime;
355 glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
356 slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
358 struct berval dn, ndn, *pdn;
362 cb.sc_next = op->o_callback;
364 starttime = op->o_time;
365 stoptime = slap_get_time () + op->ors_tlimit;
367 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
368 b0->bd_info = on->on_info->oi_orig;
370 switch (op->ors_scope) {
371 case LDAP_SCOPE_BASE:
372 if ( op->o_bd == b0 )
373 return SLAP_CB_CONTINUE;
375 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
376 if (op->o_bd && op->o_bd->be_search) {
377 rs->sr_err = op->o_bd->be_search( op, rs );
381 case LDAP_SCOPE_ONELEVEL:
382 case LDAP_SCOPE_SUBTREE:
383 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
384 op->o_callback = &cb;
385 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
386 scope0 = op->ors_scope;
387 tlimit0 = op->ors_tlimit;
393 * Execute in reverse order, most specific first
395 for (i = gi->gi_nodes; i >= 0; i--) {
396 if ( i == gi->gi_nodes ) {
400 btmp = gi->gi_n[i].gn_be;
401 pdn = &gi->gi_n[i].gn_pdn;
403 if (!btmp || !btmp->be_search)
405 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
407 if (get_no_subordinate_glue(op) && btmp != b1)
409 /* If we remembered which backend we were on before,
410 * skip down to it now
412 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
413 op->o_conn->c_pagedresults_state.ps_be &&
414 op->o_conn->c_pagedresults_state.ps_be != btmp )
417 if (tlimit0 != SLAP_NO_LIMIT) {
418 op->o_time = slap_get_time();
419 op->ors_tlimit = stoptime - op->o_time;
420 if (op->ors_tlimit <= 0) {
421 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
434 assert( op->o_bd->be_suffix != NULL );
435 assert( op->o_bd->be_nsuffix != NULL );
437 if (scope0 == LDAP_SCOPE_ONELEVEL &&
440 struct berval mdn, mndn;
441 op->ors_scope = LDAP_SCOPE_BASE;
442 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
443 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
444 rs->sr_err = op->o_bd->be_search(op, rs);
445 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
446 gs.err = LDAP_SUCCESS;
448 op->ors_scope = LDAP_SCOPE_ONELEVEL;
449 if ( op->o_req_dn.bv_val == mdn.bv_val )
451 if ( op->o_req_ndn.bv_val == mndn.bv_val )
454 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
455 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
457 rs->sr_err = glue_sub_search( op, rs, b0, on );
459 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
460 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
462 struct berval mdn, mndn;
463 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
464 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
465 rs->sr_err = glue_sub_search( op, rs, b0, on );
466 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
467 gs.err = LDAP_SUCCESS;
469 if ( op->o_req_dn.bv_val == mdn.bv_val )
471 if ( op->o_req_ndn.bv_val == mndn.bv_val )
474 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
475 rs->sr_err = glue_sub_search( op, rs, b0, on );
481 * Add errors that should result in dropping
484 case LDAP_SIZELIMIT_EXCEEDED:
485 case LDAP_TIMELIMIT_EXCEEDED:
486 case LDAP_ADMINLIMIT_EXCEEDED:
487 case LDAP_NO_SUCH_OBJECT:
488 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
489 case LDAP_X_CANNOT_CHAIN:
490 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
494 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
495 PagedResultsState *ps = op->o_pagedresults_state;
497 /* Assume this backend can be forgotten now */
498 op->o_conn->c_pagedresults_state.ps_be = NULL;
500 /* If we have a full page, exit the loop. We may
501 * need to remember this backend so we can continue
502 * from here on a subsequent request.
504 if ( rs->sr_nentries >= ps->ps_size ) {
505 /* Don't bother to remember the first backend.
506 * Only remember the last one if there's more state left.
508 if ( op->o_bd != b0 &&
509 ( op->o_conn->c_pagedresults_state.ps_cookie ||
510 op->o_bd != gi->gi_n[0].gn_be ))
511 op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
515 /* This backend has run out of entries, but more responses
516 * can fit in the page. Fake a reset of the state so the
517 * next backend will start up properly. Only back-[bh]db
518 * and back-sql look at this state info.
520 if ( ps->ps_cookieval.bv_len == sizeof( PagedResultsCookie )) {
522 memset( ps->ps_cookieval.bv_val, 0,
523 sizeof( PagedResultsCookie ));
532 op->ors_scope = scope0;
533 op->ors_tlimit = tlimit0;
534 op->o_time = starttime;
539 op->o_callback = cb.sc_next;
540 if ( op->o_abandon ) {
541 rs->sr_err = SLAPD_ABANDON;
544 rs->sr_matched = gs.matched;
545 rs->sr_ref = gs.refs;
547 rs->sr_ctrls = gs.ctrls;
549 send_ldap_result( op, rs );
552 op->o_bd->bd_info = bi0;
556 ber_bvarray_free(gs.refs);
558 for (i = gs.nctrls; --i >= 0; ) {
559 if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
560 free(gs.ctrls[i]->ldctl_value.bv_val);
568 static BackendDB toolDB;
571 glue_tool_entry_open (
576 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
578 /* We don't know which backend to talk to yet, so just
579 * remember the mode and move on...
585 toolDB.bd_info = oi->oi_orig;
591 glue_tool_entry_close (
597 if (glueBack && glueBack != GLUEBACK_DONE) {
598 if (!glueBack->be_entry_close)
600 rc = glueBack->be_entry_close (glueBack);
605 static slap_overinst *
610 slap_overinfo *oi = (slap_overinfo *)bi;
613 for ( on = oi->oi_list; on; on=on->on_next ) {
614 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
620 /* This function will only be called in tool mode */
626 slap_overinst *on = glue_tool_inst( bi );
627 glueinfo *gi = on->on_bi.bi_private;
628 static int glueOpened = 0;
629 int i, j, same, bsame = 0, rc = 0;
630 ConfigReply cr = {0};
632 if (glueOpened) return 0;
636 /* If we were invoked in tool mode, open all the underlying backends */
637 if (slapMode & SLAP_TOOL_MODE) {
638 for (i = 0; i<gi->gi_nodes; i++) {
640 /* Same bi_open as our main backend? */
641 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
642 on->on_info->oi_orig->bi_open )
645 /* Loop thru the bd_info's and make sure we only
646 * invoke their bi_open functions once each.
648 for ( j = 0; j<i; j++ ) {
649 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
650 gi->gi_n[j].gn_be->bd_info->bi_open ) {
655 /* OK, it's unique and non-NULL, call it. */
656 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
657 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
658 gi->gi_n[i].gn_be->bd_info );
659 /* Let backend.c take care of the rest of startup */
661 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
664 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
665 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
667 } /* other case is impossible */
671 /* This function will only be called in tool mode */
677 static int glueClosed = 0;
680 if (glueClosed) return 0;
684 if (slapMode & SLAP_TOOL_MODE) {
685 rc = backend_shutdown( NULL );
695 AttributeDescription *ad,
700 BackendDB *b0 = op->o_bd;
701 op->o_bd = glue_back_select( b0, dn );
703 if ( op->o_bd->be_fetch ) {
704 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
706 rc = LDAP_UNWILLING_TO_PERFORM;
713 glue_entry_release_rw (
719 BackendDB *b0 = op->o_bd;
722 op->o_bd = glue_back_select (b0, &e->e_nname);
724 if ( op->o_bd->be_release ) {
725 rc = op->o_bd->be_release( op, e, rw );
728 /* FIXME: mimic be_entry_release_rw
729 * when no be_release() available */
738 static struct berval *glue_base;
739 static int glue_scope;
740 static Filter *glue_filter;
743 glue_tool_entry_first (
747 slap_overinst *on = glue_tool_inst( b0->bd_info );
748 glueinfo *gi = on->on_bi.bi_private;
752 /* If we're starting from scratch, start at the most general */
754 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
757 for (i = gi->gi_nodes-1; i >= 0; i--) {
758 if (gi->gi_n[i].gn_be->be_entry_open &&
759 gi->gi_n[i].gn_be->be_entry_first) {
760 glueBack = gi->gi_n[i].gn_be;
766 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
767 glueBack->be_entry_open (glueBack, glueMode) != 0)
770 rc = glueBack->be_entry_first (glueBack);
771 while ( rc == NOID ) {
772 if ( glueBack && glueBack->be_entry_close )
773 glueBack->be_entry_close (glueBack);
774 for (i=0; i<gi->gi_nodes; i++) {
775 if (gi->gi_n[i].gn_be == glueBack)
779 glueBack = GLUEBACK_DONE;
782 glueBack = gi->gi_n[i-1].gn_be;
783 rc = glue_tool_entry_first (b0);
784 if ( glueBack == GLUEBACK_DONE ) {
793 glue_tool_entry_first_x (
800 slap_overinst *on = glue_tool_inst( b0->bd_info );
801 glueinfo *gi = on->on_bi.bi_private;
809 /* If we're starting from scratch, start at the most general */
811 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
814 for (i = gi->gi_nodes-1; i >= 0; i--) {
815 if (gi->gi_n[i].gn_be->be_entry_open &&
816 gi->gi_n[i].gn_be->be_entry_first_x)
818 glueBack = gi->gi_n[i].gn_be;
824 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
825 glueBack->be_entry_open (glueBack, glueMode) != 0)
828 rc = glueBack->be_entry_first_x (glueBack,
829 glue_base, glue_scope, glue_filter);
830 while ( rc == NOID ) {
831 if ( glueBack && glueBack->be_entry_close )
832 glueBack->be_entry_close (glueBack);
833 for (i=0; i<gi->gi_nodes; i++) {
834 if (gi->gi_n[i].gn_be == glueBack)
838 glueBack = GLUEBACK_DONE;
841 glueBack = gi->gi_n[i-1].gn_be;
842 rc = glue_tool_entry_first_x (b0,
843 glue_base, glue_scope, glue_filter);
844 if ( glueBack == GLUEBACK_DONE ) {
853 glue_tool_entry_next (
857 slap_overinst *on = glue_tool_inst( b0->bd_info );
858 glueinfo *gi = on->on_bi.bi_private;
862 if (!glueBack || !glueBack->be_entry_next)
865 rc = glueBack->be_entry_next (glueBack);
867 /* If we ran out of entries in one database, move on to the next */
869 if ( glueBack && glueBack->be_entry_close )
870 glueBack->be_entry_close (glueBack);
871 for (i=0; i<gi->gi_nodes; i++) {
872 if (gi->gi_n[i].gn_be == glueBack)
876 glueBack = GLUEBACK_DONE;
879 glueBack = gi->gi_n[i-1].gn_be;
880 if ( glue_base || glue_filter ) {
881 /* using entry_first_x() */
882 rc = glue_tool_entry_first_x (b0,
883 glue_base, glue_scope, glue_filter);
886 /* using entry_first() */
887 rc = glue_tool_entry_first (b0);
889 if ( glueBack == GLUEBACK_DONE ) {
898 glue_tool_dn2id_get (
907 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
908 be = glue_back_select (&b2, dn);
909 if ( be == &b2 ) be = &toolDB;
911 if (!be->be_dn2id_get)
915 if ( be->be_entry_open ) {
916 rc = be->be_entry_open (be, glueMode);
921 } else if (be != glueBack) {
922 /* If this entry belongs in a different branch than the
923 * previous one, close the current database and open the
926 if ( glueBack->be_entry_close ) {
927 glueBack->be_entry_close (glueBack);
929 if ( be->be_entry_open ) {
930 rc = be->be_entry_open (be, glueMode);
937 return be->be_dn2id_get (be, dn);
941 glue_tool_entry_get (
946 if (!glueBack || !glueBack->be_entry_get)
949 return glueBack->be_entry_get (glueBack, id);
953 glue_tool_entry_put (
963 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
964 be = glue_back_select (&b2, &e->e_nname);
965 if ( be == &b2 ) be = &toolDB;
967 if (!be->be_entry_put)
971 if ( be->be_entry_open ) {
972 rc = be->be_entry_open (be, glueMode);
977 } else if (be != glueBack) {
978 /* If this entry belongs in a different branch than the
979 * previous one, close the current database and open the
982 if ( glueBack->be_entry_close ) {
983 glueBack->be_entry_close (glueBack);
985 if ( be->be_entry_open ) {
986 rc = be->be_entry_open (be, glueMode);
993 return be->be_entry_put (be, e, text);
997 glue_tool_entry_modify (
1003 if (!glueBack || !glueBack->be_entry_modify)
1006 return glueBack->be_entry_modify (glueBack, e, text);
1010 glue_tool_entry_reindex (
1013 AttributeDescription **adv
1016 if (!glueBack || !glueBack->be_entry_reindex)
1019 return glueBack->be_entry_reindex (glueBack, id, adv);
1027 slap_overinst *on = glue_tool_inst( b0->bd_info );
1028 glueinfo *gi = on->on_bi.bi_private;
1029 BackendInfo *bi = b0->bd_info;
1032 /* just sync everyone */
1033 for (i = 0; i<gi->gi_nodes; i++)
1034 if (gi->gi_n[i].gn_be->be_sync)
1035 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1036 b0->bd_info = on->on_info->oi_orig;
1043 typedef struct glue_Addrec {
1044 struct glue_Addrec *ga_next;
1048 /* List of added subordinates */
1049 static glue_Addrec *ga_list;
1050 static int ga_adding;
1058 slap_overinst *on = (slap_overinst *)be->bd_info;
1059 slap_overinfo *oi = on->on_info;
1060 BackendInfo *bi = oi->oi_orig;
1063 if ( SLAP_GLUE_SUBORDINATE( be )) {
1064 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1065 "cannot have glue overlay!\n",
1066 be->be_suffix[0].bv_val, 0, 0 );
1070 gi = ch_calloc( 1, sizeof(glueinfo));
1071 on->on_bi.bi_private = gi;
1072 dnParent( be->be_nsuffix, &gi->gi_pdn );
1074 /* Currently the overlay framework doesn't handle these entry points
1075 * but we need them....
1077 oi->oi_bi.bi_open = glue_open;
1078 oi->oi_bi.bi_close = glue_close;
1080 /* Only advertise these if the root DB supports them */
1081 if ( bi->bi_tool_entry_open )
1082 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1083 if ( bi->bi_tool_entry_close )
1084 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1085 if ( bi->bi_tool_entry_first )
1086 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1087 /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1088 if ( bi->bi_tool_entry_first_x )
1089 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1090 if ( bi->bi_tool_entry_next )
1091 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1092 if ( bi->bi_tool_entry_get )
1093 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1094 if ( bi->bi_tool_dn2id_get )
1095 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1096 if ( bi->bi_tool_entry_put )
1097 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1098 if ( bi->bi_tool_entry_reindex )
1099 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1100 if ( bi->bi_tool_entry_modify )
1101 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1102 if ( bi->bi_tool_sync )
1103 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1105 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1108 be->bd_info = (BackendInfo *)oi;
1109 glue_sub_attach( 1 );
1121 slap_overinst *on = (slap_overinst *)be->bd_info;
1122 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
1125 return SLAP_CB_CONTINUE;
1134 slap_overinst *on = (slap_overinst *)be->bd_info;
1136 on->on_info->oi_bi.bi_db_close = 0;
1141 glue_sub_del( BackendDB *b0 )
1146 /* Find the top backend for this subordinate */
1148 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1154 if ( SLAP_GLUE_SUBORDINATE( be ))
1156 if ( !SLAP_GLUE_INSTANCE( be ))
1158 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1161 /* OK, got the right backend, find the overlay */
1162 oi = (slap_overinfo *)be->bd_info;
1163 for ( on=oi->oi_list; on; on=on->on_next ) {
1164 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1167 assert( on != NULL );
1168 gi = on->on_bi.bi_private;
1169 for ( i=0; i < gi->gi_nodes; i++ ) {
1170 if ( gi->gi_n[i].gn_be == b0 ) {
1173 for (j=i+1; j < gi->gi_nodes; j++)
1174 gi->gi_n[j-1] = gi->gi_n[j];
1181 rc = LDAP_NO_SUCH_OBJECT;
1187 /* Attach all the subordinate backends to their superior */
1189 glue_sub_attach( int online )
1191 glue_Addrec *ga, *gnext = NULL;
1199 /* For all the subordinate backends */
1200 for ( ga=ga_list; ga != NULL; ga = gnext ) {
1203 gnext = ga->ga_next;
1205 /* Find the top backend for this subordinate */
1207 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1212 if ( SLAP_GLUE_SUBORDINATE( be ))
1214 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1217 /* If it's not already configured, set up the overlay */
1218 if ( !SLAP_GLUE_INSTANCE( be )) {
1219 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1223 /* Find the overlay instance */
1224 oi = (slap_overinfo *)be->bd_info;
1225 for ( on=oi->oi_list; on; on=on->on_next ) {
1226 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1229 assert( on != NULL );
1230 gi = on->on_bi.bi_private;
1231 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1232 gi->gi_nodes * sizeof(gluenode));
1233 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1234 dnParent( &ga->ga_be->be_nsuffix[0],
1235 &gi->gi_n[gi->gi_nodes].gn_pdn );
1237 on->on_bi.bi_private = gi;
1238 ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1242 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1243 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1244 /* allow this for now, assume a superior will
1252 rc = LDAP_NO_SUCH_OBJECT;
1266 glue_sub_add( BackendDB *be, int advert, int online )
1271 if ( overlay_is_inst( be, "glue" )) {
1272 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1273 "cannot be a subordinate!\n",
1274 be->be_suffix[0].bv_val, 0, 0 );
1277 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1279 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1281 ga = ch_malloc( sizeof( glue_Addrec ));
1282 ga->ga_next = ga_list;
1287 rc = glue_sub_attach( online );
1295 glue.on_bi.bi_type = "glue";
1297 glue.on_bi.bi_db_init = glue_db_init;
1298 glue.on_bi.bi_db_close = glue_db_close;
1299 glue.on_bi.bi_db_destroy = glue_db_destroy;
1301 glue.on_bi.bi_op_search = glue_op_search;
1302 glue.on_bi.bi_op_modify = glue_op_func;
1303 glue.on_bi.bi_op_modrdn = glue_op_func;
1304 glue.on_bi.bi_op_add = glue_op_func;
1305 glue.on_bi.bi_op_delete = glue_op_func;
1306 glue.on_bi.bi_extended = glue_op_func;
1308 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1309 glue.on_bi.bi_chk_controls = glue_chk_controls;
1310 glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1311 glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1313 glue.on_response = glue_response;
1315 return overlay_register( &glue );