1 /* glue.c - backend glue overlay */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2004 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;
158 glue_op_search ( Operation *op, SlapReply *rs )
160 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
161 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
162 BackendDB *b0 = op->o_bd;
163 BackendDB *b1 = NULL, *btmp;
164 BackendInfo *bi0 = op->o_bd->bd_info;
167 glue_state gs = {0, 0, 0, NULL, 0, NULL};
168 slap_callback cb = { NULL, glue_op_response, NULL, NULL };
169 int scope0, slimit0, tlimit0;
170 struct berval dn, ndn, *pdn;
174 cb.sc_next = op->o_callback;
176 stoptime = slap_get_time () + op->ors_tlimit;
178 op->o_bd = glue_back_select (b0, &op->o_req_ndn);
179 b0->bd_info = on->on_info->oi_orig;
181 switch (op->ors_scope) {
182 case LDAP_SCOPE_BASE:
183 return SLAP_CB_CONTINUE;
185 case LDAP_SCOPE_ONELEVEL:
186 case LDAP_SCOPE_SUBTREE:
187 #ifdef LDAP_SCOPE_SUBORDINATE
188 case LDAP_SCOPE_SUBORDINATE: /* FIXME */
192 if (op->o_bd && op->o_bd->be_search) {
193 rs->sr_err = op->o_bd->be_search( op, rs );
195 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
196 "No search target found");
201 op->o_callback = &cb;
202 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
203 scope0 = op->ors_scope;
204 slimit0 = gs.slimit = op->ors_slimit;
205 tlimit0 = op->ors_tlimit;
211 * Execute in reverse order, most general first
213 for (i = gi->gi_nodes; i >= 0; i--) {
214 if ( i == gi->gi_nodes ) {
218 btmp = gi->gi_n[i].gn_be;
219 pdn = &gi->gi_n[i].gn_pdn;
221 if (!btmp || !btmp->be_search)
223 if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
225 if (tlimit0 != SLAP_NO_LIMIT) {
226 op->ors_tlimit = stoptime - slap_get_time ();
227 if (op->ors_tlimit <= 0) {
228 rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
232 if (slimit0 != SLAP_NO_LIMIT) {
233 op->ors_slimit = slimit0 - rs->sr_nentries;
234 if (op->ors_slimit < 0) {
235 rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
248 assert( op->o_bd->be_suffix );
249 assert( op->o_bd->be_nsuffix );
251 if (scope0 == LDAP_SCOPE_ONELEVEL &&
254 op->ors_scope = LDAP_SCOPE_BASE;
255 op->o_req_dn = op->o_bd->be_suffix[0];
256 op->o_req_ndn = op->o_bd->be_nsuffix[0];
257 rs->sr_err = op->o_bd->be_search(op, rs);
259 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
260 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
262 rs->sr_err = op->o_bd->be_search( op, rs );
264 } else if (scope0 == LDAP_SCOPE_SUBTREE &&
265 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
267 op->o_req_dn = op->o_bd->be_suffix[0];
268 op->o_req_ndn = op->o_bd->be_nsuffix[0];
269 rs->sr_err = op->o_bd->be_search( op, rs );
270 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
271 gs.err = LDAP_SUCCESS;
274 } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
275 rs->sr_err = op->o_bd->be_search( op, rs );
281 * Add errors that should result in dropping
284 case LDAP_SIZELIMIT_EXCEEDED:
285 case LDAP_TIMELIMIT_EXCEEDED:
286 case LDAP_ADMINLIMIT_EXCEEDED:
287 case LDAP_NO_SUCH_OBJECT:
295 op->ors_scope = scope0;
296 op->ors_slimit = slimit0;
297 op->ors_tlimit = tlimit0;
303 if ( !op->o_abandon ) {
304 op->o_callback = cb.sc_next;
306 rs->sr_matched = gs.matched;
307 rs->sr_ref = gs.refs;
309 send_ldap_result( op, rs );
313 op->o_bd->bd_info = bi0;
317 ber_bvarray_free(gs.refs);
321 static BackendDB toolDB;
324 glue_tool_entry_open (
329 slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
331 /* We don't know which backend to talk to yet, so just
332 * remember the mode and move on...
338 toolDB.bd_info = oi->oi_orig;
344 glue_tool_entry_close (
351 if (!glueBack->be_entry_close)
353 rc = glueBack->be_entry_close (glueBack);
358 static slap_overinst *
363 slap_overinfo *oi = (slap_overinfo *)bi;
366 for ( on = oi->oi_list; on; on=on->on_next ) {
367 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
373 /* This function will only be called in tool mode */
379 slap_overinst *on = glue_tool_inst( bi );
380 glueinfo *gi = on->on_bi.bi_private;
383 /* If we were invoked in tool mode, open all the underlying backends */
384 if (slapMode & SLAP_TOOL_MODE) {
385 for ( i=0; i<gi->gi_nodes; i++ ) {
386 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
387 rc = backend_startup( gi->gi_n[i].gn_be );
390 } /* other case is impossible */
394 /* This function will only be called in tool mode */
400 slap_overinst *on = glue_tool_inst( bi );
401 glueinfo *gi = on->on_bi.bi_private;
404 if (slapMode & SLAP_TOOL_MODE) {
405 for ( i=0; i<gi->gi_nodes; i++ ) {
406 rc = backend_shutdown( gi->gi_n[i].gn_be );
414 glue_tool_entry_first (
418 slap_overinst *on = glue_tool_inst( b0->bd_info );
419 glueinfo *gi = on->on_bi.bi_private;
422 /* If we're starting from scratch, start at the most general */
424 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
427 for (i = gi->gi_nodes-1; i >= 0; i--) {
428 if (gi->gi_n[i].gn_be->be_entry_open &&
429 gi->gi_n[i].gn_be->be_entry_first) {
430 glueBack = gi->gi_n[i].gn_be;
436 if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
437 glueBack->be_entry_open (glueBack, glueMode) != 0)
440 return glueBack->be_entry_first (glueBack);
444 glue_tool_entry_next (
448 slap_overinst *on = glue_tool_inst( b0->bd_info );
449 glueinfo *gi = on->on_bi.bi_private;
453 if (!glueBack || !glueBack->be_entry_next)
456 rc = glueBack->be_entry_next (glueBack);
458 /* If we ran out of entries in one database, move on to the next */
460 if ( glueBack && glueBack->be_entry_close )
461 glueBack->be_entry_close (glueBack);
462 for (i=0; i<gi->gi_nodes; i++) {
463 if (gi->gi_n[i].gn_be == glueBack)
470 glueBack = gi->gi_n[i-1].gn_be;
471 rc = glue_tool_entry_first (b0);
478 glue_tool_entry_get (
483 if (!glueBack || !glueBack->be_entry_get)
486 return glueBack->be_entry_get (glueBack, id);
490 glue_tool_entry_put (
500 b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
501 be = glue_back_select (&b2, &e->e_nname);
502 if ( be == &b2 ) be = &toolDB;
504 if (!be->be_entry_put)
508 rc = be->be_entry_open (be, glueMode);
511 } else if (be != glueBack) {
512 /* If this entry belongs in a different branch than the
513 * previous one, close the current database and open the
516 glueBack->be_entry_close (glueBack);
517 rc = be->be_entry_open (be, glueMode);
522 return be->be_entry_put (be, e, text);
526 glue_tool_entry_reindex (
531 if (!glueBack || !glueBack->be_entry_reindex)
534 return glueBack->be_entry_reindex (glueBack, id);
542 slap_overinst *on = glue_tool_inst( b0->bd_info );
543 glueinfo *gi = on->on_bi.bi_private;
546 /* just sync everyone */
547 for (i = 0; i<gi->gi_nodes; i++)
548 if (gi->gi_n[i].gn_be->be_sync)
549 gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
558 slap_overinst *on = (slap_overinst *)be->bd_info;
559 slap_overinfo *oi = on->on_info;
562 gi = ch_calloc( 1, sizeof(glueinfo));
563 on->on_bi.bi_private = gi;
564 dnParent( be->be_nsuffix, &gi->gi_pdn );
566 /* Currently the overlay framework doesn't handle these entry points
567 * but we need them....
569 oi->oi_bi.bi_open = glue_open;
570 oi->oi_bi.bi_close = glue_close;
572 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
573 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
574 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
575 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
576 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
577 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
578 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
579 oi->oi_bi.bi_tool_sync = glue_tool_sync;
581 /*FIXME : need to add support */
582 oi->oi_bi.bi_tool_dn2id_get = 0;
583 oi->oi_bi.bi_tool_id2entry_get = 0;
584 oi->oi_bi.bi_tool_entry_modify = 0;
594 slap_overinst *on = (slap_overinst *)be->bd_info;
595 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
598 return SLAP_CB_CONTINUE;
606 slap_overinst *on = (slap_overinst *)be->bd_info;
607 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
610 for ( i=0; i<gi->gi_nodes; i++ ) {
611 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
625 slap_overinst *on = (slap_overinst *)be->bd_info;
626 glueinfo *gi = (glueinfo *)on->on_bi.bi_private;
628 if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
631 struct berval bv, dn;
635 fprintf( stderr, "%s: line %d: too few arguments in "
636 "\"glue-sub <suffixDN> [async]\"\n", fname, lineno );
640 if ( strcasecmp( argv[2], "async" )) {
641 fprintf( stderr, "%s: line %d: unrecognized option "
642 "\"%s\" ignored.\n", fname, lineno, argv[2] );
647 ber_str2bv( argv[1], 0, 0, &bv );
648 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
649 fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
652 b2 = select_backend( &dn, 0, 1 );
654 fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
655 fname, lineno, argv[1] );
658 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
659 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
660 gi->gi_nodes * sizeof(gluenode));
661 gi->gi_n[gi->gi_nodes].gn_bx = b2 - backendDB;
662 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
663 gi->gi_n[gi->gi_nodes].gn_async = async;
665 on->on_bi.bi_private = gi;
668 return SLAP_CONF_UNKNOWN;
674 glue.on_bi.bi_type = "glue";
676 glue.on_bi.bi_db_init = glue_db_init;
677 glue.on_bi.bi_db_config = glue_db_config;
678 glue.on_bi.bi_db_open = glue_db_open;
679 glue.on_bi.bi_db_destroy = glue_db_destroy;
681 glue.on_bi.bi_op_search = glue_op_search;
683 return overlay_register( &glue );
686 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
688 init_module( int argc, char *argv[] )
692 #endif /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
694 #endif /* defined(SLAPD_OVER_GLUE */