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