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