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