]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
fix previous commit
[openldap] / servers / slapd / backglue.c
1 /* backglue.c - backend glue */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 /*
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.
21  *
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.
26  *  -- Howard Chu
27  */
28
29 #include "portable.h"
30
31 #include <stdio.h>
32
33 #include <ac/string.h>
34 #include <ac/socket.h>
35
36 #define SLAPD_TOOLS
37 #include "slap.h"
38
39 typedef struct gluenode {
40         BackendDB *gn_be;
41         struct berval gn_pdn;
42 } gluenode;
43
44 typedef struct glueinfo {
45         int gi_nodes;
46         struct berval gi_pdn;
47         gluenode gi_n[1];
48 } glueinfo;
49
50 static slap_overinst    glue;
51
52 static int glueMode;
53 static BackendDB *glueBack;
54
55 static slap_response glue_op_response;
56
57 /* Just like select_backend, but only for our backends */
58 static BackendDB *
59 glue_back_select (
60         BackendDB *be,
61         struct berval *dn
62 )
63 {
64         slap_overinst   *on = (slap_overinst *)be->bd_info;
65         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
66         int i;
67
68         for (i = gi->gi_nodes-1; i >= 0; i--) {
69                 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
70
71                 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
72                         return gi->gi_n[i].gn_be;
73                 }
74         }
75         be->bd_info = on->on_info->oi_orig;
76         return be;
77 }
78
79
80 typedef struct glue_state {
81         char *matched;
82         BerVarray refs;
83         LDAPControl **ctrls;
84         int err;
85         int matchlen;
86         int nrefs;
87         int nctrls;
88 } glue_state;
89
90 static int
91 glue_op_response ( Operation *op, SlapReply *rs )
92 {
93         glue_state *gs = op->o_callback->sc_private;
94
95         switch(rs->sr_type) {
96         case REP_SEARCH:
97         case REP_SEARCHREF:
98         case REP_INTERMEDIATE:
99                 return SLAP_CB_CONTINUE;
100
101         default:
102                 if (rs->sr_err == LDAP_SUCCESS ||
103                         rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
104                         rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
105                         rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
106                         rs->sr_err == LDAP_NO_SUCH_OBJECT ||
107                         gs->err != LDAP_SUCCESS)
108                         gs->err = rs->sr_err;
109                 if (gs->err == LDAP_SUCCESS && gs->matched) {
110                         ch_free (gs->matched);
111                         gs->matched = NULL;
112                         gs->matchlen = 0;
113                 }
114                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
115                         int len;
116                         len = strlen (rs->sr_matched);
117                         if (len > gs->matchlen) {
118                                 if (gs->matched)
119                                         ch_free (gs->matched);
120                                 gs->matched = ch_strdup (rs->sr_matched);
121                                 gs->matchlen = len;
122                         }
123                 }
124                 if (rs->sr_ref) {
125                         int i, j, k;
126                         BerVarray new;
127
128                         for (i=0; rs->sr_ref[i].bv_val; i++);
129
130                         j = gs->nrefs;
131                         if (!j) {
132                                 new = ch_malloc ((i+1)*sizeof(struct berval));
133                         } else {
134                                 new = ch_realloc(gs->refs,
135                                         (j+i+1)*sizeof(struct berval));
136                         }
137                         for (k=0; k<i; j++,k++) {
138                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
139                         }
140                         new[j].bv_val = NULL;
141                         gs->nrefs = j;
142                         gs->refs = new;
143                 }
144                 if (rs->sr_ctrls) {
145                         int i, j, k;
146                         LDAPControl **newctrls;
147
148                         for (i=0; rs->sr_ctrls[i]; i++);
149
150                         j = gs->nctrls;
151                         if (!j) {
152                                 newctrls = ch_malloc((i+1)*sizeof(LDAPControl *));
153                         } else {
154                                 newctrls = ch_realloc(gs->ctrls,
155                                         (j+i+1)*sizeof(LDAPControl *));
156                         }
157                         for (k=0; k<i; j++,k++) {
158                                 newctrls[j] = ch_malloc(sizeof(LDAPControl));
159                                 *newctrls[j] = *rs->sr_ctrls[k];
160                                 if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ))
161                                         ber_dupbv( &newctrls[j]->ldctl_value,
162                                                 &rs->sr_ctrls[k]->ldctl_value );
163                         }
164                         newctrls[j] = NULL;
165                         gs->nctrls = j;
166                         gs->ctrls = newctrls;
167                 }
168         }
169         return 0;
170 }
171
172 static int
173 glue_op_func ( Operation *op, SlapReply *rs )
174 {
175         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
176         BackendDB *b0 = op->o_bd;
177         BackendInfo *bi0 = op->o_bd->bd_info;
178         BI_op_modify **func;
179         slap_operation_t which = op_bind;
180         int rc;
181
182         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
183
184         /* If we're on the master backend, let overlay framework handle it */
185         if ( op->o_bd == b0 )
186                 return SLAP_CB_CONTINUE;
187
188         b0->bd_info = on->on_info->oi_orig;
189
190         switch(op->o_tag) {
191         case LDAP_REQ_ADD: which = op_add; break;
192         case LDAP_REQ_DELETE: which = op_delete; break;
193         case LDAP_REQ_MODIFY: which = op_modify; break;
194         case LDAP_REQ_MODRDN: which = op_modrdn; break;
195         default: assert( 0 ); break;
196         }
197
198         func = &op->o_bd->bd_info->bi_op_bind;
199         if ( func[which] )
200                 rc = func[which]( op, rs );
201         else
202                 rc = SLAP_CB_CONTINUE;
203
204         op->o_bd = b0;
205         op->o_bd->bd_info = bi0;
206         return rc;
207 }
208
209 static int
210 glue_chk_referrals ( Operation *op, SlapReply *rs )
211 {
212         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
213         BackendDB *b0 = op->o_bd;
214         BackendInfo *bi0 = op->o_bd->bd_info;
215         int rc;
216
217         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
218         if ( op->o_bd == b0 )
219                 return SLAP_CB_CONTINUE;
220
221         b0->bd_info = on->on_info->oi_orig;
222
223         if ( op->o_bd->bd_info->bi_chk_referrals )
224                 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
225         else
226                 rc = SLAP_CB_CONTINUE;
227
228         op->o_bd = b0;
229         op->o_bd->bd_info = bi0;
230         return rc;
231 }
232
233 static int
234 glue_chk_controls ( Operation *op, SlapReply *rs )
235 {
236         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
237         BackendDB *b0 = op->o_bd;
238         BackendInfo *bi0 = op->o_bd->bd_info;
239         int rc = SLAP_CB_CONTINUE;
240
241         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
242         if ( op->o_bd == b0 )
243                 return SLAP_CB_CONTINUE;
244
245         b0->bd_info = on->on_info->oi_orig;
246
247         /* if the subordinate database has overlays, the bi_chk_controls()
248          * hook is actually over_aux_chk_controls(); in case it actually
249          * wraps a missing hok, we need to mimic the behavior
250          * of the frontend applied to that database */
251         if ( op->o_bd->bd_info->bi_chk_controls ) {
252                 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
253         }
254
255         
256         if ( rc == SLAP_CB_CONTINUE ) {
257                 rc = backend_check_controls( op, rs );
258         }
259
260         op->o_bd = b0;
261         op->o_bd->bd_info = bi0;
262         return rc;
263 }
264
265 static int
266 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
267         slap_overinst *on )
268 {
269         /* Process any overlays on the master backend */
270         if ( op->o_bd == b0 && on->on_next ) {
271                 BackendInfo *bi = op->o_bd->bd_info;
272                 int rc = SLAP_CB_CONTINUE;
273                 for ( on=on->on_next; on; on=on->on_next ) {
274                         op->o_bd->bd_info = (BackendInfo *)on;
275                         if ( on->on_bi.bi_op_search ) {
276                                 rc = on->on_bi.bi_op_search( op, rs );
277                                 if ( rc != SLAP_CB_CONTINUE )
278                                         break;
279                         }
280                 }
281                 op->o_bd->bd_info = bi;
282                 if ( rc != SLAP_CB_CONTINUE )
283                         return rc;
284         }
285         return op->o_bd->be_search( op, rs );
286 }
287
288 static int
289 glue_op_search ( Operation *op, SlapReply *rs )
290 {
291         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
292         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
293         BackendDB *b0 = op->o_bd;
294         BackendDB *b1 = NULL, *btmp;
295         BackendInfo *bi0 = op->o_bd->bd_info;
296         int i;
297         long stoptime = 0, starttime;
298         glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
299         slap_callback cb = { NULL, glue_op_response, NULL, NULL };
300         int scope0, tlimit0;
301         struct berval dn, ndn, *pdn;
302
303         cb.sc_private = &gs;
304
305         cb.sc_next = op->o_callback;
306
307         starttime = op->o_time;
308         stoptime = slap_get_time () + op->ors_tlimit;
309
310         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
311         b0->bd_info = on->on_info->oi_orig;
312
313         switch (op->ors_scope) {
314         case LDAP_SCOPE_BASE:
315                 if ( op->o_bd == b0 )
316                         return SLAP_CB_CONTINUE;
317
318                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
319                 if (op->o_bd && op->o_bd->be_search) {
320                         rs->sr_err = op->o_bd->be_search( op, rs );
321                 }
322                 return rs->sr_err;
323
324         case LDAP_SCOPE_ONELEVEL:
325         case LDAP_SCOPE_SUBTREE:
326         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
327                 op->o_callback = &cb;
328                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
329                 scope0 = op->ors_scope;
330                 tlimit0 = op->ors_tlimit;
331                 dn = op->o_req_dn;
332                 ndn = op->o_req_ndn;
333                 b1 = op->o_bd;
334
335                 /*
336                  * Execute in reverse order, most specific first 
337                  */
338                 for (i = gi->gi_nodes; i >= 0; i--) {
339                         if ( i == gi->gi_nodes ) {
340                                 btmp = b0;
341                                 pdn = &gi->gi_pdn;
342                         } else {
343                                 btmp = gi->gi_n[i].gn_be;
344                                 pdn = &gi->gi_n[i].gn_pdn;
345                         }
346                         if (!btmp || !btmp->be_search)
347                                 continue;
348                         if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
349                                 continue;
350                         if (get_no_subordinate_glue(op) && btmp != b1)
351                                 continue;
352
353                         if (tlimit0 != SLAP_NO_LIMIT) {
354                                 op->o_time = slap_get_time();
355                                 op->ors_tlimit = stoptime - op->o_time;
356                                 if (op->ors_tlimit <= 0) {
357                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
358                                         break;
359                                 }
360                         }
361                         rs->sr_err = 0;
362                         /*
363                          * check for abandon 
364                          */
365                         if (op->o_abandon) {
366                                 goto end_of_loop;
367                         }
368                         op->o_bd = btmp;
369
370                         assert( op->o_bd->be_suffix != NULL );
371                         assert( op->o_bd->be_nsuffix != NULL );
372                         
373                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
374                                 dn_match(pdn, &ndn))
375                         {
376                                 op->ors_scope = LDAP_SCOPE_BASE;
377                                 op->o_req_dn = op->o_bd->be_suffix[0];
378                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
379                                 rs->sr_err = op->o_bd->be_search(op, rs);
380                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
381                                         gs.err = LDAP_SUCCESS;
382                                 }
383                                 op->o_req_dn = dn;
384                                 op->o_req_ndn = ndn;
385
386                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
387                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
388                         {
389                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
390
391                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
392                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
393                         {
394                                 op->o_req_dn = op->o_bd->be_suffix[0];
395                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
396                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
397                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
398                                         gs.err = LDAP_SUCCESS;
399                                 }
400                                 op->o_req_dn = dn;
401                                 op->o_req_ndn = ndn;
402
403                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
404                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
405                         }
406
407                         switch ( gs.err ) {
408
409                         /*
410                          * Add errors that should result in dropping
411                          * the search
412                          */
413                         case LDAP_SIZELIMIT_EXCEEDED:
414                         case LDAP_TIMELIMIT_EXCEEDED:
415                         case LDAP_ADMINLIMIT_EXCEEDED:
416                         case LDAP_NO_SUCH_OBJECT:
417 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
418                         case LDAP_X_CANNOT_CHAIN:
419 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
420                                 goto end_of_loop;
421                         
422                         default:
423                                 break;
424                         }
425                 }
426 end_of_loop:;
427                 op->ors_scope = scope0;
428                 op->ors_tlimit = tlimit0;
429                 op->o_time = starttime;
430                 op->o_req_dn = dn;
431                 op->o_req_ndn = ndn;
432
433                 break;
434         }
435         if ( op->o_abandon ) {
436                 rs->sr_err = SLAPD_ABANDON;
437         } else {
438                 op->o_callback = cb.sc_next;
439                 rs->sr_err = gs.err;
440                 rs->sr_matched = gs.matched;
441                 rs->sr_ref = gs.refs;
442                 rs->sr_ctrls = gs.ctrls;
443
444                 send_ldap_result( op, rs );
445         }
446
447         op->o_bd = b0;
448         op->o_bd->bd_info = bi0;
449         if (gs.matched)
450                 free (gs.matched);
451         if (gs.refs)
452                 ber_bvarray_free(gs.refs);
453         if (gs.ctrls) {
454                 for (i = gs.nctrls; --i >= 0; ) {
455                         if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
456                                 free(gs.ctrls[i]->ldctl_value.bv_val);
457                         free(gs.ctrls[i]);
458                 }
459                 free(gs.ctrls);
460         }
461         return rs->sr_err;
462 }
463
464 static BackendDB toolDB;
465
466 static int
467 glue_tool_entry_open (
468         BackendDB *b0,
469         int mode
470 )
471 {
472         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
473
474         /* We don't know which backend to talk to yet, so just
475          * remember the mode and move on...
476          */
477
478         glueMode = mode;
479         glueBack = NULL;
480         toolDB = *b0;
481         toolDB.bd_info = oi->oi_orig;
482
483         return 0;
484 }
485
486 static int
487 glue_tool_entry_close (
488         BackendDB *b0
489 )
490 {
491         int rc = 0;
492
493         if (glueBack) {
494                 if (!glueBack->be_entry_close)
495                         return 0;
496                 rc = glueBack->be_entry_close (glueBack);
497         }
498         return rc;
499 }
500
501 static slap_overinst *
502 glue_tool_inst(
503         BackendInfo *bi
504 )
505 {
506         slap_overinfo   *oi = (slap_overinfo *)bi;
507         slap_overinst   *on;
508
509         for ( on = oi->oi_list; on; on=on->on_next ) {
510                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
511                         return on;
512         }
513         return NULL;
514 }
515
516 /* This function will only be called in tool mode */
517 static int
518 glue_open (
519         BackendInfo *bi
520 )
521 {
522         slap_overinst *on = glue_tool_inst( bi );
523         glueinfo                *gi = on->on_bi.bi_private;
524         static int glueOpened = 0;
525         int i, j, same, bsame = 0, rc = 0;
526
527         if (glueOpened) return 0;
528
529         glueOpened = 1;
530
531         /* If we were invoked in tool mode, open all the underlying backends */
532         if (slapMode & SLAP_TOOL_MODE) {
533                 for (i = 0; i<gi->gi_nodes; i++) {
534                         same = 0;
535                         /* Same bi_open as our main backend? */
536                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
537                                 on->on_info->oi_orig->bi_open )
538                                 bsame = 1;
539
540                         /* Loop thru the bd_info's and make sure we only
541                          * invoke their bi_open functions once each.
542                          */
543                         for ( j = 0; j<i; j++ ) {
544                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
545                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
546                                         same = 1;
547                                         break;
548                                 }
549                         }
550                         /* OK, it's unique and non-NULL, call it. */
551                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
552                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
553                                         gi->gi_n[i].gn_be->bd_info );
554                         /* Let backend.c take care of the rest of startup */
555                         if ( !rc )
556                                 rc = backend_startup_one( gi->gi_n[i].gn_be );
557                         if ( rc ) break;
558                 }
559                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
560                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
561
562         } /* other case is impossible */
563         return rc;
564 }
565
566 /* This function will only be called in tool mode */
567 static int
568 glue_close (
569         BackendInfo *bi
570 )
571 {
572         static int glueClosed = 0;
573         int rc = 0;
574
575         if (glueClosed) return 0;
576
577         glueClosed = 1;
578
579         if (slapMode & SLAP_TOOL_MODE) {
580                 rc = backend_shutdown( NULL );
581         }
582         return rc;
583 }
584
585 static int
586 glue_entry_release_rw (
587         Operation *op,
588         Entry *e,
589         int rw
590 )
591 {
592         BackendDB *b0, b2;
593         int rc = -1;
594
595         b0 = op->o_bd;
596         b2 = *op->o_bd;
597         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
598         op->o_bd = glue_back_select (&b2, &e->e_nname);
599
600         if ( op->o_bd->be_release ) {
601                 rc = op->o_bd->be_release( op, e, rw );
602
603         } else {
604                 /* FIXME: mimic be_entry_release_rw
605                  * when no be_release() available */
606                 /* free entry */
607                 entry_free( e );
608                 rc = 0;
609         }
610         op->o_bd = b0;
611         return rc;
612 }
613
614 static ID
615 glue_tool_entry_first (
616         BackendDB *b0
617 )
618 {
619         slap_overinst   *on = glue_tool_inst( b0->bd_info );
620         glueinfo                *gi = on->on_bi.bi_private;
621         int i;
622
623         /* If we're starting from scratch, start at the most general */
624         if (!glueBack) {
625                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
626                         glueBack = &toolDB;
627                 } else {
628                         for (i = gi->gi_nodes-1; i >= 0; i--) {
629                                 if (gi->gi_n[i].gn_be->be_entry_open &&
630                                         gi->gi_n[i].gn_be->be_entry_first) {
631                                                 glueBack = gi->gi_n[i].gn_be;
632                                         break;
633                                 }
634                         }
635                 }
636         }
637         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
638                 glueBack->be_entry_open (glueBack, glueMode) != 0)
639                 return NOID;
640
641         return glueBack->be_entry_first (glueBack);
642 }
643
644 static ID
645 glue_tool_entry_next (
646         BackendDB *b0
647 )
648 {
649         slap_overinst   *on = glue_tool_inst( b0->bd_info );
650         glueinfo                *gi = on->on_bi.bi_private;
651         int i;
652         ID rc;
653
654         if (!glueBack || !glueBack->be_entry_next)
655                 return NOID;
656
657         rc = glueBack->be_entry_next (glueBack);
658
659         /* If we ran out of entries in one database, move on to the next */
660         while (rc == NOID) {
661                 if ( glueBack && glueBack->be_entry_close )
662                         glueBack->be_entry_close (glueBack);
663                 for (i=0; i<gi->gi_nodes; i++) {
664                         if (gi->gi_n[i].gn_be == glueBack)
665                                 break;
666                 }
667                 if (i == 0) {
668                         glueBack = NULL;
669                         break;
670                 } else {
671                         glueBack = gi->gi_n[i-1].gn_be;
672                         rc = glue_tool_entry_first (b0);
673                 }
674         }
675         return rc;
676 }
677
678 static Entry *
679 glue_tool_entry_get (
680         BackendDB *b0,
681         ID id
682 )
683 {
684         if (!glueBack || !glueBack->be_entry_get)
685                 return NULL;
686
687         return glueBack->be_entry_get (glueBack, id);
688 }
689
690 static ID
691 glue_tool_entry_put (
692         BackendDB *b0,
693         Entry *e,
694         struct berval *text
695 )
696 {
697         BackendDB *be, b2;
698         int rc = -1;
699
700         b2 = *b0;
701         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
702         be = glue_back_select (&b2, &e->e_nname);
703         if ( be == &b2 ) be = &toolDB;
704
705         if (!be->be_entry_put)
706                 return NOID;
707
708         if (!glueBack) {
709                 if ( be->be_entry_open ) {
710                         rc = be->be_entry_open (be, glueMode);
711                 }
712                 if (rc != 0) {
713                         return NOID;
714                 }
715         } else if (be != glueBack) {
716                 /* If this entry belongs in a different branch than the
717                  * previous one, close the current database and open the
718                  * new one.
719                  */
720                 if ( glueBack->be_entry_close ) {
721                         glueBack->be_entry_close (glueBack);
722                 }
723                 if ( be->be_entry_open ) {
724                         rc = be->be_entry_open (be, glueMode);
725                 }
726                 if (rc != 0) {
727                         return NOID;
728                 }
729         }
730         glueBack = be;
731         return be->be_entry_put (be, e, text);
732 }
733
734 static int
735 glue_tool_entry_reindex (
736         BackendDB *b0,
737         ID id
738 )
739 {
740         if (!glueBack || !glueBack->be_entry_reindex)
741                 return -1;
742
743         return glueBack->be_entry_reindex (glueBack, id);
744 }
745
746 static int
747 glue_tool_sync (
748         BackendDB *b0
749 )
750 {
751         slap_overinst   *on = glue_tool_inst( b0->bd_info );
752         glueinfo                *gi = on->on_bi.bi_private;
753         BackendInfo             *bi = b0->bd_info;
754         int i;
755
756         /* just sync everyone */
757         for (i = 0; i<gi->gi_nodes; i++)
758                 if (gi->gi_n[i].gn_be->be_sync)
759                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
760         b0->bd_info = on->on_info->oi_orig;
761         if ( b0->be_sync )
762                 b0->be_sync( b0 );
763         b0->bd_info = bi;
764         return 0;
765 }
766
767 static int
768 glue_db_init(
769         BackendDB *be
770 )
771 {
772         slap_overinst   *on = (slap_overinst *)be->bd_info;
773         slap_overinfo   *oi = on->on_info;
774         BackendInfo     *bi = oi->oi_orig;
775         glueinfo *gi;
776
777         gi = ch_calloc( 1, sizeof(glueinfo));
778         on->on_bi.bi_private = gi;
779         dnParent( be->be_nsuffix, &gi->gi_pdn );
780
781         /* Currently the overlay framework doesn't handle these entry points
782          * but we need them....
783          */
784         oi->oi_bi.bi_open = glue_open;
785         oi->oi_bi.bi_close = glue_close;
786
787         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
788
789         /* Only advertise these if the root DB supports them */
790         if ( bi->bi_tool_entry_open )
791                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
792         if ( bi->bi_tool_entry_close )
793                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
794         if ( bi->bi_tool_entry_first )
795                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
796         if ( bi->bi_tool_entry_next )
797                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
798         if ( bi->bi_tool_entry_get )
799                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
800         if ( bi->bi_tool_entry_put )
801                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
802         if ( bi->bi_tool_entry_reindex )
803                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
804         if ( bi->bi_tool_sync )
805                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
806
807         /*FIXME : need to add support */
808         oi->oi_bi.bi_tool_dn2id_get = 0;
809         oi->oi_bi.bi_tool_id2entry_get = 0;
810         oi->oi_bi.bi_tool_entry_modify = 0;
811
812         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
813
814         return 0;
815 }
816
817 static int
818 glue_db_destroy (
819         BackendDB *be
820 )
821 {
822         slap_overinst   *on = (slap_overinst *)be->bd_info;
823         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
824
825         free (gi);
826         return SLAP_CB_CONTINUE;
827 }
828
829 static int
830 glue_db_close( 
831         BackendDB *be
832 )
833 {
834         slap_overinst   *on = (slap_overinst *)be->bd_info;
835
836         on->on_info->oi_bi.bi_db_close = 0;
837         return 0;
838 }
839
840 int
841 glue_sub_del( BackendDB *b0 )
842 {
843         BackendDB *be;
844         int rc = 0;
845
846         /* Find the top backend for this subordinate */
847         be = b0;
848         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
849                 slap_overinfo *oi;
850                 slap_overinst *on;
851                 glueinfo *gi;
852                 int i;
853
854                 if ( SLAP_GLUE_SUBORDINATE( be ))
855                         continue;
856                 if ( !SLAP_GLUE_INSTANCE( be ))
857                         continue;
858                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
859                         continue;
860
861                 /* OK, got the right backend, find the overlay */
862                 oi = (slap_overinfo *)be->bd_info;
863                 for ( on=oi->oi_list; on; on=on->on_next ) {
864                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
865                                 break;
866                 }
867                 assert( on != NULL );
868                 gi = on->on_bi.bi_private;
869                 for ( i=0; i < gi->gi_nodes; i++ ) {
870                         if ( gi->gi_n[i].gn_be == b0 ) {
871                                 int j;
872
873                                 for (j=i+1; j < gi->gi_nodes; j++)
874                                         gi->gi_n[j-1] = gi->gi_n[j];
875
876                                 gi->gi_nodes--;
877                         }
878                 }
879         }
880         if ( be == NULL )
881                 rc = LDAP_NO_SUCH_OBJECT;
882
883         return rc;
884 }
885
886 typedef struct glue_Addrec {
887         struct glue_Addrec *ga_next;
888         BackendDB *ga_be;
889 } glue_Addrec;
890
891 /* List of added subordinates */
892 static glue_Addrec *ga_list;
893
894 /* Attach all the subordinate backends to their superior */
895 int
896 glue_sub_attach()
897 {
898         glue_Addrec *ga, *gnext = NULL;
899         int rc = 0;
900
901         /* For all the subordinate backends */
902         for ( ga=ga_list; ga != NULL; ga = gnext ) {
903                 BackendDB *be;
904
905                 gnext = ga->ga_next;
906
907                 /* Find the top backend for this subordinate */
908                 be = ga->ga_be;
909                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
910                         slap_overinfo *oi;
911                         slap_overinst *on;
912                         glueinfo *gi;
913
914                         if ( SLAP_GLUE_SUBORDINATE( be ))
915                                 continue;
916                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
917                                 continue;
918
919                         /* If it's not already configured, set up the overlay */
920                         if ( !SLAP_GLUE_INSTANCE( be )) {
921                                 rc = overlay_config( be, glue.on_bi.bi_type );
922                                 if ( rc )
923                                         break;
924                         }
925                         /* Find the overlay instance */
926                         oi = (slap_overinfo *)be->bd_info;
927                         for ( on=oi->oi_list; on; on=on->on_next ) {
928                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
929                                         break;
930                         }
931                         assert( on != NULL );
932                         gi = on->on_bi.bi_private;
933                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
934                                 gi->gi_nodes * sizeof(gluenode));
935                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
936                         dnParent( &ga->ga_be->be_nsuffix[0],
937                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
938                         gi->gi_nodes++;
939                         on->on_bi.bi_private = gi;
940                         break;
941                 }
942                 if ( !be ) {
943                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
944                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
945                         rc = LDAP_NO_SUCH_OBJECT;
946                 }
947                 ch_free( ga );
948                 if ( rc ) break;
949         }
950
951         ga_list = gnext;
952
953         return rc;
954 }
955
956 int
957 glue_sub_add( BackendDB *be, int advert, int online )
958 {
959         glue_Addrec *ga;
960         int rc = 0;
961
962         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
963         if ( advert )
964                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
965
966         ga = ch_malloc( sizeof( glue_Addrec ));
967         ga->ga_next = ga_list;
968         ga->ga_be = be;
969         ga_list = ga;
970
971         if ( online )
972                 rc = glue_sub_attach();
973
974         return rc;
975 }
976
977 int
978 glue_sub_init()
979 {
980         glue.on_bi.bi_type = "glue";
981
982         glue.on_bi.bi_db_init = glue_db_init;
983         glue.on_bi.bi_db_close = glue_db_close;
984         glue.on_bi.bi_db_destroy = glue_db_destroy;
985
986         glue.on_bi.bi_op_search = glue_op_search;
987         glue.on_bi.bi_op_modify = glue_op_func;
988         glue.on_bi.bi_op_modrdn = glue_op_func;
989         glue.on_bi.bi_op_add = glue_op_func;
990         glue.on_bi.bi_op_delete = glue_op_func;
991
992         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
993         glue.on_bi.bi_chk_controls = glue_chk_controls;
994
995         return overlay_register( &glue );
996 }