]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
4ddb208a0e6f35ad4509cf0bce24c75ced9bea92
[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-2004 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_last
227 };
228
229 /*
230  * default return code in case of missing backend function
231  * and overlay stack returning SLAP_CB_CONTINUE
232  */
233 static int op_rc[] = {
234         LDAP_UNWILLING_TO_PERFORM,      /* bind */
235         LDAP_UNWILLING_TO_PERFORM,      /* unbind */
236         LDAP_UNWILLING_TO_PERFORM,      /* search */
237         LDAP_UNWILLING_TO_PERFORM,      /* compare */
238         LDAP_UNWILLING_TO_PERFORM,      /* modify */
239         LDAP_UNWILLING_TO_PERFORM,      /* modrdn */
240         LDAP_UNWILLING_TO_PERFORM,      /* add */
241         LDAP_UNWILLING_TO_PERFORM,      /* delete */
242         LDAP_UNWILLING_TO_PERFORM,      /* abandon */
243         LDAP_UNWILLING_TO_PERFORM,      /* cancel */
244         LDAP_UNWILLING_TO_PERFORM,      /* extended */
245         LDAP_SUCCESS,                   /* aux_operational */
246         LDAP_SUCCESS                    /* aux_chk_referrals */
247 };
248
249 static int
250 over_op_func(
251         Operation *op,
252         SlapReply *rs,
253         enum op_which which
254 )
255 {
256         slap_overinfo *oi = op->o_bd->bd_info->bi_private;
257         slap_overinst *on = oi->oi_list;
258         BI_op_bind **func;
259         BackendDB *be = op->o_bd, db = *op->o_bd;
260         slap_callback cb = {NULL, over_back_response, NULL, NULL};
261         int rc = SLAP_CB_CONTINUE;
262
263         db.be_flags |= SLAP_DBFLAG_OVERLAY;
264         op->o_bd = &db;
265         cb.sc_next = op->o_callback;
266         cb.sc_private = oi;
267         op->o_callback = &cb;
268
269         for (; on; on=on->on_next ) {
270                 func = &on->on_bi.bi_op_bind;
271                 if ( func[which] ) {
272                         db.bd_info = (BackendInfo *)on;
273                         rc = func[which]( op, rs );
274                         if ( rc != SLAP_CB_CONTINUE ) break;
275                 }
276         }
277
278         func = &oi->oi_orig->bi_op_bind;
279         if ( func[which] && rc == SLAP_CB_CONTINUE ) {
280                 db.bd_info = oi->oi_orig;
281                 rc = func[which]( op, rs );
282         }
283         /* should not fall thru this far without anything happening... */
284         if ( rc == SLAP_CB_CONTINUE ) {
285                 rc = op_rc[ which ];
286         }
287         op->o_bd = be;
288         op->o_callback = cb.sc_next;
289         return rc;
290 }
291
292 static int
293 over_op_bind( Operation *op, SlapReply *rs )
294 {
295         return over_op_func( op, rs, op_bind );
296 }
297
298 static int
299 over_op_unbind( Operation *op, SlapReply *rs )
300 {
301         return over_op_func( op, rs, op_unbind );
302 }
303
304 static int
305 over_op_search( Operation *op, SlapReply *rs )
306 {
307         return over_op_func( op, rs, op_search );
308 }
309
310 static int
311 over_op_compare( Operation *op, SlapReply *rs )
312 {
313         return over_op_func( op, rs, op_compare );
314 }
315
316 static int
317 over_op_modify( Operation *op, SlapReply *rs )
318 {
319         return over_op_func( op, rs, op_modify );
320 }
321
322 static int
323 over_op_modrdn( Operation *op, SlapReply *rs )
324 {
325         return over_op_func( op, rs, op_modrdn );
326 }
327
328 static int
329 over_op_add( Operation *op, SlapReply *rs )
330 {
331         return over_op_func( op, rs, op_add );
332 }
333
334 static int
335 over_op_delete( Operation *op, SlapReply *rs )
336 {
337         return over_op_func( op, rs, op_delete );
338 }
339
340 static int
341 over_op_abandon( Operation *op, SlapReply *rs )
342 {
343         return over_op_func( op, rs, op_abandon );
344 }
345
346 static int
347 over_op_cancel( Operation *op, SlapReply *rs )
348 {
349         return over_op_func( op, rs, op_cancel );
350 }
351
352 static int
353 over_op_extended( Operation *op, SlapReply *rs )
354 {
355         return over_op_func( op, rs, op_extended );
356 }
357
358 static int
359 over_aux_operational( Operation *op, SlapReply *rs )
360 {
361         return over_op_func( op, rs, op_aux_operational );
362 }
363
364 static int
365 over_aux_chk_referrals( Operation *op, SlapReply *rs )
366 {
367         return over_op_func( op, rs, op_aux_chk_referrals );
368 }
369
370 int
371 overlay_register(
372         slap_overinst *on
373 )
374 {
375         on->on_next = overlays;
376         overlays = on;
377         return 0;
378 }
379
380 /*
381  * iterator on registered overlays; overlay_next( NULL ) returns the first
382  * overlay; * subsequent calls with the previously returned value allow to 
383  * iterate * over the entire list; returns NULL when no more overlays are 
384  * registered.
385  */
386
387 slap_overinst *
388 overlay_next(
389         slap_overinst *on
390 )
391 {
392         if ( on == NULL ) {
393                 return overlays;
394         }
395
396         return on->on_next;
397 }
398
399 /*
400  * returns a specific registered overlay based on the type; NULL if not
401  * registered.
402  */
403
404 slap_overinst *
405 overlay_find( const char *over_type )
406 {
407         slap_overinst *on = overlays;
408
409         assert( over_type );
410
411         for ( ; on; on = on->on_next ) {
412                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
413                         break;
414                 }
415         }
416
417         return on;
418 }
419
420 static const char overtype[] = "over";
421
422 /*
423  * returns TRUE (1) if the database is actually an overlay instance;
424  * FALSE (0) otherwise.
425  */
426
427 int
428 overlay_is_over( BackendDB *be )
429 {
430         return be->bd_info->bi_type == overtype;
431 }
432
433 /*
434  * returns TRUE (1) if the given database is actually an overlay
435  * instance and, somewhere in the list, contains the requested overlay;
436  * FALSE (0) otherwise.
437  */
438
439 int
440 overlay_is_inst( BackendDB *be, const char *over_type )
441 {
442         slap_overinst   *on;
443
444         assert( be );
445
446         if ( !overlay_is_over( be ) ) {
447                 return 0;
448         }
449         
450         on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
451         for ( ; on; on = on->on_next ) {
452                 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
453                         return 1;
454                 }
455         }
456
457         return 0;
458 }
459
460 /* add an overlay to a particular backend. */
461 int
462 overlay_config( BackendDB *be, const char *ov )
463 {
464         slap_overinst *on = NULL, *on2 = NULL;
465         slap_overinfo *oi = NULL;
466         BackendInfo *bi = NULL;
467
468         on = overlay_find( ov );
469         if ( !on ) {
470                 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
471                 return 1;
472         }
473
474         /* If this is the first overlay on this backend, set up the
475          * overlay info structure
476          */
477         if ( !overlay_is_over( be ) ) {
478                 oi = ch_malloc( sizeof( slap_overinfo ) );
479                 oi->oi_orig = be->bd_info;
480                 oi->oi_bi = *be->bd_info;
481
482                 /* Save a pointer to ourself in bi_private.
483                  * This allows us to keep working in conjunction
484                  * with backglue...
485                  */
486                 oi->oi_bi.bi_private = oi;
487                 oi->oi_list = NULL;
488                 bi = (BackendInfo *)oi;
489
490                 bi->bi_type = (char *)overtype;
491
492                 bi->bi_db_config = over_db_config;
493                 bi->bi_db_open = over_db_open;
494                 bi->bi_db_close = over_db_close;
495                 bi->bi_db_destroy = over_db_destroy;
496
497                 bi->bi_op_bind = over_op_bind;
498                 bi->bi_op_unbind = over_op_unbind;
499                 bi->bi_op_search = over_op_search;
500                 bi->bi_op_compare = over_op_compare;
501                 bi->bi_op_modify = over_op_modify;
502                 bi->bi_op_modrdn = over_op_modrdn;
503                 bi->bi_op_add = over_op_add;
504                 bi->bi_op_delete = over_op_delete;
505                 bi->bi_op_abandon = over_op_abandon;
506                 bi->bi_op_cancel = over_op_cancel;
507
508                 bi->bi_extended = over_op_extended;
509
510                 /*
511                  * this is fine because it has the same
512                  * args of the operations; we need to rework
513                  * all the hooks to share the same args
514                  * of the operations...
515                  */
516                 bi->bi_operational = over_aux_operational;
517                 bi->bi_chk_referrals = over_aux_chk_referrals;
518
519                 be->bd_info = bi;
520
521         } else {
522                 if ( overlay_is_inst( be, ov ) ) {
523                         Debug( LDAP_DEBUG_ANY, "overlay_config(): "
524                                         "warning, overlay \"%s\" "
525                                         "already in list\n", ov, 0, 0 );
526                 }
527
528                 oi = be->bd_info->bi_private;
529         }
530
531         /* Insert new overlay on head of list. Overlays are executed
532          * in reverse of config order...
533          */
534         on2 = ch_calloc( 1, sizeof(slap_overinst) );
535         *on2 = *on;
536         on2->on_info = oi;
537         on2->on_next = oi->oi_list;
538         oi->oi_list = on2;
539
540         /* Any initialization needed? */
541         if ( on->on_bi.bi_db_init ) {
542                 be->bd_info = (BackendInfo *)on2;
543                 on2->on_bi.bi_db_init( be );
544                 be->bd_info = (BackendInfo *)oi;
545         }
546
547         return 0;
548 }
549