]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
misc cleanup
[openldap] / servers / slapd / backover.c
1 /* backover.c - backend overlay routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * Functions to overlay other modules over a backend.
10  *
11  *  -- Howard Chu
12  */
13
14 #include "portable.h"
15
16 #include <stdio.h>
17
18 #include <ac/string.h>
19 #include <ac/socket.h>
20
21 #define SLAPD_TOOLS
22 #include "slap.h"
23
24 static slap_overinst *overlays;
25
26 enum db_which { db_open = 0, db_close, db_destroy };
27
28 static int
29 over_db_func(
30         BackendDB *be,
31         enum db_which which
32 )
33 {
34         slap_overinfo *oi = (slap_overinfo *) be->bd_info;
35         slap_overinst *on = oi->oi_list;
36         BackendDB bd;
37         BI_db_open **func;
38         int rc = 0;
39
40         func = &oi->oi_bd.bd_info->bi_db_open;
41         if ( func[which] ) {
42                 rc = func[which]( &oi->oi_bd );
43                 if ( rc ) return rc;
44         }
45
46         bd = *be;
47         for (; on; on=on->on_next) {
48                 bd.bd_info = &on->on_bi;
49                 func = &on->on_bi.bi_db_open;
50                 if (func[which]) {
51                         rc = func[which]( &bd );
52                         if ( rc ) break;
53                 }
54         }
55         return rc;
56 }
57
58 static int
59 over_db_config(
60         BackendDB *be,
61         const char *fname,
62         int lineno,
63         int argc,
64         char **argv
65 )
66 {
67         slap_overinfo *oi = (slap_overinfo *) be->bd_info;
68         slap_overinst *on = oi->oi_list;
69         BackendDB bd;
70         int rc = 0;
71
72         if ( oi->oi_bd.bd_info->bi_db_config ) {
73                 rc = oi->oi_bd.bd_info->bi_db_config( &oi->oi_bd, fname, lineno,
74                         argc, argv );
75                 if ( rc ) return rc;
76         }
77
78         bd = *be;
79         for (; on; on=on->on_next) {
80                 bd.bd_info = &on->on_bi;
81                 if (on->on_bi.bi_db_config) {
82                         rc = on->on_bi.bi_db_config( &bd, fname, lineno,
83                                 argc, argv );
84                         if ( rc ) break;
85                 }
86         }
87         return rc;
88 }
89
90 static int
91 over_db_open(
92         BackendDB *be
93 )
94 {
95         return over_db_func( be, db_open );
96 }
97
98 static int
99 over_db_close(
100         BackendDB *be
101 )
102 {
103         return over_db_func( be, db_close );
104 }
105
106 static int
107 over_db_destroy(
108         BackendDB *be
109 )
110 {
111         slap_overinfo *oi = (slap_overinfo *) be->bd_info;
112         slap_overinst *on = oi->oi_list, *next;
113         int rc;
114
115         rc = over_db_func( be, db_destroy );
116
117         for (next = on->on_next; on; on=next) {
118                 next = on->on_next;
119                 free( on );
120         }
121         free( oi );
122         return rc;
123 }
124
125 static int
126 over_back_response ( Operation *op, SlapReply *rs )
127 {
128         slap_overinfo *oi = (slap_overinfo *) op->o_bd->bd_info;
129         slap_overinst *on = oi->oi_list;
130         int rc = SLAP_CB_CONTINUE;
131         BackendDB *be = op->o_bd, db = *op->o_bd;
132         slap_callback *sc = op->o_callback->sc_private;
133         slap_callback *s0 = op->o_callback;
134
135         op->o_bd = &db;
136         op->o_callback = sc;
137         for (; on; on=on->on_next ) {
138                 if ( on->on_response ) {
139                         db.bd_info = (BackendInfo *)on;
140                         rc = on->on_response( op, rs );
141                         if ( rc != SLAP_CB_CONTINUE ) break;
142                 }
143         }
144         if ( sc && (rc == SLAP_CB_CONTINUE) ) {
145                 rc = sc->sc_response( op, rs );
146         }
147         op->o_bd = be;
148         op->o_callback = s0;
149         return rc;
150 }
151
152 enum op_which { op_bind = 0, op_unbind, op_search, op_compare,
153         op_modify, op_modrdn, op_add, op_delete, op_abandon,
154         op_cancel, op_extended };
155
156 static int
157 over_op_func(
158         Operation *op,
159         SlapReply *rs,
160         enum op_which which
161 )
162 {
163         slap_overinfo *oi = (slap_overinfo *) op->o_bd->bd_info;
164         slap_overinst *on = oi->oi_list;
165         BI_op_bind **func;
166         BackendDB *be = op->o_bd, db = *op->o_bd;
167         slap_callback cb = {over_back_response, NULL};
168         int rc = 0;
169
170         op->o_bd = &db;
171         cb.sc_private = op->o_callback;
172         op->o_callback = &cb;
173
174         for (; on; on=on->on_next ) {
175                 func = &on->on_bi.bi_op_bind;
176                 if ( func[which] ) {
177                         db.bd_info = (BackendInfo *)on;
178                         rc = func[which]( op, rs );
179                         if ( rc ) break;
180                 }
181         }
182
183         func = &oi->oi_bd.bd_info->bi_op_bind;
184         if ( func[which] ) {
185                 rc = func[which]( op, rs );
186         }
187
188         op->o_bd = be;
189         return rc;
190 }
191
192 static int
193 over_op_bind( Operation *op, SlapReply *rs )
194 {
195         return over_op_func( op, rs, op_bind );
196 }
197
198 static int
199 over_op_unbind( Operation *op, SlapReply *rs )
200 {
201         return over_op_func( op, rs, op_unbind );
202 }
203
204 static int
205 over_op_search( Operation *op, SlapReply *rs )
206 {
207         return over_op_func( op, rs, op_search );
208 }
209
210 static int
211 over_op_compare( Operation *op, SlapReply *rs )
212 {
213         return over_op_func( op, rs, op_compare );
214 }
215
216 static int
217 over_op_modify( Operation *op, SlapReply *rs )
218 {
219         return over_op_func( op, rs, op_modify );
220 }
221
222 static int
223 over_op_modrdn( Operation *op, SlapReply *rs )
224 {
225         return over_op_func( op, rs, op_modrdn );
226 }
227
228 static int
229 over_op_add( Operation *op, SlapReply *rs )
230 {
231         return over_op_func( op, rs, op_add );
232 }
233
234 static int
235 over_op_delete( Operation *op, SlapReply *rs )
236 {
237         return over_op_func( op, rs, op_delete );
238 }
239
240 static int
241 over_op_abandon( Operation *op, SlapReply *rs )
242 {
243         return over_op_func( op, rs, op_abandon );
244 }
245
246 static int
247 over_op_cancel( Operation *op, SlapReply *rs )
248 {
249         return over_op_func( op, rs, op_cancel );
250 }
251
252 static int
253 over_op_extended( Operation *op, SlapReply *rs )
254 {
255         return over_op_func( op, rs, op_extended );
256 }
257
258 int
259 overlay_register(
260         slap_overinst *on
261 )
262 {
263         on->on_next = overlays;
264         overlays = on;
265         return 0;
266 }
267
268 static const char overtype[] = "over";
269
270 /* add an overlay to a particular backend. */
271 int
272 overlay_config( BackendDB *be, const char *ov )
273 {
274         slap_overinst *on, *on2, *prev;
275         slap_overinfo *oi;
276         BackendInfo *bi;
277
278         for ( on = overlays; on; on=on->on_next ) {
279                 if (!strcmp( ov, on->on_bi.bi_type ) )
280                         break;
281         }
282         if (!on) {
283                 Debug( LDAP_DEBUG_ANY, "overlay %s not found\n", ov, 0, 0 );
284                 return 1;
285         }
286
287         /* If this is the first overlay on this backend, set up the
288          * overlay info structure
289          */
290         if ( be->bd_info->bi_type != overtype ) {
291                 oi = ch_malloc( sizeof(slap_overinfo) );
292                 oi->oi_bd = *be;
293                 oi->oi_bi = *be->bd_info;
294                 oi->oi_list = NULL;
295                 bi = (BackendInfo *)oi;
296
297                 bi->bi_type = (char *)overtype;
298
299                 bi->bi_db_config = over_db_config;
300                 bi->bi_db_open = over_db_open;
301                 bi->bi_db_close = over_db_close;
302                 bi->bi_db_destroy = over_db_destroy;
303
304                 bi->bi_op_bind = over_op_bind;
305                 bi->bi_op_unbind = over_op_unbind;
306                 bi->bi_op_search = over_op_search;
307                 bi->bi_op_compare = over_op_compare;
308                 bi->bi_op_modify = over_op_modify;
309                 bi->bi_op_modrdn = over_op_modrdn;
310                 bi->bi_op_add = over_op_add;
311                 bi->bi_op_delete = over_op_delete;
312                 bi->bi_op_abandon = over_op_abandon;
313                 bi->bi_op_cancel = over_op_cancel;
314                 bi->bi_extended = over_op_extended;
315
316                 be->bd_info = bi;
317         }
318
319         /* Walk to the end of the list of overlays, add the new
320          * one onto the end
321          */
322         oi = (slap_overinfo *) be->bd_info;
323         for ( prev=NULL, on2 = oi->oi_list; on2; prev=on2, on2=on2->on_next );
324         on2 = ch_malloc( sizeof(slap_overinst) );
325         if ( !prev ) {
326                 oi->oi_list = on2;
327         } else {
328                 prev->on_next = on2;
329         }
330         *on2 = *on;
331         on2->on_next = NULL;
332
333         /* Any initialization needed? */
334         if ( on->on_bi.bi_db_init ) {
335                 be->bd_info = (BackendInfo *)on2;
336                 on2->on_bi.bi_db_init( be );
337                 be->bd_info = (BackendInfo *)oi;
338         }
339
340         return 0;
341 }
342