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