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