]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
glue/rwm fixes from HEAD
[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-2007 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
39 typedef struct gluenode {
40         BackendDB *gn_be;
41         struct berval gn_pdn;
42 } gluenode;
43
44 typedef struct glueinfo {
45         int gi_nodes;
46         struct berval gi_pdn;
47         gluenode gi_n[1];
48 } glueinfo;
49
50 static slap_overinst    glue;
51
52 static int glueMode;
53 static BackendDB *glueBack;
54
55 static slap_response glue_op_response;
56
57 /* Just like select_backend, but only for our backends */
58 static BackendDB *
59 glue_back_select (
60         BackendDB *be,
61         struct berval *dn
62 )
63 {
64         slap_overinst   *on = (slap_overinst *)be->bd_info;
65         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
66         int i;
67
68         for (i = gi->gi_nodes-1; i >= 0; i--) {
69                 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
70
71                 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
72                         return gi->gi_n[i].gn_be;
73                 }
74         }
75         be->bd_info = on->on_info->oi_orig;
76         return be;
77 }
78
79
80 typedef struct glue_state {
81         char *matched;
82         BerVarray refs;
83         LDAPControl **ctrls;
84         int err;
85         int matchlen;
86         int nrefs;
87         int nctrls;
88 } glue_state;
89
90 static int
91 glue_op_cleanup( Operation *op, SlapReply *rs )
92 {
93         /* This is not a final result */
94         if (rs->sr_type == REP_RESULT )
95                 rs->sr_type = REP_GLUE_RESULT;
96         return SLAP_CB_CONTINUE;
97 }
98
99 static int
100 glue_op_response ( Operation *op, SlapReply *rs )
101 {
102         glue_state *gs = op->o_callback->sc_private;
103
104         switch(rs->sr_type) {
105         case REP_SEARCH:
106         case REP_SEARCHREF:
107         case REP_INTERMEDIATE:
108                 return SLAP_CB_CONTINUE;
109
110         default:
111                 if (rs->sr_err == LDAP_SUCCESS ||
112                         rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
113                         rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
114                         rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
115                         rs->sr_err == LDAP_NO_SUCH_OBJECT ||
116                         gs->err != LDAP_SUCCESS)
117                         gs->err = rs->sr_err;
118                 if (gs->err == LDAP_SUCCESS && gs->matched) {
119                         ch_free (gs->matched);
120                         gs->matched = NULL;
121                         gs->matchlen = 0;
122                 }
123                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
124                         int len;
125                         len = strlen (rs->sr_matched);
126                         if (len > gs->matchlen) {
127                                 if (gs->matched)
128                                         ch_free (gs->matched);
129                                 gs->matched = ch_strdup (rs->sr_matched);
130                                 gs->matchlen = len;
131                         }
132                 }
133                 if (rs->sr_ref) {
134                         int i, j, k;
135                         BerVarray new;
136
137                         for (i=0; rs->sr_ref[i].bv_val; i++);
138
139                         j = gs->nrefs;
140                         if (!j) {
141                                 new = ch_malloc ((i+1)*sizeof(struct berval));
142                         } else {
143                                 new = ch_realloc(gs->refs,
144                                         (j+i+1)*sizeof(struct berval));
145                         }
146                         for (k=0; k<i; j++,k++) {
147                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
148                         }
149                         new[j].bv_val = NULL;
150                         gs->nrefs = j;
151                         gs->refs = new;
152                 }
153                 if (rs->sr_ctrls) {
154                         int i, j, k;
155                         LDAPControl **newctrls;
156
157                         for (i=0; rs->sr_ctrls[i]; i++);
158
159                         j = gs->nctrls;
160                         if (!j) {
161                                 newctrls = ch_malloc((i+1)*sizeof(LDAPControl *));
162                         } else {
163                                 newctrls = ch_realloc(gs->ctrls,
164                                         (j+i+1)*sizeof(LDAPControl *));
165                         }
166                         for (k=0; k<i; j++,k++) {
167                                 newctrls[j] = ch_malloc(sizeof(LDAPControl));
168                                 *newctrls[j] = *rs->sr_ctrls[k];
169                                 if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ))
170                                         ber_dupbv( &newctrls[j]->ldctl_value,
171                                                 &rs->sr_ctrls[k]->ldctl_value );
172                         }
173                         newctrls[j] = NULL;
174                         gs->nctrls = j;
175                         gs->ctrls = newctrls;
176                 }
177         }
178         return 0;
179 }
180
181 static int
182 glue_op_func ( Operation *op, SlapReply *rs )
183 {
184         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
185         BackendDB *b0 = op->o_bd;
186         BackendInfo *bi0 = op->o_bd->bd_info;
187         BI_op_modify **func;
188         slap_operation_t which = op_bind;
189         int rc;
190
191         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
192
193         /* If we're on the master backend, let overlay framework handle it */
194         if ( op->o_bd == b0 )
195                 return SLAP_CB_CONTINUE;
196
197         b0->bd_info = on->on_info->oi_orig;
198
199         switch(op->o_tag) {
200         case LDAP_REQ_ADD: which = op_add; break;
201         case LDAP_REQ_DELETE: which = op_delete; break;
202         case LDAP_REQ_MODIFY: which = op_modify; break;
203         case LDAP_REQ_MODRDN: which = op_modrdn; break;
204         case LDAP_REQ_EXTENDED: which = op_extended; break;
205         default: assert( 0 ); break;
206         }
207
208         func = &op->o_bd->bd_info->bi_op_bind;
209         if ( func[which] )
210                 rc = func[which]( op, rs );
211         else
212                 rc = SLAP_CB_BYPASS;
213
214         op->o_bd = b0;
215         op->o_bd->bd_info = bi0;
216         return rc;
217 }
218
219 static int
220 glue_response ( Operation *op, SlapReply *rs )
221 {
222         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
223         BackendDB *be = op->o_bd;
224         be = glue_back_select (op->o_bd, &op->o_req_ndn);
225
226         /* If we're on the master backend, let overlay framework handle it.
227          * Otherwise, bail out.
228          */
229         return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
230 }
231
232 static int
233 glue_chk_referrals ( Operation *op, SlapReply *rs )
234 {
235         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
236         BackendDB *b0 = op->o_bd;
237         BackendInfo *bi0 = op->o_bd->bd_info;
238         int rc;
239
240         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
241         if ( op->o_bd == b0 )
242                 return SLAP_CB_CONTINUE;
243
244         b0->bd_info = on->on_info->oi_orig;
245
246         if ( op->o_bd->bd_info->bi_chk_referrals )
247                 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
248         else
249                 rc = SLAP_CB_CONTINUE;
250
251         op->o_bd = b0;
252         op->o_bd->bd_info = bi0;
253         return rc;
254 }
255
256 static int
257 glue_chk_controls ( Operation *op, SlapReply *rs )
258 {
259         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
260         BackendDB *b0 = op->o_bd;
261         BackendInfo *bi0 = op->o_bd->bd_info;
262         int rc = SLAP_CB_CONTINUE;
263
264         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
265         if ( op->o_bd == b0 )
266                 return SLAP_CB_CONTINUE;
267
268         b0->bd_info = on->on_info->oi_orig;
269
270         /* if the subordinate database has overlays, the bi_chk_controls()
271          * hook is actually over_aux_chk_controls(); in case it actually
272          * wraps a missing hok, we need to mimic the behavior
273          * of the frontend applied to that database */
274         if ( op->o_bd->bd_info->bi_chk_controls ) {
275                 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
276         }
277
278         
279         if ( rc == SLAP_CB_CONTINUE ) {
280                 rc = backend_check_controls( op, rs );
281         }
282
283         op->o_bd = b0;
284         op->o_bd->bd_info = bi0;
285         return rc;
286 }
287
288 /* ITS#4615 - overlays configured above the glue overlay should be
289  * invoked for the entire glued tree. Overlays configured below the
290  * glue overlay should only be invoked on the master backend.
291  * So, if we're searching on any subordinates, we need to force the
292  * current overlay chain to stop processing, without stopping the
293  * overall callback flow.
294  */
295 static int
296 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
297         slap_overinst *on )
298 {
299         /* Process any overlays on the master backend */
300         if ( op->o_bd == b0 && on->on_next ) {
301                 BackendInfo *bi = op->o_bd->bd_info;
302                 int rc = SLAP_CB_CONTINUE;
303                 for ( on=on->on_next; on; on=on->on_next ) {
304                         op->o_bd->bd_info = (BackendInfo *)on;
305                         if ( on->on_bi.bi_op_search ) {
306                                 rc = on->on_bi.bi_op_search( op, rs );
307                                 if ( rc != SLAP_CB_CONTINUE )
308                                         break;
309                         }
310                 }
311                 op->o_bd->bd_info = bi;
312                 if ( rc != SLAP_CB_CONTINUE )
313                         return rc;
314         }
315         return op->o_bd->be_search( op, rs );
316 }
317
318 static int
319 glue_op_search ( Operation *op, SlapReply *rs )
320 {
321         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
322         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
323         BackendDB *b0 = op->o_bd;
324         BackendDB *b1 = NULL, *btmp;
325         BackendInfo *bi0 = op->o_bd->bd_info;
326         int i;
327         long stoptime = 0, starttime;
328         glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
329         slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
330         int scope0, tlimit0;
331         struct berval dn, ndn, *pdn;
332
333         cb.sc_private = &gs;
334
335         cb.sc_next = op->o_callback;
336
337         starttime = op->o_time;
338         stoptime = slap_get_time () + op->ors_tlimit;
339
340         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
341         b0->bd_info = on->on_info->oi_orig;
342
343         switch (op->ors_scope) {
344         case LDAP_SCOPE_BASE:
345                 if ( op->o_bd == b0 )
346                         return SLAP_CB_CONTINUE;
347
348                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
349                 if (op->o_bd && op->o_bd->be_search) {
350                         rs->sr_err = op->o_bd->be_search( op, rs );
351                 }
352                 return rs->sr_err;
353
354         case LDAP_SCOPE_ONELEVEL:
355         case LDAP_SCOPE_SUBTREE:
356         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
357                 op->o_callback = &cb;
358                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
359                 scope0 = op->ors_scope;
360                 tlimit0 = op->ors_tlimit;
361                 dn = op->o_req_dn;
362                 ndn = op->o_req_ndn;
363                 b1 = op->o_bd;
364
365                 /*
366                  * Execute in reverse order, most specific first 
367                  */
368                 for (i = gi->gi_nodes; i >= 0; i--) {
369                         if ( i == gi->gi_nodes ) {
370                                 btmp = b0;
371                                 pdn = &gi->gi_pdn;
372                         } else {
373                                 btmp = gi->gi_n[i].gn_be;
374                                 pdn = &gi->gi_n[i].gn_pdn;
375                         }
376                         if (!btmp || !btmp->be_search)
377                                 continue;
378                         if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
379                                 continue;
380                         if (tlimit0 != SLAP_NO_LIMIT) {
381                                 op->o_time = slap_get_time();
382                                 op->ors_tlimit = stoptime - op->o_time;
383                                 if (op->ors_tlimit <= 0) {
384                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
385                                         break;
386                                 }
387                         }
388                         rs->sr_err = 0;
389                         /*
390                          * check for abandon 
391                          */
392                         if (op->o_abandon) {
393                                 goto end_of_loop;
394                         }
395                         op->o_bd = btmp;
396
397                         assert( op->o_bd->be_suffix != NULL );
398                         assert( op->o_bd->be_nsuffix != NULL );
399                         
400                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
401                                 dn_match(pdn, &ndn))
402                         {
403                                 op->ors_scope = LDAP_SCOPE_BASE;
404                                 op->o_req_dn = op->o_bd->be_suffix[0];
405                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
406                                 rs->sr_err = op->o_bd->be_search(op, rs);
407                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
408                                         gs.err = LDAP_SUCCESS;
409                                 }
410                                 op->ors_scope = LDAP_SCOPE_ONELEVEL;
411                                 op->o_req_dn = dn;
412                                 op->o_req_ndn = ndn;
413
414                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
415                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
416                         {
417                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
418
419                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
420                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
421                         {
422                                 op->o_req_dn = op->o_bd->be_suffix[0];
423                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
424                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
425                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
426                                         gs.err = LDAP_SUCCESS;
427                                 }
428                                 op->o_req_dn = dn;
429                                 op->o_req_ndn = ndn;
430
431                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
432                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
433                         }
434
435                         switch ( gs.err ) {
436
437                         /*
438                          * Add errors that should result in dropping
439                          * the search
440                          */
441                         case LDAP_SIZELIMIT_EXCEEDED:
442                         case LDAP_TIMELIMIT_EXCEEDED:
443                         case LDAP_ADMINLIMIT_EXCEEDED:
444                         case LDAP_NO_SUCH_OBJECT:
445 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
446                         case LDAP_X_CANNOT_CHAIN:
447 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
448                                 goto end_of_loop;
449                         
450                         default:
451                                 break;
452                         }
453                 }
454 end_of_loop:;
455                 op->ors_scope = scope0;
456                 op->ors_tlimit = tlimit0;
457                 op->o_time = starttime;
458                 op->o_req_dn = dn;
459                 op->o_req_ndn = ndn;
460
461                 break;
462         }
463         if ( op->o_abandon ) {
464                 rs->sr_err = SLAPD_ABANDON;
465         } else {
466                 op->o_callback = cb.sc_next;
467                 rs->sr_err = gs.err;
468                 rs->sr_matched = gs.matched;
469                 rs->sr_ref = gs.refs;
470                 rs->sr_ctrls = gs.ctrls;
471
472                 send_ldap_result( op, rs );
473         }
474
475         op->o_bd = b0;
476         op->o_bd->bd_info = bi0;
477         if (gs.matched)
478                 free (gs.matched);
479         if (gs.refs)
480                 ber_bvarray_free(gs.refs);
481         if (gs.ctrls) {
482                 for (i = gs.nctrls; --i >= 0; ) {
483                         if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
484                                 free(gs.ctrls[i]->ldctl_value.bv_val);
485                         free(gs.ctrls[i]);
486                 }
487                 free(gs.ctrls);
488         }
489         return rs->sr_err;
490 }
491
492 static BackendDB toolDB;
493
494 static int
495 glue_tool_entry_open (
496         BackendDB *b0,
497         int mode
498 )
499 {
500         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
501
502         /* We don't know which backend to talk to yet, so just
503          * remember the mode and move on...
504          */
505
506         glueMode = mode;
507         glueBack = NULL;
508         toolDB = *b0;
509         toolDB.bd_info = oi->oi_orig;
510
511         return 0;
512 }
513
514 static int
515 glue_tool_entry_close (
516         BackendDB *b0
517 )
518 {
519         int rc = 0;
520
521         if (glueBack) {
522                 if (!glueBack->be_entry_close)
523                         return 0;
524                 rc = glueBack->be_entry_close (glueBack);
525         }
526         return rc;
527 }
528
529 static slap_overinst *
530 glue_tool_inst(
531         BackendInfo *bi
532 )
533 {
534         slap_overinfo   *oi = (slap_overinfo *)bi;
535         slap_overinst   *on;
536
537         for ( on = oi->oi_list; on; on=on->on_next ) {
538                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
539                         return on;
540         }
541         return NULL;
542 }
543
544 /* This function will only be called in tool mode */
545 static int
546 glue_open (
547         BackendInfo *bi
548 )
549 {
550         slap_overinst *on = glue_tool_inst( bi );
551         glueinfo                *gi = on->on_bi.bi_private;
552         static int glueOpened = 0;
553         int i, j, same, bsame = 0, rc = 0;
554
555         if (glueOpened) return 0;
556
557         glueOpened = 1;
558
559         /* If we were invoked in tool mode, open all the underlying backends */
560         if (slapMode & SLAP_TOOL_MODE) {
561                 for (i = 0; i<gi->gi_nodes; i++) {
562                         same = 0;
563                         /* Same bi_open as our main backend? */
564                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
565                                 on->on_info->oi_orig->bi_open )
566                                 bsame = 1;
567
568                         /* Loop thru the bd_info's and make sure we only
569                          * invoke their bi_open functions once each.
570                          */
571                         for ( j = 0; j<i; j++ ) {
572                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
573                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
574                                         same = 1;
575                                         break;
576                                 }
577                         }
578                         /* OK, it's unique and non-NULL, call it. */
579                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
580                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
581                                         gi->gi_n[i].gn_be->bd_info );
582                         /* Let backend.c take care of the rest of startup */
583                         if ( !rc )
584                                 rc = backend_startup_one( gi->gi_n[i].gn_be );
585                         if ( rc ) break;
586                 }
587                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
588                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
589
590         } /* other case is impossible */
591         return rc;
592 }
593
594 /* This function will only be called in tool mode */
595 static int
596 glue_close (
597         BackendInfo *bi
598 )
599 {
600         static int glueClosed = 0;
601         int rc = 0;
602
603         if (glueClosed) return 0;
604
605         glueClosed = 1;
606
607         if (slapMode & SLAP_TOOL_MODE) {
608                 rc = backend_shutdown( NULL );
609         }
610         return rc;
611 }
612
613 static int
614 glue_entry_release_rw (
615         Operation *op,
616         Entry *e,
617         int rw
618 )
619 {
620         BackendDB *b0, b2;
621         int rc = -1;
622
623         b0 = op->o_bd;
624         b2 = *op->o_bd;
625         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
626         op->o_bd = glue_back_select (&b2, &e->e_nname);
627
628         if ( op->o_bd->be_release ) {
629                 rc = op->o_bd->be_release( op, e, rw );
630
631         } else {
632                 /* FIXME: mimic be_entry_release_rw
633                  * when no be_release() available */
634                 /* free entry */
635                 entry_free( e );
636                 rc = 0;
637         }
638         op->o_bd = b0;
639         return rc;
640 }
641
642 static ID
643 glue_tool_entry_first (
644         BackendDB *b0
645 )
646 {
647         slap_overinst   *on = glue_tool_inst( b0->bd_info );
648         glueinfo                *gi = on->on_bi.bi_private;
649         int i;
650
651         /* If we're starting from scratch, start at the most general */
652         if (!glueBack) {
653                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
654                         glueBack = &toolDB;
655                 } else {
656                         for (i = gi->gi_nodes-1; i >= 0; i--) {
657                                 if (gi->gi_n[i].gn_be->be_entry_open &&
658                                         gi->gi_n[i].gn_be->be_entry_first) {
659                                                 glueBack = gi->gi_n[i].gn_be;
660                                         break;
661                                 }
662                         }
663                 }
664         }
665         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
666                 glueBack->be_entry_open (glueBack, glueMode) != 0)
667                 return NOID;
668
669         return glueBack->be_entry_first (glueBack);
670 }
671
672 static ID
673 glue_tool_entry_next (
674         BackendDB *b0
675 )
676 {
677         slap_overinst   *on = glue_tool_inst( b0->bd_info );
678         glueinfo                *gi = on->on_bi.bi_private;
679         int i;
680         ID rc;
681
682         if (!glueBack || !glueBack->be_entry_next)
683                 return NOID;
684
685         rc = glueBack->be_entry_next (glueBack);
686
687         /* If we ran out of entries in one database, move on to the next */
688         while (rc == NOID) {
689                 if ( glueBack && glueBack->be_entry_close )
690                         glueBack->be_entry_close (glueBack);
691                 for (i=0; i<gi->gi_nodes; i++) {
692                         if (gi->gi_n[i].gn_be == glueBack)
693                                 break;
694                 }
695                 if (i == 0) {
696                         glueBack = NULL;
697                         break;
698                 } else {
699                         glueBack = gi->gi_n[i-1].gn_be;
700                         rc = glue_tool_entry_first (b0);
701                 }
702         }
703         return rc;
704 }
705
706 static Entry *
707 glue_tool_entry_get (
708         BackendDB *b0,
709         ID id
710 )
711 {
712         if (!glueBack || !glueBack->be_entry_get)
713                 return NULL;
714
715         return glueBack->be_entry_get (glueBack, id);
716 }
717
718 static ID
719 glue_tool_entry_put (
720         BackendDB *b0,
721         Entry *e,
722         struct berval *text
723 )
724 {
725         BackendDB *be, b2;
726         int rc = -1;
727
728         b2 = *b0;
729         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
730         be = glue_back_select (&b2, &e->e_nname);
731         if ( be == &b2 ) be = &toolDB;
732
733         if (!be->be_entry_put)
734                 return NOID;
735
736         if (!glueBack) {
737                 if ( be->be_entry_open ) {
738                         rc = be->be_entry_open (be, glueMode);
739                 }
740                 if (rc != 0) {
741                         return NOID;
742                 }
743         } else if (be != glueBack) {
744                 /* If this entry belongs in a different branch than the
745                  * previous one, close the current database and open the
746                  * new one.
747                  */
748                 if ( glueBack->be_entry_close ) {
749                         glueBack->be_entry_close (glueBack);
750                 }
751                 if ( be->be_entry_open ) {
752                         rc = be->be_entry_open (be, glueMode);
753                 }
754                 if (rc != 0) {
755                         return NOID;
756                 }
757         }
758         glueBack = be;
759         return be->be_entry_put (be, e, text);
760 }
761
762 static int
763 glue_tool_entry_reindex (
764         BackendDB *b0,
765         ID id
766 )
767 {
768         if (!glueBack || !glueBack->be_entry_reindex)
769                 return -1;
770
771         return glueBack->be_entry_reindex (glueBack, id);
772 }
773
774 static int
775 glue_tool_sync (
776         BackendDB *b0
777 )
778 {
779         slap_overinst   *on = glue_tool_inst( b0->bd_info );
780         glueinfo                *gi = on->on_bi.bi_private;
781         BackendInfo             *bi = b0->bd_info;
782         int i;
783
784         /* just sync everyone */
785         for (i = 0; i<gi->gi_nodes; i++)
786                 if (gi->gi_n[i].gn_be->be_sync)
787                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
788         b0->bd_info = on->on_info->oi_orig;
789         if ( b0->be_sync )
790                 b0->be_sync( b0 );
791         b0->bd_info = bi;
792         return 0;
793 }
794
795 static int
796 glue_db_init(
797         BackendDB *be
798 )
799 {
800         slap_overinst   *on = (slap_overinst *)be->bd_info;
801         slap_overinfo   *oi = on->on_info;
802         BackendInfo     *bi = oi->oi_orig;
803         glueinfo *gi;
804
805         if ( SLAP_GLUE_SUBORDINATE( be )) {
806                 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
807                         "cannot have glue overlay!\n",
808                         be->be_suffix[0].bv_val, 0, 0 );
809                 return LDAP_OTHER;
810         }
811
812         gi = ch_calloc( 1, sizeof(glueinfo));
813         on->on_bi.bi_private = gi;
814         dnParent( be->be_nsuffix, &gi->gi_pdn );
815
816         /* Currently the overlay framework doesn't handle these entry points
817          * but we need them....
818          */
819         oi->oi_bi.bi_open = glue_open;
820         oi->oi_bi.bi_close = glue_close;
821
822         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
823
824         /* Only advertise these if the root DB supports them */
825         if ( bi->bi_tool_entry_open )
826                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
827         if ( bi->bi_tool_entry_close )
828                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
829         if ( bi->bi_tool_entry_first )
830                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
831         if ( bi->bi_tool_entry_next )
832                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
833         if ( bi->bi_tool_entry_get )
834                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
835         if ( bi->bi_tool_entry_put )
836                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
837         if ( bi->bi_tool_entry_reindex )
838                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
839         if ( bi->bi_tool_sync )
840                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
841
842         /*FIXME : need to add support */
843         oi->oi_bi.bi_tool_dn2id_get = 0;
844         oi->oi_bi.bi_tool_id2entry_get = 0;
845         oi->oi_bi.bi_tool_entry_modify = 0;
846
847         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
848
849         return 0;
850 }
851
852 static int
853 glue_db_destroy (
854         BackendDB *be
855 )
856 {
857         slap_overinst   *on = (slap_overinst *)be->bd_info;
858         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
859
860         free (gi);
861         return SLAP_CB_CONTINUE;
862 }
863
864 static int
865 glue_db_close( 
866         BackendDB *be
867 )
868 {
869         slap_overinst   *on = (slap_overinst *)be->bd_info;
870
871         on->on_info->oi_bi.bi_db_close = 0;
872         return 0;
873 }
874
875 int
876 glue_sub_del( BackendDB *b0 )
877 {
878         BackendDB *be;
879         int rc = 0;
880
881         /* Find the top backend for this subordinate */
882         be = b0;
883         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
884                 slap_overinfo *oi;
885                 slap_overinst *on;
886                 glueinfo *gi;
887                 int i;
888
889                 if ( SLAP_GLUE_SUBORDINATE( be ))
890                         continue;
891                 if ( !SLAP_GLUE_INSTANCE( be ))
892                         continue;
893                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
894                         continue;
895
896                 /* OK, got the right backend, find the overlay */
897                 oi = (slap_overinfo *)be->bd_info;
898                 for ( on=oi->oi_list; on; on=on->on_next ) {
899                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
900                                 break;
901                 }
902                 assert( on != NULL );
903                 gi = on->on_bi.bi_private;
904                 for ( i=0; i < gi->gi_nodes; i++ ) {
905                         if ( gi->gi_n[i].gn_be == b0 ) {
906                                 int j;
907
908                                 for (j=i+1; j < gi->gi_nodes; j++)
909                                         gi->gi_n[j-1] = gi->gi_n[j];
910
911                                 gi->gi_nodes--;
912                         }
913                 }
914         }
915         if ( be == NULL )
916                 rc = LDAP_NO_SUCH_OBJECT;
917
918         return rc;
919 }
920
921 typedef struct glue_Addrec {
922         struct glue_Addrec *ga_next;
923         BackendDB *ga_be;
924 } glue_Addrec;
925
926 /* List of added subordinates */
927 static glue_Addrec *ga_list;
928
929 /* Attach all the subordinate backends to their superior */
930 int
931 glue_sub_attach()
932 {
933         glue_Addrec *ga, *gnext = NULL;
934         int rc = 0;
935
936         /* For all the subordinate backends */
937         for ( ga=ga_list; ga != NULL; ga = gnext ) {
938                 BackendDB *be;
939
940                 gnext = ga->ga_next;
941
942                 /* Find the top backend for this subordinate */
943                 be = ga->ga_be;
944                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
945                         slap_overinfo *oi;
946                         slap_overinst *on;
947                         glueinfo *gi;
948
949                         if ( SLAP_GLUE_SUBORDINATE( be ))
950                                 continue;
951                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
952                                 continue;
953
954                         /* If it's not already configured, set up the overlay */
955                         if ( !SLAP_GLUE_INSTANCE( be )) {
956                                 rc = overlay_config( be, glue.on_bi.bi_type );
957                                 if ( rc )
958                                         break;
959                         }
960                         /* Find the overlay instance */
961                         oi = (slap_overinfo *)be->bd_info;
962                         for ( on=oi->oi_list; on; on=on->on_next ) {
963                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
964                                         break;
965                         }
966                         assert( on != NULL );
967                         gi = on->on_bi.bi_private;
968                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
969                                 gi->gi_nodes * sizeof(gluenode));
970                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
971                         dnParent( &ga->ga_be->be_nsuffix[0],
972                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
973                         gi->gi_nodes++;
974                         on->on_bi.bi_private = gi;
975                         break;
976                 }
977                 if ( !be ) {
978                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
979                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
980                         rc = LDAP_NO_SUCH_OBJECT;
981                 }
982                 ch_free( ga );
983                 if ( rc ) break;
984         }
985
986         ga_list = gnext;
987
988         return rc;
989 }
990
991 int
992 glue_sub_add( BackendDB *be, int advert, int online )
993 {
994         glue_Addrec *ga;
995         int rc = 0;
996
997         if ( overlay_is_inst( be, "glue" )) {
998                 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
999                         "cannot be a subordinate!\n",
1000                         be->be_suffix[0].bv_val, 0, 0 );
1001                 return LDAP_OTHER;
1002         }
1003         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1004         if ( advert )
1005                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1006
1007         ga = ch_malloc( sizeof( glue_Addrec ));
1008         ga->ga_next = ga_list;
1009         ga->ga_be = be;
1010         ga_list = ga;
1011
1012         if ( online )
1013                 rc = glue_sub_attach();
1014
1015         return rc;
1016 }
1017
1018 int
1019 glue_sub_init()
1020 {
1021         glue.on_bi.bi_type = "glue";
1022
1023         glue.on_bi.bi_db_init = glue_db_init;
1024         glue.on_bi.bi_db_close = glue_db_close;
1025         glue.on_bi.bi_db_destroy = glue_db_destroy;
1026
1027         glue.on_bi.bi_op_search = glue_op_search;
1028         glue.on_bi.bi_op_modify = glue_op_func;
1029         glue.on_bi.bi_op_modrdn = glue_op_func;
1030         glue.on_bi.bi_op_add = glue_op_func;
1031         glue.on_bi.bi_op_delete = glue_op_func;
1032         glue.on_bi.bi_extended = glue_op_func;
1033
1034         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1035         glue.on_bi.bi_chk_controls = glue_chk_controls;
1036         glue.on_response = glue_response;
1037
1038         return overlay_register( &glue );
1039 }