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