]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/glue.c
Sync with HEAD
[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                 rs->sr_err = SLAPD_ABANDON;
401         } else {
402                 op->o_callback = cb.sc_next;
403                 rs->sr_err = gs.err;
404                 rs->sr_matched = gs.matched;
405                 rs->sr_ref = gs.refs;
406
407                 send_ldap_result( op, rs );
408         }
409
410         op->o_bd = b0;
411         op->o_bd->bd_info = bi0;
412         if (gs.matched)
413                 free (gs.matched);
414         if (gs.refs)
415                 ber_bvarray_free(gs.refs);
416         return rs->sr_err;
417 }
418
419 static BackendDB toolDB;
420
421 static int
422 glue_tool_entry_open (
423         BackendDB *b0,
424         int mode
425 )
426 {
427         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
428
429         /* We don't know which backend to talk to yet, so just
430          * remember the mode and move on...
431          */
432
433         glueMode = mode;
434         glueBack = NULL;
435         toolDB = *b0;
436         toolDB.bd_info = oi->oi_orig;
437
438         return 0;
439 }
440
441 static int
442 glue_tool_entry_close (
443         BackendDB *b0
444 )
445 {
446         int rc = 0;
447
448         if (glueBack) {
449                 if (!glueBack->be_entry_close)
450                         return 0;
451                 rc = glueBack->be_entry_close (glueBack);
452         }
453         return rc;
454 }
455
456 static slap_overinst *
457 glue_tool_inst(
458         BackendInfo *bi
459 )
460 {
461         slap_overinfo   *oi = (slap_overinfo *)bi;
462         slap_overinst   *on;
463
464         for ( on = oi->oi_list; on; on=on->on_next ) {
465                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
466                         return on;
467         }
468         return NULL;
469 }
470
471 /* This function will only be called in tool mode */
472 static int
473 glue_open (
474         BackendInfo *bi
475 )
476 {
477         slap_overinst *on = glue_tool_inst( bi );
478         glueinfo                *gi = on->on_bi.bi_private;
479         static int glueOpened = 0;
480         int i, rc = 0;
481
482         if (glueOpened) return 0;
483
484         glueOpened = 1;
485
486         /* If we were invoked in tool mode, open all the underlying backends */
487         if (slapMode & SLAP_TOOL_MODE) {
488                 rc = backend_startup( NULL );
489         } /* other case is impossible */
490         return rc;
491 }
492
493 /* This function will only be called in tool mode */
494 static int
495 glue_close (
496         BackendInfo *bi
497 )
498 {
499         slap_overinst *on = glue_tool_inst( bi );
500         glueinfo                *gi = on->on_bi.bi_private;
501         static int glueClosed = 0;
502         int i, rc = 0;
503
504         if (glueClosed) return 0;
505
506         glueClosed = 1;
507
508         if (slapMode & SLAP_TOOL_MODE) {
509                 rc = backend_shutdown( NULL );
510         }
511         return rc;
512 }
513
514 static int
515 glue_entry_release_rw (
516         Operation *op,
517         Entry *e,
518         int rw
519 )
520 {
521         BackendDB *b0, b2;
522         int rc;
523
524         b0 = op->o_bd;
525         b2 = *op->o_bd;
526         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
527         op->o_bd = glue_back_select (&b2, &e->e_nname);
528
529         rc = op->o_bd->be_release( op, e, rw );
530         op->o_bd = b0;
531         return rc;
532 }
533
534 static ID
535 glue_tool_entry_first (
536         BackendDB *b0
537 )
538 {
539         slap_overinst   *on = glue_tool_inst( b0->bd_info );
540         glueinfo                *gi = on->on_bi.bi_private;
541         int i;
542
543         /* If we're starting from scratch, start at the most general */
544         if (!glueBack) {
545                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
546                         glueBack = &toolDB;
547                 } else {
548                         for (i = gi->gi_nodes-1; i >= 0; i--) {
549                                 if (gi->gi_n[i].gn_be->be_entry_open &&
550                                         gi->gi_n[i].gn_be->be_entry_first) {
551                                                 glueBack = gi->gi_n[i].gn_be;
552                                         break;
553                                 }
554                         }
555                 }
556         }
557         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
558                 glueBack->be_entry_open (glueBack, glueMode) != 0)
559                 return NOID;
560
561         return glueBack->be_entry_first (glueBack);
562 }
563
564 static ID
565 glue_tool_entry_next (
566         BackendDB *b0
567 )
568 {
569         slap_overinst   *on = glue_tool_inst( b0->bd_info );
570         glueinfo                *gi = on->on_bi.bi_private;
571         int i;
572         ID rc;
573
574         if (!glueBack || !glueBack->be_entry_next)
575                 return NOID;
576
577         rc = glueBack->be_entry_next (glueBack);
578
579         /* If we ran out of entries in one database, move on to the next */
580         while (rc == NOID) {
581                 if ( glueBack && glueBack->be_entry_close )
582                         glueBack->be_entry_close (glueBack);
583                 for (i=0; i<gi->gi_nodes; i++) {
584                         if (gi->gi_n[i].gn_be == glueBack)
585                                 break;
586                 }
587                 if (i == 0) {
588                         glueBack = NULL;
589                         break;
590                 } else {
591                         glueBack = gi->gi_n[i-1].gn_be;
592                         rc = glue_tool_entry_first (b0);
593                 }
594         }
595         return rc;
596 }
597
598 static Entry *
599 glue_tool_entry_get (
600         BackendDB *b0,
601         ID id
602 )
603 {
604         if (!glueBack || !glueBack->be_entry_get)
605                 return NULL;
606
607         return glueBack->be_entry_get (glueBack, id);
608 }
609
610 static ID
611 glue_tool_entry_put (
612         BackendDB *b0,
613         Entry *e,
614         struct berval *text
615 )
616 {
617         BackendDB *be, b2;
618         int rc;
619
620         b2 = *b0;
621         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
622         be = glue_back_select (&b2, &e->e_nname);
623         if ( be == &b2 ) be = &toolDB;
624
625         if (!be->be_entry_put)
626                 return NOID;
627
628         if (!glueBack) {
629                 rc = be->be_entry_open (be, glueMode);
630                 if (rc != 0)
631                         return NOID;
632         } else if (be != glueBack) {
633                 /* If this entry belongs in a different branch than the
634                  * previous one, close the current database and open the
635                  * new one.
636                  */
637                 glueBack->be_entry_close (glueBack);
638                 rc = be->be_entry_open (be, glueMode);
639                 if (rc != 0)
640                         return NOID;
641         }
642         glueBack = be;
643         return be->be_entry_put (be, e, text);
644 }
645
646 static int
647 glue_tool_entry_reindex (
648         BackendDB *b0,
649         ID id
650 )
651 {
652         if (!glueBack || !glueBack->be_entry_reindex)
653                 return -1;
654
655         return glueBack->be_entry_reindex (glueBack, id);
656 }
657
658 static int
659 glue_tool_sync (
660         BackendDB *b0
661 )
662 {
663         slap_overinst   *on = glue_tool_inst( b0->bd_info );
664         glueinfo                *gi = on->on_bi.bi_private;
665         BackendInfo             *bi = b0->bd_info;
666         int i;
667
668         /* just sync everyone */
669         for (i = 0; i<gi->gi_nodes; i++)
670                 if (gi->gi_n[i].gn_be->be_sync)
671                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
672         b0->bd_info = on->on_info->oi_orig;
673         if ( b0->be_sync )
674                 b0->be_sync( b0 );
675         b0->bd_info = bi;
676         return 0;
677 }
678
679 static int
680 glue_db_init(
681         BackendDB *be
682 )
683 {
684         slap_overinst   *on = (slap_overinst *)be->bd_info;
685         slap_overinfo   *oi = on->on_info;
686         glueinfo *gi;
687
688         gi = ch_calloc( 1, sizeof(glueinfo));
689         on->on_bi.bi_private = gi;
690         dnParent( be->be_nsuffix, &gi->gi_pdn );
691
692         /* Currently the overlay framework doesn't handle these entry points
693          * but we need them....
694          */
695         oi->oi_bi.bi_open = glue_open;
696         oi->oi_bi.bi_close = glue_close;
697
698         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
699
700         oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
701         oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
702         oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
703         oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
704         oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
705         oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
706         oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
707         oi->oi_bi.bi_tool_sync = glue_tool_sync;
708
709         /*FIXME : need to add support */
710         oi->oi_bi.bi_tool_dn2id_get = 0;
711         oi->oi_bi.bi_tool_id2entry_get = 0;
712         oi->oi_bi.bi_tool_entry_modify = 0;
713
714         return 0;
715 }
716
717 static int
718 glue_db_destroy (
719         BackendDB *be
720 )
721 {
722         slap_overinst   *on = (slap_overinst *)be->bd_info;
723         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
724
725         free (gi);
726         return SLAP_CB_CONTINUE;
727 }
728
729 static int
730 glue_db_open (
731         BackendDB *be
732 )
733 {
734         slap_overinst   *on = (slap_overinst *)be->bd_info;
735         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
736         int i;
737
738         for ( i=0; i<gi->gi_nodes; i++ ) {
739                 int j;
740
741                 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
742         }
743         return 0;
744 }
745
746 static int
747 glue_db_close( 
748         BackendDB *be
749 )
750 {
751         slap_overinst   *on = (slap_overinst *)be->bd_info;
752
753         on->on_info->oi_bi.bi_db_close = NULL;
754         return 0;
755 }
756
757 static int
758 glue_db_config(
759         BackendDB       *be,
760         const char      *fname,
761         int             lineno,
762         int             argc,
763         char    **argv
764 )
765 {
766         slap_overinst   *on = (slap_overinst *)be->bd_info;
767         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
768
769         /* redundant; could be applied just once */
770         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
771
772         if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
773                 int i, async = 0, advertise = 0;
774                 BackendDB *b2;
775                 struct berval bv, dn;
776                 gluenode *gn;
777
778                 if ( argc < 2 ) {
779                         fprintf( stderr, "%s: line %d: too few arguments in "
780                                 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
781                         return -1;
782                 }
783                 for ( i = 2; i < argc; i++ ) {
784                         if ( strcasecmp( argv[i], "async" ) == 0 ) {
785                                 async = 1;
786
787                         } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
788                                 advertise = 1;
789
790                         } else {
791                                 fprintf( stderr, "%s: line %d: unrecognized option "
792                                         "\"%s\" ignored.\n", fname, lineno, argv[i] );
793                         }
794                 }
795                 ber_str2bv( argv[1], 0, 0, &bv );
796                 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
797                         fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
798                         return -1;
799                 }
800                 b2 = select_backend( &dn, 0, 1 );
801                 if ( !b2 ) {
802                         fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
803                                 fname, lineno, argv[1] );
804                         return -1;
805                 }
806                 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
807                 if ( advertise ) {
808                         SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
809                 }
810                 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
811                         gi->gi_nodes * sizeof(gluenode));
812                 gi->gi_n[gi->gi_nodes].gn_bx = b2 - backendDB;
813                 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
814                 gi->gi_n[gi->gi_nodes].gn_async = async;
815                 gi->gi_nodes++;
816                 on->on_bi.bi_private = gi;
817                 return 0;
818         }
819         return SLAP_CONF_UNKNOWN;
820 }
821
822 int
823 glue_init()
824 {
825         glue.on_bi.bi_type = "glue";
826
827         glue.on_bi.bi_db_init = glue_db_init;
828         glue.on_bi.bi_db_config = glue_db_config;
829         glue.on_bi.bi_db_open = glue_db_open;
830         glue.on_bi.bi_db_close = glue_db_close;
831         glue.on_bi.bi_db_destroy = glue_db_destroy;
832
833         glue.on_bi.bi_op_search = glue_op_search;
834         glue.on_bi.bi_op_modify = glue_op_func;
835         glue.on_bi.bi_op_modrdn = glue_op_func;
836         glue.on_bi.bi_op_add = glue_op_func;
837         glue.on_bi.bi_op_delete = glue_op_func;
838
839         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
840         glue.on_bi.bi_chk_controls = glue_chk_controls;
841
842         return overlay_register( &glue );
843 }
844
845 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
846 int
847 init_module( int argc, char *argv[] )
848 {
849         return glue_init();
850 }
851 #endif  /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
852
853 #endif  /* defined(SLAPD_OVER_GLUE */