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