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;
227 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
228 b0->bd_info = on->on_info->oi_orig;
230 if ( op->o_bd->bd_info->bi_chk_controls )
231 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
233 rc = SLAP_CB_CONTINUE;
236 op->o_bd->bd_info = bi0;
241 glue_op_search ( Operation *op, SlapReply *rs )
243 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
244 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
245 BackendDB *b0 = op->o_bd;
246 BackendDB *b1 = NULL, *btmp;
247 BackendInfo *bi0 = op->o_bd->bd_info;
250 glue_state gs = {0, 0, 0, NULL, 0, NULL};
251 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
252 int scope0, slimit0, tlimit0;
253 struct berval dn, ndn, *pdn;
257 cb.sc_next = op->o_callback;
259 stoptime = slap_get_time () + op->ors_tlimit;
261 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
262 b0->bd_info = on->on_info->oi_orig;
264 switch (op->ors_scope) {
265 case LDAP_SCOPE_BASE:
266 return SLAP_CB_CONTINUE;
268 case LDAP_SCOPE_ONELEVEL:
269 case LDAP_SCOPE_SUBTREE:
270 #ifdef LDAP_SCOPE_SUBORDINATE
271 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
276 if (op->o_bd && op->o_bd->be_search) {
277 rs->sr_err = op->o_bd->be_search( op, rs );
279 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
280 "No search target found");
286 op->o_callback = &cb;
287 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
288 scope0 = op->ors_scope;
289 slimit0 = gs.slimit = op->ors_slimit;
290 tlimit0 = op->ors_tlimit;
296 * Execute in reverse order, most general first
298 for (i = gi->gi_nodes; i >= 0; i--) {
299 if ( i == gi->gi_nodes ) {
303 btmp = gi->gi_n[i].gn_be;
304 pdn = &gi->gi_n[i].gn_pdn;
306 if (!btmp || !btmp->be_search)
308 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
310 if (tlimit0 != SLAP_NO_LIMIT) {
311 op->ors_tlimit = stoptime - slap_get_time ();
312 if (op->ors_tlimit <= 0) {
313 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
317 if (slimit0 != SLAP_NO_LIMIT) {
318 op->ors_slimit = slimit0 - rs->sr_nentries;
319 if (op->ors_slimit < 0) {
320 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
333 assert( op->o_bd->be_suffix );
334 assert( op->o_bd->be_nsuffix );
336 if (scope0 == LDAP_SCOPE_ONELEVEL &&
339 op->ors_scope = LDAP_SCOPE_BASE;
340 op->o_req_dn = op->o_bd->be_suffix[0];
341 op->o_req_ndn = op->o_bd->be_nsuffix[0];
342 rs->sr_err = op->o_bd->be_search(op, rs);
344 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
345 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
347 rs->sr_err = op->o_bd->be_search( op, rs );
349 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
350 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
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 );
355 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
356 gs.err = LDAP_SUCCESS;
359 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
360 rs->sr_err = op->o_bd->be_search( op, rs );
366 * Add errors that should result in dropping
369 case LDAP_SIZELIMIT_EXCEEDED:
370 case LDAP_TIMELIMIT_EXCEEDED:
371 case LDAP_ADMINLIMIT_EXCEEDED:
372 case LDAP_NO_SUCH_OBJECT:
380 op->ors_scope = scope0;
381 op->ors_slimit = slimit0;
382 op->ors_tlimit = tlimit0;
388 if ( !op->o_abandon ) {
389 op->o_callback = cb.sc_next;
391 rs->sr_matched = gs.matched;
392 rs->sr_ref = gs.refs;
394 send_ldap_result( op, rs );
398 op->o_bd->bd_info = bi0;
402 ber_bvarray_free(gs.refs);
406 static BackendDB toolDB;
409 glue_tool_entry_open (
414 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
416 /* We don't know which backend to talk to yet, so just
417 * remember the mode and move on...
423 toolDB.bd_info = oi->oi_orig;
429 glue_tool_entry_close (
436 if (!glueBack->be_entry_close)
438 rc = glueBack->be_entry_close (glueBack);
443 static slap_overinst *
448 slap_overinfo *oi = (slap_overinfo *)bi;
451 for ( on = oi->oi_list; on; on=on->on_next ) {
452 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
458 /* This function will only be called in tool mode */
464 slap_overinst *on = glue_tool_inst( bi );
465 glueinfo *gi = on->on_bi.bi_private;
466 static int glueOpened = 0;
469 if (glueOpened) return 0;
473 /* If we were invoked in tool mode, open all the underlying backends */
474 if (slapMode & SLAP_TOOL_MODE) {
475 rc = backend_startup( NULL );
476 } /* other case is impossible */
480 /* This function will only be called in tool mode */
486 slap_overinst *on = glue_tool_inst( bi );
487 glueinfo *gi = on->on_bi.bi_private;
488 static int glueClosed = 0;
491 if (glueClosed) return 0;
495 if (slapMode & SLAP_TOOL_MODE) {
496 rc = backend_shutdown( NULL );
502 glue_entry_release_rw (
513 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
514 op->o_bd = glue_back_select (&b2, &e->e_nname);
516 rc = op->o_bd->be_release( op, e, rw );
522 glue_tool_entry_first (
526 slap_overinst *on = glue_tool_inst( b0->bd_info );
527 glueinfo *gi = on->on_bi.bi_private;
530 /* If we're starting from scratch, start at the most general */
532 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
535 for (i = gi->gi_nodes-1; i >= 0; i--) {
536 if (gi->gi_n[i].gn_be->be_entry_open &&
537 gi->gi_n[i].gn_be->be_entry_first) {
538 glueBack = gi->gi_n[i].gn_be;
544 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
545 glueBack->be_entry_open (glueBack, glueMode) != 0)
548 return glueBack->be_entry_first (glueBack);
552 glue_tool_entry_next (
556 slap_overinst *on = glue_tool_inst( b0->bd_info );
557 glueinfo *gi = on->on_bi.bi_private;
561 if (!glueBack || !glueBack->be_entry_next)
564 rc = glueBack->be_entry_next (glueBack);
566 /* If we ran out of entries in one database, move on to the next */
568 if ( glueBack && glueBack->be_entry_close )
569 glueBack->be_entry_close (glueBack);
570 for (i=0; i<gi->gi_nodes; i++) {
571 if (gi->gi_n[i].gn_be == glueBack)
578 glueBack = gi->gi_n[i-1].gn_be;
579 rc = glue_tool_entry_first (b0);
586 glue_tool_entry_get (
591 if (!glueBack || !glueBack->be_entry_get)
594 return glueBack->be_entry_get (glueBack, id);
598 glue_tool_entry_put (
608 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
609 be = glue_back_select (&b2, &e->e_nname);
610 if ( be == &b2 ) be = &toolDB;
612 if (!be->be_entry_put)
616 rc = be->be_entry_open (be, glueMode);
619 } else if (be != glueBack) {
620 /* If this entry belongs in a different branch than the
621 * previous one, close the current database and open the
624 glueBack->be_entry_close (glueBack);
625 rc = be->be_entry_open (be, glueMode);
630 return be->be_entry_put (be, e, text);
634 glue_tool_entry_reindex (
639 if (!glueBack || !glueBack->be_entry_reindex)
642 return glueBack->be_entry_reindex (glueBack, id);
650 slap_overinst *on = glue_tool_inst( b0->bd_info );
651 glueinfo *gi = on->on_bi.bi_private;
652 BackendInfo *bi = b0->bd_info;
655 /* just sync everyone */
656 for (i = 0; i<gi->gi_nodes; i++)
657 if (gi->gi_n[i].gn_be->be_sync)
658 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
659 b0->bd_info = on->on_info->oi_orig;
671 slap_overinst *on = (slap_overinst *)be->bd_info;
672 slap_overinfo *oi = on->on_info;
675 gi = ch_calloc( 1, sizeof(glueinfo));
676 on->on_bi.bi_private = gi;
677 dnParent( be->be_nsuffix, &gi->gi_pdn );
679 /* Currently the overlay framework doesn't handle these entry points
680 * but we need them....
682 oi->oi_bi.bi_open = glue_open;
683 oi->oi_bi.bi_close = glue_close;
685 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
687 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
688 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
689 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
690 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
691 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
692 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
693 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
694 oi->oi_bi.bi_tool_sync = glue_tool_sync;
696 /*FIXME : need to add support */
697 oi->oi_bi.bi_tool_dn2id_get = 0;
698 oi->oi_bi.bi_tool_id2entry_get = 0;
699 oi->oi_bi.bi_tool_entry_modify = 0;
709 slap_overinst *on = (slap_overinst *)be->bd_info;
710 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
713 return SLAP_CB_CONTINUE;
721 slap_overinst *on = (slap_overinst *)be->bd_info;
722 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
725 for ( i=0; i<gi->gi_nodes; i++ ) {
728 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
738 slap_overinst *on = (slap_overinst *)be->bd_info;
740 on->on_info->oi_bi.bi_db_close = NULL;
753 slap_overinst *on = (slap_overinst *)be->bd_info;
754 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
756 if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
759 struct berval bv, dn;
763 fprintf( stderr, "%s: line %d: too few arguments in "
764 "\"glue-sub <suffixDN> [async]\"\n", fname, lineno );
768 if ( strcasecmp( argv[2], "async" )) {
769 fprintf( stderr, "%s: line %d: unrecognized option "
770 "\"%s\" ignored.\n", fname, lineno, argv[2] );
775 ber_str2bv( argv[1], 0, 0, &bv );
776 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
777 fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
780 b2 = select_backend( &dn, 0, 1 );
782 fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
783 fname, lineno, argv[1] );
786 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
787 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
788 gi->gi_nodes * sizeof(gluenode));
789 gi->gi_n[gi->gi_nodes].gn_bx = b2 - backendDB;
790 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
791 gi->gi_n[gi->gi_nodes].gn_async = async;
793 on->on_bi.bi_private = gi;
796 return SLAP_CONF_UNKNOWN;
802 glue.on_bi.bi_type = "glue";
804 glue.on_bi.bi_db_init = glue_db_init;
805 glue.on_bi.bi_db_config = glue_db_config;
806 glue.on_bi.bi_db_open = glue_db_open;
807 glue.on_bi.bi_db_close = glue_db_close;
808 glue.on_bi.bi_db_destroy = glue_db_destroy;
810 glue.on_bi.bi_op_search = glue_op_search;
811 glue.on_bi.bi_op_modify = glue_op_func;
812 glue.on_bi.bi_op_modrdn = glue_op_func;
813 glue.on_bi.bi_op_add = glue_op_func;
814 glue.on_bi.bi_op_delete = glue_op_func;
816 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
817 glue.on_bi.bi_chk_controls = glue_chk_controls;
819 return overlay_register( &glue );
822 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
824 init_module( int argc, char *argv[] )
828 #endif /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
830 #endif /* defined(SLAPD_OVER_GLUE */