]> git.sur5r.net Git - openldap/blob - servers/slapd/backover.c
060d7b0245177e1f9301b242819dd1df3217d924
[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 { op_bind = 0, op_unbind, op_search, op_compare,
150         op_modify, op_modrdn, op_add, op_delete, op_abandon,
151         op_cancel, op_extended };
152
153 static int
154 over_op_func(
155         Operation *op,
156         SlapReply *rs,
157         enum op_which which
158 )
159 {
160         slap_overinfo *oi = (slap_overinfo *) op->o_bd->bd_info;
161         slap_overinst *on = oi->oi_list;
162         BI_op_bind **func;
163         BackendDB *be = op->o_bd, db = *op->o_bd;
164         slap_callback cb = {NULL, over_back_response, NULL, NULL};
165         int rc = SLAP_CB_CONTINUE;
166
167         op->o_bd = &db;
168         cb.sc_next = op->o_callback;
169         cb.sc_private = oi;
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_orig->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_orig = be->bd_info;
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