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 return SLAP_CB_CONTINUE;
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 );
342 assert( op->o_bd->be_nsuffix );
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 op->o_callback = cb.sc_next;
402 rs->sr_matched = gs.matched;
403 rs->sr_ref = gs.refs;
405 send_ldap_result( op, rs );
409 op->o_bd->bd_info = bi0;
413 ber_bvarray_free(gs.refs);
417 static BackendDB toolDB;
420 glue_tool_entry_open (
425 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
427 /* We don't know which backend to talk to yet, so just
428 * remember the mode and move on...
434 toolDB.bd_info = oi->oi_orig;
440 glue_tool_entry_close (
447 if (!glueBack->be_entry_close)
449 rc = glueBack->be_entry_close (glueBack);
454 static slap_overinst *
459 slap_overinfo *oi = (slap_overinfo *)bi;
462 for ( on = oi->oi_list; on; on=on->on_next ) {
463 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
469 /* This function will only be called in tool mode */
475 slap_overinst *on = glue_tool_inst( bi );
476 glueinfo *gi = on->on_bi.bi_private;
477 static int glueOpened = 0;
480 if (glueOpened) return 0;
484 /* If we were invoked in tool mode, open all the underlying backends */
485 if (slapMode & SLAP_TOOL_MODE) {
486 rc = backend_startup( NULL );
487 } /* other case is impossible */
491 /* This function will only be called in tool mode */
497 slap_overinst *on = glue_tool_inst( bi );
498 glueinfo *gi = on->on_bi.bi_private;
499 static int glueClosed = 0;
502 if (glueClosed) return 0;
506 if (slapMode & SLAP_TOOL_MODE) {
507 rc = backend_shutdown( NULL );
513 glue_entry_release_rw (
524 b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
525 op->o_bd = glue_back_select (&b2, &e->e_nname);
527 rc = op->o_bd->be_release( op, e, rw );
533 glue_tool_entry_first (
537 slap_overinst *on = glue_tool_inst( b0->bd_info );
538 glueinfo *gi = on->on_bi.bi_private;
541 /* If we're starting from scratch, start at the most general */
543 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
546 for (i = gi->gi_nodes-1; i >= 0; i--) {
547 if (gi->gi_n[i].gn_be->be_entry_open &&
548 gi->gi_n[i].gn_be->be_entry_first) {
549 glueBack = gi->gi_n[i].gn_be;
555 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
556 glueBack->be_entry_open (glueBack, glueMode) != 0)
559 return glueBack->be_entry_first (glueBack);
563 glue_tool_entry_next (
567 slap_overinst *on = glue_tool_inst( b0->bd_info );
568 glueinfo *gi = on->on_bi.bi_private;
572 if (!glueBack || !glueBack->be_entry_next)
575 rc = glueBack->be_entry_next (glueBack);
577 /* If we ran out of entries in one database, move on to the next */
579 if ( glueBack && glueBack->be_entry_close )
580 glueBack->be_entry_close (glueBack);
581 for (i=0; i<gi->gi_nodes; i++) {
582 if (gi->gi_n[i].gn_be == glueBack)
589 glueBack = gi->gi_n[i-1].gn_be;
590 rc = glue_tool_entry_first (b0);
597 glue_tool_entry_get (
602 if (!glueBack || !glueBack->be_entry_get)
605 return glueBack->be_entry_get (glueBack, id);
609 glue_tool_entry_put (
619 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
620 be = glue_back_select (&b2, &e->e_nname);
621 if ( be == &b2 ) be = &toolDB;
623 if (!be->be_entry_put)
627 rc = be->be_entry_open (be, glueMode);
630 } else if (be != glueBack) {
631 /* If this entry belongs in a different branch than the
632 * previous one, close the current database and open the
635 glueBack->be_entry_close (glueBack);
636 rc = be->be_entry_open (be, glueMode);
641 return be->be_entry_put (be, e, text);
645 glue_tool_entry_reindex (
650 if (!glueBack || !glueBack->be_entry_reindex)
653 return glueBack->be_entry_reindex (glueBack, id);
661 slap_overinst *on = glue_tool_inst( b0->bd_info );
662 glueinfo *gi = on->on_bi.bi_private;
663 BackendInfo *bi = b0->bd_info;
666 /* just sync everyone */
667 for (i = 0; i<gi->gi_nodes; i++)
668 if (gi->gi_n[i].gn_be->be_sync)
669 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
670 b0->bd_info = on->on_info->oi_orig;
682 slap_overinst *on = (slap_overinst *)be->bd_info;
683 slap_overinfo *oi = on->on_info;
686 gi = ch_calloc( 1, sizeof(glueinfo));
687 on->on_bi.bi_private = gi;
688 dnParent( be->be_nsuffix, &gi->gi_pdn );
690 /* Currently the overlay framework doesn't handle these entry points
691 * but we need them....
693 oi->oi_bi.bi_open = glue_open;
694 oi->oi_bi.bi_close = glue_close;
696 oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
698 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
699 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
700 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
701 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
702 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
703 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
704 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
705 oi->oi_bi.bi_tool_sync = glue_tool_sync;
707 /*FIXME : need to add support */
708 oi->oi_bi.bi_tool_dn2id_get = 0;
709 oi->oi_bi.bi_tool_id2entry_get = 0;
710 oi->oi_bi.bi_tool_entry_modify = 0;
720 slap_overinst *on = (slap_overinst *)be->bd_info;
721 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
724 return SLAP_CB_CONTINUE;
732 slap_overinst *on = (slap_overinst *)be->bd_info;
733 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
736 for ( i=0; i<gi->gi_nodes; i++ ) {
739 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
749 slap_overinst *on = (slap_overinst *)be->bd_info;
751 on->on_info->oi_bi.bi_db_close = NULL;
764 slap_overinst *on = (slap_overinst *)be->bd_info;
765 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
767 /* redundant; could be applied just once */
768 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
770 if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
771 int i, async = 0, advertise = 0;
773 struct berval bv, dn;
777 fprintf( stderr, "%s: line %d: too few arguments in "
778 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
781 for ( i = 2; i < argc; i++ ) {
782 if ( strcasecmp( argv[i], "async" ) == 0 ) {
785 } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
789 fprintf( stderr, "%s: line %d: unrecognized option "
790 "\"%s\" ignored.\n", fname, lineno, argv[i] );
793 ber_str2bv( argv[1], 0, 0, &bv );
794 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
795 fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
798 b2 = select_backend( &dn, 0, 1 );
800 fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
801 fname, lineno, argv[1] );
804 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
806 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
808 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
809 gi->gi_nodes * sizeof(gluenode));
810 gi->gi_n[gi->gi_nodes].gn_bx = b2 - backendDB;
811 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
812 gi->gi_n[gi->gi_nodes].gn_async = async;
814 on->on_bi.bi_private = gi;
817 return SLAP_CONF_UNKNOWN;
823 glue.on_bi.bi_type = "glue";
825 glue.on_bi.bi_db_init = glue_db_init;
826 glue.on_bi.bi_db_config = glue_db_config;
827 glue.on_bi.bi_db_open = glue_db_open;
828 glue.on_bi.bi_db_close = glue_db_close;
829 glue.on_bi.bi_db_destroy = glue_db_destroy;
831 glue.on_bi.bi_op_search = glue_op_search;
832 glue.on_bi.bi_op_modify = glue_op_func;
833 glue.on_bi.bi_op_modrdn = glue_op_func;
834 glue.on_bi.bi_op_add = glue_op_func;
835 glue.on_bi.bi_op_delete = glue_op_func;
837 glue.on_bi.bi_chk_referrals = glue_chk_referrals;
838 glue.on_bi.bi_chk_controls = glue_chk_controls;
840 return overlay_register( &glue );
843 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
845 init_module( int argc, char *argv[] )
849 #endif /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
851 #endif /* defined(SLAPD_OVER_GLUE */