]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
Fix certificateListValidate parsing of CRL extensions
[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-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 /* 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 = op->o_bd->bd_info;
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( op->o_bd != NULL );
484
485         oi = op->o_bd->bd_info->bi_private;
486         on = oi->oi_list;
487
488         for ( ; on; on = on->on_next ) {
489                 if ( on->on_bi.bi_acl_group ) {
490                         /* NOTE: do not copy the structure until required */
491                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
492                                 db = *op->o_bd;
493                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
494                                 op->o_bd = &db;
495                         }
496
497                         op->o_bd->bd_info = (BackendInfo *)on;
498                         rc = on->on_bi.bi_acl_group( op, e,
499                                 gr_ndn, op_ndn, group_oc, group_at );
500                         if ( rc != SLAP_CB_CONTINUE ) break;
501                 }
502         }
503
504         if ( rc == SLAP_CB_CONTINUE ) {
505                 BI_acl_group            *bi_acl_group;
506
507                 /* if the database structure was changed, o_bd points to a
508                  * copy of the structure; put the original bd_info in place */
509                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
510                         op->o_bd->bd_info = oi->oi_orig;
511                 }
512
513                 if ( oi->oi_orig->bi_acl_group ) {
514                         bi_acl_group = oi->oi_orig->bi_acl_group;
515                 } else {
516                         bi_acl_group = backend_group;
517                 }
518
519                 rc = bi_acl_group( op, e,
520                         gr_ndn, op_ndn, group_oc, group_at );
521         }
522         /* should not fall thru this far without anything happening... */
523         if ( rc == SLAP_CB_CONTINUE ) {
524                 /* access not allowed */
525                 rc = 0;
526         }
527
528         op->o_bd = be;
529         op->o_bd->bd_info = bi;
530
531         return rc;
532 }
533
534 static int
535 over_acl_attribute(
536         Operation               *op,
537         Entry                   *target,
538         struct berval           *entry_ndn,
539         AttributeDescription    *entry_at,
540         BerVarray               *vals,
541         slap_access_t           access )
542 {
543         slap_overinfo *oi;
544         slap_overinst *on;
545         BackendInfo *bi = op->o_bd->bd_info;
546         BackendDB *be = op->o_bd, db;
547         int rc = SLAP_CB_CONTINUE;
548
549         /* FIXME: used to happen for instance during abandon
550          * when global overlays are used... */
551         assert( op->o_bd != NULL );
552
553         oi = op->o_bd->bd_info->bi_private;
554         on = oi->oi_list;
555
556         for ( ; on; on = on->on_next ) {
557                 if ( on->on_bi.bi_acl_attribute ) {
558                         /* NOTE: do not copy the structure until required */
559                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
560                                 db = *op->o_bd;
561                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
562                                 op->o_bd = &db;
563                         }
564
565                         op->o_bd->bd_info = (BackendInfo *)on;
566                         rc = on->on_bi.bi_acl_attribute( op, target,
567                                 entry_ndn, entry_at, vals, access );
568                         if ( rc != SLAP_CB_CONTINUE ) break;
569                 }
570         }
571
572         if ( rc == SLAP_CB_CONTINUE ) {
573                 BI_acl_attribute                *bi_acl_attribute;
574
575                 /* if the database structure was changed, o_bd points to a
576                  * copy of the structure; put the original bd_info in place */
577                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
578                         op->o_bd->bd_info = oi->oi_orig;
579                 }
580
581                 if ( oi->oi_orig->bi_acl_attribute ) {
582                         bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
583                 } else {
584                         bi_acl_attribute = backend_attribute;
585                 }
586
587                 rc = bi_acl_attribute( op, target,
588                         entry_ndn, entry_at, vals, access );
589         }
590         /* should not fall thru this far without anything happening... */
591         if ( rc == SLAP_CB_CONTINUE ) {
592                 /* access not allowed */
593                 rc = 0;
594         }
595
596         op->o_bd = be;
597         op->o_bd->bd_info = bi;
598
599         return rc;
600 }
601
602 int
603 overlay_callback_after_backover( Operation *op, slap_callback *sc, int append )
604 {
605         slap_callback **scp;
606
607         for ( scp = &op->o_callback; *scp != NULL; scp = &(*scp)->sc_next ) {
608                 if ( (*scp)->sc_response == over_back_response ) {
609                         sc->sc_next = (*scp)->sc_next;
610                         (*scp)->sc_next = sc;
611                         return 0;
612                 }
613         }
614
615         if ( append ) {
616                 *scp = sc;
617                 return 0;
618         }
619
620         return 1;
621 }
622
623 /*
624  * default return code in case of missing backend function
625  * and overlay stack returning SLAP_CB_CONTINUE
626  */
627 static int op_rc[ op_last ] = {
628         LDAP_UNWILLING_TO_PERFORM,      /* bind */
629         LDAP_UNWILLING_TO_PERFORM,      /* unbind */
630         LDAP_UNWILLING_TO_PERFORM,      /* search */
631         SLAP_CB_CONTINUE,               /* compare; pass to frontend */
632         LDAP_UNWILLING_TO_PERFORM,      /* modify */
633         LDAP_UNWILLING_TO_PERFORM,      /* modrdn */
634         LDAP_UNWILLING_TO_PERFORM,      /* add */
635         LDAP_UNWILLING_TO_PERFORM,      /* delete */
636         LDAP_UNWILLING_TO_PERFORM,      /* abandon */
637         LDAP_UNWILLING_TO_PERFORM,      /* cancel */
638         LDAP_UNWILLING_TO_PERFORM,      /* extended */
639         LDAP_SUCCESS,                   /* aux_operational */
640         LDAP_SUCCESS,                   /* aux_chk_referrals */
641         SLAP_CB_CONTINUE                /* aux_chk_controls; pass to frontend */
642 };
643
644 int overlay_op_walk(
645         Operation *op,
646         SlapReply *rs,
647         slap_operation_t which,
648         slap_overinfo *oi,
649         slap_overinst *on
650 )
651 {
652         BI_op_bind **func;
653         int rc = SLAP_CB_CONTINUE;
654
655         for (; on; on=on->on_next ) {
656                 func = &on->on_bi.bi_op_bind;
657                 if ( func[which] ) {
658                         op->o_bd->bd_info = (BackendInfo *)on;
659                         rc = func[which]( op, rs );
660                         if ( rc != SLAP_CB_CONTINUE ) break;
661                 }
662         }
663         if ( rc == SLAP_CB_BYPASS )
664                 rc = SLAP_CB_CONTINUE;
665
666         func = &oi->oi_orig->bi_op_bind;
667         if ( func[which] && rc == SLAP_CB_CONTINUE ) {
668                 op->o_bd->bd_info = oi->oi_orig;
669                 rc = func[which]( op, rs );
670         }
671         /* should not fall thru this far without anything happening... */
672         if ( rc == SLAP_CB_CONTINUE ) {
673                 rc = op_rc[ which ];
674         }
675
676         /* The underlying backend didn't handle the request, make sure
677          * overlay cleanup is processed.
678          */
679         if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
680                 slap_callback *sc_next;
681                 for ( ; op->o_callback && op->o_callback->sc_response !=
682                         over_back_response; op->o_callback = sc_next ) {
683                         sc_next = op->o_callback->sc_next;
684                         if ( op->o_callback->sc_cleanup ) {
685                                 op->o_callback->sc_cleanup( op, rs );
686                         }
687                 }
688         }
689         return rc;
690 }
691
692 static int
693 over_op_func(
694         Operation *op,
695         SlapReply *rs,
696         slap_operation_t which
697 )
698 {
699         slap_overinfo *oi;
700         slap_overinst *on;
701         BackendDB *be = op->o_bd, db;
702         slap_callback cb = {NULL, over_back_response, NULL, NULL};
703         int rc = SLAP_CB_CONTINUE;
704
705         /* FIXME: used to happen for instance during abandon
706          * when global overlays are used... */
707         assert( op->o_bd != NULL );
708
709         oi = op->o_bd->bd_info->bi_private;
710         on = oi->oi_list;
711
712         if ( !SLAP_ISOVERLAY( op->o_bd )) {
713                 db = *op->o_bd;
714                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
715                 op->o_bd = &db;
716         }
717         cb.sc_next = op->o_callback;
718         cb.sc_private = oi;
719         op->o_callback = &cb;
720
721         rc = overlay_op_walk( op, rs, which, oi, on );
722
723         op->o_bd = be;
724         op->o_callback = cb.sc_next;
725         return rc;
726 }
727
728 static int
729 over_op_bind( Operation *op, SlapReply *rs )
730 {
731         return over_op_func( op, rs, op_bind );
732 }
733
734 static int
735 over_op_unbind( Operation *op, SlapReply *rs )
736 {
737         return over_op_func( op, rs, op_unbind );
738 }
739
740 static int
741 over_op_search( Operation *op, SlapReply *rs )
742 {
743         return over_op_func( op, rs, op_search );
744 }
745
746 static int
747 over_op_compare( Operation *op, SlapReply *rs )
748 {
749         return over_op_func( op, rs, op_compare );
750 }
751
752 static int
753 over_op_modify( Operation *op, SlapReply *rs )
754 {
755         return over_op_func( op, rs, op_modify );
756 }
757
758 static int
759 over_op_modrdn( Operation *op, SlapReply *rs )
760 {
761         return over_op_func( op, rs, op_modrdn );
762 }
763
764 static int
765 over_op_add( Operation *op, SlapReply *rs )
766 {
767         return over_op_func( op, rs, op_add );
768 }
769
770 static int
771 over_op_delete( Operation *op, SlapReply *rs )
772 {
773         return over_op_func( op, rs, op_delete );
774 }
775
776 static int
777 over_op_abandon( Operation *op, SlapReply *rs )
778 {
779         return over_op_func( op, rs, op_abandon );
780 }
781
782 static int
783 over_op_cancel( Operation *op, SlapReply *rs )
784 {
785         return over_op_func( op, rs, op_cancel );
786 }
787
788 static int
789 over_op_extended( Operation *op, SlapReply *rs )
790 {
791         return over_op_func( op, rs, op_extended );
792 }
793
794 static int
795 over_aux_operational( Operation *op, SlapReply *rs )
796 {
797         return over_op_func( op, rs, op_aux_operational );
798 }
799
800 static int
801 over_aux_chk_referrals( Operation *op, SlapReply *rs )
802 {
803         return over_op_func( op, rs, op_aux_chk_referrals );
804 }
805
806 static int
807 over_aux_chk_controls( Operation *op, SlapReply *rs )
808 {
809         return over_op_func( op, rs, op_aux_chk_controls );
810 }
811
812 enum conn_which {
813         conn_init = 0,
814         conn_destroy,
815         conn_last
816 };
817
818 static int
819 over_connection_func(
820         BackendDB       *bd,
821         Connection      *conn,
822         enum conn_which which
823 )
824 {
825         slap_overinfo           *oi;
826         slap_overinst           *on;
827         BackendDB               db;
828         int                     rc = SLAP_CB_CONTINUE;
829         BI_connection_init      **func;
830
831         /* FIXME: used to happen for instance during abandon
832          * when global overlays are used... */
833         assert( bd != NULL );
834
835         oi = bd->bd_info->bi_private;
836         on = oi->oi_list;
837
838         if ( !SLAP_ISOVERLAY( bd ) ) {
839                 db = *bd;
840                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
841                 bd = &db;
842         }
843
844         for ( ; on; on = on->on_next ) {
845                 func = &on->on_bi.bi_connection_init;
846                 if ( func[ which ] ) {
847                         bd->bd_info = (BackendInfo *)on;
848                         rc = func[ which ]( bd, conn );
849                         if ( rc != SLAP_CB_CONTINUE ) break;
850                 }
851         }
852
853         func = &oi->oi_orig->bi_connection_init;
854         if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
855                 bd->bd_info = oi->oi_orig;
856                 rc = func[ which ]( bd, conn );
857         }
858         /* should not fall thru this far without anything happening... */
859         if ( rc == SLAP_CB_CONTINUE ) {
860                 rc = LDAP_UNWILLING_TO_PERFORM;
861         }
862
863         return rc;
864 }
865
866 static int
867 over_connection_init(
868         BackendDB       *bd,
869         Connection      *conn
870 )
871 {
872         return over_connection_func( bd, conn, conn_init );
873 }
874
875 static int
876 over_connection_destroy(
877         BackendDB       *bd,
878         Connection      *conn
879 )
880 {
881         return over_connection_func( bd, conn, conn_destroy );
882 }
883
884 int
885 overlay_register(
886         slap_overinst *on
887 )
888 {
889         slap_overinst   *tmp;
890
891         /* FIXME: check for duplicates? */
892         for ( tmp = overlays; tmp != NULL; tmp = tmp->on_next ) {
893                 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_type ) == 0 ) {
894                         Debug( LDAP_DEBUG_ANY,
895                                 "overlay_register(\"%s\"): "
896                                 "name already in use.\n",
897                                 on->on_bi.bi_type, 0, 0 );
898                         return -1;
899                 }
900
901                 if ( on->on_bi.bi_obsolete_names != NULL ) {
902                         int     i;
903
904                         for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
905                                 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], tmp->on_bi.bi_type ) == 0 ) {
906                                         Debug( LDAP_DEBUG_ANY,
907                                                 "overlay_register(\"%s\"): "
908                                                 "obsolete name \"%s\" already in use "
909                                                 "by overlay \"%s\".\n",
910                                                 on->on_bi.bi_type,
911                                                 on->on_bi.bi_obsolete_names[ i ],
912                                                 tmp->on_bi.bi_type );
913                                         return -1;
914                                 }
915                         }
916                 }
917
918                 if ( tmp->on_bi.bi_obsolete_names != NULL ) {
919                         int     i;
920
921                         for ( i = 0; tmp->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
922                                 int     j;
923
924                                 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
925                                         Debug( LDAP_DEBUG_ANY,
926                                                 "overlay_register(\"%s\"): "
927                                                 "name already in use "
928                                                 "as obsolete by overlay \"%s\".\n",
929                                                 on->on_bi.bi_type,
930                                                 tmp->on_bi.bi_obsolete_names[ i ], 0 );
931                                         return -1;
932                                 }
933
934                                 if ( on->on_bi.bi_obsolete_names != NULL ) {
935                                         for ( j = 0; on->on_bi.bi_obsolete_names[ j ] != NULL; j++ ) {
936                                                 if ( strcmp( on->on_bi.bi_obsolete_names[ j ], tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
937                                                         Debug( LDAP_DEBUG_ANY,
938                                                                 "overlay_register(\"%s\"): "
939                                                                 "obsolete name \"%s\" already in use "
940                                                                 "as obsolete by overlay \"%s\".\n",
941                                                                 on->on_bi.bi_type,
942                                                                 on->on_bi.bi_obsolete_names[ j ],
943                                                                 tmp->on_bi.bi_type );
944                                                         return -1;
945                                                 }
946                                         }
947                                 }
948                         }
949                 }
950         }
951
952         on->on_next = overlays;
953         overlays = on;
954         return 0;
955 }
956
957 /*
958  * iterator on registered overlays; overlay_next( NULL ) returns the first
959  * overlay; subsequent calls with the previously returned value allow to 
960  * iterate over the entire list; returns NULL when no more overlays are 
961  * registered.
962  */
963
964 slap_overinst *
965 overlay_next(
966         slap_overinst *on
967 )
968 {
969         if ( on == NULL ) {
970                 return overlays;
971         }
972
973         return on->on_next;
974 }
975
976 /*
977  * returns a specific registered overlay based on the type; NULL if not
978  * registered.
979  */
980
981 slap_overinst *
982 overlay_find( const char *over_type )
983 {
984         slap_overinst *on = overlays;
985
986         assert( over_type != NULL );
987
988         for ( ; on; on = on->on_next ) {
989                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
990                         goto foundit;
991                 }
992
993                 if ( on->on_bi.bi_obsolete_names != NULL ) {
994                         int     i;
995
996                         for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
997                                 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], over_type ) == 0 ) {
998                                         Debug( LDAP_DEBUG_ANY,
999                                                 "overlay_find(\"%s\"): "
1000                                                 "obsolete name for \"%s\".\n",
1001                                                 on->on_bi.bi_obsolete_names[ i ],
1002                                                 on->on_bi.bi_type, 0 );
1003                                         goto foundit;
1004                                 }
1005                         }
1006                 }
1007         }
1008
1009 foundit:;
1010         return on;
1011 }
1012
1013 static const char overtype[] = "over";
1014
1015 /*
1016  * returns TRUE (1) if the database is actually an overlay instance;
1017  * FALSE (0) otherwise.
1018  */
1019
1020 int
1021 overlay_is_over( BackendDB *be )
1022 {
1023         return be->bd_info->bi_type == overtype;
1024 }
1025
1026 /*
1027  * returns TRUE (1) if the given database is actually an overlay
1028  * instance and, somewhere in the list, contains the requested overlay;
1029  * FALSE (0) otherwise.
1030  */
1031
1032 int
1033 overlay_is_inst( BackendDB *be, const char *over_type )
1034 {
1035         slap_overinst   *on;
1036
1037         assert( be != NULL );
1038
1039         if ( !overlay_is_over( be ) ) {
1040                 return 0;
1041         }
1042         
1043         on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
1044         for ( ; on; on = on->on_next ) {
1045                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1046                         return 1;
1047                 }
1048         }
1049
1050         return 0;
1051 }
1052
1053 int
1054 overlay_register_control( BackendDB *be, const char *oid )
1055 {
1056         int             gotit = 0;
1057         int             cid;
1058
1059         if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1060                 return -1;
1061         }
1062
1063         if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1064                 BackendDB *bd;
1065                 
1066                 /* add to all backends... */
1067                 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1068                         if ( bd == be->bd_self ) {
1069                                 gotit = 1;
1070                         }
1071
1072                         bd->be_ctrls[ cid ] = 1;
1073                         bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1074                 }
1075
1076         }
1077         
1078         if ( !gotit ) {
1079                 be->bd_self->be_ctrls[ cid ] = 1;
1080                 be->bd_self->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1081         }
1082
1083         return 0;
1084 }
1085
1086 void
1087 overlay_destroy_one( BackendDB *be, slap_overinst *on )
1088 {
1089         slap_overinfo *oi = on->on_info;
1090         slap_overinst **oidx;
1091
1092         for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1093                 if ( *oidx == on ) {
1094                         *oidx = on->on_next;
1095                         if ( on->on_bi.bi_db_destroy ) {
1096                                 BackendInfo *bi_orig = be->bd_info;
1097                                 be->bd_info = (BackendInfo *)on;
1098                                 on->on_bi.bi_db_destroy( be, NULL );
1099                                 be->bd_info = bi_orig;
1100                         }
1101                         free( on );
1102                         break;
1103                 }
1104         }
1105 }
1106
1107 #ifdef SLAP_CONFIG_DELETE
1108 void
1109 overlay_remove( BackendDB *be, slap_overinst *on )
1110 {
1111         slap_overinfo *oi = on->on_info;
1112         slap_overinst **oidx;
1113         BackendInfo *bi_orig;
1114
1115         /* remove overlay from oi_list an call db_close and db_destroy
1116          * handlers */
1117         for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1118                 if ( *oidx == on ) {
1119                         *oidx = on->on_next;
1120                         bi_orig = be->bd_info;
1121                         be->bd_info = (BackendInfo *)on;
1122                         if ( on->on_bi.bi_db_close ) {
1123                                 on->on_bi.bi_db_close( be, NULL );
1124                         }
1125                         if ( on->on_bi.bi_db_destroy ) {
1126                                 on->on_bi.bi_db_destroy( be, NULL );
1127                         }
1128                         be->bd_info = bi_orig;
1129                         free( on );
1130                         break;
1131                 }
1132         }
1133         
1134         /* clean up after removing last overlay */
1135         if ( ! oi->oi_list ) 
1136         {
1137                 /* reset db flags and bd_info to orig */
1138                 SLAP_DBFLAGS( be ) &= ~SLAP_DBFLAG_GLOBAL_OVERLAY;
1139                 be->bd_info = oi->oi_orig;
1140                 ch_free(oi);
1141         }
1142 }
1143 #endif /* SLAP_CONFIG_DELETE */
1144
1145 void
1146 overlay_insert( BackendDB *be, slap_overinst *on2, slap_overinst ***prev,
1147         int idx )
1148 {
1149         slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1150
1151         if ( idx == -1 ) {
1152                 on2->on_next = oi->oi_list;
1153                 oi->oi_list = on2;
1154         } else {
1155                 int i;
1156                 slap_overinst *on, *otmp1 = NULL, *otmp2;
1157
1158                 /* Since the list is in reverse order and is singly linked,
1159                  * we reverse it to find the idx insertion point. Adding
1160                  * on overlay at a specific point should be a pretty
1161                  * infrequent occurrence.
1162                  */
1163                 for ( on = oi->oi_list; on; on=otmp2 ) {
1164                         otmp2 = on->on_next;
1165                         on->on_next = otmp1;
1166                         otmp1 = on;
1167                 }
1168                 oi->oi_list = NULL;
1169                 /* advance to insertion point */
1170                 for ( i=0, on = otmp1; i<idx; i++ ) {
1171                         otmp1 = on->on_next;
1172                         on->on_next = oi->oi_list;
1173                         oi->oi_list = on;
1174                 }
1175                 /* insert */
1176                 on2->on_next = oi->oi_list;
1177                 oi->oi_list = on2;
1178                 if ( otmp1 ) {
1179                         *prev = &otmp1->on_next;
1180                         /* replace remainder of list */
1181                         for ( on=otmp1; on; on=otmp1 ) {
1182                                 otmp1 = on->on_next;
1183                                 on->on_next = oi->oi_list;
1184                                 oi->oi_list = on;
1185                         }
1186                 }
1187         }
1188 }
1189
1190 void
1191 overlay_move( BackendDB *be, slap_overinst *on, int idx )
1192 {
1193         slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1194         slap_overinst **onp;
1195
1196         for (onp = &oi->oi_list; *onp; onp= &(*onp)->on_next) {
1197                 if ( *onp == on ) {
1198                         *onp = on->on_next;
1199                         break;
1200                 }
1201         }
1202         overlay_insert( be, on, &onp, idx );
1203 }
1204
1205 /* add an overlay to a particular backend. */
1206 int
1207 overlay_config( BackendDB *be, const char *ov, int idx, BackendInfo **res, ConfigReply *cr )
1208 {
1209         slap_overinst *on = NULL, *on2 = NULL, **prev;
1210         slap_overinfo *oi = NULL;
1211         BackendInfo *bi = NULL;
1212
1213         if ( res )
1214                 *res = NULL;
1215
1216         on = overlay_find( ov );
1217         if ( !on ) {
1218                 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
1219                 return 1;
1220         }
1221
1222         /* If this is the first overlay on this backend, set up the
1223          * overlay info structure
1224          */
1225         if ( !overlay_is_over( be ) ) {
1226                 int     isglobal = 0;
1227
1228                 /* NOTE: the first time a global overlay is configured,
1229                  * frontendDB gets this flag; it is used later by overlays
1230                  * to determine if they're stacked on top of the frontendDB */
1231                 if ( be->bd_info == frontendDB->bd_info || SLAP_ISGLOBALOVERLAY( be ) ) {
1232                         isglobal = 1;
1233                         if ( on->on_bi.bi_flags & SLAPO_BFLAG_DBONLY ) {
1234                                 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1235                                         "overlay \"%s\" cannot be global.\n",
1236                                         ov, 0, 0 );
1237                                 return 1;
1238                         }
1239
1240                 } else if ( on->on_bi.bi_flags & SLAPO_BFLAG_GLOBONLY ) {
1241                         Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1242                                 "overlay \"%s\" can only be global.\n",
1243                                 ov, 0, 0 );
1244                         return 1;
1245                 }
1246
1247                 oi = ch_malloc( sizeof( slap_overinfo ) );
1248                 oi->oi_orig = be->bd_info;
1249                 oi->oi_bi = *be->bd_info;
1250                 oi->oi_origdb = be;
1251
1252                 if ( isglobal ) {
1253                         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
1254                 }
1255
1256                 /* Save a pointer to ourself in bi_private.
1257                  */
1258                 oi->oi_bi.bi_private = oi;
1259                 oi->oi_list = NULL;
1260                 bi = (BackendInfo *)oi;
1261
1262                 bi->bi_type = (char *)overtype;
1263
1264                 bi->bi_db_config = over_db_config;
1265                 bi->bi_db_open = over_db_open;
1266                 bi->bi_db_close = over_db_close;
1267                 bi->bi_db_destroy = over_db_destroy;
1268
1269                 bi->bi_op_bind = over_op_bind;
1270                 bi->bi_op_unbind = over_op_unbind;
1271                 bi->bi_op_search = over_op_search;
1272                 bi->bi_op_compare = over_op_compare;
1273                 bi->bi_op_modify = over_op_modify;
1274                 bi->bi_op_modrdn = over_op_modrdn;
1275                 bi->bi_op_add = over_op_add;
1276                 bi->bi_op_delete = over_op_delete;
1277                 bi->bi_op_abandon = over_op_abandon;
1278                 bi->bi_op_cancel = over_op_cancel;
1279
1280                 bi->bi_extended = over_op_extended;
1281
1282                 /*
1283                  * this is fine because it has the same
1284                  * args of the operations; we need to rework
1285                  * all the hooks to share the same args
1286                  * of the operations...
1287                  */
1288                 bi->bi_operational = over_aux_operational;
1289                 bi->bi_chk_referrals = over_aux_chk_referrals;
1290                 bi->bi_chk_controls = over_aux_chk_controls;
1291
1292                 /* these have specific arglists */
1293                 bi->bi_entry_get_rw = over_entry_get_rw;
1294                 bi->bi_entry_release_rw = over_entry_release_rw;
1295                 bi->bi_access_allowed = over_access_allowed;
1296                 bi->bi_acl_group = over_acl_group;
1297                 bi->bi_acl_attribute = over_acl_attribute;
1298                 
1299                 bi->bi_connection_init = over_connection_init;
1300                 bi->bi_connection_destroy = over_connection_destroy;
1301
1302                 be->bd_info = bi;
1303
1304         } else {
1305                 if ( overlay_is_inst( be, ov ) ) {
1306                         if ( SLAPO_SINGLE( be ) ) {
1307                                 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1308                                         "overlay \"%s\" already in list\n",
1309                                         ov, 0, 0 );
1310                                 return 1;
1311                         }
1312                 }
1313
1314                 oi = be->bd_info->bi_private;
1315         }
1316
1317         /* Insert new overlay into list. By default overlays are
1318          * added to head of list and executed in LIFO order.
1319          */
1320         on2 = ch_calloc( 1, sizeof(slap_overinst) );
1321         *on2 = *on;
1322         on2->on_info = oi;
1323
1324         prev = &oi->oi_list;
1325         /* Do we need to find the insertion point? */
1326         if ( idx >= 0 ) {
1327                 int i;
1328
1329                 /* count current overlays */
1330                 for ( i=0, on=oi->oi_list; on; on=on->on_next, i++ );
1331
1332                 /* are we just appending a new one? */
1333                 if ( idx >= i )
1334                         idx = -1;
1335         }
1336         overlay_insert( be, on2, &prev, idx );
1337
1338         /* Any initialization needed? */
1339         if ( on2->on_bi.bi_db_init ) {
1340                 int rc;
1341                 be->bd_info = (BackendInfo *)on2;
1342                 rc = on2->on_bi.bi_db_init( be, cr);
1343                 be->bd_info = (BackendInfo *)oi;
1344                 if ( rc ) {
1345                         *prev = on2->on_next;
1346                         ch_free( on2 );
1347                         on2 = NULL;
1348                         return rc;
1349                 }
1350         }
1351
1352         if ( res )
1353                 *res = &on2->on_bi;
1354
1355         return 0;
1356 }
1357