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