]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/glue.c
fix arbitrary precision number display when no big number library is available
[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         struct berval gn_pdn;
44         int gn_async;
45 } gluenode;
46
47 typedef struct glueinfo {
48         int gi_nodes;
49         struct berval gi_pdn;
50         gluenode gi_n[1];
51 } glueinfo;
52
53 static slap_overinst    glue;
54
55 static int glueMode;
56 static BackendDB *glueBack;
57
58 static slap_response glue_op_response;
59
60 /* Just like select_backend, but only for our backends */
61 static BackendDB *
62 glue_back_select (
63         BackendDB *be,
64         struct berval *dn
65 )
66 {
67         slap_overinst   *on = (slap_overinst *)be->bd_info;
68         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
69         int i;
70
71         for (i = 0; i<gi->gi_nodes; i++) {
72                 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
73
74                 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
75                         return gi->gi_n[i].gn_be;
76                 }
77         }
78         be->bd_info = on->on_info->oi_orig;
79         return be;
80 }
81
82
83 typedef struct glue_state {
84         int err;
85         int slimit;
86         int matchlen;
87         char *matched;
88         int nrefs;
89         BerVarray refs;
90 } glue_state;
91
92 static int
93 glue_op_response ( Operation *op, SlapReply *rs )
94 {
95         glue_state *gs = op->o_callback->sc_private;
96
97         switch(rs->sr_type) {
98         case REP_SEARCH:
99                 if ( gs->slimit != SLAP_NO_LIMIT
100                                 && rs->sr_nentries >= gs->slimit )
101                 {
102                         rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
103                         return -1;
104                 }
105                 /* fallthru */
106         case REP_SEARCHREF:
107                 return SLAP_CB_CONTINUE;
108
109         default:
110                 if (rs->sr_err == LDAP_SUCCESS ||
111                         rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
112                         rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
113                         rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
114                         rs->sr_err == LDAP_NO_SUCH_OBJECT ||
115                         gs->err != LDAP_SUCCESS)
116                         gs->err = rs->sr_err;
117                 if (gs->err == LDAP_SUCCESS && gs->matched) {
118                         ch_free (gs->matched);
119                         gs->matched = NULL;
120                         gs->matchlen = 0;
121                 }
122                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
123                         int len;
124                         len = strlen (rs->sr_matched);
125                         if (len > gs->matchlen) {
126                                 if (gs->matched)
127                                         ch_free (gs->matched);
128                                 gs->matched = ch_strdup (rs->sr_matched);
129                                 gs->matchlen = len;
130                         }
131                 }
132                 if (rs->sr_ref) {
133                         int i, j, k;
134                         BerVarray new;
135
136                         for (i=0; rs->sr_ref[i].bv_val; i++);
137
138                         j = gs->nrefs;
139                         if (!j) {
140                                 new = ch_malloc ((i+1)*sizeof(struct berval));
141                         } else {
142                                 new = ch_realloc(gs->refs,
143                                         (j+i+1)*sizeof(struct berval));
144                         }
145                         for (k=0; k<i; j++,k++) {
146                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
147                         }
148                         new[j].bv_val = NULL;
149                         gs->nrefs = j;
150                         gs->refs = new;
151                 }
152         }
153         return 0;
154 }
155
156 enum glue_which {
157         op_modify = 0,
158         op_modrdn,
159         op_add,
160         op_delete
161 };
162
163 static int
164 glue_op_func ( Operation *op, SlapReply *rs )
165 {
166         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
167         BackendDB *b0 = op->o_bd;
168         BackendInfo *bi0 = op->o_bd->bd_info;
169         BI_op_modify **func;
170         enum glue_which which;
171         int rc;
172
173         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
174         b0->bd_info = on->on_info->oi_orig;
175
176         switch(op->o_tag) {
177         case LDAP_REQ_ADD: which = op_add; break;
178         case LDAP_REQ_DELETE: which = op_delete; break;
179         case LDAP_REQ_MODIFY: which = op_modify; break;
180         case LDAP_REQ_MODRDN: which = op_modrdn; break;
181         }
182
183         func = &op->o_bd->bd_info->bi_op_modify;
184         if ( func[which] )
185                 rc = func[which]( op, rs );
186         else
187                 rc = SLAP_CB_CONTINUE;
188
189         op->o_bd = b0;
190         op->o_bd->bd_info = bi0;
191         return rc;
192 }
193
194 static int
195 glue_chk_referrals ( Operation *op, SlapReply *rs )
196 {
197         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
198         BackendDB *b0 = op->o_bd;
199         BackendInfo *bi0 = op->o_bd->bd_info;
200         int rc;
201
202         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
203         b0->bd_info = on->on_info->oi_orig;
204
205         if ( op->o_bd->bd_info->bi_chk_referrals )
206                 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
207         else
208                 rc = SLAP_CB_CONTINUE;
209
210         op->o_bd = b0;
211         op->o_bd->bd_info = bi0;
212         return rc;
213 }
214
215 static int
216 glue_chk_controls ( Operation *op, SlapReply *rs )
217 {
218         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
219         BackendDB *b0 = op->o_bd;
220         BackendInfo *bi0 = op->o_bd->bd_info;
221         int rc = SLAP_CB_CONTINUE;
222
223         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
224         b0->bd_info = on->on_info->oi_orig;
225
226         /* if the subordinate database has overlays, the bi_chk_controls()
227          * hook is actually over_aux_chk_controls(); in case it actually
228          * wraps a missing hok, we need to mimic the behavior
229          * of the frontend applied to that database */
230         if ( op->o_bd->bd_info->bi_chk_controls ) {
231                 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
232         }
233
234         
235         if ( rc == SLAP_CB_CONTINUE ) {
236                 rc = backend_check_controls( op, rs );
237         }
238
239         op->o_bd = b0;
240         op->o_bd->bd_info = bi0;
241         return rc;
242 }
243
244 static int
245 glue_op_search ( Operation *op, SlapReply *rs )
246 {
247         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
248         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
249         BackendDB *b0 = op->o_bd;
250         BackendDB *b1 = NULL, *btmp;
251         BackendInfo *bi0 = op->o_bd->bd_info;
252         int i;
253         long stoptime = 0;
254         glue_state gs = {0, 0, 0, NULL, 0, NULL};
255         slap_callback cb = { NULL, glue_op_response, NULL, NULL };
256         int scope0, slimit0, tlimit0;
257         struct berval dn, ndn, *pdn;
258
259         cb.sc_private = &gs;
260
261         cb.sc_next = op->o_callback;
262
263         stoptime = slap_get_time () + op->ors_tlimit;
264
265         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
266         b0->bd_info = on->on_info->oi_orig;
267
268         switch (op->ors_scope) {
269         case LDAP_SCOPE_BASE:
270                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
271                 if (op->o_bd && op->o_bd->be_search) {
272                         rs->sr_err = op->o_bd->be_search( op, rs );
273                 }
274                 return rs->sr_err;
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 != NULL );
342                         assert( op->o_bd->be_nsuffix != NULL );
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, j, same, bsame = 0, 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                 for (i = 0; i<gi->gi_nodes; i++) {
489                         same = 0;
490                         /* Same type as our main backend? */
491                         if ( gi->gi_n[i].gn_be->bd_info == on->on_info->oi_orig )
492                                 bsame = 1;
493
494                         /* Loop thru the bd_info's and make sure we only
495                          * invoke their bi_open functions once each.
496                          */
497                         for ( j = 0; j<i; j++ ) {
498                                 if ( gi->gi_n[i].gn_be->bd_info ==
499                                         gi->gi_n[j].gn_be->bd_info ) {
500                                         same = 1;
501                                         break;
502                                 }
503                         }
504                         /* OK, it's unique and non-NULL, call it. */
505                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
506                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
507                                         gi->gi_n[i].gn_be->bd_info );
508                         /* Let backend.c take care of the rest of startup */
509                         if ( !rc )
510                                 rc = backend_startup_one( gi->gi_n[i].gn_be );
511                         if ( rc ) break;
512                 }
513                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
514                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
515
516         } /* other case is impossible */
517         return rc;
518 }
519
520 /* This function will only be called in tool mode */
521 static int
522 glue_close (
523         BackendInfo *bi
524 )
525 {
526         slap_overinst *on = glue_tool_inst( bi );
527         static int glueClosed = 0;
528         int rc = 0;
529
530         if (glueClosed) return 0;
531
532         glueClosed = 1;
533
534         if (slapMode & SLAP_TOOL_MODE) {
535                 rc = backend_shutdown( NULL );
536         }
537         return rc;
538 }
539
540 static int
541 glue_entry_release_rw (
542         Operation *op,
543         Entry *e,
544         int rw
545 )
546 {
547         BackendDB *b0, b2;
548         int rc = -1;
549
550         b0 = op->o_bd;
551         b2 = *op->o_bd;
552         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
553         op->o_bd = glue_back_select (&b2, &e->e_nname);
554
555         if ( op->o_bd->be_release ) {
556                 rc = op->o_bd->be_release( op, e, rw );
557
558         } else {
559                 /* FIXME: mimic be_entry_release_rw
560                  * when no be_release() available */
561                 /* free entry */
562                 entry_free( e );
563                 rc = 0;
564         }
565         op->o_bd = b0;
566         return rc;
567 }
568
569 static ID
570 glue_tool_entry_first (
571         BackendDB *b0
572 )
573 {
574         slap_overinst   *on = glue_tool_inst( b0->bd_info );
575         glueinfo                *gi = on->on_bi.bi_private;
576         int i;
577
578         /* If we're starting from scratch, start at the most general */
579         if (!glueBack) {
580                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
581                         glueBack = &toolDB;
582                 } else {
583                         for (i = gi->gi_nodes-1; i >= 0; i--) {
584                                 if (gi->gi_n[i].gn_be->be_entry_open &&
585                                         gi->gi_n[i].gn_be->be_entry_first) {
586                                                 glueBack = gi->gi_n[i].gn_be;
587                                         break;
588                                 }
589                         }
590                 }
591         }
592         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
593                 glueBack->be_entry_open (glueBack, glueMode) != 0)
594                 return NOID;
595
596         return glueBack->be_entry_first (glueBack);
597 }
598
599 static ID
600 glue_tool_entry_next (
601         BackendDB *b0
602 )
603 {
604         slap_overinst   *on = glue_tool_inst( b0->bd_info );
605         glueinfo                *gi = on->on_bi.bi_private;
606         int i;
607         ID rc;
608
609         if (!glueBack || !glueBack->be_entry_next)
610                 return NOID;
611
612         rc = glueBack->be_entry_next (glueBack);
613
614         /* If we ran out of entries in one database, move on to the next */
615         while (rc == NOID) {
616                 if ( glueBack && glueBack->be_entry_close )
617                         glueBack->be_entry_close (glueBack);
618                 for (i=0; i<gi->gi_nodes; i++) {
619                         if (gi->gi_n[i].gn_be == glueBack)
620                                 break;
621                 }
622                 if (i == 0) {
623                         glueBack = NULL;
624                         break;
625                 } else {
626                         glueBack = gi->gi_n[i-1].gn_be;
627                         rc = glue_tool_entry_first (b0);
628                 }
629         }
630         return rc;
631 }
632
633 static Entry *
634 glue_tool_entry_get (
635         BackendDB *b0,
636         ID id
637 )
638 {
639         if (!glueBack || !glueBack->be_entry_get)
640                 return NULL;
641
642         return glueBack->be_entry_get (glueBack, id);
643 }
644
645 static ID
646 glue_tool_entry_put (
647         BackendDB *b0,
648         Entry *e,
649         struct berval *text
650 )
651 {
652         BackendDB *be, b2;
653         int rc = -1;
654
655         b2 = *b0;
656         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
657         be = glue_back_select (&b2, &e->e_nname);
658         if ( be == &b2 ) be = &toolDB;
659
660         if (!be->be_entry_put)
661                 return NOID;
662
663         if (!glueBack) {
664                 if ( be->be_entry_open ) {
665                         rc = be->be_entry_open (be, glueMode);
666                 }
667                 if (rc != 0) {
668                         return NOID;
669                 }
670         } else if (be != glueBack) {
671                 /* If this entry belongs in a different branch than the
672                  * previous one, close the current database and open the
673                  * new one.
674                  */
675                 if ( glueBack->be_entry_close ) {
676                         glueBack->be_entry_close (glueBack);
677                 }
678                 if ( be->be_entry_open ) {
679                         rc = be->be_entry_open (be, glueMode);
680                 }
681                 if (rc != 0) {
682                         return NOID;
683                 }
684         }
685         glueBack = be;
686         return be->be_entry_put (be, e, text);
687 }
688
689 static int
690 glue_tool_entry_reindex (
691         BackendDB *b0,
692         ID id
693 )
694 {
695         if (!glueBack || !glueBack->be_entry_reindex)
696                 return -1;
697
698         return glueBack->be_entry_reindex (glueBack, id);
699 }
700
701 static int
702 glue_tool_sync (
703         BackendDB *b0
704 )
705 {
706         slap_overinst   *on = glue_tool_inst( b0->bd_info );
707         glueinfo                *gi = on->on_bi.bi_private;
708         BackendInfo             *bi = b0->bd_info;
709         int i;
710
711         /* just sync everyone */
712         for (i = 0; i<gi->gi_nodes; i++)
713                 if (gi->gi_n[i].gn_be->be_sync)
714                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
715         b0->bd_info = on->on_info->oi_orig;
716         if ( b0->be_sync )
717                 b0->be_sync( b0 );
718         b0->bd_info = bi;
719         return 0;
720 }
721
722 static int
723 glue_db_init(
724         BackendDB *be
725 )
726 {
727         slap_overinst   *on = (slap_overinst *)be->bd_info;
728         slap_overinfo   *oi = on->on_info;
729         glueinfo *gi;
730
731         gi = ch_calloc( 1, sizeof(glueinfo));
732         on->on_bi.bi_private = gi;
733         dnParent( be->be_nsuffix, &gi->gi_pdn );
734
735         /* Currently the overlay framework doesn't handle these entry points
736          * but we need them....
737          */
738         oi->oi_bi.bi_open = glue_open;
739         oi->oi_bi.bi_close = glue_close;
740
741         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
742
743         oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
744         oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
745         oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
746         oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
747         oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
748         oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
749         oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
750         oi->oi_bi.bi_tool_sync = glue_tool_sync;
751
752         /*FIXME : need to add support */
753         oi->oi_bi.bi_tool_dn2id_get = 0;
754         oi->oi_bi.bi_tool_id2entry_get = 0;
755         oi->oi_bi.bi_tool_entry_modify = 0;
756
757         return 0;
758 }
759
760 static int
761 glue_db_destroy (
762         BackendDB *be
763 )
764 {
765         slap_overinst   *on = (slap_overinst *)be->bd_info;
766         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
767
768         free (gi);
769         return SLAP_CB_CONTINUE;
770 }
771
772 static int
773 glue_db_close( 
774         BackendDB *be
775 )
776 {
777         slap_overinst   *on = (slap_overinst *)be->bd_info;
778
779         on->on_info->oi_bi.bi_db_close = NULL;
780         return 0;
781 }
782
783 static int
784 glue_db_config(
785         BackendDB       *be,
786         const char      *fname,
787         int             lineno,
788         int             argc,
789         char    **argv
790 )
791 {
792         slap_overinst   *on = (slap_overinst *)be->bd_info;
793         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
794
795         /* redundant; could be applied just once */
796         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
797
798         if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
799                 int i, async = 0, advertise = 0;
800                 BackendDB *b2;
801                 struct berval bv, dn;
802
803                 if ( argc < 2 ) {
804                         fprintf( stderr, "%s: line %d: too few arguments in "
805                                 "\"glue-sub <suffixDN> [async] [advertise]\"\n", fname, lineno );
806                         return -1;
807                 }
808                 for ( i = 2; i < argc; i++ ) {
809                         if ( strcasecmp( argv[i], "async" ) == 0 ) {
810                                 async = 1;
811
812                         } else if ( strcasecmp( argv[i], "advertise" ) == 0 ) {
813                                 advertise = 1;
814
815                         } else {
816                                 fprintf( stderr, "%s: line %d: unrecognized option "
817                                         "\"%s\" ignored.\n", fname, lineno, argv[i] );
818                         }
819                 }
820                 ber_str2bv( argv[1], 0, 0, &bv );
821                 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
822                         fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
823                         return -1;
824                 }
825                 b2 = select_backend( &dn, 0, 1 );
826                 if ( !b2 ) {
827                         fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
828                                 fname, lineno, argv[1] );
829                         return -1;
830                 }
831                 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
832                 if ( advertise ) {
833                         SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_ADVERTISE;
834                 }
835                 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
836                         gi->gi_nodes * sizeof(gluenode));
837                 gi->gi_n[gi->gi_nodes].gn_be = b2;
838                 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
839                 gi->gi_n[gi->gi_nodes].gn_async = async;
840                 gi->gi_nodes++;
841                 on->on_bi.bi_private = gi;
842                 return 0;
843         }
844         return SLAP_CONF_UNKNOWN;
845 }
846
847 int
848 glue_init()
849 {
850         glue.on_bi.bi_type = "glue";
851
852         glue.on_bi.bi_db_init = glue_db_init;
853         glue.on_bi.bi_db_config = glue_db_config;
854         glue.on_bi.bi_db_close = glue_db_close;
855         glue.on_bi.bi_db_destroy = glue_db_destroy;
856
857         glue.on_bi.bi_op_search = glue_op_search;
858         glue.on_bi.bi_op_modify = glue_op_func;
859         glue.on_bi.bi_op_modrdn = glue_op_func;
860          glue.on_bi.bi_op_add = glue_op_func;
861         glue.on_bi.bi_op_delete = glue_op_func;
862
863         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
864         glue.on_bi.bi_chk_controls = glue_chk_controls;
865
866         return overlay_register( &glue );
867 }
868
869 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
870 int
871 init_module( int argc, char *argv[] )
872 {
873         return glue_init();
874 }
875 #endif  /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
876
877 #endif  /* defined(SLAPD_OVER_GLUE */