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