]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
ITS#5322 don't try to free a NULL locker
[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-2008 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 *b1 = NULL, *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                 b1 = op->o_bd;
389
390                 /*
391                  * Execute in reverse order, most specific first 
392                  */
393                 for (i = gi->gi_nodes; i >= 0; i--) {
394                         if ( i == gi->gi_nodes ) {
395                                 btmp = b0;
396                                 pdn = &gi->gi_pdn;
397                         } else {
398                                 btmp = gi->gi_n[i].gn_be;
399                                 pdn = &gi->gi_n[i].gn_pdn;
400                         }
401                         if (!btmp || !btmp->be_search)
402                                 continue;
403                         if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
404                                 continue;
405                         if (get_no_subordinate_glue(op) && btmp != b1)
406                                 continue;
407                         /* If we remembered which backend we were on before,
408                          * skip down to it now
409                          */
410                         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
411                                 op->o_conn->c_pagedresults_state.ps_be &&
412                                 op->o_conn->c_pagedresults_state.ps_be != btmp )
413                                 continue;
414
415                         if (tlimit0 != SLAP_NO_LIMIT) {
416                                 op->o_time = slap_get_time();
417                                 op->ors_tlimit = stoptime - op->o_time;
418                                 if (op->ors_tlimit <= 0) {
419                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
420                                         break;
421                                 }
422                         }
423                         rs->sr_err = 0;
424                         /*
425                          * check for abandon 
426                          */
427                         if (op->o_abandon) {
428                                 goto end_of_loop;
429                         }
430                         op->o_bd = btmp;
431
432                         assert( op->o_bd->be_suffix != NULL );
433                         assert( op->o_bd->be_nsuffix != NULL );
434                         
435                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
436                                 dn_match(pdn, &ndn))
437                         {
438                                 op->ors_scope = LDAP_SCOPE_BASE;
439                                 op->o_req_dn = op->o_bd->be_suffix[0];
440                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
441                                 rs->sr_err = op->o_bd->be_search(op, rs);
442                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
443                                         gs.err = LDAP_SUCCESS;
444                                 }
445                                 op->ors_scope = LDAP_SCOPE_ONELEVEL;
446                                 op->o_req_dn = dn;
447                                 op->o_req_ndn = ndn;
448
449                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
450                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
451                         {
452                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
453
454                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
455                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
456                         {
457                                 op->o_req_dn = op->o_bd->be_suffix[0];
458                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
459                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
460                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
461                                         gs.err = LDAP_SUCCESS;
462                                 }
463                                 op->o_req_dn = dn;
464                                 op->o_req_ndn = ndn;
465
466                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
467                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
468                         }
469
470                         switch ( gs.err ) {
471
472                         /*
473                          * Add errors that should result in dropping
474                          * the search
475                          */
476                         case LDAP_SIZELIMIT_EXCEEDED:
477                         case LDAP_TIMELIMIT_EXCEEDED:
478                         case LDAP_ADMINLIMIT_EXCEEDED:
479                         case LDAP_NO_SUCH_OBJECT:
480 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
481                         case LDAP_X_CANNOT_CHAIN:
482 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
483                                 goto end_of_loop;
484
485                         case LDAP_SUCCESS:
486                                 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
487                                         PagedResultsState *ps = op->o_pagedresults_state;
488
489                                         /* Assume this backend can be forgotten now */
490                                         op->o_conn->c_pagedresults_state.ps_be = NULL;
491
492                                         /* If we have a full page, exit the loop. We may
493                                          * need to remember this backend so we can continue
494                                          * from here on a subsequent request.
495                                          */
496                                         if ( rs->sr_nentries >= ps->ps_size ) {
497                                                 /* Don't bother to remember the first backend.
498                                                  * Only remember the last one if there's more state left.
499                                                  */
500                                                 if ( op->o_bd != b0 &&
501                                                         ( op->o_conn->c_pagedresults_state.ps_cookie ||
502                                                         op->o_bd != gi->gi_n[0].gn_be ))
503                                                         op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
504                                                 goto end_of_loop;
505                                         }
506
507                                         /* This backend has run out of entries, but more responses
508                                          * can fit in the page. Fake a reset of the state so the
509                                          * next backend will start up properly. Only back-[bh]db
510                                          * and back-sql look at this state info.
511                                          */
512                                         if ( ps->ps_cookieval.bv_len == sizeof( PagedResultsCookie )) {
513                                                 ps->ps_cookie = 0;
514                                                 memset( ps->ps_cookieval.bv_val, 0,
515                                                         sizeof( PagedResultsCookie ));
516                                         }
517                                 }
518                                 
519                         default:
520                                 break;
521                         }
522                 }
523 end_of_loop:;
524                 op->ors_scope = scope0;
525                 op->ors_tlimit = tlimit0;
526                 op->o_time = starttime;
527                 op->o_req_dn = dn;
528                 op->o_req_ndn = ndn;
529
530                 break;
531         }
532         if ( op->o_abandon ) {
533                 rs->sr_err = SLAPD_ABANDON;
534         } else {
535                 op->o_callback = cb.sc_next;
536                 rs->sr_err = gs.err;
537                 rs->sr_matched = gs.matched;
538                 rs->sr_ref = gs.refs;
539                 rs->sr_ctrls = gs.ctrls;
540
541                 send_ldap_result( op, rs );
542         }
543
544         op->o_bd = b0;
545         op->o_bd->bd_info = bi0;
546         if (gs.matched)
547                 free (gs.matched);
548         if (gs.refs)
549                 ber_bvarray_free(gs.refs);
550         if (gs.ctrls) {
551                 for (i = gs.nctrls; --i >= 0; ) {
552                         if (!BER_BVISNULL( &gs.ctrls[i]->ldctl_value ))
553                                 free(gs.ctrls[i]->ldctl_value.bv_val);
554                         free(gs.ctrls[i]);
555                 }
556                 free(gs.ctrls);
557         }
558         return rs->sr_err;
559 }
560
561 static BackendDB toolDB;
562
563 static int
564 glue_tool_entry_open (
565         BackendDB *b0,
566         int mode
567 )
568 {
569         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
570
571         /* We don't know which backend to talk to yet, so just
572          * remember the mode and move on...
573          */
574
575         glueMode = mode;
576         glueBack = NULL;
577         toolDB = *b0;
578         toolDB.bd_info = oi->oi_orig;
579
580         return 0;
581 }
582
583 static int
584 glue_tool_entry_close (
585         BackendDB *b0
586 )
587 {
588         int rc = 0;
589
590         if (glueBack) {
591                 if (!glueBack->be_entry_close)
592                         return 0;
593                 rc = glueBack->be_entry_close (glueBack);
594         }
595         return rc;
596 }
597
598 static slap_overinst *
599 glue_tool_inst(
600         BackendInfo *bi
601 )
602 {
603         slap_overinfo   *oi = (slap_overinfo *)bi;
604         slap_overinst   *on;
605
606         for ( on = oi->oi_list; on; on=on->on_next ) {
607                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
608                         return on;
609         }
610         return NULL;
611 }
612
613 /* This function will only be called in tool mode */
614 static int
615 glue_open (
616         BackendInfo *bi
617 )
618 {
619         slap_overinst *on = glue_tool_inst( bi );
620         glueinfo                *gi = on->on_bi.bi_private;
621         static int glueOpened = 0;
622         int i, j, same, bsame = 0, rc = 0;
623
624         if (glueOpened) return 0;
625
626         glueOpened = 1;
627
628         /* If we were invoked in tool mode, open all the underlying backends */
629         if (slapMode & SLAP_TOOL_MODE) {
630                 for (i = 0; i<gi->gi_nodes; i++) {
631                         same = 0;
632                         /* Same bi_open as our main backend? */
633                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
634                                 on->on_info->oi_orig->bi_open )
635                                 bsame = 1;
636
637                         /* Loop thru the bd_info's and make sure we only
638                          * invoke their bi_open functions once each.
639                          */
640                         for ( j = 0; j<i; j++ ) {
641                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
642                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
643                                         same = 1;
644                                         break;
645                                 }
646                         }
647                         /* OK, it's unique and non-NULL, call it. */
648                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
649                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
650                                         gi->gi_n[i].gn_be->bd_info );
651                         /* Let backend.c take care of the rest of startup */
652                         if ( !rc )
653                                 rc = backend_startup_one( gi->gi_n[i].gn_be, NULL );
654                         if ( rc ) break;
655                 }
656                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
657                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
658
659         } /* other case is impossible */
660         return rc;
661 }
662
663 /* This function will only be called in tool mode */
664 static int
665 glue_close (
666         BackendInfo *bi
667 )
668 {
669         static int glueClosed = 0;
670         int rc = 0;
671
672         if (glueClosed) return 0;
673
674         glueClosed = 1;
675
676         if (slapMode & SLAP_TOOL_MODE) {
677                 rc = backend_shutdown( NULL );
678         }
679         return rc;
680 }
681
682 static int
683 glue_entry_get_rw (
684         Operation               *op,
685         struct berval   *dn,
686         ObjectClass             *oc,
687         AttributeDescription    *ad,
688         int     rw,
689         Entry   **e )
690 {
691         int rc;
692         BackendDB *b0 = op->o_bd;
693         op->o_bd = glue_back_select( b0, dn );
694
695         if ( op->o_bd->be_fetch ) {
696                 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
697         } else {
698                 rc = LDAP_UNWILLING_TO_PERFORM;
699         }
700         op->o_bd =b0;
701         return rc;
702 }
703
704 static int
705 glue_entry_release_rw (
706         Operation *op,
707         Entry *e,
708         int rw
709 )
710 {
711         BackendDB *b0 = op->o_bd;
712         int rc = -1;
713
714         op->o_bd = glue_back_select (b0, &e->e_nname);
715
716         if ( op->o_bd->be_release ) {
717                 rc = op->o_bd->be_release( op, e, rw );
718
719         } else {
720                 /* FIXME: mimic be_entry_release_rw
721                  * when no be_release() available */
722                 /* free entry */
723                 entry_free( e );
724                 rc = 0;
725         }
726         op->o_bd = b0;
727         return rc;
728 }
729
730 static ID
731 glue_tool_entry_first (
732         BackendDB *b0
733 )
734 {
735         slap_overinst   *on = glue_tool_inst( b0->bd_info );
736         glueinfo                *gi = on->on_bi.bi_private;
737         int i;
738
739         /* If we're starting from scratch, start at the most general */
740         if (!glueBack) {
741                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
742                         glueBack = &toolDB;
743                 } else {
744                         for (i = gi->gi_nodes-1; i >= 0; i--) {
745                                 if (gi->gi_n[i].gn_be->be_entry_open &&
746                                         gi->gi_n[i].gn_be->be_entry_first) {
747                                                 glueBack = gi->gi_n[i].gn_be;
748                                         break;
749                                 }
750                         }
751                 }
752         }
753         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
754                 glueBack->be_entry_open (glueBack, glueMode) != 0)
755                 return NOID;
756
757         return glueBack->be_entry_first (glueBack);
758 }
759
760 static ID
761 glue_tool_entry_next (
762         BackendDB *b0
763 )
764 {
765         slap_overinst   *on = glue_tool_inst( b0->bd_info );
766         glueinfo                *gi = on->on_bi.bi_private;
767         int i;
768         ID rc;
769
770         if (!glueBack || !glueBack->be_entry_next)
771                 return NOID;
772
773         rc = glueBack->be_entry_next (glueBack);
774
775         /* If we ran out of entries in one database, move on to the next */
776         while (rc == NOID) {
777                 if ( glueBack && glueBack->be_entry_close )
778                         glueBack->be_entry_close (glueBack);
779                 for (i=0; i<gi->gi_nodes; i++) {
780                         if (gi->gi_n[i].gn_be == glueBack)
781                                 break;
782                 }
783                 if (i == 0) {
784                         glueBack = NULL;
785                         break;
786                 } else {
787                         glueBack = gi->gi_n[i-1].gn_be;
788                         rc = glue_tool_entry_first (b0);
789                 }
790         }
791         return rc;
792 }
793
794 static Entry *
795 glue_tool_entry_get (
796         BackendDB *b0,
797         ID id
798 )
799 {
800         if (!glueBack || !glueBack->be_entry_get)
801                 return NULL;
802
803         return glueBack->be_entry_get (glueBack, id);
804 }
805
806 static ID
807 glue_tool_entry_put (
808         BackendDB *b0,
809         Entry *e,
810         struct berval *text
811 )
812 {
813         BackendDB *be, b2;
814         int rc = -1;
815
816         b2 = *b0;
817         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
818         be = glue_back_select (&b2, &e->e_nname);
819         if ( be == &b2 ) be = &toolDB;
820
821         if (!be->be_entry_put)
822                 return NOID;
823
824         if (!glueBack) {
825                 if ( be->be_entry_open ) {
826                         rc = be->be_entry_open (be, glueMode);
827                 }
828                 if (rc != 0) {
829                         return NOID;
830                 }
831         } else if (be != glueBack) {
832                 /* If this entry belongs in a different branch than the
833                  * previous one, close the current database and open the
834                  * new one.
835                  */
836                 if ( glueBack->be_entry_close ) {
837                         glueBack->be_entry_close (glueBack);
838                 }
839                 if ( be->be_entry_open ) {
840                         rc = be->be_entry_open (be, glueMode);
841                 }
842                 if (rc != 0) {
843                         return NOID;
844                 }
845         }
846         glueBack = be;
847         return be->be_entry_put (be, e, text);
848 }
849
850 static int
851 glue_tool_entry_reindex (
852         BackendDB *b0,
853         ID id,
854         AttributeDescription **adv
855 )
856 {
857         if (!glueBack || !glueBack->be_entry_reindex)
858                 return -1;
859
860         return glueBack->be_entry_reindex (glueBack, id, adv);
861 }
862
863 static int
864 glue_tool_sync (
865         BackendDB *b0
866 )
867 {
868         slap_overinst   *on = glue_tool_inst( b0->bd_info );
869         glueinfo                *gi = on->on_bi.bi_private;
870         BackendInfo             *bi = b0->bd_info;
871         int i;
872
873         /* just sync everyone */
874         for (i = 0; i<gi->gi_nodes; i++)
875                 if (gi->gi_n[i].gn_be->be_sync)
876                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
877         b0->bd_info = on->on_info->oi_orig;
878         if ( b0->be_sync )
879                 b0->be_sync( b0 );
880         b0->bd_info = bi;
881         return 0;
882 }
883
884 static int
885 glue_db_init(
886         BackendDB *be,
887         ConfigReply *cr
888 )
889 {
890         slap_overinst   *on = (slap_overinst *)be->bd_info;
891         slap_overinfo   *oi = on->on_info;
892         BackendInfo     *bi = oi->oi_orig;
893         glueinfo *gi;
894
895         if ( SLAP_GLUE_SUBORDINATE( be )) {
896                 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
897                         "cannot have glue overlay!\n",
898                         be->be_suffix[0].bv_val, 0, 0 );
899                 return LDAP_OTHER;
900         }
901
902         gi = ch_calloc( 1, sizeof(glueinfo));
903         on->on_bi.bi_private = gi;
904         dnParent( be->be_nsuffix, &gi->gi_pdn );
905
906         /* Currently the overlay framework doesn't handle these entry points
907          * but we need them....
908          */
909         oi->oi_bi.bi_open = glue_open;
910         oi->oi_bi.bi_close = glue_close;
911
912         /* Only advertise these if the root DB supports them */
913         if ( bi->bi_tool_entry_open )
914                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
915         if ( bi->bi_tool_entry_close )
916                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
917         if ( bi->bi_tool_entry_first )
918                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
919         if ( bi->bi_tool_entry_next )
920                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
921         if ( bi->bi_tool_entry_get )
922                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
923         if ( bi->bi_tool_entry_put )
924                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
925         if ( bi->bi_tool_entry_reindex )
926                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
927         if ( bi->bi_tool_sync )
928                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
929
930         /*FIXME : need to add support */
931         oi->oi_bi.bi_tool_dn2id_get = 0;
932         oi->oi_bi.bi_tool_entry_modify = 0;
933
934         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
935
936         return 0;
937 }
938
939 static int
940 glue_db_destroy (
941         BackendDB *be,
942         ConfigReply *cr
943 )
944 {
945         slap_overinst   *on = (slap_overinst *)be->bd_info;
946         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
947
948         free (gi);
949         return SLAP_CB_CONTINUE;
950 }
951
952 static int
953 glue_db_close( 
954         BackendDB *be,
955         ConfigReply *cr
956 )
957 {
958         slap_overinst   *on = (slap_overinst *)be->bd_info;
959
960         on->on_info->oi_bi.bi_db_close = 0;
961         return 0;
962 }
963
964 int
965 glue_sub_del( BackendDB *b0 )
966 {
967         BackendDB *be;
968         int rc = 0;
969
970         /* Find the top backend for this subordinate */
971         be = b0;
972         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
973                 slap_overinfo *oi;
974                 slap_overinst *on;
975                 glueinfo *gi;
976                 int i;
977
978                 if ( SLAP_GLUE_SUBORDINATE( be ))
979                         continue;
980                 if ( !SLAP_GLUE_INSTANCE( be ))
981                         continue;
982                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
983                         continue;
984
985                 /* OK, got the right backend, find the overlay */
986                 oi = (slap_overinfo *)be->bd_info;
987                 for ( on=oi->oi_list; on; on=on->on_next ) {
988                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
989                                 break;
990                 }
991                 assert( on != NULL );
992                 gi = on->on_bi.bi_private;
993                 for ( i=0; i < gi->gi_nodes; i++ ) {
994                         if ( gi->gi_n[i].gn_be == b0 ) {
995                                 int j;
996
997                                 for (j=i+1; j < gi->gi_nodes; j++)
998                                         gi->gi_n[j-1] = gi->gi_n[j];
999
1000                                 gi->gi_nodes--;
1001                         }
1002                 }
1003         }
1004         if ( be == NULL )
1005                 rc = LDAP_NO_SUCH_OBJECT;
1006
1007         return rc;
1008 }
1009
1010 typedef struct glue_Addrec {
1011         struct glue_Addrec *ga_next;
1012         BackendDB *ga_be;
1013 } glue_Addrec;
1014
1015 /* List of added subordinates */
1016 static glue_Addrec *ga_list;
1017
1018 /* Attach all the subordinate backends to their superior */
1019 int
1020 glue_sub_attach()
1021 {
1022         glue_Addrec *ga, *gnext = NULL;
1023         int rc = 0;
1024
1025         /* For all the subordinate backends */
1026         for ( ga=ga_list; ga != NULL; ga = gnext ) {
1027                 BackendDB *be;
1028
1029                 gnext = ga->ga_next;
1030
1031                 /* Find the top backend for this subordinate */
1032                 be = ga->ga_be;
1033                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1034                         slap_overinfo *oi;
1035                         slap_overinst *on;
1036                         glueinfo *gi;
1037
1038                         if ( SLAP_GLUE_SUBORDINATE( be ))
1039                                 continue;
1040                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1041                                 continue;
1042
1043                         /* If it's not already configured, set up the overlay */
1044                         if ( !SLAP_GLUE_INSTANCE( be )) {
1045                                 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL );
1046                                 if ( rc )
1047                                         break;
1048                         }
1049                         /* Find the overlay instance */
1050                         oi = (slap_overinfo *)be->bd_info;
1051                         for ( on=oi->oi_list; on; on=on->on_next ) {
1052                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1053                                         break;
1054                         }
1055                         assert( on != NULL );
1056                         gi = on->on_bi.bi_private;
1057                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1058                                 gi->gi_nodes * sizeof(gluenode));
1059                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1060                         dnParent( &ga->ga_be->be_nsuffix[0],
1061                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
1062                         gi->gi_nodes++;
1063                         on->on_bi.bi_private = gi;
1064                         break;
1065                 }
1066                 if ( !be ) {
1067                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1068                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1069                         rc = LDAP_NO_SUCH_OBJECT;
1070                 }
1071                 ch_free( ga );
1072                 if ( rc ) break;
1073         }
1074
1075         ga_list = gnext;
1076
1077         return rc;
1078 }
1079
1080 int
1081 glue_sub_add( BackendDB *be, int advert, int online )
1082 {
1083         glue_Addrec *ga;
1084         int rc = 0;
1085
1086         if ( overlay_is_inst( be, "glue" )) {
1087                 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1088                         "cannot be a subordinate!\n",
1089                         be->be_suffix[0].bv_val, 0, 0 );
1090                 return LDAP_OTHER;
1091         }
1092         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1093         if ( advert )
1094                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1095
1096         ga = ch_malloc( sizeof( glue_Addrec ));
1097         ga->ga_next = ga_list;
1098         ga->ga_be = be;
1099         ga_list = ga;
1100
1101         if ( online )
1102                 rc = glue_sub_attach();
1103
1104         return rc;
1105 }
1106
1107 int
1108 glue_sub_init()
1109 {
1110         glue.on_bi.bi_type = "glue";
1111
1112         glue.on_bi.bi_db_init = glue_db_init;
1113         glue.on_bi.bi_db_close = glue_db_close;
1114         glue.on_bi.bi_db_destroy = glue_db_destroy;
1115
1116         glue.on_bi.bi_op_search = glue_op_search;
1117         glue.on_bi.bi_op_modify = glue_op_func;
1118         glue.on_bi.bi_op_modrdn = glue_op_func;
1119         glue.on_bi.bi_op_add = glue_op_func;
1120         glue.on_bi.bi_op_delete = glue_op_func;
1121         glue.on_bi.bi_extended = glue_op_func;
1122
1123         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1124         glue.on_bi.bi_chk_controls = glue_chk_controls;
1125         glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1126         glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1127
1128         glue.on_response = glue_response;
1129
1130         return overlay_register( &glue );
1131 }