]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
ITS#6468: Implement bi_access_allowed.
[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
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 struct berval *glue_base;
739 static int glue_scope;
740 static Filter *glue_filter;
741
742 static ID
743 glue_tool_entry_first (
744         BackendDB *b0
745 )
746 {
747         slap_overinst   *on = glue_tool_inst( b0->bd_info );
748         glueinfo                *gi = on->on_bi.bi_private;
749         int i;
750         ID rc;
751
752         /* If we're starting from scratch, start at the most general */
753         if (!glueBack) {
754                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
755                         glueBack = &toolDB;
756                 } else {
757                         for (i = gi->gi_nodes-1; i >= 0; i--) {
758                                 if (gi->gi_n[i].gn_be->be_entry_open &&
759                                         gi->gi_n[i].gn_be->be_entry_first) {
760                                                 glueBack = gi->gi_n[i].gn_be;
761                                         break;
762                                 }
763                         }
764                 }
765         }
766         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
767                 glueBack->be_entry_open (glueBack, glueMode) != 0)
768                 return NOID;
769
770         rc = glueBack->be_entry_first (glueBack);
771         while ( rc == NOID ) {
772                 if ( glueBack && glueBack->be_entry_close )
773                         glueBack->be_entry_close (glueBack);
774                 for (i=0; i<gi->gi_nodes; i++) {
775                         if (gi->gi_n[i].gn_be == glueBack)
776                                 break;
777                 }
778                 if (i == 0) {
779                         glueBack = GLUEBACK_DONE;
780                         break;
781                 } else {
782                         glueBack = gi->gi_n[i-1].gn_be;
783                         rc = glue_tool_entry_first (b0);
784                         if ( glueBack == GLUEBACK_DONE ) {
785                                 break;
786                         }
787                 }
788         }
789         return rc;
790 }
791
792 static ID
793 glue_tool_entry_first_x (
794         BackendDB *b0,
795         struct berval *base,
796         int scope,
797         Filter *f
798 )
799 {
800         slap_overinst   *on = glue_tool_inst( b0->bd_info );
801         glueinfo                *gi = on->on_bi.bi_private;
802         int i;
803         ID rc;
804
805         glue_base = base;
806         glue_scope = scope;
807         glue_filter = f;
808
809         /* If we're starting from scratch, start at the most general */
810         if (!glueBack) {
811                 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
812                         glueBack = &toolDB;
813                 } else {
814                         for (i = gi->gi_nodes-1; i >= 0; i--) {
815                                 if (gi->gi_n[i].gn_be->be_entry_open &&
816                                         gi->gi_n[i].gn_be->be_entry_first_x)
817                                 {
818                                         glueBack = gi->gi_n[i].gn_be;
819                                         break;
820                                 }
821                         }
822                 }
823         }
824         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
825                 glueBack->be_entry_open (glueBack, glueMode) != 0)
826                 return NOID;
827
828         rc = glueBack->be_entry_first_x (glueBack,
829                 glue_base, glue_scope, glue_filter);
830         while ( rc == NOID ) {
831                 if ( glueBack && glueBack->be_entry_close )
832                         glueBack->be_entry_close (glueBack);
833                 for (i=0; i<gi->gi_nodes; i++) {
834                         if (gi->gi_n[i].gn_be == glueBack)
835                                 break;
836                 }
837                 if (i == 0) {
838                         glueBack = GLUEBACK_DONE;
839                         break;
840                 } else {
841                         glueBack = gi->gi_n[i-1].gn_be;
842                         rc = glue_tool_entry_first_x (b0,
843                                 glue_base, glue_scope, glue_filter);
844                         if ( glueBack == GLUEBACK_DONE ) {
845                                 break;
846                         }
847                 }
848         }
849         return rc;
850 }
851
852 static ID
853 glue_tool_entry_next (
854         BackendDB *b0
855 )
856 {
857         slap_overinst   *on = glue_tool_inst( b0->bd_info );
858         glueinfo                *gi = on->on_bi.bi_private;
859         int i;
860         ID rc;
861
862         if (!glueBack || !glueBack->be_entry_next)
863                 return NOID;
864
865         rc = glueBack->be_entry_next (glueBack);
866
867         /* If we ran out of entries in one database, move on to the next */
868         while (rc == NOID) {
869                 if ( glueBack && glueBack->be_entry_close )
870                         glueBack->be_entry_close (glueBack);
871                 for (i=0; i<gi->gi_nodes; i++) {
872                         if (gi->gi_n[i].gn_be == glueBack)
873                                 break;
874                 }
875                 if (i == 0) {
876                         glueBack = GLUEBACK_DONE;
877                         break;
878                 } else {
879                         glueBack = gi->gi_n[i-1].gn_be;
880                         if ( glue_base || glue_filter ) {
881                                 /* using entry_first_x() */
882                                 rc = glue_tool_entry_first_x (b0,
883                                         glue_base, glue_scope, glue_filter);
884
885                         } else {
886                                 /* using entry_first() */
887                                 rc = glue_tool_entry_first (b0);
888                         }
889                         if ( glueBack == GLUEBACK_DONE ) {
890                                 break;
891                         }
892                 }
893         }
894         return rc;
895 }
896
897 static ID
898 glue_tool_dn2id_get (
899         BackendDB *b0,
900         struct berval *dn
901 )
902 {
903         BackendDB *be, b2;
904         int rc = -1;
905
906         b2 = *b0;
907         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
908         be = glue_back_select (&b2, dn);
909         if ( be == &b2 ) be = &toolDB;
910
911         if (!be->be_dn2id_get)
912                 return NOID;
913
914         if (!glueBack) {
915                 if ( be->be_entry_open ) {
916                         rc = be->be_entry_open (be, glueMode);
917                 }
918                 if (rc != 0) {
919                         return NOID;
920                 }
921         } else if (be != glueBack) {
922                 /* If this entry belongs in a different branch than the
923                  * previous one, close the current database and open the
924                  * new one.
925                  */
926                 if ( glueBack->be_entry_close ) {
927                         glueBack->be_entry_close (glueBack);
928                 }
929                 if ( be->be_entry_open ) {
930                         rc = be->be_entry_open (be, glueMode);
931                 }
932                 if (rc != 0) {
933                         return NOID;
934                 }
935         }
936         glueBack = be;
937         return be->be_dn2id_get (be, dn);
938 }
939
940 static Entry *
941 glue_tool_entry_get (
942         BackendDB *b0,
943         ID id
944 )
945 {
946         if (!glueBack || !glueBack->be_entry_get)
947                 return NULL;
948
949         return glueBack->be_entry_get (glueBack, id);
950 }
951
952 static ID
953 glue_tool_entry_put (
954         BackendDB *b0,
955         Entry *e,
956         struct berval *text
957 )
958 {
959         BackendDB *be, b2;
960         int rc = -1;
961
962         b2 = *b0;
963         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
964         be = glue_back_select (&b2, &e->e_nname);
965         if ( be == &b2 ) be = &toolDB;
966
967         if (!be->be_entry_put)
968                 return NOID;
969
970         if (!glueBack) {
971                 if ( be->be_entry_open ) {
972                         rc = be->be_entry_open (be, glueMode);
973                 }
974                 if (rc != 0) {
975                         return NOID;
976                 }
977         } else if (be != glueBack) {
978                 /* If this entry belongs in a different branch than the
979                  * previous one, close the current database and open the
980                  * new one.
981                  */
982                 if ( glueBack->be_entry_close ) {
983                         glueBack->be_entry_close (glueBack);
984                 }
985                 if ( be->be_entry_open ) {
986                         rc = be->be_entry_open (be, glueMode);
987                 }
988                 if (rc != 0) {
989                         return NOID;
990                 }
991         }
992         glueBack = be;
993         return be->be_entry_put (be, e, text);
994 }
995
996 static ID
997 glue_tool_entry_modify (
998         BackendDB *b0,
999         Entry *e,
1000         struct berval *text
1001 )
1002 {
1003         if (!glueBack || !glueBack->be_entry_modify)
1004                 return NOID;
1005
1006         return glueBack->be_entry_modify (glueBack, e, text);
1007 }
1008
1009 static int
1010 glue_tool_entry_reindex (
1011         BackendDB *b0,
1012         ID id,
1013         AttributeDescription **adv
1014 )
1015 {
1016         if (!glueBack || !glueBack->be_entry_reindex)
1017                 return -1;
1018
1019         return glueBack->be_entry_reindex (glueBack, id, adv);
1020 }
1021
1022 static int
1023 glue_tool_sync (
1024         BackendDB *b0
1025 )
1026 {
1027         slap_overinst   *on = glue_tool_inst( b0->bd_info );
1028         glueinfo                *gi = on->on_bi.bi_private;
1029         BackendInfo             *bi = b0->bd_info;
1030         int i;
1031
1032         /* just sync everyone */
1033         for (i = 0; i<gi->gi_nodes; i++)
1034                 if (gi->gi_n[i].gn_be->be_sync)
1035                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1036         b0->bd_info = on->on_info->oi_orig;
1037         if ( b0->be_sync )
1038                 b0->be_sync( b0 );
1039         b0->bd_info = bi;
1040         return 0;
1041 }
1042
1043 typedef struct glue_Addrec {
1044         struct glue_Addrec *ga_next;
1045         BackendDB *ga_be;
1046 } glue_Addrec;
1047
1048 /* List of added subordinates */
1049 static glue_Addrec *ga_list;
1050 static int ga_adding;
1051
1052 static int
1053 glue_db_init(
1054         BackendDB *be,
1055         ConfigReply *cr
1056 )
1057 {
1058         slap_overinst   *on = (slap_overinst *)be->bd_info;
1059         slap_overinfo   *oi = on->on_info;
1060         BackendInfo     *bi = oi->oi_orig;
1061         glueinfo *gi;
1062
1063         if ( SLAP_GLUE_SUBORDINATE( be )) {
1064                 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1065                         "cannot have glue overlay!\n",
1066                         be->be_suffix[0].bv_val, 0, 0 );
1067                 return LDAP_OTHER;
1068         }
1069
1070         gi = ch_calloc( 1, sizeof(glueinfo));
1071         on->on_bi.bi_private = gi;
1072         dnParent( be->be_nsuffix, &gi->gi_pdn );
1073
1074         /* Currently the overlay framework doesn't handle these entry points
1075          * but we need them....
1076          */
1077         oi->oi_bi.bi_open = glue_open;
1078         oi->oi_bi.bi_close = glue_close;
1079
1080         /* Only advertise these if the root DB supports them */
1081         if ( bi->bi_tool_entry_open )
1082                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1083         if ( bi->bi_tool_entry_close )
1084                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1085         if ( bi->bi_tool_entry_first )
1086                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1087         /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1088         if ( bi->bi_tool_entry_first_x )
1089                 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1090         if ( bi->bi_tool_entry_next )
1091                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1092         if ( bi->bi_tool_entry_get )
1093                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1094         if ( bi->bi_tool_dn2id_get )
1095                 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1096         if ( bi->bi_tool_entry_put )
1097                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1098         if ( bi->bi_tool_entry_reindex )
1099                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1100         if ( bi->bi_tool_entry_modify )
1101                 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1102         if ( bi->bi_tool_sync )
1103                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1104
1105         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1106
1107         if ( ga_list ) {
1108                 be->bd_info = (BackendInfo *)oi;
1109                 glue_sub_attach( 1 );
1110         }
1111
1112         return 0;
1113 }
1114
1115 static int
1116 glue_db_destroy (
1117         BackendDB *be,
1118         ConfigReply *cr
1119 )
1120 {
1121         slap_overinst   *on = (slap_overinst *)be->bd_info;
1122         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
1123
1124         free (gi);
1125         return SLAP_CB_CONTINUE;
1126 }
1127
1128 static int
1129 glue_db_close( 
1130         BackendDB *be,
1131         ConfigReply *cr
1132 )
1133 {
1134         slap_overinst   *on = (slap_overinst *)be->bd_info;
1135
1136         on->on_info->oi_bi.bi_db_close = 0;
1137         return 0;
1138 }
1139
1140 int
1141 glue_sub_del( BackendDB *b0 )
1142 {
1143         BackendDB *be;
1144         int rc = 0;
1145
1146         /* Find the top backend for this subordinate */
1147         be = b0;
1148         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1149                 slap_overinfo *oi;
1150                 slap_overinst *on;
1151                 glueinfo *gi;
1152                 int i;
1153
1154                 if ( SLAP_GLUE_SUBORDINATE( be ))
1155                         continue;
1156                 if ( !SLAP_GLUE_INSTANCE( be ))
1157                         continue;
1158                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1159                         continue;
1160
1161                 /* OK, got the right backend, find the overlay */
1162                 oi = (slap_overinfo *)be->bd_info;
1163                 for ( on=oi->oi_list; on; on=on->on_next ) {
1164                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1165                                 break;
1166                 }
1167                 assert( on != NULL );
1168                 gi = on->on_bi.bi_private;
1169                 for ( i=0; i < gi->gi_nodes; i++ ) {
1170                         if ( gi->gi_n[i].gn_be == b0 ) {
1171                                 int j;
1172
1173                                 for (j=i+1; j < gi->gi_nodes; j++)
1174                                         gi->gi_n[j-1] = gi->gi_n[j];
1175
1176                                 gi->gi_nodes--;
1177                         }
1178                 }
1179         }
1180         if ( be == NULL )
1181                 rc = LDAP_NO_SUCH_OBJECT;
1182
1183         return rc;
1184 }
1185
1186
1187 /* Attach all the subordinate backends to their superior */
1188 int
1189 glue_sub_attach( int online )
1190 {
1191         glue_Addrec *ga, *gnext = NULL;
1192         int rc = 0;
1193
1194         if ( ga_adding )
1195                 return 0;
1196
1197         ga_adding = 1;
1198
1199         /* For all the subordinate backends */
1200         for ( ga=ga_list; ga != NULL; ga = gnext ) {
1201                 BackendDB *be;
1202
1203                 gnext = ga->ga_next;
1204
1205                 /* Find the top backend for this subordinate */
1206                 be = ga->ga_be;
1207                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1208                         slap_overinfo *oi;
1209                         slap_overinst *on;
1210                         glueinfo *gi;
1211
1212                         if ( SLAP_GLUE_SUBORDINATE( be ))
1213                                 continue;
1214                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1215                                 continue;
1216
1217                         /* If it's not already configured, set up the overlay */
1218                         if ( !SLAP_GLUE_INSTANCE( be )) {
1219                                 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1220                                 if ( rc )
1221                                         break;
1222                         }
1223                         /* Find the overlay instance */
1224                         oi = (slap_overinfo *)be->bd_info;
1225                         for ( on=oi->oi_list; on; on=on->on_next ) {
1226                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1227                                         break;
1228                         }
1229                         assert( on != NULL );
1230                         gi = on->on_bi.bi_private;
1231                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1232                                 gi->gi_nodes * sizeof(gluenode));
1233                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1234                         dnParent( &ga->ga_be->be_nsuffix[0],
1235                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
1236                         gi->gi_nodes++;
1237                         on->on_bi.bi_private = gi;
1238                         ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1239                         break;
1240                 }
1241                 if ( !be ) {
1242                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1243                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1244                         /* allow this for now, assume a superior will
1245                          * be added later
1246                          */
1247                         if ( online ) {
1248                                 rc = 0;
1249                                 gnext = ga_list;
1250                                 break;
1251                         }
1252                         rc = LDAP_NO_SUCH_OBJECT;
1253                 }
1254                 ch_free( ga );
1255                 if ( rc ) break;
1256         }
1257
1258         ga_list = gnext;
1259
1260         ga_adding = 0;
1261
1262         return rc;
1263 }
1264
1265 int
1266 glue_sub_add( BackendDB *be, int advert, int online )
1267 {
1268         glue_Addrec *ga;
1269         int rc = 0;
1270
1271         if ( overlay_is_inst( be, "glue" )) {
1272                 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1273                         "cannot be a subordinate!\n",
1274                         be->be_suffix[0].bv_val, 0, 0 );
1275                 return LDAP_OTHER;
1276         }
1277         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1278         if ( advert )
1279                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1280
1281         ga = ch_malloc( sizeof( glue_Addrec ));
1282         ga->ga_next = ga_list;
1283         ga->ga_be = be;
1284         ga_list = ga;
1285
1286         if ( online )
1287                 rc = glue_sub_attach( online );
1288
1289         return rc;
1290 }
1291
1292 static int
1293 glue_access_allowed(
1294         Operation               *op,
1295         Entry                   *e,
1296         AttributeDescription    *desc,
1297         struct berval           *val,
1298         slap_access_t           access,
1299         AccessControlState      *state,
1300         slap_mask_t             *maskp )
1301 {
1302         BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
1303         int rc;
1304
1305         if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
1306                 return SLAP_CB_CONTINUE;
1307
1308         b0 = op->o_bd;
1309         op->o_bd = be;
1310         rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
1311         op->o_bd = b0;
1312         return rc;
1313 }
1314
1315 int
1316 glue_sub_init()
1317 {
1318         glue.on_bi.bi_type = "glue";
1319
1320         glue.on_bi.bi_db_init = glue_db_init;
1321         glue.on_bi.bi_db_close = glue_db_close;
1322         glue.on_bi.bi_db_destroy = glue_db_destroy;
1323
1324         glue.on_bi.bi_op_search = glue_op_search;
1325         glue.on_bi.bi_op_modify = glue_op_func;
1326         glue.on_bi.bi_op_modrdn = glue_op_func;
1327         glue.on_bi.bi_op_add = glue_op_func;
1328         glue.on_bi.bi_op_delete = glue_op_func;
1329         glue.on_bi.bi_extended = glue_op_func;
1330
1331         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1332         glue.on_bi.bi_chk_controls = glue_chk_controls;
1333         glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1334         glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1335         glue.on_bi.bi_access_allowed = glue_access_allowed;
1336
1337         glue.on_response = glue_response;
1338
1339         return overlay_register( &glue );
1340 }