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