]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
ACIs almost entirely factored out of slapd
[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-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 /* 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 enum db_which {
33         db_open = 0,
34         db_close,
35         db_destroy,
36         db_last
37 };
38
39 static int
40 over_db_func(
41         BackendDB *be,
42         enum db_which which
43 )
44 {
45         slap_overinfo *oi = be->bd_info->bi_private;
46         slap_overinst *on = oi->oi_list;
47         BackendInfo *bi_orig = be->bd_info;
48         BI_db_open **func;
49         int rc = 0;
50
51         func = &oi->oi_orig->bi_db_open;
52         if ( func[which] ) {
53                 be->bd_info = oi->oi_orig;
54                 rc = func[which]( be );
55         }
56
57         for (; on && rc == 0; on=on->on_next) {
58                 be->bd_info = &on->on_bi;
59                 func = &on->on_bi.bi_db_open;
60                 if (func[which]) {
61                         rc = func[which]( be );
62                 }
63         }
64         be->bd_info = bi_orig;
65         return rc;
66 }
67
68 static int
69 over_db_config(
70         BackendDB *be,
71         const char *fname,
72         int lineno,
73         int argc,
74         char **argv
75 )
76 {
77         slap_overinfo *oi = be->bd_info->bi_private;
78         slap_overinst *on = oi->oi_list;
79         BackendInfo *bi_orig = be->bd_info;
80         struct ConfigOCs *be_cf_ocs = be->be_cf_ocs;
81         ConfigArgs ca = {0};
82         int rc = 0;
83
84         if ( oi->oi_orig->bi_db_config ) {
85                 be->bd_info = oi->oi_orig;
86                 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
87                 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
88                         argc, argv );
89
90                 if ( be->bd_info != oi->oi_orig ) {
91                         slap_overinfo   *oi2;
92                         slap_overinst   *on2, **onp;
93                         BackendDB       be2 = *be;
94                         int             i;
95
96                         /* a database added an overlay;
97                          * work it around... */
98                         assert( overlay_is_over( be ) );
99                         
100                         oi2 = ( slap_overinfo * )be->bd_info->bi_private;
101                         on2 = oi2->oi_list;
102
103                         /* need to put a uniqueness check here as well;
104                          * note that in principle there could be more than
105                          * one overlay as a result of multiple calls to
106                          * overlay_config() */
107                         be2.bd_info = (BackendInfo *)oi;
108
109                         for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
110                                 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
111                                         Debug( LDAP_DEBUG_ANY, "over_db_config(): "
112                                                         "warning, freshly added "
113                                                         "overlay #%d \"%s\" is already in list\n",
114                                                         i, (*onp)->on_bi.bi_type, 0 );
115
116                                         /* NOTE: if the overlay already exists,
117                                          * there is no way to merge the results
118                                          * of the configuration that may have 
119                                          * occurred during bi_db_config(); we
120                                          * just issue a warning, and the 
121                                          * administrator should deal with this */
122                                 }
123                         }
124                         *onp = oi->oi_list;
125
126                         oi->oi_list = on2;
127
128                         ch_free( be->bd_info );
129                 }
130
131                 be->bd_info = (BackendInfo *)oi;
132                 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
133         }
134
135         ca.argv = argv;
136         ca.argc = argc;
137         ca.fname = fname;
138         ca.lineno = lineno;
139         ca.be = be;
140         for (; on; on=on->on_next) {
141                 rc = SLAP_CONF_UNKNOWN;
142                 if (on->on_bi.bi_cf_ocs) {
143                         ConfigTable *ct;
144                         ca.bi = &on->on_bi;
145                         ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
146                         if ( ct ) {
147                                 rc = config_add_vals( ct, &ca );
148                         }
149                 }
150                 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
151                         be->bd_info = &on->on_bi;
152                         rc = on->on_bi.bi_db_config( be, fname, lineno,
153                                 argc, argv );
154                         if ( rc != SLAP_CONF_UNKNOWN ) break;
155                 }
156         }
157         be->bd_info = bi_orig;
158         be->be_cf_ocs = be_cf_ocs;
159         
160         return rc;
161 }
162
163 static int
164 over_db_open(
165         BackendDB *be
166 )
167 {
168         return over_db_func( be, db_open );
169 }
170
171 static int
172 over_db_close(
173         BackendDB *be
174 )
175 {
176         slap_overinfo *oi = be->bd_info->bi_private;
177         slap_overinst *on = oi->oi_list;
178         BackendInfo *bi_orig = be->bd_info;
179         int rc = 0;
180
181         for (; on && rc == 0; on=on->on_next) {
182                 be->bd_info = &on->on_bi;
183                 if ( be->bd_info->bi_db_close ) {
184                         rc = be->bd_info->bi_db_close( be );
185                 }
186         }
187
188         if ( oi->oi_orig->bi_db_close ) {
189                 be->bd_info = oi->oi_orig;
190                 rc = be->bd_info->bi_db_close( be );
191         }
192
193         be->bd_info = bi_orig;
194         return rc;
195 }
196
197 static int
198 over_db_destroy(
199         BackendDB *be
200 )
201 {
202         slap_overinfo *oi = be->bd_info->bi_private;
203         slap_overinst *on = oi->oi_list, *next;
204         int rc;
205
206         rc = over_db_func( be, db_destroy );
207
208         for (next = on->on_next; on; on=next) {
209                 next = on->on_next;
210                 free( on );
211         }
212         free( oi );
213         return rc;
214 }
215
216 static int
217 over_back_response ( Operation *op, SlapReply *rs )
218 {
219         slap_overinfo *oi = op->o_callback->sc_private;
220         slap_overinst *on = oi->oi_list;
221         int rc = SLAP_CB_CONTINUE;
222         BackendDB *be = op->o_bd, db = *op->o_bd;
223
224         db.be_flags |= SLAP_DBFLAG_OVERLAY;
225         op->o_bd = &db;
226         for (; on; on=on->on_next ) {
227                 if ( on->on_response ) {
228                         db.bd_info = (BackendInfo *)on;
229                         rc = on->on_response( op, rs );
230                         if ( rc != SLAP_CB_CONTINUE ) break;
231                 }
232         }
233         op->o_bd = be;
234         return rc;
235 }
236
237 #ifdef SLAP_OVERLAY_ACCESS
238 static int
239 over_access_allowed(
240         Operation               *op,
241         Entry                   *e,
242         AttributeDescription    *desc,
243         struct berval           *val,
244         slap_access_t           access,
245         AccessControlState      *state,
246         slap_mask_t             *maskp )
247 {
248         slap_overinfo *oi;
249         slap_overinst *on;
250         BackendInfo *bi = op->o_bd->bd_info;
251         BackendDB *be = op->o_bd, db;
252         int rc = SLAP_CB_CONTINUE;
253
254         /* FIXME: used to happen for instance during abandon
255          * when global overlays are used... */
256         assert( op->o_bd != NULL );
257
258         oi = op->o_bd->bd_info->bi_private;
259         on = oi->oi_list;
260
261         for ( ; on; on = on->on_next ) {
262                 if ( on->on_bi.bi_access_allowed ) {
263                         /* NOTE: do not copy the structure until required */
264                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
265                                 db = *op->o_bd;
266                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
267                                 op->o_bd = &db;
268                         }
269
270                         op->o_bd->bd_info = (BackendInfo *)on;
271                         rc = on->on_bi.bi_access_allowed( op, e,
272                                 desc, val, access, state, maskp );
273                         if ( rc != SLAP_CB_CONTINUE ) break;
274                 }
275         }
276
277         if ( rc == SLAP_CB_CONTINUE ) {
278                 BI_access_allowed       *bi_access_allowed;
279
280                 /* if the database structure was changed, o_bd points to a
281                  * copy of the structure; put the original bd_info in place */
282                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
283                         op->o_bd->bd_info = oi->oi_orig;
284                 }
285
286                 if ( oi->oi_orig->bi_access_allowed ) {
287                         bi_access_allowed = oi->oi_orig->bi_access_allowed;
288                 } else {
289                         bi_access_allowed = slap_access_allowed;
290                 }
291
292                 rc = bi_access_allowed( op, e,
293                         desc, val, access, state, maskp );
294         }
295         /* should not fall thru this far without anything happening... */
296         if ( rc == SLAP_CB_CONTINUE ) {
297                 /* access not allowed */
298                 rc = 0;
299         }
300
301         op->o_bd = be;
302         op->o_bd->bd_info = bi;
303
304         return rc;
305 }
306
307 static int
308 over_acl_group(
309         Operation               *op,
310         Entry                   *e,
311         struct berval           *gr_ndn,
312         struct berval           *op_ndn,
313         ObjectClass             *group_oc,
314         AttributeDescription    *group_at )
315 {
316         slap_overinfo *oi;
317         slap_overinst *on;
318         BackendInfo *bi = op->o_bd->bd_info;
319         BackendDB *be = op->o_bd, db;
320         int rc = SLAP_CB_CONTINUE;
321
322         /* FIXME: used to happen for instance during abandon
323          * when global overlays are used... */
324         assert( op->o_bd != NULL );
325
326         oi = op->o_bd->bd_info->bi_private;
327         on = oi->oi_list;
328
329         for ( ; on; on = on->on_next ) {
330                 if ( on->on_bi.bi_acl_group ) {
331                         /* NOTE: do not copy the structure until required */
332                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
333                                 db = *op->o_bd;
334                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
335                                 op->o_bd = &db;
336                         }
337
338                         op->o_bd->bd_info = (BackendInfo *)on;
339                         rc = on->on_bi.bi_acl_group( op, e,
340                                 gr_ndn, op_ndn, group_oc, group_at );
341                         if ( rc != SLAP_CB_CONTINUE ) break;
342                 }
343         }
344
345         if ( rc == SLAP_CB_CONTINUE ) {
346                 BI_acl_group            *bi_acl_group;
347
348                 /* if the database structure was changed, o_bd points to a
349                  * copy of the structure; put the original bd_info in place */
350                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
351                         op->o_bd->bd_info = oi->oi_orig;
352                 }
353
354                 if ( oi->oi_orig->bi_acl_group ) {
355                         bi_acl_group = oi->oi_orig->bi_acl_group;
356                 } else {
357                         bi_acl_group = backend_group;
358                 }
359
360                 rc = bi_acl_group( op, e,
361                         gr_ndn, op_ndn, group_oc, group_at );
362         }
363         /* should not fall thru this far without anything happening... */
364         if ( rc == SLAP_CB_CONTINUE ) {
365                 /* access not allowed */
366                 rc = 0;
367         }
368
369         op->o_bd = be;
370         op->o_bd->bd_info = bi;
371
372         return rc;
373 }
374
375 static int
376 over_acl_attribute(
377         Operation               *op,
378         Entry                   *target,
379         struct berval           *entry_ndn,
380         AttributeDescription    *entry_at,
381         BerVarray               *vals,
382         slap_access_t           access )
383 {
384         slap_overinfo *oi;
385         slap_overinst *on;
386         BackendInfo *bi = op->o_bd->bd_info;
387         BackendDB *be = op->o_bd, db;
388         int rc = SLAP_CB_CONTINUE;
389
390         /* FIXME: used to happen for instance during abandon
391          * when global overlays are used... */
392         assert( op->o_bd != NULL );
393
394         oi = op->o_bd->bd_info->bi_private;
395         on = oi->oi_list;
396
397         for ( ; on; on = on->on_next ) {
398                 if ( on->on_bi.bi_acl_attribute ) {
399                         /* NOTE: do not copy the structure until required */
400                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
401                                 db = *op->o_bd;
402                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
403                                 op->o_bd = &db;
404                         }
405
406                         op->o_bd->bd_info = (BackendInfo *)on;
407                         rc = on->on_bi.bi_acl_attribute( op, target,
408                                 entry_ndn, entry_at, vals, access );
409                         if ( rc != SLAP_CB_CONTINUE ) break;
410                 }
411         }
412
413         if ( rc == SLAP_CB_CONTINUE ) {
414                 BI_acl_attribute                *bi_acl_attribute;
415
416                 /* if the database structure was changed, o_bd points to a
417                  * copy of the structure; put the original bd_info in place */
418                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
419                         op->o_bd->bd_info = oi->oi_orig;
420                 }
421
422                 if ( oi->oi_orig->bi_acl_attribute ) {
423                         bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
424                 } else {
425                         bi_acl_attribute = backend_attribute;
426                 }
427
428                 rc = bi_acl_attribute( op, target,
429                         entry_ndn, entry_at, vals, access );
430         }
431         /* should not fall thru this far without anything happening... */
432         if ( rc == SLAP_CB_CONTINUE ) {
433                 /* access not allowed */
434                 rc = 0;
435         }
436
437         op->o_bd = be;
438         op->o_bd->bd_info = bi;
439
440         return rc;
441 }
442 #endif /* SLAP_OVERLAY_ACCESS */
443
444 /*
445  * default return code in case of missing backend function
446  * and overlay stack returning SLAP_CB_CONTINUE
447  */
448 static int op_rc[ op_last ] = {
449         LDAP_UNWILLING_TO_PERFORM,      /* bind */
450         LDAP_UNWILLING_TO_PERFORM,      /* unbind */
451         LDAP_UNWILLING_TO_PERFORM,      /* search */
452         SLAP_CB_CONTINUE,               /* compare; pass to frontend */
453         LDAP_UNWILLING_TO_PERFORM,      /* modify */
454         LDAP_UNWILLING_TO_PERFORM,      /* modrdn */
455         LDAP_UNWILLING_TO_PERFORM,      /* add */
456         LDAP_UNWILLING_TO_PERFORM,      /* delete */
457         LDAP_UNWILLING_TO_PERFORM,      /* abandon */
458         LDAP_UNWILLING_TO_PERFORM,      /* cancel */
459         LDAP_UNWILLING_TO_PERFORM,      /* extended */
460         LDAP_SUCCESS,                   /* aux_operational */
461         LDAP_SUCCESS,                   /* aux_chk_referrals */
462         SLAP_CB_CONTINUE                /* aux_chk_controls; pass to frontend */
463 };
464
465 int overlay_op_walk(
466         Operation *op,
467         SlapReply *rs,
468         slap_operation_t which,
469         slap_overinfo *oi,
470         slap_overinst *on
471 )
472 {
473         BI_op_bind **func;
474         int rc = SLAP_CB_CONTINUE;
475
476         for (; on; on=on->on_next ) {
477                 func = &on->on_bi.bi_op_bind;
478                 if ( func[which] ) {
479                         op->o_bd->bd_info = (BackendInfo *)on;
480                         rc = func[which]( op, rs );
481                         if ( rc != SLAP_CB_CONTINUE ) break;
482                 }
483         }
484
485         func = &oi->oi_orig->bi_op_bind;
486         if ( func[which] && rc == SLAP_CB_CONTINUE ) {
487                 op->o_bd->bd_info = oi->oi_orig;
488                 rc = func[which]( op, rs );
489         }
490         /* should not fall thru this far without anything happening... */
491         if ( rc == SLAP_CB_CONTINUE ) {
492                 rc = op_rc[ which ];
493         }
494
495         /* The underlying backend didn't handle the request, make sure
496          * overlay cleanup is processed.
497          */
498         if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
499                 slap_callback *sc_next;
500                 for ( ; op->o_callback && op->o_callback->sc_response !=
501                         over_back_response; op->o_callback = sc_next ) {
502                         sc_next = op->o_callback->sc_next;
503                         if ( op->o_callback->sc_cleanup ) {
504                                 op->o_callback->sc_cleanup( op, rs );
505                         }
506                 }
507         }
508         return rc;
509 }
510
511 static int
512 over_op_func(
513         Operation *op,
514         SlapReply *rs,
515         slap_operation_t which
516 )
517 {
518         slap_overinfo *oi;
519         slap_overinst *on;
520         BackendDB *be = op->o_bd, db;
521         slap_callback cb = {NULL, over_back_response, NULL, NULL};
522         int rc = SLAP_CB_CONTINUE;
523
524         /* FIXME: used to happen for instance during abandon
525          * when global overlays are used... */
526         assert( op->o_bd != NULL );
527
528         oi = op->o_bd->bd_info->bi_private;
529         on = oi->oi_list;
530
531         if ( !SLAP_ISOVERLAY( op->o_bd )) {
532                 db = *op->o_bd;
533                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
534                 op->o_bd = &db;
535         }
536         cb.sc_next = op->o_callback;
537         cb.sc_private = oi;
538         op->o_callback = &cb;
539
540         rc = overlay_op_walk( op, rs, which, oi, on );
541
542         op->o_bd = be;
543         op->o_callback = cb.sc_next;
544         return rc;
545 }
546
547 static int
548 over_op_bind( Operation *op, SlapReply *rs )
549 {
550         return over_op_func( op, rs, op_bind );
551 }
552
553 static int
554 over_op_unbind( Operation *op, SlapReply *rs )
555 {
556         return over_op_func( op, rs, op_unbind );
557 }
558
559 static int
560 over_op_search( Operation *op, SlapReply *rs )
561 {
562         return over_op_func( op, rs, op_search );
563 }
564
565 static int
566 over_op_compare( Operation *op, SlapReply *rs )
567 {
568         return over_op_func( op, rs, op_compare );
569 }
570
571 static int
572 over_op_modify( Operation *op, SlapReply *rs )
573 {
574         return over_op_func( op, rs, op_modify );
575 }
576
577 static int
578 over_op_modrdn( Operation *op, SlapReply *rs )
579 {
580         return over_op_func( op, rs, op_modrdn );
581 }
582
583 static int
584 over_op_add( Operation *op, SlapReply *rs )
585 {
586         return over_op_func( op, rs, op_add );
587 }
588
589 static int
590 over_op_delete( Operation *op, SlapReply *rs )
591 {
592         return over_op_func( op, rs, op_delete );
593 }
594
595 static int
596 over_op_abandon( Operation *op, SlapReply *rs )
597 {
598         return over_op_func( op, rs, op_abandon );
599 }
600
601 static int
602 over_op_cancel( Operation *op, SlapReply *rs )
603 {
604         return over_op_func( op, rs, op_cancel );
605 }
606
607 static int
608 over_op_extended( Operation *op, SlapReply *rs )
609 {
610         return over_op_func( op, rs, op_extended );
611 }
612
613 static int
614 over_aux_operational( Operation *op, SlapReply *rs )
615 {
616         return over_op_func( op, rs, op_aux_operational );
617 }
618
619 static int
620 over_aux_chk_referrals( Operation *op, SlapReply *rs )
621 {
622         return over_op_func( op, rs, op_aux_chk_referrals );
623 }
624
625 static int
626 over_aux_chk_controls( Operation *op, SlapReply *rs )
627 {
628         return over_op_func( op, rs, op_aux_chk_controls );
629 }
630
631 enum conn_which {
632         conn_init = 0,
633         conn_destroy,
634         conn_last
635 };
636
637 static int
638 over_connection_func(
639         BackendDB       *bd,
640         Connection      *conn,
641         enum conn_which which
642 )
643 {
644         slap_overinfo           *oi;
645         slap_overinst           *on;
646         BackendDB               db;
647         int                     rc = SLAP_CB_CONTINUE;
648         BI_connection_init      **func;
649
650         /* FIXME: used to happen for instance during abandon
651          * when global overlays are used... */
652         assert( bd != NULL );
653
654         oi = bd->bd_info->bi_private;
655         on = oi->oi_list;
656
657         if ( !SLAP_ISOVERLAY( bd ) ) {
658                 db = *bd;
659                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
660                 bd = &db;
661         }
662
663         for ( ; on; on = on->on_next ) {
664                 func = &on->on_bi.bi_connection_init;
665                 if ( func[ which ] ) {
666                         bd->bd_info = (BackendInfo *)on;
667                         rc = func[ which ]( bd, conn );
668                         if ( rc != SLAP_CB_CONTINUE ) break;
669                 }
670         }
671
672         func = &oi->oi_orig->bi_connection_init;
673         if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
674                 bd->bd_info = oi->oi_orig;
675                 rc = func[ which ]( bd, conn );
676         }
677         /* should not fall thru this far without anything happening... */
678         if ( rc == SLAP_CB_CONTINUE ) {
679                 rc = LDAP_UNWILLING_TO_PERFORM;
680         }
681
682         return rc;
683 }
684
685 static int
686 over_connection_init(
687         BackendDB       *bd,
688         Connection      *conn
689 )
690 {
691         return over_connection_func( bd, conn, conn_init );
692 }
693
694 static int
695 over_connection_destroy(
696         BackendDB       *bd,
697         Connection      *conn
698 )
699 {
700         return over_connection_func( bd, conn, conn_destroy );
701 }
702
703 int
704 overlay_register(
705         slap_overinst *on
706 )
707 {
708         on->on_next = overlays;
709         overlays = on;
710         return 0;
711 }
712
713 /*
714  * iterator on registered overlays; overlay_next( NULL ) returns the first
715  * overlay; * subsequent calls with the previously returned value allow to 
716  * iterate * over the entire list; returns NULL when no more overlays are 
717  * registered.
718  */
719
720 slap_overinst *
721 overlay_next(
722         slap_overinst *on
723 )
724 {
725         if ( on == NULL ) {
726                 return overlays;
727         }
728
729         return on->on_next;
730 }
731
732 /*
733  * returns a specific registered overlay based on the type; NULL if not
734  * registered.
735  */
736
737 slap_overinst *
738 overlay_find( const char *over_type )
739 {
740         slap_overinst *on = overlays;
741
742         assert( over_type != NULL );
743
744         for ( ; on; on = on->on_next ) {
745                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
746                         break;
747                 }
748         }
749
750         return on;
751 }
752
753 static const char overtype[] = "over";
754
755 /*
756  * returns TRUE (1) if the database is actually an overlay instance;
757  * FALSE (0) otherwise.
758  */
759
760 int
761 overlay_is_over( BackendDB *be )
762 {
763         return be->bd_info->bi_type == overtype;
764 }
765
766 /*
767  * returns TRUE (1) if the given database is actually an overlay
768  * instance and, somewhere in the list, contains the requested overlay;
769  * FALSE (0) otherwise.
770  */
771
772 int
773 overlay_is_inst( BackendDB *be, const char *over_type )
774 {
775         slap_overinst   *on;
776
777         assert( be != NULL );
778
779         if ( !overlay_is_over( be ) ) {
780                 return 0;
781         }
782         
783         on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
784         for ( ; on; on = on->on_next ) {
785                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
786                         return 1;
787                 }
788         }
789
790         return 0;
791 }
792
793 int
794 overlay_register_control( BackendDB *be, const char *oid )
795 {
796         int             rc = 0;
797         int             gotit = 0;
798         int             cid;
799
800         if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
801                 return -1;
802         }
803
804         if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
805                 BackendDB *bd;
806                 
807                 /* add to all backends... */
808                 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
809                         if ( be == bd ) {
810                                 gotit = 1;
811                         }
812
813                         bd->be_ctrls[ cid ] = 1;
814                         bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
815                 }
816
817         }
818         
819         if ( rc == 0 && !gotit ) {
820                 be->be_ctrls[ cid ] = 1;
821                 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
822         }
823
824         return rc;
825 }
826
827 void
828 overlay_destroy_one( BackendDB *be, slap_overinst *on )
829 {
830         slap_overinfo *oi = on->on_info;
831         slap_overinst **oidx;
832
833         for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
834                 if ( *oidx == on ) {
835                         *oidx = on->on_next;
836                         if ( on->on_bi.bi_db_destroy ) {
837                                 BackendInfo *bi_orig = be->bd_info;
838                                 be->bd_info = (BackendInfo *)on;
839                                 on->on_bi.bi_db_destroy( be );
840                                 be->bd_info = bi_orig;
841                         }
842                         free( on );
843                         break;
844                 }
845         }
846 }
847
848 /* add an overlay to a particular backend. */
849 int
850 overlay_config( BackendDB *be, const char *ov )
851 {
852         slap_overinst *on = NULL, *on2 = NULL;
853         slap_overinfo *oi = NULL;
854         BackendInfo *bi = NULL;
855
856         on = overlay_find( ov );
857         if ( !on ) {
858                 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
859                 return 1;
860         }
861
862         /* If this is the first overlay on this backend, set up the
863          * overlay info structure
864          */
865         if ( !overlay_is_over( be ) ) {
866                 oi = ch_malloc( sizeof( slap_overinfo ) );
867                 oi->oi_orig = be->bd_info;
868                 oi->oi_bi = *be->bd_info;
869
870                 /* NOTE: the first time a global overlay is configured,
871                  * frontendDB gets this flag; it is used later by overlays
872                  * to determine if they're stacked on top of the frontendDB */
873                 if ( oi->oi_orig == frontendDB->bd_info ) {
874                         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
875                 }
876
877                 /* Save a pointer to ourself in bi_private.
878                  */
879                 oi->oi_bi.bi_private = oi;
880                 oi->oi_list = NULL;
881                 bi = (BackendInfo *)oi;
882
883                 bi->bi_type = (char *)overtype;
884
885                 bi->bi_db_config = over_db_config;
886                 bi->bi_db_open = over_db_open;
887                 bi->bi_db_close = over_db_close;
888                 bi->bi_db_destroy = over_db_destroy;
889
890                 bi->bi_op_bind = over_op_bind;
891                 bi->bi_op_unbind = over_op_unbind;
892                 bi->bi_op_search = over_op_search;
893                 bi->bi_op_compare = over_op_compare;
894                 bi->bi_op_modify = over_op_modify;
895                 bi->bi_op_modrdn = over_op_modrdn;
896                 bi->bi_op_add = over_op_add;
897                 bi->bi_op_delete = over_op_delete;
898                 bi->bi_op_abandon = over_op_abandon;
899                 bi->bi_op_cancel = over_op_cancel;
900
901                 bi->bi_extended = over_op_extended;
902
903                 /*
904                  * this is fine because it has the same
905                  * args of the operations; we need to rework
906                  * all the hooks to share the same args
907                  * of the operations...
908                  */
909                 bi->bi_operational = over_aux_operational;
910                 bi->bi_chk_referrals = over_aux_chk_referrals;
911                 bi->bi_chk_controls = over_aux_chk_controls;
912
913 #ifdef SLAP_OVERLAY_ACCESS
914                 /* these have specific arglists */
915                 bi->bi_access_allowed = over_access_allowed;
916                 bi->bi_acl_group = over_acl_group;
917                 bi->bi_acl_attribute = over_acl_attribute;
918 #endif /* SLAP_OVERLAY_ACCESS */
919                 
920                 bi->bi_connection_init = over_connection_init;
921                 bi->bi_connection_destroy = over_connection_destroy;
922
923                 be->bd_info = bi;
924
925         } else {
926                 if ( overlay_is_inst( be, ov ) ) {
927                         Debug( LDAP_DEBUG_ANY, "overlay_config(): "
928                                         "warning, overlay \"%s\" "
929                                         "already in list\n", ov, 0, 0 );
930                 }
931
932                 oi = be->bd_info->bi_private;
933         }
934
935         /* Insert new overlay on head of list. Overlays are executed
936          * in reverse of config order...
937          */
938         on2 = ch_calloc( 1, sizeof(slap_overinst) );
939         *on2 = *on;
940         on2->on_info = oi;
941         on2->on_next = oi->oi_list;
942         oi->oi_list = on2;
943
944         /* Any initialization needed? */
945         if ( on->on_bi.bi_db_init ) {
946                 int rc;
947                 be->bd_info = (BackendInfo *)on2;
948                 rc = on2->on_bi.bi_db_init( be );
949                 be->bd_info = (BackendInfo *)oi;
950                 if ( rc ) return rc;
951         }
952
953         return 0;
954 }
955