]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
8d719ab1e3f7d03705cc82a210300813d9be878b
[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 type as our main backend? */
481                         if ( gi->gi_n[i].gn_be->bd_info == on->on_info->oi_orig )
482                                 bsame = 1;
483
484                         /* Loop thru the bd_info's and make sure we only
485                          * invoke their bi_open functions once each.
486                          */
487                         for ( j = 0; j<i; j++ ) {
488                                 if ( gi->gi_n[i].gn_be->bd_info ==
489                                         gi->gi_n[j].gn_be->bd_info ) {
490                                         same = 1;
491                                         break;
492                                 }
493                         }
494                         /* OK, it's unique and non-NULL, call it. */
495                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
496                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
497                                         gi->gi_n[i].gn_be->bd_info );
498                         /* Let backend.c take care of the rest of startup */
499                         if ( !rc )
500                                 rc = backend_startup_one( gi->gi_n[i].gn_be );
501                         if ( rc ) break;
502                 }
503                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
504                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
505
506         } /* other case is impossible */
507         return rc;
508 }
509
510 /* This function will only be called in tool mode */
511 static int
512 glue_close (
513         BackendInfo *bi
514 )
515 {
516         static int glueClosed = 0;
517         int rc = 0;
518
519         if (glueClosed) return 0;
520
521         glueClosed = 1;
522
523         if (slapMode & SLAP_TOOL_MODE) {
524                 rc = backend_shutdown( NULL );
525         }
526         return rc;
527 }
528
529 static int
530 glue_entry_release_rw (
531         Operation *op,
532         Entry *e,
533         int rw
534 )
535 {
536         BackendDB *b0, b2;
537         int rc = -1;
538
539         b0 = op->o_bd;
540         b2 = *op->o_bd;
541         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
542         op->o_bd = glue_back_select (&b2, &e->e_nname);
543
544         if ( op->o_bd->be_release ) {
545                 rc = op->o_bd->be_release( op, e, rw );
546
547         } else {
548                 /* FIXME: mimic be_entry_release_rw
549                  * when no be_release() available */
550                 /* free entry */
551                 entry_free( e );
552                 rc = 0;
553         }
554         op->o_bd = b0;
555         return rc;
556 }
557
558 static ID
559 glue_tool_entry_first (
560         BackendDB *b0
561 )
562 {
563         slap_overinst   *on = glue_tool_inst( b0->bd_info );
564         glueinfo                *gi = on->on_bi.bi_private;
565         int i;
566
567         /* If we're starting from scratch, start at the most general */
568         if (!glueBack) {
569                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
570                         glueBack = &toolDB;
571                 } else {
572                         for (i = gi->gi_nodes-1; i >= 0; i--) {
573                                 if (gi->gi_n[i].gn_be->be_entry_open &&
574                                         gi->gi_n[i].gn_be->be_entry_first) {
575                                                 glueBack = gi->gi_n[i].gn_be;
576                                         break;
577                                 }
578                         }
579                 }
580         }
581         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
582                 glueBack->be_entry_open (glueBack, glueMode) != 0)
583                 return NOID;
584
585         return glueBack->be_entry_first (glueBack);
586 }
587
588 static ID
589 glue_tool_entry_next (
590         BackendDB *b0
591 )
592 {
593         slap_overinst   *on = glue_tool_inst( b0->bd_info );
594         glueinfo                *gi = on->on_bi.bi_private;
595         int i;
596         ID rc;
597
598         if (!glueBack || !glueBack->be_entry_next)
599                 return NOID;
600
601         rc = glueBack->be_entry_next (glueBack);
602
603         /* If we ran out of entries in one database, move on to the next */
604         while (rc == NOID) {
605                 if ( glueBack && glueBack->be_entry_close )
606                         glueBack->be_entry_close (glueBack);
607                 for (i=0; i<gi->gi_nodes; i++) {
608                         if (gi->gi_n[i].gn_be == glueBack)
609                                 break;
610                 }
611                 if (i == 0) {
612                         glueBack = NULL;
613                         break;
614                 } else {
615                         glueBack = gi->gi_n[i-1].gn_be;
616                         rc = glue_tool_entry_first (b0);
617                 }
618         }
619         return rc;
620 }
621
622 static Entry *
623 glue_tool_entry_get (
624         BackendDB *b0,
625         ID id
626 )
627 {
628         if (!glueBack || !glueBack->be_entry_get)
629                 return NULL;
630
631         return glueBack->be_entry_get (glueBack, id);
632 }
633
634 static ID
635 glue_tool_entry_put (
636         BackendDB *b0,
637         Entry *e,
638         struct berval *text
639 )
640 {
641         BackendDB *be, b2;
642         int rc = -1;
643
644         b2 = *b0;
645         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
646         be = glue_back_select (&b2, &e->e_nname);
647         if ( be == &b2 ) be = &toolDB;
648
649         if (!be->be_entry_put)
650                 return NOID;
651
652         if (!glueBack) {
653                 if ( be->be_entry_open ) {
654                         rc = be->be_entry_open (be, glueMode);
655                 }
656                 if (rc != 0) {
657                         return NOID;
658                 }
659         } else if (be != glueBack) {
660                 /* If this entry belongs in a different branch than the
661                  * previous one, close the current database and open the
662                  * new one.
663                  */
664                 if ( glueBack->be_entry_close ) {
665                         glueBack->be_entry_close (glueBack);
666                 }
667                 if ( be->be_entry_open ) {
668                         rc = be->be_entry_open (be, glueMode);
669                 }
670                 if (rc != 0) {
671                         return NOID;
672                 }
673         }
674         glueBack = be;
675         return be->be_entry_put (be, e, text);
676 }
677
678 static int
679 glue_tool_entry_reindex (
680         BackendDB *b0,
681         ID id
682 )
683 {
684         if (!glueBack || !glueBack->be_entry_reindex)
685                 return -1;
686
687         return glueBack->be_entry_reindex (glueBack, id);
688 }
689
690 static int
691 glue_tool_sync (
692         BackendDB *b0
693 )
694 {
695         slap_overinst   *on = glue_tool_inst( b0->bd_info );
696         glueinfo                *gi = on->on_bi.bi_private;
697         BackendInfo             *bi = b0->bd_info;
698         int i;
699
700         /* just sync everyone */
701         for (i = 0; i<gi->gi_nodes; i++)
702                 if (gi->gi_n[i].gn_be->be_sync)
703                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
704         b0->bd_info = on->on_info->oi_orig;
705         if ( b0->be_sync )
706                 b0->be_sync( b0 );
707         b0->bd_info = bi;
708         return 0;
709 }
710
711 static int
712 glue_db_init(
713         BackendDB *be
714 )
715 {
716         slap_overinst   *on = (slap_overinst *)be->bd_info;
717         slap_overinfo   *oi = on->on_info;
718         glueinfo *gi;
719
720         gi = ch_calloc( 1, sizeof(glueinfo));
721         on->on_bi.bi_private = gi;
722         dnParent( be->be_nsuffix, &gi->gi_pdn );
723
724         /* Currently the overlay framework doesn't handle these entry points
725          * but we need them....
726          */
727         oi->oi_bi.bi_open = glue_open;
728         oi->oi_bi.bi_close = glue_close;
729
730         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
731
732         oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
733         oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
734         oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
735         oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
736         oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
737         oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
738         oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
739         oi->oi_bi.bi_tool_sync = glue_tool_sync;
740
741         /*FIXME : need to add support */
742         oi->oi_bi.bi_tool_dn2id_get = 0;
743         oi->oi_bi.bi_tool_id2entry_get = 0;
744         oi->oi_bi.bi_tool_entry_modify = 0;
745
746         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
747
748         return 0;
749 }
750
751 static int
752 glue_db_destroy (
753         BackendDB *be
754 )
755 {
756         slap_overinst   *on = (slap_overinst *)be->bd_info;
757         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
758
759         free (gi);
760         return SLAP_CB_CONTINUE;
761 }
762
763 static int
764 glue_db_close( 
765         BackendDB *be
766 )
767 {
768         slap_overinst   *on = (slap_overinst *)be->bd_info;
769
770         on->on_info->oi_bi.bi_db_close = NULL;
771         return 0;
772 }
773
774 int
775 glue_sub_del( BackendDB *b0 )
776 {
777         BackendDB *be;
778         int rc = 0;
779
780         /* Find the top backend for this subordinate */
781         be = b0;
782         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
783                 slap_overinfo *oi;
784                 slap_overinst *on;
785                 glueinfo *gi;
786                 int i;
787
788                 if ( SLAP_GLUE_SUBORDINATE( be ))
789                         continue;
790                 if ( !SLAP_GLUE_INSTANCE( be ))
791                         continue;
792                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
793                         continue;
794
795                 /* OK, got the right backend, find the overlay */
796                 oi = (slap_overinfo *)be->bd_info;
797                 for ( on=oi->oi_list; on; on=on->on_next ) {
798                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
799                                 break;
800                 }
801                 assert( on != NULL );
802                 gi = on->on_bi.bi_private;
803                 for ( i=0; i < gi->gi_nodes; i++ ) {
804                         if ( gi->gi_n[i].gn_be == b0 ) {
805                                 int j;
806
807                                 for (j=i+1; j < gi->gi_nodes; j++)
808                                         gi->gi_n[j-1] = gi->gi_n[j];
809
810                                 gi->gi_nodes--;
811                         }
812                 }
813         }
814         if ( be == NULL )
815                 rc = LDAP_NO_SUCH_OBJECT;
816
817         return rc;
818 }
819
820 typedef struct glue_Addrec {
821         struct glue_Addrec *ga_next;
822         BackendDB *ga_be;
823 } glue_Addrec;
824
825 /* List of added subordinates */
826 static glue_Addrec *ga_list;
827
828 /* Attach all the subordinate backends to their superior */
829 static int
830 glue_sub_attach()
831 {
832         glue_Addrec *ga, *gnext = NULL;
833         int rc = 0;
834
835         /* For all the subordinate backends */
836         for ( ga=ga_list; ga != NULL; ga = gnext ) {
837                 BackendDB *be;
838
839                 gnext = ga->ga_next;
840
841                 /* Find the top backend for this subordinate */
842                 be = ga->ga_be;
843                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
844                         slap_overinfo *oi;
845                         slap_overinst *on;
846                         glueinfo *gi;
847
848                         if ( SLAP_GLUE_SUBORDINATE( be ))
849                                 continue;
850                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
851                                 continue;
852
853                         /* If it's not already configured, set up the overlay */
854                         if ( !SLAP_GLUE_INSTANCE( be )) {
855                                 rc = overlay_config( be, glue.on_bi.bi_type );
856                                 if ( rc )
857                                         break;
858                         }
859                         /* Find the overlay instance */
860                         oi = (slap_overinfo *)be->bd_info;
861                         for ( on=oi->oi_list; on; on=on->on_next ) {
862                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
863                                         break;
864                         }
865                         assert( on != NULL );
866                         gi = on->on_bi.bi_private;
867                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
868                                 gi->gi_nodes * sizeof(gluenode));
869                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
870                         dnParent( &ga->ga_be->be_nsuffix[0],
871                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
872                         gi->gi_nodes++;
873                         on->on_bi.bi_private = gi;
874                         break;
875                 }
876                 if ( !be ) {
877                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
878                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
879                         rc = LDAP_NO_SUCH_OBJECT;
880                 }
881                 ch_free( ga );
882                 if ( rc ) break;
883         }
884
885         ga_list = gnext;
886
887         return rc;
888 }
889
890 int
891 glue_sub_add( BackendDB *be, int advert, int online )
892 {
893         glue_Addrec *ga;
894         int rc = 0;
895
896         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
897         if ( advert )
898                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
899
900         ga = ch_malloc( sizeof( glue_Addrec ));
901         ga->ga_next = NULL;
902         ga->ga_be = be;
903         if ( ga_list ) {
904                 glue_Addrec *g2 = ga_list;
905
906                 for ( ; g2 && g2->ga_next; g2=g2->ga_next );
907                 g2->ga_next = ga;
908         } else {
909                 ga_list = ga;
910         }
911
912         if ( online )
913                 rc = glue_sub_attach();
914
915         return rc;
916 }
917
918 int
919 glue_sub_init()
920 {
921         int rc;
922
923         glue.on_bi.bi_type = "glue";
924
925         glue.on_bi.bi_db_init = glue_db_init;
926         glue.on_bi.bi_db_close = glue_db_close;
927         glue.on_bi.bi_db_destroy = glue_db_destroy;
928
929         glue.on_bi.bi_op_search = glue_op_search;
930         glue.on_bi.bi_op_modify = glue_op_func;
931         glue.on_bi.bi_op_modrdn = glue_op_func;
932         glue.on_bi.bi_op_add = glue_op_func;
933         glue.on_bi.bi_op_delete = glue_op_func;
934
935         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
936         glue.on_bi.bi_chk_controls = glue_chk_controls;
937
938         rc = overlay_register( &glue );
939         if ( rc ) return rc;
940
941         return glue_sub_attach();
942 }