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