]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
rework temporary malloc of glued controls (fixes ITS#6504); fix glued paged results
[openldap] / servers / slapd / backglue.c
1 /* backglue.c - backend glue */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2009 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 #include <stdio.h>
32
33 #include <ac/string.h>
34 #include <ac/socket.h>
35
36 #define SLAPD_TOOLS
37 #include "slap.h"
38 #include "lutil.h"
39 #include "config.h"
40
41 typedef struct gluenode {
42         BackendDB *gn_be;
43         struct berval gn_pdn;
44 } gluenode;
45
46 typedef struct glueinfo {
47         int gi_nodes;
48         struct berval gi_pdn;
49         gluenode gi_n[1];
50 } glueinfo;
51
52 static slap_overinst    glue;
53
54 static int glueMode;
55 static BackendDB *glueBack;
56 static BackendDB glueBackDone;
57 #define GLUEBACK_DONE (&glueBackDone)
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 = gi->gi_nodes-1; i >= 0; i--) {
73                 assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
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         char *matched;
86         BerVarray refs;
87         LDAPControl **ctrls;
88         int err;
89         int matchlen;
90         int nrefs;
91         int nctrls;
92 } glue_state;
93
94 static int
95 glue_op_cleanup( Operation *op, SlapReply *rs )
96 {
97         /* This is not a final result */
98         if (rs->sr_type == REP_RESULT )
99                 rs->sr_type = REP_GLUE_RESULT;
100         return SLAP_CB_CONTINUE;
101 }
102
103 static int
104 glue_op_response ( Operation *op, SlapReply *rs )
105 {
106         glue_state *gs = op->o_callback->sc_private;
107
108         switch(rs->sr_type) {
109         case REP_SEARCH:
110         case REP_SEARCHREF:
111         case REP_INTERMEDIATE:
112                 return SLAP_CB_CONTINUE;
113
114         default:
115                 if (rs->sr_err == LDAP_SUCCESS ||
116                         rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
117                         rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
118                         rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
119                         rs->sr_err == LDAP_NO_SUCH_OBJECT ||
120                         gs->err != LDAP_SUCCESS)
121                         gs->err = rs->sr_err;
122                 if (gs->err == LDAP_SUCCESS && gs->matched) {
123                         ch_free (gs->matched);
124                         gs->matched = NULL;
125                         gs->matchlen = 0;
126                 }
127                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
128                         int len;
129                         len = strlen (rs->sr_matched);
130                         if (len > gs->matchlen) {
131                                 if (gs->matched)
132                                         ch_free (gs->matched);
133                                 gs->matched = ch_strdup (rs->sr_matched);
134                                 gs->matchlen = len;
135                         }
136                 }
137                 if (rs->sr_ref) {
138                         int i, j, k;
139                         BerVarray new;
140
141                         for (i=0; rs->sr_ref[i].bv_val; i++);
142
143                         j = gs->nrefs;
144                         if (!j) {
145                                 new = ch_malloc ((i+1)*sizeof(struct berval));
146                         } else {
147                                 new = ch_realloc(gs->refs,
148                                         (j+i+1)*sizeof(struct berval));
149                         }
150                         for (k=0; k<i; j++,k++) {
151                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
152                         }
153                         new[j].bv_val = NULL;
154                         gs->nrefs = j;
155                         gs->refs = new;
156                 }
157                 if (rs->sr_ctrls) {
158                         int i, j, k;
159                         LDAPControl **newctrls;
160
161                         for (i=0; rs->sr_ctrls[i]; i++);
162
163                         j = gs->nctrls;
164                         if (!j) {
165                                 newctrls = op->o_tmpalloc((i+1)*sizeof(LDAPControl *),
166                                         op->o_tmpmemctx);
167                         } else {
168                                 /* Forget old pagedResults response if we're sending
169                                  * a new one now
170                                  */
171                                 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
172                                         int newpage = 0;
173                                         for ( k=0; k<i; k++ ) {
174                                                 if ( !strcmp(rs->sr_ctrls[k]->ldctl_oid,
175                                                         LDAP_CONTROL_PAGEDRESULTS )) {
176                                                         newpage = 1;
177                                                         break;
178                                                 }
179                                         }
180                                         if ( newpage ) {
181                                                 for ( k=0; k<j; k++ ) {
182                                                         if ( !strcmp(gs->ctrls[k]->ldctl_oid,
183                                                                 LDAP_CONTROL_PAGEDRESULTS ))
184                                                         {
185                                                                 op->o_tmpfree(gs->ctrls[k], op->o_tmpmemctx);
186                                                                 gs->ctrls[k] = gs->ctrls[--j];
187                                                                 gs->ctrls[j] = NULL;
188                                                                 break;
189                                                         }
190                                                 }
191                                         }
192                                 }
193                                 newctrls = op->o_tmprealloc(gs->ctrls,
194                                         (j+i+1)*sizeof(LDAPControl *), op->o_tmpmemctx);
195                         }
196                         for (k=0; k<i; j++,k++) {
197                                 ber_len_t oidlen = strlen( rs->sr_ctrls[k]->ldctl_oid );
198                                 newctrls[j] = op->o_tmpalloc(sizeof(LDAPControl) + oidlen + 1 + rs->sr_ctrls[k]->ldctl_value.bv_len + 1,
199                                         op->o_tmpmemctx);
200                                 newctrls[j]->ldctl_iscritical = rs->sr_ctrls[k]->ldctl_iscritical;
201                                 newctrls[j]->ldctl_oid = (char *)&newctrls[j][1];
202                                 lutil_strcopy( newctrls[j]->ldctl_oid, rs->sr_ctrls[k]->ldctl_oid );
203                                 if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ) ) {
204                                         newctrls[j]->ldctl_value.bv_val = &newctrls[j]->ldctl_oid[oidlen + 1];
205                                         newctrls[j]->ldctl_value.bv_len = rs->sr_ctrls[k]->ldctl_value.bv_len;
206                                         lutil_memcopy( newctrls[j]->ldctl_value.bv_val,
207                                                 rs->sr_ctrls[k]->ldctl_value.bv_val,
208                                                 rs->sr_ctrls[k]->ldctl_value.bv_len + 1 );
209                                 } else {
210                                         BER_BVZERO( &newctrls[j]->ldctl_value );
211                                 }
212                         }
213                         newctrls[j] = NULL;
214                         gs->nctrls = j;
215                         gs->ctrls = newctrls;
216                 }
217         }
218         return 0;
219 }
220
221 static int
222 glue_op_func ( Operation *op, SlapReply *rs )
223 {
224         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
225         BackendDB *b0 = op->o_bd;
226         BackendInfo *bi0 = op->o_bd->bd_info;
227         BI_op_modify **func;
228         slap_operation_t which = op_bind;
229         int rc;
230
231         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
232
233         /* If we're on the master backend, let overlay framework handle it */
234         if ( op->o_bd == b0 )
235                 return SLAP_CB_CONTINUE;
236
237         b0->bd_info = on->on_info->oi_orig;
238
239         switch(op->o_tag) {
240         case LDAP_REQ_ADD: which = op_add; break;
241         case LDAP_REQ_DELETE: which = op_delete; break;
242         case LDAP_REQ_MODIFY: which = op_modify; break;
243         case LDAP_REQ_MODRDN: which = op_modrdn; break;
244         case LDAP_REQ_EXTENDED: which = op_extended; break;
245         default: assert( 0 ); break;
246         }
247
248         func = &op->o_bd->bd_info->bi_op_bind;
249         if ( func[which] )
250                 rc = func[which]( op, rs );
251         else
252                 rc = SLAP_CB_BYPASS;
253
254         op->o_bd = b0;
255         op->o_bd->bd_info = bi0;
256         return rc;
257 }
258
259 static int
260 glue_response ( Operation *op, SlapReply *rs )
261 {
262         BackendDB *be = op->o_bd;
263         be = glue_back_select (op->o_bd, &op->o_req_ndn);
264
265         /* If we're on the master backend, let overlay framework handle it.
266          * Otherwise, bail out.
267          */
268         return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
269 }
270
271 static int
272 glue_chk_referrals ( Operation *op, SlapReply *rs )
273 {
274         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
275         BackendDB *b0 = op->o_bd;
276         BackendInfo *bi0 = op->o_bd->bd_info;
277         int rc;
278
279         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
280         if ( op->o_bd == b0 )
281                 return SLAP_CB_CONTINUE;
282
283         b0->bd_info = on->on_info->oi_orig;
284
285         if ( op->o_bd->bd_info->bi_chk_referrals )
286                 rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
287         else
288                 rc = SLAP_CB_CONTINUE;
289
290         op->o_bd = b0;
291         op->o_bd->bd_info = bi0;
292         return rc;
293 }
294
295 static int
296 glue_chk_controls ( Operation *op, SlapReply *rs )
297 {
298         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
299         BackendDB *b0 = op->o_bd;
300         BackendInfo *bi0 = op->o_bd->bd_info;
301         int rc = SLAP_CB_CONTINUE;
302
303         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
304         if ( op->o_bd == b0 )
305                 return SLAP_CB_CONTINUE;
306
307         b0->bd_info = on->on_info->oi_orig;
308
309         /* if the subordinate database has overlays, the bi_chk_controls()
310          * hook is actually over_aux_chk_controls(); in case it actually
311          * wraps a missing hok, we need to mimic the behavior
312          * of the frontend applied to that database */
313         if ( op->o_bd->bd_info->bi_chk_controls ) {
314                 rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
315         }
316
317         
318         if ( rc == SLAP_CB_CONTINUE ) {
319                 rc = backend_check_controls( op, rs );
320         }
321
322         op->o_bd = b0;
323         op->o_bd->bd_info = bi0;
324         return rc;
325 }
326
327 /* ITS#4615 - overlays configured above the glue overlay should be
328  * invoked for the entire glued tree. Overlays configured below the
329  * glue overlay should only be invoked on the master backend.
330  * So, if we're searching on any subordinates, we need to force the
331  * current overlay chain to stop processing, without stopping the
332  * overall callback flow.
333  */
334 static int
335 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
336         slap_overinst *on )
337 {
338         /* Process any overlays on the master backend */
339         if ( op->o_bd == b0 && on->on_next ) {
340                 BackendInfo *bi = op->o_bd->bd_info;
341                 int rc = SLAP_CB_CONTINUE;
342                 for ( on=on->on_next; on; on=on->on_next ) {
343                         op->o_bd->bd_info = (BackendInfo *)on;
344                         if ( on->on_bi.bi_op_search ) {
345                                 rc = on->on_bi.bi_op_search( op, rs );
346                                 if ( rc != SLAP_CB_CONTINUE )
347                                         break;
348                         }
349                 }
350                 op->o_bd->bd_info = bi;
351                 if ( rc != SLAP_CB_CONTINUE )
352                         return rc;
353         }
354         return op->o_bd->be_search( op, rs );
355 }
356
357 static int
358 glue_op_search ( Operation *op, SlapReply *rs )
359 {
360         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
361         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
362         BackendDB *b0 = op->o_bd;
363         BackendDB *b1 = NULL, *btmp;
364         BackendInfo *bi0 = op->o_bd->bd_info;
365         int i;
366         long stoptime = 0, starttime;
367         glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
368         slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
369         int scope0, tlimit0;
370         struct berval dn, ndn, *pdn;
371
372         cb.sc_private = &gs;
373
374         cb.sc_next = op->o_callback;
375
376         starttime = op->o_time;
377         stoptime = slap_get_time () + op->ors_tlimit;
378
379         op->o_bd = glue_back_select (b0, &op->o_req_ndn);
380         b0->bd_info = on->on_info->oi_orig;
381
382         switch (op->ors_scope) {
383         case LDAP_SCOPE_BASE:
384                 if ( op->o_bd == b0 )
385                         return SLAP_CB_CONTINUE;
386
387                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
388                 if (op->o_bd && op->o_bd->be_search) {
389                         rs->sr_err = op->o_bd->be_search( op, rs );
390                 }
391                 return rs->sr_err;
392
393         case LDAP_SCOPE_ONELEVEL:
394         case LDAP_SCOPE_SUBTREE:
395         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
396                 op->o_callback = &cb;
397                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
398                 scope0 = op->ors_scope;
399                 tlimit0 = op->ors_tlimit;
400                 dn = op->o_req_dn;
401                 ndn = op->o_req_ndn;
402                 b1 = op->o_bd;
403
404                 /*
405                  * Execute in reverse order, most specific first 
406                  */
407                 for (i = gi->gi_nodes; i >= 0; i--) {
408                         if ( i == gi->gi_nodes ) {
409                                 btmp = b0;
410                                 pdn = &gi->gi_pdn;
411                         } else {
412                                 btmp = gi->gi_n[i].gn_be;
413                                 pdn = &gi->gi_n[i].gn_pdn;
414                         }
415                         if (!btmp || !btmp->be_search)
416                                 continue;
417                         if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
418                                 continue;
419                         if (get_no_subordinate_glue(op) && btmp != b1)
420                                 continue;
421                         /* If we remembered which backend we were on before,
422                          * skip down to it now
423                          */
424                         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
425                                 op->o_conn->c_pagedresults_state.ps_be &&
426                                 op->o_conn->c_pagedresults_state.ps_be != btmp )
427                                 continue;
428
429                         if (tlimit0 != SLAP_NO_LIMIT) {
430                                 op->o_time = slap_get_time();
431                                 op->ors_tlimit = stoptime - op->o_time;
432                                 if (op->ors_tlimit <= 0) {
433                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
434                                         break;
435                                 }
436                         }
437                         rs->sr_err = 0;
438                         /*
439                          * check for abandon 
440                          */
441                         if (op->o_abandon) {
442                                 goto end_of_loop;
443                         }
444                         op->o_bd = btmp;
445
446                         assert( op->o_bd->be_suffix != NULL );
447                         assert( op->o_bd->be_nsuffix != NULL );
448                         
449                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
450                                 dn_match(pdn, &ndn))
451                         {
452                                 struct berval mdn, mndn;
453                                 op->ors_scope = LDAP_SCOPE_BASE;
454                                 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
455                                 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
456                                 rs->sr_err = op->o_bd->be_search(op, rs);
457                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
458                                         gs.err = LDAP_SUCCESS;
459                                 }
460                                 op->ors_scope = LDAP_SCOPE_ONELEVEL;
461                                 if ( op->o_req_dn.bv_val == mdn.bv_val )
462                                         op->o_req_dn = dn;
463                                 if ( op->o_req_ndn.bv_val == mndn.bv_val )
464                                         op->o_req_ndn = ndn;
465
466                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
467                                 dn_match(&op->o_bd->be_nsuffix[0], &ndn))
468                         {
469                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
470
471                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
472                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
473                         {
474                                 struct berval mdn, mndn;
475                                 mdn = op->o_req_dn = op->o_bd->be_suffix[0];
476                                 mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
477                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
478                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
479                                         gs.err = LDAP_SUCCESS;
480                                 }
481                                 if ( op->o_req_dn.bv_val == mdn.bv_val )
482                                         op->o_req_dn = dn;
483                                 if ( op->o_req_ndn.bv_val == mndn.bv_val )
484                                         op->o_req_ndn = ndn;
485
486                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
487                                 rs->sr_err = glue_sub_search( op, rs, b0, on );
488                         }
489
490                         switch ( gs.err ) {
491
492                         /*
493                          * Add errors that should result in dropping
494                          * the search
495                          */
496                         case LDAP_SIZELIMIT_EXCEEDED:
497                         case LDAP_TIMELIMIT_EXCEEDED:
498                         case LDAP_ADMINLIMIT_EXCEEDED:
499                         case LDAP_NO_SUCH_OBJECT:
500 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
501                         case LDAP_X_CANNOT_CHAIN:
502 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
503                                 goto end_of_loop;
504
505                         case LDAP_SUCCESS:
506                                 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
507                                         PagedResultsState *ps = op->o_pagedresults_state;
508
509                                         /* Assume this backend can be forgotten now */
510                                         op->o_conn->c_pagedresults_state.ps_be = NULL;
511
512                                         /* If we have a full page, exit the loop. We may
513                                          * need to remember this backend so we can continue
514                                          * from here on a subsequent request.
515                                          */
516                                         if ( rs->sr_nentries >= ps->ps_size ) {
517                                                 /* Don't bother to remember the first backend.
518                                                  * Only remember the last one if there's more state left.
519                                                  */
520                                                 if ( op->o_bd != b0 &&
521                                                         ( op->o_conn->c_pagedresults_state.ps_cookie ||
522                                                         op->o_bd != gi->gi_n[0].gn_be ))
523                                                         op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
524                                                 goto end_of_loop;
525                                         }
526
527                                         /* This backend has run out of entries, but more responses
528                                          * can fit in the page. Fake a reset of the state so the
529                                          * next backend will start up properly. Only back-[bh]db
530                                          * and back-sql look at this state info.
531                                          */
532                                         if ( ps->ps_cookieval.bv_len == sizeof( PagedResultsCookie )) {
533                                                 ps->ps_cookie = 0;
534                                                 memset( ps->ps_cookieval.bv_val, 0,
535                                                         sizeof( PagedResultsCookie ));
536                                         }
537
538
539                                         {
540                                                 /* change the size of the page in the request
541                                                  * that will be propagated */
542                                                 BerElementBuffer berbuf;
543                                                 BerElement *ber = (BerElement *)&berbuf;
544                                                 int size = ps->ps_size - rs->sr_nentries;
545                                                 struct berval cookie = BER_BVC(""), value;
546                                                 int c;
547
548                                                 for (c = 0; op->o_ctrls[c] != NULL; c++) {
549                                                         if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
550                                                                 break;
551                                                 }
552
553                                                 ber_init2( ber, NULL, LBER_USE_DER );
554                                                 ber_printf( ber, "{iO}", size, &cookie );
555                                                 ber_flatten2( ber, &value, 0 );
556                                                 assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
557                                                 op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
558                                                 lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
559                                                         value.bv_val, value.bv_len );
560                                                 ber_free_buf( ber );
561                                         }
562                                 }
563                                 
564                         default:
565                                 break;
566                         }
567                 }
568 end_of_loop:;
569                 op->ors_scope = scope0;
570                 op->ors_tlimit = tlimit0;
571                 op->o_time = starttime;
572
573                 break;
574         }
575
576         op->o_callback = cb.sc_next;
577         if ( op->o_abandon ) {
578                 rs->sr_err = SLAPD_ABANDON;
579         } else {
580                 rs->sr_err = gs.err;
581                 rs->sr_matched = gs.matched;
582                 rs->sr_ref = gs.refs;
583         }
584         rs->sr_ctrls = gs.ctrls;
585
586         send_ldap_result( op, rs );
587
588         op->o_bd = b0;
589         op->o_bd->bd_info = bi0;
590         if (gs.matched)
591                 free (gs.matched);
592         if (gs.refs)
593                 ber_bvarray_free(gs.refs);
594         if (gs.ctrls) {
595                 for (i = gs.nctrls; --i >= 0; ) {
596                         op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
597                 }
598                 op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
599         }
600         return rs->sr_err;
601 }
602
603 static BackendDB toolDB;
604
605 static int
606 glue_tool_entry_open (
607         BackendDB *b0,
608         int mode
609 )
610 {
611         slap_overinfo   *oi = (slap_overinfo *)b0->bd_info;
612
613         /* We don't know which backend to talk to yet, so just
614          * remember the mode and move on...
615          */
616
617         glueMode = mode;
618         glueBack = NULL;
619         toolDB = *b0;
620         toolDB.bd_info = oi->oi_orig;
621
622         return 0;
623 }
624
625 static int
626 glue_tool_entry_close (
627         BackendDB *b0
628 )
629 {
630         int rc = 0;
631
632         if (glueBack && glueBack != GLUEBACK_DONE) {
633                 if (!glueBack->be_entry_close)
634                         return 0;
635                 rc = glueBack->be_entry_close (glueBack);
636         }
637         return rc;
638 }
639
640 static slap_overinst *
641 glue_tool_inst(
642         BackendInfo *bi
643 )
644 {
645         slap_overinfo   *oi = (slap_overinfo *)bi;
646         slap_overinst   *on;
647
648         for ( on = oi->oi_list; on; on=on->on_next ) {
649                 if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
650                         return on;
651         }
652         return NULL;
653 }
654
655 /* This function will only be called in tool mode */
656 static int
657 glue_open (
658         BackendInfo *bi
659 )
660 {
661         slap_overinst *on = glue_tool_inst( bi );
662         glueinfo                *gi = on->on_bi.bi_private;
663         static int glueOpened = 0;
664         int i, j, same, bsame = 0, rc = 0;
665         ConfigReply cr = {0};
666
667         if (glueOpened) return 0;
668
669         glueOpened = 1;
670
671         /* If we were invoked in tool mode, open all the underlying backends */
672         if (slapMode & SLAP_TOOL_MODE) {
673                 for (i = 0; i<gi->gi_nodes; i++) {
674                         same = 0;
675                         /* Same bi_open as our main backend? */
676                         if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
677                                 on->on_info->oi_orig->bi_open )
678                                 bsame = 1;
679
680                         /* Loop thru the bd_info's and make sure we only
681                          * invoke their bi_open functions once each.
682                          */
683                         for ( j = 0; j<i; j++ ) {
684                                 if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
685                                         gi->gi_n[j].gn_be->bd_info->bi_open ) {
686                                         same = 1;
687                                         break;
688                                 }
689                         }
690                         /* OK, it's unique and non-NULL, call it. */
691                         if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
692                                 rc = gi->gi_n[i].gn_be->bd_info->bi_open(
693                                         gi->gi_n[i].gn_be->bd_info );
694                         /* Let backend.c take care of the rest of startup */
695                         if ( !rc )
696                                 rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
697                         if ( rc ) break;
698                 }
699                 if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
700                         rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
701
702         } /* other case is impossible */
703         return rc;
704 }
705
706 /* This function will only be called in tool mode */
707 static int
708 glue_close (
709         BackendInfo *bi
710 )
711 {
712         static int glueClosed = 0;
713         int rc = 0;
714
715         if (glueClosed) return 0;
716
717         glueClosed = 1;
718
719         if (slapMode & SLAP_TOOL_MODE) {
720                 rc = backend_shutdown( NULL );
721         }
722         return rc;
723 }
724
725 static int
726 glue_entry_get_rw (
727         Operation               *op,
728         struct berval   *dn,
729         ObjectClass             *oc,
730         AttributeDescription    *ad,
731         int     rw,
732         Entry   **e )
733 {
734         int rc;
735         BackendDB *b0 = op->o_bd;
736         op->o_bd = glue_back_select( b0, dn );
737
738         if ( op->o_bd->be_fetch ) {
739                 rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
740         } else {
741                 rc = LDAP_UNWILLING_TO_PERFORM;
742         }
743         op->o_bd =b0;
744         return rc;
745 }
746
747 static int
748 glue_entry_release_rw (
749         Operation *op,
750         Entry *e,
751         int rw
752 )
753 {
754         BackendDB *b0 = op->o_bd;
755         int rc = -1;
756
757         op->o_bd = glue_back_select (b0, &e->e_nname);
758
759         if ( op->o_bd->be_release ) {
760                 rc = op->o_bd->be_release( op, e, rw );
761
762         } else {
763                 /* FIXME: mimic be_entry_release_rw
764                  * when no be_release() available */
765                 /* free entry */
766                 entry_free( e );
767                 rc = 0;
768         }
769         op->o_bd = b0;
770         return rc;
771 }
772
773 static struct berval *glue_base;
774 static int glue_scope;
775 static Filter *glue_filter;
776
777 static ID
778 glue_tool_entry_first (
779         BackendDB *b0
780 )
781 {
782         slap_overinst   *on = glue_tool_inst( b0->bd_info );
783         glueinfo                *gi = on->on_bi.bi_private;
784         int i;
785         ID rc;
786
787         /* If we're starting from scratch, start at the most general */
788         if (!glueBack) {
789                 if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
790                         glueBack = &toolDB;
791                 } else {
792                         for (i = gi->gi_nodes-1; i >= 0; i--) {
793                                 if (gi->gi_n[i].gn_be->be_entry_open &&
794                                         gi->gi_n[i].gn_be->be_entry_first) {
795                                                 glueBack = gi->gi_n[i].gn_be;
796                                         break;
797                                 }
798                         }
799                 }
800         }
801         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
802                 glueBack->be_entry_open (glueBack, glueMode) != 0)
803                 return NOID;
804
805         rc = glueBack->be_entry_first (glueBack);
806         while ( rc == NOID ) {
807                 if ( glueBack && glueBack->be_entry_close )
808                         glueBack->be_entry_close (glueBack);
809                 for (i=0; i<gi->gi_nodes; i++) {
810                         if (gi->gi_n[i].gn_be == glueBack)
811                                 break;
812                 }
813                 if (i == 0) {
814                         glueBack = GLUEBACK_DONE;
815                         break;
816                 } else {
817                         glueBack = gi->gi_n[i-1].gn_be;
818                         rc = glue_tool_entry_first (b0);
819                         if ( glueBack == GLUEBACK_DONE ) {
820                                 break;
821                         }
822                 }
823         }
824         return rc;
825 }
826
827 static ID
828 glue_tool_entry_first_x (
829         BackendDB *b0,
830         struct berval *base,
831         int scope,
832         Filter *f
833 )
834 {
835         slap_overinst   *on = glue_tool_inst( b0->bd_info );
836         glueinfo                *gi = on->on_bi.bi_private;
837         int i;
838         ID rc;
839
840         glue_base = base;
841         glue_scope = scope;
842         glue_filter = f;
843
844         /* If we're starting from scratch, start at the most general */
845         if (!glueBack) {
846                 if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
847                         glueBack = &toolDB;
848                 } else {
849                         for (i = gi->gi_nodes-1; i >= 0; i--) {
850                                 if (gi->gi_n[i].gn_be->be_entry_open &&
851                                         gi->gi_n[i].gn_be->be_entry_first_x)
852                                 {
853                                         glueBack = gi->gi_n[i].gn_be;
854                                         break;
855                                 }
856                         }
857                 }
858         }
859         if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
860                 glueBack->be_entry_open (glueBack, glueMode) != 0)
861                 return NOID;
862
863         rc = glueBack->be_entry_first_x (glueBack,
864                 glue_base, glue_scope, glue_filter);
865         while ( rc == NOID ) {
866                 if ( glueBack && glueBack->be_entry_close )
867                         glueBack->be_entry_close (glueBack);
868                 for (i=0; i<gi->gi_nodes; i++) {
869                         if (gi->gi_n[i].gn_be == glueBack)
870                                 break;
871                 }
872                 if (i == 0) {
873                         glueBack = GLUEBACK_DONE;
874                         break;
875                 } else {
876                         glueBack = gi->gi_n[i-1].gn_be;
877                         rc = glue_tool_entry_first_x (b0,
878                                 glue_base, glue_scope, glue_filter);
879                         if ( glueBack == GLUEBACK_DONE ) {
880                                 break;
881                         }
882                 }
883         }
884         return rc;
885 }
886
887 static ID
888 glue_tool_entry_next (
889         BackendDB *b0
890 )
891 {
892         slap_overinst   *on = glue_tool_inst( b0->bd_info );
893         glueinfo                *gi = on->on_bi.bi_private;
894         int i;
895         ID rc;
896
897         if (!glueBack || !glueBack->be_entry_next)
898                 return NOID;
899
900         rc = glueBack->be_entry_next (glueBack);
901
902         /* If we ran out of entries in one database, move on to the next */
903         while (rc == NOID) {
904                 if ( glueBack && glueBack->be_entry_close )
905                         glueBack->be_entry_close (glueBack);
906                 for (i=0; i<gi->gi_nodes; i++) {
907                         if (gi->gi_n[i].gn_be == glueBack)
908                                 break;
909                 }
910                 if (i == 0) {
911                         glueBack = GLUEBACK_DONE;
912                         break;
913                 } else {
914                         glueBack = gi->gi_n[i-1].gn_be;
915                         if ( glue_base || glue_filter ) {
916                                 /* using entry_first_x() */
917                                 rc = glue_tool_entry_first_x (b0,
918                                         glue_base, glue_scope, glue_filter);
919
920                         } else {
921                                 /* using entry_first() */
922                                 rc = glue_tool_entry_first (b0);
923                         }
924                         if ( glueBack == GLUEBACK_DONE ) {
925                                 break;
926                         }
927                 }
928         }
929         return rc;
930 }
931
932 static ID
933 glue_tool_dn2id_get (
934         BackendDB *b0,
935         struct berval *dn
936 )
937 {
938         BackendDB *be, b2;
939         int rc = -1;
940
941         b2 = *b0;
942         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
943         be = glue_back_select (&b2, dn);
944         if ( be == &b2 ) be = &toolDB;
945
946         if (!be->be_dn2id_get)
947                 return NOID;
948
949         if (!glueBack) {
950                 if ( be->be_entry_open ) {
951                         rc = be->be_entry_open (be, glueMode);
952                 }
953                 if (rc != 0) {
954                         return NOID;
955                 }
956         } else if (be != glueBack) {
957                 /* If this entry belongs in a different branch than the
958                  * previous one, close the current database and open the
959                  * new one.
960                  */
961                 if ( glueBack->be_entry_close ) {
962                         glueBack->be_entry_close (glueBack);
963                 }
964                 if ( be->be_entry_open ) {
965                         rc = be->be_entry_open (be, glueMode);
966                 }
967                 if (rc != 0) {
968                         return NOID;
969                 }
970         }
971         glueBack = be;
972         return be->be_dn2id_get (be, dn);
973 }
974
975 static Entry *
976 glue_tool_entry_get (
977         BackendDB *b0,
978         ID id
979 )
980 {
981         if (!glueBack || !glueBack->be_entry_get)
982                 return NULL;
983
984         return glueBack->be_entry_get (glueBack, id);
985 }
986
987 static ID
988 glue_tool_entry_put (
989         BackendDB *b0,
990         Entry *e,
991         struct berval *text
992 )
993 {
994         BackendDB *be, b2;
995         int rc = -1;
996
997         b2 = *b0;
998         b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
999         be = glue_back_select (&b2, &e->e_nname);
1000         if ( be == &b2 ) be = &toolDB;
1001
1002         if (!be->be_entry_put)
1003                 return NOID;
1004
1005         if (!glueBack) {
1006                 if ( be->be_entry_open ) {
1007                         rc = be->be_entry_open (be, glueMode);
1008                 }
1009                 if (rc != 0) {
1010                         return NOID;
1011                 }
1012         } else if (be != glueBack) {
1013                 /* If this entry belongs in a different branch than the
1014                  * previous one, close the current database and open the
1015                  * new one.
1016                  */
1017                 if ( glueBack->be_entry_close ) {
1018                         glueBack->be_entry_close (glueBack);
1019                 }
1020                 if ( be->be_entry_open ) {
1021                         rc = be->be_entry_open (be, glueMode);
1022                 }
1023                 if (rc != 0) {
1024                         return NOID;
1025                 }
1026         }
1027         glueBack = be;
1028         return be->be_entry_put (be, e, text);
1029 }
1030
1031 static ID
1032 glue_tool_entry_modify (
1033         BackendDB *b0,
1034         Entry *e,
1035         struct berval *text
1036 )
1037 {
1038         if (!glueBack || !glueBack->be_entry_modify)
1039                 return NOID;
1040
1041         return glueBack->be_entry_modify (glueBack, e, text);
1042 }
1043
1044 static int
1045 glue_tool_entry_reindex (
1046         BackendDB *b0,
1047         ID id,
1048         AttributeDescription **adv
1049 )
1050 {
1051         if (!glueBack || !glueBack->be_entry_reindex)
1052                 return -1;
1053
1054         return glueBack->be_entry_reindex (glueBack, id, adv);
1055 }
1056
1057 static int
1058 glue_tool_sync (
1059         BackendDB *b0
1060 )
1061 {
1062         slap_overinst   *on = glue_tool_inst( b0->bd_info );
1063         glueinfo                *gi = on->on_bi.bi_private;
1064         BackendInfo             *bi = b0->bd_info;
1065         int i;
1066
1067         /* just sync everyone */
1068         for (i = 0; i<gi->gi_nodes; i++)
1069                 if (gi->gi_n[i].gn_be->be_sync)
1070                         gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1071         b0->bd_info = on->on_info->oi_orig;
1072         if ( b0->be_sync )
1073                 b0->be_sync( b0 );
1074         b0->bd_info = bi;
1075         return 0;
1076 }
1077
1078 typedef struct glue_Addrec {
1079         struct glue_Addrec *ga_next;
1080         BackendDB *ga_be;
1081 } glue_Addrec;
1082
1083 /* List of added subordinates */
1084 static glue_Addrec *ga_list;
1085 static int ga_adding;
1086
1087 static int
1088 glue_db_init(
1089         BackendDB *be,
1090         ConfigReply *cr
1091 )
1092 {
1093         slap_overinst   *on = (slap_overinst *)be->bd_info;
1094         slap_overinfo   *oi = on->on_info;
1095         BackendInfo     *bi = oi->oi_orig;
1096         glueinfo *gi;
1097
1098         if ( SLAP_GLUE_SUBORDINATE( be )) {
1099                 Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1100                         "cannot have glue overlay!\n",
1101                         be->be_suffix[0].bv_val, 0, 0 );
1102                 return LDAP_OTHER;
1103         }
1104
1105         gi = ch_calloc( 1, sizeof(glueinfo));
1106         on->on_bi.bi_private = gi;
1107         dnParent( be->be_nsuffix, &gi->gi_pdn );
1108
1109         /* Currently the overlay framework doesn't handle these entry points
1110          * but we need them....
1111          */
1112         oi->oi_bi.bi_open = glue_open;
1113         oi->oi_bi.bi_close = glue_close;
1114
1115         /* Only advertise these if the root DB supports them */
1116         if ( bi->bi_tool_entry_open )
1117                 oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1118         if ( bi->bi_tool_entry_close )
1119                 oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1120         if ( bi->bi_tool_entry_first )
1121                 oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1122         /* FIXME: check whether all support bi_tool_entry_first_x() ? */
1123         if ( bi->bi_tool_entry_first_x )
1124                 oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1125         if ( bi->bi_tool_entry_next )
1126                 oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1127         if ( bi->bi_tool_entry_get )
1128                 oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1129         if ( bi->bi_tool_dn2id_get )
1130                 oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1131         if ( bi->bi_tool_entry_put )
1132                 oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1133         if ( bi->bi_tool_entry_reindex )
1134                 oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1135         if ( bi->bi_tool_entry_modify )
1136                 oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1137         if ( bi->bi_tool_sync )
1138                 oi->oi_bi.bi_tool_sync = glue_tool_sync;
1139
1140         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1141
1142         if ( ga_list ) {
1143                 be->bd_info = (BackendInfo *)oi;
1144                 glue_sub_attach( 1 );
1145         }
1146
1147         return 0;
1148 }
1149
1150 static int
1151 glue_db_destroy (
1152         BackendDB *be,
1153         ConfigReply *cr
1154 )
1155 {
1156         slap_overinst   *on = (slap_overinst *)be->bd_info;
1157         glueinfo                *gi = (glueinfo *)on->on_bi.bi_private;
1158
1159         free (gi);
1160         return SLAP_CB_CONTINUE;
1161 }
1162
1163 static int
1164 glue_db_close( 
1165         BackendDB *be,
1166         ConfigReply *cr
1167 )
1168 {
1169         slap_overinst   *on = (slap_overinst *)be->bd_info;
1170
1171         on->on_info->oi_bi.bi_db_close = 0;
1172         return 0;
1173 }
1174
1175 int
1176 glue_sub_del( BackendDB *b0 )
1177 {
1178         BackendDB *be;
1179         int rc = 0;
1180
1181         /* Find the top backend for this subordinate */
1182         be = b0;
1183         while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1184                 slap_overinfo *oi;
1185                 slap_overinst *on;
1186                 glueinfo *gi;
1187                 int i;
1188
1189                 if ( SLAP_GLUE_SUBORDINATE( be ))
1190                         continue;
1191                 if ( !SLAP_GLUE_INSTANCE( be ))
1192                         continue;
1193                 if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1194                         continue;
1195
1196                 /* OK, got the right backend, find the overlay */
1197                 oi = (slap_overinfo *)be->bd_info;
1198                 for ( on=oi->oi_list; on; on=on->on_next ) {
1199                         if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1200                                 break;
1201                 }
1202                 assert( on != NULL );
1203                 gi = on->on_bi.bi_private;
1204                 for ( i=0; i < gi->gi_nodes; i++ ) {
1205                         if ( gi->gi_n[i].gn_be == b0 ) {
1206                                 int j;
1207
1208                                 for (j=i+1; j < gi->gi_nodes; j++)
1209                                         gi->gi_n[j-1] = gi->gi_n[j];
1210
1211                                 gi->gi_nodes--;
1212                         }
1213                 }
1214         }
1215         if ( be == NULL )
1216                 rc = LDAP_NO_SUCH_OBJECT;
1217
1218         return rc;
1219 }
1220
1221
1222 /* Attach all the subordinate backends to their superior */
1223 int
1224 glue_sub_attach( int online )
1225 {
1226         glue_Addrec *ga, *gnext = NULL;
1227         int rc = 0;
1228
1229         if ( ga_adding )
1230                 return 0;
1231
1232         ga_adding = 1;
1233
1234         /* For all the subordinate backends */
1235         for ( ga=ga_list; ga != NULL; ga = gnext ) {
1236                 BackendDB *be;
1237
1238                 gnext = ga->ga_next;
1239
1240                 /* Find the top backend for this subordinate */
1241                 be = ga->ga_be;
1242                 while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1243                         slap_overinfo *oi;
1244                         slap_overinst *on;
1245                         glueinfo *gi;
1246
1247                         if ( SLAP_GLUE_SUBORDINATE( be ))
1248                                 continue;
1249                         if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1250                                 continue;
1251
1252                         /* If it's not already configured, set up the overlay */
1253                         if ( !SLAP_GLUE_INSTANCE( be )) {
1254                                 rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1255                                 if ( rc )
1256                                         break;
1257                         }
1258                         /* Find the overlay instance */
1259                         oi = (slap_overinfo *)be->bd_info;
1260                         for ( on=oi->oi_list; on; on=on->on_next ) {
1261                                 if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1262                                         break;
1263                         }
1264                         assert( on != NULL );
1265                         gi = on->on_bi.bi_private;
1266                         gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1267                                 gi->gi_nodes * sizeof(gluenode));
1268                         gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1269                         dnParent( &ga->ga_be->be_nsuffix[0],
1270                                 &gi->gi_n[gi->gi_nodes].gn_pdn );
1271                         gi->gi_nodes++;
1272                         on->on_bi.bi_private = gi;
1273                         ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1274                         break;
1275                 }
1276                 if ( !be ) {
1277                         Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1278                                 ga->ga_be->be_suffix[0].bv_val, 0, 0 );
1279                         /* allow this for now, assume a superior will
1280                          * be added later
1281                          */
1282                         if ( online ) {
1283                                 rc = 0;
1284                                 gnext = ga_list;
1285                                 break;
1286                         }
1287                         rc = LDAP_NO_SUCH_OBJECT;
1288                 }
1289                 ch_free( ga );
1290                 if ( rc ) break;
1291         }
1292
1293         ga_list = gnext;
1294
1295         ga_adding = 0;
1296
1297         return rc;
1298 }
1299
1300 int
1301 glue_sub_add( BackendDB *be, int advert, int online )
1302 {
1303         glue_Addrec *ga;
1304         int rc = 0;
1305
1306         if ( overlay_is_inst( be, "glue" )) {
1307                 Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1308                         "cannot be a subordinate!\n",
1309                         be->be_suffix[0].bv_val, 0, 0 );
1310                 return LDAP_OTHER;
1311         }
1312         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1313         if ( advert )
1314                 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1315
1316         ga = ch_malloc( sizeof( glue_Addrec ));
1317         ga->ga_next = ga_list;
1318         ga->ga_be = be;
1319         ga_list = ga;
1320
1321         if ( online )
1322                 rc = glue_sub_attach( online );
1323
1324         return rc;
1325 }
1326
1327 static int
1328 glue_access_allowed(
1329         Operation               *op,
1330         Entry                   *e,
1331         AttributeDescription    *desc,
1332         struct berval           *val,
1333         slap_access_t           access,
1334         AccessControlState      *state,
1335         slap_mask_t             *maskp )
1336 {
1337         BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
1338         int rc;
1339
1340         if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
1341                 return SLAP_CB_CONTINUE;
1342
1343         b0 = op->o_bd;
1344         op->o_bd = be;
1345         rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
1346         op->o_bd = b0;
1347         return rc;
1348 }
1349
1350 int
1351 glue_sub_init()
1352 {
1353         glue.on_bi.bi_type = "glue";
1354
1355         glue.on_bi.bi_db_init = glue_db_init;
1356         glue.on_bi.bi_db_close = glue_db_close;
1357         glue.on_bi.bi_db_destroy = glue_db_destroy;
1358
1359         glue.on_bi.bi_op_search = glue_op_search;
1360         glue.on_bi.bi_op_modify = glue_op_func;
1361         glue.on_bi.bi_op_modrdn = glue_op_func;
1362         glue.on_bi.bi_op_add = glue_op_func;
1363         glue.on_bi.bi_op_delete = glue_op_func;
1364         glue.on_bi.bi_extended = glue_op_func;
1365
1366         glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1367         glue.on_bi.bi_chk_controls = glue_chk_controls;
1368         glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1369         glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1370         glue.on_bi.bi_access_allowed = glue_access_allowed;
1371
1372         glue.on_response = glue_response;
1373
1374         return overlay_register( &glue );
1375 }