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