]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
c09b5e6802168086141a8b0eee6239d9c29ba480
[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
29 static slap_overinst *overlays;
30
31 enum db_which { db_open = 0, db_close, db_destroy };
32
33 static int
34 over_db_func(
35         BackendDB *be,
36         enum db_which which
37 )
38 {
39         slap_overinfo *oi = be->bd_info->bi_private;
40         slap_overinst *on = oi->oi_list;
41         BackendInfo *bi_orig = be->bd_info;
42         BI_db_open **func;
43         int rc = 0;
44
45         func = &oi->oi_orig->bi_db_open;
46         if ( func[which] ) {
47                 be->bd_info = oi->oi_orig;
48                 rc = func[which]( be );
49         }
50
51         for (; on && rc == 0; on=on->on_next) {
52                 be->bd_info = &on->on_bi;
53                 func = &on->on_bi.bi_db_open;
54                 if (func[which]) {
55                         rc = func[which]( be );
56                 }
57         }
58         be->bd_info = bi_orig;
59         return rc;
60 }
61
62 static int
63 over_db_config(
64         BackendDB *be,
65         const char *fname,
66         int lineno,
67         int argc,
68         char **argv
69 )
70 {
71         slap_overinfo *oi = be->bd_info->bi_private;
72         slap_overinst *on = oi->oi_list;
73         BackendInfo *bi_orig = be->bd_info;
74         int rc = 0;
75
76         if ( oi->oi_orig->bi_db_config ) {
77                 be->bd_info = oi->oi_orig;
78                 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
79                         argc, argv );
80
81                 if ( be->bd_info != oi->oi_orig ) {
82                         slap_overinfo   *oi2;
83                         slap_overinst   *on2, **onp;
84                         BackendDB       be2 = *be;
85                         int             i;
86
87                         /* a database added an overlay;
88                          * work it around... */
89                         assert( overlay_is_over( be ) );
90                         
91                         oi2 = ( slap_overinfo * )be->bd_info->bi_private;
92                         on2 = oi2->oi_list;
93
94                         /* need to put a uniqueness check here as well;
95                          * note that in principle there could be more than
96                          * one overlay as a result of multiple calls to
97                          * overlay_config() */
98                         be2.bd_info = (BackendInfo *)oi;
99
100                         for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
101                                 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
102                                         Debug( LDAP_DEBUG_ANY, "over_db_config(): "
103                                                         "warning, freshly added "
104                                                         "overlay #%d \"%s\" is already in list\n",
105                                                         i, (*onp)->on_bi.bi_type, 0 );
106
107                                         /* NOTE: if the overlay already exists,
108                                          * there is no way to merge the results
109                                          * of the configuration that may have 
110                                          * occurred during bi_db_config(); we
111                                          * just issue a warning, and the 
112                                          * administrator should deal with this */
113                                 }
114                         }
115                         *onp = oi->oi_list;
116
117                         oi->oi_list = on2;
118
119                         ch_free( be->bd_info );
120                 }
121
122                 be->bd_info = (BackendInfo *)oi;
123                 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
124         }
125
126         for (; on; on=on->on_next) {
127                 if (on->on_bi.bi_db_config) {
128                         be->bd_info = &on->on_bi;
129                         rc = on->on_bi.bi_db_config( be, fname, lineno,
130                                 argc, argv );
131                         if ( rc != SLAP_CONF_UNKNOWN ) break;
132                 }
133         }
134         be->bd_info = bi_orig;
135         return rc;
136 }
137
138 static int
139 over_db_open(
140         BackendDB *be
141 )
142 {
143         return over_db_func( be, db_open );
144 }
145
146 static int
147 over_db_close(
148         BackendDB *be
149 )
150 {
151         slap_overinfo *oi = be->bd_info->bi_private;
152         slap_overinst *on = oi->oi_list;
153         BackendInfo *bi_orig = be->bd_info;
154         int rc = 0;
155
156         for (; on && rc == 0; on=on->on_next) {
157                 be->bd_info = &on->on_bi;
158                 if ( be->bd_info->bi_db_close ) {
159                         rc = be->bd_info->bi_db_close( be );
160                 }
161         }
162
163         if ( oi->oi_orig->bi_db_close ) {
164                 be->bd_info = oi->oi_orig;
165                 rc = be->bd_info->bi_db_close( be );
166         }
167
168         be->bd_info = bi_orig;
169         return rc;
170 }
171
172 static int
173 over_db_destroy(
174         BackendDB *be
175 )
176 {
177         slap_overinfo *oi = be->bd_info->bi_private;
178         slap_overinst *on = oi->oi_list, *next;
179         int rc;
180
181         rc = over_db_func( be, db_destroy );
182
183         for (next = on->on_next; on; on=next) {
184                 next = on->on_next;
185                 free( on );
186         }
187         free( oi );
188         return rc;
189 }
190
191 static int
192 over_back_response ( Operation *op, SlapReply *rs )
193 {
194         slap_overinfo *oi = op->o_callback->sc_private;
195         slap_overinst *on = oi->oi_list;
196         int rc = SLAP_CB_CONTINUE;
197         BackendDB *be = op->o_bd, db = *op->o_bd;
198
199         db.be_flags |= SLAP_DBFLAG_OVERLAY;
200         op->o_bd = &db;
201         for (; on; on=on->on_next ) {
202                 if ( on->on_response ) {
203                         db.bd_info = (BackendInfo *)on;
204                         rc = on->on_response( op, rs );
205                         if ( rc != SLAP_CB_CONTINUE ) break;
206                 }
207         }
208         op->o_bd = be;
209         return rc;
210 }
211
212 enum op_which {
213         op_bind = 0,
214         op_unbind,
215         op_search,
216         op_compare,
217         op_modify,
218         op_modrdn,
219         op_add,
220         op_delete,
221         op_abandon,
222         op_cancel,
223         op_extended,
224         op_aux_operational,
225         op_aux_chk_referrals,
226         op_aux_chk_controls,
227         op_last
228 };
229
230 /*
231  * default return code in case of missing backend function
232  * and overlay stack returning SLAP_CB_CONTINUE
233  */
234 static int op_rc[] = {
235         LDAP_UNWILLING_TO_PERFORM,      /* bind */
236         LDAP_UNWILLING_TO_PERFORM,      /* unbind */
237         LDAP_UNWILLING_TO_PERFORM,      /* search */
238         SLAP_CB_CONTINUE,               /* compare; pass to frontend */
239         LDAP_UNWILLING_TO_PERFORM,      /* modify */
240         LDAP_UNWILLING_TO_PERFORM,      /* modrdn */
241         LDAP_UNWILLING_TO_PERFORM,      /* add */
242         LDAP_UNWILLING_TO_PERFORM,      /* delete */
243         LDAP_UNWILLING_TO_PERFORM,      /* abandon */
244         LDAP_UNWILLING_TO_PERFORM,      /* cancel */
245         LDAP_UNWILLING_TO_PERFORM,      /* extended */
246         LDAP_SUCCESS,                   /* aux_operational */
247         LDAP_SUCCESS,                   /* aux_chk_referrals */
248         SLAP_CB_CONTINUE                /* aux_chk_controls; pass to frontend */
249 };
250
251 #ifdef SLAP_OVERLAY_ACCESS
252 static int
253 over_access_allowed(
254         Operation               *op,
255         Entry                   *e,
256         AttributeDescription    *desc,
257         struct berval           *val,
258         slap_access_t           access,
259         AccessControlState      *state,
260         slap_mask_t             *maskp )
261 {
262         slap_overinfo *oi;
263         slap_overinst *on;
264         BackendDB *be = op->o_bd, db;
265         int rc = SLAP_CB_CONTINUE;
266
267         /* FIXME: used to happen for instance during abandon
268          * when global overlays are used... */
269         assert( op->o_bd != NULL );
270
271         oi = op->o_bd->bd_info->bi_private;
272         on = oi->oi_list;
273
274         for ( ; on; on = on->on_next ) {
275                 if ( on->on_bi.bi_access_allowed ) {
276                         /* NOTE: do not copy the structure until required */
277                         if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
278                                 db = *op->o_bd;
279                                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
280                                 op->o_bd = &db;
281                         }
282
283                         op->o_bd->bd_info = (BackendInfo *)on;
284                         rc = on->on_bi.bi_access_allowed( op, e,
285                                 desc, val, access, state, maskp );
286                         if ( rc != SLAP_CB_CONTINUE ) break;
287                 }
288         }
289
290         if ( rc == SLAP_CB_CONTINUE && oi->oi_orig->bi_access_allowed ) {
291                 /* if the database structure was changed, o_bd points to a
292                  * copy of the structure; put the original bd_info in place */
293                 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
294                         op->o_bd->bd_info = oi->oi_orig;
295                 }
296
297                 rc = oi->oi_orig->bi_access_allowed( op, e,
298                         desc, val, access, state, maskp );
299         }
300         /* should not fall thru this far without anything happening... */
301         if ( rc == SLAP_CB_CONTINUE ) {
302                 /* access not allowed */
303                 rc = 0;
304         }
305
306         op->o_bd = be;
307         return rc;
308 }
309 #endif /* SLAP_OVERLAY_ACCESS */
310
311 static int
312 over_op_func(
313         Operation *op,
314         SlapReply *rs,
315         enum op_which which
316 )
317 {
318         slap_overinfo *oi;
319         slap_overinst *on;
320         BI_op_bind **func;
321         BackendDB *be = op->o_bd, db;
322         slap_callback cb = {NULL, over_back_response, NULL, NULL};
323         int rc = SLAP_CB_CONTINUE;
324
325         /* FIXME: used to happen for instance during abandon
326          * when global overlays are used... */
327         assert( op->o_bd != NULL );
328
329         oi = op->o_bd->bd_info->bi_private;
330         on = oi->oi_list;
331
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         cb.sc_next = op->o_callback;
338         cb.sc_private = oi;
339         op->o_callback = &cb;
340
341         for (; on; on=on->on_next ) {
342                 func = &on->on_bi.bi_op_bind;
343                 if ( func[which] ) {
344                         op->o_bd->bd_info = (BackendInfo *)on;
345                         rc = func[which]( op, rs );
346                         if ( rc != SLAP_CB_CONTINUE ) break;
347                 }
348         }
349
350         func = &oi->oi_orig->bi_op_bind;
351         if ( func[which] && rc == SLAP_CB_CONTINUE ) {
352                 op->o_bd->bd_info = oi->oi_orig;
353                 rc = func[which]( op, rs );
354         }
355         /* should not fall thru this far without anything happening... */
356         if ( rc == SLAP_CB_CONTINUE ) {
357                 rc = op_rc[ which ];
358         }
359
360         /* The underlying backend didn't handle the request, make sure
361          * overlay cleanup is processed.
362          */
363         if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
364                 slap_callback *sc_next;
365                 for ( ; op->o_callback && op->o_callback != cb.sc_next; 
366                         op->o_callback = sc_next ) {
367                         sc_next = op->o_callback->sc_next;
368                         if ( op->o_callback->sc_cleanup ) {
369                                 op->o_callback->sc_cleanup( op, rs );
370                         }
371                 }
372         }
373         op->o_bd = be;
374         op->o_callback = cb.sc_next;
375         return rc;
376 }
377
378 static int
379 over_op_bind( Operation *op, SlapReply *rs )
380 {
381         return over_op_func( op, rs, op_bind );
382 }
383
384 static int
385 over_op_unbind( Operation *op, SlapReply *rs )
386 {
387         return over_op_func( op, rs, op_unbind );
388 }
389
390 static int
391 over_op_search( Operation *op, SlapReply *rs )
392 {
393         return over_op_func( op, rs, op_search );
394 }
395
396 static int
397 over_op_compare( Operation *op, SlapReply *rs )
398 {
399         return over_op_func( op, rs, op_compare );
400 }
401
402 static int
403 over_op_modify( Operation *op, SlapReply *rs )
404 {
405         return over_op_func( op, rs, op_modify );
406 }
407
408 static int
409 over_op_modrdn( Operation *op, SlapReply *rs )
410 {
411         return over_op_func( op, rs, op_modrdn );
412 }
413
414 static int
415 over_op_add( Operation *op, SlapReply *rs )
416 {
417         return over_op_func( op, rs, op_add );
418 }
419
420 static int
421 over_op_delete( Operation *op, SlapReply *rs )
422 {
423         return over_op_func( op, rs, op_delete );
424 }
425
426 static int
427 over_op_abandon( Operation *op, SlapReply *rs )
428 {
429         return over_op_func( op, rs, op_abandon );
430 }
431
432 static int
433 over_op_cancel( Operation *op, SlapReply *rs )
434 {
435         return over_op_func( op, rs, op_cancel );
436 }
437
438 static int
439 over_op_extended( Operation *op, SlapReply *rs )
440 {
441         return over_op_func( op, rs, op_extended );
442 }
443
444 static int
445 over_aux_operational( Operation *op, SlapReply *rs )
446 {
447         return over_op_func( op, rs, op_aux_operational );
448 }
449
450 static int
451 over_aux_chk_referrals( Operation *op, SlapReply *rs )
452 {
453         return over_op_func( op, rs, op_aux_chk_referrals );
454 }
455
456 static int
457 over_aux_chk_controls( Operation *op, SlapReply *rs )
458 {
459         return over_op_func( op, rs, op_aux_chk_controls );
460 }
461
462 static int
463 over_connection_destroy(
464         BackendDB       *bd,
465         Connection      *conn
466 )
467 {
468         slap_overinfo *oi;
469         slap_overinst *on;
470         BackendDB db;
471         int rc = SLAP_CB_CONTINUE;
472
473         /* FIXME: used to happen for instance during abandon
474          * when global overlays are used... */
475         assert( bd != NULL );
476
477         oi = bd->bd_info->bi_private;
478         on = oi->oi_list;
479
480         if ( !SLAP_ISOVERLAY( bd )) {
481                 db = *bd;
482                 db.be_flags |= SLAP_DBFLAG_OVERLAY;
483                 bd = &db;
484         }
485
486         for (; on; on=on->on_next ) {
487                 if ( on->on_bi.bi_connection_destroy ) {
488                         bd->bd_info = (BackendInfo *)on;
489                         rc = on->on_bi.bi_connection_destroy( bd, conn );
490                         if ( rc != SLAP_CB_CONTINUE ) break;
491                 }
492         }
493
494         if ( oi->oi_orig->bi_connection_destroy && rc == SLAP_CB_CONTINUE ) {
495                 bd->bd_info = oi->oi_orig;
496                 rc = oi->oi_orig->bi_connection_destroy( bd, conn );
497         }
498         /* should not fall thru this far without anything happening... */
499         if ( rc == SLAP_CB_CONTINUE ) {
500                 rc = LDAP_UNWILLING_TO_PERFORM;
501         }
502
503         return rc;
504 }
505
506 int
507 overlay_register(
508         slap_overinst *on
509 )
510 {
511         on->on_next = overlays;
512         overlays = on;
513         return 0;
514 }
515
516 /*
517  * iterator on registered overlays; overlay_next( NULL ) returns the first
518  * overlay; * subsequent calls with the previously returned value allow to 
519  * iterate * over the entire list; returns NULL when no more overlays are 
520  * registered.
521  */
522
523 slap_overinst *
524 overlay_next(
525         slap_overinst *on
526 )
527 {
528         if ( on == NULL ) {
529                 return overlays;
530         }
531
532         return on->on_next;
533 }
534
535 /*
536  * returns a specific registered overlay based on the type; NULL if not
537  * registered.
538  */
539
540 slap_overinst *
541 overlay_find( const char *over_type )
542 {
543         slap_overinst *on = overlays;
544
545         assert( over_type );
546
547         for ( ; on; on = on->on_next ) {
548                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
549                         break;
550                 }
551         }
552
553         return on;
554 }
555
556 static const char overtype[] = "over";
557
558 /*
559  * returns TRUE (1) if the database is actually an overlay instance;
560  * FALSE (0) otherwise.
561  */
562
563 int
564 overlay_is_over( BackendDB *be )
565 {
566         return be->bd_info->bi_type == overtype;
567 }
568
569 /*
570  * returns TRUE (1) if the given database is actually an overlay
571  * instance and, somewhere in the list, contains the requested overlay;
572  * FALSE (0) otherwise.
573  */
574
575 int
576 overlay_is_inst( BackendDB *be, const char *over_type )
577 {
578         slap_overinst   *on;
579
580         assert( be );
581
582         if ( !overlay_is_over( be ) ) {
583                 return 0;
584         }
585         
586         on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
587         for ( ; on; on = on->on_next ) {
588                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
589                         return 1;
590                 }
591         }
592
593         return 0;
594 }
595
596 int
597 overlay_register_control( BackendDB *be, const char *oid )
598 {
599         int             rc = 0;
600         int             gotit = 0;
601         int             cid;
602
603         if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
604                 return -1;
605         }
606
607         if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
608                 BackendDB *bd;
609                 
610                 /* add to all backends... */
611                 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
612                         if ( be == bd ) {
613                                 gotit = 1;
614                         }
615
616                         bd->be_ctrls[ cid ] = 1;
617                         bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
618                 }
619
620         }
621         
622         if ( rc == 0 && !gotit ) {
623                 be->be_ctrls[ cid ] = 1;
624                 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
625         }
626
627         return rc;
628 }
629
630 /* add an overlay to a particular backend. */
631 int
632 overlay_config( BackendDB *be, const char *ov )
633 {
634         slap_overinst *on = NULL, *on2 = NULL;
635         slap_overinfo *oi = NULL;
636         BackendInfo *bi = NULL;
637
638         on = overlay_find( ov );
639         if ( !on ) {
640                 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
641                 return 1;
642         }
643
644         /* If this is the first overlay on this backend, set up the
645          * overlay info structure
646          */
647         if ( !overlay_is_over( be ) ) {
648                 oi = ch_malloc( sizeof( slap_overinfo ) );
649                 oi->oi_orig = be->bd_info;
650                 oi->oi_bi = *be->bd_info;
651
652                 /* NOTE: the first time a global overlay is configured,
653                  * frontendDB gets this flag; it is used later by overlays
654                  * to determine if they're stacked on top of the frontendDB */
655                 if ( oi->oi_orig == frontendDB->bd_info ) {
656                         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
657                 }
658
659                 /* Save a pointer to ourself in bi_private.
660                  */
661                 oi->oi_bi.bi_private = oi;
662                 oi->oi_list = NULL;
663                 bi = (BackendInfo *)oi;
664
665                 bi->bi_type = (char *)overtype;
666
667                 bi->bi_db_config = over_db_config;
668                 bi->bi_db_open = over_db_open;
669                 bi->bi_db_close = over_db_close;
670                 bi->bi_db_destroy = over_db_destroy;
671
672                 bi->bi_op_bind = over_op_bind;
673                 bi->bi_op_unbind = over_op_unbind;
674                 bi->bi_op_search = over_op_search;
675                 bi->bi_op_compare = over_op_compare;
676                 bi->bi_op_modify = over_op_modify;
677                 bi->bi_op_modrdn = over_op_modrdn;
678                 bi->bi_op_add = over_op_add;
679                 bi->bi_op_delete = over_op_delete;
680                 bi->bi_op_abandon = over_op_abandon;
681                 bi->bi_op_cancel = over_op_cancel;
682
683                 bi->bi_extended = over_op_extended;
684
685                 /*
686                  * this is fine because it has the same
687                  * args of the operations; we need to rework
688                  * all the hooks to share the same args
689                  * of the operations...
690                  */
691                 bi->bi_operational = over_aux_operational;
692                 bi->bi_chk_referrals = over_aux_chk_referrals;
693                 bi->bi_chk_controls = over_aux_chk_controls;
694
695 #ifdef SLAP_OVERLAY_ACCESS
696                 /* this has a specific arglist */
697                 bi->bi_access_allowed = over_access_allowed;
698 #endif /* SLAP_OVERLAY_ACCESS */
699                 
700                 bi->bi_connection_destroy = over_connection_destroy;
701
702                 be->bd_info = bi;
703
704         } else {
705                 if ( overlay_is_inst( be, ov ) ) {
706                         Debug( LDAP_DEBUG_ANY, "overlay_config(): "
707                                         "warning, overlay \"%s\" "
708                                         "already in list\n", ov, 0, 0 );
709                 }
710
711                 oi = be->bd_info->bi_private;
712         }
713
714         /* Insert new overlay on head of list. Overlays are executed
715          * in reverse of config order...
716          */
717         on2 = ch_calloc( 1, sizeof(slap_overinst) );
718         *on2 = *on;
719         on2->on_info = oi;
720         on2->on_next = oi->oi_list;
721         oi->oi_list = on2;
722
723         /* Any initialization needed? */
724         if ( on->on_bi.bi_db_init ) {
725                 int rc;
726                 be->bd_info = (BackendInfo *)on2;
727                 rc = on2->on_bi.bi_db_init( be );
728                 be->bd_info = (BackendInfo *)oi;
729                 if ( rc ) return rc;
730         }
731
732         return 0;
733 }
734