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