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>
41 typedef struct gluenode {
46 typedef struct glueinfo {
52 static slap_overinst glue;
55 static BackendDB *glueBack;
56 static BackendDB glueBackDone;
57 #define GLUEBACK_DONE (&glueBackDone)
59 static slap_overinst * glue_tool_inst( BackendInfo *bi);
61 static slap_response glue_op_response;
63 /* Just like select_backend, but only for our backends */
70 slap_overinst *on = (slap_overinst *)be->bd_info;
71 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
74 for (i = gi->gi_nodes-1; i >= 0; i--) {
75 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
77 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
78 return gi->gi_n[i].gn_be;
81 be->bd_info = on->on_info->oi_orig;
86 typedef struct glue_state {
97 glue_op_cleanup( Operation *op, SlapReply *rs )
99 /* This is not a final result */
100 if (rs->sr_type == REP_RESULT )
101 rs->sr_type = REP_GLUE_RESULT;
102 return SLAP_CB_CONTINUE;
106 glue_op_response ( Operation *op, SlapReply *rs )
108 glue_state *gs = op->o_callback->sc_private;
110 switch(rs->sr_type) {
113 case REP_INTERMEDIATE:
114 return SLAP_CB_CONTINUE;
117 if (rs->sr_err == LDAP_SUCCESS ||
118 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
119 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
120 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
121 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
122 gs->err != LDAP_SUCCESS)
123 gs->err = rs->sr_err;
124 if (gs->err == LDAP_SUCCESS && gs->matched) {
125 ch_free (gs->matched);
129 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
131 len = strlen (rs->sr_matched);
132 if (len > gs->matchlen) {
134 ch_free (gs->matched);
135 gs->matched = ch_strdup (rs->sr_matched);
143 for (i=0; rs->sr_ref[i].bv_val; i++);
147 new = ch_malloc ((i+1)*sizeof(struct berval));
149 new = ch_realloc(gs->refs,
150 (j+i+1)*sizeof(struct berval));
152 for (k=0; k<i; j++,k++) {
153 ber_dupbv( &new[j], &rs->sr_ref[k] );
155 new[j].bv_val = NULL;
161 LDAPControl **newctrls;
163 for (i=0; rs->sr_ctrls[i]; i++);
167 newctrls = op->o_tmpalloc((i+1)*sizeof(LDAPControl *),
170 /* Forget old pagedResults response if we're sending
173 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
175 for ( k=0; k<i; k++ ) {
176 if ( !strcmp(rs->sr_ctrls[k]->ldctl_oid,
177 LDAP_CONTROL_PAGEDRESULTS )) {
183 for ( k=0; k<j; k++ ) {
184 if ( !strcmp(gs->ctrls[k]->ldctl_oid,
185 LDAP_CONTROL_PAGEDRESULTS ))
187 op->o_tmpfree(gs->ctrls[k], op->o_tmpmemctx);
188 gs->ctrls[k] = gs->ctrls[--j];
195 newctrls = op->o_tmprealloc(gs->ctrls,
196 (j+i+1)*sizeof(LDAPControl *), op->o_tmpmemctx);
198 for (k=0; k<i; j++,k++) {
199 ber_len_t oidlen = strlen( rs->sr_ctrls[k]->ldctl_oid );
200 newctrls[j] = op->o_tmpalloc(sizeof(LDAPControl) + oidlen + 1 + rs->sr_ctrls[k]->ldctl_value.bv_len + 1,
202 newctrls[j]->ldctl_iscritical = rs->sr_ctrls[k]->ldctl_iscritical;
203 newctrls[j]->ldctl_oid = (char *)&newctrls[j][1];
204 lutil_strcopy( newctrls[j]->ldctl_oid, rs->sr_ctrls[k]->ldctl_oid );
205 if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ) ) {
206 newctrls[j]->ldctl_value.bv_val = &newctrls[j]->ldctl_oid[oidlen + 1];
207 newctrls[j]->ldctl_value.bv_len = rs->sr_ctrls[k]->ldctl_value.bv_len;
208 lutil_memcopy( newctrls[j]->ldctl_value.bv_val,
209 rs->sr_ctrls[k]->ldctl_value.bv_val,
210 rs->sr_ctrls[k]->ldctl_value.bv_len + 1 );
212 BER_BVZERO( &newctrls[j]->ldctl_value );
217 gs->ctrls = newctrls;
224 glue_op_func ( Operation *op, SlapReply *rs )
226 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
227 BackendDB *b0 = op->o_bd;
228 BackendInfo *bi0 = op->o_bd->bd_info;
230 slap_operation_t which = op_bind;
233 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
235 /* If we're on the master backend, let overlay framework handle it */
236 if ( op->o_bd == b0 )
237 return SLAP_CB_CONTINUE;
239 b0->bd_info = on->on_info->oi_orig;
242 case LDAP_REQ_ADD: which = op_add; break;
243 case LDAP_REQ_DELETE: which = op_delete; break;
244 case LDAP_REQ_MODIFY: which = op_modify; break;
245 case LDAP_REQ_MODRDN: which = op_modrdn; break;
246 case LDAP_REQ_EXTENDED: which = op_extended; break;
247 default: assert( 0 ); break;
250 func = &op->o_bd->bd_info->bi_op_bind;
252 rc = func[which]( op, rs );
257 op->o_bd->bd_info = bi0;
262 glue_response ( Operation *op, SlapReply *rs )
264 BackendDB *be = op->o_bd;
265 be = glue_back_select (op->o_bd, &op->o_req_ndn);
267 /* If we're on the master backend, let overlay framework handle it.
268 * Otherwise, bail out.
270 return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
274 glue_chk_referrals ( Operation *op, SlapReply *rs )
276 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
277 BackendDB *b0 = op->o_bd;
278 BackendInfo *bi0 = op->o_bd->bd_info;
281 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
282 if ( op->o_bd == b0 )
283 return SLAP_CB_CONTINUE;
285 b0->bd_info = on->on_info->oi_orig;
287 if ( op->o_bd->bd_info->bi_chk_referrals )
288 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
290 rc = SLAP_CB_CONTINUE;
293 op->o_bd->bd_info = bi0;
298 glue_chk_controls ( Operation *op, SlapReply *rs )
300 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
301 BackendDB *b0 = op->o_bd;
302 BackendInfo *bi0 = op->o_bd->bd_info;
303 int rc = SLAP_CB_CONTINUE;
305 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
306 if ( op->o_bd == b0 )
307 return SLAP_CB_CONTINUE;
309 b0->bd_info = on->on_info->oi_orig;
311 /* if the subordinate database has overlays, the bi_chk_controls()
312 * hook is actually over_aux_chk_controls(); in case it actually
313 * wraps a missing hok, we need to mimic the behavior
314 * of the frontend applied to that database */
315 if ( op->o_bd->bd_info->bi_chk_controls ) {
316 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
320 if ( rc == SLAP_CB_CONTINUE ) {
321 rc = backend_check_controls( op, rs );
325 op->o_bd->bd_info = bi0;
329 /* ITS#4615 - overlays configured above the glue overlay should be
330 * invoked for the entire glued tree. Overlays configured below the
331 * glue overlay should only be invoked on the master backend.
332 * So, if we're searching on any subordinates, we need to force the
333 * current overlay chain to stop processing, without stopping the
334 * overall callback flow.
337 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
340 /* Process any overlays on the master backend */
341 if ( op->o_bd == b0 && on->on_next ) {
342 BackendInfo *bi = op->o_bd->bd_info;
343 int rc = SLAP_CB_CONTINUE;
344 for ( on=on->on_next; on; on=on->on_next ) {
345 op->o_bd->bd_info = (BackendInfo *)on;
346 if ( on->on_bi.bi_op_search ) {
347 rc = on->on_bi.bi_op_search( op, rs );
348 if ( rc != SLAP_CB_CONTINUE )
352 op->o_bd->bd_info = bi;
353 if ( rc != SLAP_CB_CONTINUE )
356 return op->o_bd->be_search( op, rs );
360 glue_op_search ( Operation *op, SlapReply *rs )
362 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
363 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
364 BackendDB *b0 = op->o_bd;
365 BackendDB *b1 = NULL, *btmp;
366 BackendInfo *bi0 = op->o_bd->bd_info;
368 long stoptime = 0, starttime;
369 glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
370 slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
372 struct berval dn, ndn, *pdn;
376 cb.sc_next = op->o_callback;
378 starttime = op->o_time;
379 stoptime = slap_get_time () + op->ors_tlimit;
381 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
382 b0->bd_info = on->on_info->oi_orig;
384 switch (op->ors_scope) {
385 case LDAP_SCOPE_BASE:
386 if ( op->o_bd == b0 )
387 return SLAP_CB_CONTINUE;
389 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
390 if (op->o_bd && op->o_bd->be_search) {
391 rs->sr_err = op->o_bd->be_search( op, rs );
395 case LDAP_SCOPE_ONELEVEL:
396 case LDAP_SCOPE_SUBTREE:
397 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
398 op->o_callback = &cb;
399 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
400 scope0 = op->ors_scope;
401 tlimit0 = op->ors_tlimit;
407 * Execute in reverse order, most specific first
409 for (i = gi->gi_nodes; i >= 0; i--) {
410 if ( i == gi->gi_nodes ) {
414 btmp = gi->gi_n[i].gn_be;
415 pdn = &gi->gi_n[i].gn_pdn;
417 if (!btmp || !btmp->be_search)
419 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
421 if (get_no_subordinate_glue(op) && btmp != b1)
423 /* If we remembered which backend we were on before,
424 * skip down to it now
426 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
427 op->o_conn->c_pagedresults_state.ps_be &&
428 op->o_conn->c_pagedresults_state.ps_be != btmp )
431 if (tlimit0 != SLAP_NO_LIMIT) {
432 op->o_time = slap_get_time();
433 op->ors_tlimit = stoptime - op->o_time;
434 if (op->ors_tlimit <= 0) {
435 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
448 assert( op->o_bd->be_suffix != NULL );
449 assert( op->o_bd->be_nsuffix != NULL );
451 if (scope0 == LDAP_SCOPE_ONELEVEL &&
454 struct berval mdn, mndn;
455 op->ors_scope = LDAP_SCOPE_BASE;
456 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
457 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
458 rs->sr_err = op->o_bd->be_search(op, rs);
459 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
460 gs.err = LDAP_SUCCESS;
462 op->ors_scope = LDAP_SCOPE_ONELEVEL;
463 if ( op->o_req_dn.bv_val == mdn.bv_val )
465 if ( op->o_req_ndn.bv_val == mndn.bv_val )
468 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
469 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
471 rs->sr_err = glue_sub_search( op, rs, b0, on );
473 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
474 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
476 struct berval mdn, mndn;
477 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
478 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
479 rs->sr_err = glue_sub_search( op, rs, b0, on );
480 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
481 gs.err = LDAP_SUCCESS;
483 if ( op->o_req_dn.bv_val == mdn.bv_val )
485 if ( op->o_req_ndn.bv_val == mndn.bv_val )
488 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
489 rs->sr_err = glue_sub_search( op, rs, b0, on );
495 * Add errors that should result in dropping
498 case LDAP_SIZELIMIT_EXCEEDED:
499 case LDAP_TIMELIMIT_EXCEEDED:
500 case LDAP_ADMINLIMIT_EXCEEDED:
501 case LDAP_NO_SUCH_OBJECT:
502 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
503 case LDAP_X_CANNOT_CHAIN:
504 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
508 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
509 PagedResultsState *ps = op->o_pagedresults_state;
511 /* Assume this backend can be forgotten now */
512 op->o_conn->c_pagedresults_state.ps_be = NULL;
514 /* If we have a full page, exit the loop. We may
515 * need to remember this backend so we can continue
516 * from here on a subsequent request.
518 if ( rs->sr_nentries >= ps->ps_size ) {
519 /* Don't bother to remember the first backend.
520 * Only remember the last one if there's more state left.
522 if ( op->o_bd != b0 &&
523 ( op->o_conn->c_pagedresults_state.ps_cookie ||
524 op->o_bd != gi->gi_n[0].gn_be ))
525 op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
529 /* This backend has run out of entries, but more responses
530 * can fit in the page. Fake a reset of the state so the
531 * next backend will start up properly. Only back-[bh]db
532 * and back-sql look at this state info.
534 if ( ps->ps_cookieval.bv_len == sizeof( PagedResultsCookie )) {
536 memset( ps->ps_cookieval.bv_val, 0,
537 sizeof( PagedResultsCookie ));
542 /* change the size of the page in the request
543 * that will be propagated */
544 BerElementBuffer berbuf;
545 BerElement *ber = (BerElement *)&berbuf;
546 int size = ps->ps_size - rs->sr_nentries;
547 struct berval cookie = BER_BVC(""), value;
550 for (c = 0; op->o_ctrls[c] != NULL; c++) {
551 if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
555 ber_init2( ber, NULL, LBER_USE_DER );
556 ber_printf( ber, "{iO}", size, &cookie );
557 ber_flatten2( ber, &value, 0 );
558 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
559 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
560 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
561 value.bv_val, value.bv_len );
571 op->ors_scope = scope0;
572 op->ors_tlimit = tlimit0;
573 op->o_time = starttime;
578 op->o_callback = cb.sc_next;
579 if ( op->o_abandon ) {
580 rs->sr_err = SLAPD_ABANDON;
583 rs->sr_matched = gs.matched;
584 rs->sr_ref = gs.refs;
586 rs->sr_ctrls = gs.ctrls;
588 send_ldap_result( op, rs );
591 op->o_bd->bd_info = bi0;
595 ber_bvarray_free(gs.refs);
597 for (i = gs.nctrls; --i >= 0; ) {
598 op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
600 op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
605 static BackendDB toolDB;
608 glue_tool_entry_open (
613 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
615 /* We don't know which backend to talk to yet, so just
616 * remember the mode and move on...
622 toolDB.bd_info = oi->oi_orig;
626 slap_overinst *on = glue_tool_inst( b0->bd_info );
627 glueinfo *gi = on->on_bi.bi_private;
630 for (i = 0; i < gi->gi_nodes; i++) {
634 dnParent( &gi->gi_n[i].gn_be->be_nsuffix[0], &pdn );
635 bd = select_backend( &pdn, 0 );
640 if ( overlay_is_over( bd ) ) {
641 slap_overinfo *oi = (slap_overinfo *)bd->bd_info;
643 db.bd_info = oi->oi_orig;
647 if ( !bd->bd_info->bi_tool_dn2id_get
648 || !bd->bd_info->bi_tool_entry_open
649 || !bd->bd_info->bi_tool_entry_close )
654 bd->bd_info->bi_tool_entry_open( bd, 0 );
655 id = bd->bd_info->bi_tool_dn2id_get( bd, &gi->gi_n[i].gn_be->be_nsuffix[0] );
656 bd->bd_info->bi_tool_entry_close( bd );
658 Debug( LDAP_DEBUG_ANY,
659 "glue_tool_entry_open: subordinate database suffix entry DN=\"%s\" also present in superior database rooted at DN=\"%s\"\n",
660 gi->gi_n[i].gn_be->be_suffix[0].bv_val, bd->be_suffix[0].bv_val, 0 );
671 glue_tool_entry_close (
677 if (glueBack && glueBack != GLUEBACK_DONE) {
678 if (!glueBack->be_entry_close)
680 rc = glueBack->be_entry_close (glueBack);
685 static slap_overinst *
690 slap_overinfo *oi = (slap_overinfo *)bi;
693 for ( on = oi->oi_list; on; on=on->on_next ) {
694 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
700 /* This function will only be called in tool mode */
706 slap_overinst *on = glue_tool_inst( bi );
707 glueinfo *gi = on->on_bi.bi_private;
708 static int glueOpened = 0;
709 int i, j, same, bsame = 0, rc = 0;
710 ConfigReply cr = {0};
712 if (glueOpened) return 0;
716 /* If we were invoked in tool mode, open all the underlying backends */
717 if (slapMode & SLAP_TOOL_MODE) {
718 for (i = 0; i<gi->gi_nodes; i++) {
720 /* Same bi_open as our main backend? */
721 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
722 on->on_info->oi_orig->bi_open )
725 /* Loop thru the bd_info's and make sure we only
726 * invoke their bi_open functions once each.
728 for ( j = 0; j<i; j++ ) {
729 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
730 gi->gi_n[j].gn_be->bd_info->bi_open ) {
735 /* OK, it's unique and non-NULL, call it. */
736 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
737 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
738 gi->gi_n[i].gn_be->bd_info );
739 /* Let backend.c take care of the rest of startup */
741 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
744 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
745 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
747 } /* other case is impossible */
751 /* This function will only be called in tool mode */
757 static int glueClosed = 0;
760 if (glueClosed) return 0;
764 if (slapMode & SLAP_TOOL_MODE) {
765 rc = backend_shutdown( NULL );
775 AttributeDescription *ad,
780 BackendDB *b0 = op->o_bd;
781 op->o_bd = glue_back_select( b0, dn );
783 if ( op->o_bd->be_fetch ) {
784 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
786 rc = LDAP_UNWILLING_TO_PERFORM;
793 glue_entry_release_rw (
799 BackendDB *b0 = op->o_bd;
802 op->o_bd = glue_back_select (b0, &e->e_nname);
804 if ( op->o_bd->be_release ) {
805 rc = op->o_bd->be_release( op, e, rw );
808 /* FIXME: mimic be_entry_release_rw
809 * when no be_release() available */
818 static struct berval *glue_base;
819 static int glue_scope;
820 static Filter *glue_filter;
823 glue_tool_entry_first (
827 slap_overinst *on = glue_tool_inst( b0->bd_info );
828 glueinfo *gi = on->on_bi.bi_private;
832 /* If we're starting from scratch, start at the most general */
834 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
837 for (i = gi->gi_nodes-1; i >= 0; i--) {
838 if (gi->gi_n[i].gn_be->be_entry_open &&
839 gi->gi_n[i].gn_be->be_entry_first) {
840 glueBack = gi->gi_n[i].gn_be;
846 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
847 glueBack->be_entry_open (glueBack, glueMode) != 0)
850 rc = glueBack->be_entry_first (glueBack);
851 while ( rc == NOID ) {
852 if ( glueBack && glueBack->be_entry_close )
853 glueBack->be_entry_close (glueBack);
854 for (i=0; i<gi->gi_nodes; i++) {
855 if (gi->gi_n[i].gn_be == glueBack)
859 glueBack = GLUEBACK_DONE;
862 glueBack = gi->gi_n[i-1].gn_be;
863 rc = glue_tool_entry_first (b0);
864 if ( glueBack == GLUEBACK_DONE ) {
873 glue_tool_entry_first_x (
880 slap_overinst *on = glue_tool_inst( b0->bd_info );
881 glueinfo *gi = on->on_bi.bi_private;
889 /* If we're starting from scratch, start at the most general */
891 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
894 for (i = gi->gi_nodes-1; i >= 0; i--) {
895 if (gi->gi_n[i].gn_be->be_entry_open &&
896 gi->gi_n[i].gn_be->be_entry_first_x)
898 glueBack = gi->gi_n[i].gn_be;
904 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
905 glueBack->be_entry_open (glueBack, glueMode) != 0)
908 rc = glueBack->be_entry_first_x (glueBack,
909 glue_base, glue_scope, glue_filter);
910 while ( rc == NOID ) {
911 if ( glueBack && glueBack->be_entry_close )
912 glueBack->be_entry_close (glueBack);
913 for (i=0; i<gi->gi_nodes; i++) {
914 if (gi->gi_n[i].gn_be == glueBack)
918 glueBack = GLUEBACK_DONE;
921 glueBack = gi->gi_n[i-1].gn_be;
922 rc = glue_tool_entry_first_x (b0,
923 glue_base, glue_scope, glue_filter);
924 if ( glueBack == GLUEBACK_DONE ) {
933 glue_tool_entry_next (
937 slap_overinst *on = glue_tool_inst( b0->bd_info );
938 glueinfo *gi = on->on_bi.bi_private;
942 if (!glueBack || !glueBack->be_entry_next)
945 rc = glueBack->be_entry_next (glueBack);
947 /* If we ran out of entries in one database, move on to the next */
949 if ( glueBack && glueBack->be_entry_close )
950 glueBack->be_entry_close (glueBack);
951 for (i=0; i<gi->gi_nodes; i++) {
952 if (gi->gi_n[i].gn_be == glueBack)
956 glueBack = GLUEBACK_DONE;
959 glueBack = gi->gi_n[i-1].gn_be;
960 if ( glue_base || glue_filter ) {
961 /* using entry_first_x() */
962 rc = glue_tool_entry_first_x (b0,
963 glue_base, glue_scope, glue_filter);
966 /* using entry_first() */
967 rc = glue_tool_entry_first (b0);
969 if ( glueBack == GLUEBACK_DONE ) {
978 glue_tool_dn2id_get (
987 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
988 be = glue_back_select (&b2, dn);
989 if ( be == &b2 ) be = &toolDB;
991 if (!be->be_dn2id_get)
995 if ( be->be_entry_open ) {
996 rc = be->be_entry_open (be, glueMode);
1001 } else if (be != glueBack) {
1002 /* If this entry belongs in a different branch than the
1003 * previous one, close the current database and open the
1006 if ( glueBack->be_entry_close ) {
1007 glueBack->be_entry_close (glueBack);
1009 if ( be->be_entry_open ) {
1010 rc = be->be_entry_open (be, glueMode);
1017 return be->be_dn2id_get (be, dn);
1021 glue_tool_entry_get (
1026 if (!glueBack || !glueBack->be_entry_get)
1029 return glueBack->be_entry_get (glueBack, id);
1033 glue_tool_entry_put (
1043 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1044 be = glue_back_select (&b2, &e->e_nname);
1045 if ( be == &b2 ) be = &toolDB;
1047 if (!be->be_entry_put)
1051 if ( be->be_entry_open ) {
1052 rc = be->be_entry_open (be, glueMode);
1057 } else if (be != glueBack) {
1058 /* If this entry belongs in a different branch than the
1059 * previous one, close the current database and open the
1062 if ( glueBack->be_entry_close ) {
1063 glueBack->be_entry_close (glueBack);
1065 if ( be->be_entry_open ) {
1066 rc = be->be_entry_open (be, glueMode);
1073 return be->be_entry_put (be, e, text);
1077 glue_tool_entry_modify (
1083 if (!glueBack || !glueBack->be_entry_modify)
1086 return glueBack->be_entry_modify (glueBack, e, text);
1090 glue_tool_entry_reindex (
1093 AttributeDescription **adv
1096 if (!glueBack || !glueBack->be_entry_reindex)
1099 return glueBack->be_entry_reindex (glueBack, id, adv);
1107 slap_overinst *on = glue_tool_inst( b0->bd_info );
1108 glueinfo *gi = on->on_bi.bi_private;
1109 BackendInfo *bi = b0->bd_info;
1112 /* just sync everyone */
1113 for (i = 0; i<gi->gi_nodes; i++)
1114 if (gi->gi_n[i].gn_be->be_sync)
1115 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1116 b0->bd_info = on->on_info->oi_orig;
1123 typedef struct glue_Addrec {
1124 struct glue_Addrec *ga_next;
1128 /* List of added subordinates */
1129 static glue_Addrec *ga_list;
1130 static int ga_adding;
1138 slap_overinst *on = (slap_overinst *)be->bd_info;
1139 slap_overinfo *oi = on->on_info;
1140 BackendInfo *bi = oi->oi_orig;
1143 if ( SLAP_GLUE_SUBORDINATE( be )) {
1144 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1145 "cannot have glue overlay!\n",
1146 be->be_suffix[0].bv_val, 0, 0 );
1150 gi = ch_calloc( 1, sizeof(glueinfo));
1151 on->on_bi.bi_private = gi;
1152 dnParent( be->be_nsuffix, &gi->gi_pdn );
1154 /* Currently the overlay framework doesn't handle these entry points
1155 * but we need them....
1157 oi->oi_bi.bi_open = glue_open;
1158 oi->oi_bi.bi_close = glue_close;
1160 /* Only advertise these if the root DB supports them */
1161 if ( bi->bi_tool_entry_open )
1162 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1163 if ( bi->bi_tool_entry_close )
1164 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1165 if ( bi->bi_tool_entry_first )
1166 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1167 /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1168 if ( bi->bi_tool_entry_first_x )
1169 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1170 if ( bi->bi_tool_entry_next )
1171 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1172 if ( bi->bi_tool_entry_get )
1173 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1174 if ( bi->bi_tool_dn2id_get )
1175 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1176 if ( bi->bi_tool_entry_put )
1177 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1178 if ( bi->bi_tool_entry_reindex )
1179 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1180 if ( bi->bi_tool_entry_modify )
1181 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1182 if ( bi->bi_tool_sync )
1183 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1185 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1188 be->bd_info = (BackendInfo *)oi;
1189 glue_sub_attach( 1 );
1201 slap_overinst *on = (slap_overinst *)be->bd_info;
1202 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
1205 return SLAP_CB_CONTINUE;
1214 slap_overinst *on = (slap_overinst *)be->bd_info;
1216 on->on_info->oi_bi.bi_db_close = 0;
1221 glue_sub_del( BackendDB *b0 )
1226 /* Find the top backend for this subordinate */
1228 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1234 if ( SLAP_GLUE_SUBORDINATE( be ))
1236 if ( !SLAP_GLUE_INSTANCE( be ))
1238 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1241 /* OK, got the right backend, find the overlay */
1242 oi = (slap_overinfo *)be->bd_info;
1243 for ( on=oi->oi_list; on; on=on->on_next ) {
1244 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1247 assert( on != NULL );
1248 gi = on->on_bi.bi_private;
1249 for ( i=0; i < gi->gi_nodes; i++ ) {
1250 if ( gi->gi_n[i].gn_be == b0 ) {
1253 for (j=i+1; j < gi->gi_nodes; j++)
1254 gi->gi_n[j-1] = gi->gi_n[j];
1261 rc = LDAP_NO_SUCH_OBJECT;
1267 /* Attach all the subordinate backends to their superior */
1269 glue_sub_attach( int online )
1271 glue_Addrec *ga, *gnext = NULL;
1279 /* For all the subordinate backends */
1280 for ( ga=ga_list; ga != NULL; ga = gnext ) {
1283 gnext = ga->ga_next;
1285 /* Find the top backend for this subordinate */
1287 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1292 if ( SLAP_GLUE_SUBORDINATE( be ))
1294 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1297 /* If it's not already configured, set up the overlay */
1298 if ( !SLAP_GLUE_INSTANCE( be )) {
1299 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1303 /* Find the overlay instance */
1304 oi = (slap_overinfo *)be->bd_info;
1305 for ( on=oi->oi_list; on; on=on->on_next ) {
1306 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1309 assert( on != NULL );
1310 gi = on->on_bi.bi_private;
1311 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1312 gi->gi_nodes * sizeof(gluenode));
1313 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1314 dnParent( &ga->ga_be->be_nsuffix[0],
1315 &gi->gi_n[gi->gi_nodes].gn_pdn );
1317 on->on_bi.bi_private = gi;
1318 ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1322 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1323 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1324 /* allow this for now, assume a superior will
1332 rc = LDAP_NO_SUCH_OBJECT;
1346 glue_sub_add( BackendDB *be, int advert, int online )
1351 if ( overlay_is_inst( be, "glue" )) {
1352 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1353 "cannot be a subordinate!\n",
1354 be->be_suffix[0].bv_val, 0, 0 );
1357 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1359 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1361 ga = ch_malloc( sizeof( glue_Addrec ));
1362 ga->ga_next = ga_list;
1367 rc = glue_sub_attach( online );
1375 glue.on_bi.bi_type = "glue";
1377 glue.on_bi.bi_db_init = glue_db_init;
1378 glue.on_bi.bi_db_close = glue_db_close;
1379 glue.on_bi.bi_db_destroy = glue_db_destroy;
1381 glue.on_bi.bi_op_search = glue_op_search;
1382 glue.on_bi.bi_op_modify = glue_op_func;
1383 glue.on_bi.bi_op_modrdn = glue_op_func;
1384 glue.on_bi.bi_op_add = glue_op_func;
1385 glue.on_bi.bi_op_delete = glue_op_func;
1386 glue.on_bi.bi_extended = glue_op_func;
1388 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1389 glue.on_bi.bi_chk_controls = glue_chk_controls;
1390 glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1391 glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1393 glue.on_response = glue_response;
1395 return overlay_register( &glue );