]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/glue.c
fix spelling
[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                 return SLAP_CB_CONTINUE;
275
276         case LDAP_SCOPE_ONELEVEL:
277         case LDAP_SCOPE_SUBTREE:
278 #ifdef LDAP_SCOPE_SUBORDINATE
279         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
280 #endif
281
282 #if 0
283                 if ( op->o_sync ) {
284                         if (op->o_bd && op->o_bd->be_search) {
285                                 rs->sr_err = op->o_bd->be_search( op, rs );
286                         } else {
287                                 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
288                                                 "No search target found");
289                         }
290                         return rs->sr_err;
291                 }
292 #endif
293
294                 op->o_callback = &cb;
295                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
296                 scope0 = op->ors_scope;
297                 slimit0 = gs.slimit = op->ors_slimit;
298                 tlimit0 = op->ors_tlimit;
299                 dn = op->o_req_dn;
300                 ndn = op->o_req_ndn;
301                 b1 = op->o_bd;
302
303                 /*
304                  * Execute in reverse order, most general first 
305                  */
306                 for (i = gi->gi_nodes; i >= 0; i--) {
307                         if ( i == gi->gi_nodes ) {
308                                 btmp = b0;
309                                 pdn = &gi->gi_pdn;
310                         } else {
311                                 btmp = gi->gi_n[i].gn_be;
312                                 pdn = &gi->gi_n[i].gn_pdn;
313                         }
314                         if (!btmp || !btmp->be_search)
315                                 continue;
316                         if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
317                                 continue;
318                         if (tlimit0 != SLAP_NO_LIMIT) {
319                                 op->ors_tlimit = stoptime - slap_get_time ();
320                                 if (op->ors_tlimit <= 0) {
321                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
322                                         break;
323                                 }
324                         }
325                         if (slimit0 != SLAP_NO_LIMIT) {
326                                 op->ors_slimit = slimit0 - rs->sr_nentries;
327                                 if (op->ors_slimit < 0) {
328                                         rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
329                                         break;
330                                 }
331                         }
332                         rs->sr_err = 0;
333                         /*
334                          * check for abandon 
335                          */
336                         if (op->o_abandon) {
337                                 goto end_of_loop;
338                         }
339                         op->o_bd = btmp;
340
341                         assert( op->o_bd->be_suffix );
342                         assert( op->o_bd->be_nsuffix );
343                         
344                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
345                                 dn_match(pdn, &ndn))
346                         {
347                                 op->ors_scope = LDAP_SCOPE_BASE;
348                                 op->o_req_dn = op->o_bd->be_suffix[0];
349                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
350                                 rs->sr_err = op->o_bd->be_search(op, rs);
351
352                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
353                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
354                         {
355                                 rs->sr_err = op->o_bd->be_search( op, rs );
356
357                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
358                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
359                         {
360                                 op->o_req_dn = op->o_bd->be_suffix[0];
361                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
362                                 rs->sr_err = op->o_bd->be_search( op, rs );
363                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
364                                         gs.err = LDAP_SUCCESS;
365                                 }
366
367                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
368                                 rs->sr_err = op->o_bd->be_search( op, rs );
369                         }
370
371                         switch ( gs.err ) {
372
373                         /*
374                          * Add errors that should result in dropping
375                          * the search
376                          */
377                         case LDAP_SIZELIMIT_EXCEEDED:
378                         case LDAP_TIMELIMIT_EXCEEDED:
379                         case LDAP_ADMINLIMIT_EXCEEDED:
380                         case LDAP_NO_SUCH_OBJECT:
381 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
382                         case LDAP_CANNOT_CHAIN:
383 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
384                                 goto end_of_loop;
385                         
386                         default:
387                                 break;
388                         }
389                 }
390 end_of_loop:;
391                 op->ors_scope = scope0;
392                 op->ors_slimit = slimit0;
393                 op->ors_tlimit = tlimit0;
394                 op->o_req_dn = dn;
395                 op->o_req_ndn = ndn;
396
397                 break;
398         }
399         if ( !op->o_abandon ) {
400                 op->o_callback = cb.sc_next;
401                 rs->sr_err = gs.err;
402                 rs->sr_matched = gs.matched;
403                 rs->sr_ref = gs.refs;
404
405                 send_ldap_result( op, rs );
406         }
407
408         op->o_bd = b0;
409         op->o_bd->bd_info = bi0;
410         if (gs.matched)
411                 free (gs.matched);
412         if (gs.refs)
413                 ber_bvarray_free(gs.refs);
414         return rs->sr_err;
415 }
416
417 static BackendDB toolDB;
418
419 static int
420 glue_tool_entry_open (
421         BackendDB *b0,
422         int mode
423 )
424 {
425         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
426
427         /* We don't know which backend to talk to yet, so just
428          * remember the mode and move on...
429          */
430
431         glueMode = mode;
432         glueBack = NULL;
433         toolDB = *b0;
434         toolDB.bd_info = oi->oi_orig;
435
436         return 0;
437 }
438
439 static int
440 glue_tool_entry_close (
441         BackendDB *b0
442 )
443 {
444         int rc = 0;
445
446         if (glueBack) {
447                 if (!glueBack->be_entry_close)
448                         return 0;
449                 rc = glueBack->be_entry_close (glueBack);
450         }
451         return rc;
452 }
453
454 static slap_overinst *
455 glue_tool_inst(
456         BackendInfo *bi
457 )
458 {
459         slap_overinfo   *oi = (slap_overinfo *)bi;
460         slap_overinst   *on;
461
462         for ( on = oi->oi_list; on; on=on->on_next ) {
463                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
464                         return on;
465         }
466         return NULL;
467 }
468
469 /* This function will only be called in tool mode */
470 static int
471 glue_open (
472         BackendInfo *bi
473 )
474 {
475         slap_overinst *on = glue_tool_inst( bi );
476         glueinfo                *gi = on->on_bi.bi_private;
477         static int glueOpened = 0;
478         int i, rc = 0;
479
480         if (glueOpened) return 0;
481
482         glueOpened = 1;
483
484         /* If we were invoked in tool mode, open all the underlying backends */
485         if (slapMode & SLAP_TOOL_MODE) {
486                 rc = backend_startup( NULL );
487         } /* other case is impossible */
488         return rc;
489 }
490
491 /* This function will only be called in tool mode */
492 static int
493 glue_close (
494         BackendInfo *bi
495 )
496 {
497         slap_overinst *on = glue_tool_inst( bi );
498         glueinfo                *gi = on->on_bi.bi_private;
499         static int glueClosed = 0;
500         int i, rc = 0;
501
502         if (glueClosed) return 0;
503
504         glueClosed = 1;
505
506         if (slapMode & SLAP_TOOL_MODE) {
507                 rc = backend_shutdown( NULL );
508         }
509         return rc;
510 }
511
512 static int
513 glue_entry_release_rw (
514         Operation *op,
515         Entry *e,
516         int rw
517 )
518 {
519         BackendDB *b0, b2;
520         int rc;
521
522         b0 = op->o_bd;
523         b2 = *op->o_bd;
524         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
525         op->o_bd = glue_back_select (&b2, &e->e_nname);
526
527         rc = op->o_bd->be_release( op, e, rw );
528         op->o_bd = b0;
529         return rc;
530 }
531
532 static ID
533 glue_tool_entry_first (
534         BackendDB *b0
535 )
536 {
537         slap_overinst   *on = glue_tool_inst( b0->bd_info );
538         glueinfo                *gi = on->on_bi.bi_private;
539         int i;
540
541         /* If we're starting from scratch, start at the most general */
542         if (!glueBack) {
543                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
544                         glueBack = &toolDB;
545                 } else {
546                         for (i = gi->gi_nodes-1; i >= 0; i--) {
547                                 if (gi->gi_n[i].gn_be->be_entry_open &&
548                                         gi->gi_n[i].gn_be->be_entry_first) {
549                                                 glueBack = gi->gi_n[i].gn_be;
550                                         break;
551                                 }
552                         }
553                 }
554         }
555         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
556                 glueBack->be_entry_open (glueBack, glueMode) != 0)
557                 return NOID;
558
559         return glueBack->be_entry_first (glueBack);
560 }
561
562 static ID
563 glue_tool_entry_next (
564         BackendDB *b0
565 )
566 {
567         slap_overinst   *on = glue_tool_inst( b0->bd_info );
568         glueinfo                *gi = on->on_bi.bi_private;
569         int i;
570         ID rc;
571
572         if (!glueBack || !glueBack->be_entry_next)
573                 return NOID;
574
575         rc = glueBack->be_entry_next (glueBack);
576
577         /* If we ran out of entries in one database, move on to the next */
578         while (rc == NOID) {
579                 if ( glueBack && glueBack->be_entry_close )
580                         glueBack->be_entry_close (glueBack);
581                 for (i=0; i<gi->gi_nodes; i++) {
582                         if (gi->gi_n[i].gn_be == glueBack)
583                                 break;
584                 }
585                 if (i == 0) {
586                         glueBack = NULL;
587                         break;
588                 } else {
589                         glueBack = gi->gi_n[i-1].gn_be;
590                         rc = glue_tool_entry_first (b0);
591                 }
592         }
593         return rc;
594 }
595
596 static Entry *
597 glue_tool_entry_get (
598         BackendDB *b0,
599         ID id
600 )
601 {
602         if (!glueBack || !glueBack->be_entry_get)
603                 return NULL;
604
605         return glueBack->be_entry_get (glueBack, id);
606 }
607
608 static ID
609 glue_tool_entry_put (
610         BackendDB *b0,
611         Entry *e,
612         struct berval *text
613 )
614 {
615         BackendDB *be, b2;
616         int rc;
617
618         b2 = *b0;
619         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
620         be = glue_back_select (&b2, &e->e_nname);
621         if ( be == &b2 ) be = &toolDB;
622
623         if (!be->be_entry_put)
624                 return NOID;
625
626         if (!glueBack) {
627                 rc = be->be_entry_open (be, glueMode);
628                 if (rc != 0)
629                         return NOID;
630         } else if (be != glueBack) {
631                 /* If this entry belongs in a different branch than the
632                  * previous one, close the current database and open the
633                  * new one.
634                  */
635                 glueBack->be_entry_close (glueBack);
636                 rc = be->be_entry_open (be, glueMode);
637                 if (rc != 0)
638                         return NOID;
639         }
640         glueBack = be;
641         return be->be_entry_put (be, e, text);
642 }
643
644 static int
645 glue_tool_entry_reindex (
646         BackendDB *b0,
647         ID id
648 )
649 {
650         if (!glueBack || !glueBack->be_entry_reindex)
651                 return -1;
652
653         return glueBack->be_entry_reindex (glueBack, id);
654 }
655
656 static int
657 glue_tool_sync (
658         BackendDB *b0
659 )
660 {
661         slap_overinst   *on = glue_tool_inst( b0->bd_info );
662         glueinfo                *gi = on->on_bi.bi_private;
663         BackendInfo             *bi = b0->bd_info;
664         int i;
665
666         /* just sync everyone */
667         for (i = 0; i<gi->gi_nodes; i++)
668                 if (gi->gi_n[i].gn_be->be_sync)
669                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
670         b0->bd_info = on->on_info->oi_orig;
671         if ( b0->be_sync )
672                 b0->be_sync( b0 );
673         b0->bd_info = bi;
674         return 0;
675 }
676
677 static int
678 glue_db_init(
679         BackendDB *be
680 )
681 {
682         slap_overinst   *on = (slap_overinst *)be->bd_info;
683         slap_overinfo   *oi = on->on_info;
684         glueinfo *gi;
685
686         gi = ch_calloc( 1, sizeof(glueinfo));
687         on->on_bi.bi_private = gi;
688         dnParent( be->be_nsuffix, &gi->gi_pdn );
689
690         /* Currently the overlay framework doesn't handle these entry points
691          * but we need them....
692          */
693         oi->oi_bi.bi_open = glue_open;
694         oi->oi_bi.bi_close = glue_close;
695
696         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
697
698         oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
699         oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
700         oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
701         oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
702         oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
703         oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
704         oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
705         oi->oi_bi.bi_tool_sync = glue_tool_sync;
706
707         /*FIXME : need to add support */
708         oi->oi_bi.bi_tool_dn2id_get = 0;
709         oi->oi_bi.bi_tool_id2entry_get = 0;
710         oi->oi_bi.bi_tool_entry_modify = 0;
711
712         return 0;
713 }
714
715 static int
716 glue_db_destroy (
717         BackendDB *be
718 )
719 {
720         slap_overinst   *on = (slap_overinst *)be->bd_info;
721         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
722
723         free (gi);
724         return SLAP_CB_CONTINUE;
725 }
726
727 static int
728 glue_db_open (
729         BackendDB *be
730 )
731 {
732         slap_overinst   *on = (slap_overinst *)be->bd_info;
733         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
734         int i;
735
736         for ( i=0; i<gi->gi_nodes; i++ ) {
737                 int j;
738
739                 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
740         }
741         return 0;
742 }
743
744 static int
745 glue_db_close( 
746         BackendDB *be
747 )
748 {
749         slap_overinst   *on = (slap_overinst *)be->bd_info;
750
751         on->on_info->oi_bi.bi_db_close = NULL;
752         return 0;
753 }
754
755 static int
756 glue_db_config(
757         BackendDB       *be,
758         const char      *fname,
759         int             lineno,
760         int             argc,
761         char    **argv
762 )
763 {
764         slap_overinst   *on = (slap_overinst *)be->bd_info;
765         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
766
767         /* redundant; could be applied just once */
768         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
769
770         if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
771                 int i, async = 0, advertise = 0;
772                 BackendDB *b2;
773                 struct berval bv, dn;
774                 gluenode *gn;
775
776                 if ( argc < 2 ) {
777                         fprintf( stderr, "%s: line %d: too few arguments in "
778                                 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
779                         return -1;
780                 }
781                 for ( i = 2; i < argc; i++ ) {
782                         if ( strcasecmp( argv[i], "async" ) == 0 ) {
783                                 async = 1;
784
785                         } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
786                                 advertise = 1;
787
788                         } else {
789                                 fprintf( stderr, "%s: line %d: unrecognized option "
790                                         "\"%s\" ignored.\n", fname, lineno, argv[i] );
791                         }
792                 }
793                 ber_str2bv( argv[1], 0, 0, &bv );
794                 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
795                         fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
796                         return -1;
797                 }
798                 b2 = select_backend( &dn, 0, 1 );
799                 if ( !b2 ) {
800                         fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
801                                 fname, lineno, argv[1] );
802                         return -1;
803                 }
804                 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
805                 if ( advertise ) {
806                         SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
807                 }
808                 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
809                         gi->gi_nodes * sizeof(gluenode));
810                 gi->gi_n[gi->gi_nodes].gn_bx = b2 - backendDB;
811                 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
812                 gi->gi_n[gi->gi_nodes].gn_async = async;
813                 gi->gi_nodes++;
814                 on->on_bi.bi_private = gi;
815                 return 0;
816         }
817         return SLAP_CONF_UNKNOWN;
818 }
819
820 int
821 glue_init()
822 {
823         glue.on_bi.bi_type = "glue";
824
825         glue.on_bi.bi_db_init = glue_db_init;
826         glue.on_bi.bi_db_config = glue_db_config;
827         glue.on_bi.bi_db_open = glue_db_open;
828         glue.on_bi.bi_db_close = glue_db_close;
829         glue.on_bi.bi_db_destroy = glue_db_destroy;
830
831         glue.on_bi.bi_op_search = glue_op_search;
832         glue.on_bi.bi_op_modify = glue_op_func;
833         glue.on_bi.bi_op_modrdn = glue_op_func;
834         glue.on_bi.bi_op_add = glue_op_func;
835         glue.on_bi.bi_op_delete = glue_op_func;
836
837         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
838         glue.on_bi.bi_chk_controls = glue_chk_controls;
839
840         return overlay_register( &glue );
841 }
842
843 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
844 int
845 init_module( int argc, char *argv[] )
846 {
847         return glue_init();
848 }
849 #endif  /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
850
851 #endif  /* defined(SLAPD_OVER_GLUE */