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