1 /* backglue.c - backend glue */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2009 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_response glue_op_response;
61 /* Just like select_backend, but only for our backends */
68 slap_overinst *on = (slap_overinst *)be->bd_info;
69 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
72 for (i = gi->gi_nodes-1; i >= 0; i--) {
73 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
75 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
76 return gi->gi_n[i].gn_be;
79 be->bd_info = on->on_info->oi_orig;
84 typedef struct glue_state {
95 glue_op_cleanup( Operation *op, SlapReply *rs )
97 /* This is not a final result */
98 if (rs->sr_type == REP_RESULT )
99 rs->sr_type = REP_GLUE_RESULT;
100 return SLAP_CB_CONTINUE;
104 glue_op_response ( Operation *op, SlapReply *rs )
106 glue_state *gs = op->o_callback->sc_private;
108 switch(rs->sr_type) {
111 case REP_INTERMEDIATE:
112 return SLAP_CB_CONTINUE;
115 if (rs->sr_err == LDAP_SUCCESS ||
116 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
117 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
118 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
119 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
120 gs->err != LDAP_SUCCESS)
121 gs->err = rs->sr_err;
122 if (gs->err == LDAP_SUCCESS && gs->matched) {
123 ch_free (gs->matched);
127 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
129 len = strlen (rs->sr_matched);
130 if (len > gs->matchlen) {
132 ch_free (gs->matched);
133 gs->matched = ch_strdup (rs->sr_matched);
141 for (i=0; rs->sr_ref[i].bv_val; i++);
145 new = ch_malloc ((i+1)*sizeof(struct berval));
147 new = ch_realloc(gs->refs,
148 (j+i+1)*sizeof(struct berval));
150 for (k=0; k<i; j++,k++) {
151 ber_dupbv( &new[j], &rs->sr_ref[k] );
153 new[j].bv_val = NULL;
159 LDAPControl **newctrls;
161 for (i=0; rs->sr_ctrls[i]; i++);
165 newctrls = op->o_tmpalloc((i+1)*sizeof(LDAPControl *),
168 /* Forget old pagedResults response if we're sending
171 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
173 for ( k=0; k<i; k++ ) {
174 if ( !strcmp(rs->sr_ctrls[k]->ldctl_oid,
175 LDAP_CONTROL_PAGEDRESULTS )) {
181 for ( k=0; k<j; k++ ) {
182 if ( !strcmp(gs->ctrls[k]->ldctl_oid,
183 LDAP_CONTROL_PAGEDRESULTS ))
185 op->o_tmpfree(gs->ctrls[k], op->o_tmpmemctx);
186 gs->ctrls[k] = gs->ctrls[--j];
193 newctrls = op->o_tmprealloc(gs->ctrls,
194 (j+i+1)*sizeof(LDAPControl *), op->o_tmpmemctx);
196 for (k=0; k<i; j++,k++) {
197 ber_len_t oidlen = strlen( rs->sr_ctrls[k]->ldctl_oid );
198 newctrls[j] = op->o_tmpalloc(sizeof(LDAPControl) + oidlen + 1 + rs->sr_ctrls[k]->ldctl_value.bv_len + 1,
200 newctrls[j]->ldctl_iscritical = rs->sr_ctrls[k]->ldctl_iscritical;
201 newctrls[j]->ldctl_oid = (char *)&newctrls[j][1];
202 lutil_strcopy( newctrls[j]->ldctl_oid, rs->sr_ctrls[k]->ldctl_oid );
203 if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ) ) {
204 newctrls[j]->ldctl_value.bv_val = &newctrls[j]->ldctl_oid[oidlen + 1];
205 newctrls[j]->ldctl_value.bv_len = rs->sr_ctrls[k]->ldctl_value.bv_len;
206 lutil_memcopy( newctrls[j]->ldctl_value.bv_val,
207 rs->sr_ctrls[k]->ldctl_value.bv_val,
208 rs->sr_ctrls[k]->ldctl_value.bv_len + 1 );
210 BER_BVZERO( &newctrls[j]->ldctl_value );
215 gs->ctrls = newctrls;
222 glue_op_func ( Operation *op, SlapReply *rs )
224 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
225 BackendDB *b0 = op->o_bd;
226 BackendInfo *bi0 = op->o_bd->bd_info;
228 slap_operation_t which = op_bind;
231 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
233 /* If we're on the master backend, let overlay framework handle it */
234 if ( op->o_bd == b0 )
235 return SLAP_CB_CONTINUE;
237 b0->bd_info = on->on_info->oi_orig;
240 case LDAP_REQ_ADD: which = op_add; break;
241 case LDAP_REQ_DELETE: which = op_delete; break;
242 case LDAP_REQ_MODIFY: which = op_modify; break;
243 case LDAP_REQ_MODRDN: which = op_modrdn; break;
244 case LDAP_REQ_EXTENDED: which = op_extended; break;
245 default: assert( 0 ); break;
248 func = &op->o_bd->bd_info->bi_op_bind;
250 rc = func[which]( op, rs );
255 op->o_bd->bd_info = bi0;
260 glue_response ( Operation *op, SlapReply *rs )
262 BackendDB *be = op->o_bd;
263 be = glue_back_select (op->o_bd, &op->o_req_ndn);
265 /* If we're on the master backend, let overlay framework handle it.
266 * Otherwise, bail out.
268 return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
272 glue_chk_referrals ( Operation *op, SlapReply *rs )
274 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
275 BackendDB *b0 = op->o_bd;
276 BackendInfo *bi0 = op->o_bd->bd_info;
279 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
280 if ( op->o_bd == b0 )
281 return SLAP_CB_CONTINUE;
283 b0->bd_info = on->on_info->oi_orig;
285 if ( op->o_bd->bd_info->bi_chk_referrals )
286 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
288 rc = SLAP_CB_CONTINUE;
291 op->o_bd->bd_info = bi0;
296 glue_chk_controls ( Operation *op, SlapReply *rs )
298 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
299 BackendDB *b0 = op->o_bd;
300 BackendInfo *bi0 = op->o_bd->bd_info;
301 int rc = SLAP_CB_CONTINUE;
303 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
304 if ( op->o_bd == b0 )
305 return SLAP_CB_CONTINUE;
307 b0->bd_info = on->on_info->oi_orig;
309 /* if the subordinate database has overlays, the bi_chk_controls()
310 * hook is actually over_aux_chk_controls(); in case it actually
311 * wraps a missing hok, we need to mimic the behavior
312 * of the frontend applied to that database */
313 if ( op->o_bd->bd_info->bi_chk_controls ) {
314 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
318 if ( rc == SLAP_CB_CONTINUE ) {
319 rc = backend_check_controls( op, rs );
323 op->o_bd->bd_info = bi0;
327 /* ITS#4615 - overlays configured above the glue overlay should be
328 * invoked for the entire glued tree. Overlays configured below the
329 * glue overlay should only be invoked on the master backend.
330 * So, if we're searching on any subordinates, we need to force the
331 * current overlay chain to stop processing, without stopping the
332 * overall callback flow.
335 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
338 /* Process any overlays on the master backend */
339 if ( op->o_bd == b0 && on->on_next ) {
340 BackendInfo *bi = op->o_bd->bd_info;
341 int rc = SLAP_CB_CONTINUE;
342 for ( on=on->on_next; on; on=on->on_next ) {
343 op->o_bd->bd_info = (BackendInfo *)on;
344 if ( on->on_bi.bi_op_search ) {
345 rc = on->on_bi.bi_op_search( op, rs );
346 if ( rc != SLAP_CB_CONTINUE )
350 op->o_bd->bd_info = bi;
351 if ( rc != SLAP_CB_CONTINUE )
354 return op->o_bd->be_search( op, rs );
358 glue_op_search ( Operation *op, SlapReply *rs )
360 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
361 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
362 BackendDB *b0 = op->o_bd;
363 BackendDB *b1 = NULL, *btmp;
364 BackendInfo *bi0 = op->o_bd->bd_info;
366 long stoptime = 0, starttime;
367 glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
368 slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
370 struct berval dn, ndn, *pdn;
374 cb.sc_next = op->o_callback;
376 starttime = op->o_time;
377 stoptime = slap_get_time () + op->ors_tlimit;
379 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
380 b0->bd_info = on->on_info->oi_orig;
382 switch (op->ors_scope) {
383 case LDAP_SCOPE_BASE:
384 if ( op->o_bd == b0 )
385 return SLAP_CB_CONTINUE;
387 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
388 if (op->o_bd && op->o_bd->be_search) {
389 rs->sr_err = op->o_bd->be_search( op, rs );
393 case LDAP_SCOPE_ONELEVEL:
394 case LDAP_SCOPE_SUBTREE:
395 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
396 op->o_callback = &cb;
397 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
398 scope0 = op->ors_scope;
399 tlimit0 = op->ors_tlimit;
405 * Execute in reverse order, most specific first
407 for (i = gi->gi_nodes; i >= 0; i--) {
408 if ( i == gi->gi_nodes ) {
412 btmp = gi->gi_n[i].gn_be;
413 pdn = &gi->gi_n[i].gn_pdn;
415 if (!btmp || !btmp->be_search)
417 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
419 if (get_no_subordinate_glue(op) && btmp != b1)
421 /* If we remembered which backend we were on before,
422 * skip down to it now
424 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
425 op->o_conn->c_pagedresults_state.ps_be &&
426 op->o_conn->c_pagedresults_state.ps_be != btmp )
429 if (tlimit0 != SLAP_NO_LIMIT) {
430 op->o_time = slap_get_time();
431 op->ors_tlimit = stoptime - op->o_time;
432 if (op->ors_tlimit <= 0) {
433 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
446 assert( op->o_bd->be_suffix != NULL );
447 assert( op->o_bd->be_nsuffix != NULL );
449 if (scope0 == LDAP_SCOPE_ONELEVEL &&
452 struct berval mdn, mndn;
453 op->ors_scope = LDAP_SCOPE_BASE;
454 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
455 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
456 rs->sr_err = op->o_bd->be_search(op, rs);
457 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
458 gs.err = LDAP_SUCCESS;
460 op->ors_scope = LDAP_SCOPE_ONELEVEL;
461 if ( op->o_req_dn.bv_val == mdn.bv_val )
463 if ( op->o_req_ndn.bv_val == mndn.bv_val )
466 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
467 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
469 rs->sr_err = glue_sub_search( op, rs, b0, on );
471 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
472 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
474 struct berval mdn, mndn;
475 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
476 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
477 rs->sr_err = glue_sub_search( op, rs, b0, on );
478 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
479 gs.err = LDAP_SUCCESS;
481 if ( op->o_req_dn.bv_val == mdn.bv_val )
483 if ( op->o_req_ndn.bv_val == mndn.bv_val )
486 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
487 rs->sr_err = glue_sub_search( op, rs, b0, on );
493 * Add errors that should result in dropping
496 case LDAP_SIZELIMIT_EXCEEDED:
497 case LDAP_TIMELIMIT_EXCEEDED:
498 case LDAP_ADMINLIMIT_EXCEEDED:
499 case LDAP_NO_SUCH_OBJECT:
500 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
501 case LDAP_X_CANNOT_CHAIN:
502 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
506 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
507 PagedResultsState *ps = op->o_pagedresults_state;
509 /* Assume this backend can be forgotten now */
510 op->o_conn->c_pagedresults_state.ps_be = NULL;
512 /* If we have a full page, exit the loop. We may
513 * need to remember this backend so we can continue
514 * from here on a subsequent request.
516 if ( rs->sr_nentries >= ps->ps_size ) {
517 /* Don't bother to remember the first backend.
518 * Only remember the last one if there's more state left.
520 if ( op->o_bd != b0 &&
521 ( op->o_conn->c_pagedresults_state.ps_cookie ||
522 op->o_bd != gi->gi_n[0].gn_be ))
523 op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
527 /* This backend has run out of entries, but more responses
528 * can fit in the page. Fake a reset of the state so the
529 * next backend will start up properly. Only back-[bh]db
530 * and back-sql look at this state info.
532 if ( ps->ps_cookieval.bv_len == sizeof( PagedResultsCookie )) {
534 memset( ps->ps_cookieval.bv_val, 0,
535 sizeof( PagedResultsCookie ));
540 /* change the size of the page in the request
541 * that will be propagated */
542 BerElementBuffer berbuf;
543 BerElement *ber = (BerElement *)&berbuf;
544 int size = ps->ps_size - rs->sr_nentries;
545 struct berval cookie = BER_BVC(""), value;
548 for (c = 0; op->o_ctrls[c] != NULL; c++) {
549 if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
553 ber_init2( ber, NULL, LBER_USE_DER );
554 ber_printf( ber, "{iO}", size, &cookie );
555 ber_flatten2( ber, &value, 0 );
556 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
557 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
558 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
559 value.bv_val, value.bv_len );
569 op->ors_scope = scope0;
570 op->ors_tlimit = tlimit0;
571 op->o_time = starttime;
576 op->o_callback = cb.sc_next;
577 if ( op->o_abandon ) {
578 rs->sr_err = SLAPD_ABANDON;
581 rs->sr_matched = gs.matched;
582 rs->sr_ref = gs.refs;
584 rs->sr_ctrls = gs.ctrls;
586 send_ldap_result( op, rs );
589 op->o_bd->bd_info = bi0;
593 ber_bvarray_free(gs.refs);
595 for (i = gs.nctrls; --i >= 0; ) {
596 op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
598 op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
603 static BackendDB toolDB;
606 glue_tool_entry_open (
611 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
613 /* We don't know which backend to talk to yet, so just
614 * remember the mode and move on...
620 toolDB.bd_info = oi->oi_orig;
626 glue_tool_entry_close (
632 if (glueBack && glueBack != GLUEBACK_DONE) {
633 if (!glueBack->be_entry_close)
635 rc = glueBack->be_entry_close (glueBack);
640 static slap_overinst *
645 slap_overinfo *oi = (slap_overinfo *)bi;
648 for ( on = oi->oi_list; on; on=on->on_next ) {
649 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
655 /* This function will only be called in tool mode */
661 slap_overinst *on = glue_tool_inst( bi );
662 glueinfo *gi = on->on_bi.bi_private;
663 static int glueOpened = 0;
664 int i, j, same, bsame = 0, rc = 0;
665 ConfigReply cr = {0};
667 if (glueOpened) return 0;
671 /* If we were invoked in tool mode, open all the underlying backends */
672 if (slapMode & SLAP_TOOL_MODE) {
673 for (i = 0; i<gi->gi_nodes; i++) {
675 /* Same bi_open as our main backend? */
676 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
677 on->on_info->oi_orig->bi_open )
680 /* Loop thru the bd_info's and make sure we only
681 * invoke their bi_open functions once each.
683 for ( j = 0; j<i; j++ ) {
684 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
685 gi->gi_n[j].gn_be->bd_info->bi_open ) {
690 /* OK, it's unique and non-NULL, call it. */
691 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
692 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
693 gi->gi_n[i].gn_be->bd_info );
694 /* Let backend.c take care of the rest of startup */
696 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
699 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
700 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
702 } /* other case is impossible */
706 /* This function will only be called in tool mode */
712 static int glueClosed = 0;
715 if (glueClosed) return 0;
719 if (slapMode & SLAP_TOOL_MODE) {
720 rc = backend_shutdown( NULL );
730 AttributeDescription *ad,
735 BackendDB *b0 = op->o_bd;
736 op->o_bd = glue_back_select( b0, dn );
738 if ( op->o_bd->be_fetch ) {
739 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
741 rc = LDAP_UNWILLING_TO_PERFORM;
748 glue_entry_release_rw (
754 BackendDB *b0 = op->o_bd;
757 op->o_bd = glue_back_select (b0, &e->e_nname);
759 if ( op->o_bd->be_release ) {
760 rc = op->o_bd->be_release( op, e, rw );
763 /* FIXME: mimic be_entry_release_rw
764 * when no be_release() available */
773 static struct berval *glue_base;
774 static int glue_scope;
775 static Filter *glue_filter;
778 glue_tool_entry_first (
782 slap_overinst *on = glue_tool_inst( b0->bd_info );
783 glueinfo *gi = on->on_bi.bi_private;
787 /* If we're starting from scratch, start at the most general */
789 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
792 for (i = gi->gi_nodes-1; i >= 0; i--) {
793 if (gi->gi_n[i].gn_be->be_entry_open &&
794 gi->gi_n[i].gn_be->be_entry_first) {
795 glueBack = gi->gi_n[i].gn_be;
801 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
802 glueBack->be_entry_open (glueBack, glueMode) != 0)
805 rc = glueBack->be_entry_first (glueBack);
806 while ( rc == NOID ) {
807 if ( glueBack && glueBack->be_entry_close )
808 glueBack->be_entry_close (glueBack);
809 for (i=0; i<gi->gi_nodes; i++) {
810 if (gi->gi_n[i].gn_be == glueBack)
814 glueBack = GLUEBACK_DONE;
817 glueBack = gi->gi_n[i-1].gn_be;
818 rc = glue_tool_entry_first (b0);
819 if ( glueBack == GLUEBACK_DONE ) {
828 glue_tool_entry_first_x (
835 slap_overinst *on = glue_tool_inst( b0->bd_info );
836 glueinfo *gi = on->on_bi.bi_private;
844 /* If we're starting from scratch, start at the most general */
846 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
849 for (i = gi->gi_nodes-1; i >= 0; i--) {
850 if (gi->gi_n[i].gn_be->be_entry_open &&
851 gi->gi_n[i].gn_be->be_entry_first_x)
853 glueBack = gi->gi_n[i].gn_be;
859 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
860 glueBack->be_entry_open (glueBack, glueMode) != 0)
863 rc = glueBack->be_entry_first_x (glueBack,
864 glue_base, glue_scope, glue_filter);
865 while ( rc == NOID ) {
866 if ( glueBack && glueBack->be_entry_close )
867 glueBack->be_entry_close (glueBack);
868 for (i=0; i<gi->gi_nodes; i++) {
869 if (gi->gi_n[i].gn_be == glueBack)
873 glueBack = GLUEBACK_DONE;
876 glueBack = gi->gi_n[i-1].gn_be;
877 rc = glue_tool_entry_first_x (b0,
878 glue_base, glue_scope, glue_filter);
879 if ( glueBack == GLUEBACK_DONE ) {
888 glue_tool_entry_next (
892 slap_overinst *on = glue_tool_inst( b0->bd_info );
893 glueinfo *gi = on->on_bi.bi_private;
897 if (!glueBack || !glueBack->be_entry_next)
900 rc = glueBack->be_entry_next (glueBack);
902 /* If we ran out of entries in one database, move on to the next */
904 if ( glueBack && glueBack->be_entry_close )
905 glueBack->be_entry_close (glueBack);
906 for (i=0; i<gi->gi_nodes; i++) {
907 if (gi->gi_n[i].gn_be == glueBack)
911 glueBack = GLUEBACK_DONE;
914 glueBack = gi->gi_n[i-1].gn_be;
915 if ( glue_base || glue_filter ) {
916 /* using entry_first_x() */
917 rc = glue_tool_entry_first_x (b0,
918 glue_base, glue_scope, glue_filter);
921 /* using entry_first() */
922 rc = glue_tool_entry_first (b0);
924 if ( glueBack == GLUEBACK_DONE ) {
933 glue_tool_dn2id_get (
942 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
943 be = glue_back_select (&b2, dn);
944 if ( be == &b2 ) be = &toolDB;
946 if (!be->be_dn2id_get)
950 if ( be->be_entry_open ) {
951 rc = be->be_entry_open (be, glueMode);
956 } else if (be != glueBack) {
957 /* If this entry belongs in a different branch than the
958 * previous one, close the current database and open the
961 if ( glueBack->be_entry_close ) {
962 glueBack->be_entry_close (glueBack);
964 if ( be->be_entry_open ) {
965 rc = be->be_entry_open (be, glueMode);
972 return be->be_dn2id_get (be, dn);
976 glue_tool_entry_get (
981 if (!glueBack || !glueBack->be_entry_get)
984 return glueBack->be_entry_get (glueBack, id);
988 glue_tool_entry_put (
998 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
999 be = glue_back_select (&b2, &e->e_nname);
1000 if ( be == &b2 ) be = &toolDB;
1002 if (!be->be_entry_put)
1006 if ( be->be_entry_open ) {
1007 rc = be->be_entry_open (be, glueMode);
1012 } else if (be != glueBack) {
1013 /* If this entry belongs in a different branch than the
1014 * previous one, close the current database and open the
1017 if ( glueBack->be_entry_close ) {
1018 glueBack->be_entry_close (glueBack);
1020 if ( be->be_entry_open ) {
1021 rc = be->be_entry_open (be, glueMode);
1028 return be->be_entry_put (be, e, text);
1032 glue_tool_entry_modify (
1038 if (!glueBack || !glueBack->be_entry_modify)
1041 return glueBack->be_entry_modify (glueBack, e, text);
1045 glue_tool_entry_reindex (
1048 AttributeDescription **adv
1051 if (!glueBack || !glueBack->be_entry_reindex)
1054 return glueBack->be_entry_reindex (glueBack, id, adv);
1062 slap_overinst *on = glue_tool_inst( b0->bd_info );
1063 glueinfo *gi = on->on_bi.bi_private;
1064 BackendInfo *bi = b0->bd_info;
1067 /* just sync everyone */
1068 for (i = 0; i<gi->gi_nodes; i++)
1069 if (gi->gi_n[i].gn_be->be_sync)
1070 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1071 b0->bd_info = on->on_info->oi_orig;
1078 typedef struct glue_Addrec {
1079 struct glue_Addrec *ga_next;
1083 /* List of added subordinates */
1084 static glue_Addrec *ga_list;
1085 static int ga_adding;
1093 slap_overinst *on = (slap_overinst *)be->bd_info;
1094 slap_overinfo *oi = on->on_info;
1095 BackendInfo *bi = oi->oi_orig;
1098 if ( SLAP_GLUE_SUBORDINATE( be )) {
1099 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1100 "cannot have glue overlay!\n",
1101 be->be_suffix[0].bv_val, 0, 0 );
1105 gi = ch_calloc( 1, sizeof(glueinfo));
1106 on->on_bi.bi_private = gi;
1107 dnParent( be->be_nsuffix, &gi->gi_pdn );
1109 /* Currently the overlay framework doesn't handle these entry points
1110 * but we need them....
1112 oi->oi_bi.bi_open = glue_open;
1113 oi->oi_bi.bi_close = glue_close;
1115 /* Only advertise these if the root DB supports them */
1116 if ( bi->bi_tool_entry_open )
1117 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1118 if ( bi->bi_tool_entry_close )
1119 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1120 if ( bi->bi_tool_entry_first )
1121 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1122 /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1123 if ( bi->bi_tool_entry_first_x )
1124 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1125 if ( bi->bi_tool_entry_next )
1126 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1127 if ( bi->bi_tool_entry_get )
1128 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1129 if ( bi->bi_tool_dn2id_get )
1130 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1131 if ( bi->bi_tool_entry_put )
1132 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1133 if ( bi->bi_tool_entry_reindex )
1134 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1135 if ( bi->bi_tool_entry_modify )
1136 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1137 if ( bi->bi_tool_sync )
1138 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1140 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1143 be->bd_info = (BackendInfo *)oi;
1144 glue_sub_attach( 1 );
1156 slap_overinst *on = (slap_overinst *)be->bd_info;
1157 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
1160 return SLAP_CB_CONTINUE;
1169 slap_overinst *on = (slap_overinst *)be->bd_info;
1171 on->on_info->oi_bi.bi_db_close = 0;
1176 glue_sub_del( BackendDB *b0 )
1181 /* Find the top backend for this subordinate */
1183 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1189 if ( SLAP_GLUE_SUBORDINATE( be ))
1191 if ( !SLAP_GLUE_INSTANCE( be ))
1193 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1196 /* OK, got the right backend, find the overlay */
1197 oi = (slap_overinfo *)be->bd_info;
1198 for ( on=oi->oi_list; on; on=on->on_next ) {
1199 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1202 assert( on != NULL );
1203 gi = on->on_bi.bi_private;
1204 for ( i=0; i < gi->gi_nodes; i++ ) {
1205 if ( gi->gi_n[i].gn_be == b0 ) {
1208 for (j=i+1; j < gi->gi_nodes; j++)
1209 gi->gi_n[j-1] = gi->gi_n[j];
1216 rc = LDAP_NO_SUCH_OBJECT;
1222 /* Attach all the subordinate backends to their superior */
1224 glue_sub_attach( int online )
1226 glue_Addrec *ga, *gnext = NULL;
1234 /* For all the subordinate backends */
1235 for ( ga=ga_list; ga != NULL; ga = gnext ) {
1238 gnext = ga->ga_next;
1240 /* Find the top backend for this subordinate */
1242 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1247 if ( SLAP_GLUE_SUBORDINATE( be ))
1249 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1252 /* If it's not already configured, set up the overlay */
1253 if ( !SLAP_GLUE_INSTANCE( be )) {
1254 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1258 /* Find the overlay instance */
1259 oi = (slap_overinfo *)be->bd_info;
1260 for ( on=oi->oi_list; on; on=on->on_next ) {
1261 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1264 assert( on != NULL );
1265 gi = on->on_bi.bi_private;
1266 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1267 gi->gi_nodes * sizeof(gluenode));
1268 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1269 dnParent( &ga->ga_be->be_nsuffix[0],
1270 &gi->gi_n[gi->gi_nodes].gn_pdn );
1272 on->on_bi.bi_private = gi;
1273 ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1277 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1278 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1279 /* allow this for now, assume a superior will
1287 rc = LDAP_NO_SUCH_OBJECT;
1301 glue_sub_add( BackendDB *be, int advert, int online )
1306 if ( overlay_is_inst( be, "glue" )) {
1307 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1308 "cannot be a subordinate!\n",
1309 be->be_suffix[0].bv_val, 0, 0 );
1312 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1314 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1316 ga = ch_malloc( sizeof( glue_Addrec ));
1317 ga->ga_next = ga_list;
1322 rc = glue_sub_attach( online );
1328 glue_access_allowed(
1331 AttributeDescription *desc,
1333 slap_access_t access,
1334 AccessControlState *state,
1335 slap_mask_t *maskp )
1337 BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
1340 if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
1341 return SLAP_CB_CONTINUE;
1345 rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
1353 glue.on_bi.bi_type = "glue";
1355 glue.on_bi.bi_db_init = glue_db_init;
1356 glue.on_bi.bi_db_close = glue_db_close;
1357 glue.on_bi.bi_db_destroy = glue_db_destroy;
1359 glue.on_bi.bi_op_search = glue_op_search;
1360 glue.on_bi.bi_op_modify = glue_op_func;
1361 glue.on_bi.bi_op_modrdn = glue_op_func;
1362 glue.on_bi.bi_op_add = glue_op_func;
1363 glue.on_bi.bi_op_delete = glue_op_func;
1364 glue.on_bi.bi_extended = glue_op_func;
1366 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1367 glue.on_bi.bi_chk_controls = glue_chk_controls;
1368 glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1369 glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1370 glue.on_bi.bi_access_allowed = glue_access_allowed;
1372 glue.on_response = glue_response;
1374 return overlay_register( &glue );