]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
cleanup
[openldap] / servers / slapd / overlays / translucent.c
1 /* translucent.c - translucent proxy module */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2004-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2005 Symas Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Symas Corp. for inclusion in
19  * OpenLDAP Software.  This work was sponsored by Hewlett-Packard.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_TRANSLUCENT
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "lutil.h"
33
34 /* config block */
35
36 typedef struct translucent_configuration {
37         int debug;
38         int strict;
39         int no_add;
40         int glue;
41 } translucent_configuration;
42
43 /* stack of captive backends */
44
45 typedef struct overlay_stack {
46         BackendInfo *info;                      /* captive backend */
47         void *private;                          /* local backend_private */
48         translucent_configuration *config;      /* our_private: configuration */
49 } overlay_stack;
50
51 /* for translucent_init() */
52
53 static slap_overinst translucent;
54
55 /*
56 ** glue_parent()
57 **      call syncrepl_add_glue() with the parent suffix;
58 **
59 */
60
61 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
62
63 void glue_parent(Operation *op) {
64         Operation nop = *op;
65         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
66         struct berval ndn = BER_BVNULL;
67         Attribute *a;
68         Entry *e;
69         struct berval   pdn;
70
71         dnParent( &op->o_req_ndn, &pdn );
72         ber_dupbv_x( &ndn, &pdn, op->o_tmpmemctx );
73
74         Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", ndn.bv_val, 0, 0);
75
76         e = ch_calloc(1, sizeof(Entry));
77         e->e_id = NOID;
78         ber_dupbv(&e->e_name, &ndn);
79         ber_dupbv(&e->e_nname, &ndn);
80
81         a = ch_calloc(1, sizeof(Attribute));
82         a->a_desc = slap_schema.si_ad_objectClass;
83         a->a_vals = ch_malloc(sizeof(struct berval) * 3);
84         ber_dupbv(&a->a_vals[0], &glue[0]);
85         ber_dupbv(&a->a_vals[1], &glue[1]);
86         ber_dupbv(&a->a_vals[2], &glue[2]);
87         a->a_nvals = a->a_vals;
88         a->a_next = e->e_attrs;
89         e->e_attrs = a;
90
91         a = ch_calloc(1, sizeof(Attribute));
92         a->a_desc = slap_schema.si_ad_structuralObjectClass;
93         a->a_vals = ch_malloc(sizeof(struct berval) * 2);
94         ber_dupbv(&a->a_vals[0], &glue[1]);
95         ber_dupbv(&a->a_vals[1], &glue[2]);
96         a->a_nvals = a->a_vals;
97         a->a_next = e->e_attrs;
98         e->e_attrs = a;
99
100         nop.o_req_dn = ndn;
101         nop.o_req_ndn = ndn;
102         nop.ora_e = e;
103         nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
104
105         syncrepl_add_glue(&nop, e);
106
107         op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
108
109         return;
110 }
111
112 /*
113 ** dup_bervarray()
114 **      copy a BerVarray;
115 */
116
117 BerVarray dup_bervarray(BerVarray b) {
118         int i, len;
119         BerVarray nb;
120         for(len = 0; b[len].bv_val; len++);
121         nb = ch_malloc((len+1) * sizeof(BerValue));
122         for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
123         nb[len].bv_val = NULL;
124         nb[len].bv_len = 0;
125         return(nb);
126 }
127
128 /*
129 ** free_attr_chain()
130 **      free only the Attribute*, not the contents;
131 **
132 */
133 void free_attr_chain(Attribute *a) {
134         Attribute *ax;
135         for(; a; a = ax) {
136                 ax = a->a_next;
137                 ch_free(a);
138         }
139         return;
140 }
141
142 /*
143 ** translucent_add()
144 **      if not bound as root, send ACCESS error;
145 **      if config.glue, glue_parent();
146 **      return CONTINUE;
147 **
148 */
149
150 static int translucent_add(Operation *op, SlapReply *rs) {
151         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
152         overlay_stack *ov = on->on_bi.bi_private;
153         Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
154                 op->o_req_dn.bv_val, 0, 0);
155         if(!be_isroot(op)) {
156                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
157                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
158                         "user modification of overlay database not permitted");
159                 return(rs->sr_err);
160         }
161         if(!ov->config->glue) glue_parent(op);
162         return(SLAP_CB_CONTINUE);
163 }
164
165 /*
166 ** translucent_modrdn()
167 **      if not bound as root, send ACCESS error;
168 **      if !config.glue, glue_parent();
169 **      else return CONTINUE;
170 **
171 */
172
173 static int translucent_modrdn(Operation *op, SlapReply *rs) {
174         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
175         overlay_stack *ov = on->on_bi.bi_private;
176         Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
177                 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
178         if(!be_isroot(op)) {
179                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
180                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
181                         "user modification of overlay database not permitted");
182                 return(rs->sr_err);
183         }
184         if(!ov->config->glue) glue_parent(op);
185         return(SLAP_CB_CONTINUE);
186 }
187
188 /*
189 ** translucent_delete()
190 **      if not bound as root, send ACCESS error;
191 **      else return CONTINUE;
192 **
193 */
194
195 static int translucent_delete(Operation *op, SlapReply *rs) {
196         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
197         Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
198                 op->o_req_dn.bv_val, 0, 0);
199         if(!be_isroot(op)) {
200                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
201                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
202                         "user modification of overlay database not permitted");
203                 return(rs->sr_err);
204         }
205         return(SLAP_CB_CONTINUE);
206 }
207
208 static int
209 translucent_tag_cb( Operation *op, SlapReply *rs )
210 {
211         op->o_tag = (ber_tag_t)op->o_callback->sc_private;
212         rs->sr_tag = slap_req2res( op->o_tag );
213
214         return SLAP_CB_CONTINUE;
215 }
216
217 /*
218 ** translucent_modify()
219 **      modify in local backend if exists in both;
220 **      otherwise, add to local backend;
221 **      fail if not defined in captive backend;
222 **
223 */
224
225 static int translucent_modify(Operation *op, SlapReply *rs) {
226         SlapReply nrs = { REP_RESULT };
227         Operation nop = *op;
228
229         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
230         overlay_stack *ov = on->on_bi.bi_private;
231         void *private = op->o_bd->be_private;
232         Entry ne, *e = NULL, *re = NULL;
233         Attribute *a, *ax;
234         Modifications *m, *mm;
235         int del, rc, erc = 0;
236         slap_callback cb = { 0 };
237
238         Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
239                 op->o_req_dn.bv_val, 0, 0);
240
241 /*
242 ** fetch entry from the captive backend;
243 ** if it did not exist, fail;
244 ** release it, if captive backend supports this;
245 **
246 */
247
248         op->o_bd->bd_info = (BackendInfo *) on->on_info;
249         op->o_bd->be_private = ov->private;
250         rc = ov->info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
251         op->o_bd->be_private = private;
252
253         /* if(ov->config->no_add && (!re || rc != LDAP_SUCCESS)) */
254         if(rc != LDAP_SUCCESS || re == NULL ) {
255                 send_ldap_error(op, rs, LDAP_NO_SUCH_OBJECT,
256                         "attempt to modify nonexistent local record");
257                 return(rs->sr_err);
258         }
259
260 /*
261 ** fetch entry from local backend;
262 ** if it exists:
263 **      foreach Modification:
264 **          if attr not present in local:
265 **              if Mod == LDAP_MOD_DELETE:
266 **                  if remote attr not present, return NO_SUCH;
267 **                  if remote attr present, drop this Mod;
268 **              else force this Mod to LDAP_MOD_ADD;
269 **      return CONTINUE;
270 **
271 */
272
273         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
274
275         if(e && rc == LDAP_SUCCESS) {
276                 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
277                 for(m = op->orm_modlist; m; m = m->sml_next) {
278                         for(a = e->e_attrs; a; a = a->a_next)
279                                 if(a->a_desc == m->sml_desc) break;
280                         if(a) continue;         /* found local attr */
281                         if(m->sml_op == LDAP_MOD_DELETE) {
282                                 for(a = re->e_attrs; a; a = a->a_next)
283                                         if(a->a_desc == m->sml_desc) break;
284                                 /* not found remote attr */
285                                 if(!a) {
286                                         erc = LDAP_NO_SUCH_ATTRIBUTE;
287                                         goto release;
288                                 }
289                                 if(ov->config->strict) {
290                                         erc = LDAP_CONSTRAINT_VIOLATION;
291                                         goto release;
292                                 }
293                                 Debug(LDAP_DEBUG_TRACE,
294                                         "=> translucent_modify: silently dropping delete: %s\n",
295                                         m->sml_desc->ad_cname.bv_val, 0, 0);
296                                 for(mm = op->orm_modlist; mm->sml_next != m; mm = mm->sml_next);
297                                 mm->sml_next = m->sml_next;
298                                 mm = m;
299                                 m = m->sml_next;
300                                 mm->sml_next = NULL;            /* hack */
301                                 slap_mods_free(mm, 1);
302                                 if(m) continue;
303                         }
304                         m->sml_op = LDAP_MOD_ADD;
305                 }
306                 erc = SLAP_CB_CONTINUE;
307 release:
308                 if(re) {
309                         op->o_bd->be_private = ov->private;
310                         if(ov->info->bi_entry_release_rw)
311                                 ov->info->bi_entry_release_rw(op, re, 0);
312                         else
313                                 entry_free(re);
314                         op->o_bd->be_private = private;
315                 }
316                 be_entry_release_r(op, e);
317                 if(erc == SLAP_CB_CONTINUE) {
318                         op->o_bd->bd_info = (BackendInfo *) on;
319                         return(erc);
320                 } else if(erc) {
321                         send_ldap_error(op, rs, erc,
322                                 "attempt to delete nonexistent attribute");
323                         return(erc);
324                 }
325         }
326
327         /* don't leak remote entry copy */
328         if(re) {
329                 op->o_bd->be_private = ov->private;
330                 if(ov->info->bi_entry_release_rw)
331                         ov->info->bi_entry_release_rw(op, re, 0);
332                 else
333                         entry_free(re);
334                 op->o_bd->be_private = private;
335         }
336 /*
337 ** foreach Modification:
338 **      if MOD_ADD or MOD_REPLACE, add Attribute;
339 ** if no Modifications were suitable:
340 **      if config.strict, throw CONSTRAINT_VIOLATION;
341 **      else, return early SUCCESS;
342 ** fabricate Entry with new Attribute chain;
343 ** glue_parent() for this Entry;
344 ** call bi_op_add() in local backend;
345 **
346 */
347
348         Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
349         a = NULL;
350         for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
351                 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
352                    ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
353                         Debug(LDAP_DEBUG_ANY,
354                                 "=> translucent_modify: silently dropped modification(%d): %s\n",
355                                 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
356                         if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
357                         continue;
358                 }
359                 a = ch_calloc(1, sizeof(Attribute));
360                 a->a_desc  = m->sml_desc;
361                 a->a_vals  = m->sml_values;
362                 a->a_nvals = m->sml_nvalues;
363                 a->a_next  = ax;
364                 ax = a;
365         }
366
367         if(del && ov->config->strict) {
368                 free_attr_chain(a);
369                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
370                         "attempt to delete attributes from local database");
371                 return(rs->sr_err);
372         }
373
374         if(!ax) {
375                 if(ov->config->strict) {
376                         send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
377                                 "modification contained other than ADD or REPLACE");
378                         return(rs->sr_err);
379                 }
380                 op->o_bd->bd_info = (BackendInfo *) on;
381                 /* rs->sr_text = "no valid modification found"; */
382                 rs->sr_err = LDAP_SUCCESS;
383                 send_ldap_result(op, rs);
384                 return(rs->sr_err);
385         }
386
387         ne.e_id         = NOID;
388         ne.e_name       = op->o_req_dn;
389         ne.e_nname      = op->o_req_ndn;
390         ne.e_attrs      = a;
391         ne.e_ocflags    = 0;
392         ne.e_bv.bv_len  = 0;
393         ne.e_bv.bv_val  = NULL;
394         ne.e_private    = NULL;
395
396         nop.o_tag       = LDAP_REQ_ADD;
397         nop.oq_add.rs_e = &ne;
398
399         op->o_bd->bd_info = (BackendInfo *) on;
400         glue_parent(&nop);
401
402         cb.sc_response = translucent_tag_cb;
403         cb.sc_private = (void *)LDAP_REQ_MODIFY;
404         cb.sc_next = nop.o_callback;
405         nop.o_callback = &cb;
406         rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
407         free_attr_chain(a);
408
409         return(rc);
410 }
411
412 static int translucent_compare(Operation *op, SlapReply *rs) {
413         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
414         overlay_stack *ov = on->on_bi.bi_private;
415         void *private = op->o_bd->be_private;
416
417         AttributeAssertion *ava = op->orc_ava;
418         Entry *e;
419         int rc;
420
421         Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
422                 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
423
424 /*
425 ** if the local backend has an entry for this attribute:
426 **      CONTINUE and let it do the compare;
427 **
428 */
429
430         op->o_bd->bd_info = (BackendInfo *) on->on_info;
431         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e);
432         if(e && rc == LDAP_SUCCESS) {
433                 be_entry_release_r(op, e);
434                 op->o_bd->bd_info = (BackendInfo *) on;
435                 return(SLAP_CB_CONTINUE);
436         }
437
438 /*
439 ** call compare() in the captive backend;
440 ** return the result;
441 **
442 */
443
444         op->o_bd->be_private = ov->private;
445         rc = ov->info->bi_op_compare(op, rs);
446         op->o_bd->be_private = private;
447         op->o_bd->bd_info = (BackendInfo *) on;
448         return(rc);
449 }
450
451 /*
452 ** translucent_search_cb()
453 **      merge local data with the search result
454 **
455 */
456
457 static int translucent_search_cb(Operation *op, SlapReply *rs) {
458         slap_overinst *on;
459         Entry *e, *re = NULL;
460         Attribute *a, *ax, *an, *as = NULL;
461         void *private;
462         int rc;
463
464         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
465                 return(SLAP_CB_CONTINUE);
466
467         Debug(LDAP_DEBUG_TRACE, "==> tranclucent_search_cb: %s\n",
468                 rs->sr_entry->e_name.bv_val, 0, 0);
469
470         on = (slap_overinst *) op->o_bd->bd_info;
471         op->o_bd->bd_info = (BackendInfo *) on->on_info;
472
473         private = op->o_bd->be_private;
474         op->o_bd->be_private = op->o_callback->sc_private;
475
476         rc = be_entry_get_rw(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
477
478 /*
479 ** if we got an entry from local backend:
480 **      make a copy of this search result;
481 **      foreach local attr:
482 **              foreach search result attr:
483 **                      if match, result attr with local attr;
484 **                      if new local, add to list;
485 **      append new local attrs to search result;
486 **
487 */
488
489         if(e && rc == LDAP_SUCCESS) {
490                 re = entry_dup(rs->sr_entry);
491                 for(ax = e->e_attrs; ax; ax = ax->a_next) {
492 #if 0
493                         if(is_at_operational(ax->a_desc->ad_type)) continue;
494 #endif
495                         for(a = re->e_attrs; a; a = a->a_next) {
496                                 if(a->a_desc == ax->a_desc) {
497                                         if(a->a_vals != a->a_nvals)
498                                                 ber_bvarray_free(a->a_nvals);
499                                         ber_bvarray_free(a->a_vals);
500                                         a->a_vals = dup_bervarray(ax->a_vals);
501                                         a->a_nvals = (ax->a_vals == ax->a_nvals) ?
502                                                 a->a_vals : dup_bervarray(ax->a_nvals);
503                                         break;
504                                 }
505                         }
506                         if(a) continue;
507                         an = attr_dup(ax);
508                         an->a_next = as;
509                         as = an;
510                 }
511                 be_entry_release_r(op, e);
512
513                 /* literally append, so locals are always last */
514                 if(as) {
515                         if(re->e_attrs) {
516                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
517                                 ax->a_next = as;
518                         } else {
519                                 re->e_attrs = as;
520                         }
521                 }
522                 rs->sr_entry = re;
523                 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
524         }
525
526         op->o_bd->be_private = private;
527         op->o_bd->bd_info = (BackendInfo *) on;
528
529         return(SLAP_CB_CONTINUE);
530 }
531
532 /*
533 ** translucent_search()
534 **      search via captive backend;
535 **      override results with any local data;
536 **
537 */
538
539 static int translucent_search(Operation *op, SlapReply *rs) {
540         Operation nop = *op;
541
542         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
543         slap_callback cb = { NULL, NULL, NULL, NULL };
544         overlay_stack *ov = on->on_bi.bi_private;
545         void *private = op->o_bd->be_private;
546         int rc;
547
548         Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
549                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
550         cb.sc_response = (slap_response *) translucent_search_cb;
551         cb.sc_private = private;
552
553         cb.sc_next = nop.o_callback;
554         nop.o_callback = &cb;
555
556         op->o_bd->be_private = ov->private;
557         rc = ov->info->bi_op_search(&nop, rs);
558         op->o_bd->be_private = private;
559
560         return(rs->sr_err);
561 }
562
563
564 /*
565 ** translucent_bind()
566 **      pass bind request to captive backend;
567 **
568 */
569
570 static int translucent_bind(Operation *op, SlapReply *rs) {
571         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
572         overlay_stack *ov = on->on_bi.bi_private;
573         void *private = op->o_bd->be_private;
574         int rc = 0;
575
576         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
577                 op->o_req_dn.bv_val, op->orb_method, 0);
578
579         op->o_bd->be_private = ov->private;
580         rc = ov->info->bi_op_bind(op, rs);
581         op->o_bd->be_private = private;
582
583         return(rc);
584 }
585
586 /*
587 ** translucent_connection_destroy()
588 **      pass disconnect notification to captive backend;
589 **
590 */
591
592 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
593         slap_overinst *on = (slap_overinst *) be->bd_info;
594         overlay_stack *ov = on->on_bi.bi_private;
595         void *private = be->be_private;
596         int rc = 0;
597
598         Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
599
600         be->be_private = ov->private;
601         rc = ov->info->bi_connection_destroy(be, conn);
602         be->be_private = private;
603
604         return(rc);
605 }
606
607 /*
608 ** translucent_db_config()
609 **      pass config directives to captive backend;
610 **      parse unrecognized directives ourselves;
611 **
612 */
613
614 static int translucent_db_config(
615         BackendDB       *be,
616         const char      *fname,
617         int             lineno,
618         int             argc,
619         char            **argv
620 )
621 {
622         slap_overinst *on = (slap_overinst *) be->bd_info;
623         overlay_stack *ov = on->on_bi.bi_private;
624         void *private = be->be_private;
625         void *be_cf_ocs = be->be_cf_ocs;
626         int rc;
627
628         /* "this should never happen" */
629         if(!ov->info) {
630                 fprintf(stderr, "fatal: captive backend not initialized");
631                 return(1);
632         }
633
634         be->be_private = ov->private;
635         be->be_cf_ocs = ov->info->bi_cf_ocs;
636         rc = ov->info->bi_db_config ? ov->info->bi_db_config(be, fname, lineno, argc, argv) : 0;
637         be->be_private = private;
638         be->be_cf_ocs = be_cf_ocs;
639
640         /* pass okay or error up, SLAP_CONF_UNKNOWN might be ours */
641         if(rc == 0 || rc == 1) return(rc);
642
643         rc = 0;
644         if(!strcasecmp(*argv, "translucent_strict")) {
645                 ov->config->strict++;
646         } else if(!strcasecmp(*argv, "translucent_no_add")) {
647                 ov->config->no_add++;
648         } else if(!strcasecmp(*argv, "translucent_no_glue")) {
649                 ov->config->glue++;
650         } else if(!strcasecmp(*argv, "translucent_debug")) {
651                 if(argc == 1) {
652                         ov->config->debug = 0xFFFF;
653                         rc = 0;
654                 } else if(argc == 2) {
655                         if ( lutil_atoi( &ov->config->debug, argv[1]) != 0 ) {
656                                 fprintf(stderr, "%s: line %d: unable to parse debug \"%s\"\n",
657                                         fname, lineno, argv[1]);
658                                 return 1;
659                         }
660                         rc = 0;
661                 } else {
662                         fprintf(stderr, "%s: line %d: too many arguments (%d) to debug\n",
663                                 fname, lineno, argc);
664                         rc = 1;
665                 }
666         } else {
667                 fprintf(stderr, "%s: line %d: unknown keyword %s\n",
668                         fname, lineno, *argv);
669                 rc = SLAP_CONF_UNKNOWN;
670         }
671         return(rc);
672 }
673
674 /*
675 ** translucent_db_init()
676 **      initialize the captive backend;
677 **
678 */
679
680 static int translucent_db_init(BackendDB *be) {
681         slap_overinst *on = (slap_overinst *) be->bd_info;
682         void *private = be->be_private;
683         overlay_stack *ov;
684         int rc;
685
686         Debug(LDAP_DEBUG_TRACE, "==> translucent_init\n", 0, 0, 0);
687
688         ov = ch_calloc(1, sizeof(overlay_stack));
689         ov->config = ch_calloc(1, sizeof(translucent_configuration));
690         ov->info = backend_info("ldap");
691
692         if(!ov->info) {
693                 ch_free( ov );
694                 Debug(LDAP_DEBUG_ANY, "translucent: backend_info failed!\n", 0, 0, 0);
695                 return(1);
696         }
697
698         /* forcibly disable schema checking on the local backend */
699         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
700
701         be->be_private = NULL;
702         rc = ov->info->bi_db_init ? ov->info->bi_db_init(be) : 0;
703
704         if(rc) Debug(LDAP_DEBUG_TRACE,
705                 "translucent: bi_db_init() returned error %d\n", rc, 0, 0);
706
707         ov->private = be->be_private;
708         be->be_private = private;
709         on->on_bi.bi_private = ov;
710         return(rc);
711 }
712
713 /*
714 ** translucent_db_open()
715 **      if the captive backend has an open() method, call it;
716 **
717 */
718
719 static int translucent_db_open(BackendDB *be) {
720         slap_overinst *on = (slap_overinst *) be->bd_info;
721         overlay_stack *ov = on->on_bi.bi_private;
722         void *private = be->be_private;
723         int rc;
724
725         /* "should never happen" */
726         if(!ov->info) {
727                 Debug(LDAP_DEBUG_ANY, "translucent_open() called with bad ov->info\n", 0, 0, 0);
728                 return(LDAP_OTHER);
729         }
730
731         Debug(LDAP_DEBUG_TRACE, "translucent_open\n", 0, 0, 0);
732
733         be->be_private = ov->private;
734         rc = ov->info->bi_db_open ? ov->info->bi_db_open(be) : 0;
735         be->be_private = private;
736
737         if(rc) Debug(LDAP_DEBUG_TRACE,
738                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
739
740         return(rc);
741 }
742
743 /*
744 ** translucent_db_close()
745 **      if the captive backend has a close() method, call it;
746 **      free any config data;
747 **
748 */
749
750 static int translucent_db_close(BackendDB *be) {
751         slap_overinst *on = (slap_overinst *) be->bd_info;
752         overlay_stack *ov = on->on_bi.bi_private;
753         int rc = 0;
754
755         if ( ov ) {
756                 void *private = be->be_private;
757
758                 be->be_private = ov->private;
759                 rc = (ov->info && ov->info->bi_db_close) ? ov->info->bi_db_close(be) : 0;
760                 be->be_private = private;
761                 if(ov->config) ch_free(ov->config);
762                 ov->config = NULL;
763         }
764
765         return(rc);
766 }
767
768 /*
769 ** translucent_db_destroy()
770 **      if the captive backend has a db_destroy() method, call it
771 **
772 */
773
774 static int translucent_db_destroy(BackendDB *be) {
775         slap_overinst *on = (slap_overinst *) be->bd_info;
776         overlay_stack *ov = on->on_bi.bi_private;
777         int rc = 0;
778
779         if ( ov ) {
780                 void *private = be->be_private;
781
782                 be->be_private = ov->private;
783                 rc = (ov->info && ov->info->bi_db_destroy) ? ov->info->bi_db_destroy(be) : 0;
784                 be->be_private = private;
785                 ch_free(ov);
786                 on->on_bi.bi_private = NULL;
787         }
788
789         return(rc);
790 }
791
792 /*
793 ** translucent_initialize()
794 **      initialize the slap_overinst with our entry points;
795 **
796 */
797
798 int translucent_initialize() {
799
800         translucent.on_bi.bi_type       = "translucent";
801         translucent.on_bi.bi_db_init    = translucent_db_init;
802         translucent.on_bi.bi_db_config  = translucent_db_config;
803         translucent.on_bi.bi_db_open    = translucent_db_open;
804         translucent.on_bi.bi_db_close   = translucent_db_close;
805         translucent.on_bi.bi_db_destroy = translucent_db_destroy;
806         translucent.on_bi.bi_op_bind    = translucent_bind;
807         translucent.on_bi.bi_op_add     = translucent_add;
808         translucent.on_bi.bi_op_modify  = translucent_modify;
809         translucent.on_bi.bi_op_modrdn  = translucent_modrdn;
810         translucent.on_bi.bi_op_delete  = translucent_delete;
811         translucent.on_bi.bi_op_search  = translucent_search;
812         translucent.on_bi.bi_op_compare = translucent_compare;
813         translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
814
815         return(overlay_register(&translucent));
816 }
817
818 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
819 int init_module(int argc, char *argv[]) {
820         return translucent_initialize();
821 }
822 #endif
823
824 #endif /* SLAPD_OVER_TRANSLUCENT */
825