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