1 /* rwm.c - rewrite/remap operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
24 #include <ac/string.h>
30 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie )
32 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
33 struct ldaprwmap *rwmap =
34 (struct ldaprwmap *)on->on_bi.bi_private;
36 struct berval dn = BER_BVNULL,
42 * Rewrite the dn if needed
48 dc.ctx = (char *)cookie;
49 #else /* ! ENABLE_REWRITE */
50 dc.tofrom = ((int *)cookie)[0];
52 #endif /* ! ENABLE_REWRITE */
54 /* NOTE: in those cases where only the ndn is available,
55 * and the caller sets op->o_req_dn = op->o_req_ndn,
56 * only rewrite the op->o_req_ndn and use it as
57 * op->o_req_dn as well */
59 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
61 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
63 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
66 if ( rc != LDAP_SUCCESS ) {
70 if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
71 || ndn.bv_val == op->o_req_ndn.bv_val )
76 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
77 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
82 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
89 rwm_op_add( Operation *op, SlapReply *rs )
91 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
92 struct ldaprwmap *rwmap =
93 (struct ldaprwmap *)on->on_bi.bi_private;
97 Attribute **ap = NULL;
98 char *olddn = op->o_req_dn.bv_val;
101 #ifdef ENABLE_REWRITE
102 rc = rwm_op_dn_massage( op, rs, "addDN" );
103 #else /* ! ENABLE_REWRITE */
105 rc = rwm_op_dn_massage( op, rs, &rc );
106 #endif /* ! ENABLE_REWRITE */
107 if ( rc != LDAP_SUCCESS ) {
108 op->o_bd->bd_info = (BackendInfo *)on->on_info;
109 send_ldap_error( op, rs, rc, "addDN massage error" );
113 if ( olddn != op->o_req_dn.bv_val ) {
114 ch_free( op->ora_e->e_name.bv_val );
115 ch_free( op->ora_e->e_nname.bv_val );
117 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
118 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
121 /* Count number of attributes in entry */
122 isupdate = be_shadow_update( op );
123 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
126 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
127 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
131 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[ last ] ); last++ )
134 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
135 struct ldapmapping *mapping = NULL;
137 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
139 if ( mapping == NULL ) {
140 if ( rwmap->rwm_at.drop_missing ) {
141 /* FIXME: we allow to remove objectClasses as well;
142 * if the resulting entry is inconsistent, that's
143 * the relayed database's business...
145 ch_free( (*ap)->a_vals[ j ].bv_val );
147 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
149 BER_BVZERO( &(*ap)->a_vals[ last ] );
155 ch_free( (*ap)->a_vals[ j ].bv_val );
156 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
160 } else if ( !isupdate && (*ap)->a_desc->ad_type->sat_no_user_mod ) {
164 struct ldapmapping *mapping = NULL;
166 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
168 if ( mapping == NULL ) {
169 if ( rwmap->rwm_at.drop_missing ) {
174 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
175 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
178 * FIXME: rewrite could fail; in this case
179 * the operation should give up, right?
181 #ifdef ENABLE_REWRITE
182 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
184 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
185 #else /* ! ENABLE_REWRITE */
187 rc = rwm_dnattr_rewrite( op, rs, &rc, (*ap)->a_vals,
188 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
189 #endif /* ! ENABLE_REWRITE */
194 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
195 #ifdef ENABLE_REWRITE
196 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
198 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
199 #else /* ! ENABLE_REWRITE */
201 rc = rwm_referral_rewrite( op, rs, &rc, (*ap)->a_vals,
202 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
203 #endif /* ! ENABLE_REWRITE */
204 if ( rc != LDAP_SUCCESS ) {
209 if ( mapping != NULL ) {
210 assert( mapping->m_dst_ad != NULL );
211 (*ap)->a_desc = mapping->m_dst_ad;
220 /* FIXME: leaking attribute/values? */
227 /* TODO: map attribute types, values of DN-valued attributes ... */
228 return SLAP_CB_CONTINUE;
231 #ifdef ENABLE_REWRITE
233 rwm_conn_init( BackendDB *be, Connection *conn )
235 slap_overinst *on = (slap_overinst *) be->bd_info;
236 struct ldaprwmap *rwmap =
237 (struct ldaprwmap *)on->on_bi.bi_private;
239 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
241 return SLAP_CB_CONTINUE;
245 rwm_conn_destroy( BackendDB *be, Connection *conn )
247 slap_overinst *on = (slap_overinst *) be->bd_info;
248 struct ldaprwmap *rwmap =
249 (struct ldaprwmap *)on->on_bi.bi_private;
251 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
253 return SLAP_CB_CONTINUE;
255 #endif /* ENABLE_REWRITE */
258 rwm_op_bind( Operation *op, SlapReply *rs )
260 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
263 #ifdef ENABLE_REWRITE
264 rc = rwm_op_dn_massage( op, rs, "bindDN" );
265 #else /* ! ENABLE_REWRITE */
267 rc = rwm_op_dn_massage( op, rs, &rc );
268 #endif /* ! ENABLE_REWRITE */
269 if ( rc != LDAP_SUCCESS ) {
270 op->o_bd->bd_info = (BackendInfo *)on->on_info;
271 send_ldap_error( op, rs, rc, "bindDN massage error" );
275 return SLAP_CB_CONTINUE;
279 rwm_op_unbind( Operation *op, SlapReply *rs )
281 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
282 struct ldaprwmap *rwmap =
283 (struct ldaprwmap *)on->on_bi.bi_private;
285 #ifdef ENABLE_REWRITE
286 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
287 #endif /* ENABLE_REWRITE */
289 return SLAP_CB_CONTINUE;
293 rwm_op_compare( Operation *op, SlapReply *rs )
295 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
296 struct ldaprwmap *rwmap =
297 (struct ldaprwmap *)on->on_bi.bi_private;
300 struct berval mapped_at = BER_BVNULL,
301 mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
303 #ifdef ENABLE_REWRITE
304 rc = rwm_op_dn_massage( op, rs, "compareDN" );
305 #else /* ! ENABLE_REWRITE */
307 rc = rwm_op_dn_massage( op, rs, &rc );
308 #endif /* ! ENABLE_REWRITE */
309 if ( rc != LDAP_SUCCESS ) {
310 op->o_bd->bd_info = (BackendInfo *)on->on_info;
311 send_ldap_error( op, rs, rc, "compareDN massage error" );
315 /* if the attribute is an objectClass, try to remap its value */
316 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
317 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
319 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
320 &mapped_vals[0], RWM_MAP );
321 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
323 op->o_bd->bd_info = (BackendInfo *)on->on_info;
324 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
327 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
328 free( op->orc_ava->aa_value.bv_val );
329 op->orc_ava->aa_value = mapped_vals[0];
331 mapped_at = op->orc_ava->aa_desc->ad_cname;
334 struct ldapmapping *mapping = NULL;
335 AttributeDescription *ad = op->orc_ava->aa_desc;
337 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
339 if ( mapping == NULL ) {
340 if ( rwmap->rwm_at.drop_missing ) {
341 op->o_bd->bd_info = (BackendInfo *)on->on_info;
342 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
347 assert( mapping->m_dst_ad != NULL );
348 ad = mapping->m_dst_ad;
351 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
352 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
354 struct berval *mapped_valsp[2];
356 mapped_valsp[0] = &mapped_vals[0];
357 mapped_valsp[1] = &mapped_vals[1];
359 mapped_vals[0] = op->orc_ava->aa_value;
361 #ifdef ENABLE_REWRITE
362 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
363 #else /* ! ENABLE_REWRITE */
365 rc = rwm_dnattr_rewrite( op, rs, &rc, NULL, mapped_valsp );
366 #endif /* ! ENABLE_REWRITE */
368 if ( rc != LDAP_SUCCESS ) {
369 op->o_bd->bd_info = (BackendInfo *)on->on_info;
370 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
374 op->orc_ava->aa_value = mapped_vals[0];
376 op->orc_ava->aa_desc = ad;
379 return SLAP_CB_CONTINUE;
383 rwm_op_delete( Operation *op, SlapReply *rs )
385 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
388 #ifdef ENABLE_REWRITE
389 rc = rwm_op_dn_massage( op, rs, "deleteDN" );
390 #else /* ! ENABLE_REWRITE */
392 rc = rwm_op_dn_massage( op, rs, &rc );
393 #endif /* ! ENABLE_REWRITE */
394 if ( rc != LDAP_SUCCESS ) {
395 op->o_bd->bd_info = (BackendInfo *)on->on_info;
396 send_ldap_error( op, rs, rc, "deleteDN massage error" );
400 return SLAP_CB_CONTINUE;
404 rwm_op_modify( Operation *op, SlapReply *rs )
406 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
407 struct ldaprwmap *rwmap =
408 (struct ldaprwmap *)on->on_bi.bi_private;
414 #ifdef ENABLE_REWRITE
415 rc = rwm_op_dn_massage( op, rs, "modifyDN" );
416 #else /* ! ENABLE_REWRITE */
418 rc = rwm_op_dn_massage( op, rs, &rc );
419 #endif /* ! ENABLE_REWRITE */
420 if ( rc != LDAP_SUCCESS ) {
421 op->o_bd->bd_info = (BackendInfo *)on->on_info;
422 send_ldap_error( op, rs, rc, "modifyDN massage error" );
426 isupdate = be_shadow_update( op );
427 for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
430 struct ldapmapping *mapping = NULL;
432 if ( (*mlp)->sml_desc == slap_schema.si_ad_objectClass
433 || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass )
437 } else if ( !isupdate && (*mlp)->sml_desc->ad_type->sat_no_user_mod ) {
443 drop_missing = rwm_mapping( &rwmap->rwm_at,
444 &(*mlp)->sml_desc->ad_cname,
446 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
452 if ( (*mlp)->sml_values != NULL ) {
456 for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[ last ] ); last++ )
460 for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
461 struct ldapmapping *oc_mapping = NULL;
463 ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
464 &oc_mapping, RWM_MAP );
465 if ( oc_mapping == NULL ) {
466 if ( rwmap->rwm_at.drop_missing ) {
467 /* FIXME: we allow to remove objectClasses as well;
468 * if the resulting entry is inconsistent, that's
469 * the relayed database's business...
471 ch_free( (*mlp)->sml_values[ j ].bv_val );
473 (*mlp)->sml_values[ j ] = (*mlp)->sml_values[ last ];
475 BER_BVZERO( &(*mlp)->sml_values[ last ] );
481 ch_free( (*mlp)->sml_values[ j ].bv_val );
482 ber_dupbv( &(*mlp)->sml_values[ j ], &oc_mapping->m_dst );
487 if ( (*mlp)->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
488 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
490 #ifdef ENABLE_REWRITE
491 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
493 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
494 #else /* ! ENABLE_REWRITE */
496 rc = rwm_dnattr_rewrite( op, rs, &rc,
498 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
499 #endif /* ! ENABLE_REWRITE */
501 } else if ( (*mlp)->sml_desc == slap_schema.si_ad_ref ) {
502 #ifdef ENABLE_REWRITE
503 rc = rwm_referral_rewrite( op, rs,
506 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
507 #else /* ! ENABLE_REWRITE */
509 rc = rwm_referral_rewrite( op, rs, &rc,
511 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
512 #endif /* ! ENABLE_REWRITE */
513 if ( rc != LDAP_SUCCESS ) {
518 if ( rc != LDAP_SUCCESS ) {
525 if ( mapping != NULL ) {
526 /* use new attribute description */
527 assert( mapping->m_dst_ad != NULL );
528 (*mlp)->sml_desc = mapping->m_dst_ad;
531 mlp = &(*mlp)->sml_next;
536 *mlp = (*mlp)->sml_next;
537 slap_mod_free( &ml->sml_mod, 0 );
541 return SLAP_CB_CONTINUE;
545 rwm_op_modrdn( Operation *op, SlapReply *rs )
547 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
548 struct ldaprwmap *rwmap =
549 (struct ldaprwmap *)on->on_bi.bi_private;
553 if ( op->orr_newSup ) {
555 struct berval nnewSup = BER_BVNULL;
556 struct berval newSup = BER_BVNULL;
559 * Rewrite the new superior, if defined and required
562 #ifdef ENABLE_REWRITE
563 dc.conn = op->o_conn;
565 dc.ctx = "newSuperiorDN";
566 #else /* ! ENABLE_REWRITE */
569 #endif /* ! ENABLE_REWRITE */
570 newSup = *op->orr_newSup;
571 nnewSup = *op->orr_nnewSup;
572 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
573 if ( rc != LDAP_SUCCESS ) {
574 op->o_bd->bd_info = (BackendInfo *)on->on_info;
575 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
579 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
580 op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
581 op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
582 *op->orr_newSup = newSup;
583 *op->orr_nnewSup = nnewSup;
588 * Rewrite the dn, if needed
590 #ifdef ENABLE_REWRITE
591 rc = rwm_op_dn_massage( op, rs, "renameDN" );
592 #else /* ! ENABLE_REWRITE */
594 rc = rwm_op_dn_massage( op, rs, &rc );
595 #endif /* ! ENABLE_REWRITE */
596 if ( rc != LDAP_SUCCESS ) {
597 op->o_bd->bd_info = (BackendInfo *)on->on_info;
598 send_ldap_error( op, rs, rc, "renameDN massage error" );
602 /* TODO: rewrite newRDN, attribute types,
603 * values of DN-valued attributes ... */
604 return SLAP_CB_CONTINUE;
607 static slap_callback rwm_cb;
614 ber_memfree_x( data, NULL );
617 static slap_callback *
618 rwm_callback_get( Operation *op )
622 if ( op->o_threadctx == NULL ) {
626 ldap_pvt_thread_pool_getkey( op->o_threadctx,
627 rwm_keyfree, &data, NULL );
628 if ( data == NULL ) {
629 data = ch_calloc( sizeof( slap_callback ), 1 );
630 ldap_pvt_thread_pool_setkey( op->o_threadctx,
631 rwm_keyfree, data, rwm_keyfree );
634 return (slap_callback *)data;
638 rwm_swap_attrs( Operation *op, SlapReply *rs )
640 slap_callback *cb = op->o_callback;
641 AttributeName *an = (AttributeName *)cb->sc_private;
645 return SLAP_CB_CONTINUE;
649 rwm_op_search( Operation *op, SlapReply *rs )
651 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
652 struct ldaprwmap *rwmap =
653 (struct ldaprwmap *)on->on_bi.bi_private;
658 struct berval fstr = BER_BVNULL;
661 slap_callback *cb = NULL;
662 AttributeName *an = NULL;
666 #ifdef ENABLE_REWRITE
667 rc = rwm_op_dn_massage( op, rs, "searchDN" );
668 #else /* ! ENABLE_REWRITE */
670 rc = rwm_op_dn_massage( op, rs, &rc );
671 #endif /* ! ENABLE_REWRITE */
672 if ( rc != LDAP_SUCCESS ) {
673 text = "searchDN massage error";
678 * Rewrite the dn if needed
681 #ifdef ENABLE_REWRITE
682 dc.conn = op->o_conn;
684 dc.ctx = "searchFilterAttrDN";
685 #else /* ! ENABLE_REWRITE */
688 #endif /* ! ENABLE_REWRITE */
690 rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
691 if ( rc != LDAP_SUCCESS ) {
692 text = "searchFilter/searchFilterAttrDN massage error";
696 f = str2filter_x( op, fstr.bv_val );
699 text = "massaged filter parse error";
703 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
704 ch_free( op->ors_filterstr.bv_val );
707 if( op->ors_filter ) {
708 filter_free_x( op, op->ors_filter );
712 op->ors_filterstr = fstr;
714 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
715 op->ors_attrs, &an, RWM_MAP );
716 if ( rc != LDAP_SUCCESS ) {
717 text = "attribute list mapping error";
721 cb = rwm_callback_get( op );
723 cb->sc_response = rwm_swap_attrs;
724 cb->sc_cleanup = NULL;
725 cb->sc_private = (void *)op->ors_attrs;
726 cb->sc_next = op->o_callback;
731 return SLAP_CB_CONTINUE;
739 filter_free_x( op, f );
742 if ( !BER_BVISNULL( &fstr ) ) {
743 ch_free( fstr.bv_val );
746 op->o_bd->bd_info = (BackendInfo *)on->on_info;
747 send_ldap_error( op, rs, rc, text );
754 rwm_extended( Operation *op, SlapReply *rs )
756 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
759 #ifdef ENABLE_REWRITE
760 rc = rwm_op_dn_massage( op, rs, "extendedDN" );
761 #else /* ! ENABLE_REWRITE */
763 rc = rwm_op_dn_massage( op, rs, &rc );
764 #endif /* ! ENABLE_REWRITE */
765 if ( rc != LDAP_SUCCESS ) {
766 op->o_bd->bd_info = (BackendInfo *)on->on_info;
767 send_ldap_error( op, rs, rc, "extendedDN massage error" );
771 /* TODO: rewrite/map extended data ? ... */
772 return SLAP_CB_CONTINUE;
776 rwm_matched( Operation *op, SlapReply *rs )
778 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
779 struct ldaprwmap *rwmap =
780 (struct ldaprwmap *)on->on_bi.bi_private;
782 struct berval dn, mdn;
786 if ( rs->sr_matched == NULL ) {
787 return SLAP_CB_CONTINUE;
791 #ifdef ENABLE_REWRITE
792 dc.conn = op->o_conn;
794 dc.ctx = "matchedDN";
795 #else /* ! ENABLE_REWRITE */
798 #endif /* ! ENABLE_REWRITE */
799 ber_str2bv( rs->sr_matched, 0, 0, &dn );
801 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
802 if ( rc != LDAP_SUCCESS ) {
804 rs->sr_text = "Rewrite error";
808 if ( mdn.bv_val != dn.bv_val ) {
809 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
810 ch_free( (void *)rs->sr_matched );
813 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
815 rs->sr_matched = mdn.bv_val;
818 return SLAP_CB_CONTINUE;
822 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
824 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
825 struct ldaprwmap *rwmap =
826 (struct ldaprwmap *)on->on_bi.bi_private;
834 * Rewrite the dn attrs, if needed
837 #ifdef ENABLE_REWRITE
838 dc.conn = op->o_conn;
840 #else /* ! ENABLE_REWRITE */
843 #endif /* ! ENABLE_REWRITE */
845 /* FIXME: the entries are in the remote mapping form;
846 * so we need to select those attributes we are willing
847 * to return, and remap them accordingly */
849 /* FIXME: in principle, one could map an attribute
850 * on top of another, which already exists.
851 * As such, in the end there might exist more than
852 * one instance of an attribute.
853 * We should at least check if this occurs, and issue
854 * an error (because multiple instances of attrs in
855 * response are not valid), or merge the values (what
856 * about duplicate values?) */
857 isupdate = be_shadow_update( op );
858 for ( ap = a_first; *ap; ) {
859 struct ldapmapping *mapping = NULL;
864 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
869 if ( op->ors_attrs != NULL &&
870 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
871 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
876 drop_missing = rwm_mapping( &rwmap->rwm_at,
877 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
878 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
883 if ( mapping != NULL ) {
884 (*ap)->a_desc = mapping->m_dst_ad;
888 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
889 if ( stripEntryDN ) {
890 /* will be generated by frontend */
894 } else if ( !isupdate
895 && (*ap)->a_desc->ad_type->sat_no_user_mod
896 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
901 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
905 /* empty? leave it in place because of attrsonly and vlv */
910 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
911 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
915 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
916 struct berval mapped;
918 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
919 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
920 ch_free( bv[0].bv_val );
921 BER_BVZERO( &bv[0] );
922 if ( &(*ap)->a_vals[last] > &bv[0] ) {
923 bv[0] = (*ap)->a_vals[last];
924 BER_BVZERO( &(*ap)->a_vals[last] );
929 } else if ( mapped.bv_val != bv[0].bv_val ) {
931 * FIXME: after LBER_FREEing
932 * the value is replaced by
935 ch_free( bv[0].bv_val );
936 ber_dupbv( &bv[0], &mapped );
941 * It is necessary to try to rewrite attributes with
942 * dn syntax because they might be used in ACLs as
943 * members of groups; since ACLs are applied to the
944 * rewritten stuff, no dn-based subject clause could
945 * be used at the ldap backend side (see
946 * http://www.OpenLDAP.org/faq/data/cache/452.html)
947 * The problem can be overcome by moving the dn-based
948 * ACLs to the target directory server, and letting
949 * everything pass thru the ldap backend. */
950 /* FIXME: handle distinguishedName-like syntaxes, like
951 * nameAndOptionalUID */
952 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
953 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
955 #ifdef ENABLE_REWRITE
956 dc.ctx = "searchAttrDN";
957 #endif /* ENABLE_REWRITE */
958 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
959 if ( rc != LDAP_SUCCESS ) {
963 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
964 #ifdef ENABLE_REWRITE
965 dc.ctx = "searchAttrDN";
966 #endif /* ENABLE_REWRITE */
967 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
968 if ( rc != LDAP_SUCCESS ) {
973 if ( mapping != NULL ) {
974 /* rewrite the attribute description */
975 assert( mapping->m_dst_ad != NULL );
976 (*ap)->a_desc = mapping->m_dst_ad;
994 rwm_send_entry( Operation *op, SlapReply *rs )
996 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
997 struct ldaprwmap *rwmap =
998 (struct ldaprwmap *)on->on_bi.bi_private;
1002 struct berval dn = BER_BVNULL,
1007 assert( rs->sr_entry != NULL );
1010 * Rewrite the dn of the result, if needed
1013 #ifdef ENABLE_REWRITE
1014 dc.conn = op->o_conn;
1016 dc.ctx = "searchEntryDN";
1017 #else /* ! ENABLE_REWRITE */
1020 #endif /* ! ENABLE_REWRITE */
1023 flags = rs->sr_flags;
1024 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1025 /* FIXME: all we need to duplicate are:
1028 * - attributes that are requested
1029 * - no values if attrsonly is set
1034 rc = LDAP_NO_MEMORY;
1038 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1042 * Note: this may fail if the target host(s) schema differs
1043 * from the one known to the meta, and a DN with unknown
1044 * attributes is returned.
1048 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1049 if ( rc != LDAP_SUCCESS ) {
1054 if ( e->e_name.bv_val != dn.bv_val ) {
1055 ch_free( e->e_name.bv_val );
1056 ch_free( e->e_nname.bv_val );
1062 /* TODO: map entry attribute types, objectclasses
1063 * and dn-valued attribute values */
1065 /* FIXME: the entries are in the remote mapping form;
1066 * so we need to select those attributes we are willing
1067 * to return, and remap them accordingly */
1068 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1071 if ( rs->sr_operational_attrs ) {
1072 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1077 rs->sr_flags = flags;
1079 return SLAP_CB_CONTINUE;
1082 if ( e != NULL && e != rs->sr_entry ) {
1083 if ( e->e_name.bv_val == dn.bv_val ) {
1084 BER_BVZERO( &e->e_name );
1087 if ( e->e_nname.bv_val == ndn.bv_val ) {
1088 BER_BVZERO( &e->e_nname );
1094 if ( !BER_BVISNULL( &dn ) ) {
1095 ch_free( dn.bv_val );
1098 if ( !BER_BVISNULL( &ndn ) ) {
1099 ch_free( ndn.bv_val );
1106 rwm_operational( Operation *op, SlapReply *rs )
1108 /* FIXME: the entries are in the remote mapping form;
1109 * so we need to select those attributes we are willing
1110 * to return, and remap them accordingly */
1111 if ( rs->sr_operational_attrs ) {
1112 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1115 return SLAP_CB_CONTINUE;
1119 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1120 * rewritten for subsequent operations; fine for plain suffixmassage,
1121 * but destroys everything else */
1123 rwm_chk_referrals( Operation *op, SlapReply *rs )
1125 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1128 #ifdef ENABLE_REWRITE
1129 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1130 #else /* ! ENABLE_REWRITE */
1132 rc = rwm_op_dn_massage( op, rs, &rc );
1133 #endif /* ! ENABLE_REWRITE */
1134 if ( rc != LDAP_SUCCESS ) {
1135 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1136 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1140 return SLAP_CB_CONTINUE;
1153 #ifdef ENABLE_REWRITE
1154 slap_overinst *on = (slap_overinst *) be->bd_info;
1155 struct ldaprwmap *rwmap =
1156 (struct ldaprwmap *)on->on_bi.bi_private;
1158 return rewrite_parse( rwmap->rwm_rw,
1159 fname, lineno, argc, argv );
1161 #else /* !ENABLE_REWRITE */
1162 fprintf( stderr, "%s: line %d: rewrite capabilities "
1163 "are not enabled\n", fname, lineno );
1164 #endif /* !ENABLE_REWRITE */
1170 rwm_suffixmassage_config(
1178 slap_overinst *on = (slap_overinst *) be->bd_info;
1179 struct ldaprwmap *rwmap =
1180 (struct ldaprwmap *)on->on_bi.bi_private;
1182 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1184 #ifdef ENABLE_REWRITE
1186 #endif /* ENABLE_REWRITE */
1191 * suffixmassage [<suffix>] <massaged suffix>
1193 * the [<suffix>] field must be defined as a valid suffix
1194 * for the current database;
1195 * the <massaged suffix> shouldn't have already been
1196 * defined as a valid suffix for the current server
1199 if ( be->be_suffix == NULL ) {
1200 fprintf( stderr, "%s: line %d: "
1201 " \"suffixMassage [<suffix>]"
1202 " <massaged suffix>\" without "
1203 "<suffix> part requires database "
1204 "suffix be defined first.\n",
1208 bvnc = be->be_suffix[ 0 ];
1211 } else if ( argc == 3 ) {
1212 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1216 fprintf( stderr, "%s: line %d: syntax is"
1217 " \"suffixMassage [<suffix>]"
1218 " <massaged suffix>\"\n",
1223 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1224 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1225 fname, lineno, bvnc.bv_val );
1229 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1230 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1231 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1232 fname, lineno, brnc.bv_val );
1233 free( nvnc.bv_val );
1234 free( pvnc.bv_val );
1238 #ifdef ENABLE_REWRITE
1240 * The suffix massaging is emulated
1241 * by means of the rewrite capabilities
1243 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1244 &pvnc, &nvnc, &prnc, &nrnc );
1245 free( nvnc.bv_val );
1246 free( pvnc.bv_val );
1247 free( nrnc.bv_val );
1248 free( prnc.bv_val );
1252 #else /* !ENABLE_REWRITE */
1253 ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1254 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1256 ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1257 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1258 #endif /* !ENABLE_REWRITE */
1272 slap_overinst *on = (slap_overinst *) be->bd_info;
1273 struct ldaprwmap *rwmap =
1274 (struct ldaprwmap *)on->on_bi.bi_private;
1276 /* objectclass/attribute mapping */
1277 return rwm_map_config( &rwmap->rwm_oc,
1279 fname, lineno, argc, argv );
1283 rwm_response( Operation *op, SlapReply *rs )
1285 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1286 struct ldaprwmap *rwmap =
1287 (struct ldaprwmap *)on->on_bi.bi_private;
1291 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1292 return rwm_send_entry( op, rs );
1295 switch( op->o_tag ) {
1296 case LDAP_REQ_SEARCH:
1297 /* Note: the operation attrs are remapped */
1298 if ( rs->sr_type == REP_RESULT
1299 && op->ors_attrs != NULL
1300 && op->ors_attrs != rs->sr_attrs )
1302 ch_free( op->ors_attrs );
1303 op->ors_attrs = rs->sr_attrs;
1309 case LDAP_REQ_DELETE:
1310 case LDAP_REQ_MODRDN:
1311 case LDAP_REQ_MODIFY:
1312 case LDAP_REQ_COMPARE:
1313 case LDAP_REQ_EXTENDED:
1318 * Rewrite the dn of the referrals, if needed
1321 #ifdef ENABLE_REWRITE
1322 dc.conn = op->o_conn;
1324 dc.ctx = "referralDN";
1325 #else /* ! ENABLE_REWRITE */
1328 #endif /* ! ENABLE_REWRITE */
1329 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1330 if ( rc != LDAP_SUCCESS ) {
1335 rc = rwm_matched( op, rs );
1339 rc = SLAP_CB_CONTINUE;
1355 slap_overinst *on = (slap_overinst *) be->bd_info;
1356 struct ldaprwmap *rwmap =
1357 (struct ldaprwmap *)on->on_bi.bi_private;
1362 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1364 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1367 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1368 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1370 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1371 rc = rwm_m_config( be, fname, lineno, argc, argv );
1373 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1374 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1376 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1379 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1384 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1385 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1387 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1388 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1391 /* TODO: not implemented yet */
1392 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1393 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1398 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1399 fname, lineno, argv[ 1 ] );
1404 rc = SLAP_CONF_UNKNOWN;
1419 slap_overinst *on = (slap_overinst *) be->bd_info;
1420 struct ldapmapping *mapping = NULL;
1421 struct ldaprwmap *rwmap;
1423 rwmap = (struct ldaprwmap *)ch_malloc(sizeof(struct ldaprwmap));
1424 memset(rwmap, 0, sizeof(struct ldaprwmap));
1426 #ifdef ENABLE_REWRITE
1427 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1428 if ( rwmap->rwm_rw == NULL ) {
1436 /* this rewriteContext by default must be null;
1437 * rules can be added if required */
1438 rargv[ 0 ] = "rewriteContext";
1439 rargv[ 1 ] = "searchFilter";
1441 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1443 rargv[ 0 ] = "rewriteContext";
1444 rargv[ 1 ] = "default";
1446 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1449 #endif /* ENABLE_REWRITE */
1451 if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1452 rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1457 on->on_bi.bi_private = (void *)rwmap;
1467 slap_overinst *on = (slap_overinst *) be->bd_info;
1470 if ( on->on_bi.bi_private ) {
1471 struct ldaprwmap *rwmap =
1472 (struct ldaprwmap *)on->on_bi.bi_private;
1474 #ifdef ENABLE_REWRITE
1475 if (rwmap->rwm_rw) {
1476 rewrite_info_delete( &rwmap->rwm_rw );
1478 #else /* !ENABLE_REWRITE */
1479 if ( rwmap->rwm_suffix_massage ) {
1480 ber_bvarray_free( rwmap->rwm_suffix_massage );
1482 #endif /* !ENABLE_REWRITE */
1484 avl_free( rwmap->rwm_oc.remap, NULL );
1485 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1486 avl_free( rwmap->rwm_at.remap, NULL );
1487 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1493 static slap_overinst rwm = { { NULL } };
1498 memset( &rwm, 0, sizeof( slap_overinst ) );
1500 rwm.on_bi.bi_type = "rwm";
1502 rwm.on_bi.bi_db_init = rwm_db_init;
1503 rwm.on_bi.bi_db_config = rwm_db_config;
1504 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1506 rwm.on_bi.bi_op_bind = rwm_op_bind;
1507 rwm.on_bi.bi_op_search = rwm_op_search;
1508 rwm.on_bi.bi_op_compare = rwm_op_compare;
1509 rwm.on_bi.bi_op_modify = rwm_op_modify;
1510 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1511 rwm.on_bi.bi_op_add = rwm_op_add;
1512 rwm.on_bi.bi_op_delete = rwm_op_delete;
1513 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1514 rwm.on_bi.bi_extended = rwm_extended;
1516 rwm.on_bi.bi_operational = rwm_operational;
1517 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1519 #ifdef ENABLE_REWRITE
1520 rwm.on_bi.bi_connection_init = rwm_conn_init;
1521 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1522 #endif /* ENABLE_REWRITE */
1524 rwm.on_response = rwm_response;
1526 return overlay_register( &rwm );
1529 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1531 init_module( int argc, char *argv[] )
1535 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1537 #endif /* SLAPD_OVER_RWM */