1 /* backglue.c - backend glue */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2014 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, *bi1;
229 slap_operation_t which = op_bind;
232 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
234 /* If we're on the master backend, let overlay framework handle it */
235 if ( op->o_bd == b0 )
236 return SLAP_CB_CONTINUE;
238 b0->bd_info = on->on_info->oi_orig;
241 case LDAP_REQ_ADD: which = op_add; break;
242 case LDAP_REQ_DELETE: which = op_delete; break;
243 case LDAP_REQ_MODIFY: which = op_modify; break;
244 case LDAP_REQ_MODRDN: which = op_modrdn; break;
245 case LDAP_REQ_EXTENDED: which = op_extended; break;
246 default: assert( 0 ); break;
249 bi1 = op->o_bd->bd_info;
250 rc = (&bi1->bi_op_bind)[ which ] ?
251 (&bi1->bi_op_bind)[ which ]( op, rs ) : SLAP_CB_BYPASS;
254 op->o_bd->bd_info = bi0;
259 glue_op_abandon( Operation *op, SlapReply *rs )
261 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
262 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
263 BackendDB *b0 = op->o_bd;
264 BackendInfo *bi0 = op->o_bd->bd_info;
267 b0->bd_info = on->on_info->oi_orig;
269 for (i = gi->gi_nodes-1; i >= 0; i--) {
270 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
271 op->o_bd = gi->gi_n[i].gn_be;
272 if ( op->o_bd == b0 )
274 if ( op->o_bd->bd_info->bi_op_abandon )
275 op->o_bd->bd_info->bi_op_abandon( op, rs );
278 op->o_bd->bd_info = bi0;
279 return SLAP_CB_CONTINUE;
283 glue_response ( Operation *op, SlapReply *rs )
285 BackendDB *be = op->o_bd;
286 be = glue_back_select (op->o_bd, &op->o_req_ndn);
288 /* If we're on the master backend, let overlay framework handle it.
289 * Otherwise, bail out.
291 return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
295 glue_chk_referrals ( Operation *op, SlapReply *rs )
297 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
298 BackendDB *b0 = op->o_bd;
299 BackendInfo *bi0 = op->o_bd->bd_info;
302 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
303 if ( op->o_bd == b0 )
304 return SLAP_CB_CONTINUE;
306 b0->bd_info = on->on_info->oi_orig;
308 if ( op->o_bd->bd_info->bi_chk_referrals )
309 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
311 rc = SLAP_CB_CONTINUE;
314 op->o_bd->bd_info = bi0;
319 glue_chk_controls ( Operation *op, SlapReply *rs )
321 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
322 BackendDB *b0 = op->o_bd;
323 BackendInfo *bi0 = op->o_bd->bd_info;
324 int rc = SLAP_CB_CONTINUE;
326 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
327 if ( op->o_bd == b0 )
328 return SLAP_CB_CONTINUE;
330 b0->bd_info = on->on_info->oi_orig;
332 /* if the subordinate database has overlays, the bi_chk_controls()
333 * hook is actually over_aux_chk_controls(); in case it actually
334 * wraps a missing hok, we need to mimic the behavior
335 * of the frontend applied to that database */
336 if ( op->o_bd->bd_info->bi_chk_controls ) {
337 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
341 if ( rc == SLAP_CB_CONTINUE ) {
342 rc = backend_check_controls( op, rs );
346 op->o_bd->bd_info = bi0;
350 /* ITS#4615 - overlays configured above the glue overlay should be
351 * invoked for the entire glued tree. Overlays configured below the
352 * glue overlay should only be invoked on the master backend.
353 * So, if we're searching on any subordinates, we need to force the
354 * current overlay chain to stop processing, without stopping the
355 * overall callback flow.
358 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
361 /* Process any overlays on the master backend */
362 if ( op->o_bd == b0 && on->on_next ) {
363 BackendInfo *bi = op->o_bd->bd_info;
364 int rc = SLAP_CB_CONTINUE;
365 for ( on=on->on_next; on; on=on->on_next ) {
366 op->o_bd->bd_info = (BackendInfo *)on;
367 if ( on->on_bi.bi_op_search ) {
368 rc = on->on_bi.bi_op_search( op, rs );
369 if ( rc != SLAP_CB_CONTINUE )
373 op->o_bd->bd_info = bi;
374 if ( rc != SLAP_CB_CONTINUE )
377 return op->o_bd->be_search( op, rs );
380 static const ID glueID = NOID;
381 static const struct berval gluecookie = { sizeof( glueID ), (char *)&glueID };
384 glue_op_search ( Operation *op, SlapReply *rs )
386 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
387 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
388 BackendDB *b0 = op->o_bd;
389 BackendDB *b1 = NULL, *btmp;
390 BackendInfo *bi0 = op->o_bd->bd_info;
392 long stoptime = 0, starttime;
393 glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
394 slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
396 struct berval dn, ndn, *pdn;
400 cb.sc_next = op->o_callback;
402 starttime = op->o_time;
403 stoptime = slap_get_time () + op->ors_tlimit;
405 /* reset dummy cookie used to keep paged results going across databases */
406 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED
407 && bvmatch( &((PagedResultsState *)op->o_pagedresults_state)->ps_cookieval, &gluecookie ) )
409 PagedResultsState *ps = op->o_pagedresults_state;
410 BerElementBuffer berbuf;
411 BerElement *ber = (BerElement *)&berbuf;
412 struct berval cookie = BER_BVC(""), value;
415 for (c = 0; op->o_ctrls[c] != NULL; c++) {
416 if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
420 assert( op->o_ctrls[c] != NULL );
422 ber_init2( ber, NULL, LBER_USE_DER );
423 ber_printf( ber, "{iO}", ps->ps_size, &cookie );
424 ber_flatten2( ber, &value, 0 );
425 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
426 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
427 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
428 value.bv_val, value.bv_len );
431 ps->ps_cookie = (PagedResultsCookie)0;
432 BER_BVZERO( &ps->ps_cookieval );
435 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
436 b0->bd_info = on->on_info->oi_orig;
438 switch (op->ors_scope) {
439 case LDAP_SCOPE_BASE:
440 if ( op->o_bd == b0 )
441 return SLAP_CB_CONTINUE;
443 if (op->o_bd && op->o_bd->be_search) {
444 rs->sr_err = op->o_bd->be_search( op, rs );
446 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
450 case LDAP_SCOPE_ONELEVEL:
451 case LDAP_SCOPE_SUBTREE:
452 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
453 op->o_callback = &cb;
454 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
455 scope0 = op->ors_scope;
456 tlimit0 = op->ors_tlimit;
462 * Execute in reverse order, most specific first
464 for (i = gi->gi_nodes; i >= 0; i--) {
465 if ( i == gi->gi_nodes ) {
469 btmp = gi->gi_n[i].gn_be;
470 pdn = &gi->gi_n[i].gn_pdn;
472 if (!btmp || !btmp->be_search)
474 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
476 if (get_no_subordinate_glue(op) && btmp != b1)
478 /* If we remembered which backend we were on before,
479 * skip down to it now
481 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
482 op->o_conn->c_pagedresults_state.ps_be &&
483 op->o_conn->c_pagedresults_state.ps_be != btmp )
486 if (tlimit0 != SLAP_NO_LIMIT) {
487 op->o_time = slap_get_time();
488 op->ors_tlimit = stoptime - op->o_time;
489 if (op->ors_tlimit <= 0) {
490 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
503 assert( op->o_bd->be_suffix != NULL );
504 assert( op->o_bd->be_nsuffix != NULL );
506 if (scope0 == LDAP_SCOPE_ONELEVEL &&
509 struct berval mdn, mndn;
510 op->ors_scope = LDAP_SCOPE_BASE;
511 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
512 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
513 rs->sr_err = op->o_bd->be_search(op, rs);
514 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
515 gs.err = LDAP_SUCCESS;
517 op->ors_scope = LDAP_SCOPE_ONELEVEL;
518 if ( op->o_req_dn.bv_val == mdn.bv_val )
520 if ( op->o_req_ndn.bv_val == mndn.bv_val )
523 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
524 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
526 rs->sr_err = glue_sub_search( op, rs, b0, on );
528 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
529 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
531 struct berval mdn, mndn;
532 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
533 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
534 rs->sr_err = glue_sub_search( op, rs, b0, on );
535 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
536 gs.err = LDAP_SUCCESS;
538 if ( op->o_req_dn.bv_val == mdn.bv_val )
540 if ( op->o_req_ndn.bv_val == mndn.bv_val )
543 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
544 rs->sr_err = glue_sub_search( op, rs, b0, on );
550 * Add errors that should result in dropping
553 case LDAP_SIZELIMIT_EXCEEDED:
554 case LDAP_TIMELIMIT_EXCEEDED:
555 case LDAP_ADMINLIMIT_EXCEEDED:
556 case LDAP_NO_SUCH_OBJECT:
557 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
558 case LDAP_X_CANNOT_CHAIN:
559 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
563 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
564 PagedResultsState *ps = op->o_pagedresults_state;
566 /* Assume this backend can be forgotten now */
567 op->o_conn->c_pagedresults_state.ps_be = NULL;
569 /* If we have a full page, exit the loop. We may
570 * need to remember this backend so we can continue
571 * from here on a subsequent request.
573 if ( rs->sr_nentries >= ps->ps_size ) {
574 PagedResultsState *cps = &op->o_conn->c_pagedresults_state;
576 /* Don't bother to remember the first backend.
577 * Only remember the last one if there's more state left.
579 if ( op->o_bd != b0 &&
580 ( cps->ps_cookie != NOID
581 || !BER_BVISNULL( &cps->ps_cookieval )
582 || op->o_bd != gi->gi_n[0].gn_be ) )
584 op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
587 /* Check whether the cookie is empty,
588 * and give remaining databases a chance
590 if ( op->o_bd != gi->gi_n[0].gn_be || cps->ps_cookie == NOID ) {
593 for ( c = 0; gs.ctrls[c] != NULL; c++ ) {
594 if ( strcmp( gs.ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 ) {
599 if ( gs.ctrls[c] != NULL ) {
600 BerElementBuffer berbuf;
601 BerElement *ber = (BerElement *)&berbuf;
604 struct berval cookie, value;
606 ber_init2( ber, &gs.ctrls[c]->ldctl_value, LBER_USE_DER );
608 tag = ber_scanf( ber, "{im}", &size, &cookie );
609 assert( tag != LBER_ERROR );
611 if ( BER_BVISEMPTY( &cookie ) && op->o_bd != gi->gi_n[0].gn_be ) {
612 /* delete old, create new cookie with NOID */
613 PagedResultsCookie respcookie = (PagedResultsCookie)NOID;
614 ber_len_t oidlen = strlen( gs.ctrls[c]->ldctl_oid );
615 LDAPControl *newctrl;
617 /* it's next database's turn */
619 op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[gi->gi_nodes - 1].gn_be;
622 op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[(i > 0 ? i - 1: 0)].gn_be;
625 cookie.bv_val = (char *)&respcookie;
626 cookie.bv_len = sizeof( PagedResultsCookie );
628 ber_init2( ber, NULL, LBER_USE_DER );
629 ber_printf( ber, "{iO}", 0, &cookie );
630 ber_flatten2( ber, &value, 0 );
632 newctrl = op->o_tmprealloc( gs.ctrls[c],
633 sizeof(LDAPControl) + oidlen + 1 + value.bv_len + 1,
635 newctrl->ldctl_iscritical = gs.ctrls[c]->ldctl_iscritical;
636 newctrl->ldctl_oid = (char *)&newctrl[1];
637 lutil_strcopy( newctrl->ldctl_oid, gs.ctrls[c]->ldctl_oid );
638 newctrl->ldctl_value.bv_len = value.bv_len;
639 lutil_memcopy( newctrl->ldctl_value.bv_val,
640 value.bv_val, value.bv_len );
642 gs.ctrls[c] = newctrl;
646 } else if ( !BER_BVISEMPTY( &cookie ) && op->o_bd != b0 ) {
647 /* if cookie not empty, it's again this database's turn */
648 op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
656 /* This backend has run out of entries, but more responses
657 * can fit in the page. Fake a reset of the state so the
658 * next backend will start up properly. Only back-[bh]db
659 * and back-sql look at this state info.
661 ps->ps_cookie = (PagedResultsCookie)0;
662 BER_BVZERO( &ps->ps_cookieval );
665 /* change the size of the page in the request
666 * that will be propagated, and reset the cookie */
667 BerElementBuffer berbuf;
668 BerElement *ber = (BerElement *)&berbuf;
669 int size = ps->ps_size - rs->sr_nentries;
670 struct berval cookie = BER_BVC(""), value;
673 for (c = 0; op->o_ctrls[c] != NULL; c++) {
674 if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
678 assert( op->o_ctrls[c] != NULL );
680 ber_init2( ber, NULL, LBER_USE_DER );
681 ber_printf( ber, "{iO}", size, &cookie );
682 ber_flatten2( ber, &value, 0 );
683 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
684 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
685 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
686 value.bv_val, value.bv_len );
696 op->ors_scope = scope0;
697 op->ors_tlimit = tlimit0;
698 op->o_time = starttime;
703 op->o_callback = cb.sc_next;
704 if ( op->o_abandon ) {
705 rs->sr_err = SLAPD_ABANDON;
708 rs->sr_matched = gs.matched;
709 rs->sr_ref = gs.refs;
711 rs->sr_ctrls = gs.ctrls;
713 send_ldap_result( op, rs );
716 op->o_bd->bd_info = bi0;
720 ber_bvarray_free(gs.refs);
722 for (i = gs.nctrls; --i >= 0; ) {
723 op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
725 op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
730 static BackendDB toolDB;
733 glue_tool_entry_open (
738 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
740 /* We don't know which backend to talk to yet, so just
741 * remember the mode and move on...
747 toolDB.bd_info = oi->oi_orig;
751 slap_overinst *on = glue_tool_inst( b0->bd_info );
752 glueinfo *gi = on->on_bi.bi_private;
755 for (i = 0; i < gi->gi_nodes; i++) {
759 dnParent( &gi->gi_n[i].gn_be->be_nsuffix[0], &pdn );
760 bd = select_backend( &pdn, 0 );
765 if ( overlay_is_over( bd ) ) {
766 slap_overinfo *oi = (slap_overinfo *)bd->bd_info;
768 db.bd_info = oi->oi_orig;
772 if ( !bd->bd_info->bi_tool_dn2id_get
773 || !bd->bd_info->bi_tool_entry_open
774 || !bd->bd_info->bi_tool_entry_close )
779 bd->bd_info->bi_tool_entry_open( bd, 0 );
780 id = bd->bd_info->bi_tool_dn2id_get( bd, &gi->gi_n[i].gn_be->be_nsuffix[0] );
781 bd->bd_info->bi_tool_entry_close( bd );
783 Debug( LDAP_DEBUG_ANY,
784 "glue_tool_entry_open: subordinate database suffix entry DN=\"%s\" also present in superior database rooted at DN=\"%s\"\n",
785 gi->gi_n[i].gn_be->be_suffix[0].bv_val, bd->be_suffix[0].bv_val, 0 );
796 glue_tool_entry_close (
802 if (glueBack && glueBack != GLUEBACK_DONE) {
803 if (!glueBack->be_entry_close)
805 rc = glueBack->be_entry_close (glueBack);
810 static slap_overinst *
815 slap_overinfo *oi = (slap_overinfo *)bi;
818 for ( on = oi->oi_list; on; on=on->on_next ) {
819 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
825 /* This function will only be called in tool mode */
831 slap_overinst *on = glue_tool_inst( bi );
832 glueinfo *gi = on->on_bi.bi_private;
833 static int glueOpened = 0;
834 int i, j, same, bsame = 0, rc = 0;
835 ConfigReply cr = {0};
837 if (glueOpened) return 0;
841 /* If we were invoked in tool mode, open all the underlying backends */
842 if (slapMode & SLAP_TOOL_MODE) {
843 for (i = 0; i<gi->gi_nodes; i++) {
845 /* Same bi_open as our main backend? */
846 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
847 on->on_info->oi_orig->bi_open )
850 /* Loop thru the bd_info's and make sure we only
851 * invoke their bi_open functions once each.
853 for ( j = 0; j<i; j++ ) {
854 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
855 gi->gi_n[j].gn_be->bd_info->bi_open ) {
860 /* OK, it's unique and non-NULL, call it. */
861 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
862 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
863 gi->gi_n[i].gn_be->bd_info );
864 /* Let backend.c take care of the rest of startup */
866 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
869 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
870 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
872 } /* other case is impossible */
876 /* This function will only be called in tool mode */
882 static int glueClosed = 0;
885 if (glueClosed) return 0;
889 if (slapMode & SLAP_TOOL_MODE) {
890 rc = backend_shutdown( NULL );
900 AttributeDescription *ad,
905 BackendDB *b0 = op->o_bd;
906 op->o_bd = glue_back_select( b0, dn );
908 if ( op->o_bd->be_fetch ) {
909 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
911 rc = LDAP_UNWILLING_TO_PERFORM;
918 glue_entry_release_rw (
924 BackendDB *b0 = op->o_bd;
927 op->o_bd = glue_back_select (b0, &e->e_nname);
929 if ( op->o_bd->be_release ) {
930 rc = op->o_bd->be_release( op, e, rw );
933 /* FIXME: mimic be_entry_release_rw
934 * when no be_release() available */
943 static struct berval *glue_base;
944 static int glue_scope;
945 static Filter *glue_filter;
948 glue_tool_entry_first (
952 slap_overinst *on = glue_tool_inst( b0->bd_info );
953 glueinfo *gi = on->on_bi.bi_private;
957 /* If we're starting from scratch, start at the most general */
959 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
962 for (i = gi->gi_nodes-1; i >= 0; i--) {
963 if (gi->gi_n[i].gn_be->be_entry_open &&
964 gi->gi_n[i].gn_be->be_entry_first) {
965 glueBack = gi->gi_n[i].gn_be;
971 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
972 glueBack->be_entry_open (glueBack, glueMode) != 0)
975 rc = glueBack->be_entry_first (glueBack);
976 while ( rc == NOID ) {
977 if ( glueBack && glueBack->be_entry_close )
978 glueBack->be_entry_close (glueBack);
979 for (i=0; i<gi->gi_nodes; i++) {
980 if (gi->gi_n[i].gn_be == glueBack)
984 glueBack = GLUEBACK_DONE;
987 glueBack = gi->gi_n[i-1].gn_be;
988 rc = glue_tool_entry_first (b0);
989 if ( glueBack == GLUEBACK_DONE ) {
998 glue_tool_entry_first_x (
1000 struct berval *base,
1005 slap_overinst *on = glue_tool_inst( b0->bd_info );
1006 glueinfo *gi = on->on_bi.bi_private;
1014 /* If we're starting from scratch, start at the most general */
1016 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
1019 for (i = gi->gi_nodes-1; i >= 0; i--) {
1020 if (gi->gi_n[i].gn_be->be_entry_open &&
1021 gi->gi_n[i].gn_be->be_entry_first_x)
1023 glueBack = gi->gi_n[i].gn_be;
1029 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
1030 glueBack->be_entry_open (glueBack, glueMode) != 0)
1033 rc = glueBack->be_entry_first_x (glueBack,
1034 glue_base, glue_scope, glue_filter);
1035 while ( rc == NOID ) {
1036 if ( glueBack && glueBack->be_entry_close )
1037 glueBack->be_entry_close (glueBack);
1038 for (i=0; i<gi->gi_nodes; i++) {
1039 if (gi->gi_n[i].gn_be == glueBack)
1043 glueBack = GLUEBACK_DONE;
1046 glueBack = gi->gi_n[i-1].gn_be;
1047 rc = glue_tool_entry_first_x (b0,
1048 glue_base, glue_scope, glue_filter);
1049 if ( glueBack == GLUEBACK_DONE ) {
1058 glue_tool_entry_next (
1062 slap_overinst *on = glue_tool_inst( b0->bd_info );
1063 glueinfo *gi = on->on_bi.bi_private;
1067 if (!glueBack || !glueBack->be_entry_next)
1070 rc = glueBack->be_entry_next (glueBack);
1072 /* If we ran out of entries in one database, move on to the next */
1073 while (rc == NOID) {
1074 if ( glueBack && glueBack->be_entry_close )
1075 glueBack->be_entry_close (glueBack);
1076 for (i=0; i<gi->gi_nodes; i++) {
1077 if (gi->gi_n[i].gn_be == glueBack)
1081 glueBack = GLUEBACK_DONE;
1084 glueBack = gi->gi_n[i-1].gn_be;
1085 if ( glue_base || glue_filter ) {
1086 /* using entry_first_x() */
1087 rc = glue_tool_entry_first_x (b0,
1088 glue_base, glue_scope, glue_filter);
1091 /* using entry_first() */
1092 rc = glue_tool_entry_first (b0);
1094 if ( glueBack == GLUEBACK_DONE ) {
1103 glue_tool_dn2id_get (
1112 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1113 be = glue_back_select (&b2, dn);
1114 if ( be == &b2 ) be = &toolDB;
1116 if (!be->be_dn2id_get)
1120 if ( be->be_entry_open ) {
1121 rc = be->be_entry_open (be, glueMode);
1126 } else if (be != glueBack) {
1127 /* If this entry belongs in a different branch than the
1128 * previous one, close the current database and open the
1131 if ( glueBack->be_entry_close ) {
1132 glueBack->be_entry_close (glueBack);
1134 if ( be->be_entry_open ) {
1135 rc = be->be_entry_open (be, glueMode);
1142 return be->be_dn2id_get (be, dn);
1146 glue_tool_entry_get (
1151 if (!glueBack || !glueBack->be_entry_get)
1154 return glueBack->be_entry_get (glueBack, id);
1158 glue_tool_entry_put (
1168 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1169 be = glue_back_select (&b2, &e->e_nname);
1170 if ( be == &b2 ) be = &toolDB;
1172 if (!be->be_entry_put)
1176 if ( be->be_entry_open ) {
1177 rc = be->be_entry_open (be, glueMode);
1182 } else if (be != glueBack) {
1183 /* If this entry belongs in a different branch than the
1184 * previous one, close the current database and open the
1187 if ( glueBack->be_entry_close ) {
1188 glueBack->be_entry_close (glueBack);
1190 if ( be->be_entry_open ) {
1191 rc = be->be_entry_open (be, glueMode);
1198 return be->be_entry_put (be, e, text);
1202 glue_tool_entry_modify (
1208 if (!glueBack || !glueBack->be_entry_modify)
1211 return glueBack->be_entry_modify (glueBack, e, text);
1215 glue_tool_entry_reindex (
1218 AttributeDescription **adv
1221 if (!glueBack || !glueBack->be_entry_reindex)
1224 return glueBack->be_entry_reindex (glueBack, id, adv);
1232 slap_overinst *on = glue_tool_inst( b0->bd_info );
1233 glueinfo *gi = on->on_bi.bi_private;
1234 BackendInfo *bi = b0->bd_info;
1237 /* just sync everyone */
1238 for (i = 0; i<gi->gi_nodes; i++)
1239 if (gi->gi_n[i].gn_be->be_sync)
1240 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1241 b0->bd_info = on->on_info->oi_orig;
1248 typedef struct glue_Addrec {
1249 struct glue_Addrec *ga_next;
1253 /* List of added subordinates */
1254 static glue_Addrec *ga_list;
1255 static int ga_adding;
1263 slap_overinst *on = (slap_overinst *)be->bd_info;
1264 slap_overinfo *oi = on->on_info;
1265 BackendInfo *bi = oi->oi_orig;
1268 if ( SLAP_GLUE_SUBORDINATE( be )) {
1269 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1270 "cannot have glue overlay!\n",
1271 be->be_suffix[0].bv_val, 0, 0 );
1275 gi = ch_calloc( 1, sizeof(glueinfo));
1276 on->on_bi.bi_private = gi;
1277 dnParent( be->be_nsuffix, &gi->gi_pdn );
1279 /* Currently the overlay framework doesn't handle these entry points
1280 * but we need them....
1282 oi->oi_bi.bi_open = glue_open;
1283 oi->oi_bi.bi_close = glue_close;
1285 /* Only advertise these if the root DB supports them */
1286 if ( bi->bi_tool_entry_open )
1287 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1288 if ( bi->bi_tool_entry_close )
1289 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1290 if ( bi->bi_tool_entry_first )
1291 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1292 /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1293 if ( bi->bi_tool_entry_first_x )
1294 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1295 if ( bi->bi_tool_entry_next )
1296 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1297 if ( bi->bi_tool_entry_get )
1298 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1299 if ( bi->bi_tool_dn2id_get )
1300 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1301 if ( bi->bi_tool_entry_put )
1302 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1303 if ( bi->bi_tool_entry_reindex )
1304 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1305 if ( bi->bi_tool_entry_modify )
1306 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1307 if ( bi->bi_tool_sync )
1308 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1310 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1313 be->bd_info = (BackendInfo *)oi;
1314 glue_sub_attach( 1 );
1326 slap_overinst *on = (slap_overinst *)be->bd_info;
1327 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
1330 return SLAP_CB_CONTINUE;
1339 slap_overinst *on = (slap_overinst *)be->bd_info;
1341 on->on_info->oi_bi.bi_db_close = 0;
1346 glue_sub_del( BackendDB *b0 )
1351 /* Find the top backend for this subordinate */
1353 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1359 if ( SLAP_GLUE_SUBORDINATE( be ))
1361 if ( !SLAP_GLUE_INSTANCE( be ))
1363 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1366 /* OK, got the right backend, find the overlay */
1367 oi = (slap_overinfo *)be->bd_info;
1368 for ( on=oi->oi_list; on; on=on->on_next ) {
1369 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1372 assert( on != NULL );
1373 gi = on->on_bi.bi_private;
1374 for ( i=0; i < gi->gi_nodes; i++ ) {
1375 if ( gi->gi_n[i].gn_be == b0 ) {
1378 for (j=i+1; j < gi->gi_nodes; j++)
1379 gi->gi_n[j-1] = gi->gi_n[j];
1386 rc = LDAP_NO_SUCH_OBJECT;
1392 /* Attach all the subordinate backends to their superior */
1394 glue_sub_attach( int online )
1396 glue_Addrec *ga, *gnext = NULL;
1404 /* For all the subordinate backends */
1405 for ( ga=ga_list; ga != NULL; ga = gnext ) {
1408 gnext = ga->ga_next;
1410 /* Find the top backend for this subordinate */
1412 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1417 if ( SLAP_GLUE_SUBORDINATE( be ))
1419 if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1422 /* If it's not already configured, set up the overlay */
1423 if ( !SLAP_GLUE_INSTANCE( be )) {
1424 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1428 /* Find the overlay instance */
1429 oi = (slap_overinfo *)be->bd_info;
1430 for ( on=oi->oi_list; on; on=on->on_next ) {
1431 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1434 assert( on != NULL );
1435 gi = on->on_bi.bi_private;
1436 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1437 gi->gi_nodes * sizeof(gluenode));
1438 gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1439 dnParent( &ga->ga_be->be_nsuffix[0],
1440 &gi->gi_n[gi->gi_nodes].gn_pdn );
1442 on->on_bi.bi_private = gi;
1443 ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1447 Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1448 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1449 /* allow this for now, assume a superior will
1457 rc = LDAP_NO_SUCH_OBJECT;
1471 glue_sub_add( BackendDB *be, int advert, int online )
1476 if ( overlay_is_inst( be, "glue" )) {
1477 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1478 "cannot be a subordinate!\n",
1479 be->be_suffix[0].bv_val, 0, 0 );
1482 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1484 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1486 ga = ch_malloc( sizeof( glue_Addrec ));
1487 ga->ga_next = ga_list;
1492 rc = glue_sub_attach( online );
1498 glue_access_allowed(
1501 AttributeDescription *desc,
1503 slap_access_t access,
1504 AccessControlState *state,
1505 slap_mask_t *maskp )
1507 BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
1510 if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
1511 return SLAP_CB_CONTINUE;
1515 rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
1523 glue.on_bi.bi_type = "glue";
1525 glue.on_bi.bi_db_init = glue_db_init;
1526 glue.on_bi.bi_db_close = glue_db_close;
1527 glue.on_bi.bi_db_destroy = glue_db_destroy;
1529 glue.on_bi.bi_op_search = glue_op_search;
1530 glue.on_bi.bi_op_modify = glue_op_func;
1531 glue.on_bi.bi_op_modrdn = glue_op_func;
1532 glue.on_bi.bi_op_add = glue_op_func;
1533 glue.on_bi.bi_op_delete = glue_op_func;
1534 glue.on_bi.bi_op_abandon = glue_op_abandon;
1535 glue.on_bi.bi_extended = glue_op_func;
1537 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1538 glue.on_bi.bi_chk_controls = glue_chk_controls;
1539 glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1540 glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1541 glue.on_bi.bi_access_allowed = glue_access_allowed;
1543 glue.on_response = glue_response;
1545 return overlay_register( &glue );