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