]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
always pass a DN to the underlying database (ITS#6070)
[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 "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         if ( op->o_abandon ) {
539                 rs->sr_err = SLAPD_ABANDON;
540         } else {
541                 op->o_callback = cb.sc_next;
542                 rs->sr_err = gs.err;
543                 rs->sr_matched = gs.matched;
544                 rs->sr_ref = gs.refs;
545                 rs->sr_ctrls = gs.ctrls;
546
547                 send_ldap_result( op, rs );
548         }
549
550         op->o_bd = b0;
551         op->o_bd->bd_info = bi0;
552         if (gs.matched)
553                 free (gs.matched);
554         if (gs.refs)
555                 ber_bvarray_free(gs.refs);
556         if (gs.ctrls) {
557                 for (i = gs.nctrls; --i >= 0; ) {
558                         if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
559                                 free(gs.ctrls[i]->ldctl_value.bv_val);
560                         free(gs.ctrls[i]);
561                 }
562                 free(gs.ctrls);
563         }
564         return rs->sr_err;
565 }
566
567 static BackendDB toolDB;
568
569 static int
570 glue_tool_entry_open (
571         BackendDB *b0,
572         int mode
573 )
574 {
575         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
576
577         /* We don't know which backend to talk to yet, so just
578          * remember the mode and move on...
579          */
580
581         glueMode = mode;
582         glueBack = NULL;
583         toolDB = *b0;
584         toolDB.bd_info = oi->oi_orig;
585
586         return 0;
587 }
588
589 static int
590 glue_tool_entry_close (
591         BackendDB *b0
592 )
593 {
594         int rc = 0;
595
596         if (glueBack && glueBack != GLUEBACK_DONE) {
597                 if (!glueBack->be_entry_close)
598                         return 0;
599                 rc = glueBack->be_entry_close (glueBack);
600         }
601         return rc;
602 }
603
604 static slap_overinst *
605 glue_tool_inst(
606         BackendInfo *bi
607 )
608 {
609         slap_overinfo   *oi = (slap_overinfo *)bi;
610         slap_overinst   *on;
611
612         for ( on = oi->oi_list; on; on=on->on_next ) {
613                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
614                         return on;
615         }
616         return NULL;
617 }
618
619 /* This function will only be called in tool mode */
620 static int
621 glue_open (
622         BackendInfo *bi
623 )
624 {
625         slap_overinst *on = glue_tool_inst( bi );
626         glueinfo                *gi = on->on_bi.bi_private;
627         static int glueOpened = 0;
628         int i, j, same, bsame = 0, rc = 0;
629         ConfigReply cr = {0};
630
631         if (glueOpened) return 0;
632
633         glueOpened = 1;
634
635         /* If we were invoked in tool mode, open all the underlying backends */
636         if (slapMode & SLAP_TOOL_MODE) {
637                 for (i = 0; i<gi->gi_nodes; i++) {
638                         same = 0;
639                         /* Same bi_open as our main backend? */
640                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
641                                 on->on_info->oi_orig->bi_open )
642                                 bsame = 1;
643
644                         /* Loop thru the bd_info's and make sure we only
645                          * invoke their bi_open functions once each.
646                          */
647                         for ( j = 0; j<i; j++ ) {
648                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
649                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
650                                         same = 1;
651                                         break;
652                                 }
653                         }
654                         /* OK, it's unique and non-NULL, call it. */
655                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
656                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
657                                         gi->gi_n[i].gn_be->bd_info );
658                         /* Let backend.c take care of the rest of startup */
659                         if ( !rc )
660                                 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
661                         if ( rc ) break;
662                 }
663                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
664                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
665
666         } /* other case is impossible */
667         return rc;
668 }
669
670 /* This function will only be called in tool mode */
671 static int
672 glue_close (
673         BackendInfo *bi
674 )
675 {
676         static int glueClosed = 0;
677         int rc = 0;
678
679         if (glueClosed) return 0;
680
681         glueClosed = 1;
682
683         if (slapMode & SLAP_TOOL_MODE) {
684                 rc = backend_shutdown( NULL );
685         }
686         return rc;
687 }
688
689 static int
690 glue_entry_get_rw (
691         Operation               *op,
692         struct berval   *dn,
693         ObjectClass             *oc,
694         AttributeDescription    *ad,
695         int     rw,
696         Entry   **e )
697 {
698         int rc;
699         BackendDB *b0 = op->o_bd;
700         op->o_bd = glue_back_select( b0, dn );
701
702         if ( op->o_bd->be_fetch ) {
703                 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
704         } else {
705                 rc = LDAP_UNWILLING_TO_PERFORM;
706         }
707         op->o_bd =b0;
708         return rc;
709 }
710
711 static int
712 glue_entry_release_rw (
713         Operation *op,
714         Entry *e,
715         int rw
716 )
717 {
718         BackendDB *b0 = op->o_bd;
719         int rc = -1;
720
721         op->o_bd = glue_back_select (b0, &e->e_nname);
722
723         if ( op->o_bd->be_release ) {
724                 rc = op->o_bd->be_release( op, e, rw );
725
726         } else {
727                 /* FIXME: mimic be_entry_release_rw
728                  * when no be_release() available */
729                 /* free entry */
730                 entry_free( e );
731                 rc = 0;
732         }
733         op->o_bd = b0;
734         return rc;
735 }
736
737 static ID
738 glue_tool_entry_first (
739         BackendDB *b0
740 )
741 {
742         slap_overinst   *on = glue_tool_inst( b0->bd_info );
743         glueinfo                *gi = on->on_bi.bi_private;
744         int i;
745         ID rc;
746
747         /* If we're starting from scratch, start at the most general */
748         if (!glueBack) {
749                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
750                         glueBack = &toolDB;
751                 } else {
752                         for (i = gi->gi_nodes-1; i >= 0; i--) {
753                                 if (gi->gi_n[i].gn_be->be_entry_open &&
754                                         gi->gi_n[i].gn_be->be_entry_first) {
755                                                 glueBack = gi->gi_n[i].gn_be;
756                                         break;
757                                 }
758                         }
759                 }
760         }
761         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
762                 glueBack->be_entry_open (glueBack, glueMode) != 0)
763                 return NOID;
764
765         rc = glueBack->be_entry_first (glueBack);
766         while ( rc == NOID ) {
767                 if ( glueBack && glueBack->be_entry_close )
768                         glueBack->be_entry_close (glueBack);
769                 for (i=0; i<gi->gi_nodes; i++) {
770                         if (gi->gi_n[i].gn_be == glueBack)
771                                 break;
772                 }
773                 if (i == 0) {
774                         glueBack = GLUEBACK_DONE;
775                         break;
776                 } else {
777                         glueBack = gi->gi_n[i-1].gn_be;
778                         rc = glue_tool_entry_first (b0);
779                         if ( glueBack == GLUEBACK_DONE ) {
780                                 break;
781                         }
782                 }
783         }
784         return rc;
785 }
786
787 static ID
788 glue_tool_entry_next (
789         BackendDB *b0
790 )
791 {
792         slap_overinst   *on = glue_tool_inst( b0->bd_info );
793         glueinfo                *gi = on->on_bi.bi_private;
794         int i;
795         ID rc;
796
797         if (!glueBack || !glueBack->be_entry_next)
798                 return NOID;
799
800         rc = glueBack->be_entry_next (glueBack);
801
802         /* If we ran out of entries in one database, move on to the next */
803         while (rc == NOID) {
804                 if ( glueBack && glueBack->be_entry_close )
805                         glueBack->be_entry_close (glueBack);
806                 for (i=0; i<gi->gi_nodes; i++) {
807                         if (gi->gi_n[i].gn_be == glueBack)
808                                 break;
809                 }
810                 if (i == 0) {
811                         glueBack = GLUEBACK_DONE;
812                         break;
813                 } else {
814                         glueBack = gi->gi_n[i-1].gn_be;
815                         rc = glue_tool_entry_first (b0);
816                         if ( glueBack == GLUEBACK_DONE ) {
817                                 break;
818                         }
819                 }
820         }
821         return rc;
822 }
823
824 static ID
825 glue_tool_dn2id_get (
826         BackendDB *b0,
827         struct berval *dn
828 )
829 {
830         BackendDB *be, b2;
831         int rc = -1;
832
833         b2 = *b0;
834         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
835         be = glue_back_select (&b2, dn);
836         if ( be == &b2 ) be = &toolDB;
837
838         if (!be->be_dn2id_get)
839                 return NOID;
840
841         if (!glueBack) {
842                 if ( be->be_entry_open ) {
843                         rc = be->be_entry_open (be, glueMode);
844                 }
845                 if (rc != 0) {
846                         return NOID;
847                 }
848         } else if (be != glueBack) {
849                 /* If this entry belongs in a different branch than the
850                  * previous one, close the current database and open the
851                  * new one.
852                  */
853                 if ( glueBack->be_entry_close ) {
854                         glueBack->be_entry_close (glueBack);
855                 }
856                 if ( be->be_entry_open ) {
857                         rc = be->be_entry_open (be, glueMode);
858                 }
859                 if (rc != 0) {
860                         return NOID;
861                 }
862         }
863         glueBack = be;
864         return be->be_dn2id_get (be, dn);
865 }
866
867 static Entry *
868 glue_tool_entry_get (
869         BackendDB *b0,
870         ID id
871 )
872 {
873         if (!glueBack || !glueBack->be_entry_get)
874                 return NULL;
875
876         return glueBack->be_entry_get (glueBack, id);
877 }
878
879 static ID
880 glue_tool_entry_put (
881         BackendDB *b0,
882         Entry *e,
883         struct berval *text
884 )
885 {
886         BackendDB *be, b2;
887         int rc = -1;
888
889         b2 = *b0;
890         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
891         be = glue_back_select (&b2, &e->e_nname);
892         if ( be == &b2 ) be = &toolDB;
893
894         if (!be->be_entry_put)
895                 return NOID;
896
897         if (!glueBack) {
898                 if ( be->be_entry_open ) {
899                         rc = be->be_entry_open (be, glueMode);
900                 }
901                 if (rc != 0) {
902                         return NOID;
903                 }
904         } else if (be != glueBack) {
905                 /* If this entry belongs in a different branch than the
906                  * previous one, close the current database and open the
907                  * new one.
908                  */
909                 if ( glueBack->be_entry_close ) {
910                         glueBack->be_entry_close (glueBack);
911                 }
912                 if ( be->be_entry_open ) {
913                         rc = be->be_entry_open (be, glueMode);
914                 }
915                 if (rc != 0) {
916                         return NOID;
917                 }
918         }
919         glueBack = be;
920         return be->be_entry_put (be, e, text);
921 }
922
923 static ID
924 glue_tool_entry_modify (
925         BackendDB *b0,
926         Entry *e,
927         struct berval *text
928 )
929 {
930         if (!glueBack || !glueBack->be_entry_modify)
931                 return NOID;
932
933         return glueBack->be_entry_modify (glueBack, e, text);
934 }
935
936 static int
937 glue_tool_entry_reindex (
938         BackendDB *b0,
939         ID id,
940         AttributeDescription **adv
941 )
942 {
943         if (!glueBack || !glueBack->be_entry_reindex)
944                 return -1;
945
946         return glueBack->be_entry_reindex (glueBack, id, adv);
947 }
948
949 static int
950 glue_tool_sync (
951         BackendDB *b0
952 )
953 {
954         slap_overinst   *on = glue_tool_inst( b0->bd_info );
955         glueinfo                *gi = on->on_bi.bi_private;
956         BackendInfo             *bi = b0->bd_info;
957         int i;
958
959         /* just sync everyone */
960         for (i = 0; i<gi->gi_nodes; i++)
961                 if (gi->gi_n[i].gn_be->be_sync)
962                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
963         b0->bd_info = on->on_info->oi_orig;
964         if ( b0->be_sync )
965                 b0->be_sync( b0 );
966         b0->bd_info = bi;
967         return 0;
968 }
969
970 typedef struct glue_Addrec {
971         struct glue_Addrec *ga_next;
972         BackendDB *ga_be;
973 } glue_Addrec;
974
975 /* List of added subordinates */
976 static glue_Addrec *ga_list;
977 static int ga_adding;
978
979 static int
980 glue_db_init(
981         BackendDB *be,
982         ConfigReply *cr
983 )
984 {
985         slap_overinst   *on = (slap_overinst *)be->bd_info;
986         slap_overinfo   *oi = on->on_info;
987         BackendInfo     *bi = oi->oi_orig;
988         glueinfo *gi;
989
990         if ( SLAP_GLUE_SUBORDINATE( be )) {
991                 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
992                         "cannot have glue overlay!\n",
993                         be->be_suffix[0].bv_val, 0, 0 );
994                 return LDAP_OTHER;
995         }
996
997         gi = ch_calloc( 1, sizeof(glueinfo));
998         on->on_bi.bi_private = gi;
999         dnParent( be->be_nsuffix, &gi->gi_pdn );
1000
1001         /* Currently the overlay framework doesn't handle these entry points
1002          * but we need them....
1003          */
1004         oi->oi_bi.bi_open = glue_open;
1005         oi->oi_bi.bi_close = glue_close;
1006
1007         /* Only advertise these if the root DB supports them */
1008         if ( bi->bi_tool_entry_open )
1009                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1010         if ( bi->bi_tool_entry_close )
1011                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1012         if ( bi->bi_tool_entry_first )
1013                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1014         if ( bi->bi_tool_entry_next )
1015                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1016         if ( bi->bi_tool_entry_get )
1017                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1018         if ( bi->bi_tool_dn2id_get )
1019                 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1020         if ( bi->bi_tool_entry_put )
1021                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1022         if ( bi->bi_tool_entry_reindex )
1023                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1024         if ( bi->bi_tool_entry_modify )
1025                 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1026         if ( bi->bi_tool_sync )
1027                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1028
1029         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1030
1031         if ( ga_list ) {
1032                 be->bd_info = (BackendInfo *)oi;
1033                 glue_sub_attach( 1 );
1034         }
1035
1036         return 0;
1037 }
1038
1039 static int
1040 glue_db_destroy (
1041         BackendDB *be,
1042         ConfigReply *cr
1043 )
1044 {
1045         slap_overinst   *on = (slap_overinst *)be->bd_info;
1046         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
1047
1048         free (gi);
1049         return SLAP_CB_CONTINUE;
1050 }
1051
1052 static int
1053 glue_db_close( 
1054         BackendDB *be,
1055         ConfigReply *cr
1056 )
1057 {
1058         slap_overinst   *on = (slap_overinst *)be->bd_info;
1059
1060         on->on_info->oi_bi.bi_db_close = 0;
1061         return 0;
1062 }
1063
1064 int
1065 glue_sub_del( BackendDB *b0 )
1066 {
1067         BackendDB *be;
1068         int rc = 0;
1069
1070         /* Find the top backend for this subordinate */
1071         be = b0;
1072         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1073                 slap_overinfo *oi;
1074                 slap_overinst *on;
1075                 glueinfo *gi;
1076                 int i;
1077
1078                 if ( SLAP_GLUE_SUBORDINATE( be ))
1079                         continue;
1080                 if ( !SLAP_GLUE_INSTANCE( be ))
1081                         continue;
1082                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1083                         continue;
1084
1085                 /* OK, got the right backend, find the overlay */
1086                 oi = (slap_overinfo *)be->bd_info;
1087                 for ( on=oi->oi_list; on; on=on->on_next ) {
1088                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1089                                 break;
1090                 }
1091                 assert( on != NULL );
1092                 gi = on->on_bi.bi_private;
1093                 for ( i=0; i < gi->gi_nodes; i++ ) {
1094                         if ( gi->gi_n[i].gn_be == b0 ) {
1095                                 int j;
1096
1097                                 for (j=i+1; j < gi->gi_nodes; j++)
1098                                         gi->gi_n[j-1] = gi->gi_n[j];
1099
1100                                 gi->gi_nodes--;
1101                         }
1102                 }
1103         }
1104         if ( be == NULL )
1105                 rc = LDAP_NO_SUCH_OBJECT;
1106
1107         return rc;
1108 }
1109
1110
1111 /* Attach all the subordinate backends to their superior */
1112 int
1113 glue_sub_attach( int online )
1114 {
1115         glue_Addrec *ga, *gnext = NULL;
1116         int rc = 0;
1117
1118         if ( ga_adding )
1119                 return 0;
1120
1121         ga_adding = 1;
1122
1123         /* For all the subordinate backends */
1124         for ( ga=ga_list; ga != NULL; ga = gnext ) {
1125                 BackendDB *be;
1126
1127                 gnext = ga->ga_next;
1128
1129                 /* Find the top backend for this subordinate */
1130                 be = ga->ga_be;
1131                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1132                         slap_overinfo *oi;
1133                         slap_overinst *on;
1134                         glueinfo *gi;
1135
1136                         if ( SLAP_GLUE_SUBORDINATE( be ))
1137                                 continue;
1138                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1139                                 continue;
1140
1141                         /* If it's not already configured, set up the overlay */
1142                         if ( !SLAP_GLUE_INSTANCE( be )) {
1143                                 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1144                                 if ( rc )
1145                                         break;
1146                         }
1147                         /* Find the overlay instance */
1148                         oi = (slap_overinfo *)be->bd_info;
1149                         for ( on=oi->oi_list; on; on=on->on_next ) {
1150                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1151                                         break;
1152                         }
1153                         assert( on != NULL );
1154                         gi = on->on_bi.bi_private;
1155                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1156                                 gi->gi_nodes * sizeof(gluenode));
1157                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1158                         dnParent( &ga->ga_be->be_nsuffix[0],
1159                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
1160                         gi->gi_nodes++;
1161                         on->on_bi.bi_private = gi;
1162                         ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1163                         break;
1164                 }
1165                 if ( !be ) {
1166                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1167                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1168                         /* allow this for now, assume a superior will
1169                          * be added later
1170                          */
1171                         if ( online ) {
1172                                 rc = 0;
1173                                 gnext = ga_list;
1174                                 break;
1175                         }
1176                         rc = LDAP_NO_SUCH_OBJECT;
1177                 }
1178                 ch_free( ga );
1179                 if ( rc ) break;
1180         }
1181
1182         ga_list = gnext;
1183
1184         ga_adding = 0;
1185
1186         return rc;
1187 }
1188
1189 int
1190 glue_sub_add( BackendDB *be, int advert, int online )
1191 {
1192         glue_Addrec *ga;
1193         int rc = 0;
1194
1195         if ( overlay_is_inst( be, "glue" )) {
1196                 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1197                         "cannot be a subordinate!\n",
1198                         be->be_suffix[0].bv_val, 0, 0 );
1199                 return LDAP_OTHER;
1200         }
1201         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1202         if ( advert )
1203                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1204
1205         ga = ch_malloc( sizeof( glue_Addrec ));
1206         ga->ga_next = ga_list;
1207         ga->ga_be = be;
1208         ga_list = ga;
1209
1210         if ( online )
1211                 rc = glue_sub_attach( online );
1212
1213         return rc;
1214 }
1215
1216 int
1217 glue_sub_init()
1218 {
1219         glue.on_bi.bi_type = "glue";
1220
1221         glue.on_bi.bi_db_init = glue_db_init;
1222         glue.on_bi.bi_db_close = glue_db_close;
1223         glue.on_bi.bi_db_destroy = glue_db_destroy;
1224
1225         glue.on_bi.bi_op_search = glue_op_search;
1226         glue.on_bi.bi_op_modify = glue_op_func;
1227         glue.on_bi.bi_op_modrdn = glue_op_func;
1228         glue.on_bi.bi_op_add = glue_op_func;
1229         glue.on_bi.bi_op_delete = glue_op_func;
1230         glue.on_bi.bi_extended = glue_op_func;
1231
1232         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1233         glue.on_bi.bi_chk_controls = glue_chk_controls;
1234         glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1235         glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1236
1237         glue.on_response = glue_response;
1238
1239         return overlay_register( &glue );
1240 }