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