]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
434d29e643c3e32009920732a77890a6922026c3
[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
384                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
385                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
386                         {
387                                 op->ors_scope = LDAP_SCOPE_SUBTREE;
388                                 op->o_req_dn = dn;
389                                 op->o_req_ndn = ndn;
390                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
391
392                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
393                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
394                         {
395                                 op->o_req_dn = op->o_bd->be_suffix[0];
396                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
397                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
398                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
399                                         gs.err = LDAP_SUCCESS;
400                                 }
401                                 op->o_req_dn = dn;
402                                 op->o_req_ndn = ndn;
403
404                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
405                                 op->ors_scope = scope0;
406                                 op->o_req_dn = dn;
407                                 op->o_req_ndn = ndn;
408                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
409                         }
410
411                         switch ( gs.err ) {
412
413                         /*
414                          * Add errors that should result in dropping
415                          * the search
416                          */
417                         case LDAP_SIZELIMIT_EXCEEDED:
418                         case LDAP_TIMELIMIT_EXCEEDED:
419                         case LDAP_ADMINLIMIT_EXCEEDED:
420                         case LDAP_NO_SUCH_OBJECT:
421 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
422                         case LDAP_X_CANNOT_CHAIN:
423 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
424                                 goto end_of_loop;
425                         
426                         default:
427                                 break;
428                         }
429                 }
430 end_of_loop:;
431                 op->ors_scope = scope0;
432                 op->ors_tlimit = tlimit0;
433                 op->o_time = starttime;
434                 op->o_req_dn = dn;
435                 op->o_req_ndn = ndn;
436
437                 break;
438         }
439         if ( op->o_abandon ) {
440                 rs->sr_err = SLAPD_ABANDON;
441         } else {
442                 op->o_callback = cb.sc_next;
443                 rs->sr_err = gs.err;
444                 rs->sr_matched = gs.matched;
445                 rs->sr_ref = gs.refs;
446                 rs->sr_ctrls = gs.ctrls;
447
448                 send_ldap_result( op, rs );
449         }
450
451         op->o_bd = b0;
452         op->o_bd->bd_info = bi0;
453         if (gs.matched)
454                 free (gs.matched);
455         if (gs.refs)
456                 ber_bvarray_free(gs.refs);
457         if (gs.ctrls) {
458                 for (i = gs.nctrls; --i >= 0; ) {
459                         if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
460                                 free(gs.ctrls[i]->ldctl_value.bv_val);
461                         free(gs.ctrls[i]);
462                 }
463                 free(gs.ctrls);
464         }
465         return rs->sr_err;
466 }
467
468 static BackendDB toolDB;
469
470 static int
471 glue_tool_entry_open (
472         BackendDB *b0,
473         int mode
474 )
475 {
476         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
477
478         /* We don't know which backend to talk to yet, so just
479          * remember the mode and move on...
480          */
481
482         glueMode = mode;
483         glueBack = NULL;
484         toolDB = *b0;
485         toolDB.bd_info = oi->oi_orig;
486
487         return 0;
488 }
489
490 static int
491 glue_tool_entry_close (
492         BackendDB *b0
493 )
494 {
495         int rc = 0;
496
497         if (glueBack) {
498                 if (!glueBack->be_entry_close)
499                         return 0;
500                 rc = glueBack->be_entry_close (glueBack);
501         }
502         return rc;
503 }
504
505 static slap_overinst *
506 glue_tool_inst(
507         BackendInfo *bi
508 )
509 {
510         slap_overinfo   *oi = (slap_overinfo *)bi;
511         slap_overinst   *on;
512
513         for ( on = oi->oi_list; on; on=on->on_next ) {
514                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
515                         return on;
516         }
517         return NULL;
518 }
519
520 /* This function will only be called in tool mode */
521 static int
522 glue_open (
523         BackendInfo *bi
524 )
525 {
526         slap_overinst *on = glue_tool_inst( bi );
527         glueinfo                *gi = on->on_bi.bi_private;
528         static int glueOpened = 0;
529         int i, j, same, bsame = 0, rc = 0;
530
531         if (glueOpened) return 0;
532
533         glueOpened = 1;
534
535         /* If we were invoked in tool mode, open all the underlying backends */
536         if (slapMode & SLAP_TOOL_MODE) {
537                 for (i = 0; i<gi->gi_nodes; i++) {
538                         same = 0;
539                         /* Same bi_open as our main backend? */
540                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
541                                 on->on_info->oi_orig->bi_open )
542                                 bsame = 1;
543
544                         /* Loop thru the bd_info's and make sure we only
545                          * invoke their bi_open functions once each.
546                          */
547                         for ( j = 0; j<i; j++ ) {
548                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
549                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
550                                         same = 1;
551                                         break;
552                                 }
553                         }
554                         /* OK, it's unique and non-NULL, call it. */
555                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
556                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
557                                         gi->gi_n[i].gn_be->bd_info );
558                         /* Let backend.c take care of the rest of startup */
559                         if ( !rc )
560                                 rc = backend_startup_one( gi->gi_n[i].gn_be );
561                         if ( rc ) break;
562                 }
563                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
564                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
565
566         } /* other case is impossible */
567         return rc;
568 }
569
570 /* This function will only be called in tool mode */
571 static int
572 glue_close (
573         BackendInfo *bi
574 )
575 {
576         static int glueClosed = 0;
577         int rc = 0;
578
579         if (glueClosed) return 0;
580
581         glueClosed = 1;
582
583         if (slapMode & SLAP_TOOL_MODE) {
584                 rc = backend_shutdown( NULL );
585         }
586         return rc;
587 }
588
589 static int
590 glue_entry_release_rw (
591         Operation *op,
592         Entry *e,
593         int rw
594 )
595 {
596         BackendDB *b0, b2;
597         int rc = -1;
598
599         b0 = op->o_bd;
600         b2 = *op->o_bd;
601         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
602         op->o_bd = glue_back_select (&b2, &e->e_nname);
603
604         if ( op->o_bd->be_release ) {
605                 rc = op->o_bd->be_release( op, e, rw );
606
607         } else {
608                 /* FIXME: mimic be_entry_release_rw
609                  * when no be_release() available */
610                 /* free entry */
611                 entry_free( e );
612                 rc = 0;
613         }
614         op->o_bd = b0;
615         return rc;
616 }
617
618 static ID
619 glue_tool_entry_first (
620         BackendDB *b0
621 )
622 {
623         slap_overinst   *on = glue_tool_inst( b0->bd_info );
624         glueinfo                *gi = on->on_bi.bi_private;
625         int i;
626
627         /* If we're starting from scratch, start at the most general */
628         if (!glueBack) {
629                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
630                         glueBack = &toolDB;
631                 } else {
632                         for (i = gi->gi_nodes-1; i >= 0; i--) {
633                                 if (gi->gi_n[i].gn_be->be_entry_open &&
634                                         gi->gi_n[i].gn_be->be_entry_first) {
635                                                 glueBack = gi->gi_n[i].gn_be;
636                                         break;
637                                 }
638                         }
639                 }
640         }
641         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
642                 glueBack->be_entry_open (glueBack, glueMode) != 0)
643                 return NOID;
644
645         return glueBack->be_entry_first (glueBack);
646 }
647
648 static ID
649 glue_tool_entry_next (
650         BackendDB *b0
651 )
652 {
653         slap_overinst   *on = glue_tool_inst( b0->bd_info );
654         glueinfo                *gi = on->on_bi.bi_private;
655         int i;
656         ID rc;
657
658         if (!glueBack || !glueBack->be_entry_next)
659                 return NOID;
660
661         rc = glueBack->be_entry_next (glueBack);
662
663         /* If we ran out of entries in one database, move on to the next */
664         while (rc == NOID) {
665                 if ( glueBack && glueBack->be_entry_close )
666                         glueBack->be_entry_close (glueBack);
667                 for (i=0; i<gi->gi_nodes; i++) {
668                         if (gi->gi_n[i].gn_be == glueBack)
669                                 break;
670                 }
671                 if (i == 0) {
672                         glueBack = NULL;
673                         break;
674                 } else {
675                         glueBack = gi->gi_n[i-1].gn_be;
676                         rc = glue_tool_entry_first (b0);
677                 }
678         }
679         return rc;
680 }
681
682 static Entry *
683 glue_tool_entry_get (
684         BackendDB *b0,
685         ID id
686 )
687 {
688         if (!glueBack || !glueBack->be_entry_get)
689                 return NULL;
690
691         return glueBack->be_entry_get (glueBack, id);
692 }
693
694 static ID
695 glue_tool_entry_put (
696         BackendDB *b0,
697         Entry *e,
698         struct berval *text
699 )
700 {
701         BackendDB *be, b2;
702         int rc = -1;
703
704         b2 = *b0;
705         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
706         be = glue_back_select (&b2, &e->e_nname);
707         if ( be == &b2 ) be = &toolDB;
708
709         if (!be->be_entry_put)
710                 return NOID;
711
712         if (!glueBack) {
713                 if ( be->be_entry_open ) {
714                         rc = be->be_entry_open (be, glueMode);
715                 }
716                 if (rc != 0) {
717                         return NOID;
718                 }
719         } else if (be != glueBack) {
720                 /* If this entry belongs in a different branch than the
721                  * previous one, close the current database and open the
722                  * new one.
723                  */
724                 if ( glueBack->be_entry_close ) {
725                         glueBack->be_entry_close (glueBack);
726                 }
727                 if ( be->be_entry_open ) {
728                         rc = be->be_entry_open (be, glueMode);
729                 }
730                 if (rc != 0) {
731                         return NOID;
732                 }
733         }
734         glueBack = be;
735         return be->be_entry_put (be, e, text);
736 }
737
738 static int
739 glue_tool_entry_reindex (
740         BackendDB *b0,
741         ID id
742 )
743 {
744         if (!glueBack || !glueBack->be_entry_reindex)
745                 return -1;
746
747         return glueBack->be_entry_reindex (glueBack, id);
748 }
749
750 static int
751 glue_tool_sync (
752         BackendDB *b0
753 )
754 {
755         slap_overinst   *on = glue_tool_inst( b0->bd_info );
756         glueinfo                *gi = on->on_bi.bi_private;
757         BackendInfo             *bi = b0->bd_info;
758         int i;
759
760         /* just sync everyone */
761         for (i = 0; i<gi->gi_nodes; i++)
762                 if (gi->gi_n[i].gn_be->be_sync)
763                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
764         b0->bd_info = on->on_info->oi_orig;
765         if ( b0->be_sync )
766                 b0->be_sync( b0 );
767         b0->bd_info = bi;
768         return 0;
769 }
770
771 static int
772 glue_db_init(
773         BackendDB *be
774 )
775 {
776         slap_overinst   *on = (slap_overinst *)be->bd_info;
777         slap_overinfo   *oi = on->on_info;
778         BackendInfo     *bi = oi->oi_orig;
779         glueinfo *gi;
780
781         gi = ch_calloc( 1, sizeof(glueinfo));
782         on->on_bi.bi_private = gi;
783         dnParent( be->be_nsuffix, &gi->gi_pdn );
784
785         /* Currently the overlay framework doesn't handle these entry points
786          * but we need them....
787          */
788         oi->oi_bi.bi_open = glue_open;
789         oi->oi_bi.bi_close = glue_close;
790
791         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
792
793         /* Only advertise these if the root DB supports them */
794         if ( bi->bi_tool_entry_open )
795                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
796         if ( bi->bi_tool_entry_close )
797                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
798         if ( bi->bi_tool_entry_first )
799                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
800         if ( bi->bi_tool_entry_next )
801                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
802         if ( bi->bi_tool_entry_get )
803                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
804         if ( bi->bi_tool_entry_put )
805                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
806         if ( bi->bi_tool_entry_reindex )
807                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
808         if ( bi->bi_tool_sync )
809                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
810
811         /*FIXME : need to add support */
812         oi->oi_bi.bi_tool_dn2id_get = 0;
813         oi->oi_bi.bi_tool_id2entry_get = 0;
814         oi->oi_bi.bi_tool_entry_modify = 0;
815
816         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
817
818         return 0;
819 }
820
821 static int
822 glue_db_destroy (
823         BackendDB *be
824 )
825 {
826         slap_overinst   *on = (slap_overinst *)be->bd_info;
827         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
828
829         free (gi);
830         return SLAP_CB_CONTINUE;
831 }
832
833 static int
834 glue_db_close( 
835         BackendDB *be
836 )
837 {
838         slap_overinst   *on = (slap_overinst *)be->bd_info;
839
840         on->on_info->oi_bi.bi_db_close = 0;
841         return 0;
842 }
843
844 int
845 glue_sub_del( BackendDB *b0 )
846 {
847         BackendDB *be;
848         int rc = 0;
849
850         /* Find the top backend for this subordinate */
851         be = b0;
852         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
853                 slap_overinfo *oi;
854                 slap_overinst *on;
855                 glueinfo *gi;
856                 int i;
857
858                 if ( SLAP_GLUE_SUBORDINATE( be ))
859                         continue;
860                 if ( !SLAP_GLUE_INSTANCE( be ))
861                         continue;
862                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
863                         continue;
864
865                 /* OK, got the right backend, find the overlay */
866                 oi = (slap_overinfo *)be->bd_info;
867                 for ( on=oi->oi_list; on; on=on->on_next ) {
868                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
869                                 break;
870                 }
871                 assert( on != NULL );
872                 gi = on->on_bi.bi_private;
873                 for ( i=0; i < gi->gi_nodes; i++ ) {
874                         if ( gi->gi_n[i].gn_be == b0 ) {
875                                 int j;
876
877                                 for (j=i+1; j < gi->gi_nodes; j++)
878                                         gi->gi_n[j-1] = gi->gi_n[j];
879
880                                 gi->gi_nodes--;
881                         }
882                 }
883         }
884         if ( be == NULL )
885                 rc = LDAP_NO_SUCH_OBJECT;
886
887         return rc;
888 }
889
890 typedef struct glue_Addrec {
891         struct glue_Addrec *ga_next;
892         BackendDB *ga_be;
893 } glue_Addrec;
894
895 /* List of added subordinates */
896 static glue_Addrec *ga_list;
897
898 /* Attach all the subordinate backends to their superior */
899 int
900 glue_sub_attach()
901 {
902         glue_Addrec *ga, *gnext = NULL;
903         int rc = 0;
904
905         /* For all the subordinate backends */
906         for ( ga=ga_list; ga != NULL; ga = gnext ) {
907                 BackendDB *be;
908
909                 gnext = ga->ga_next;
910
911                 /* Find the top backend for this subordinate */
912                 be = ga->ga_be;
913                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
914                         slap_overinfo *oi;
915                         slap_overinst *on;
916                         glueinfo *gi;
917
918                         if ( SLAP_GLUE_SUBORDINATE( be ))
919                                 continue;
920                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
921                                 continue;
922
923                         /* If it's not already configured, set up the overlay */
924                         if ( !SLAP_GLUE_INSTANCE( be )) {
925                                 rc = overlay_config( be, glue.on_bi.bi_type );
926                                 if ( rc )
927                                         break;
928                         }
929                         /* Find the overlay instance */
930                         oi = (slap_overinfo *)be->bd_info;
931                         for ( on=oi->oi_list; on; on=on->on_next ) {
932                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
933                                         break;
934                         }
935                         assert( on != NULL );
936                         gi = on->on_bi.bi_private;
937                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
938                                 gi->gi_nodes * sizeof(gluenode));
939                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
940                         dnParent( &ga->ga_be->be_nsuffix[0],
941                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
942                         gi->gi_nodes++;
943                         on->on_bi.bi_private = gi;
944                         break;
945                 }
946                 if ( !be ) {
947                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
948                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
949                         rc = LDAP_NO_SUCH_OBJECT;
950                 }
951                 ch_free( ga );
952                 if ( rc ) break;
953         }
954
955         ga_list = gnext;
956
957         return rc;
958 }
959
960 int
961 glue_sub_add( BackendDB *be, int advert, int online )
962 {
963         glue_Addrec *ga;
964         int rc = 0;
965
966         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
967         if ( advert )
968                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
969
970         ga = ch_malloc( sizeof( glue_Addrec ));
971         ga->ga_next = ga_list;
972         ga->ga_be = be;
973         ga_list = ga;
974
975         if ( online )
976                 rc = glue_sub_attach();
977
978         return rc;
979 }
980
981 int
982 glue_sub_init()
983 {
984         glue.on_bi.bi_type = "glue";
985
986         glue.on_bi.bi_db_init = glue_db_init;
987         glue.on_bi.bi_db_close = glue_db_close;
988         glue.on_bi.bi_db_destroy = glue_db_destroy;
989
990         glue.on_bi.bi_op_search = glue_op_search;
991         glue.on_bi.bi_op_modify = glue_op_func;
992         glue.on_bi.bi_op_modrdn = glue_op_func;
993         glue.on_bi.bi_op_add = glue_op_func;
994         glue.on_bi.bi_op_delete = glue_op_func;
995
996         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
997         glue.on_bi.bi_chk_controls = glue_chk_controls;
998
999         return overlay_register( &glue );
1000 }