]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/glue.c
looks a little better (but test033 doesn't work any more; disabling)
[openldap] / servers / slapd / overlays / glue.c
1 /* glue.c - backend glue overlay */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 /*
18  * Functions to glue a bunch of other backends into a single tree.
19  * All of the glued backends must share a common suffix. E.g., you
20  * can glue o=foo and ou=bar,o=foo but you can't glue o=foo and o=bar.
21  *
22  * The purpose of these functions is to allow you to split a single database
23  * into pieces (for load balancing purposes, whatever) but still be able
24  * to treat it as a single database after it's been split. As such, each
25  * of the glued backends should have identical rootdn.
26  *  -- Howard Chu
27  */
28
29 #include "portable.h"
30
31 #ifdef SLAPD_OVER_GLUE
32
33 #include <stdio.h>
34
35 #include <ac/string.h>
36 #include <ac/socket.h>
37
38 #define SLAPD_TOOLS
39 #include "slap.h"
40
41 typedef struct gluenode {
42         BackendDB *gn_be;
43         int     gn_bx;
44         struct berval gn_pdn;
45         int gn_async;
46 } gluenode;
47
48 typedef struct glueinfo {
49         int gi_nodes;
50         struct berval gi_pdn;
51         gluenode gi_n[1];
52 } glueinfo;
53
54 static slap_overinst    glue;
55
56 static int glueMode;
57 static BackendDB *glueBack;
58
59 static slap_response glue_op_response;
60
61 /* Just like select_backend, but only for our backends */
62 static BackendDB *
63 glue_back_select (
64         BackendDB *be,
65         struct berval *dn
66 )
67 {
68         slap_overinst   *on = (slap_overinst *)be->bd_info;
69         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
70         int i;
71
72         for (i = 0; i<gi->gi_nodes; i++) {
73                 assert( gi->gi_n[i].gn_be->be_nsuffix );
74
75                 if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
76                         return gi->gi_n[i].gn_be;
77                 }
78         }
79         be->bd_info = on->on_info->oi_orig;
80         return be;
81 }
82
83
84 typedef struct glue_state {
85         int err;
86         int slimit;
87         int matchlen;
88         char *matched;
89         int nrefs;
90         BerVarray refs;
91 } glue_state;
92
93 static int
94 glue_op_response ( Operation *op, SlapReply *rs )
95 {
96         glue_state *gs = op->o_callback->sc_private;
97
98         switch(rs->sr_type) {
99         case REP_SEARCH:
100                 if ( gs->slimit != SLAP_NO_LIMIT
101                                 && rs->sr_nentries >= gs->slimit )
102                 {
103                         rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
104                         return -1;
105                 }
106                 /* fallthru */
107         case REP_SEARCHREF:
108                 return SLAP_CB_CONTINUE;
109
110         default:
111                 if (rs->sr_err == LDAP_SUCCESS ||
112                         rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
113                         rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
114                         rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
115                         rs->sr_err == LDAP_NO_SUCH_OBJECT ||
116                         gs->err != LDAP_SUCCESS)
117                         gs->err = rs->sr_err;
118                 if (gs->err == LDAP_SUCCESS && gs->matched) {
119                         ch_free (gs->matched);
120                         gs->matched = NULL;
121                         gs->matchlen = 0;
122                 }
123                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
124                         int len;
125                         len = strlen (rs->sr_matched);
126                         if (len > gs->matchlen) {
127                                 if (gs->matched)
128                                         ch_free (gs->matched);
129                                 gs->matched = ch_strdup (rs->sr_matched);
130                                 gs->matchlen = len;
131                         }
132                 }
133                 if (rs->sr_ref) {
134                         int i, j, k;
135                         BerVarray new;
136
137                         for (i=0; rs->sr_ref[i].bv_val; i++);
138
139                         j = gs->nrefs;
140                         if (!j) {
141                                 new = ch_malloc ((i+1)*sizeof(struct berval));
142                         } else {
143                                 new = ch_realloc(gs->refs,
144                                         (j+i+1)*sizeof(struct berval));
145                         }
146                         for (k=0; k<i; j++,k++) {
147                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
148                         }
149                         new[j].bv_val = NULL;
150                         gs->nrefs = j;
151                         gs->refs = new;
152                 }
153         }
154         return 0;
155 }
156
157 enum glue_which {
158         op_modify = 0,
159         op_modrdn,
160         op_add,
161         op_delete
162 };
163
164 static int
165 glue_op_func ( Operation *op, SlapReply *rs )
166 {
167         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
168         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
169         BackendDB *b0 = op->o_bd;
170         BackendInfo *bi0 = op->o_bd->bd_info;
171         BI_op_modify **func;
172         enum glue_which which;
173         int rc;
174
175         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
176         b0->bd_info = on->on_info->oi_orig;
177
178         switch(op->o_tag) {
179         case LDAP_REQ_ADD: which = op_add; break;
180         case LDAP_REQ_DELETE: which = op_delete; break;
181         case LDAP_REQ_MODIFY: which = op_modify; break;
182         case LDAP_REQ_MODRDN: which = op_modrdn; break;
183         }
184
185         func = &op->o_bd->bd_info->bi_op_modify;
186         if ( func[which] )
187                 rc = func[which]( op, rs );
188         else
189                 rc = SLAP_CB_CONTINUE;
190
191         op->o_bd = b0;
192         op->o_bd->bd_info = bi0;
193         return rc;
194 }
195
196 static int
197 glue_chk_referrals ( Operation *op, SlapReply *rs )
198 {
199         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
200         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
201         BackendDB *b0 = op->o_bd;
202         BackendInfo *bi0 = op->o_bd->bd_info;
203         int rc;
204
205         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
206         b0->bd_info = on->on_info->oi_orig;
207
208         if ( op->o_bd->bd_info->bi_chk_referrals )
209                 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
210         else
211                 rc = SLAP_CB_CONTINUE;
212
213         op->o_bd = b0;
214         op->o_bd->bd_info = bi0;
215         return rc;
216 }
217
218 static int
219 glue_chk_controls ( Operation *op, SlapReply *rs )
220 {
221         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
222         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
223         BackendDB *b0 = op->o_bd;
224         BackendInfo *bi0 = op->o_bd->bd_info;
225         int rc;
226
227         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
228         b0->bd_info = on->on_info->oi_orig;
229
230         if ( op->o_bd->bd_info->bi_chk_controls )
231                 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
232         else
233                 rc = SLAP_CB_CONTINUE;
234
235         op->o_bd = b0;
236         op->o_bd->bd_info = bi0;
237         return rc;
238 }
239
240 static int
241 glue_op_search ( Operation *op, SlapReply *rs )
242 {
243         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
244         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
245         BackendDB *b0 = op->o_bd;
246         BackendDB *b1 = NULL, *btmp;
247         BackendInfo *bi0 = op->o_bd->bd_info;
248         int i;
249         long stoptime = 0;
250         glue_state gs = {0, 0, 0, NULL, 0, NULL};
251         slap_callback cb = { NULL, glue_op_response, NULL, NULL };
252         int scope0, slimit0, tlimit0;
253         struct berval dn, ndn, *pdn;
254
255         cb.sc_private = &gs;
256
257         cb.sc_next = op->o_callback;
258
259         stoptime = slap_get_time () + op->ors_tlimit;
260
261         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
262         b0->bd_info = on->on_info->oi_orig;
263
264         switch (op->ors_scope) {
265         case LDAP_SCOPE_BASE:
266                 return SLAP_CB_CONTINUE;
267
268         case LDAP_SCOPE_ONELEVEL:
269         case LDAP_SCOPE_SUBTREE:
270 #ifdef LDAP_SCOPE_SUBORDINATE
271         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
272 #endif
273
274 #if 0
275                 if ( op->o_sync ) {
276                         if (op->o_bd && op->o_bd->be_search) {
277                                 rs->sr_err = op->o_bd->be_search( op, rs );
278                         } else {
279                                 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
280                                                 "No search target found");
281                         }
282                         return rs->sr_err;
283                 }
284 #endif
285
286                 op->o_callback = &cb;
287                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
288                 scope0 = op->ors_scope;
289                 slimit0 = gs.slimit = op->ors_slimit;
290                 tlimit0 = op->ors_tlimit;
291                 dn = op->o_req_dn;
292                 ndn = op->o_req_ndn;
293                 b1 = op->o_bd;
294
295                 /*
296                  * Execute in reverse order, most general first 
297                  */
298                 for (i = gi->gi_nodes; i >= 0; i--) {
299                         if ( i == gi->gi_nodes ) {
300                                 btmp = b0;
301                                 pdn = &gi->gi_pdn;
302                         } else {
303                                 btmp = gi->gi_n[i].gn_be;
304                                 pdn = &gi->gi_n[i].gn_pdn;
305                         }
306                         if (!btmp || !btmp->be_search)
307                                 continue;
308                         if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
309                                 continue;
310                         if (tlimit0 != SLAP_NO_LIMIT) {
311                                 op->ors_tlimit = stoptime - slap_get_time ();
312                                 if (op->ors_tlimit <= 0) {
313                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
314                                         break;
315                                 }
316                         }
317                         if (slimit0 != SLAP_NO_LIMIT) {
318                                 op->ors_slimit = slimit0 - rs->sr_nentries;
319                                 if (op->ors_slimit < 0) {
320                                         rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
321                                         break;
322                                 }
323                         }
324                         rs->sr_err = 0;
325                         /*
326                          * check for abandon 
327                          */
328                         if (op->o_abandon) {
329                                 goto end_of_loop;
330                         }
331                         op->o_bd = btmp;
332
333                         assert( op->o_bd->be_suffix );
334                         assert( op->o_bd->be_nsuffix );
335                         
336                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
337                                 dn_match(pdn, &ndn))
338                         {
339                                 op->ors_scope = LDAP_SCOPE_BASE;
340                                 op->o_req_dn = op->o_bd->be_suffix[0];
341                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
342                                 rs->sr_err = op->o_bd->be_search(op, rs);
343
344                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
345                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
346                         {
347                                 rs->sr_err = op->o_bd->be_search( op, rs );
348
349                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
350                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
351                         {
352                                 op->o_req_dn = op->o_bd->be_suffix[0];
353                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
354                                 rs->sr_err = op->o_bd->be_search( op, rs );
355                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
356                                         gs.err = LDAP_SUCCESS;
357                                 }
358
359                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
360                                 rs->sr_err = op->o_bd->be_search( op, rs );
361                         }
362
363                         switch ( gs.err ) {
364
365                         /*
366                          * Add errors that should result in dropping
367                          * the search
368                          */
369                         case LDAP_SIZELIMIT_EXCEEDED:
370                         case LDAP_TIMELIMIT_EXCEEDED:
371                         case LDAP_ADMINLIMIT_EXCEEDED:
372                         case LDAP_NO_SUCH_OBJECT:
373                                 goto end_of_loop;
374                         
375                         default:
376                                 break;
377                         }
378                 }
379 end_of_loop:;
380                 op->ors_scope = scope0;
381                 op->ors_slimit = slimit0;
382                 op->ors_tlimit = tlimit0;
383                 op->o_req_dn = dn;
384                 op->o_req_ndn = ndn;
385
386                 break;
387         }
388         if ( !op->o_abandon ) {
389                 op->o_callback = cb.sc_next;
390                 rs->sr_err = gs.err;
391                 rs->sr_matched = gs.matched;
392                 rs->sr_ref = gs.refs;
393
394                 send_ldap_result( op, rs );
395         }
396
397         op->o_bd = b0;
398         op->o_bd->bd_info = bi0;
399         if (gs.matched)
400                 free (gs.matched);
401         if (gs.refs)
402                 ber_bvarray_free(gs.refs);
403         return rs->sr_err;
404 }
405
406 static BackendDB toolDB;
407
408 static int
409 glue_tool_entry_open (
410         BackendDB *b0,
411         int mode
412 )
413 {
414         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
415
416         /* We don't know which backend to talk to yet, so just
417          * remember the mode and move on...
418          */
419
420         glueMode = mode;
421         glueBack = NULL;
422         toolDB = *b0;
423         toolDB.bd_info = oi->oi_orig;
424
425         return 0;
426 }
427
428 static int
429 glue_tool_entry_close (
430         BackendDB *b0
431 )
432 {
433         int rc = 0;
434
435         if (glueBack) {
436                 if (!glueBack->be_entry_close)
437                         return 0;
438                 rc = glueBack->be_entry_close (glueBack);
439         }
440         return rc;
441 }
442
443 static slap_overinst *
444 glue_tool_inst(
445         BackendInfo *bi
446 )
447 {
448         slap_overinfo   *oi = (slap_overinfo *)bi;
449         slap_overinst   *on;
450
451         for ( on = oi->oi_list; on; on=on->on_next ) {
452                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
453                         return on;
454         }
455         return NULL;
456 }
457
458 /* This function will only be called in tool mode */
459 static int
460 glue_open (
461         BackendInfo *bi
462 )
463 {
464         slap_overinst *on = glue_tool_inst( bi );
465         glueinfo                *gi = on->on_bi.bi_private;
466         static int glueOpened = 0;
467         int i, rc = 0;
468
469         if (glueOpened) return 0;
470
471         glueOpened = 1;
472
473         /* If we were invoked in tool mode, open all the underlying backends */
474         if (slapMode & SLAP_TOOL_MODE) {
475                 rc = backend_startup( NULL );
476         } /* other case is impossible */
477         return rc;
478 }
479
480 /* This function will only be called in tool mode */
481 static int
482 glue_close (
483         BackendInfo *bi
484 )
485 {
486         slap_overinst *on = glue_tool_inst( bi );
487         glueinfo                *gi = on->on_bi.bi_private;
488         static int glueClosed = 0;
489         int i, rc = 0;
490
491         if (glueClosed) return 0;
492
493         glueClosed = 1;
494
495         if (slapMode & SLAP_TOOL_MODE) {
496                 rc = backend_shutdown( NULL );
497         }
498         return rc;
499 }
500
501 static int
502 glue_entry_release_rw (
503         Operation *op,
504         Entry *e,
505         int rw
506 )
507 {
508         BackendDB *b0, b2;
509         int rc;
510
511         b0 = op->o_bd;
512         b2 = *op->o_bd;
513         b2.bd_info = (BackendInfo *)glue_tool_inst( op->o_bd->bd_info );
514         op->o_bd = glue_back_select (&b2, &e->e_nname);
515
516         rc = op->o_bd->be_release( op, e, rw );
517         op->o_bd = b0;
518         return rc;
519 }
520
521 static ID
522 glue_tool_entry_first (
523         BackendDB *b0
524 )
525 {
526         slap_overinst   *on = glue_tool_inst( b0->bd_info );
527         glueinfo                *gi = on->on_bi.bi_private;
528         int i;
529
530         /* If we're starting from scratch, start at the most general */
531         if (!glueBack) {
532                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
533                         glueBack = &toolDB;
534                 } else {
535                         for (i = gi->gi_nodes-1; i >= 0; i--) {
536                                 if (gi->gi_n[i].gn_be->be_entry_open &&
537                                         gi->gi_n[i].gn_be->be_entry_first) {
538                                                 glueBack = gi->gi_n[i].gn_be;
539                                         break;
540                                 }
541                         }
542                 }
543         }
544         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
545                 glueBack->be_entry_open (glueBack, glueMode) != 0)
546                 return NOID;
547
548         return glueBack->be_entry_first (glueBack);
549 }
550
551 static ID
552 glue_tool_entry_next (
553         BackendDB *b0
554 )
555 {
556         slap_overinst   *on = glue_tool_inst( b0->bd_info );
557         glueinfo                *gi = on->on_bi.bi_private;
558         int i;
559         ID rc;
560
561         if (!glueBack || !glueBack->be_entry_next)
562                 return NOID;
563
564         rc = glueBack->be_entry_next (glueBack);
565
566         /* If we ran out of entries in one database, move on to the next */
567         while (rc == NOID) {
568                 if ( glueBack && glueBack->be_entry_close )
569                         glueBack->be_entry_close (glueBack);
570                 for (i=0; i<gi->gi_nodes; i++) {
571                         if (gi->gi_n[i].gn_be == glueBack)
572                                 break;
573                 }
574                 if (i == 0) {
575                         glueBack = NULL;
576                         break;
577                 } else {
578                         glueBack = gi->gi_n[i-1].gn_be;
579                         rc = glue_tool_entry_first (b0);
580                 }
581         }
582         return rc;
583 }
584
585 static Entry *
586 glue_tool_entry_get (
587         BackendDB *b0,
588         ID id
589 )
590 {
591         if (!glueBack || !glueBack->be_entry_get)
592                 return NULL;
593
594         return glueBack->be_entry_get (glueBack, id);
595 }
596
597 static ID
598 glue_tool_entry_put (
599         BackendDB *b0,
600         Entry *e,
601         struct berval *text
602 )
603 {
604         BackendDB *be, b2;
605         int rc;
606
607         b2 = *b0;
608         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
609         be = glue_back_select (&b2, &e->e_nname);
610         if ( be == &b2 ) be = &toolDB;
611
612         if (!be->be_entry_put)
613                 return NOID;
614
615         if (!glueBack) {
616                 rc = be->be_entry_open (be, glueMode);
617                 if (rc != 0)
618                         return NOID;
619         } else if (be != glueBack) {
620                 /* If this entry belongs in a different branch than the
621                  * previous one, close the current database and open the
622                  * new one.
623                  */
624                 glueBack->be_entry_close (glueBack);
625                 rc = be->be_entry_open (be, glueMode);
626                 if (rc != 0)
627                         return NOID;
628         }
629         glueBack = be;
630         return be->be_entry_put (be, e, text);
631 }
632
633 static int
634 glue_tool_entry_reindex (
635         BackendDB *b0,
636         ID id
637 )
638 {
639         if (!glueBack || !glueBack->be_entry_reindex)
640                 return -1;
641
642         return glueBack->be_entry_reindex (glueBack, id);
643 }
644
645 static int
646 glue_tool_sync (
647         BackendDB *b0
648 )
649 {
650         slap_overinst   *on = glue_tool_inst( b0->bd_info );
651         glueinfo                *gi = on->on_bi.bi_private;
652         BackendInfo             *bi = b0->bd_info;
653         int i;
654
655         /* just sync everyone */
656         for (i = 0; i<gi->gi_nodes; i++)
657                 if (gi->gi_n[i].gn_be->be_sync)
658                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
659         b0->bd_info = on->on_info->oi_orig;
660         if ( b0->be_sync )
661                 b0->be_sync( b0 );
662         b0->bd_info = bi;
663         return 0;
664 }
665
666 static int
667 glue_db_init(
668         BackendDB *be
669 )
670 {
671         slap_overinst   *on = (slap_overinst *)be->bd_info;
672         slap_overinfo   *oi = on->on_info;
673         glueinfo *gi;
674
675         gi = ch_calloc( 1, sizeof(glueinfo));
676         on->on_bi.bi_private = gi;
677         dnParent( be->be_nsuffix, &gi->gi_pdn );
678
679         /* Currently the overlay framework doesn't handle these entry points
680          * but we need them....
681          */
682         oi->oi_bi.bi_open = glue_open;
683         oi->oi_bi.bi_close = glue_close;
684
685         oi->oi_bi.bi_entry_release_rw = glue_entry_release_rw;
686
687         oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
688         oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
689         oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
690         oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
691         oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
692         oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
693         oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
694         oi->oi_bi.bi_tool_sync = glue_tool_sync;
695
696         /*FIXME : need to add support */
697         oi->oi_bi.bi_tool_dn2id_get = 0;
698         oi->oi_bi.bi_tool_id2entry_get = 0;
699         oi->oi_bi.bi_tool_entry_modify = 0;
700
701         return 0;
702 }
703
704 static int
705 glue_db_destroy (
706         BackendDB *be
707 )
708 {
709         slap_overinst   *on = (slap_overinst *)be->bd_info;
710         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
711
712         free (gi);
713         return SLAP_CB_CONTINUE;
714 }
715
716 static int
717 glue_db_open (
718         BackendDB *be
719 )
720 {
721         slap_overinst   *on = (slap_overinst *)be->bd_info;
722         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
723         int i;
724
725         for ( i=0; i<gi->gi_nodes; i++ ) {
726                 int j;
727
728                 gi->gi_n[i].gn_be = backendDB + gi->gi_n[i].gn_bx;
729         }
730         return 0;
731 }
732
733 static int
734 glue_db_close( 
735         BackendDB *be
736 )
737 {
738         slap_overinst   *on = (slap_overinst *)be->bd_info;
739
740         on->on_info->oi_bi.bi_db_close = NULL;
741         return 0;
742 }
743
744 static int
745 glue_db_config(
746         BackendDB       *be,
747         const char      *fname,
748         int             lineno,
749         int             argc,
750         char    **argv
751 )
752 {
753         slap_overinst   *on = (slap_overinst *)be->bd_info;
754         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
755
756         if ( strcasecmp( argv[0], "glue-sub" ) == 0 ) {
757                 int async = 0;
758                 BackendDB *b2;
759                 struct berval bv, dn;
760                 gluenode *gn;
761
762                 if ( argc < 2 ) {
763                         fprintf( stderr, "%s: line %d: too few arguments in "
764                                 "\"glue-sub <suffixDN> [async]\"\n", fname, lineno );
765                         return -1;
766                 }
767                 if ( argc == 3 ) {
768                         if ( strcasecmp( argv[2], "async" )) {
769                                 fprintf( stderr, "%s: line %d: unrecognized option "
770                                         "\"%s\" ignored.\n", fname, lineno, argv[2] );
771                         } else {
772                                 async = 1;
773                         }
774                 }
775                 ber_str2bv( argv[1], 0, 0, &bv );
776                 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL )) {
777                         fprintf( stderr, "invalid suffixDN \"%s\"\n", argv[1] );
778                         return -1;
779                 }
780                 b2 = select_backend( &dn, 0, 1 );
781                 if ( !b2 ) {
782                         fprintf( stderr, "%s: line %d: unknown suffix \"%s\"\n",
783                                 fname, lineno, argv[1] );
784                         return -1;
785                 }
786                 SLAP_DBFLAGS(b2) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
787                 gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
788                         gi->gi_nodes * sizeof(gluenode));
789                 gi->gi_n[gi->gi_nodes].gn_bx = b2 - backendDB;
790                 dnParent( &b2->be_nsuffix[0], &gi->gi_n[gi->gi_nodes].gn_pdn );
791                 gi->gi_n[gi->gi_nodes].gn_async = async;
792                 gi->gi_nodes++;
793                 on->on_bi.bi_private = gi;
794                 return 0;
795         }
796         return SLAP_CONF_UNKNOWN;
797 }
798
799 int
800 glue_init()
801 {
802         glue.on_bi.bi_type = "glue";
803
804         glue.on_bi.bi_db_init = glue_db_init;
805         glue.on_bi.bi_db_config = glue_db_config;
806         glue.on_bi.bi_db_open = glue_db_open;
807         glue.on_bi.bi_db_close = glue_db_close;
808         glue.on_bi.bi_db_destroy = glue_db_destroy;
809
810         glue.on_bi.bi_op_search = glue_op_search;
811         glue.on_bi.bi_op_modify = glue_op_func;
812         glue.on_bi.bi_op_modrdn = glue_op_func;
813         glue.on_bi.bi_op_add = glue_op_func;
814         glue.on_bi.bi_op_delete = glue_op_func;
815
816         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
817         glue.on_bi.bi_chk_controls = glue_chk_controls;
818
819         return overlay_register( &glue );
820 }
821
822 #if SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC
823 int
824 init_module( int argc, char *argv[] )
825 {
826         return glue_init();
827 }
828 #endif  /* SLAPD_OVER_GLUE == SLAPD_MOD_DYNAMIC */
829
830 #endif  /* defined(SLAPD_OVER_GLUE */