]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
happy new year
[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-2007 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 = LDAP_REQ_MODIFY;
212         op->orm_modlist = op->o_callback->sc_private;
213         rs->sr_tag = slap_req2res( op->o_tag );
214
215         return SLAP_CB_CONTINUE;
216 }
217
218 /*
219 ** translucent_modify()
220 **      modify in local backend if exists in both;
221 **      otherwise, add to local backend;
222 **      fail if not defined in captive backend;
223 **
224 */
225
226 static int translucent_modify(Operation *op, SlapReply *rs) {
227         SlapReply nrs = { REP_RESULT };
228         Operation nop = *op;
229
230         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
231         overlay_stack *ov = on->on_bi.bi_private;
232         void *private = op->o_bd->be_private;
233         Entry ne, *e = NULL, *re = NULL;
234         Attribute *a, *ax;
235         Modifications *m, **mm;
236         int del, rc, erc = 0;
237         slap_callback cb = { 0 };
238
239         Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
240                 op->o_req_dn.bv_val, 0, 0);
241
242 /*
243 ** fetch entry from the captive backend;
244 ** if it did not exist, fail;
245 ** release it, if captive backend supports this;
246 **
247 */
248
249         op->o_bd->bd_info = (BackendInfo *) on->on_info;
250         op->o_bd->be_private = ov->private;
251         rc = ov->info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
252         op->o_bd->be_private = private;
253
254         /* if(ov->config->no_add && (!re || rc != LDAP_SUCCESS)) */
255         if(rc != LDAP_SUCCESS || re == NULL ) {
256                 send_ldap_error(op, rs, LDAP_NO_SUCH_OBJECT,
257                         "attempt to modify nonexistent local record");
258                 return(rs->sr_err);
259         }
260
261 /*
262 ** fetch entry from local backend;
263 ** if it exists:
264 **      foreach Modification:
265 **          if attr not present in local:
266 **              if Mod == LDAP_MOD_DELETE:
267 **                  if remote attr not present, return NO_SUCH;
268 **                  if remote attr present, drop this Mod;
269 **              else force this Mod to LDAP_MOD_ADD;
270 **      return CONTINUE;
271 **
272 */
273
274         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
275
276         if(e && rc == LDAP_SUCCESS) {
277                 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
278                 for(mm = &op->orm_modlist; *mm; ) {
279                         m = *mm;
280                         for(a = e->e_attrs; a; a = a->a_next)
281                                 if(a->a_desc == m->sml_desc) break;
282                         if(a) {
283                                 mm = &m->sml_next;
284                                 continue;               /* found local attr */
285                         }
286                         if(m->sml_op == LDAP_MOD_DELETE) {
287                                 for(a = re->e_attrs; a; a = a->a_next)
288                                         if(a->a_desc == m->sml_desc) break;
289                                 /* not found remote attr */
290                                 if(!a) {
291                                         erc = LDAP_NO_SUCH_ATTRIBUTE;
292                                         goto release;
293                                 }
294                                 if(ov->config->strict) {
295                                         erc = LDAP_CONSTRAINT_VIOLATION;
296                                         goto release;
297                                 }
298                                 Debug(LDAP_DEBUG_TRACE,
299                                         "=> translucent_modify: silently dropping delete: %s\n",
300                                         m->sml_desc->ad_cname.bv_val, 0, 0);
301                                 *mm = m->sml_next;
302                                 m->sml_next = NULL;
303                                 slap_mods_free(m, 1);
304                                 continue;
305                         }
306                         m->sml_op = LDAP_MOD_ADD;
307                         mm = &m->sml_next;
308                 }
309                 erc = SLAP_CB_CONTINUE;
310 release:
311                 if(re) {
312                         op->o_bd->be_private = ov->private;
313                         if(ov->info->bi_entry_release_rw)
314                                 ov->info->bi_entry_release_rw(op, re, 0);
315                         else
316                                 entry_free(re);
317                         op->o_bd->be_private = private;
318                 }
319                 be_entry_release_r(op, e);
320                 if(erc == SLAP_CB_CONTINUE) {
321                         op->o_bd->bd_info = (BackendInfo *) on;
322                         return(erc);
323                 } else if(erc) {
324                         send_ldap_error(op, rs, erc,
325                                 "attempt to delete nonexistent attribute");
326                         return(erc);
327                 }
328         }
329
330         /* don't leak remote entry copy */
331         if(re) {
332                 op->o_bd->be_private = ov->private;
333                 if(ov->info->bi_entry_release_rw)
334                         ov->info->bi_entry_release_rw(op, re, 0);
335                 else
336                         entry_free(re);
337                 op->o_bd->be_private = private;
338         }
339 /*
340 ** foreach Modification:
341 **      if MOD_ADD or MOD_REPLACE, add Attribute;
342 ** if no Modifications were suitable:
343 **      if config.strict, throw CONSTRAINT_VIOLATION;
344 **      else, return early SUCCESS;
345 ** fabricate Entry with new Attribute chain;
346 ** glue_parent() for this Entry;
347 ** call bi_op_add() in local backend;
348 **
349 */
350
351         Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
352         a = NULL;
353         for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
354                 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
355                    ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
356                         Debug(LDAP_DEBUG_ANY,
357                                 "=> translucent_modify: silently dropped modification(%d): %s\n",
358                                 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
359                         if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
360                         continue;
361                 }
362                 a = ch_calloc(1, sizeof(Attribute));
363                 a->a_desc  = m->sml_desc;
364                 a->a_vals  = m->sml_values;
365                 a->a_nvals = m->sml_nvalues ? m->sml_nvalues : a->a_vals;
366                 a->a_next  = ax;
367                 ax = a;
368         }
369
370         if(del && ov->config->strict) {
371                 free_attr_chain(a);
372                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
373                         "attempt to delete attributes from local database");
374                 return(rs->sr_err);
375         }
376
377         if(!ax) {
378                 if(ov->config->strict) {
379                         send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
380                                 "modification contained other than ADD or REPLACE");
381                         return(rs->sr_err);
382                 }
383                 op->o_bd->bd_info = (BackendInfo *) on;
384                 /* rs->sr_text = "no valid modification found"; */
385                 rs->sr_err = LDAP_SUCCESS;
386                 send_ldap_result(op, rs);
387                 return(rs->sr_err);
388         }
389
390         ne.e_id         = NOID;
391         ne.e_name       = op->o_req_dn;
392         ne.e_nname      = op->o_req_ndn;
393         ne.e_attrs      = a;
394         ne.e_ocflags    = 0;
395         ne.e_bv.bv_len  = 0;
396         ne.e_bv.bv_val  = NULL;
397         ne.e_private    = NULL;
398
399         nop.o_tag       = LDAP_REQ_ADD;
400         nop.oq_add.rs_e = &ne;
401
402         op->o_bd->bd_info = (BackendInfo *) on;
403         glue_parent(&nop);
404
405         cb.sc_response = translucent_tag_cb;
406         cb.sc_private = op->orm_modlist;
407         cb.sc_next = nop.o_callback;
408         nop.o_callback = &cb;
409         rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
410         free_attr_chain(a);
411
412         return(rc);
413 }
414
415 static int translucent_compare(Operation *op, SlapReply *rs) {
416         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
417         overlay_stack *ov = on->on_bi.bi_private;
418         void *private = op->o_bd->be_private;
419
420         AttributeAssertion *ava = op->orc_ava;
421         Entry *e;
422         int rc;
423
424         Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
425                 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
426
427 /*
428 ** if the local backend has an entry for this attribute:
429 **      CONTINUE and let it do the compare;
430 **
431 */
432
433         op->o_bd->bd_info = (BackendInfo *) on->on_info;
434         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e);
435         if(e && rc == LDAP_SUCCESS) {
436                 be_entry_release_r(op, e);
437                 op->o_bd->bd_info = (BackendInfo *) on;
438                 return(SLAP_CB_CONTINUE);
439         }
440
441 /*
442 ** call compare() in the captive backend;
443 ** return the result;
444 **
445 */
446
447         op->o_bd->be_private = ov->private;
448         rc = ov->info->bi_op_compare(op, rs);
449         op->o_bd->be_private = private;
450         op->o_bd->bd_info = (BackendInfo *) on;
451         return(rc);
452 }
453
454 /*
455 ** translucent_search_cb()
456 **      merge local data with the search result
457 **
458 */
459
460 static int translucent_search_cb(Operation *op, SlapReply *rs) {
461         slap_overinst *on;
462         Entry *e, *re = NULL;
463         Attribute *a, *ax, *an, *as = NULL;
464         void *private;
465         int rc;
466
467         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
468                 return(SLAP_CB_CONTINUE);
469
470         Debug(LDAP_DEBUG_TRACE, "==> tranclucent_search_cb: %s\n",
471                 rs->sr_entry->e_name.bv_val, 0, 0);
472
473         on = (slap_overinst *) op->o_bd->bd_info;
474         op->o_bd->bd_info = (BackendInfo *) on->on_info;
475
476         private = op->o_bd->be_private;
477         op->o_bd->be_private = op->o_callback->sc_private;
478
479         rc = be_entry_get_rw(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
480
481 /*
482 ** if we got an entry from local backend:
483 **      make a copy of this search result;
484 **      foreach local attr:
485 **              foreach search result attr:
486 **                      if match, result attr with local attr;
487 **                      if new local, add to list;
488 **      append new local attrs to search result;
489 **
490 */
491
492         if(e && rc == LDAP_SUCCESS) {
493                 re = entry_dup(rs->sr_entry);
494                 for(ax = e->e_attrs; ax; ax = ax->a_next) {
495 #if 0
496                         if(is_at_operational(ax->a_desc->ad_type)) continue;
497 #endif
498                         for(a = re->e_attrs; a; a = a->a_next) {
499                                 if(a->a_desc == ax->a_desc) {
500                                         if(a->a_vals != a->a_nvals)
501                                                 ber_bvarray_free(a->a_nvals);
502                                         ber_bvarray_free(a->a_vals);
503                                         a->a_vals = dup_bervarray(ax->a_vals);
504                                         a->a_nvals = (ax->a_vals == ax->a_nvals) ?
505                                                 a->a_vals : dup_bervarray(ax->a_nvals);
506                                         break;
507                                 }
508                         }
509                         if(a) continue;
510                         an = attr_dup(ax);
511                         an->a_next = as;
512                         as = an;
513                 }
514                 be_entry_release_r(op, e);
515
516                 /* literally append, so locals are always last */
517                 if(as) {
518                         if(re->e_attrs) {
519                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
520                                 ax->a_next = as;
521                         } else {
522                                 re->e_attrs = as;
523                         }
524                 }
525                 rs->sr_entry = re;
526                 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
527         }
528
529         op->o_bd->be_private = private;
530         op->o_bd->bd_info = (BackendInfo *) on;
531
532         return(SLAP_CB_CONTINUE);
533 }
534
535 /*
536 ** translucent_search()
537 **      search via captive backend;
538 **      override results with any local data;
539 **
540 */
541
542 static int translucent_search(Operation *op, SlapReply *rs) {
543         Operation nop = *op;
544
545         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
546         slap_callback cb = { NULL, NULL, NULL, NULL };
547         overlay_stack *ov = on->on_bi.bi_private;
548         void *private = op->o_bd->be_private;
549         int rc;
550
551         Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
552                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
553         cb.sc_response = (slap_response *) translucent_search_cb;
554         cb.sc_private = private;
555
556         cb.sc_next = nop.o_callback;
557         nop.o_callback = &cb;
558
559         op->o_bd->be_private = ov->private;
560         rc = ov->info->bi_op_search(&nop, rs);
561         op->o_bd->be_private = private;
562
563         return(rs->sr_err);
564 }
565
566
567 /*
568 ** translucent_bind()
569 **      pass bind request to captive backend;
570 **
571 */
572
573 static int translucent_bind(Operation *op, SlapReply *rs) {
574         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
575         overlay_stack *ov = on->on_bi.bi_private;
576         void *private = op->o_bd->be_private;
577         int rc = 0;
578
579         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
580                 op->o_req_dn.bv_val, op->orb_method, 0);
581
582         op->o_bd->be_private = ov->private;
583         rc = ov->info->bi_op_bind(op, rs);
584         op->o_bd->be_private = private;
585
586         return(rc);
587 }
588
589 /*
590 ** translucent_connection_destroy()
591 **      pass disconnect notification to captive backend;
592 **
593 */
594
595 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
596         slap_overinst *on = (slap_overinst *) be->bd_info;
597         overlay_stack *ov = on->on_bi.bi_private;
598         void *private = be->be_private;
599         int rc = 0;
600
601         Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
602
603         be->be_private = ov->private;
604         rc = ov->info->bi_connection_destroy(be, conn);
605         be->be_private = private;
606
607         return(rc);
608 }
609
610 /*
611 ** translucent_db_config()
612 **      pass config directives to captive backend;
613 **      parse unrecognized directives ourselves;
614 **
615 */
616
617 static int translucent_db_config(
618         BackendDB       *be,
619         const char      *fname,
620         int             lineno,
621         int             argc,
622         char            **argv
623 )
624 {
625         slap_overinst *on = (slap_overinst *) be->bd_info;
626         overlay_stack *ov = on->on_bi.bi_private;
627         void *private = be->be_private;
628         void *be_cf_ocs = be->be_cf_ocs;
629         int rc;
630
631         /* "this should never happen" */
632         if(!ov->info) {
633                 fprintf(stderr, "fatal: captive backend not initialized");
634                 return(1);
635         }
636
637         be->be_private = ov->private;
638         be->be_cf_ocs = ov->info->bi_cf_ocs;
639         rc = ov->info->bi_db_config ? ov->info->bi_db_config(be, fname, lineno, argc, argv) : 0;
640         be->be_private = private;
641         be->be_cf_ocs = be_cf_ocs;
642
643         /* pass okay or error up, SLAP_CONF_UNKNOWN might be ours */
644         if(rc == 0 || rc == 1) return(rc);
645
646         rc = 0;
647         if(!strcasecmp(*argv, "translucent_strict")) {
648                 ov->config->strict++;
649         } else if(!strcasecmp(*argv, "translucent_no_add")) {
650                 ov->config->no_add++;
651         } else if(!strcasecmp(*argv, "translucent_no_glue")) {
652                 ov->config->glue++;
653         } else if(!strcasecmp(*argv, "translucent_debug")) {
654                 if(argc == 1) {
655                         ov->config->debug = 0xFFFF;
656                         rc = 0;
657                 } else if(argc == 2) {
658                         if ( lutil_atoi( &ov->config->debug, argv[1]) != 0 ) {
659                                 fprintf(stderr, "%s: line %d: unable to parse debug \"%s\"\n",
660                                         fname, lineno, argv[1]);
661                                 return 1;
662                         }
663                         rc = 0;
664                 } else {
665                         fprintf(stderr, "%s: line %d: too many arguments (%d) to debug\n",
666                                 fname, lineno, argc);
667                         rc = 1;
668                 }
669         } else {
670                 fprintf(stderr, "%s: line %d: unknown keyword %s\n",
671                         fname, lineno, *argv);
672                 rc = SLAP_CONF_UNKNOWN;
673         }
674         return(rc);
675 }
676
677 /*
678 ** translucent_db_init()
679 **      initialize the captive backend;
680 **
681 */
682
683 static int translucent_db_init(BackendDB *be) {
684         slap_overinst *on = (slap_overinst *) be->bd_info;
685         void *private = be->be_private;
686         overlay_stack *ov;
687         int rc;
688
689         Debug(LDAP_DEBUG_TRACE, "==> translucent_init\n", 0, 0, 0);
690
691         ov = ch_calloc(1, sizeof(overlay_stack));
692         ov->config = ch_calloc(1, sizeof(translucent_configuration));
693         ov->info = backend_info("ldap");
694
695         if(!ov->info) {
696                 Debug(LDAP_DEBUG_ANY, "translucent: backend_info failed!\n", 0, 0, 0);
697                 return(1);
698         }
699
700         /* forcibly disable schema checking on the local backend */
701         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
702
703         be->be_private = NULL;
704         rc = ov->info->bi_db_init ? ov->info->bi_db_init(be) : 0;
705
706         if(rc) Debug(LDAP_DEBUG_TRACE,
707                 "translucent: bi_db_init() returned error %d\n", rc, 0, 0);
708
709         ov->private = be->be_private;
710         be->be_private = private;
711         on->on_bi.bi_private = ov;
712         return(rc);
713 }
714
715 /*
716 ** translucent_db_open()
717 **      if the captive backend has an open() method, call it;
718 **
719 */
720
721 static int translucent_db_open(BackendDB *be) {
722         slap_overinst *on = (slap_overinst *) be->bd_info;
723         overlay_stack *ov = on->on_bi.bi_private;
724         void *private = be->be_private;
725         int rc;
726
727         /* "should never happen" */
728         if(!ov->info) {
729                 Debug(LDAP_DEBUG_ANY, "translucent_open() called with bad ov->info\n", 0, 0, 0);
730                 return(LDAP_OTHER);
731         }
732
733         Debug(LDAP_DEBUG_TRACE, "translucent_open\n", 0, 0, 0);
734
735         be->be_private = ov->private;
736         rc = ov->info->bi_db_open ? ov->info->bi_db_open(be) : 0;
737         be->be_private = private;
738
739         if(rc) Debug(LDAP_DEBUG_TRACE,
740                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
741
742         return(rc);
743 }
744
745 /*
746 ** translucent_db_close()
747 **      if the captive backend has a close() method, call it;
748 **      free any config data;
749 **
750 */
751
752 static int translucent_db_close(BackendDB *be) {
753         slap_overinst *on = (slap_overinst *) be->bd_info;
754         overlay_stack *ov = on->on_bi.bi_private;
755         int rc = 0;
756
757         if ( ov ) {
758                 void *private = be->be_private;
759
760                 be->be_private = ov->private;
761                 rc = (ov->info && ov->info->bi_db_close) ? ov->info->bi_db_close(be) : 0;
762                 be->be_private = private;
763                 if(ov->config) ch_free(ov->config);
764                 ov->config = NULL;
765         }
766
767         return(rc);
768 }
769
770 /*
771 ** translucent_db_destroy()
772 **      if the captive backend has a db_destroy() method, call it
773 **
774 */
775
776 static int translucent_db_destroy(BackendDB *be) {
777         slap_overinst *on = (slap_overinst *) be->bd_info;
778         overlay_stack *ov = on->on_bi.bi_private;
779         int rc = 0;
780
781         if ( ov ) {
782                 void *private = be->be_private;
783
784                 be->be_private = ov->private;
785                 rc = (ov->info && ov->info->bi_db_destroy) ? ov->info->bi_db_destroy(be) : 0;
786                 be->be_private = private;
787                 ch_free(ov);
788                 on->on_bi.bi_private = NULL;
789         }
790
791         return(rc);
792 }
793
794 /*
795 ** translucent_initialize()
796 **      initialize the slap_overinst with our entry points;
797 **
798 */
799
800 int translucent_initialize() {
801
802         translucent.on_bi.bi_type       = "translucent";
803         translucent.on_bi.bi_db_init    = translucent_db_init;
804         translucent.on_bi.bi_db_config  = translucent_db_config;
805         translucent.on_bi.bi_db_open    = translucent_db_open;
806         translucent.on_bi.bi_db_close   = translucent_db_close;
807         translucent.on_bi.bi_db_destroy = translucent_db_destroy;
808         translucent.on_bi.bi_op_bind    = translucent_bind;
809         translucent.on_bi.bi_op_add     = translucent_add;
810         translucent.on_bi.bi_op_modify  = translucent_modify;
811         translucent.on_bi.bi_op_modrdn  = translucent_modrdn;
812         translucent.on_bi.bi_op_delete  = translucent_delete;
813         translucent.on_bi.bi_op_search  = translucent_search;
814         translucent.on_bi.bi_op_compare = translucent_compare;
815         translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
816
817         return(overlay_register(&translucent));
818 }
819
820 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
821 int init_module(int argc, char *argv[]) {
822         return translucent_initialize();
823 }
824 #endif
825
826 #endif /* SLAPD_OVER_TRANSLUCENT */
827