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