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