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