]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / servers / slapd / backover.c
1 /* backover.c - backend overlay routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2013 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 /* Functions to overlay other modules over a backend. */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/string.h>
24 #include <ac/socket.h>
25
26 #define SLAPD_TOOLS
27 #include "slap.h"
28 #include "config.h"
29
30 static slap_overinst *overlays;
31
32 static int
33 over_db_config(
34         BackendDB *be,
35         const char *fname,
36         int lineno,
37         int argc,
38         char **argv
39 )
40 {
41         slap_overinfo *oi = be->bd_info->bi_private;
42         slap_overinst *on = oi->oi_list;
43         BackendInfo *bi_orig = be->bd_info;
44         struct ConfigOCs *be_cf_ocs = be->be_cf_ocs;
45         ConfigArgs ca = {0};
46         int rc = 0;
47
48         if ( oi->oi_orig->bi_db_config ) {
49                 be->bd_info = oi->oi_orig;
50                 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
51                 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
52                         argc, argv );
53
54                 if ( be->bd_info != oi->oi_orig ) {
55                         slap_overinfo   *oi2;
56                         slap_overinst   *on2, **onp;
57                         BackendDB       be2 = *be;
58                         int             i;
59
60                         /* a database added an overlay;
61                          * work it around... */
62                         assert( overlay_is_over( be ) );
63                         
64                         oi2 = ( slap_overinfo * )be->bd_info->bi_private;
65                         on2 = oi2->oi_list;
66
67                         /* need to put a uniqueness check here as well;
68                          * note that in principle there could be more than
69                          * one overlay as a result of multiple calls to
70                          * overlay_config() */
71                         be2.bd_info = (BackendInfo *)oi;
72
73                         for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
74                                 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
75                                         Debug( LDAP_DEBUG_ANY, "over_db_config(): "
76                                                         "warning, freshly added "
77                                                         "overlay #%d \"%s\" is already in list\n",
78                                                         i, (*onp)->on_bi.bi_type, 0 );
79
80                                         /* NOTE: if the overlay already exists,
81                                          * there is no way to merge the results
82                                          * of the configuration that may have 
83                                          * occurred during bi_db_config(); we
84                                          * just issue a warning, and the 
85                                          * administrator should deal with this */
86                                 }
87                         }
88                         *onp = oi->oi_list;
89
90                         oi->oi_list = on2;
91
92                         ch_free( be->bd_info );
93                 }
94
95                 be->bd_info = (BackendInfo *)oi;
96                 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
97         }
98
99         ca.argv = argv;
100         ca.argc = argc;
101         ca.fname = fname;
102         ca.lineno = lineno;
103         ca.be = be;
104         snprintf( ca.log, sizeof( ca.log ), "%s: line %d",
105                         ca.fname, ca.lineno );
106         ca.op = SLAP_CONFIG_ADD;
107         ca.valx = -1;
108
109         for (; on; on=on->on_next) {
110                 rc = SLAP_CONF_UNKNOWN;
111                 if (on->on_bi.bi_cf_ocs) {
112                         ConfigTable *ct;
113                         ca.bi = &on->on_bi;
114                         ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
115                         if ( ct ) {
116                                 ca.table = on->on_bi.bi_cf_ocs->co_type;
117                                 rc = config_add_vals( ct, &ca );
118                                 if ( rc != SLAP_CONF_UNKNOWN )
119                                         break;
120                         }
121                 }
122                 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
123                         be->bd_info = &on->on_bi;
124                         rc = on->on_bi.bi_db_config( be, fname, lineno,
125                                 argc, argv );
126                         if ( rc != SLAP_CONF_UNKNOWN ) break;
127                 }
128         }
129         be->bd_info = bi_orig;
130         be->be_cf_ocs = be_cf_ocs;
131         
132         return rc;
133 }
134
135 static int
136 over_db_open(
137         BackendDB *be,
138         ConfigReply *cr
139 )
140 {
141         slap_overinfo *oi = be->bd_info->bi_private;
142         slap_overinst *on = oi->oi_list;
143         BackendDB db = *be;
144         int rc = 0;
145
146         db.be_flags |= SLAP_DBFLAG_OVERLAY;
147         db.bd_info = oi->oi_orig;
148         if ( db.bd_info->bi_db_open ) {
149                 rc = db.bd_info->bi_db_open( &db, cr );
150         }
151
152         for (; on && rc == 0; on=on->on_next) {
153                 db.bd_info = &on->on_bi;
154                 if ( db.bd_info->bi_db_open ) {
155                         rc = db.bd_info->bi_db_open( &db, cr );
156                 }
157         }
158
159         return rc;
160 }
161
162 static int
163 over_db_close(
164         BackendDB *be,
165         ConfigReply *cr
166 )
167 {
168         slap_overinfo *oi = be->bd_info->bi_private;
169         slap_overinst *on = oi->oi_list;
170         BackendInfo *bi_orig = be->bd_info;
171         int rc = 0;
172
173         for (; on && rc == 0; on=on->on_next) {
174                 be->bd_info = &on->on_bi;
175                 if ( be->bd_info->bi_db_close ) {
176                         rc = be->bd_info->bi_db_close( be, cr );
177                 }
178         }
179
180         if ( oi->oi_orig->bi_db_close ) {
181                 be->bd_info = oi->oi_orig;
182                 rc = be->bd_info->bi_db_close( be, cr );
183         }
184
185         be->bd_info = bi_orig;
186         return rc;
187 }
188
189 static int
190 over_db_destroy(
191         BackendDB *be,
192         ConfigReply *cr
193 )
194 {
195         slap_overinfo *oi = be->bd_info->bi_private;
196         slap_overinst *on = oi->oi_list, *next;
197         BackendInfo *bi_orig = be->bd_info;
198         int rc = 0;
199
200         be->bd_info = oi->oi_orig;
201         if ( be->bd_info->bi_db_destroy ) {
202                 rc = be->bd_info->bi_db_destroy( be, cr );
203         }
204
205         for (; on && rc == 0; on=on->on_next) {
206                 be->bd_info = &on->on_bi;
207                 if ( be->bd_info->bi_db_destroy ) {
208                         rc = be->bd_info->bi_db_destroy( be, cr );
209                 }
210         }
211
212         on = oi->oi_list;
213         if ( on ) {
214                 for (next = on->on_next; on; on=next) {
215                         next = on->on_next;
216                         free( on );
217                 }
218         }
219         be->bd_info = bi_orig;
220         free( oi );
221         return rc;
222 }
223
224 static int
225 over_back_response ( Operation *op, SlapReply *rs )
226 {
227         slap_overinfo *oi = op->o_callback->sc_private;
228         slap_overinst *on = oi->oi_list;
229         int rc = SLAP_CB_CONTINUE;
230         BackendDB *be = op->o_bd, db = *op->o_bd;
231
232         db.be_flags |= SLAP_DBFLAG_OVERLAY;
233         op->o_bd = &db;
234         for (; on; on=on->on_next ) {
235                 if ( on->on_response ) {
236                         db.bd_info = (BackendInfo *)on;
237                         rc = on->on_response( op, rs );
238                         if ( rc != SLAP_CB_CONTINUE ) break;
239                 }
240         }
241         /* Bypass the remaining on_response layers, but allow
242          * normal execution to continue.
243          */
244         if ( rc == SLAP_CB_BYPASS )
245                 rc = SLAP_CB_CONTINUE;
246         op->o_bd = be;
247         return rc;
248 }
249
250 static int
251 over_access_allowed(
252         Operation               *op,
253         Entry                   *e,
254         AttributeDescription    *desc,
255         struct berval           *val,
256         slap_access_t           access,
257         AccessControlState      *state,
258         slap_mask_t             *maskp )
259 {
260         slap_overinfo *oi;
261         slap_overinst *on;
262         BackendInfo *bi;
263         BackendDB *be = op->o_bd, db;
264         int rc = SLAP_CB_CONTINUE;
265
266         /* FIXME: used to happen for instance during abandon
267          * when global overlays are used... */
268         assert( op->o_bd != NULL );
269
270         bi = op->o_bd->bd_info;
271         /* Were we invoked on the frontend? */
272         if ( !bi->bi_access_allowed ) {
273                 oi = frontendDB->bd_info->bi_private;
274         } else {
275                 oi = op->o_bd->bd_info->bi_private;
276         }
277         on = oi->oi_list;
278
279         for ( ; on; on = on->on_next ) {
280                 if ( on->on_bi.bi_access_allowed ) {
281                         /* NOTE: do not copy the structure until required */
282                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
283                                 db = *op->o_bd;
284                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
285                                 op->o_bd = &db;
286                         }
287
288                         op->o_bd->bd_info = (BackendInfo *)on;
289                         rc = on->on_bi.bi_access_allowed( op, e,
290                                 desc, val, access, state, maskp );
291                         if ( rc != SLAP_CB_CONTINUE ) break;
292                 }
293         }
294
295         if ( rc == SLAP_CB_CONTINUE ) {
296                 BI_access_allowed       *bi_access_allowed;
297
298                 /* if the database structure was changed, o_bd points to a
299                  * copy of the structure; put the original bd_info in place */
300                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
301                         op->o_bd->bd_info = oi->oi_orig;
302                 }
303
304                 if ( oi->oi_orig->bi_access_allowed ) {
305                         bi_access_allowed = oi->oi_orig->bi_access_allowed;
306                 } else {
307                         bi_access_allowed = slap_access_allowed;
308                 }
309
310                 rc = bi_access_allowed( op, e,
311                         desc, val, access, state, maskp );
312         }
313         /* should not fall thru this far without anything happening... */
314         if ( rc == SLAP_CB_CONTINUE ) {
315                 /* access not allowed */
316                 rc = 0;
317         }
318
319         op->o_bd = be;
320         op->o_bd->bd_info = bi;
321
322         return rc;
323 }
324
325 int
326 overlay_entry_get_ov(
327         Operation               *op,
328         struct berval   *dn,
329         ObjectClass             *oc,
330         AttributeDescription    *ad,
331         int     rw,
332         Entry   **e,
333         slap_overinst *on )
334 {
335         slap_overinfo *oi = on->on_info;
336         BackendDB *be = op->o_bd, db;
337         BackendInfo *bi = op->o_bd->bd_info;
338         int rc = SLAP_CB_CONTINUE;
339
340         for ( ; on; on = on->on_next ) {
341                 if ( on->on_bi.bi_entry_get_rw ) {
342                         /* NOTE: do not copy the structure until required */
343                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
344                                 db = *op->o_bd;
345                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
346                                 op->o_bd = &db;
347                         }
348
349                         op->o_bd->bd_info = (BackendInfo *)on;
350                         rc = on->on_bi.bi_entry_get_rw( op, dn,
351                                 oc, ad, rw, e );
352                         if ( rc != SLAP_CB_CONTINUE ) break;
353                 }
354         }
355
356         if ( rc == SLAP_CB_CONTINUE ) {
357                 /* if the database structure was changed, o_bd points to a
358                  * copy of the structure; put the original bd_info in place */
359                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
360                         op->o_bd->bd_info = oi->oi_orig;
361                 }
362
363                 if ( oi->oi_orig->bi_entry_get_rw ) {
364                         rc = oi->oi_orig->bi_entry_get_rw( op, dn,
365                                 oc, ad, rw, e );
366                 }
367         }
368         /* should not fall thru this far without anything happening... */
369         if ( rc == SLAP_CB_CONTINUE ) {
370                 rc = LDAP_UNWILLING_TO_PERFORM;
371         }
372
373         op->o_bd = be;
374         op->o_bd->bd_info = bi;
375
376         return rc;
377 }
378
379 static int
380 over_entry_get_rw(
381         Operation               *op,
382         struct berval   *dn,
383         ObjectClass             *oc,
384         AttributeDescription    *ad,
385         int     rw,
386         Entry   **e )
387 {
388         slap_overinfo *oi;
389         slap_overinst *on;
390
391         assert( op->o_bd != NULL );
392
393         oi = op->o_bd->bd_info->bi_private;
394         on = oi->oi_list;
395
396         return overlay_entry_get_ov( op, dn, oc, ad, rw, e, on );
397 }
398
399 int
400 overlay_entry_release_ov(
401         Operation       *op,
402         Entry   *e,
403         int rw,
404         slap_overinst *on )
405 {
406         slap_overinfo *oi = on->on_info;
407         BackendDB *be = op->o_bd, db;
408         BackendInfo *bi = op->o_bd->bd_info;
409         int rc = SLAP_CB_CONTINUE;
410
411         for ( ; on; on = on->on_next ) {
412                 if ( on->on_bi.bi_entry_release_rw ) {
413                         /* NOTE: do not copy the structure until required */
414                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
415                                 db = *op->o_bd;
416                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
417                                 op->o_bd = &db;
418                         }
419
420                         op->o_bd->bd_info = (BackendInfo *)on;
421                         rc = on->on_bi.bi_entry_release_rw( op, e, rw );
422                         if ( rc != SLAP_CB_CONTINUE ) break;
423                 }
424         }
425
426         if ( rc == SLAP_CB_CONTINUE ) {
427                 /* if the database structure was changed, o_bd points to a
428                  * copy of the structure; put the original bd_info in place */
429                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
430                         op->o_bd->bd_info = oi->oi_orig;
431                 }
432
433                 if ( oi->oi_orig->bi_entry_release_rw ) {
434                         rc = oi->oi_orig->bi_entry_release_rw( op, e, rw );
435                 }
436         }
437         /* should not fall thru this far without anything happening... */
438         if ( rc == SLAP_CB_CONTINUE ) {
439                 entry_free( e );
440                 rc = 0;
441         }
442
443         op->o_bd = be;
444         op->o_bd->bd_info = bi;
445
446         return rc;
447 }
448
449 static int
450 over_entry_release_rw(
451         Operation       *op,
452         Entry   *e,
453         int rw )
454 {
455         slap_overinfo *oi;
456         slap_overinst *on;
457
458         assert( op->o_bd != NULL );
459
460         oi = op->o_bd->bd_info->bi_private;
461         on = oi->oi_list;
462
463         return overlay_entry_release_ov( op, e, rw, on );
464 }
465
466 static int
467 over_acl_group(
468         Operation               *op,
469         Entry                   *e,
470         struct berval           *gr_ndn,
471         struct berval           *op_ndn,
472         ObjectClass             *group_oc,
473         AttributeDescription    *group_at )
474 {
475         slap_overinfo *oi;
476         slap_overinst *on;
477         BackendInfo *bi;
478         BackendDB *be = op->o_bd, db;
479         int rc = SLAP_CB_CONTINUE;
480
481         /* FIXME: used to happen for instance during abandon
482          * when global overlays are used... */
483         assert( be != NULL );
484
485         bi = be->bd_info;
486         oi = bi->bi_private;
487         on = oi->oi_list;
488
489         for ( ; on; on = on->on_next ) {
490                 if ( on->on_bi.bi_acl_group ) {
491                         /* NOTE: do not copy the structure until required */
492                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
493                                 db = *op->o_bd;
494                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
495                                 op->o_bd = &db;
496                         }
497
498                         op->o_bd->bd_info = (BackendInfo *)on;
499                         rc = on->on_bi.bi_acl_group( op, e,
500                                 gr_ndn, op_ndn, group_oc, group_at );
501                         if ( rc != SLAP_CB_CONTINUE ) break;
502                 }
503         }
504
505         if ( rc == SLAP_CB_CONTINUE ) {
506                 BI_acl_group            *bi_acl_group;
507
508                 /* if the database structure was changed, o_bd points to a
509                  * copy of the structure; put the original bd_info in place */
510                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
511                         op->o_bd->bd_info = oi->oi_orig;
512                 }
513
514                 if ( oi->oi_orig->bi_acl_group ) {
515                         bi_acl_group = oi->oi_orig->bi_acl_group;
516                 } else {
517                         bi_acl_group = backend_group;
518                 }
519
520                 rc = bi_acl_group( op, e,
521                         gr_ndn, op_ndn, group_oc, group_at );
522         }
523         /* should not fall thru this far without anything happening... */
524         if ( rc == SLAP_CB_CONTINUE ) {
525                 /* access not allowed */
526                 rc = 0;
527         }
528
529         op->o_bd = be;
530         op->o_bd->bd_info = bi;
531
532         return rc;
533 }
534
535 static int
536 over_acl_attribute(
537         Operation               *op,
538         Entry                   *target,
539         struct berval           *entry_ndn,
540         AttributeDescription    *entry_at,
541         BerVarray               *vals,
542         slap_access_t           access )
543 {
544         slap_overinfo *oi;
545         slap_overinst *on;
546         BackendInfo *bi;
547         BackendDB *be = op->o_bd, db;
548         int rc = SLAP_CB_CONTINUE;
549
550         /* FIXME: used to happen for instance during abandon
551          * when global overlays are used... */
552         assert( be != NULL );
553
554         bi = be->bd_info;
555         oi = bi->bi_private;
556         on = oi->oi_list;
557
558         for ( ; on; on = on->on_next ) {
559                 if ( on->on_bi.bi_acl_attribute ) {
560                         /* NOTE: do not copy the structure until required */
561                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
562                                 db = *op->o_bd;
563                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
564                                 op->o_bd = &db;
565                         }
566
567                         op->o_bd->bd_info = (BackendInfo *)on;
568                         rc = on->on_bi.bi_acl_attribute( op, target,
569                                 entry_ndn, entry_at, vals, access );
570                         if ( rc != SLAP_CB_CONTINUE ) break;
571                 }
572         }
573
574         if ( rc == SLAP_CB_CONTINUE ) {
575                 BI_acl_attribute                *bi_acl_attribute;
576
577                 /* if the database structure was changed, o_bd points to a
578                  * copy of the structure; put the original bd_info in place */
579                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
580                         op->o_bd->bd_info = oi->oi_orig;
581                 }
582
583                 if ( oi->oi_orig->bi_acl_attribute ) {
584                         bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
585                 } else {
586                         bi_acl_attribute = backend_attribute;
587                 }
588
589                 rc = bi_acl_attribute( op, target,
590                         entry_ndn, entry_at, vals, access );
591         }
592         /* should not fall thru this far without anything happening... */
593         if ( rc == SLAP_CB_CONTINUE ) {
594                 /* access not allowed */
595                 rc = 0;
596         }
597
598         op->o_bd = be;
599         op->o_bd->bd_info = bi;
600
601         return rc;
602 }
603
604 int
605 overlay_callback_after_backover( Operation *op, slap_callback *sc, int append )
606 {
607         slap_callback **scp;
608
609         for ( scp = &op->o_callback; *scp != NULL; scp = &(*scp)->sc_next ) {
610                 if ( (*scp)->sc_response == over_back_response ) {
611                         sc->sc_next = (*scp)->sc_next;
612                         (*scp)->sc_next = sc;
613                         return 0;
614                 }
615         }
616
617         if ( append ) {
618                 *scp = sc;
619                 return 0;
620         }
621
622         return 1;
623 }
624
625 /*
626  * default return code in case of missing backend function
627  * and overlay stack returning SLAP_CB_CONTINUE
628  */
629 static int op_rc[ op_last ] = {
630         LDAP_UNWILLING_TO_PERFORM,      /* bind */
631         LDAP_UNWILLING_TO_PERFORM,      /* unbind */
632         LDAP_UNWILLING_TO_PERFORM,      /* search */
633         SLAP_CB_CONTINUE,               /* compare; pass to frontend */
634         LDAP_UNWILLING_TO_PERFORM,      /* modify */
635         LDAP_UNWILLING_TO_PERFORM,      /* modrdn */
636         LDAP_UNWILLING_TO_PERFORM,      /* add */
637         LDAP_UNWILLING_TO_PERFORM,      /* delete */
638         LDAP_UNWILLING_TO_PERFORM,      /* abandon */
639         LDAP_UNWILLING_TO_PERFORM,      /* cancel */
640         LDAP_UNWILLING_TO_PERFORM,      /* extended */
641         LDAP_SUCCESS,                   /* aux_operational */
642         LDAP_SUCCESS,                   /* aux_chk_referrals */
643         SLAP_CB_CONTINUE                /* aux_chk_controls; pass to frontend */
644 };
645
646 int overlay_op_walk(
647         Operation *op,
648         SlapReply *rs,
649         slap_operation_t which,
650         slap_overinfo *oi,
651         slap_overinst *on
652 )
653 {
654         BI_op_bind **func;
655         int rc = SLAP_CB_CONTINUE;
656
657         for (; on; on=on->on_next ) {
658                 func = &on->on_bi.bi_op_bind;
659                 if ( func[which] ) {
660                         op->o_bd->bd_info = (BackendInfo *)on;
661                         rc = func[which]( op, rs );
662                         if ( rc != SLAP_CB_CONTINUE ) break;
663                 }
664         }
665         if ( rc == SLAP_CB_BYPASS )
666                 rc = SLAP_CB_CONTINUE;
667
668         func = &oi->oi_orig->bi_op_bind;
669         if ( func[which] && rc == SLAP_CB_CONTINUE ) {
670                 op->o_bd->bd_info = oi->oi_orig;
671                 rc = func[which]( op, rs );
672         }
673         /* should not fall thru this far without anything happening... */
674         if ( rc == SLAP_CB_CONTINUE ) {
675                 rc = op_rc[ which ];
676         }
677
678         /* The underlying backend didn't handle the request, make sure
679          * overlay cleanup is processed.
680          */
681         if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
682                 slap_callback *sc_next;
683                 for ( ; op->o_callback && op->o_callback->sc_response !=
684                         over_back_response; op->o_callback = sc_next ) {
685                         sc_next = op->o_callback->sc_next;
686                         if ( op->o_callback->sc_cleanup ) {
687                                 op->o_callback->sc_cleanup( op, rs );
688                         }
689                 }
690         }
691         return rc;
692 }
693
694 static int
695 over_op_func(
696         Operation *op,
697         SlapReply *rs,
698         slap_operation_t which
699 )
700 {
701         slap_overinfo *oi;
702         slap_overinst *on;
703         BackendDB *be = op->o_bd, db;
704         slap_callback cb = {NULL, over_back_response, NULL, NULL}, **sc;
705         int rc = SLAP_CB_CONTINUE;
706
707         /* FIXME: used to happen for instance during abandon
708          * when global overlays are used... */
709         assert( op->o_bd != NULL );
710
711         oi = op->o_bd->bd_info->bi_private;
712         on = oi->oi_list;
713
714         if ( !SLAP_ISOVERLAY( op->o_bd )) {
715                 db = *op->o_bd;
716                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
717                 op->o_bd = &db;
718         }
719         cb.sc_next = op->o_callback;
720         cb.sc_private = oi;
721         op->o_callback = &cb;
722
723         rc = overlay_op_walk( op, rs, which, oi, on );
724         for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) {
725                 if ( *sc == &cb ) {
726                         *sc = cb.sc_next;
727                         break;
728                 }
729         }
730
731         op->o_bd = be;
732         return rc;
733 }
734
735 static int
736 over_op_bind( Operation *op, SlapReply *rs )
737 {
738         return over_op_func( op, rs, op_bind );
739 }
740
741 static int
742 over_op_unbind( Operation *op, SlapReply *rs )
743 {
744         return over_op_func( op, rs, op_unbind );
745 }
746
747 static int
748 over_op_search( Operation *op, SlapReply *rs )
749 {
750         return over_op_func( op, rs, op_search );
751 }
752
753 static int
754 over_op_compare( Operation *op, SlapReply *rs )
755 {
756         return over_op_func( op, rs, op_compare );
757 }
758
759 static int
760 over_op_modify( Operation *op, SlapReply *rs )
761 {
762         return over_op_func( op, rs, op_modify );
763 }
764
765 static int
766 over_op_modrdn( Operation *op, SlapReply *rs )
767 {
768         return over_op_func( op, rs, op_modrdn );
769 }
770
771 static int
772 over_op_add( Operation *op, SlapReply *rs )
773 {
774         return over_op_func( op, rs, op_add );
775 }
776
777 static int
778 over_op_delete( Operation *op, SlapReply *rs )
779 {
780         return over_op_func( op, rs, op_delete );
781 }
782
783 static int
784 over_op_abandon( Operation *op, SlapReply *rs )
785 {
786         return over_op_func( op, rs, op_abandon );
787 }
788
789 static int
790 over_op_cancel( Operation *op, SlapReply *rs )
791 {
792         return over_op_func( op, rs, op_cancel );
793 }
794
795 static int
796 over_op_extended( Operation *op, SlapReply *rs )
797 {
798         return over_op_func( op, rs, op_extended );
799 }
800
801 static int
802 over_aux_operational( Operation *op, SlapReply *rs )
803 {
804         return over_op_func( op, rs, op_aux_operational );
805 }
806
807 static int
808 over_aux_chk_referrals( Operation *op, SlapReply *rs )
809 {
810         return over_op_func( op, rs, op_aux_chk_referrals );
811 }
812
813 static int
814 over_aux_chk_controls( Operation *op, SlapReply *rs )
815 {
816         return over_op_func( op, rs, op_aux_chk_controls );
817 }
818
819 enum conn_which {
820         conn_init = 0,
821         conn_destroy,
822         conn_last
823 };
824
825 static int
826 over_connection_func(
827         BackendDB       *bd,
828         Connection      *conn,
829         enum conn_which which
830 )
831 {
832         slap_overinfo           *oi;
833         slap_overinst           *on;
834         BackendDB               db;
835         int                     rc = SLAP_CB_CONTINUE;
836         BI_connection_init      **func;
837
838         /* FIXME: used to happen for instance during abandon
839          * when global overlays are used... */
840         assert( bd != NULL );
841
842         oi = bd->bd_info->bi_private;
843         on = oi->oi_list;
844
845         if ( !SLAP_ISOVERLAY( bd ) ) {
846                 db = *bd;
847                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
848                 bd = &db;
849         }
850
851         for ( ; on; on = on->on_next ) {
852                 func = &on->on_bi.bi_connection_init;
853                 if ( func[ which ] ) {
854                         bd->bd_info = (BackendInfo *)on;
855                         rc = func[ which ]( bd, conn );
856                         if ( rc != SLAP_CB_CONTINUE ) break;
857                 }
858         }
859
860         func = &oi->oi_orig->bi_connection_init;
861         if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
862                 bd->bd_info = oi->oi_orig;
863                 rc = func[ which ]( bd, conn );
864         }
865         /* should not fall thru this far without anything happening... */
866         if ( rc == SLAP_CB_CONTINUE ) {
867                 rc = LDAP_UNWILLING_TO_PERFORM;
868         }
869
870         return rc;
871 }
872
873 static int
874 over_connection_init(
875         BackendDB       *bd,
876         Connection      *conn
877 )
878 {
879         return over_connection_func( bd, conn, conn_init );
880 }
881
882 static int
883 over_connection_destroy(
884         BackendDB       *bd,
885         Connection      *conn
886 )
887 {
888         return over_connection_func( bd, conn, conn_destroy );
889 }
890
891 int
892 overlay_register(
893         slap_overinst *on
894 )
895 {
896         slap_overinst   *tmp;
897
898         /* FIXME: check for duplicates? */
899         for ( tmp = overlays; tmp != NULL; tmp = tmp->on_next ) {
900                 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_type ) == 0 ) {
901                         Debug( LDAP_DEBUG_ANY,
902                                 "overlay_register(\"%s\"): "
903                                 "name already in use.\n",
904                                 on->on_bi.bi_type, 0, 0 );
905                         return -1;
906                 }
907
908                 if ( on->on_bi.bi_obsolete_names != NULL ) {
909                         int     i;
910
911                         for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
912                                 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], tmp->on_bi.bi_type ) == 0 ) {
913                                         Debug( LDAP_DEBUG_ANY,
914                                                 "overlay_register(\"%s\"): "
915                                                 "obsolete name \"%s\" already in use "
916                                                 "by overlay \"%s\".\n",
917                                                 on->on_bi.bi_type,
918                                                 on->on_bi.bi_obsolete_names[ i ],
919                                                 tmp->on_bi.bi_type );
920                                         return -1;
921                                 }
922                         }
923                 }
924
925                 if ( tmp->on_bi.bi_obsolete_names != NULL ) {
926                         int     i;
927
928                         for ( i = 0; tmp->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
929                                 int     j;
930
931                                 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
932                                         Debug( LDAP_DEBUG_ANY,
933                                                 "overlay_register(\"%s\"): "
934                                                 "name already in use "
935                                                 "as obsolete by overlay \"%s\".\n",
936                                                 on->on_bi.bi_type,
937                                                 tmp->on_bi.bi_obsolete_names[ i ], 0 );
938                                         return -1;
939                                 }
940
941                                 if ( on->on_bi.bi_obsolete_names != NULL ) {
942                                         for ( j = 0; on->on_bi.bi_obsolete_names[ j ] != NULL; j++ ) {
943                                                 if ( strcmp( on->on_bi.bi_obsolete_names[ j ], tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
944                                                         Debug( LDAP_DEBUG_ANY,
945                                                                 "overlay_register(\"%s\"): "
946                                                                 "obsolete name \"%s\" already in use "
947                                                                 "as obsolete by overlay \"%s\".\n",
948                                                                 on->on_bi.bi_type,
949                                                                 on->on_bi.bi_obsolete_names[ j ],
950                                                                 tmp->on_bi.bi_type );
951                                                         return -1;
952                                                 }
953                                         }
954                                 }
955                         }
956                 }
957         }
958
959         on->on_next = overlays;
960         overlays = on;
961         return 0;
962 }
963
964 /*
965  * iterator on registered overlays; overlay_next( NULL ) returns the first
966  * overlay; subsequent calls with the previously returned value allow to 
967  * iterate over the entire list; returns NULL when no more overlays are 
968  * registered.
969  */
970
971 slap_overinst *
972 overlay_next(
973         slap_overinst *on
974 )
975 {
976         if ( on == NULL ) {
977                 return overlays;
978         }
979
980         return on->on_next;
981 }
982
983 /*
984  * returns a specific registered overlay based on the type; NULL if not
985  * registered.
986  */
987
988 slap_overinst *
989 overlay_find( const char *over_type )
990 {
991         slap_overinst *on = overlays;
992
993         assert( over_type != NULL );
994
995         for ( ; on; on = on->on_next ) {
996                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
997                         goto foundit;
998                 }
999
1000                 if ( on->on_bi.bi_obsolete_names != NULL ) {
1001                         int     i;
1002
1003                         for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
1004                                 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], over_type ) == 0 ) {
1005                                         Debug( LDAP_DEBUG_ANY,
1006                                                 "overlay_find(\"%s\"): "
1007                                                 "obsolete name for \"%s\".\n",
1008                                                 on->on_bi.bi_obsolete_names[ i ],
1009                                                 on->on_bi.bi_type, 0 );
1010                                         goto foundit;
1011                                 }
1012                         }
1013                 }
1014         }
1015
1016 foundit:;
1017         return on;
1018 }
1019
1020 static const char overtype[] = "over";
1021
1022 /*
1023  * returns TRUE (1) if the database is actually an overlay instance;
1024  * FALSE (0) otherwise.
1025  */
1026
1027 int
1028 overlay_is_over( BackendDB *be )
1029 {
1030         return be->bd_info->bi_type == overtype;
1031 }
1032
1033 /*
1034  * returns TRUE (1) if the given database is actually an overlay
1035  * instance and, somewhere in the list, contains the requested overlay;
1036  * FALSE (0) otherwise.
1037  */
1038
1039 int
1040 overlay_is_inst( BackendDB *be, const char *over_type )
1041 {
1042         slap_overinst   *on;
1043
1044         assert( be != NULL );
1045
1046         if ( !overlay_is_over( be ) ) {
1047                 return 0;
1048         }
1049         
1050         on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
1051         for ( ; on; on = on->on_next ) {
1052                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1053                         return 1;
1054                 }
1055         }
1056
1057         return 0;
1058 }
1059
1060 int
1061 overlay_register_control( BackendDB *be, const char *oid )
1062 {
1063         int             gotit = 0;
1064         int             cid;
1065
1066         if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1067                 return -1;
1068         }
1069
1070         if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1071                 BackendDB *bd;
1072                 
1073                 /* add to all backends... */
1074                 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1075                         if ( bd == be->bd_self ) {
1076                                 gotit = 1;
1077                         }
1078
1079                         /* overlays can be instanciated multiple times, use
1080                          * be_ctrls[ cid ] as an instance counter, so that the
1081                          * overlay's controls are only really disabled after the
1082                          * last instance called overlay_register_control() */
1083                         bd->be_ctrls[ cid ]++;
1084                         bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1085                 }
1086
1087         }
1088         
1089         if ( !gotit ) {
1090                 /* overlays can be instanciated multiple times, use
1091                  * be_ctrls[ cid ] as an instance counter, so that the
1092                  * overlay's controls are only really unregistered after the
1093                  * last instance called overlay_register_control() */
1094                 be->bd_self->be_ctrls[ cid ]++;
1095                 be->bd_self->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1096         }
1097
1098         return 0;
1099 }
1100
1101 #ifdef SLAP_CONFIG_DELETE
1102 void
1103 overlay_unregister_control( BackendDB *be, const char *oid )
1104 {
1105         int             gotit = 0;
1106         int             cid;
1107
1108         if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1109                 return;
1110         }
1111
1112         if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1113                 BackendDB *bd;
1114
1115                 /* remove from all backends... */
1116                 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1117                         if ( bd == be->bd_self ) {
1118                                 gotit = 1;
1119                         }
1120
1121                         bd->be_ctrls[ cid ]--;
1122                 }
1123         }
1124
1125         if ( !gotit ) {
1126                 be->bd_self->be_ctrls[ cid ]--;
1127         }
1128 }
1129 #endif /* SLAP_CONFIG_DELETE */
1130
1131 void
1132 overlay_destroy_one( BackendDB *be, slap_overinst *on )
1133 {
1134         slap_overinfo *oi = on->on_info;
1135         slap_overinst **oidx;
1136
1137         for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1138                 if ( *oidx == on ) {
1139                         *oidx = on->on_next;
1140                         if ( on->on_bi.bi_db_destroy ) {
1141                                 BackendInfo *bi_orig = be->bd_info;
1142                                 be->bd_info = (BackendInfo *)on;
1143                                 on->on_bi.bi_db_destroy( be, NULL );
1144                                 be->bd_info = bi_orig;
1145                         }
1146                         free( on );
1147                         break;
1148                 }
1149         }
1150 }
1151
1152 #ifdef SLAP_CONFIG_DELETE
1153 typedef struct ov_remove_ctx {
1154         BackendDB *be;
1155         slap_overinst *on;
1156 } ov_remove_ctx;
1157
1158 int
1159 overlay_remove_cb( Operation *op, SlapReply *rs )
1160 {
1161         ov_remove_ctx *rm_ctx = (ov_remove_ctx*) op->o_callback->sc_private;
1162         BackendInfo *bi_orig = rm_ctx->be->bd_info;
1163
1164         rm_ctx->be->bd_info = (BackendInfo*) rm_ctx->on;
1165
1166         if ( rm_ctx->on->on_bi.bi_db_close ) {
1167                 rm_ctx->on->on_bi.bi_db_close( rm_ctx->be, NULL );
1168         }
1169         if ( rm_ctx->on->on_bi.bi_db_destroy ) {
1170                 rm_ctx->on->on_bi.bi_db_destroy( rm_ctx->be, NULL );
1171         }
1172
1173         /* clean up after removing last overlay */
1174         if ( ! rm_ctx->on->on_info->oi_list ) {
1175                 /* reset db flags and bd_info to orig */
1176                 SLAP_DBFLAGS( rm_ctx->be ) &= ~SLAP_DBFLAG_GLOBAL_OVERLAY;
1177                 rm_ctx->be->bd_info = rm_ctx->on->on_info->oi_orig;
1178                 ch_free(rm_ctx->on->on_info);
1179         } else {
1180                 rm_ctx->be->bd_info = bi_orig;
1181         }
1182         free( rm_ctx->on );
1183         op->o_tmpfree(rm_ctx, op->o_tmpmemctx);
1184         return SLAP_CB_CONTINUE;
1185 }
1186
1187 void
1188 overlay_remove( BackendDB *be, slap_overinst *on, Operation *op )
1189 {
1190         slap_overinfo *oi = on->on_info;
1191         slap_overinst **oidx;
1192         ov_remove_ctx *rm_ctx;
1193         slap_callback *rm_cb, *cb;
1194
1195         /* remove overlay from oi_list */
1196         for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1197                 if ( *oidx == on ) {
1198                         *oidx = on->on_next;
1199                         break;
1200                 }
1201         }
1202
1203         /* The db_close and db_destroy handlers to cleanup a release
1204          * the overlay's resources are called from the cleanup callback
1205          */
1206         rm_ctx = op->o_tmpalloc( sizeof( ov_remove_ctx ), op->o_tmpmemctx );
1207         rm_ctx->be = be;
1208         rm_ctx->on = on;
1209
1210         rm_cb = op->o_tmpalloc( sizeof( slap_callback ), op->o_tmpmemctx );
1211         rm_cb->sc_next = NULL;
1212         rm_cb->sc_cleanup = overlay_remove_cb;
1213         rm_cb->sc_response = NULL;
1214         rm_cb->sc_private = (void*) rm_ctx;
1215
1216         /* Append callback to the end of the list */
1217         if ( !op->o_callback ) {
1218                 op->o_callback = rm_cb;
1219         } else {
1220                 for ( cb = op->o_callback; cb->sc_next; cb = cb->sc_next );
1221                 cb->sc_next = rm_cb;
1222         }
1223 }
1224 #endif /* SLAP_CONFIG_DELETE */
1225
1226 void
1227 overlay_insert( BackendDB *be, slap_overinst *on2, slap_overinst ***prev,
1228         int idx )
1229 {
1230         slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1231
1232         if ( idx == -1 ) {
1233                 on2->on_next = oi->oi_list;
1234                 oi->oi_list = on2;
1235         } else {
1236                 int i, novs;
1237                 slap_overinst *on, **prev;
1238
1239                 /* Since the list is in reverse order and is singly linked,
1240                  * we have to count the overlays and then insert backwards.
1241                  * Adding on overlay at a specific point should be a pretty
1242                  * infrequent occurrence.
1243                  */
1244                 novs = 0;
1245                 for ( on = oi->oi_list; on; on=on->on_next )
1246                         novs++;
1247
1248                 if (idx > novs)
1249                         idx = 0;
1250                 else
1251                         idx = novs - idx;
1252
1253                 /* advance to insertion point */
1254                 prev = &oi->oi_list;
1255                 for ( i=0; i<idx; i++ ) {
1256                         on = *prev;
1257                         prev = &on->on_next;
1258                 }
1259                 /* insert */
1260                 on2->on_next = *prev;
1261                 *prev = on2;
1262         }
1263 }
1264
1265 void
1266 overlay_move( BackendDB *be, slap_overinst *on, int idx )
1267 {
1268         slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1269         slap_overinst **onp;
1270
1271         for (onp = &oi->oi_list; *onp; onp= &(*onp)->on_next) {
1272                 if ( *onp == on ) {
1273                         *onp = on->on_next;
1274                         break;
1275                 }
1276         }
1277         overlay_insert( be, on, &onp, idx );
1278 }
1279
1280 /* add an overlay to a particular backend. */
1281 int
1282 overlay_config( BackendDB *be, const char *ov, int idx, BackendInfo **res, ConfigReply *cr )
1283 {
1284         slap_overinst *on = NULL, *on2 = NULL, **prev;
1285         slap_overinfo *oi = NULL;
1286         BackendInfo *bi = NULL;
1287
1288         if ( res )
1289                 *res = NULL;
1290
1291         on = overlay_find( ov );
1292         if ( !on ) {
1293                 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
1294                 return 1;
1295         }
1296
1297         /* If this is the first overlay on this backend, set up the
1298          * overlay info structure
1299          */
1300         if ( !overlay_is_over( be ) ) {
1301                 int     isglobal = 0;
1302
1303                 /* NOTE: the first time a global overlay is configured,
1304                  * frontendDB gets this flag; it is used later by overlays
1305                  * to determine if they're stacked on top of the frontendDB */
1306                 if ( be->bd_info == frontendDB->bd_info || SLAP_ISGLOBALOVERLAY( be ) ) {
1307                         isglobal = 1;
1308                         if ( on->on_bi.bi_flags & SLAPO_BFLAG_DBONLY ) {
1309                                 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1310                                         "overlay \"%s\" cannot be global.\n",
1311                                         ov, 0, 0 );
1312                                 return 1;
1313                         }
1314
1315                 } else if ( on->on_bi.bi_flags & SLAPO_BFLAG_GLOBONLY ) {
1316                         Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1317                                 "overlay \"%s\" can only be global.\n",
1318                                 ov, 0, 0 );
1319                         return 1;
1320                 }
1321
1322                 oi = ch_malloc( sizeof( slap_overinfo ) );
1323                 oi->oi_orig = be->bd_info;
1324                 oi->oi_bi = *be->bd_info;
1325                 oi->oi_origdb = be;
1326
1327                 if ( isglobal ) {
1328                         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
1329                 }
1330
1331                 /* Save a pointer to ourself in bi_private.
1332                  */
1333                 oi->oi_bi.bi_private = oi;
1334                 oi->oi_list = NULL;
1335                 bi = (BackendInfo *)oi;
1336
1337                 bi->bi_type = (char *)overtype;
1338
1339                 bi->bi_db_config = over_db_config;
1340                 bi->bi_db_open = over_db_open;
1341                 bi->bi_db_close = over_db_close;
1342                 bi->bi_db_destroy = over_db_destroy;
1343
1344                 bi->bi_op_bind = over_op_bind;
1345                 bi->bi_op_unbind = over_op_unbind;
1346                 bi->bi_op_search = over_op_search;
1347                 bi->bi_op_compare = over_op_compare;
1348                 bi->bi_op_modify = over_op_modify;
1349                 bi->bi_op_modrdn = over_op_modrdn;
1350                 bi->bi_op_add = over_op_add;
1351                 bi->bi_op_delete = over_op_delete;
1352                 bi->bi_op_abandon = over_op_abandon;
1353                 bi->bi_op_cancel = over_op_cancel;
1354
1355                 bi->bi_extended = over_op_extended;
1356
1357                 /*
1358                  * this is fine because it has the same
1359                  * args of the operations; we need to rework
1360                  * all the hooks to share the same args
1361                  * of the operations...
1362                  */
1363                 bi->bi_operational = over_aux_operational;
1364                 bi->bi_chk_referrals = over_aux_chk_referrals;
1365                 bi->bi_chk_controls = over_aux_chk_controls;
1366
1367                 /* these have specific arglists */
1368                 bi->bi_entry_get_rw = over_entry_get_rw;
1369                 bi->bi_entry_release_rw = over_entry_release_rw;
1370                 bi->bi_access_allowed = over_access_allowed;
1371                 bi->bi_acl_group = over_acl_group;
1372                 bi->bi_acl_attribute = over_acl_attribute;
1373                 
1374                 bi->bi_connection_init = over_connection_init;
1375                 bi->bi_connection_destroy = over_connection_destroy;
1376
1377                 be->bd_info = bi;
1378
1379         } else {
1380                 if ( overlay_is_inst( be, ov ) ) {
1381                         if ( SLAPO_SINGLE( be ) ) {
1382                                 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1383                                         "overlay \"%s\" already in list\n",
1384                                         ov, 0, 0 );
1385                                 return 1;
1386                         }
1387                 }
1388
1389                 oi = be->bd_info->bi_private;
1390         }
1391
1392         /* Insert new overlay into list. By default overlays are
1393          * added to head of list and executed in LIFO order.
1394          */
1395         on2 = ch_calloc( 1, sizeof(slap_overinst) );
1396         *on2 = *on;
1397         on2->on_info = oi;
1398
1399         prev = &oi->oi_list;
1400         /* Do we need to find the insertion point? */
1401         if ( idx >= 0 ) {
1402                 int i;
1403
1404                 /* count current overlays */
1405                 for ( i=0, on=oi->oi_list; on; on=on->on_next, i++ );
1406
1407                 /* are we just appending a new one? */
1408                 if ( idx >= i )
1409                         idx = -1;
1410         }
1411         overlay_insert( be, on2, &prev, idx );
1412
1413         /* Any initialization needed? */
1414         if ( on2->on_bi.bi_db_init ) {
1415                 int rc;
1416                 be->bd_info = (BackendInfo *)on2;
1417                 rc = on2->on_bi.bi_db_init( be, cr);
1418                 be->bd_info = (BackendInfo *)oi;
1419                 if ( rc ) {
1420                         *prev = on2->on_next;
1421                         ch_free( on2 );
1422                         on2 = NULL;
1423                         return rc;
1424                 }
1425         }
1426
1427         if ( res )
1428                 *res = &on2->on_bi;
1429
1430         return 0;
1431 }
1432