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 {
48 typedef struct glueinfo {
54 static slap_overinst glue;
57 static BackendDB *glueBack;
59 static slap_response glue_op_response;
61 /* Just like select_backend, but only for our backends */
68 slap_overinst *on = (slap_overinst *)be->bd_info;
69 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
72 for (i = 0; i<gi->gi_nodes; i++) {
73 assert( gi->gi_n[i].gn_be->be_nsuffix );
75 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
76 return gi->gi_n[i].gn_be;
79 be->bd_info = on->on_info->oi_orig;
84 typedef struct glue_state {
94 glue_op_response ( Operation *op, SlapReply *rs )
96 glue_state *gs = op->o_callback->sc_private;
100 if ( gs->slimit != SLAP_NO_LIMIT
101 && rs->sr_nentries >= gs->slimit )
103 rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
108 return SLAP_CB_CONTINUE;
111 if (rs->sr_err == LDAP_SUCCESS ||
112 rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
113 rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
114 rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
115 rs->sr_err == LDAP_NO_SUCH_OBJECT ||
116 gs->err != LDAP_SUCCESS)
117 gs->err = rs->sr_err;
118 if (gs->err == LDAP_SUCCESS && gs->matched) {
119 ch_free (gs->matched);
123 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
125 len = strlen (rs->sr_matched);
126 if (len > gs->matchlen) {
128 ch_free (gs->matched);
129 gs->matched = ch_strdup (rs->sr_matched);
137 for (i=0; rs->sr_ref[i].bv_val; i++);
141 new = ch_malloc ((i+1)*sizeof(struct berval));
143 new = ch_realloc(gs->refs,
144 (j+i+1)*sizeof(struct berval));
146 for (k=0; k<i; j++,k++) {
147 ber_dupbv( &new[j], &rs->sr_ref[k] );
149 new[j].bv_val = NULL;
165 glue_op_func ( Operation *op, SlapReply *rs )
167 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
168 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
169 BackendDB *b0 = op->o_bd;
170 BackendInfo *bi0 = op->o_bd->bd_info;
172 enum glue_which which;
175 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
176 b0->bd_info = on->on_info->oi_orig;
179 case LDAP_REQ_ADD: which = op_add; break;
180 case LDAP_REQ_DELETE: which = op_delete; break;
181 case LDAP_REQ_MODIFY: which = op_modify; break;
182 case LDAP_REQ_MODRDN: which = op_modrdn; break;
185 func = &op->o_bd->bd_info->bi_op_modify;
187 rc = func[which]( op, rs );
189 rc = SLAP_CB_CONTINUE;
192 op->o_bd->bd_info = bi0;
197 glue_chk_referrals ( Operation *op, SlapReply *rs )
199 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
200 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
201 BackendDB *b0 = op->o_bd;
202 BackendInfo *bi0 = op->o_bd->bd_info;
205 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
206 b0->bd_info = on->on_info->oi_orig;
208 if ( op->o_bd->bd_info->bi_chk_referrals )
209 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
211 rc = SLAP_CB_CONTINUE;
214 op->o_bd->bd_info = bi0;
219 glue_chk_controls ( Operation *op, SlapReply *rs )
221 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
222 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
223 BackendDB *b0 = op->o_bd;
224 BackendInfo *bi0 = op->o_bd->bd_info;
225 int rc = SLAP_CB_CONTINUE;
227 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
228 b0->bd_info = on->on_info->oi_orig;
230 /* if the subordinate database has overlays, the bi_chk_controls()
231 * hook is actually over_aux_chk_controls(); in case it actually
232 * wraps a missing hok, we need to mimic the behavior
233 * of the frontend applied to that database */
234 if ( op->o_bd->bd_info->bi_chk_controls ) {
235 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
239 if ( rc == SLAP_CB_CONTINUE ) {
240 rc = backend_check_controls( op, rs );
244 op->o_bd->bd_info = bi0;
249 glue_op_search ( Operation *op, SlapReply *rs )
251 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
252 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
253 BackendDB *b0 = op->o_bd;
254 BackendDB *b1 = NULL, *btmp;
255 BackendInfo *bi0 = op->o_bd->bd_info;
258 glue_state gs = {0, 0, 0, NULL, 0, NULL};
259 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
260 int scope0, slimit0, tlimit0;
261 struct berval dn, ndn, *pdn;
265 cb.sc_next = op->o_callback;
267 stoptime = slap_get_time () + op->ors_tlimit;
269 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
270 b0->bd_info = on->on_info->oi_orig;
272 switch (op->ors_scope) {
273 case LDAP_SCOPE_BASE:
274 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
275 if (op->o_bd && op->o_bd->be_search) {
276 rs->sr_err = op->o_bd->be_search( op, rs );
280 case LDAP_SCOPE_ONELEVEL:
281 case LDAP_SCOPE_SUBTREE:
282 #ifdef LDAP_SCOPE_SUBORDINATE
283 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
288 if (op->o_bd && op->o_bd->be_search) {
289 rs->sr_err = op->o_bd->be_search( op, rs );
291 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
292 "No search target found");
298 op->o_callback = &cb;
299 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
300 scope0 = op->ors_scope;
301 slimit0 = gs.slimit = op->ors_slimit;
302 tlimit0 = op->ors_tlimit;
308 * Execute in reverse order, most general first
310 for (i = gi->gi_nodes; i >= 0; i--) {
311 if ( i == gi->gi_nodes ) {
315 btmp = gi->gi_n[i].gn_be;
316 pdn = &gi->gi_n[i].gn_pdn;
318 if (!btmp || !btmp->be_search)
320 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
322 if (tlimit0 != SLAP_NO_LIMIT) {
323 op->ors_tlimit = stoptime - slap_get_time ();
324 if (op->ors_tlimit <= 0) {
325 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
329 if (slimit0 != SLAP_NO_LIMIT) {
330 op->ors_slimit = slimit0 - rs->sr_nentries;
331 if (op->ors_slimit < 0) {
332 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
345 assert( op->o_bd->be_suffix );
346 assert( op->o_bd->be_nsuffix );
348 if (scope0 == LDAP_SCOPE_ONELEVEL &&
351 op->ors_scope = LDAP_SCOPE_BASE;
352 op->o_req_dn = op->o_bd->be_suffix[0];
353 op->o_req_ndn = op->o_bd->be_nsuffix[0];
354 rs->sr_err = op->o_bd->be_search(op, rs);
356 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
357 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
359 rs->sr_err = op->o_bd->be_search( op, rs );
361 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
362 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
364 op->o_req_dn = op->o_bd->be_suffix[0];
365 op->o_req_ndn = op->o_bd->be_nsuffix[0];
366 rs->sr_err = op->o_bd->be_search( op, rs );
367 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
368 gs.err = LDAP_SUCCESS;
371 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
372 rs->sr_err = op->o_bd->be_search( op, rs );
378 * Add errors that should result in dropping
381 case LDAP_SIZELIMIT_EXCEEDED:
382 case LDAP_TIMELIMIT_EXCEEDED:
383 case LDAP_ADMINLIMIT_EXCEEDED:
384 case LDAP_NO_SUCH_OBJECT:
385 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
386 case LDAP_CANNOT_CHAIN:
387 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
395 op->ors_scope = scope0;
396 op->ors_slimit = slimit0;
397 op->ors_tlimit = tlimit0;
403 if ( op->o_abandon ) {
404 rs->sr_err = SLAPD_ABANDON;
406 op->o_callback = cb.sc_next;
408 rs->sr_matched = gs.matched;
409 rs->sr_ref = gs.refs;
411 send_ldap_result( op, rs );
415 op->o_bd->bd_info = bi0;
419 ber_bvarray_free(gs.refs);
423 static BackendDB toolDB;
426 glue_tool_entry_open (
431 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
433 /* We don't know which backend to talk to yet, so just
434 * remember the mode and move on...
440 toolDB.bd_info = oi->oi_orig;
446 glue_tool_entry_close (
453 if (!glueBack->be_entry_close)
455 rc = glueBack->be_entry_close (glueBack);
460 static slap_overinst *
465 slap_overinfo *oi = (slap_overinfo *)bi;
468 for ( on = oi->oi_list; on; on=on->on_next ) {
469 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
475 /* This function will only be called in tool mode */
481 slap_overinst *on = glue_tool_inst( bi );
482 glueinfo *gi = on->on_bi.bi_private;
483 static int glueOpened = 0;
486 if (glueOpened) return 0;
490 /* If we were invoked in tool mode, open all the underlying backends */
491 if (slapMode & SLAP_TOOL_MODE) {
492 rc = backend_startup( NULL );
493 } /* other case is impossible */
497 /* This function will only be called in tool mode */
503 slap_overinst *on = glue_tool_inst( bi );
504 glueinfo *gi = on->on_bi.bi_private;
505 static int glueClosed = 0;
508 if (glueClosed) return 0;
512 if (slapMode & SLAP_TOOL_MODE) {
513 rc = backend_shutdown( NULL );
519 glue_entry_release_rw (
530 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
531 op->o_bd = glue_back_select (&b2, &e->e_nname);
533 rc = op->o_bd->be_release( op, e, rw );
539 glue_tool_entry_first (
543 slap_overinst *on = glue_tool_inst( b0->bd_info );
544 glueinfo *gi = on->on_bi.bi_private;
547 /* If we're starting from scratch, start at the most general */
549 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
552 for (i = gi->gi_nodes-1; i >= 0; i--) {
553 if (gi->gi_n[i].gn_be->be_entry_open &&
554 gi->gi_n[i].gn_be->be_entry_first) {
555 glueBack = gi->gi_n[i].gn_be;
561 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
562 glueBack->be_entry_open (glueBack, glueMode) != 0)
565 return glueBack->be_entry_first (glueBack);
569 glue_tool_entry_next (
573 slap_overinst *on = glue_tool_inst( b0->bd_info );
574 glueinfo *gi = on->on_bi.bi_private;
578 if (!glueBack || !glueBack->be_entry_next)
581 rc = glueBack->be_entry_next (glueBack);
583 /* If we ran out of entries in one database, move on to the next */
585 if ( glueBack && glueBack->be_entry_close )
586 glueBack->be_entry_close (glueBack);
587 for (i=0; i<gi->gi_nodes; i++) {
588 if (gi->gi_n[i].gn_be == glueBack)
595 glueBack = gi->gi_n[i-1].gn_be;
596 rc = glue_tool_entry_first (b0);
603 glue_tool_entry_get (
608 if (!glueBack || !glueBack->be_entry_get)
611 return glueBack->be_entry_get (glueBack, id);
615 glue_tool_entry_put (
625 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
626 be = glue_back_select (&b2, &e->e_nname);
627 if ( be == &b2 ) be = &toolDB;
629 if (!be->be_entry_put)
633 rc = be->be_entry_open (be, glueMode);
636 } else if (be != glueBack) {
637 /* If this entry belongs in a different branch than the
638 * previous one, close the current database and open the
641 glueBack->be_entry_close (glueBack);
642 rc = be->be_entry_open (be, glueMode);
647 return be->be_entry_put (be, e, text);
651 glue_tool_entry_reindex (
656 if (!glueBack || !glueBack->be_entry_reindex)
659 return glueBack->be_entry_reindex (glueBack, id);
667 slap_overinst *on = glue_tool_inst( b0->bd_info );
668 glueinfo *gi = on->on_bi.bi_private;
669 BackendInfo *bi = b0->bd_info;
672 /* just sync everyone */
673 for (i = 0; i<gi->gi_nodes; i++)
674 if (gi->gi_n[i].gn_be->be_sync)
675 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
676 b0->bd_info = on->on_info->oi_orig;
688 slap_overinst *on = (slap_overinst *)be->bd_info;
689 slap_overinfo *oi = on->on_info;
692 gi = ch_calloc( 1, sizeof(glueinfo));
693 on->on_bi.bi_private = gi;
694 dnParent( be->be_nsuffix, &gi->gi_pdn );
696 /* Currently the overlay framework doesn't handle these entry points
697 * but we need them....
699 oi->oi_bi.bi_open = glue_open;
700 oi->oi_bi.bi_close = glue_close;
702 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
704 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
705 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
706 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
707 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
708 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
709 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
710 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
711 oi->oi_bi.bi_tool_sync = glue_tool_sync;
713 /*FIXME : need to add support */
714 oi->oi_bi.bi_tool_dn2id_get = 0;
715 oi->oi_bi.bi_tool_id2entry_get = 0;
716 oi->oi_bi.bi_tool_entry_modify = 0;
726 slap_overinst *on = (slap_overinst *)be->bd_info;
727 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
730 return SLAP_CB_CONTINUE;
738 slap_overinst *on = (slap_overinst *)be->bd_info;
739 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
742 for ( i=0; i<gi->gi_nodes; i++ ) {
745 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
755 slap_overinst *on = (slap_overinst *)be->bd_info;
757 on->on_info->oi_bi.bi_db_close = NULL;
770 slap_overinst *on = (slap_overinst *)be->bd_info;
771 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
773 /* redundant; could be applied just once */
774 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
776 if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
777 int i, async = 0, advertise = 0;
779 struct berval bv, dn;
783 fprintf( stderr, "%s: line %d: too few arguments in "
784 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
787 for ( i = 2; i < argc; i++ ) {
788 if ( strcasecmp( argv[i], "async" ) == 0 ) {
791 } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
795 fprintf( stderr, "%s: line %d: unrecognized option "
796 "\"%s\" ignored.\n", fname, lineno, argv[i] );
799 ber_str2bv( argv[1], 0, 0, &bv );
800 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
801 fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
804 b2 = select_backend( &dn, 0, 1 );
806 fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
807 fname, lineno, argv[1] );
810 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
812 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
814 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
815 gi->gi_nodes * sizeof(gluenode));
816 gi->gi_n[gi->gi_nodes].gn_bx = b2 - backendDB;
817 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
818 gi->gi_n[gi->gi_nodes].gn_async = async;
820 on->on_bi.bi_private = gi;
823 return SLAP_CONF_UNKNOWN;
829 glue.on_bi.bi_type = "glue";
831 glue.on_bi.bi_db_init = glue_db_init;
832 glue.on_bi.bi_db_config = glue_db_config;
833 glue.on_bi.bi_db_open = glue_db_open;
834 glue.on_bi.bi_db_close = glue_db_close;
835 glue.on_bi.bi_db_destroy = glue_db_destroy;
837 glue.on_bi.bi_op_search = glue_op_search;
838 glue.on_bi.bi_op_modify = glue_op_func;
839 glue.on_bi.bi_op_modrdn = glue_op_func;
840 glue.on_bi.bi_op_add = glue_op_func;
841 glue.on_bi.bi_op_delete = glue_op_func;
843 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
844 glue.on_bi.bi_chk_controls = glue_chk_controls;
846 return overlay_register( &glue );
849 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
851 init_module( int argc, char *argv[] )
855 #endif /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
857 #endif /* defined(SLAPD_OVER_GLUE */