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