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;
157 glue_op_func ( Operation *op, SlapReply *rs )
159 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
160 BackendDB *b0 = op->o_bd;
161 BackendInfo *bi0 = op->o_bd->bd_info;
163 slap_operation_t which;
166 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
167 b0->bd_info = on->on_info->oi_orig;
170 case LDAP_REQ_ADD: which = op_add; break;
171 case LDAP_REQ_DELETE: which = op_delete; break;
172 case LDAP_REQ_MODIFY: which = op_modify; break;
173 case LDAP_REQ_MODRDN: which = op_modrdn; break;
176 func = &op->o_bd->bd_info->bi_op_bind;
178 rc = func[which]( op, rs );
180 rc = SLAP_CB_CONTINUE;
183 op->o_bd->bd_info = bi0;
188 glue_chk_referrals ( Operation *op, SlapReply *rs )
190 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
191 BackendDB *b0 = op->o_bd;
192 BackendInfo *bi0 = op->o_bd->bd_info;
195 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
196 b0->bd_info = on->on_info->oi_orig;
198 if ( op->o_bd->bd_info->bi_chk_referrals )
199 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
201 rc = SLAP_CB_CONTINUE;
204 op->o_bd->bd_info = bi0;
209 glue_chk_controls ( Operation *op, SlapReply *rs )
211 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
212 BackendDB *b0 = op->o_bd;
213 BackendInfo *bi0 = op->o_bd->bd_info;
214 int rc = SLAP_CB_CONTINUE;
216 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
217 b0->bd_info = on->on_info->oi_orig;
219 /* if the subordinate database has overlays, the bi_chk_controls()
220 * hook is actually over_aux_chk_controls(); in case it actually
221 * wraps a missing hok, we need to mimic the behavior
222 * of the frontend applied to that database */
223 if ( op->o_bd->bd_info->bi_chk_controls ) {
224 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
228 if ( rc == SLAP_CB_CONTINUE ) {
229 rc = backend_check_controls( op, rs );
233 op->o_bd->bd_info = bi0;
238 glue_op_search ( Operation *op, SlapReply *rs )
240 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
241 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
242 BackendDB *b0 = op->o_bd;
243 BackendDB *b1 = NULL, *btmp;
244 BackendInfo *bi0 = op->o_bd->bd_info;
247 glue_state gs = {0, 0, 0, NULL, 0, NULL};
248 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
249 int scope0, slimit0, tlimit0;
250 struct berval dn, ndn, *pdn;
254 cb.sc_next = op->o_callback;
256 stoptime = slap_get_time () + op->ors_tlimit;
258 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
259 b0->bd_info = on->on_info->oi_orig;
261 switch (op->ors_scope) {
262 case LDAP_SCOPE_BASE:
263 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
264 if (op->o_bd && op->o_bd->be_search) {
265 rs->sr_err = op->o_bd->be_search( op, rs );
269 case LDAP_SCOPE_ONELEVEL:
270 case LDAP_SCOPE_SUBTREE:
271 #ifdef LDAP_SCOPE_SUBORDINATE
272 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
277 if (op->o_bd && op->o_bd->be_search) {
278 rs->sr_err = op->o_bd->be_search( op, rs );
280 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
281 "No search target found");
287 op->o_callback = &cb;
288 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
289 scope0 = op->ors_scope;
290 slimit0 = gs.slimit = op->ors_slimit;
291 tlimit0 = op->ors_tlimit;
297 * Execute in reverse order, most general first
299 for (i = gi->gi_nodes; i >= 0; i--) {
300 if ( i == gi->gi_nodes ) {
304 btmp = gi->gi_n[i].gn_be;
305 pdn = &gi->gi_n[i].gn_pdn;
307 if (!btmp || !btmp->be_search)
309 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
311 if (tlimit0 != SLAP_NO_LIMIT) {
312 op->ors_tlimit = stoptime - slap_get_time ();
313 if (op->ors_tlimit <= 0) {
314 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
318 if (slimit0 != SLAP_NO_LIMIT) {
319 op->ors_slimit = slimit0 - rs->sr_nentries;
320 if (op->ors_slimit < 0) {
321 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
334 assert( op->o_bd->be_suffix != NULL );
335 assert( op->o_bd->be_nsuffix != NULL );
337 if (scope0 == LDAP_SCOPE_ONELEVEL &&
340 op->ors_scope = LDAP_SCOPE_BASE;
341 op->o_req_dn = op->o_bd->be_suffix[0];
342 op->o_req_ndn = op->o_bd->be_nsuffix[0];
343 rs->sr_err = op->o_bd->be_search(op, rs);
345 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
346 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
348 rs->sr_err = op->o_bd->be_search( op, rs );
350 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
351 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
353 op->o_req_dn = op->o_bd->be_suffix[0];
354 op->o_req_ndn = op->o_bd->be_nsuffix[0];
355 rs->sr_err = op->o_bd->be_search( op, rs );
356 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
357 gs.err = LDAP_SUCCESS;
360 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
361 rs->sr_err = op->o_bd->be_search( op, rs );
367 * Add errors that should result in dropping
370 case LDAP_SIZELIMIT_EXCEEDED:
371 case LDAP_TIMELIMIT_EXCEEDED:
372 case LDAP_ADMINLIMIT_EXCEEDED:
373 case LDAP_NO_SUCH_OBJECT:
374 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
375 case LDAP_CANNOT_CHAIN:
376 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
384 op->ors_scope = scope0;
385 op->ors_slimit = slimit0;
386 op->ors_tlimit = tlimit0;
392 if ( op->o_abandon ) {
393 rs->sr_err = SLAPD_ABANDON;
395 op->o_callback = cb.sc_next;
397 rs->sr_matched = gs.matched;
398 rs->sr_ref = gs.refs;
400 send_ldap_result( op, rs );
404 op->o_bd->bd_info = bi0;
408 ber_bvarray_free(gs.refs);
412 static BackendDB toolDB;
415 glue_tool_entry_open (
420 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
422 /* We don't know which backend to talk to yet, so just
423 * remember the mode and move on...
429 toolDB.bd_info = oi->oi_orig;
435 glue_tool_entry_close (
442 if (!glueBack->be_entry_close)
444 rc = glueBack->be_entry_close (glueBack);
449 static slap_overinst *
454 slap_overinfo *oi = (slap_overinfo *)bi;
457 for ( on = oi->oi_list; on; on=on->on_next ) {
458 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
464 /* This function will only be called in tool mode */
470 slap_overinst *on = glue_tool_inst( bi );
471 glueinfo *gi = on->on_bi.bi_private;
472 static int glueOpened = 0;
473 int i, j, same, bsame = 0, rc = 0;
475 if (glueOpened) return 0;
479 /* If we were invoked in tool mode, open all the underlying backends */
480 if (slapMode & SLAP_TOOL_MODE) {
481 for (i = 0; i<gi->gi_nodes; i++) {
483 /* Same type as our main backend? */
484 if ( gi->gi_n[i].gn_be->bd_info == on->on_info->oi_orig )
487 /* Loop thru the bd_info's and make sure we only
488 * invoke their bi_open functions once each.
490 for ( j = 0; j<i; j++ ) {
491 if ( gi->gi_n[i].gn_be->bd_info ==
492 gi->gi_n[j].gn_be->bd_info ) {
497 /* OK, it's unique and non-NULL, call it. */
498 if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
499 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
500 gi->gi_n[i].gn_be->bd_info );
501 /* Let backend.c take care of the rest of startup */
503 rc = backend_startup_one( gi->gi_n[i].gn_be );
506 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
507 rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
509 } /* other case is impossible */
513 /* This function will only be called in tool mode */
519 slap_overinst *on = glue_tool_inst( bi );
520 static int glueClosed = 0;
523 if (glueClosed) return 0;
527 if (slapMode & SLAP_TOOL_MODE) {
528 rc = backend_shutdown( NULL );
534 glue_entry_release_rw (
545 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
546 op->o_bd = glue_back_select (&b2, &e->e_nname);
548 if ( op->o_bd->be_release ) {
549 rc = op->o_bd->be_release( op, e, rw );
552 /* FIXME: mimic be_entry_release_rw
553 * when no be_release() available */
563 glue_tool_entry_first (
567 slap_overinst *on = glue_tool_inst( b0->bd_info );
568 glueinfo *gi = on->on_bi.bi_private;
571 /* If we're starting from scratch, start at the most general */
573 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
576 for (i = gi->gi_nodes-1; i >= 0; i--) {
577 if (gi->gi_n[i].gn_be->be_entry_open &&
578 gi->gi_n[i].gn_be->be_entry_first) {
579 glueBack = gi->gi_n[i].gn_be;
585 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
586 glueBack->be_entry_open (glueBack, glueMode) != 0)
589 return glueBack->be_entry_first (glueBack);
593 glue_tool_entry_next (
597 slap_overinst *on = glue_tool_inst( b0->bd_info );
598 glueinfo *gi = on->on_bi.bi_private;
602 if (!glueBack || !glueBack->be_entry_next)
605 rc = glueBack->be_entry_next (glueBack);
607 /* If we ran out of entries in one database, move on to the next */
609 if ( glueBack && glueBack->be_entry_close )
610 glueBack->be_entry_close (glueBack);
611 for (i=0; i<gi->gi_nodes; i++) {
612 if (gi->gi_n[i].gn_be == glueBack)
619 glueBack = gi->gi_n[i-1].gn_be;
620 rc = glue_tool_entry_first (b0);
627 glue_tool_entry_get (
632 if (!glueBack || !glueBack->be_entry_get)
635 return glueBack->be_entry_get (glueBack, id);
639 glue_tool_entry_put (
649 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
650 be = glue_back_select (&b2, &e->e_nname);
651 if ( be == &b2 ) be = &toolDB;
653 if (!be->be_entry_put)
657 if ( be->be_entry_open ) {
658 rc = be->be_entry_open (be, glueMode);
663 } else if (be != glueBack) {
664 /* If this entry belongs in a different branch than the
665 * previous one, close the current database and open the
668 if ( glueBack->be_entry_close ) {
669 glueBack->be_entry_close (glueBack);
671 if ( be->be_entry_open ) {
672 rc = be->be_entry_open (be, glueMode);
679 return be->be_entry_put (be, e, text);
683 glue_tool_entry_reindex (
688 if (!glueBack || !glueBack->be_entry_reindex)
691 return glueBack->be_entry_reindex (glueBack, id);
699 slap_overinst *on = glue_tool_inst( b0->bd_info );
700 glueinfo *gi = on->on_bi.bi_private;
701 BackendInfo *bi = b0->bd_info;
704 /* just sync everyone */
705 for (i = 0; i<gi->gi_nodes; i++)
706 if (gi->gi_n[i].gn_be->be_sync)
707 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
708 b0->bd_info = on->on_info->oi_orig;
720 slap_overinst *on = (slap_overinst *)be->bd_info;
721 slap_overinfo *oi = on->on_info;
724 gi = ch_calloc( 1, sizeof(glueinfo));
725 on->on_bi.bi_private = gi;
726 dnParent( be->be_nsuffix, &gi->gi_pdn );
728 /* Currently the overlay framework doesn't handle these entry points
729 * but we need them....
731 oi->oi_bi.bi_open = glue_open;
732 oi->oi_bi.bi_close = glue_close;
734 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
736 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
737 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
738 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
739 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
740 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
741 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
742 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
743 oi->oi_bi.bi_tool_sync = glue_tool_sync;
745 /*FIXME : need to add support */
746 oi->oi_bi.bi_tool_dn2id_get = 0;
747 oi->oi_bi.bi_tool_id2entry_get = 0;
748 oi->oi_bi.bi_tool_entry_modify = 0;
758 slap_overinst *on = (slap_overinst *)be->bd_info;
759 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
762 return SLAP_CB_CONTINUE;
770 slap_overinst *on = (slap_overinst *)be->bd_info;
772 on->on_info->oi_bi.bi_db_close = NULL;
785 slap_overinst *on = (slap_overinst *)be->bd_info;
786 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
788 /* redundant; could be applied just once */
789 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
791 if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
792 int i, async = 0, advertise = 0;
794 struct berval bv, dn;
797 fprintf( stderr, "%s: line %d: too few arguments in "
798 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
801 for ( i = 2; i < argc; i++ ) {
802 if ( strcasecmp( argv[i], "async" ) == 0 ) {
805 } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
809 fprintf( stderr, "%s: line %d: unrecognized option "
810 "\"%s\" ignored.\n", fname, lineno, argv[i] );
813 ber_str2bv( argv[1], 0, 0, &bv );
814 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
815 fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
818 b2 = select_backend( &dn, 0, 1 );
820 fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
821 fname, lineno, argv[1] );
824 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
826 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
828 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
829 gi->gi_nodes * sizeof(gluenode));
830 gi->gi_n[gi->gi_nodes].gn_be = b2;
831 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
832 gi->gi_n[gi->gi_nodes].gn_async = async;
834 on->on_bi.bi_private = gi;
837 return SLAP_CONF_UNKNOWN;
843 glue.on_bi.bi_type = "glue";
845 glue.on_bi.bi_db_init = glue_db_init;
846 glue.on_bi.bi_db_config = glue_db_config;
847 glue.on_bi.bi_db_close = glue_db_close;
848 glue.on_bi.bi_db_destroy = glue_db_destroy;
850 glue.on_bi.bi_op_search = glue_op_search;
851 glue.on_bi.bi_op_modify = glue_op_func;
852 glue.on_bi.bi_op_modrdn = glue_op_func;
853 glue.on_bi.bi_op_add = glue_op_func;
854 glue.on_bi.bi_op_delete = glue_op_func;
856 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
857 glue.on_bi.bi_chk_controls = glue_chk_controls;
859 return overlay_register( &glue );
862 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
864 init_module( int argc, char *argv[] )
868 #endif /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
870 #endif /* defined(SLAPD_OVER_GLUE */