]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
add referral check to functions elaborated by overlays
[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 = (slap_overinfo *) be->bd_info;
40         slap_overinst *on = oi->oi_list;
41         BI_db_open **func;
42         int rc = 0;
43
44         func = &oi->oi_orig->bi_db_open;
45         if ( func[which] ) {
46                 be->bd_info = oi->oi_orig;
47                 rc = func[which]( be );
48         }
49
50         for (; on && rc == 0; on=on->on_next) {
51                 be->bd_info = &on->on_bi;
52                 func = &on->on_bi.bi_db_open;
53                 if (func[which]) {
54                         rc = func[which]( be );
55                 }
56         }
57         be->bd_info = (BackendInfo *)oi;
58         return rc;
59 }
60
61 static int
62 over_db_config(
63         BackendDB *be,
64         const char *fname,
65         int lineno,
66         int argc,
67         char **argv
68 )
69 {
70         slap_overinfo *oi = (slap_overinfo *) be->bd_info;
71         slap_overinst *on = oi->oi_list;
72         int rc = 0;
73
74         if ( oi->oi_orig->bi_db_config ) {
75                 be->bd_info = oi->oi_orig;
76                 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
77                         argc, argv );
78                 be->bd_info = (BackendInfo *)oi;
79                 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
80         }
81
82         for (; on; on=on->on_next) {
83                 if (on->on_bi.bi_db_config) {
84                         be->bd_info = &on->on_bi;
85                         rc = on->on_bi.bi_db_config( be, fname, lineno,
86                                 argc, argv );
87                         if ( rc != SLAP_CONF_UNKNOWN ) break;
88                 }
89         }
90         be->bd_info = (BackendInfo *)oi;
91         return rc;
92 }
93
94 static int
95 over_db_open(
96         BackendDB *be
97 )
98 {
99         return over_db_func( be, db_open );
100 }
101
102 static int
103 over_db_close(
104         BackendDB *be
105 )
106 {
107         return over_db_func( be, db_close );
108 }
109
110 static int
111 over_db_destroy(
112         BackendDB *be
113 )
114 {
115         slap_overinfo *oi = (slap_overinfo *) be->bd_info;
116         slap_overinst *on = oi->oi_list, *next;
117         int rc;
118
119         rc = over_db_func( be, db_destroy );
120
121         for (next = on->on_next; on; on=next) {
122                 next = on->on_next;
123                 free( on );
124         }
125         free( oi );
126         return rc;
127 }
128
129 static int
130 over_back_response ( Operation *op, SlapReply *rs )
131 {
132         slap_overinfo *oi = op->o_callback->sc_private;
133         slap_overinst *on = oi->oi_list;
134         int rc = SLAP_CB_CONTINUE;
135         BackendDB *be = op->o_bd, db = *op->o_bd;
136
137         op->o_bd = &db;
138         for (; on; on=on->on_next ) {
139                 if ( on->on_response ) {
140                         db.bd_info = (BackendInfo *)on;
141                         rc = on->on_response( op, rs );
142                         if ( rc != SLAP_CB_CONTINUE ) break;
143                 }
144         }
145         op->o_bd = be;
146         return rc;
147 }
148
149 enum op_which {
150         op_bind = 0,
151         op_unbind,
152         op_search,
153         op_compare,
154         op_modify,
155         op_modrdn,
156         op_add,
157         op_delete,
158         op_abandon,
159         op_cancel,
160         op_extended,
161         op_aux_chk_referrals,
162         op_last
163 };
164
165 static int
166 over_op_func(
167         Operation *op,
168         SlapReply *rs,
169         enum op_which which
170 )
171 {
172         slap_overinfo *oi = (slap_overinfo *) op->o_bd->bd_info;
173         slap_overinst *on = oi->oi_list;
174         BI_op_bind **func;
175         BackendDB *be = op->o_bd, db = *op->o_bd;
176         slap_callback cb = {NULL, over_back_response, NULL, NULL};
177         int rc = SLAP_CB_CONTINUE;
178
179         op->o_bd = &db;
180         cb.sc_next = op->o_callback;
181         cb.sc_private = oi;
182         op->o_callback = &cb;
183
184         for (; on; on=on->on_next ) {
185                 func = &on->on_bi.bi_op_bind;
186                 if ( func[which] ) {
187                         db.bd_info = (BackendInfo *)on;
188                         rc = func[which]( op, rs );
189                         if ( rc != SLAP_CB_CONTINUE ) break;
190                 }
191         }
192
193         op->o_bd = be;
194         func = &oi->oi_orig->bi_op_bind;
195         if ( func[which] && rc == SLAP_CB_CONTINUE ) {
196                 rc = func[which]( op, rs );
197         }
198         /* should not fall thru this far without anything happening... */
199         if ( rc == SLAP_CB_CONTINUE ) {
200                 rc = LDAP_UNWILLING_TO_PERFORM;
201         }
202         op->o_callback = cb.sc_next;
203         return rc;
204 }
205
206 static int
207 over_op_bind( Operation *op, SlapReply *rs )
208 {
209         return over_op_func( op, rs, op_bind );
210 }
211
212 static int
213 over_op_unbind( Operation *op, SlapReply *rs )
214 {
215         return over_op_func( op, rs, op_unbind );
216 }
217
218 static int
219 over_op_search( Operation *op, SlapReply *rs )
220 {
221         return over_op_func( op, rs, op_search );
222 }
223
224 static int
225 over_op_compare( Operation *op, SlapReply *rs )
226 {
227         return over_op_func( op, rs, op_compare );
228 }
229
230 static int
231 over_op_modify( Operation *op, SlapReply *rs )
232 {
233         return over_op_func( op, rs, op_modify );
234 }
235
236 static int
237 over_op_modrdn( Operation *op, SlapReply *rs )
238 {
239         return over_op_func( op, rs, op_modrdn );
240 }
241
242 static int
243 over_op_add( Operation *op, SlapReply *rs )
244 {
245         return over_op_func( op, rs, op_add );
246 }
247
248 static int
249 over_op_delete( Operation *op, SlapReply *rs )
250 {
251         return over_op_func( op, rs, op_delete );
252 }
253
254 static int
255 over_op_abandon( Operation *op, SlapReply *rs )
256 {
257         return over_op_func( op, rs, op_abandon );
258 }
259
260 static int
261 over_op_cancel( Operation *op, SlapReply *rs )
262 {
263         return over_op_func( op, rs, op_cancel );
264 }
265
266 static int
267 over_op_extended( Operation *op, SlapReply *rs )
268 {
269         return over_op_func( op, rs, op_extended );
270 }
271
272 static int
273 over_chk_referrals( Operation *op, SlapReply *rs )
274 {
275         return over_op_func( op, rs, op_aux_chk_referrals );
276 }
277
278 int
279 overlay_register(
280         slap_overinst *on
281 )
282 {
283         on->on_next = overlays;
284         overlays = on;
285         return 0;
286 }
287
288 slap_overinst *
289 overlay_next(
290         slap_overinst *on
291 )
292 {
293         if ( on == NULL ) {
294                 return overlays;
295         }
296
297         return on->on_next;
298 }
299
300 static const char overtype[] = "over";
301
302 /* add an overlay to a particular backend. */
303 int
304 overlay_config( BackendDB *be, const char *ov )
305 {
306         slap_overinst *on = NULL, *on2 = NULL, *prev = NULL;
307         slap_overinfo *oi = NULL;
308         BackendInfo *bi = NULL;
309
310         for ( on = overlays; on; on=on->on_next ) {
311                 if (!strcmp( ov, on->on_bi.bi_type ) )
312                         break;
313         }
314         if (!on) {
315                 Debug( LDAP_DEBUG_ANY, "overlay %s not found\n", ov, 0, 0 );
316                 return 1;
317         }
318
319         /* If this is the first overlay on this backend, set up the
320          * overlay info structure
321          */
322         if ( be->bd_info->bi_type != overtype ) {
323                 oi = ch_malloc( sizeof(slap_overinfo) );
324                 oi->oi_orig = be->bd_info;
325                 oi->oi_bi = *be->bd_info;
326                 oi->oi_list = NULL;
327                 bi = (BackendInfo *)oi;
328
329                 bi->bi_type = (char *)overtype;
330
331                 bi->bi_db_config = over_db_config;
332                 bi->bi_db_open = over_db_open;
333                 bi->bi_db_close = over_db_close;
334                 bi->bi_db_destroy = over_db_destroy;
335
336                 bi->bi_op_bind = over_op_bind;
337                 bi->bi_op_unbind = over_op_unbind;
338                 bi->bi_op_search = over_op_search;
339                 bi->bi_op_compare = over_op_compare;
340                 bi->bi_op_modify = over_op_modify;
341                 bi->bi_op_modrdn = over_op_modrdn;
342                 bi->bi_op_add = over_op_add;
343                 bi->bi_op_delete = over_op_delete;
344                 bi->bi_op_abandon = over_op_abandon;
345                 bi->bi_op_cancel = over_op_cancel;
346
347                 bi->bi_extended = over_op_extended;
348
349                 /*
350                  * this is fine because it has the same
351                  * args of the operations; we need to rework
352                  * all the hooks to share the same args
353                  * of the operations...
354                  */
355                 bi->bi_chk_referrals = over_chk_referrals;
356
357                 be->bd_info = bi;
358
359         } else {
360                 oi = (slap_overinfo *) be->bd_info;
361         }
362
363 #if 0
364         /* Walk to the end of the list of overlays, add the new
365          * one onto the end
366          */
367         for ( prev=NULL, on2 = oi->oi_list; on2; prev=on2, on2=on2->on_next );
368         on2 = ch_calloc( 1, sizeof(slap_overinst) );
369         if ( !prev ) {
370                 oi->oi_list = on2;
371         } else {
372                 prev->on_next = on2;
373         }
374         *on2 = *on;
375         on2->on_next = NULL;
376         on2->on_info = oi;
377 #else
378         /* Insert new overlay on head of list. Overlays are executed
379          * in reverse of config order...
380          */
381         on2 = ch_calloc( 1, sizeof(slap_overinst) );
382         *on2 = *on;
383         on2->on_info = oi;
384         on2->on_next = oi->oi_list;
385         oi->oi_list = on2;
386 #endif
387
388         /* Any initialization needed? */
389         if ( on->on_bi.bi_db_init ) {
390                 be->bd_info = (BackendInfo *)on2;
391                 on2->on_bi.bi_db_init( be );
392                 be->bd_info = (BackendInfo *)oi;
393         }
394
395         return 0;
396 }
397