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