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 != NULL );
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 BackendDB *b0 = op->o_bd;
168 BackendInfo *bi0 = op->o_bd->bd_info;
170 enum glue_which which;
173 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
174 b0->bd_info = on->on_info->oi_orig;
177 case LDAP_REQ_ADD: which = op_add; break;
178 case LDAP_REQ_DELETE: which = op_delete; break;
179 case LDAP_REQ_MODIFY: which = op_modify; break;
180 case LDAP_REQ_MODRDN: which = op_modrdn; break;
183 func = &op->o_bd->bd_info->bi_op_modify;
185 rc = func[which]( op, rs );
187 rc = SLAP_CB_CONTINUE;
190 op->o_bd->bd_info = bi0;
195 glue_chk_referrals ( Operation *op, SlapReply *rs )
197 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
198 BackendDB *b0 = op->o_bd;
199 BackendInfo *bi0 = op->o_bd->bd_info;
202 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
203 b0->bd_info = on->on_info->oi_orig;
205 if ( op->o_bd->bd_info->bi_chk_referrals )
206 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
208 rc = SLAP_CB_CONTINUE;
211 op->o_bd->bd_info = bi0;
216 glue_chk_controls ( Operation *op, SlapReply *rs )
218 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
219 BackendDB *b0 = op->o_bd;
220 BackendInfo *bi0 = op->o_bd->bd_info;
221 int rc = SLAP_CB_CONTINUE;
223 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
224 b0->bd_info = on->on_info->oi_orig;
226 /* if the subordinate database has overlays, the bi_chk_controls()
227 * hook is actually over_aux_chk_controls(); in case it actually
228 * wraps a missing hok, we need to mimic the behavior
229 * of the frontend applied to that database */
230 if ( op->o_bd->bd_info->bi_chk_controls ) {
231 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
235 if ( rc == SLAP_CB_CONTINUE ) {
236 rc = backend_check_controls( op, rs );
240 op->o_bd->bd_info = bi0;
245 glue_op_search ( Operation *op, SlapReply *rs )
247 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
248 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
249 BackendDB *b0 = op->o_bd;
250 BackendDB *b1 = NULL, *btmp;
251 BackendInfo *bi0 = op->o_bd->bd_info;
254 glue_state gs = {0, 0, 0, NULL, 0, NULL};
255 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
256 int scope0, slimit0, tlimit0;
257 struct berval dn, ndn, *pdn;
261 cb.sc_next = op->o_callback;
263 stoptime = slap_get_time () + op->ors_tlimit;
265 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
266 b0->bd_info = on->on_info->oi_orig;
268 switch (op->ors_scope) {
269 case LDAP_SCOPE_BASE:
270 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
271 if (op->o_bd && op->o_bd->be_search) {
272 rs->sr_err = op->o_bd->be_search( op, rs );
276 case LDAP_SCOPE_ONELEVEL:
277 case LDAP_SCOPE_SUBTREE:
278 #ifdef LDAP_SCOPE_SUBORDINATE
279 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
284 if (op->o_bd && op->o_bd->be_search) {
285 rs->sr_err = op->o_bd->be_search( op, rs );
287 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
288 "No search target found");
294 op->o_callback = &cb;
295 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
296 scope0 = op->ors_scope;
297 slimit0 = gs.slimit = op->ors_slimit;
298 tlimit0 = op->ors_tlimit;
304 * Execute in reverse order, most general first
306 for (i = gi->gi_nodes; i >= 0; i--) {
307 if ( i == gi->gi_nodes ) {
311 btmp = gi->gi_n[i].gn_be;
312 pdn = &gi->gi_n[i].gn_pdn;
314 if (!btmp || !btmp->be_search)
316 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
318 if (tlimit0 != SLAP_NO_LIMIT) {
319 op->ors_tlimit = stoptime - slap_get_time ();
320 if (op->ors_tlimit <= 0) {
321 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
325 if (slimit0 != SLAP_NO_LIMIT) {
326 op->ors_slimit = slimit0 - rs->sr_nentries;
327 if (op->ors_slimit < 0) {
328 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
341 assert( op->o_bd->be_suffix != NULL );
342 assert( op->o_bd->be_nsuffix != NULL );
344 if (scope0 == LDAP_SCOPE_ONELEVEL &&
347 op->ors_scope = LDAP_SCOPE_BASE;
348 op->o_req_dn = op->o_bd->be_suffix[0];
349 op->o_req_ndn = op->o_bd->be_nsuffix[0];
350 rs->sr_err = op->o_bd->be_search(op, rs);
352 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
353 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
355 rs->sr_err = op->o_bd->be_search( op, rs );
357 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
358 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
360 op->o_req_dn = op->o_bd->be_suffix[0];
361 op->o_req_ndn = op->o_bd->be_nsuffix[0];
362 rs->sr_err = op->o_bd->be_search( op, rs );
363 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
364 gs.err = LDAP_SUCCESS;
367 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
368 rs->sr_err = op->o_bd->be_search( op, rs );
374 * Add errors that should result in dropping
377 case LDAP_SIZELIMIT_EXCEEDED:
378 case LDAP_TIMELIMIT_EXCEEDED:
379 case LDAP_ADMINLIMIT_EXCEEDED:
380 case LDAP_NO_SUCH_OBJECT:
381 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
382 case LDAP_CANNOT_CHAIN:
383 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
391 op->ors_scope = scope0;
392 op->ors_slimit = slimit0;
393 op->ors_tlimit = tlimit0;
399 if ( op->o_abandon ) {
400 rs->sr_err = SLAPD_ABANDON;
402 op->o_callback = cb.sc_next;
404 rs->sr_matched = gs.matched;
405 rs->sr_ref = gs.refs;
407 send_ldap_result( op, rs );
411 op->o_bd->bd_info = bi0;
415 ber_bvarray_free(gs.refs);
419 static BackendDB toolDB;
422 glue_tool_entry_open (
427 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
429 /* We don't know which backend to talk to yet, so just
430 * remember the mode and move on...
436 toolDB.bd_info = oi->oi_orig;
442 glue_tool_entry_close (
449 if (!glueBack->be_entry_close)
451 rc = glueBack->be_entry_close (glueBack);
456 static slap_overinst *
461 slap_overinfo *oi = (slap_overinfo *)bi;
464 for ( on = oi->oi_list; on; on=on->on_next ) {
465 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
471 /* This function will only be called in tool mode */
477 slap_overinst *on = glue_tool_inst( bi );
478 glueinfo *gi = on->on_bi.bi_private;
479 static int glueOpened = 0;
480 int i, j, same, bsame = 0, rc = 0;
482 if (glueOpened) return 0;
486 /* If we were invoked in tool mode, open all the underlying backends */
487 if (slapMode & SLAP_TOOL_MODE) {
488 for (i = 0; i<gi->gi_nodes; i++) {
490 /* Same type as our main backend? */
491 if ( gi->gi_n[i].gn_be->bd_info == on->on_info->oi_orig )
494 /* Loop thru the bd_info's and make sure we only
495 * invoke their bi_open functions once each.
497 for ( j = 0; j<i; j++ ) {
498 if ( gi->gi_n[i].gn_be->bd_info ==
499 gi->gi_n[j].gn_be->bd_info ) {
504 /* OK, it's unique and non-NULL, call it. */
505 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
506 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
507 gi->gi_n[i].gn_be->bd_info );
508 /* Let backend.c take care of the rest of startup */
510 rc = backend_startup_one( gi->gi_n[i].gn_be );
513 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
514 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
516 } /* other case is impossible */
520 /* This function will only be called in tool mode */
526 slap_overinst *on = glue_tool_inst( bi );
527 static int glueClosed = 0;
530 if (glueClosed) return 0;
534 if (slapMode & SLAP_TOOL_MODE) {
535 rc = backend_shutdown( NULL );
541 glue_entry_release_rw (
552 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
553 op->o_bd = glue_back_select (&b2, &e->e_nname);
555 if ( op->o_bd->be_release ) {
556 rc = op->o_bd->be_release( op, e, rw );
559 /* FIXME: mimic be_entry_release_rw
560 * when no be_release() available */
570 glue_tool_entry_first (
574 slap_overinst *on = glue_tool_inst( b0->bd_info );
575 glueinfo *gi = on->on_bi.bi_private;
578 /* If we're starting from scratch, start at the most general */
580 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
583 for (i = gi->gi_nodes-1; i >= 0; i--) {
584 if (gi->gi_n[i].gn_be->be_entry_open &&
585 gi->gi_n[i].gn_be->be_entry_first) {
586 glueBack = gi->gi_n[i].gn_be;
592 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
593 glueBack->be_entry_open (glueBack, glueMode) != 0)
596 return glueBack->be_entry_first (glueBack);
600 glue_tool_entry_next (
604 slap_overinst *on = glue_tool_inst( b0->bd_info );
605 glueinfo *gi = on->on_bi.bi_private;
609 if (!glueBack || !glueBack->be_entry_next)
612 rc = glueBack->be_entry_next (glueBack);
614 /* If we ran out of entries in one database, move on to the next */
616 if ( glueBack && glueBack->be_entry_close )
617 glueBack->be_entry_close (glueBack);
618 for (i=0; i<gi->gi_nodes; i++) {
619 if (gi->gi_n[i].gn_be == glueBack)
626 glueBack = gi->gi_n[i-1].gn_be;
627 rc = glue_tool_entry_first (b0);
634 glue_tool_entry_get (
639 if (!glueBack || !glueBack->be_entry_get)
642 return glueBack->be_entry_get (glueBack, id);
646 glue_tool_entry_put (
656 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
657 be = glue_back_select (&b2, &e->e_nname);
658 if ( be == &b2 ) be = &toolDB;
660 if (!be->be_entry_put)
664 if ( be->be_entry_open ) {
665 rc = be->be_entry_open (be, glueMode);
670 } else if (be != glueBack) {
671 /* If this entry belongs in a different branch than the
672 * previous one, close the current database and open the
675 if ( glueBack->be_entry_close ) {
676 glueBack->be_entry_close (glueBack);
678 if ( be->be_entry_open ) {
679 rc = be->be_entry_open (be, glueMode);
686 return be->be_entry_put (be, e, text);
690 glue_tool_entry_reindex (
695 if (!glueBack || !glueBack->be_entry_reindex)
698 return glueBack->be_entry_reindex (glueBack, id);
706 slap_overinst *on = glue_tool_inst( b0->bd_info );
707 glueinfo *gi = on->on_bi.bi_private;
708 BackendInfo *bi = b0->bd_info;
711 /* just sync everyone */
712 for (i = 0; i<gi->gi_nodes; i++)
713 if (gi->gi_n[i].gn_be->be_sync)
714 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
715 b0->bd_info = on->on_info->oi_orig;
727 slap_overinst *on = (slap_overinst *)be->bd_info;
728 slap_overinfo *oi = on->on_info;
731 gi = ch_calloc( 1, sizeof(glueinfo));
732 on->on_bi.bi_private = gi;
733 dnParent( be->be_nsuffix, &gi->gi_pdn );
735 /* Currently the overlay framework doesn't handle these entry points
736 * but we need them....
738 oi->oi_bi.bi_open = glue_open;
739 oi->oi_bi.bi_close = glue_close;
741 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
743 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
744 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
745 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
746 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
747 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
748 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
749 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
750 oi->oi_bi.bi_tool_sync = glue_tool_sync;
752 /*FIXME : need to add support */
753 oi->oi_bi.bi_tool_dn2id_get = 0;
754 oi->oi_bi.bi_tool_id2entry_get = 0;
755 oi->oi_bi.bi_tool_entry_modify = 0;
765 slap_overinst *on = (slap_overinst *)be->bd_info;
766 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
769 return SLAP_CB_CONTINUE;
777 slap_overinst *on = (slap_overinst *)be->bd_info;
779 on->on_info->oi_bi.bi_db_close = NULL;
792 slap_overinst *on = (slap_overinst *)be->bd_info;
793 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
795 /* redundant; could be applied just once */
796 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
798 if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
799 int i, async = 0, advertise = 0;
801 struct berval bv, dn;
804 fprintf( stderr, "%s: line %d: too few arguments in "
805 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
808 for ( i = 2; i < argc; i++ ) {
809 if ( strcasecmp( argv[i], "async" ) == 0 ) {
812 } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
816 fprintf( stderr, "%s: line %d: unrecognized option "
817 "\"%s\" ignored.\n", fname, lineno, argv[i] );
820 ber_str2bv( argv[1], 0, 0, &bv );
821 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
822 fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
825 b2 = select_backend( &dn, 0, 1 );
827 fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
828 fname, lineno, argv[1] );
831 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
833 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
835 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
836 gi->gi_nodes * sizeof(gluenode));
837 gi->gi_n[gi->gi_nodes].gn_be = b2;
838 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
839 gi->gi_n[gi->gi_nodes].gn_async = async;
841 on->on_bi.bi_private = gi;
844 return SLAP_CONF_UNKNOWN;
850 glue.on_bi.bi_type = "glue";
852 glue.on_bi.bi_db_init = glue_db_init;
853 glue.on_bi.bi_db_config = glue_db_config;
854 glue.on_bi.bi_db_close = glue_db_close;
855 glue.on_bi.bi_db_destroy = glue_db_destroy;
857 glue.on_bi.bi_op_search = glue_op_search;
858 glue.on_bi.bi_op_modify = glue_op_func;
859 glue.on_bi.bi_op_modrdn = glue_op_func;
860 glue.on_bi.bi_op_add = glue_op_func;
861 glue.on_bi.bi_op_delete = glue_op_func;
863 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
864 glue.on_bi.bi_chk_controls = glue_chk_controls;
866 return overlay_register( &glue );
869 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
871 init_module( int argc, char *argv[] )
875 #endif /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
877 #endif /* defined(SLAPD_OVER_GLUE */