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