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