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