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