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