]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
don't set dummy cookie when dealing with last database (ITS#6507)
[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 "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                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
423                 if (op->o_bd && op->o_bd->be_search) {
424                         rs->sr_err = op->o_bd->be_search( op, rs );
425                 }
426                 return rs->sr_err;
427
428         case LDAP_SCOPE_ONELEVEL:
429         case LDAP_SCOPE_SUBTREE:
430         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
431                 op->o_callback = &cb;
432                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
433                 scope0 = op->ors_scope;
434                 tlimit0 = op->ors_tlimit;
435                 dn = op->o_req_dn;
436                 ndn = op->o_req_ndn;
437                 b1 = op->o_bd;
438
439                 /*
440                  * Execute in reverse order, most specific first 
441                  */
442                 for (i = gi->gi_nodes; i >= 0; i--) {
443                         if ( i == gi->gi_nodes ) {
444                                 btmp = b0;
445                                 pdn = &gi->gi_pdn;
446                         } else {
447                                 btmp = gi->gi_n[i].gn_be;
448                                 pdn = &gi->gi_n[i].gn_pdn;
449                         }
450                         if (!btmp || !btmp->be_search)
451                                 continue;
452                         if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
453                                 continue;
454                         if (get_no_subordinate_glue(op) && btmp != b1)
455                                 continue;
456                         /* If we remembered which backend we were on before,
457                          * skip down to it now
458                          */
459                         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
460                                 op->o_conn->c_pagedresults_state.ps_be &&
461                                 op->o_conn->c_pagedresults_state.ps_be != btmp )
462                                 continue;
463
464                         if (tlimit0 != SLAP_NO_LIMIT) {
465                                 op->o_time = slap_get_time();
466                                 op->ors_tlimit = stoptime - op->o_time;
467                                 if (op->ors_tlimit <= 0) {
468                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
469                                         break;
470                                 }
471                         }
472                         rs->sr_err = 0;
473                         /*
474                          * check for abandon 
475                          */
476                         if (op->o_abandon) {
477                                 goto end_of_loop;
478                         }
479                         op->o_bd = btmp;
480
481                         assert( op->o_bd->be_suffix != NULL );
482                         assert( op->o_bd->be_nsuffix != NULL );
483                         
484                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
485                                 dn_match(pdn, &ndn))
486                         {
487                                 struct berval mdn, mndn;
488                                 op->ors_scope = LDAP_SCOPE_BASE;
489                                 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
490                                 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
491                                 rs->sr_err = op->o_bd->be_search(op, rs);
492                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
493                                         gs.err = LDAP_SUCCESS;
494                                 }
495                                 op->ors_scope = LDAP_SCOPE_ONELEVEL;
496                                 if ( op->o_req_dn.bv_val == mdn.bv_val )
497                                         op->o_req_dn = dn;
498                                 if ( op->o_req_ndn.bv_val == mndn.bv_val )
499                                         op->o_req_ndn = ndn;
500
501                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
502                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
503                         {
504                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
505
506                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
507                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
508                         {
509                                 struct berval mdn, mndn;
510                                 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
511                                 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
512                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
513                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
514                                         gs.err = LDAP_SUCCESS;
515                                 }
516                                 if ( op->o_req_dn.bv_val == mdn.bv_val )
517                                         op->o_req_dn = dn;
518                                 if ( op->o_req_ndn.bv_val == mndn.bv_val )
519                                         op->o_req_ndn = ndn;
520
521                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
522                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
523                         }
524
525                         switch ( gs.err ) {
526
527                         /*
528                          * Add errors that should result in dropping
529                          * the search
530                          */
531                         case LDAP_SIZELIMIT_EXCEEDED:
532                         case LDAP_TIMELIMIT_EXCEEDED:
533                         case LDAP_ADMINLIMIT_EXCEEDED:
534                         case LDAP_NO_SUCH_OBJECT:
535 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
536                         case LDAP_X_CANNOT_CHAIN:
537 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
538                                 goto end_of_loop;
539
540                         case LDAP_SUCCESS:
541                                 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
542                                         PagedResultsState *ps = op->o_pagedresults_state;
543
544                                         /* Assume this backend can be forgotten now */
545                                         op->o_conn->c_pagedresults_state.ps_be = NULL;
546
547                                         /* If we have a full page, exit the loop. We may
548                                          * need to remember this backend so we can continue
549                                          * from here on a subsequent request.
550                                          */
551                                         if ( rs->sr_nentries >= ps->ps_size ) {
552                                                 /* Don't bother to remember the first backend.
553                                                  * Only remember the last one if there's more state left.
554                                                  */
555                                                 if ( op->o_bd != b0 &&
556                                                         ( !BER_BVISNULL( &op->o_conn->c_pagedresults_state.ps_cookieval ) ||
557                                                         op->o_bd != gi->gi_n[0].gn_be ))
558                                                 {
559                                                         op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
560                                                 }
561
562                                                 /* Check whether the cookie is empty,
563                                                  * and give remaining databases a chance
564                                                  */
565                                                 if ( op->o_bd != gi->gi_n[0].gn_be ||
566                                                         BER_BVISNULL( &op->o_conn->c_pagedresults_state.ps_cookieval ) )
567                                                 {
568                                                         int             c;
569
570                                                         for ( c = 0; gs.ctrls[c] != NULL; c++ ) {
571                                                                 if ( strcmp( gs.ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 ) {
572                                                                         break;
573                                                                 }
574                                                         }
575
576                                                         if ( gs.ctrls[c] != NULL ) {
577                                                                 BerElementBuffer berbuf;
578                                                                 BerElement      *ber = (BerElement *)&berbuf;
579                                                                 ber_tag_t       tag;
580                                                                 ber_int_t       size;
581                                                                 struct berval   cookie, value;
582                                                         
583                                                                 ber_init2( ber, &gs.ctrls[c]->ldctl_value, LBER_USE_DER );
584
585                                                                 tag = ber_scanf( ber, "{im}", &size, &cookie );
586                                                                 assert( tag != LBER_ERROR );
587
588                                                                 if ( BER_BVISEMPTY( &cookie ) && op->o_bd != gi->gi_n[0].gn_be ) {
589                                                                         /* delete old, create new cookie with NOID */
590                                                                         PagedResultsCookie respcookie = (PagedResultsCookie)NOID;
591                                                                         ber_len_t oidlen = strlen( gs.ctrls[c]->ldctl_oid );
592                                                                         LDAPControl *newctrl;
593
594                                                                         /* it's next database's turn */
595                                                                         if ( btmp == b0 ) {
596                                                                                 op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[gi->gi_nodes - 1].gn_be;
597
598                                                                         } else {
599                                                                                 op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[(i > 0 ? i - 1: 0)].gn_be;
600                                                                         }
601
602                                                                         cookie.bv_val = (char *)&respcookie;
603                                                                         cookie.bv_len = sizeof( PagedResultsCookie );
604
605                                                                         ber_init2( ber, NULL, LBER_USE_DER );
606                                                                         ber_printf( ber, "{iO}", 0, &cookie );
607                                                                         ber_flatten2( ber, &value, 0 );
608
609                                                                         newctrl = op->o_tmprealloc( gs.ctrls[c],
610                                                                                 sizeof(LDAPControl) + oidlen + 1 + value.bv_len + 1,
611                                                                                 op->o_tmpmemctx);
612                                                                         newctrl->ldctl_iscritical = gs.ctrls[c]->ldctl_iscritical;
613                                                                         newctrl->ldctl_oid = (char *)&newctrl[1];
614                                                                         lutil_strcopy( newctrl->ldctl_oid, gs.ctrls[c]->ldctl_oid );
615                                                                         newctrl->ldctl_value.bv_len = value.bv_len;
616                                                                         lutil_memcopy( newctrl->ldctl_value.bv_val,
617                                                                                 value.bv_val, value.bv_len );
618
619                                                                         gs.ctrls[c] = newctrl;
620
621                                                                         ber_free_buf( ber );
622
623                                                                 } else if ( op->o_bd != gi->gi_n[0].gn_be ) {
624                                                                         /* if cookie not empty, it's again this database's turn */
625                                                                         op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
626                                                                 }
627                                                         }
628                                                 }
629
630                                                 goto end_of_loop;
631                                         }
632
633                                         /* This backend has run out of entries, but more responses
634                                          * can fit in the page. Fake a reset of the state so the
635                                          * next backend will start up properly. Only back-[bh]db
636                                          * and back-sql look at this state info.
637                                          */
638                                         if ( ps->ps_cookieval.bv_len == sizeof( PagedResultsCookie )) {
639                                                 ps->ps_cookie = (PagedResultsCookie)0;
640 #if 0
641                                                 memset( ps->ps_cookieval.bv_val, 0,
642                                                         sizeof( PagedResultsCookie ));
643 #endif
644                                                 BER_BVZERO( &ps->ps_cookieval );
645                                         }
646
647                                         {
648                                                 /* change the size of the page in the request
649                                                  * that will be propagated, and reset the cookie */
650                                                 BerElementBuffer berbuf;
651                                                 BerElement *ber = (BerElement *)&berbuf;
652                                                 int size = ps->ps_size - rs->sr_nentries;
653                                                 struct berval cookie = BER_BVC(""), value;
654                                                 int c;
655
656                                                 for (c = 0; op->o_ctrls[c] != NULL; c++) {
657                                                         if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
658                                                                 break;
659                                                 }
660
661                                                 assert( op->o_ctrls[c] != NULL );
662
663                                                 ber_init2( ber, NULL, LBER_USE_DER );
664                                                 ber_printf( ber, "{iO}", size, &cookie );
665                                                 ber_flatten2( ber, &value, 0 );
666                                                 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
667                                                 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
668                                                 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
669                                                         value.bv_val, value.bv_len );
670                                                 ber_free_buf( ber );
671                                         }
672                                 }
673                                 
674                         default:
675                                 break;
676                         }
677                 }
678 end_of_loop:;
679                 op->ors_scope = scope0;
680                 op->ors_tlimit = tlimit0;
681                 op->o_time = starttime;
682
683                 break;
684         }
685
686         op->o_callback = cb.sc_next;
687         if ( op->o_abandon ) {
688                 rs->sr_err = SLAPD_ABANDON;
689         } else {
690                 rs->sr_err = gs.err;
691                 rs->sr_matched = gs.matched;
692                 rs->sr_ref = gs.refs;
693         }
694         rs->sr_ctrls = gs.ctrls;
695
696         send_ldap_result( op, rs );
697
698         op->o_bd = b0;
699         op->o_bd->bd_info = bi0;
700         if (gs.matched)
701                 free (gs.matched);
702         if (gs.refs)
703                 ber_bvarray_free(gs.refs);
704         if (gs.ctrls) {
705                 for (i = gs.nctrls; --i >= 0; ) {
706                         op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
707                 }
708                 op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
709         }
710         return rs->sr_err;
711 }
712
713 static BackendDB toolDB;
714
715 static int
716 glue_tool_entry_open (
717         BackendDB *b0,
718         int mode
719 )
720 {
721         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
722
723         /* We don't know which backend to talk to yet, so just
724          * remember the mode and move on...
725          */
726
727         glueMode = mode;
728         glueBack = NULL;
729         toolDB = *b0;
730         toolDB.bd_info = oi->oi_orig;
731
732         /* Sanity checks */
733         {
734                 slap_overinst *on = glue_tool_inst( b0->bd_info );
735                 glueinfo        *gi = on->on_bi.bi_private;
736
737                 int i;
738                 for (i = 0; i < gi->gi_nodes; i++) {
739                         BackendDB *bd;
740                         struct berval pdn;
741         
742                         dnParent( &gi->gi_n[i].gn_be->be_nsuffix[0], &pdn );
743                         bd = select_backend( &pdn, 0 );
744                         if ( bd ) {
745                                 ID id;
746                                 BackendDB db;
747         
748                                 if ( overlay_is_over( bd ) ) {
749                                         slap_overinfo *oi = (slap_overinfo *)bd->bd_info;
750                                         db = *bd;
751                                         db.bd_info = oi->oi_orig;
752                                         bd = &db;
753                                 }
754         
755                                 if ( !bd->bd_info->bi_tool_dn2id_get
756                                         || !bd->bd_info->bi_tool_entry_open
757                                         || !bd->bd_info->bi_tool_entry_close )
758                                 {
759                                         continue;
760                                 }
761         
762                                 bd->bd_info->bi_tool_entry_open( bd, 0 );
763                                 id = bd->bd_info->bi_tool_dn2id_get( bd, &gi->gi_n[i].gn_be->be_nsuffix[0] );
764                                 bd->bd_info->bi_tool_entry_close( bd );
765                                 if ( id != NOID ) {
766                                         Debug( LDAP_DEBUG_ANY,
767                                                 "glue_tool_entry_open: subordinate database suffix entry DN=\"%s\" also present in superior database rooted at DN=\"%s\"\n",
768                                                 gi->gi_n[i].gn_be->be_suffix[0].bv_val, bd->be_suffix[0].bv_val, 0 );
769                                         return LDAP_OTHER;
770                                 }
771                         }
772                 }
773         }
774         
775         return 0;
776 }
777
778 static int
779 glue_tool_entry_close (
780         BackendDB *b0
781 )
782 {
783         int rc = 0;
784
785         if (glueBack && glueBack != GLUEBACK_DONE) {
786                 if (!glueBack->be_entry_close)
787                         return 0;
788                 rc = glueBack->be_entry_close (glueBack);
789         }
790         return rc;
791 }
792
793 static slap_overinst *
794 glue_tool_inst(
795         BackendInfo *bi
796 )
797 {
798         slap_overinfo   *oi = (slap_overinfo *)bi;
799         slap_overinst   *on;
800
801         for ( on = oi->oi_list; on; on=on->on_next ) {
802                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
803                         return on;
804         }
805         return NULL;
806 }
807
808 /* This function will only be called in tool mode */
809 static int
810 glue_open (
811         BackendInfo *bi
812 )
813 {
814         slap_overinst *on = glue_tool_inst( bi );
815         glueinfo                *gi = on->on_bi.bi_private;
816         static int glueOpened = 0;
817         int i, j, same, bsame = 0, rc = 0;
818         ConfigReply cr = {0};
819
820         if (glueOpened) return 0;
821
822         glueOpened = 1;
823
824         /* If we were invoked in tool mode, open all the underlying backends */
825         if (slapMode & SLAP_TOOL_MODE) {
826                 for (i = 0; i<gi->gi_nodes; i++) {
827                         same = 0;
828                         /* Same bi_open as our main backend? */
829                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
830                                 on->on_info->oi_orig->bi_open )
831                                 bsame = 1;
832
833                         /* Loop thru the bd_info's and make sure we only
834                          * invoke their bi_open functions once each.
835                          */
836                         for ( j = 0; j<i; j++ ) {
837                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
838                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
839                                         same = 1;
840                                         break;
841                                 }
842                         }
843                         /* OK, it's unique and non-NULL, call it. */
844                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
845                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
846                                         gi->gi_n[i].gn_be->bd_info );
847                         /* Let backend.c take care of the rest of startup */
848                         if ( !rc )
849                                 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
850                         if ( rc ) break;
851                 }
852                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
853                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
854
855         } /* other case is impossible */
856         return rc;
857 }
858
859 /* This function will only be called in tool mode */
860 static int
861 glue_close (
862         BackendInfo *bi
863 )
864 {
865         static int glueClosed = 0;
866         int rc = 0;
867
868         if (glueClosed) return 0;
869
870         glueClosed = 1;
871
872         if (slapMode & SLAP_TOOL_MODE) {
873                 rc = backend_shutdown( NULL );
874         }
875         return rc;
876 }
877
878 static int
879 glue_entry_get_rw (
880         Operation               *op,
881         struct berval   *dn,
882         ObjectClass             *oc,
883         AttributeDescription    *ad,
884         int     rw,
885         Entry   **e )
886 {
887         int rc;
888         BackendDB *b0 = op->o_bd;
889         op->o_bd = glue_back_select( b0, dn );
890
891         if ( op->o_bd->be_fetch ) {
892                 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
893         } else {
894                 rc = LDAP_UNWILLING_TO_PERFORM;
895         }
896         op->o_bd =b0;
897         return rc;
898 }
899
900 static int
901 glue_entry_release_rw (
902         Operation *op,
903         Entry *e,
904         int rw
905 )
906 {
907         BackendDB *b0 = op->o_bd;
908         int rc = -1;
909
910         op->o_bd = glue_back_select (b0, &e->e_nname);
911
912         if ( op->o_bd->be_release ) {
913                 rc = op->o_bd->be_release( op, e, rw );
914
915         } else {
916                 /* FIXME: mimic be_entry_release_rw
917                  * when no be_release() available */
918                 /* free entry */
919                 entry_free( e );
920                 rc = 0;
921         }
922         op->o_bd = b0;
923         return rc;
924 }
925
926 static struct berval *glue_base;
927 static int glue_scope;
928 static Filter *glue_filter;
929
930 static ID
931 glue_tool_entry_first (
932         BackendDB *b0
933 )
934 {
935         slap_overinst   *on = glue_tool_inst( b0->bd_info );
936         glueinfo                *gi = on->on_bi.bi_private;
937         int i;
938         ID rc;
939
940         /* If we're starting from scratch, start at the most general */
941         if (!glueBack) {
942                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
943                         glueBack = &toolDB;
944                 } else {
945                         for (i = gi->gi_nodes-1; i >= 0; i--) {
946                                 if (gi->gi_n[i].gn_be->be_entry_open &&
947                                         gi->gi_n[i].gn_be->be_entry_first) {
948                                                 glueBack = gi->gi_n[i].gn_be;
949                                         break;
950                                 }
951                         }
952                 }
953         }
954         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
955                 glueBack->be_entry_open (glueBack, glueMode) != 0)
956                 return NOID;
957
958         rc = glueBack->be_entry_first (glueBack);
959         while ( rc == NOID ) {
960                 if ( glueBack && glueBack->be_entry_close )
961                         glueBack->be_entry_close (glueBack);
962                 for (i=0; i<gi->gi_nodes; i++) {
963                         if (gi->gi_n[i].gn_be == glueBack)
964                                 break;
965                 }
966                 if (i == 0) {
967                         glueBack = GLUEBACK_DONE;
968                         break;
969                 } else {
970                         glueBack = gi->gi_n[i-1].gn_be;
971                         rc = glue_tool_entry_first (b0);
972                         if ( glueBack == GLUEBACK_DONE ) {
973                                 break;
974                         }
975                 }
976         }
977         return rc;
978 }
979
980 static ID
981 glue_tool_entry_first_x (
982         BackendDB *b0,
983         struct berval *base,
984         int scope,
985         Filter *f
986 )
987 {
988         slap_overinst   *on = glue_tool_inst( b0->bd_info );
989         glueinfo                *gi = on->on_bi.bi_private;
990         int i;
991         ID rc;
992
993         glue_base = base;
994         glue_scope = scope;
995         glue_filter = f;
996
997         /* If we're starting from scratch, start at the most general */
998         if (!glueBack) {
999                 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
1000                         glueBack = &toolDB;
1001                 } else {
1002                         for (i = gi->gi_nodes-1; i >= 0; i--) {
1003                                 if (gi->gi_n[i].gn_be->be_entry_open &&
1004                                         gi->gi_n[i].gn_be->be_entry_first_x)
1005                                 {
1006                                         glueBack = gi->gi_n[i].gn_be;
1007                                         break;
1008                                 }
1009                         }
1010                 }
1011         }
1012         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
1013                 glueBack->be_entry_open (glueBack, glueMode) != 0)
1014                 return NOID;
1015
1016         rc = glueBack->be_entry_first_x (glueBack,
1017                 glue_base, glue_scope, glue_filter);
1018         while ( rc == NOID ) {
1019                 if ( glueBack && glueBack->be_entry_close )
1020                         glueBack->be_entry_close (glueBack);
1021                 for (i=0; i<gi->gi_nodes; i++) {
1022                         if (gi->gi_n[i].gn_be == glueBack)
1023                                 break;
1024                 }
1025                 if (i == 0) {
1026                         glueBack = GLUEBACK_DONE;
1027                         break;
1028                 } else {
1029                         glueBack = gi->gi_n[i-1].gn_be;
1030                         rc = glue_tool_entry_first_x (b0,
1031                                 glue_base, glue_scope, glue_filter);
1032                         if ( glueBack == GLUEBACK_DONE ) {
1033                                 break;
1034                         }
1035                 }
1036         }
1037         return rc;
1038 }
1039
1040 static ID
1041 glue_tool_entry_next (
1042         BackendDB *b0
1043 )
1044 {
1045         slap_overinst   *on = glue_tool_inst( b0->bd_info );
1046         glueinfo                *gi = on->on_bi.bi_private;
1047         int i;
1048         ID rc;
1049
1050         if (!glueBack || !glueBack->be_entry_next)
1051                 return NOID;
1052
1053         rc = glueBack->be_entry_next (glueBack);
1054
1055         /* If we ran out of entries in one database, move on to the next */
1056         while (rc == NOID) {
1057                 if ( glueBack && glueBack->be_entry_close )
1058                         glueBack->be_entry_close (glueBack);
1059                 for (i=0; i<gi->gi_nodes; i++) {
1060                         if (gi->gi_n[i].gn_be == glueBack)
1061                                 break;
1062                 }
1063                 if (i == 0) {
1064                         glueBack = GLUEBACK_DONE;
1065                         break;
1066                 } else {
1067                         glueBack = gi->gi_n[i-1].gn_be;
1068                         if ( glue_base || glue_filter ) {
1069                                 /* using entry_first_x() */
1070                                 rc = glue_tool_entry_first_x (b0,
1071                                         glue_base, glue_scope, glue_filter);
1072
1073                         } else {
1074                                 /* using entry_first() */
1075                                 rc = glue_tool_entry_first (b0);
1076                         }
1077                         if ( glueBack == GLUEBACK_DONE ) {
1078                                 break;
1079                         }
1080                 }
1081         }
1082         return rc;
1083 }
1084
1085 static ID
1086 glue_tool_dn2id_get (
1087         BackendDB *b0,
1088         struct berval *dn
1089 )
1090 {
1091         BackendDB *be, b2;
1092         int rc = -1;
1093
1094         b2 = *b0;
1095         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1096         be = glue_back_select (&b2, dn);
1097         if ( be == &b2 ) be = &toolDB;
1098
1099         if (!be->be_dn2id_get)
1100                 return NOID;
1101
1102         if (!glueBack) {
1103                 if ( be->be_entry_open ) {
1104                         rc = be->be_entry_open (be, glueMode);
1105                 }
1106                 if (rc != 0) {
1107                         return NOID;
1108                 }
1109         } else if (be != glueBack) {
1110                 /* If this entry belongs in a different branch than the
1111                  * previous one, close the current database and open the
1112                  * new one.
1113                  */
1114                 if ( glueBack->be_entry_close ) {
1115                         glueBack->be_entry_close (glueBack);
1116                 }
1117                 if ( be->be_entry_open ) {
1118                         rc = be->be_entry_open (be, glueMode);
1119                 }
1120                 if (rc != 0) {
1121                         return NOID;
1122                 }
1123         }
1124         glueBack = be;
1125         return be->be_dn2id_get (be, dn);
1126 }
1127
1128 static Entry *
1129 glue_tool_entry_get (
1130         BackendDB *b0,
1131         ID id
1132 )
1133 {
1134         if (!glueBack || !glueBack->be_entry_get)
1135                 return NULL;
1136
1137         return glueBack->be_entry_get (glueBack, id);
1138 }
1139
1140 static ID
1141 glue_tool_entry_put (
1142         BackendDB *b0,
1143         Entry *e,
1144         struct berval *text
1145 )
1146 {
1147         BackendDB *be, b2;
1148         int rc = -1;
1149
1150         b2 = *b0;
1151         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1152         be = glue_back_select (&b2, &e->e_nname);
1153         if ( be == &b2 ) be = &toolDB;
1154
1155         if (!be->be_entry_put)
1156                 return NOID;
1157
1158         if (!glueBack) {
1159                 if ( be->be_entry_open ) {
1160                         rc = be->be_entry_open (be, glueMode);
1161                 }
1162                 if (rc != 0) {
1163                         return NOID;
1164                 }
1165         } else if (be != glueBack) {
1166                 /* If this entry belongs in a different branch than the
1167                  * previous one, close the current database and open the
1168                  * new one.
1169                  */
1170                 if ( glueBack->be_entry_close ) {
1171                         glueBack->be_entry_close (glueBack);
1172                 }
1173                 if ( be->be_entry_open ) {
1174                         rc = be->be_entry_open (be, glueMode);
1175                 }
1176                 if (rc != 0) {
1177                         return NOID;
1178                 }
1179         }
1180         glueBack = be;
1181         return be->be_entry_put (be, e, text);
1182 }
1183
1184 static ID
1185 glue_tool_entry_modify (
1186         BackendDB *b0,
1187         Entry *e,
1188         struct berval *text
1189 )
1190 {
1191         if (!glueBack || !glueBack->be_entry_modify)
1192                 return NOID;
1193
1194         return glueBack->be_entry_modify (glueBack, e, text);
1195 }
1196
1197 static int
1198 glue_tool_entry_reindex (
1199         BackendDB *b0,
1200         ID id,
1201         AttributeDescription **adv
1202 )
1203 {
1204         if (!glueBack || !glueBack->be_entry_reindex)
1205                 return -1;
1206
1207         return glueBack->be_entry_reindex (glueBack, id, adv);
1208 }
1209
1210 static int
1211 glue_tool_sync (
1212         BackendDB *b0
1213 )
1214 {
1215         slap_overinst   *on = glue_tool_inst( b0->bd_info );
1216         glueinfo                *gi = on->on_bi.bi_private;
1217         BackendInfo             *bi = b0->bd_info;
1218         int i;
1219
1220         /* just sync everyone */
1221         for (i = 0; i<gi->gi_nodes; i++)
1222                 if (gi->gi_n[i].gn_be->be_sync)
1223                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1224         b0->bd_info = on->on_info->oi_orig;
1225         if ( b0->be_sync )
1226                 b0->be_sync( b0 );
1227         b0->bd_info = bi;
1228         return 0;
1229 }
1230
1231 typedef struct glue_Addrec {
1232         struct glue_Addrec *ga_next;
1233         BackendDB *ga_be;
1234 } glue_Addrec;
1235
1236 /* List of added subordinates */
1237 static glue_Addrec *ga_list;
1238 static int ga_adding;
1239
1240 static int
1241 glue_db_init(
1242         BackendDB *be,
1243         ConfigReply *cr
1244 )
1245 {
1246         slap_overinst   *on = (slap_overinst *)be->bd_info;
1247         slap_overinfo   *oi = on->on_info;
1248         BackendInfo     *bi = oi->oi_orig;
1249         glueinfo *gi;
1250
1251         if ( SLAP_GLUE_SUBORDINATE( be )) {
1252                 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1253                         "cannot have glue overlay!\n",
1254                         be->be_suffix[0].bv_val, 0, 0 );
1255                 return LDAP_OTHER;
1256         }
1257
1258         gi = ch_calloc( 1, sizeof(glueinfo));
1259         on->on_bi.bi_private = gi;
1260         dnParent( be->be_nsuffix, &gi->gi_pdn );
1261
1262         /* Currently the overlay framework doesn't handle these entry points
1263          * but we need them....
1264          */
1265         oi->oi_bi.bi_open = glue_open;
1266         oi->oi_bi.bi_close = glue_close;
1267
1268         /* Only advertise these if the root DB supports them */
1269         if ( bi->bi_tool_entry_open )
1270                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1271         if ( bi->bi_tool_entry_close )
1272                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1273         if ( bi->bi_tool_entry_first )
1274                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1275         /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1276         if ( bi->bi_tool_entry_first_x )
1277                 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1278         if ( bi->bi_tool_entry_next )
1279                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1280         if ( bi->bi_tool_entry_get )
1281                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1282         if ( bi->bi_tool_dn2id_get )
1283                 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1284         if ( bi->bi_tool_entry_put )
1285                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1286         if ( bi->bi_tool_entry_reindex )
1287                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1288         if ( bi->bi_tool_entry_modify )
1289                 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1290         if ( bi->bi_tool_sync )
1291                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1292
1293         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1294
1295         if ( ga_list ) {
1296                 be->bd_info = (BackendInfo *)oi;
1297                 glue_sub_attach( 1 );
1298         }
1299
1300         return 0;
1301 }
1302
1303 static int
1304 glue_db_destroy (
1305         BackendDB *be,
1306         ConfigReply *cr
1307 )
1308 {
1309         slap_overinst   *on = (slap_overinst *)be->bd_info;
1310         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
1311
1312         free (gi);
1313         return SLAP_CB_CONTINUE;
1314 }
1315
1316 static int
1317 glue_db_close( 
1318         BackendDB *be,
1319         ConfigReply *cr
1320 )
1321 {
1322         slap_overinst   *on = (slap_overinst *)be->bd_info;
1323
1324         on->on_info->oi_bi.bi_db_close = 0;
1325         return 0;
1326 }
1327
1328 int
1329 glue_sub_del( BackendDB *b0 )
1330 {
1331         BackendDB *be;
1332         int rc = 0;
1333
1334         /* Find the top backend for this subordinate */
1335         be = b0;
1336         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1337                 slap_overinfo *oi;
1338                 slap_overinst *on;
1339                 glueinfo *gi;
1340                 int i;
1341
1342                 if ( SLAP_GLUE_SUBORDINATE( be ))
1343                         continue;
1344                 if ( !SLAP_GLUE_INSTANCE( be ))
1345                         continue;
1346                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1347                         continue;
1348
1349                 /* OK, got the right backend, find the overlay */
1350                 oi = (slap_overinfo *)be->bd_info;
1351                 for ( on=oi->oi_list; on; on=on->on_next ) {
1352                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1353                                 break;
1354                 }
1355                 assert( on != NULL );
1356                 gi = on->on_bi.bi_private;
1357                 for ( i=0; i < gi->gi_nodes; i++ ) {
1358                         if ( gi->gi_n[i].gn_be == b0 ) {
1359                                 int j;
1360
1361                                 for (j=i+1; j < gi->gi_nodes; j++)
1362                                         gi->gi_n[j-1] = gi->gi_n[j];
1363
1364                                 gi->gi_nodes--;
1365                         }
1366                 }
1367         }
1368         if ( be == NULL )
1369                 rc = LDAP_NO_SUCH_OBJECT;
1370
1371         return rc;
1372 }
1373
1374
1375 /* Attach all the subordinate backends to their superior */
1376 int
1377 glue_sub_attach( int online )
1378 {
1379         glue_Addrec *ga, *gnext = NULL;
1380         int rc = 0;
1381
1382         if ( ga_adding )
1383                 return 0;
1384
1385         ga_adding = 1;
1386
1387         /* For all the subordinate backends */
1388         for ( ga=ga_list; ga != NULL; ga = gnext ) {
1389                 BackendDB *be;
1390
1391                 gnext = ga->ga_next;
1392
1393                 /* Find the top backend for this subordinate */
1394                 be = ga->ga_be;
1395                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1396                         slap_overinfo *oi;
1397                         slap_overinst *on;
1398                         glueinfo *gi;
1399
1400                         if ( SLAP_GLUE_SUBORDINATE( be ))
1401                                 continue;
1402                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1403                                 continue;
1404
1405                         /* If it's not already configured, set up the overlay */
1406                         if ( !SLAP_GLUE_INSTANCE( be )) {
1407                                 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1408                                 if ( rc )
1409                                         break;
1410                         }
1411                         /* Find the overlay instance */
1412                         oi = (slap_overinfo *)be->bd_info;
1413                         for ( on=oi->oi_list; on; on=on->on_next ) {
1414                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1415                                         break;
1416                         }
1417                         assert( on != NULL );
1418                         gi = on->on_bi.bi_private;
1419                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1420                                 gi->gi_nodes * sizeof(gluenode));
1421                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1422                         dnParent( &ga->ga_be->be_nsuffix[0],
1423                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
1424                         gi->gi_nodes++;
1425                         on->on_bi.bi_private = gi;
1426                         ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1427                         break;
1428                 }
1429                 if ( !be ) {
1430                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1431                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1432                         /* allow this for now, assume a superior will
1433                          * be added later
1434                          */
1435                         if ( online ) {
1436                                 rc = 0;
1437                                 gnext = ga_list;
1438                                 break;
1439                         }
1440                         rc = LDAP_NO_SUCH_OBJECT;
1441                 }
1442                 ch_free( ga );
1443                 if ( rc ) break;
1444         }
1445
1446         ga_list = gnext;
1447
1448         ga_adding = 0;
1449
1450         return rc;
1451 }
1452
1453 int
1454 glue_sub_add( BackendDB *be, int advert, int online )
1455 {
1456         glue_Addrec *ga;
1457         int rc = 0;
1458
1459         if ( overlay_is_inst( be, "glue" )) {
1460                 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1461                         "cannot be a subordinate!\n",
1462                         be->be_suffix[0].bv_val, 0, 0 );
1463                 return LDAP_OTHER;
1464         }
1465         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1466         if ( advert )
1467                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1468
1469         ga = ch_malloc( sizeof( glue_Addrec ));
1470         ga->ga_next = ga_list;
1471         ga->ga_be = be;
1472         ga_list = ga;
1473
1474         if ( online )
1475                 rc = glue_sub_attach( online );
1476
1477         return rc;
1478 }
1479
1480 static int
1481 glue_access_allowed(
1482         Operation               *op,
1483         Entry                   *e,
1484         AttributeDescription    *desc,
1485         struct berval           *val,
1486         slap_access_t           access,
1487         AccessControlState      *state,
1488         slap_mask_t             *maskp )
1489 {
1490         BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
1491         int rc;
1492
1493         if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
1494                 return SLAP_CB_CONTINUE;
1495
1496         b0 = op->o_bd;
1497         op->o_bd = be;
1498         rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
1499         op->o_bd = b0;
1500         return rc;
1501 }
1502
1503 int
1504 glue_sub_init()
1505 {
1506         glue.on_bi.bi_type = "glue";
1507
1508         glue.on_bi.bi_db_init = glue_db_init;
1509         glue.on_bi.bi_db_close = glue_db_close;
1510         glue.on_bi.bi_db_destroy = glue_db_destroy;
1511
1512         glue.on_bi.bi_op_search = glue_op_search;
1513         glue.on_bi.bi_op_modify = glue_op_func;
1514         glue.on_bi.bi_op_modrdn = glue_op_func;
1515         glue.on_bi.bi_op_add = glue_op_func;
1516         glue.on_bi.bi_op_delete = glue_op_func;
1517         glue.on_bi.bi_extended = glue_op_func;
1518
1519         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1520         glue.on_bi.bi_chk_controls = glue_chk_controls;
1521         glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1522         glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1523         glue.on_bi.bi_access_allowed = glue_access_allowed;
1524
1525         glue.on_response = glue_response;
1526
1527         return overlay_register( &glue );
1528 }