]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
ITS#4623 fix from HEAD
[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 (tlimit0 != SLAP_NO_LIMIT) {
381                                 op->o_time = slap_get_time();
382                                 op->ors_tlimit = stoptime - op->o_time;
383                                 if (op->ors_tlimit <= 0) {
384                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
385                                         break;
386                                 }
387                         }
388                         rs->sr_err = 0;
389                         /*
390                          * check for abandon 
391                          */
392                         if (op->o_abandon) {
393                                 goto end_of_loop;
394                         }
395                         op->o_bd = btmp;
396
397                         assert( op->o_bd->be_suffix != NULL );
398                         assert( op->o_bd->be_nsuffix != NULL );
399                         
400                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
401                                 dn_match(pdn, &ndn))
402                         {
403                                 op->ors_scope = LDAP_SCOPE_BASE;
404                                 op->o_req_dn = op->o_bd->be_suffix[0];
405                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
406                                 rs->sr_err = op->o_bd->be_search(op, rs);
407                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
408                                         gs.err = LDAP_SUCCESS;
409                                 }
410                                 op->ors_scope = LDAP_SCOPE_ONELEVEL;
411                                 op->o_req_dn = dn;
412                                 op->o_req_ndn = ndn;
413
414                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
415                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
416                         {
417                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
418
419                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
420                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
421                         {
422                                 op->o_req_dn = op->o_bd->be_suffix[0];
423                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
424                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
425                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
426                                         gs.err = LDAP_SUCCESS;
427                                 }
428                                 op->o_req_dn = dn;
429                                 op->o_req_ndn = ndn;
430
431                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
432                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
433                         }
434
435                         switch ( gs.err ) {
436
437                         /*
438                          * Add errors that should result in dropping
439                          * the search
440                          */
441                         case LDAP_SIZELIMIT_EXCEEDED:
442                         case LDAP_TIMELIMIT_EXCEEDED:
443                         case LDAP_ADMINLIMIT_EXCEEDED:
444                         case LDAP_NO_SUCH_OBJECT:
445 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
446                         case LDAP_X_CANNOT_CHAIN:
447 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
448                                 goto end_of_loop;
449                         
450                         default:
451                                 break;
452                         }
453                 }
454 end_of_loop:;
455                 op->ors_scope = scope0;
456                 op->ors_tlimit = tlimit0;
457                 op->o_time = starttime;
458                 op->o_req_dn = dn;
459                 op->o_req_ndn = ndn;
460
461                 break;
462         }
463         if ( op->o_abandon ) {
464                 rs->sr_err = SLAPD_ABANDON;
465         } else {
466                 op->o_callback = cb.sc_next;
467                 rs->sr_err = gs.err;
468                 rs->sr_matched = gs.matched;
469                 rs->sr_ref = gs.refs;
470                 rs->sr_ctrls = gs.ctrls;
471
472                 send_ldap_result( op, rs );
473         }
474
475         op->o_bd = b0;
476         op->o_bd->bd_info = bi0;
477         if (gs.matched)
478                 free (gs.matched);
479         if (gs.refs)
480                 ber_bvarray_free(gs.refs);
481         if (gs.ctrls) {
482                 for (i = gs.nctrls; --i >= 0; ) {
483                         if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
484                                 free(gs.ctrls[i]->ldctl_value.bv_val);
485                         free(gs.ctrls[i]);
486                 }
487                 free(gs.ctrls);
488         }
489         return rs->sr_err;
490 }
491
492 static BackendDB toolDB;
493
494 static int
495 glue_tool_entry_open (
496         BackendDB *b0,
497         int mode
498 )
499 {
500         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
501
502         /* We don't know which backend to talk to yet, so just
503          * remember the mode and move on...
504          */
505
506         glueMode = mode;
507         glueBack = NULL;
508         toolDB = *b0;
509         toolDB.bd_info = oi->oi_orig;
510
511         return 0;
512 }
513
514 static int
515 glue_tool_entry_close (
516         BackendDB *b0
517 )
518 {
519         int rc = 0;
520
521         if (glueBack) {
522                 if (!glueBack->be_entry_close)
523                         return 0;
524                 rc = glueBack->be_entry_close (glueBack);
525         }
526         return rc;
527 }
528
529 static slap_overinst *
530 glue_tool_inst(
531         BackendInfo *bi
532 )
533 {
534         slap_overinfo   *oi = (slap_overinfo *)bi;
535         slap_overinst   *on;
536
537         for ( on = oi->oi_list; on; on=on->on_next ) {
538                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
539                         return on;
540         }
541         return NULL;
542 }
543
544 /* This function will only be called in tool mode */
545 static int
546 glue_open (
547         BackendInfo *bi
548 )
549 {
550         slap_overinst *on = glue_tool_inst( bi );
551         glueinfo                *gi = on->on_bi.bi_private;
552         static int glueOpened = 0;
553         int i, j, same, bsame = 0, rc = 0;
554
555         if (glueOpened) return 0;
556
557         glueOpened = 1;
558
559         /* If we were invoked in tool mode, open all the underlying backends */
560         if (slapMode & SLAP_TOOL_MODE) {
561                 for (i = 0; i<gi->gi_nodes; i++) {
562                         same = 0;
563                         /* Same bi_open as our main backend? */
564                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
565                                 on->on_info->oi_orig->bi_open )
566                                 bsame = 1;
567
568                         /* Loop thru the bd_info's and make sure we only
569                          * invoke their bi_open functions once each.
570                          */
571                         for ( j = 0; j<i; j++ ) {
572                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
573                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
574                                         same = 1;
575                                         break;
576                                 }
577                         }
578                         /* OK, it's unique and non-NULL, call it. */
579                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
580                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
581                                         gi->gi_n[i].gn_be->bd_info );
582                         /* Let backend.c take care of the rest of startup */
583                         if ( !rc )
584                                 rc = backend_startup_one( gi->gi_n[i].gn_be );
585                         if ( rc ) break;
586                 }
587                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
588                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
589
590         } /* other case is impossible */
591         return rc;
592 }
593
594 /* This function will only be called in tool mode */
595 static int
596 glue_close (
597         BackendInfo *bi
598 )
599 {
600         static int glueClosed = 0;
601         int rc = 0;
602
603         if (glueClosed) return 0;
604
605         glueClosed = 1;
606
607         if (slapMode & SLAP_TOOL_MODE) {
608                 rc = backend_shutdown( NULL );
609         }
610         return rc;
611 }
612
613 static int
614 glue_entry_get_rw (
615         Operation               *op,
616         struct berval   *dn,
617         ObjectClass             *oc,
618         AttributeDescription    *ad,
619         int     rw,
620         Entry   **e )
621 {
622         BackendDB *b0 = op->o_bd;
623         op->o_bd = glue_back_select( b0, dn );
624         int rc;
625
626         if ( op->o_bd->be_fetch ) {
627                 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
628         } else {
629                 rc = LDAP_UNWILLING_TO_PERFORM;
630         }
631         op->o_bd =b0;
632         return rc;
633 }
634
635 static int
636 glue_entry_release_rw (
637         Operation *op,
638         Entry *e,
639         int rw
640 )
641 {
642         BackendDB *b0 = op->o_bd;
643         int rc = -1;
644
645         op->o_bd = glue_back_select (b0, &e->e_nname);
646
647         if ( op->o_bd->be_release ) {
648                 rc = op->o_bd->be_release( op, e, rw );
649
650         } else {
651                 /* FIXME: mimic be_entry_release_rw
652                  * when no be_release() available */
653                 /* free entry */
654                 entry_free( e );
655                 rc = 0;
656         }
657         op->o_bd = b0;
658         return rc;
659 }
660
661 static ID
662 glue_tool_entry_first (
663         BackendDB *b0
664 )
665 {
666         slap_overinst   *on = glue_tool_inst( b0->bd_info );
667         glueinfo                *gi = on->on_bi.bi_private;
668         int i;
669
670         /* If we're starting from scratch, start at the most general */
671         if (!glueBack) {
672                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
673                         glueBack = &toolDB;
674                 } else {
675                         for (i = gi->gi_nodes-1; i >= 0; i--) {
676                                 if (gi->gi_n[i].gn_be->be_entry_open &&
677                                         gi->gi_n[i].gn_be->be_entry_first) {
678                                                 glueBack = gi->gi_n[i].gn_be;
679                                         break;
680                                 }
681                         }
682                 }
683         }
684         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
685                 glueBack->be_entry_open (glueBack, glueMode) != 0)
686                 return NOID;
687
688         return glueBack->be_entry_first (glueBack);
689 }
690
691 static ID
692 glue_tool_entry_next (
693         BackendDB *b0
694 )
695 {
696         slap_overinst   *on = glue_tool_inst( b0->bd_info );
697         glueinfo                *gi = on->on_bi.bi_private;
698         int i;
699         ID rc;
700
701         if (!glueBack || !glueBack->be_entry_next)
702                 return NOID;
703
704         rc = glueBack->be_entry_next (glueBack);
705
706         /* If we ran out of entries in one database, move on to the next */
707         while (rc == NOID) {
708                 if ( glueBack && glueBack->be_entry_close )
709                         glueBack->be_entry_close (glueBack);
710                 for (i=0; i<gi->gi_nodes; i++) {
711                         if (gi->gi_n[i].gn_be == glueBack)
712                                 break;
713                 }
714                 if (i == 0) {
715                         glueBack = NULL;
716                         break;
717                 } else {
718                         glueBack = gi->gi_n[i-1].gn_be;
719                         rc = glue_tool_entry_first (b0);
720                 }
721         }
722         return rc;
723 }
724
725 static Entry *
726 glue_tool_entry_get (
727         BackendDB *b0,
728         ID id
729 )
730 {
731         if (!glueBack || !glueBack->be_entry_get)
732                 return NULL;
733
734         return glueBack->be_entry_get (glueBack, id);
735 }
736
737 static ID
738 glue_tool_entry_put (
739         BackendDB *b0,
740         Entry *e,
741         struct berval *text
742 )
743 {
744         BackendDB *be, b2;
745         int rc = -1;
746
747         b2 = *b0;
748         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
749         be = glue_back_select (&b2, &e->e_nname);
750         if ( be == &b2 ) be = &toolDB;
751
752         if (!be->be_entry_put)
753                 return NOID;
754
755         if (!glueBack) {
756                 if ( be->be_entry_open ) {
757                         rc = be->be_entry_open (be, glueMode);
758                 }
759                 if (rc != 0) {
760                         return NOID;
761                 }
762         } else if (be != glueBack) {
763                 /* If this entry belongs in a different branch than the
764                  * previous one, close the current database and open the
765                  * new one.
766                  */
767                 if ( glueBack->be_entry_close ) {
768                         glueBack->be_entry_close (glueBack);
769                 }
770                 if ( be->be_entry_open ) {
771                         rc = be->be_entry_open (be, glueMode);
772                 }
773                 if (rc != 0) {
774                         return NOID;
775                 }
776         }
777         glueBack = be;
778         return be->be_entry_put (be, e, text);
779 }
780
781 static int
782 glue_tool_entry_reindex (
783         BackendDB *b0,
784         ID id
785 )
786 {
787         if (!glueBack || !glueBack->be_entry_reindex)
788                 return -1;
789
790         return glueBack->be_entry_reindex (glueBack, id);
791 }
792
793 static int
794 glue_tool_sync (
795         BackendDB *b0
796 )
797 {
798         slap_overinst   *on = glue_tool_inst( b0->bd_info );
799         glueinfo                *gi = on->on_bi.bi_private;
800         BackendInfo             *bi = b0->bd_info;
801         int i;
802
803         /* just sync everyone */
804         for (i = 0; i<gi->gi_nodes; i++)
805                 if (gi->gi_n[i].gn_be->be_sync)
806                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
807         b0->bd_info = on->on_info->oi_orig;
808         if ( b0->be_sync )
809                 b0->be_sync( b0 );
810         b0->bd_info = bi;
811         return 0;
812 }
813
814 static int
815 glue_db_init(
816         BackendDB *be
817 )
818 {
819         slap_overinst   *on = (slap_overinst *)be->bd_info;
820         slap_overinfo   *oi = on->on_info;
821         BackendInfo     *bi = oi->oi_orig;
822         glueinfo *gi;
823
824         if ( SLAP_GLUE_SUBORDINATE( be )) {
825                 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
826                         "cannot have glue overlay!\n",
827                         be->be_suffix[0].bv_val, 0, 0 );
828                 return LDAP_OTHER;
829         }
830
831         gi = ch_calloc( 1, sizeof(glueinfo));
832         on->on_bi.bi_private = gi;
833         dnParent( be->be_nsuffix, &gi->gi_pdn );
834
835         /* Currently the overlay framework doesn't handle these entry points
836          * but we need them....
837          */
838         oi->oi_bi.bi_open = glue_open;
839         oi->oi_bi.bi_close = glue_close;
840
841         /* Only advertise these if the root DB supports them */
842         if ( bi->bi_tool_entry_open )
843                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
844         if ( bi->bi_tool_entry_close )
845                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
846         if ( bi->bi_tool_entry_first )
847                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
848         if ( bi->bi_tool_entry_next )
849                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
850         if ( bi->bi_tool_entry_get )
851                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
852         if ( bi->bi_tool_entry_put )
853                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
854         if ( bi->bi_tool_entry_reindex )
855                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
856         if ( bi->bi_tool_sync )
857                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
858
859         /*FIXME : need to add support */
860         oi->oi_bi.bi_tool_dn2id_get = 0;
861         oi->oi_bi.bi_tool_id2entry_get = 0;
862         oi->oi_bi.bi_tool_entry_modify = 0;
863
864         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
865
866         return 0;
867 }
868
869 static int
870 glue_db_destroy (
871         BackendDB *be
872 )
873 {
874         slap_overinst   *on = (slap_overinst *)be->bd_info;
875         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
876
877         free (gi);
878         return SLAP_CB_CONTINUE;
879 }
880
881 static int
882 glue_db_close( 
883         BackendDB *be
884 )
885 {
886         slap_overinst   *on = (slap_overinst *)be->bd_info;
887
888         on->on_info->oi_bi.bi_db_close = 0;
889         return 0;
890 }
891
892 int
893 glue_sub_del( BackendDB *b0 )
894 {
895         BackendDB *be;
896         int rc = 0;
897
898         /* Find the top backend for this subordinate */
899         be = b0;
900         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
901                 slap_overinfo *oi;
902                 slap_overinst *on;
903                 glueinfo *gi;
904                 int i;
905
906                 if ( SLAP_GLUE_SUBORDINATE( be ))
907                         continue;
908                 if ( !SLAP_GLUE_INSTANCE( be ))
909                         continue;
910                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
911                         continue;
912
913                 /* OK, got the right backend, find the overlay */
914                 oi = (slap_overinfo *)be->bd_info;
915                 for ( on=oi->oi_list; on; on=on->on_next ) {
916                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
917                                 break;
918                 }
919                 assert( on != NULL );
920                 gi = on->on_bi.bi_private;
921                 for ( i=0; i < gi->gi_nodes; i++ ) {
922                         if ( gi->gi_n[i].gn_be == b0 ) {
923                                 int j;
924
925                                 for (j=i+1; j < gi->gi_nodes; j++)
926                                         gi->gi_n[j-1] = gi->gi_n[j];
927
928                                 gi->gi_nodes--;
929                         }
930                 }
931         }
932         if ( be == NULL )
933                 rc = LDAP_NO_SUCH_OBJECT;
934
935         return rc;
936 }
937
938 typedef struct glue_Addrec {
939         struct glue_Addrec *ga_next;
940         BackendDB *ga_be;
941 } glue_Addrec;
942
943 /* List of added subordinates */
944 static glue_Addrec *ga_list;
945
946 /* Attach all the subordinate backends to their superior */
947 int
948 glue_sub_attach()
949 {
950         glue_Addrec *ga, *gnext = NULL;
951         int rc = 0;
952
953         /* For all the subordinate backends */
954         for ( ga=ga_list; ga != NULL; ga = gnext ) {
955                 BackendDB *be;
956
957                 gnext = ga->ga_next;
958
959                 /* Find the top backend for this subordinate */
960                 be = ga->ga_be;
961                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
962                         slap_overinfo *oi;
963                         slap_overinst *on;
964                         glueinfo *gi;
965
966                         if ( SLAP_GLUE_SUBORDINATE( be ))
967                                 continue;
968                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
969                                 continue;
970
971                         /* If it's not already configured, set up the overlay */
972                         if ( !SLAP_GLUE_INSTANCE( be )) {
973                                 rc = overlay_config( be, glue.on_bi.bi_type );
974                                 if ( rc )
975                                         break;
976                         }
977                         /* Find the overlay instance */
978                         oi = (slap_overinfo *)be->bd_info;
979                         for ( on=oi->oi_list; on; on=on->on_next ) {
980                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
981                                         break;
982                         }
983                         assert( on != NULL );
984                         gi = on->on_bi.bi_private;
985                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
986                                 gi->gi_nodes * sizeof(gluenode));
987                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
988                         dnParent( &ga->ga_be->be_nsuffix[0],
989                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
990                         gi->gi_nodes++;
991                         on->on_bi.bi_private = gi;
992                         break;
993                 }
994                 if ( !be ) {
995                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
996                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
997                         rc = LDAP_NO_SUCH_OBJECT;
998                 }
999                 ch_free( ga );
1000                 if ( rc ) break;
1001         }
1002
1003         ga_list = gnext;
1004
1005         return rc;
1006 }
1007
1008 int
1009 glue_sub_add( BackendDB *be, int advert, int online )
1010 {
1011         glue_Addrec *ga;
1012         int rc = 0;
1013
1014         if ( overlay_is_inst( be, "glue" )) {
1015                 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1016                         "cannot be a subordinate!\n",
1017                         be->be_suffix[0].bv_val, 0, 0 );
1018                 return LDAP_OTHER;
1019         }
1020         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1021         if ( advert )
1022                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1023
1024         ga = ch_malloc( sizeof( glue_Addrec ));
1025         ga->ga_next = ga_list;
1026         ga->ga_be = be;
1027         ga_list = ga;
1028
1029         if ( online )
1030                 rc = glue_sub_attach();
1031
1032         return rc;
1033 }
1034
1035 int
1036 glue_sub_init()
1037 {
1038         glue.on_bi.bi_type = "glue";
1039
1040         glue.on_bi.bi_db_init = glue_db_init;
1041         glue.on_bi.bi_db_close = glue_db_close;
1042         glue.on_bi.bi_db_destroy = glue_db_destroy;
1043
1044         glue.on_bi.bi_op_search = glue_op_search;
1045         glue.on_bi.bi_op_modify = glue_op_func;
1046         glue.on_bi.bi_op_modrdn = glue_op_func;
1047         glue.on_bi.bi_op_add = glue_op_func;
1048         glue.on_bi.bi_op_delete = glue_op_func;
1049         glue.on_bi.bi_extended = glue_op_func;
1050
1051         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1052         glue.on_bi.bi_chk_controls = glue_chk_controls;
1053         glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1054         glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1055
1056         glue.on_response = glue_response;
1057
1058         return overlay_register( &glue );
1059 }