1 /* glue.c - backend glue overlay */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2005 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.
31 #ifdef SLAPD_OVER_GLUE
35 #include <ac/string.h>
36 #include <ac/socket.h>
41 typedef struct gluenode {
47 typedef struct glueinfo {
53 static slap_overinst glue;
56 static BackendDB *glueBack;
58 static slap_response glue_op_response;
60 /* Just like select_backend, but only for our backends */
67 slap_overinst *on = (slap_overinst *)be->bd_info;
68 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
71 for (i = 0; i<gi->gi_nodes; i++) {
72 assert( gi->gi_n[i].gn_be->be_nsuffix );
74 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
75 return gi->gi_n[i].gn_be;
78 be->bd_info = on->on_info->oi_orig;
83 typedef struct glue_state {
93 glue_op_response ( Operation *op, SlapReply *rs )
95 glue_state *gs = op->o_callback->sc_private;
99 if ( gs->slimit != SLAP_NO_LIMIT
100 && rs->sr_nentries >= gs->slimit )
102 rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
107 return SLAP_CB_CONTINUE;
110 if (rs->sr_err == LDAP_SUCCESS ||
111 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
112 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
113 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
114 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
115 gs->err != LDAP_SUCCESS)
116 gs->err = rs->sr_err;
117 if (gs->err == LDAP_SUCCESS && gs->matched) {
118 ch_free (gs->matched);
122 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
124 len = strlen (rs->sr_matched);
125 if (len > gs->matchlen) {
127 ch_free (gs->matched);
128 gs->matched = ch_strdup (rs->sr_matched);
136 for (i=0; rs->sr_ref[i].bv_val; i++);
140 new = ch_malloc ((i+1)*sizeof(struct berval));
142 new = ch_realloc(gs->refs,
143 (j+i+1)*sizeof(struct berval));
145 for (k=0; k<i; j++,k++) {
146 ber_dupbv( &new[j], &rs->sr_ref[k] );
148 new[j].bv_val = NULL;
164 glue_op_func ( Operation *op, SlapReply *rs )
166 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
167 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
168 BackendDB *b0 = op->o_bd;
169 BackendInfo *bi0 = op->o_bd->bd_info;
171 enum glue_which which;
174 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
175 b0->bd_info = on->on_info->oi_orig;
178 case LDAP_REQ_ADD: which = op_add; break;
179 case LDAP_REQ_DELETE: which = op_delete; break;
180 case LDAP_REQ_MODIFY: which = op_modify; break;
181 case LDAP_REQ_MODRDN: which = op_modrdn; break;
184 func = &op->o_bd->bd_info->bi_op_modify;
186 rc = func[which]( op, rs );
188 rc = SLAP_CB_CONTINUE;
191 op->o_bd->bd_info = bi0;
196 glue_chk_referrals ( Operation *op, SlapReply *rs )
198 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
199 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
200 BackendDB *b0 = op->o_bd;
201 BackendInfo *bi0 = op->o_bd->bd_info;
204 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
205 b0->bd_info = on->on_info->oi_orig;
207 if ( op->o_bd->bd_info->bi_chk_referrals )
208 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
210 rc = SLAP_CB_CONTINUE;
213 op->o_bd->bd_info = bi0;
218 glue_chk_controls ( Operation *op, SlapReply *rs )
220 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
221 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
222 BackendDB *b0 = op->o_bd;
223 BackendInfo *bi0 = op->o_bd->bd_info;
224 int rc = SLAP_CB_CONTINUE;
226 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
227 b0->bd_info = on->on_info->oi_orig;
229 /* if the subordinate database has overlays, the bi_chk_controls()
230 * hook is actually over_aux_chk_controls(); in case it actually
231 * wraps a missing hok, we need to mimic the behavior
232 * of the frontend applied to that database */
233 if ( op->o_bd->bd_info->bi_chk_controls ) {
234 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
238 if ( rc == SLAP_CB_CONTINUE ) {
239 rc = backend_check_controls( op, rs );
243 op->o_bd->bd_info = bi0;
248 glue_op_search ( Operation *op, SlapReply *rs )
250 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
251 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
252 BackendDB *b0 = op->o_bd;
253 BackendDB *b1 = NULL, *btmp;
254 BackendInfo *bi0 = op->o_bd->bd_info;
257 glue_state gs = {0, 0, 0, NULL, 0, NULL};
258 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
259 int scope0, slimit0, tlimit0;
260 struct berval dn, ndn, *pdn;
264 cb.sc_next = op->o_callback;
266 stoptime = slap_get_time () + op->ors_tlimit;
268 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
269 b0->bd_info = on->on_info->oi_orig;
271 switch (op->ors_scope) {
272 case LDAP_SCOPE_BASE:
273 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
274 if (op->o_bd && op->o_bd->be_search) {
275 rs->sr_err = op->o_bd->be_search( op, rs );
279 case LDAP_SCOPE_ONELEVEL:
280 case LDAP_SCOPE_SUBTREE:
281 #ifdef LDAP_SCOPE_SUBORDINATE
282 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
287 if (op->o_bd && op->o_bd->be_search) {
288 rs->sr_err = op->o_bd->be_search( op, rs );
290 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
291 "No search target found");
297 op->o_callback = &cb;
298 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
299 scope0 = op->ors_scope;
300 slimit0 = gs.slimit = op->ors_slimit;
301 tlimit0 = op->ors_tlimit;
307 * Execute in reverse order, most general first
309 for (i = gi->gi_nodes; i >= 0; i--) {
310 if ( i == gi->gi_nodes ) {
314 btmp = gi->gi_n[i].gn_be;
315 pdn = &gi->gi_n[i].gn_pdn;
317 if (!btmp || !btmp->be_search)
319 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
321 if (tlimit0 != SLAP_NO_LIMIT) {
322 op->ors_tlimit = stoptime - slap_get_time ();
323 if (op->ors_tlimit <= 0) {
324 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
328 if (slimit0 != SLAP_NO_LIMIT) {
329 op->ors_slimit = slimit0 - rs->sr_nentries;
330 if (op->ors_slimit < 0) {
331 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
344 assert( op->o_bd->be_suffix );
345 assert( op->o_bd->be_nsuffix );
347 if (scope0 == LDAP_SCOPE_ONELEVEL &&
350 op->ors_scope = LDAP_SCOPE_BASE;
351 op->o_req_dn = op->o_bd->be_suffix[0];
352 op->o_req_ndn = op->o_bd->be_nsuffix[0];
353 rs->sr_err = op->o_bd->be_search(op, rs);
355 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
356 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
358 rs->sr_err = op->o_bd->be_search( op, rs );
360 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
361 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
363 op->o_req_dn = op->o_bd->be_suffix[0];
364 op->o_req_ndn = op->o_bd->be_nsuffix[0];
365 rs->sr_err = op->o_bd->be_search( op, rs );
366 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
367 gs.err = LDAP_SUCCESS;
370 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
371 rs->sr_err = op->o_bd->be_search( op, rs );
377 * Add errors that should result in dropping
380 case LDAP_SIZELIMIT_EXCEEDED:
381 case LDAP_TIMELIMIT_EXCEEDED:
382 case LDAP_ADMINLIMIT_EXCEEDED:
383 case LDAP_NO_SUCH_OBJECT:
384 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
385 case LDAP_CANNOT_CHAIN:
386 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
394 op->ors_scope = scope0;
395 op->ors_slimit = slimit0;
396 op->ors_tlimit = tlimit0;
402 if ( op->o_abandon ) {
403 rs->sr_err = SLAPD_ABANDON;
405 op->o_callback = cb.sc_next;
407 rs->sr_matched = gs.matched;
408 rs->sr_ref = gs.refs;
410 send_ldap_result( op, rs );
414 op->o_bd->bd_info = bi0;
418 ber_bvarray_free(gs.refs);
422 static BackendDB toolDB;
425 glue_tool_entry_open (
430 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
432 /* We don't know which backend to talk to yet, so just
433 * remember the mode and move on...
439 toolDB.bd_info = oi->oi_orig;
445 glue_tool_entry_close (
452 if (!glueBack->be_entry_close)
454 rc = glueBack->be_entry_close (glueBack);
459 static slap_overinst *
464 slap_overinfo *oi = (slap_overinfo *)bi;
467 for ( on = oi->oi_list; on; on=on->on_next ) {
468 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
474 /* This function will only be called in tool mode */
480 slap_overinst *on = glue_tool_inst( bi );
481 glueinfo *gi = on->on_bi.bi_private;
482 static int glueOpened = 0;
485 if (glueOpened) return 0;
489 /* If we were invoked in tool mode, open all the underlying backends */
490 if (slapMode & SLAP_TOOL_MODE) {
491 rc = backend_startup( NULL );
492 } /* other case is impossible */
496 /* This function will only be called in tool mode */
502 slap_overinst *on = glue_tool_inst( bi );
503 glueinfo *gi = on->on_bi.bi_private;
504 static int glueClosed = 0;
507 if (glueClosed) return 0;
511 if (slapMode & SLAP_TOOL_MODE) {
512 rc = backend_shutdown( NULL );
518 glue_entry_release_rw (
529 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
530 op->o_bd = glue_back_select (&b2, &e->e_nname);
532 rc = op->o_bd->be_release( op, e, rw );
538 glue_tool_entry_first (
542 slap_overinst *on = glue_tool_inst( b0->bd_info );
543 glueinfo *gi = on->on_bi.bi_private;
546 /* If we're starting from scratch, start at the most general */
548 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
551 for (i = gi->gi_nodes-1; i >= 0; i--) {
552 if (gi->gi_n[i].gn_be->be_entry_open &&
553 gi->gi_n[i].gn_be->be_entry_first) {
554 glueBack = gi->gi_n[i].gn_be;
560 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
561 glueBack->be_entry_open (glueBack, glueMode) != 0)
564 return glueBack->be_entry_first (glueBack);
568 glue_tool_entry_next (
572 slap_overinst *on = glue_tool_inst( b0->bd_info );
573 glueinfo *gi = on->on_bi.bi_private;
577 if (!glueBack || !glueBack->be_entry_next)
580 rc = glueBack->be_entry_next (glueBack);
582 /* If we ran out of entries in one database, move on to the next */
584 if ( glueBack && glueBack->be_entry_close )
585 glueBack->be_entry_close (glueBack);
586 for (i=0; i<gi->gi_nodes; i++) {
587 if (gi->gi_n[i].gn_be == glueBack)
594 glueBack = gi->gi_n[i-1].gn_be;
595 rc = glue_tool_entry_first (b0);
602 glue_tool_entry_get (
607 if (!glueBack || !glueBack->be_entry_get)
610 return glueBack->be_entry_get (glueBack, id);
614 glue_tool_entry_put (
624 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
625 be = glue_back_select (&b2, &e->e_nname);
626 if ( be == &b2 ) be = &toolDB;
628 if (!be->be_entry_put)
632 rc = be->be_entry_open (be, glueMode);
635 } else if (be != glueBack) {
636 /* If this entry belongs in a different branch than the
637 * previous one, close the current database and open the
640 glueBack->be_entry_close (glueBack);
641 rc = be->be_entry_open (be, glueMode);
646 return be->be_entry_put (be, e, text);
650 glue_tool_entry_reindex (
655 if (!glueBack || !glueBack->be_entry_reindex)
658 return glueBack->be_entry_reindex (glueBack, id);
666 slap_overinst *on = glue_tool_inst( b0->bd_info );
667 glueinfo *gi = on->on_bi.bi_private;
668 BackendInfo *bi = b0->bd_info;
671 /* just sync everyone */
672 for (i = 0; i<gi->gi_nodes; i++)
673 if (gi->gi_n[i].gn_be->be_sync)
674 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
675 b0->bd_info = on->on_info->oi_orig;
687 slap_overinst *on = (slap_overinst *)be->bd_info;
688 slap_overinfo *oi = on->on_info;
691 gi = ch_calloc( 1, sizeof(glueinfo));
692 on->on_bi.bi_private = gi;
693 dnParent( be->be_nsuffix, &gi->gi_pdn );
695 /* Currently the overlay framework doesn't handle these entry points
696 * but we need them....
698 oi->oi_bi.bi_open = glue_open;
699 oi->oi_bi.bi_close = glue_close;
701 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
703 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
704 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
705 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
706 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
707 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
708 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
709 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
710 oi->oi_bi.bi_tool_sync = glue_tool_sync;
712 /*FIXME : need to add support */
713 oi->oi_bi.bi_tool_dn2id_get = 0;
714 oi->oi_bi.bi_tool_id2entry_get = 0;
715 oi->oi_bi.bi_tool_entry_modify = 0;
725 slap_overinst *on = (slap_overinst *)be->bd_info;
726 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
729 return SLAP_CB_CONTINUE;
737 slap_overinst *on = (slap_overinst *)be->bd_info;
739 on->on_info->oi_bi.bi_db_close = NULL;
752 slap_overinst *on = (slap_overinst *)be->bd_info;
753 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
755 /* redundant; could be applied just once */
756 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
758 if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
759 int i, async = 0, advertise = 0;
761 struct berval bv, dn;
765 fprintf( stderr, "%s: line %d: too few arguments in "
766 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
769 for ( i = 2; i < argc; i++ ) {
770 if ( strcasecmp( argv[i], "async" ) == 0 ) {
773 } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
777 fprintf( stderr, "%s: line %d: unrecognized option "
778 "\"%s\" ignored.\n", fname, lineno, argv[i] );
781 ber_str2bv( argv[1], 0, 0, &bv );
782 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
783 fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
786 b2 = select_backend( &dn, 0, 1 );
788 fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
789 fname, lineno, argv[1] );
792 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
794 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
796 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
797 gi->gi_nodes * sizeof(gluenode));
798 gi->gi_n[gi->gi_nodes].gn_be = b2;
799 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
800 gi->gi_n[gi->gi_nodes].gn_async = async;
802 on->on_bi.bi_private = gi;
805 return SLAP_CONF_UNKNOWN;
811 glue.on_bi.bi_type = "glue";
813 glue.on_bi.bi_db_init = glue_db_init;
814 glue.on_bi.bi_db_config = glue_db_config;
815 glue.on_bi.bi_db_close = glue_db_close;
816 glue.on_bi.bi_db_destroy = glue_db_destroy;
818 glue.on_bi.bi_op_search = glue_op_search;
819 glue.on_bi.bi_op_modify = glue_op_func;
820 glue.on_bi.bi_op_modrdn = glue_op_func;
821 glue.on_bi.bi_op_add = glue_op_func;
822 glue.on_bi.bi_op_delete = glue_op_func;
824 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
825 glue.on_bi.bi_chk_controls = glue_chk_controls;
827 return overlay_register( &glue );
830 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
832 init_module( int argc, char *argv[] )
836 #endif /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
838 #endif /* defined(SLAPD_OVER_GLUE */