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