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 );
359 static const ID glueID = NOID;
360 static const struct berval gluecookie = { sizeof( glueID ), (char *)&glueID };
363 glue_op_search ( Operation *op, SlapReply *rs )
365 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
366 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
367 BackendDB *b0 = op->o_bd;
368 BackendDB *b1 = NULL, *btmp;
369 BackendInfo *bi0 = op->o_bd->bd_info;
371 long stoptime = 0, starttime;
372 glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
373 slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
375 struct berval dn, ndn, *pdn;
379 cb.sc_next = op->o_callback;
381 starttime = op->o_time;
382 stoptime = slap_get_time () + op->ors_tlimit;
384 /* reset dummy cookie used to keep paged results going across databases */
385 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED
386 && bvmatch( &((PagedResultsState *)op->o_pagedresults_state)->ps_cookieval, &gluecookie ) )
388 PagedResultsState *ps = op->o_pagedresults_state;
389 BerElementBuffer berbuf;
390 BerElement *ber = (BerElement *)&berbuf;
391 struct berval cookie = BER_BVC(""), value;
394 for (c = 0; op->o_ctrls[c] != NULL; c++) {
395 if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
399 assert( op->o_ctrls[c] != NULL );
401 ber_init2( ber, NULL, LBER_USE_DER );
402 ber_printf( ber, "{iO}", ps->ps_size, &cookie );
403 ber_flatten2( ber, &value, 0 );
404 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
405 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
406 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
407 value.bv_val, value.bv_len );
410 ps->ps_cookie = (PagedResultsCookie)0;
411 BER_BVZERO( &ps->ps_cookieval );
414 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
415 b0->bd_info = on->on_info->oi_orig;
417 switch (op->ors_scope) {
418 case LDAP_SCOPE_BASE:
419 if ( op->o_bd == b0 )
420 return SLAP_CB_CONTINUE;
422 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
423 if (op->o_bd && op->o_bd->be_search) {
424 rs->sr_err = op->o_bd->be_search( op, rs );
428 case LDAP_SCOPE_ONELEVEL:
429 case LDAP_SCOPE_SUBTREE:
430 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
431 op->o_callback = &cb;
432 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
433 scope0 = op->ors_scope;
434 tlimit0 = op->ors_tlimit;
440 * Execute in reverse order, most specific first
442 for (i = gi->gi_nodes; i >= 0; i--) {
443 if ( i == gi->gi_nodes ) {
447 btmp = gi->gi_n[i].gn_be;
448 pdn = &gi->gi_n[i].gn_pdn;
450 if (!btmp || !btmp->be_search)
452 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
454 if (get_no_subordinate_glue(op) && btmp != b1)
456 /* If we remembered which backend we were on before,
457 * skip down to it now
459 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
460 op->o_conn->c_pagedresults_state.ps_be &&
461 op->o_conn->c_pagedresults_state.ps_be != btmp )
464 if (tlimit0 != SLAP_NO_LIMIT) {
465 op->o_time = slap_get_time();
466 op->ors_tlimit = stoptime - op->o_time;
467 if (op->ors_tlimit <= 0) {
468 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
481 assert( op->o_bd->be_suffix != NULL );
482 assert( op->o_bd->be_nsuffix != NULL );
484 if (scope0 == LDAP_SCOPE_ONELEVEL &&
487 struct berval mdn, mndn;
488 op->ors_scope = LDAP_SCOPE_BASE;
489 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
490 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
491 rs->sr_err = op->o_bd->be_search(op, rs);
492 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
493 gs.err = LDAP_SUCCESS;
495 op->ors_scope = LDAP_SCOPE_ONELEVEL;
496 if ( op->o_req_dn.bv_val == mdn.bv_val )
498 if ( op->o_req_ndn.bv_val == mndn.bv_val )
501 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
502 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
504 rs->sr_err = glue_sub_search( op, rs, b0, on );
506 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
507 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
509 struct berval mdn, mndn;
510 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
511 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
512 rs->sr_err = glue_sub_search( op, rs, b0, on );
513 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
514 gs.err = LDAP_SUCCESS;
516 if ( op->o_req_dn.bv_val == mdn.bv_val )
518 if ( op->o_req_ndn.bv_val == mndn.bv_val )
521 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
522 rs->sr_err = glue_sub_search( op, rs, b0, on );
528 * Add errors that should result in dropping
531 case LDAP_SIZELIMIT_EXCEEDED:
532 case LDAP_TIMELIMIT_EXCEEDED:
533 case LDAP_ADMINLIMIT_EXCEEDED:
534 case LDAP_NO_SUCH_OBJECT:
535 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
536 case LDAP_X_CANNOT_CHAIN:
537 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
541 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
542 PagedResultsState *ps = op->o_pagedresults_state;
544 /* Assume this backend can be forgotten now */
545 op->o_conn->c_pagedresults_state.ps_be = NULL;
547 /* If we have a full page, exit the loop. We may
548 * need to remember this backend so we can continue
549 * from here on a subsequent request.
551 if ( rs->sr_nentries >= ps->ps_size ) {
552 PagedResultsState *cps = &op->o_conn->c_pagedresults_state;
554 /* Don't bother to remember the first backend.
555 * Only remember the last one if there's more state left.
557 if ( op->o_bd != b0 &&
558 ( cps->ps_cookie != NOID
559 || !BER_BVISNULL( &cps->ps_cookieval )
560 || op->o_bd != gi->gi_n[0].gn_be ) )
562 op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
565 /* Check whether the cookie is empty,
566 * and give remaining databases a chance
568 if ( op->o_bd != gi->gi_n[0].gn_be || cps->ps_cookie == NOID ) {
571 for ( c = 0; gs.ctrls[c] != NULL; c++ ) {
572 if ( strcmp( gs.ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 ) {
577 if ( gs.ctrls[c] != NULL ) {
578 BerElementBuffer berbuf;
579 BerElement *ber = (BerElement *)&berbuf;
582 struct berval cookie, value;
584 ber_init2( ber, &gs.ctrls[c]->ldctl_value, LBER_USE_DER );
586 tag = ber_scanf( ber, "{im}", &size, &cookie );
587 assert( tag != LBER_ERROR );
589 if ( BER_BVISEMPTY( &cookie ) && op->o_bd != gi->gi_n[0].gn_be ) {
590 /* delete old, create new cookie with NOID */
591 PagedResultsCookie respcookie = (PagedResultsCookie)NOID;
592 ber_len_t oidlen = strlen( gs.ctrls[c]->ldctl_oid );
593 LDAPControl *newctrl;
595 /* it's next database's turn */
597 op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[gi->gi_nodes - 1].gn_be;
600 op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[(i > 0 ? i - 1: 0)].gn_be;
603 cookie.bv_val = (char *)&respcookie;
604 cookie.bv_len = sizeof( PagedResultsCookie );
606 ber_init2( ber, NULL, LBER_USE_DER );
607 ber_printf( ber, "{iO}", 0, &cookie );
608 ber_flatten2( ber, &value, 0 );
610 newctrl = op->o_tmprealloc( gs.ctrls[c],
611 sizeof(LDAPControl) + oidlen + 1 + value.bv_len + 1,
613 newctrl->ldctl_iscritical = gs.ctrls[c]->ldctl_iscritical;
614 newctrl->ldctl_oid = (char *)&newctrl[1];
615 lutil_strcopy( newctrl->ldctl_oid, gs.ctrls[c]->ldctl_oid );
616 newctrl->ldctl_value.bv_len = value.bv_len;
617 lutil_memcopy( newctrl->ldctl_value.bv_val,
618 value.bv_val, value.bv_len );
620 gs.ctrls[c] = newctrl;
624 } else if ( !BER_BVISEMPTY( &cookie ) && op->o_bd != b0 ) {
625 /* if cookie not empty, it's again this database's turn */
626 op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
634 /* This backend has run out of entries, but more responses
635 * can fit in the page. Fake a reset of the state so the
636 * next backend will start up properly. Only back-[bh]db
637 * and back-sql look at this state info.
639 ps->ps_cookie = (PagedResultsCookie)0;
640 BER_BVZERO( &ps->ps_cookieval );
643 /* change the size of the page in the request
644 * that will be propagated, and reset the cookie */
645 BerElementBuffer berbuf;
646 BerElement *ber = (BerElement *)&berbuf;
647 int size = ps->ps_size - rs->sr_nentries;
648 struct berval cookie = BER_BVC(""), value;
651 for (c = 0; op->o_ctrls[c] != NULL; c++) {
652 if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
656 assert( op->o_ctrls[c] != NULL );
658 ber_init2( ber, NULL, LBER_USE_DER );
659 ber_printf( ber, "{iO}", size, &cookie );
660 ber_flatten2( ber, &value, 0 );
661 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
662 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
663 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
664 value.bv_val, value.bv_len );
674 op->ors_scope = scope0;
675 op->ors_tlimit = tlimit0;
676 op->o_time = starttime;
681 op->o_callback = cb.sc_next;
682 if ( op->o_abandon ) {
683 rs->sr_err = SLAPD_ABANDON;
686 rs->sr_matched = gs.matched;
687 rs->sr_ref = gs.refs;
689 rs->sr_ctrls = gs.ctrls;
691 send_ldap_result( op, rs );
694 op->o_bd->bd_info = bi0;
698 ber_bvarray_free(gs.refs);
700 for (i = gs.nctrls; --i >= 0; ) {
701 op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
703 op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
708 static BackendDB toolDB;
711 glue_tool_entry_open (
716 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
718 /* We don't know which backend to talk to yet, so just
719 * remember the mode and move on...
725 toolDB.bd_info = oi->oi_orig;
729 slap_overinst *on = glue_tool_inst( b0->bd_info );
730 glueinfo *gi = on->on_bi.bi_private;
733 for (i = 0; i < gi->gi_nodes; i++) {
737 dnParent( &gi->gi_n[i].gn_be->be_nsuffix[0], &pdn );
738 bd = select_backend( &pdn, 0 );
743 if ( overlay_is_over( bd ) ) {
744 slap_overinfo *oi = (slap_overinfo *)bd->bd_info;
746 db.bd_info = oi->oi_orig;
750 if ( !bd->bd_info->bi_tool_dn2id_get
751 || !bd->bd_info->bi_tool_entry_open
752 || !bd->bd_info->bi_tool_entry_close )
757 bd->bd_info->bi_tool_entry_open( bd, 0 );
758 id = bd->bd_info->bi_tool_dn2id_get( bd, &gi->gi_n[i].gn_be->be_nsuffix[0] );
759 bd->bd_info->bi_tool_entry_close( bd );
761 Debug( LDAP_DEBUG_ANY,
762 "glue_tool_entry_open: subordinate database suffix entry DN=\"%s\" also present in superior database rooted at DN=\"%s\"\n",
763 gi->gi_n[i].gn_be->be_suffix[0].bv_val, bd->be_suffix[0].bv_val, 0 );
774 glue_tool_entry_close (
780 if (glueBack && glueBack != GLUEBACK_DONE) {
781 if (!glueBack->be_entry_close)
783 rc = glueBack->be_entry_close (glueBack);
788 static slap_overinst *
793 slap_overinfo *oi = (slap_overinfo *)bi;
796 for ( on = oi->oi_list; on; on=on->on_next ) {
797 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
803 /* This function will only be called in tool mode */
809 slap_overinst *on = glue_tool_inst( bi );
810 glueinfo *gi = on->on_bi.bi_private;
811 static int glueOpened = 0;
812 int i, j, same, bsame = 0, rc = 0;
813 ConfigReply cr = {0};
815 if (glueOpened) return 0;
819 /* If we were invoked in tool mode, open all the underlying backends */
820 if (slapMode & SLAP_TOOL_MODE) {
821 for (i = 0; i<gi->gi_nodes; i++) {
823 /* Same bi_open as our main backend? */
824 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
825 on->on_info->oi_orig->bi_open )
828 /* Loop thru the bd_info's and make sure we only
829 * invoke their bi_open functions once each.
831 for ( j = 0; j<i; j++ ) {
832 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
833 gi->gi_n[j].gn_be->bd_info->bi_open ) {
838 /* OK, it's unique and non-NULL, call it. */
839 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
840 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
841 gi->gi_n[i].gn_be->bd_info );
842 /* Let backend.c take care of the rest of startup */
844 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
847 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
848 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
850 } /* other case is impossible */
854 /* This function will only be called in tool mode */
860 static int glueClosed = 0;
863 if (glueClosed) return 0;
867 if (slapMode & SLAP_TOOL_MODE) {
868 rc = backend_shutdown( NULL );
878 AttributeDescription *ad,
883 BackendDB *b0 = op->o_bd;
884 op->o_bd = glue_back_select( b0, dn );
886 if ( op->o_bd->be_fetch ) {
887 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
889 rc = LDAP_UNWILLING_TO_PERFORM;
896 glue_entry_release_rw (
902 BackendDB *b0 = op->o_bd;
905 op->o_bd = glue_back_select (b0, &e->e_nname);
907 if ( op->o_bd->be_release ) {
908 rc = op->o_bd->be_release( op, e, rw );
911 /* FIXME: mimic be_entry_release_rw
912 * when no be_release() available */
921 static struct berval *glue_base;
922 static int glue_scope;
923 static Filter *glue_filter;
926 glue_tool_entry_first (
930 slap_overinst *on = glue_tool_inst( b0->bd_info );
931 glueinfo *gi = on->on_bi.bi_private;
935 /* If we're starting from scratch, start at the most general */
937 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
940 for (i = gi->gi_nodes-1; i >= 0; i--) {
941 if (gi->gi_n[i].gn_be->be_entry_open &&
942 gi->gi_n[i].gn_be->be_entry_first) {
943 glueBack = gi->gi_n[i].gn_be;
949 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
950 glueBack->be_entry_open (glueBack, glueMode) != 0)
953 rc = glueBack->be_entry_first (glueBack);
954 while ( rc == NOID ) {
955 if ( glueBack && glueBack->be_entry_close )
956 glueBack->be_entry_close (glueBack);
957 for (i=0; i<gi->gi_nodes; i++) {
958 if (gi->gi_n[i].gn_be == glueBack)
962 glueBack = GLUEBACK_DONE;
965 glueBack = gi->gi_n[i-1].gn_be;
966 rc = glue_tool_entry_first (b0);
967 if ( glueBack == GLUEBACK_DONE ) {
976 glue_tool_entry_first_x (
983 slap_overinst *on = glue_tool_inst( b0->bd_info );
984 glueinfo *gi = on->on_bi.bi_private;
992 /* If we're starting from scratch, start at the most general */
994 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
997 for (i = gi->gi_nodes-1; i >= 0; i--) {
998 if (gi->gi_n[i].gn_be->be_entry_open &&
999 gi->gi_n[i].gn_be->be_entry_first_x)
1001 glueBack = gi->gi_n[i].gn_be;
1007 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
1008 glueBack->be_entry_open (glueBack, glueMode) != 0)
1011 rc = glueBack->be_entry_first_x (glueBack,
1012 glue_base, glue_scope, glue_filter);
1013 while ( rc == NOID ) {
1014 if ( glueBack && glueBack->be_entry_close )
1015 glueBack->be_entry_close (glueBack);
1016 for (i=0; i<gi->gi_nodes; i++) {
1017 if (gi->gi_n[i].gn_be == glueBack)
1021 glueBack = GLUEBACK_DONE;
1024 glueBack = gi->gi_n[i-1].gn_be;
1025 rc = glue_tool_entry_first_x (b0,
1026 glue_base, glue_scope, glue_filter);
1027 if ( glueBack == GLUEBACK_DONE ) {
1036 glue_tool_entry_next (
1040 slap_overinst *on = glue_tool_inst( b0->bd_info );
1041 glueinfo *gi = on->on_bi.bi_private;
1045 if (!glueBack || !glueBack->be_entry_next)
1048 rc = glueBack->be_entry_next (glueBack);
1050 /* If we ran out of entries in one database, move on to the next */
1051 while (rc == NOID) {
1052 if ( glueBack && glueBack->be_entry_close )
1053 glueBack->be_entry_close (glueBack);
1054 for (i=0; i<gi->gi_nodes; i++) {
1055 if (gi->gi_n[i].gn_be == glueBack)
1059 glueBack = GLUEBACK_DONE;
1062 glueBack = gi->gi_n[i-1].gn_be;
1063 if ( glue_base || glue_filter ) {
1064 /* using entry_first_x() */
1065 rc = glue_tool_entry_first_x (b0,
1066 glue_base, glue_scope, glue_filter);
1069 /* using entry_first() */
1070 rc = glue_tool_entry_first (b0);
1072 if ( glueBack == GLUEBACK_DONE ) {
1081 glue_tool_dn2id_get (
1090 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1091 be = glue_back_select (&b2, dn);
1092 if ( be == &b2 ) be = &toolDB;
1094 if (!be->be_dn2id_get)
1098 if ( be->be_entry_open ) {
1099 rc = be->be_entry_open (be, glueMode);
1104 } else if (be != glueBack) {
1105 /* If this entry belongs in a different branch than the
1106 * previous one, close the current database and open the
1109 if ( glueBack->be_entry_close ) {
1110 glueBack->be_entry_close (glueBack);
1112 if ( be->be_entry_open ) {
1113 rc = be->be_entry_open (be, glueMode);
1120 return be->be_dn2id_get (be, dn);
1124 glue_tool_entry_get (
1129 if (!glueBack || !glueBack->be_entry_get)
1132 return glueBack->be_entry_get (glueBack, id);
1136 glue_tool_entry_put (
1146 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1147 be = glue_back_select (&b2, &e->e_nname);
1148 if ( be == &b2 ) be = &toolDB;
1150 if (!be->be_entry_put)
1154 if ( be->be_entry_open ) {
1155 rc = be->be_entry_open (be, glueMode);
1160 } else if (be != glueBack) {
1161 /* If this entry belongs in a different branch than the
1162 * previous one, close the current database and open the
1165 if ( glueBack->be_entry_close ) {
1166 glueBack->be_entry_close (glueBack);
1168 if ( be->be_entry_open ) {
1169 rc = be->be_entry_open (be, glueMode);
1176 return be->be_entry_put (be, e, text);
1180 glue_tool_entry_modify (
1186 if (!glueBack || !glueBack->be_entry_modify)
1189 return glueBack->be_entry_modify (glueBack, e, text);
1193 glue_tool_entry_reindex (
1196 AttributeDescription **adv
1199 if (!glueBack || !glueBack->be_entry_reindex)
1202 return glueBack->be_entry_reindex (glueBack, id, adv);
1210 slap_overinst *on = glue_tool_inst( b0->bd_info );
1211 glueinfo *gi = on->on_bi.bi_private;
1212 BackendInfo *bi = b0->bd_info;
1215 /* just sync everyone */
1216 for (i = 0; i<gi->gi_nodes; i++)
1217 if (gi->gi_n[i].gn_be->be_sync)
1218 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1219 b0->bd_info = on->on_info->oi_orig;
1226 typedef struct glue_Addrec {
1227 struct glue_Addrec *ga_next;
1231 /* List of added subordinates */
1232 static glue_Addrec *ga_list;
1233 static int ga_adding;
1241 slap_overinst *on = (slap_overinst *)be->bd_info;
1242 slap_overinfo *oi = on->on_info;
1243 BackendInfo *bi = oi->oi_orig;
1246 if ( SLAP_GLUE_SUBORDINATE( be )) {
1247 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1248 "cannot have glue overlay!\n",
1249 be->be_suffix[0].bv_val, 0, 0 );
1253 gi = ch_calloc( 1, sizeof(glueinfo));
1254 on->on_bi.bi_private = gi;
1255 dnParent( be->be_nsuffix, &gi->gi_pdn );
1257 /* Currently the overlay framework doesn't handle these entry points
1258 * but we need them....
1260 oi->oi_bi.bi_open = glue_open;
1261 oi->oi_bi.bi_close = glue_close;
1263 /* Only advertise these if the root DB supports them */
1264 if ( bi->bi_tool_entry_open )
1265 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1266 if ( bi->bi_tool_entry_close )
1267 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1268 if ( bi->bi_tool_entry_first )
1269 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1270 /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1271 if ( bi->bi_tool_entry_first_x )
1272 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1273 if ( bi->bi_tool_entry_next )
1274 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1275 if ( bi->bi_tool_entry_get )
1276 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1277 if ( bi->bi_tool_dn2id_get )
1278 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1279 if ( bi->bi_tool_entry_put )
1280 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1281 if ( bi->bi_tool_entry_reindex )
1282 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1283 if ( bi->bi_tool_entry_modify )
1284 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1285 if ( bi->bi_tool_sync )
1286 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1288 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1291 be->bd_info = (BackendInfo *)oi;
1292 glue_sub_attach( 1 );
1304 slap_overinst *on = (slap_overinst *)be->bd_info;
1305 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
1308 return SLAP_CB_CONTINUE;
1317 slap_overinst *on = (slap_overinst *)be->bd_info;
1319 on->on_info->oi_bi.bi_db_close = 0;
1324 glue_sub_del( BackendDB *b0 )
1329 /* Find the top backend for this subordinate */
1331 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1337 if ( SLAP_GLUE_SUBORDINATE( be ))
1339 if ( !SLAP_GLUE_INSTANCE( be ))
1341 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1344 /* OK, got the right backend, find the overlay */
1345 oi = (slap_overinfo *)be->bd_info;
1346 for ( on=oi->oi_list; on; on=on->on_next ) {
1347 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1350 assert( on != NULL );
1351 gi = on->on_bi.bi_private;
1352 for ( i=0; i < gi->gi_nodes; i++ ) {
1353 if ( gi->gi_n[i].gn_be == b0 ) {
1356 for (j=i+1; j < gi->gi_nodes; j++)
1357 gi->gi_n[j-1] = gi->gi_n[j];
1364 rc = LDAP_NO_SUCH_OBJECT;
1370 /* Attach all the subordinate backends to their superior */
1372 glue_sub_attach( int online )
1374 glue_Addrec *ga, *gnext = NULL;
1382 /* For all the subordinate backends */
1383 for ( ga=ga_list; ga != NULL; ga = gnext ) {
1386 gnext = ga->ga_next;
1388 /* Find the top backend for this subordinate */
1390 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1395 if ( SLAP_GLUE_SUBORDINATE( be ))
1397 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1400 /* If it's not already configured, set up the overlay */
1401 if ( !SLAP_GLUE_INSTANCE( be )) {
1402 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1406 /* Find the overlay instance */
1407 oi = (slap_overinfo *)be->bd_info;
1408 for ( on=oi->oi_list; on; on=on->on_next ) {
1409 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1412 assert( on != NULL );
1413 gi = on->on_bi.bi_private;
1414 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1415 gi->gi_nodes * sizeof(gluenode));
1416 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1417 dnParent( &ga->ga_be->be_nsuffix[0],
1418 &gi->gi_n[gi->gi_nodes].gn_pdn );
1420 on->on_bi.bi_private = gi;
1421 ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1425 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1426 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1427 /* allow this for now, assume a superior will
1435 rc = LDAP_NO_SUCH_OBJECT;
1449 glue_sub_add( BackendDB *be, int advert, int online )
1454 if ( overlay_is_inst( be, "glue" )) {
1455 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1456 "cannot be a subordinate!\n",
1457 be->be_suffix[0].bv_val, 0, 0 );
1460 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1462 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1464 ga = ch_malloc( sizeof( glue_Addrec ));
1465 ga->ga_next = ga_list;
1470 rc = glue_sub_attach( online );
1476 glue_access_allowed(
1479 AttributeDescription *desc,
1481 slap_access_t access,
1482 AccessControlState *state,
1483 slap_mask_t *maskp )
1485 BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
1488 if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
1489 return SLAP_CB_CONTINUE;
1493 rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
1501 glue.on_bi.bi_type = "glue";
1503 glue.on_bi.bi_db_init = glue_db_init;
1504 glue.on_bi.bi_db_close = glue_db_close;
1505 glue.on_bi.bi_db_destroy = glue_db_destroy;
1507 glue.on_bi.bi_op_search = glue_op_search;
1508 glue.on_bi.bi_op_modify = glue_op_func;
1509 glue.on_bi.bi_op_modrdn = glue_op_func;
1510 glue.on_bi.bi_op_add = glue_op_func;
1511 glue.on_bi.bi_op_delete = glue_op_func;
1512 glue.on_bi.bi_extended = glue_op_func;
1514 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1515 glue.on_bi.bi_chk_controls = glue_chk_controls;
1516 glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1517 glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1518 glue.on_bi.bi_access_allowed = glue_access_allowed;
1520 glue.on_response = glue_response;
1522 return overlay_register( &glue );