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