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