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 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
78 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
79 op->o_tmpfree( op->o_req_dn.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
175 == 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 );
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;
232 rwm_op_bind( Operation *op, SlapReply *rs )
234 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
235 struct ldaprwmap *rwmap =
236 (struct ldaprwmap *)on->on_bi.bi_private;
239 #ifdef ENABLE_REWRITE
240 ( void )rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
241 ( void )rewrite_session_init( rwmap->rwm_rw, op->o_conn );
243 rc = rwm_op_dn_massage( op, rs, "bindDN" );
244 #else /* ! ENABLE_REWRITE */
246 rc = rwm_op_dn_massage( op, rs, &rc );
247 #endif /* ! ENABLE_REWRITE */
248 if ( rc != LDAP_SUCCESS ) {
249 op->o_bd->bd_info = (BackendInfo *)on->on_info;
250 send_ldap_error( op, rs, rc, "bindDN massage error" );
254 return SLAP_CB_CONTINUE;
258 rwm_op_unbind( Operation *op, SlapReply *rs )
260 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
261 struct ldaprwmap *rwmap =
262 (struct ldaprwmap *)on->on_bi.bi_private;
264 #ifdef ENABLE_REWRITE
265 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
266 #endif /* ENABLE_REWRITE */
268 return SLAP_CB_CONTINUE;
272 rwm_op_compare( Operation *op, SlapReply *rs )
274 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
275 struct ldaprwmap *rwmap =
276 (struct ldaprwmap *)on->on_bi.bi_private;
279 struct berval mapped_at = BER_BVNULL,
280 mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
282 #ifdef ENABLE_REWRITE
283 rc = rwm_op_dn_massage( op, rs, "compareDN" );
284 #else /* ! ENABLE_REWRITE */
286 rc = rwm_op_dn_massage( op, rs, &rc );
287 #endif /* ! ENABLE_REWRITE */
288 if ( rc != LDAP_SUCCESS ) {
289 op->o_bd->bd_info = (BackendInfo *)on->on_info;
290 send_ldap_error( op, rs, rc, "compareDN massage error" );
294 /* if the attribute is an objectClass, try to remap its value */
295 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
296 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
298 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
299 &mapped_vals[0], RWM_MAP );
300 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
302 op->o_bd->bd_info = (BackendInfo *)on->on_info;
303 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
306 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
307 free( op->orc_ava->aa_value.bv_val );
308 op->orc_ava->aa_value = mapped_vals[0];
310 mapped_at = op->orc_ava->aa_desc->ad_cname;
313 struct ldapmapping *mapping = NULL;
314 AttributeDescription *ad = op->orc_ava->aa_desc;
316 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
318 if ( mapping == NULL ) {
319 if ( rwmap->rwm_at.drop_missing ) {
320 op->o_bd->bd_info = (BackendInfo *)on->on_info;
321 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
326 assert( mapping->m_dst_ad );
327 ad = mapping->m_dst_ad;
330 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
332 struct berval *mapped_valsp[2];
334 mapped_valsp[0] = &mapped_vals[0];
335 mapped_valsp[1] = &mapped_vals[1];
337 mapped_vals[0] = op->orc_ava->aa_value;
339 #ifdef ENABLE_REWRITE
340 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
341 #else /* ! ENABLE_REWRITE */
343 rc = rwm_dnattr_rewrite( op, rs, &rc, NULL, mapped_valsp );
344 #endif /* ! ENABLE_REWRITE */
346 if ( rc != LDAP_SUCCESS ) {
347 op->o_bd->bd_info = (BackendInfo *)on->on_info;
348 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
352 op->orc_ava->aa_value = mapped_vals[0];
354 op->orc_ava->aa_desc = ad;
357 return SLAP_CB_CONTINUE;
361 rwm_op_delete( Operation *op, SlapReply *rs )
363 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
366 #ifdef ENABLE_REWRITE
367 rc = rwm_op_dn_massage( op, rs, "deleteDN" );
368 #else /* ! ENABLE_REWRITE */
370 rc = rwm_op_dn_massage( op, rs, &rc );
371 #endif /* ! ENABLE_REWRITE */
372 if ( rc != LDAP_SUCCESS ) {
373 op->o_bd->bd_info = (BackendInfo *)on->on_info;
374 send_ldap_error( op, rs, rc, "deleteDN massage error" );
378 return SLAP_CB_CONTINUE;
382 rwm_op_modify( Operation *op, SlapReply *rs )
384 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
385 struct ldaprwmap *rwmap =
386 (struct ldaprwmap *)on->on_bi.bi_private;
392 #ifdef ENABLE_REWRITE
393 rc = rwm_op_dn_massage( op, rs, "modifyDN" );
394 #else /* ! ENABLE_REWRITE */
396 rc = rwm_op_dn_massage( op, rs, &rc );
397 #endif /* ! ENABLE_REWRITE */
398 if ( rc != LDAP_SUCCESS ) {
399 op->o_bd->bd_info = (BackendInfo *)on->on_info;
400 send_ldap_error( op, rs, rc, "modifyDN massage error" );
404 isupdate = be_shadow_update( op );
405 for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
408 struct ldapmapping *mapping = NULL;
410 if ( (*mlp)->sml_desc == slap_schema.si_ad_objectClass
411 || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass )
415 } else if ( !isupdate && (*mlp)->sml_desc->ad_type->sat_no_user_mod ) {
421 drop_missing = rwm_mapping( &rwmap->rwm_at,
422 &(*mlp)->sml_desc->ad_cname,
424 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
430 if ( (*mlp)->sml_values != NULL ) {
434 for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[ last ] ); last++ )
438 for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
439 struct ldapmapping *mapping = NULL;
441 ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
443 if ( mapping == NULL ) {
444 if ( rwmap->rwm_at.drop_missing ) {
445 /* FIXME: we allow to remove objectClasses as well;
446 * if the resulting entry is inconsistent, that's
447 * the relayed database's business...
449 ch_free( (*mlp)->sml_values[ j ].bv_val );
451 (*mlp)->sml_values[ j ] = (*mlp)->sml_values[ last ];
453 BER_BVZERO( &(*mlp)->sml_values[ last ] );
459 ch_free( (*mlp)->sml_values[ j ].bv_val );
460 ber_dupbv( &(*mlp)->sml_values[ j ], &mapping->m_dst );
465 if ( (*mlp)->sml_desc->ad_type->sat_syntax ==
466 slap_schema.si_syn_distinguishedName )
468 #ifdef ENABLE_REWRITE
469 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
471 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
472 #else /* ! ENABLE_REWRITE */
474 rc = rwm_dnattr_rewrite( op, rs, &rc,
476 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
477 #endif /* ! ENABLE_REWRITE */
479 } else if ( (*mlp)->sml_desc == slap_schema.si_ad_ref ) {
480 #ifdef ENABLE_REWRITE
481 rc = rwm_referral_rewrite( op, rs,
484 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
485 #else /* ! ENABLE_REWRITE */
487 rc = rwm_referral_rewrite( op, rs, &rc,
489 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
490 #endif /* ! ENABLE_REWRITE */
491 if ( rc != LDAP_SUCCESS ) {
496 if ( rc != LDAP_SUCCESS ) {
503 if ( mapping != NULL ) {
504 /* use new attribute description */
505 assert( mapping->m_dst_ad );
506 (*mlp)->sml_desc = mapping->m_dst_ad;
509 mlp = &(*mlp)->sml_next;
514 *mlp = (*mlp)->sml_next;
515 slap_mod_free( &ml->sml_mod, 0 );
519 /* TODO: rewrite attribute types, values of DN-valued attributes ... */
520 return SLAP_CB_CONTINUE;
524 rwm_op_modrdn( Operation *op, SlapReply *rs )
526 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
527 struct ldaprwmap *rwmap =
528 (struct ldaprwmap *)on->on_bi.bi_private;
532 if ( op->orr_newSup ) {
534 struct berval nnewSup = BER_BVNULL;
535 struct berval newSup = BER_BVNULL;
538 * Rewrite the new superior, if defined and required
541 #ifdef ENABLE_REWRITE
542 dc.conn = op->o_conn;
544 dc.ctx = "newSuperiorDN";
545 #else /* ! ENABLE_REWRITE */
548 #endif /* ! ENABLE_REWRITE */
549 newSup = *op->orr_newSup;
550 nnewSup = *op->orr_nnewSup;
551 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
552 if ( rc != LDAP_SUCCESS ) {
553 op->o_bd->bd_info = (BackendInfo *)on->on_info;
554 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
558 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
559 op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
560 op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
561 *op->orr_newSup = newSup;
562 *op->orr_nnewSup = nnewSup;
567 * Rewrite the dn, if needed
569 #ifdef ENABLE_REWRITE
570 rc = rwm_op_dn_massage( op, rs, "renameDN" );
571 #else /* ! ENABLE_REWRITE */
573 rc = rwm_op_dn_massage( op, rs, &rc );
574 #endif /* ! ENABLE_REWRITE */
575 if ( rc != LDAP_SUCCESS ) {
576 op->o_bd->bd_info = (BackendInfo *)on->on_info;
577 send_ldap_error( op, rs, rc, "renameDN massage error" );
581 /* TODO: rewrite newRDN, attribute types,
582 * values of DN-valued attributes ... */
583 return SLAP_CB_CONTINUE;
587 rwm_swap_attrs( Operation *op, SlapReply *rs )
589 slap_callback *cb = op->o_callback;
590 AttributeName *an = (AttributeName *)cb->sc_private;
594 return SLAP_CB_CONTINUE;
597 static int rwm_freeself( Operation *op, SlapReply *rs )
599 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_RESULT ) {
600 assert( op->o_callback );
602 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
603 op->o_callback = NULL;
606 return SLAP_CB_CONTINUE;
610 rwm_op_search( Operation *op, SlapReply *rs )
612 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
613 struct ldaprwmap *rwmap =
614 (struct ldaprwmap *)on->on_bi.bi_private;
619 struct berval fstr = BER_BVNULL;
623 AttributeName *an = NULL;
627 #ifdef ENABLE_REWRITE
628 rc = rwm_op_dn_massage( op, rs, "searchDN" );
629 #else /* ! ENABLE_REWRITE */
631 rc = rwm_op_dn_massage( op, rs, &rc );
632 #endif /* ! ENABLE_REWRITE */
633 if ( rc != LDAP_SUCCESS ) {
634 text = "searchDN massage error";
639 * Rewrite the dn if needed
642 #ifdef ENABLE_REWRITE
643 dc.conn = op->o_conn;
645 dc.ctx = "searchFilterAttrDN";
646 #else /* ! ENABLE_REWRITE */
649 #endif /* ! ENABLE_REWRITE */
651 rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
652 if ( rc != LDAP_SUCCESS ) {
653 text = "searchFilter/searchFilterAttrDN massage error";
657 f = str2filter_x( op, fstr.bv_val );
660 text = "massaged filter parse error";
664 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
665 ch_free( op->ors_filterstr.bv_val );
668 if( op->ors_filter ) {
669 filter_free_x( op, op->ors_filter );
673 op->ors_filterstr = fstr;
675 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
676 op->ors_attrs, &an, RWM_MAP );
677 if ( rc != LDAP_SUCCESS ) {
678 text = "attribute list mapping error";
682 cb = (slap_callback *) op->o_tmpcalloc( sizeof( slap_callback ),
683 1, op->o_tmpmemctx );
689 cb->sc_response = rwm_swap_attrs;
690 cb->sc_cleanup = rwm_freeself;
691 cb->sc_private = (void *)op->ors_attrs;
692 cb->sc_next = op->o_callback;
697 return SLAP_CB_CONTINUE;
705 filter_free_x( op, f );
708 if ( !BER_BVISNULL( &fstr ) ) {
709 ch_free( fstr.bv_val );
712 op->o_bd->bd_info = (BackendInfo *)on->on_info;
713 send_ldap_error( op, rs, rc, text );
720 rwm_extended( Operation *op, SlapReply *rs )
722 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
725 #ifdef ENABLE_REWRITE
726 rc = rwm_op_dn_massage( op, rs, "extendedDN" );
727 #else /* ! ENABLE_REWRITE */
729 rc = rwm_op_dn_massage( op, rs, &rc );
730 #endif /* ! ENABLE_REWRITE */
731 if ( rc != LDAP_SUCCESS ) {
732 op->o_bd->bd_info = (BackendInfo *)on->on_info;
733 send_ldap_error( op, rs, rc, "extendedDN massage error" );
737 /* TODO: rewrite/map extended data ? ... */
738 return SLAP_CB_CONTINUE;
742 rwm_matched( Operation *op, SlapReply *rs )
744 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
745 struct ldaprwmap *rwmap =
746 (struct ldaprwmap *)on->on_bi.bi_private;
748 struct berval dn, mdn;
752 if ( rs->sr_matched == NULL ) {
753 return SLAP_CB_CONTINUE;
757 #ifdef ENABLE_REWRITE
758 dc.conn = op->o_conn;
760 dc.ctx = "matchedDN";
761 #else /* ! ENABLE_REWRITE */
764 #endif /* ! ENABLE_REWRITE */
765 ber_str2bv( rs->sr_matched, 0, 0, &dn );
767 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
768 if ( rc != LDAP_SUCCESS ) {
770 rs->sr_text = "Rewrite error";
774 if ( mdn.bv_val != dn.bv_val ) {
775 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
776 ch_free( (void *)rs->sr_matched );
779 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
781 rs->sr_matched = mdn.bv_val;
784 return SLAP_CB_CONTINUE;
788 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
790 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
791 struct ldaprwmap *rwmap =
792 (struct ldaprwmap *)on->on_bi.bi_private;
800 * Rewrite the dn attrs, if needed
803 #ifdef ENABLE_REWRITE
804 dc.conn = op->o_conn;
806 #else /* ! ENABLE_REWRITE */
809 #endif /* ! ENABLE_REWRITE */
811 /* FIXME: the entries are in the remote mapping form;
812 * so we need to select those attributes we are willing
813 * to return, and remap them accordingly */
815 /* FIXME: in principle, one could map an attribute
816 * on top of another, which already exists.
817 * As such, in the end there might exist more than
818 * one instance of an attribute.
819 * We should at least check if this occurs, and issue
820 * an error (because multiple instances of attrs in
821 * response are not valid), or merge the values (what
822 * about duplicate values?) */
823 isupdate = be_shadow_update( op );
824 for ( ap = a_first; *ap; ) {
825 struct ldapmapping *mapping;
830 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
835 drop_missing = rwm_mapping( &rwmap->rwm_at,
836 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
837 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
842 if ( mapping != NULL ) {
843 (*ap)->a_desc = mapping->m_dst_ad;
846 if ( op->ors_attrs != NULL &&
847 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
848 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
854 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
855 if ( stripEntryDN ) {
856 /* will be generated by frontend */
860 } else if ( !isupdate
861 && (*ap)->a_desc->ad_type->sat_no_user_mod
862 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
867 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
871 /* empty? for now, we leave it in place */
876 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
877 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
881 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
882 struct berval mapped;
884 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
885 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
886 ch_free( bv[0].bv_val );
887 BER_BVZERO( &bv[0] );
888 if ( &(*ap)->a_vals[last] > &bv[0] ) {
889 bv[0] = (*ap)->a_vals[last];
890 BER_BVZERO( &(*ap)->a_vals[last] );
895 } else if ( mapped.bv_val != bv[0].bv_val ) {
897 * FIXME: after LBER_FREEing
898 * the value is replaced by
901 ch_free( bv[0].bv_val );
902 ber_dupbv( &bv[0], &mapped );
907 * It is necessary to try to rewrite attributes with
908 * dn syntax because they might be used in ACLs as
909 * members of groups; since ACLs are applied to the
910 * rewritten stuff, no dn-based subject clause could
911 * be used at the ldap backend side (see
912 * http://www.OpenLDAP.org/faq/data/cache/452.html)
913 * The problem can be overcome by moving the dn-based
914 * ACLs to the target directory server, and letting
915 * everything pass thru the ldap backend. */
916 /* FIXME: handle distinguishedName-like syntaxes, like
917 * nameAndOptionalUID */
918 } else if ( (*ap)->a_desc->ad_type->sat_syntax ==
919 slap_schema.si_syn_distinguishedName )
921 #ifdef ENABLE_REWRITE
922 dc.ctx = "searchAttrDN";
923 #endif /* ENABLE_REWRITE */
924 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
925 if ( rc != LDAP_SUCCESS ) {
929 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
930 #ifdef ENABLE_REWRITE
931 dc.ctx = "searchAttrDN";
932 #endif /* ENABLE_REWRITE */
933 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
934 if ( rc != LDAP_SUCCESS ) {
939 if ( mapping != NULL ) {
940 /* rewrite the attribute description */
941 assert( mapping->m_dst_ad );
942 (*ap)->a_desc = mapping->m_dst_ad;
960 rwm_send_entry( Operation *op, SlapReply *rs )
962 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
963 struct ldaprwmap *rwmap =
964 (struct ldaprwmap *)on->on_bi.bi_private;
968 struct berval dn = BER_BVNULL,
973 assert( rs->sr_entry );
976 * Rewrite the dn of the result, if needed
979 #ifdef ENABLE_REWRITE
980 dc.conn = op->o_conn;
982 dc.ctx = "searchEntryDN";
983 #else /* ! ENABLE_REWRITE */
986 #endif /* ! ENABLE_REWRITE */
989 flags = rs->sr_flags;
990 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
991 /* FIXME: all we need to duplicate are:
994 * - attributes that are requested
995 * - no values if attrsonly is set
1000 rc = LDAP_NO_MEMORY;
1004 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1008 * Note: this may fail if the target host(s) schema differs
1009 * from the one known to the meta, and a DN with unknown
1010 * attributes is returned.
1014 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1015 if ( rc != LDAP_SUCCESS ) {
1020 if ( e->e_name.bv_val != dn.bv_val ) {
1021 ch_free( e->e_name.bv_val );
1022 ch_free( e->e_nname.bv_val );
1028 /* TODO: map entry attribute types, objectclasses
1029 * and dn-valued attribute values */
1031 /* FIXME: the entries are in the remote mapping form;
1032 * so we need to select those attributes we are willing
1033 * to return, and remap them accordingly */
1034 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1036 if ( rs->sr_operational_attrs ) {
1037 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1041 rs->sr_flags = flags;
1043 return SLAP_CB_CONTINUE;
1046 if ( !BER_BVISNULL( &dn ) ) {
1047 ch_free( dn.bv_val );
1050 if ( !BER_BVISNULL( &ndn ) ) {
1051 ch_free( ndn.bv_val );
1054 if ( e != NULL && e != rs->sr_entry ) {
1062 rwm_operational( Operation *op, SlapReply *rs )
1064 /* FIXME: the entries are in the remote mapping form;
1065 * so we need to select those attributes we are willing
1066 * to return, and remap them accordingly */
1067 if ( rs->sr_operational_attrs ) {
1068 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1071 return SLAP_CB_CONTINUE;
1075 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1076 * rewritten for subsequent operations; fine for plain suffixmassage,
1077 * but destroys everything else */
1079 rwm_chk_referrals( Operation *op, SlapReply *rs )
1081 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1084 #ifdef ENABLE_REWRITE
1085 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1086 #else /* ! ENABLE_REWRITE */
1088 rc = rwm_op_dn_massage( op, rs, &rc );
1089 #endif /* ! ENABLE_REWRITE */
1090 if ( rc != LDAP_SUCCESS ) {
1091 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1092 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1096 return SLAP_CB_CONTINUE;
1109 #ifdef ENABLE_REWRITE
1110 slap_overinst *on = (slap_overinst *) be->bd_info;
1111 struct ldaprwmap *rwmap =
1112 (struct ldaprwmap *)on->on_bi.bi_private;
1114 return rewrite_parse( rwmap->rwm_rw,
1115 fname, lineno, argc, argv );
1117 #else /* !ENABLE_REWRITE */
1118 fprintf( stderr, "%s: line %d: rewrite capabilities "
1119 "are not enabled\n", fname, lineno );
1120 #endif /* !ENABLE_REWRITE */
1126 rwm_suffixmassage_config(
1134 slap_overinst *on = (slap_overinst *) be->bd_info;
1135 struct ldaprwmap *rwmap =
1136 (struct ldaprwmap *)on->on_bi.bi_private;
1138 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1140 #ifdef ENABLE_REWRITE
1142 #endif /* ENABLE_REWRITE */
1147 * suffixmassage [<suffix>] <massaged suffix>
1149 * the [<suffix>] field must be defined as a valid suffix
1150 * for the current database;
1151 * the <massaged suffix> shouldn't have already been
1152 * defined as a valid suffix for the current server
1155 if ( be->be_suffix == NULL ) {
1156 fprintf( stderr, "%s: line %d: "
1157 " \"suffixMassage [<suffix>]"
1158 " <massaged suffix>\" without "
1159 "<suffix> part requires database "
1160 "suffix be defined first.\n",
1164 bvnc = be->be_suffix[ 0 ];
1167 } else if ( argc == 3 ) {
1168 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1172 fprintf( stderr, "%s: line %d: syntax is"
1173 " \"suffixMassage [<suffix>]"
1174 " <massaged suffix>\"\n",
1179 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1180 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1181 fname, lineno, bvnc.bv_val );
1185 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1186 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1187 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1188 fname, lineno, brnc.bv_val );
1189 free( nvnc.bv_val );
1190 free( pvnc.bv_val );
1194 #ifdef ENABLE_REWRITE
1196 * The suffix massaging is emulated
1197 * by means of the rewrite capabilities
1199 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1200 &pvnc, &nvnc, &prnc, &nrnc );
1201 free( nvnc.bv_val );
1202 free( pvnc.bv_val );
1203 free( nrnc.bv_val );
1204 free( prnc.bv_val );
1208 #else /* !ENABLE_REWRITE */
1209 ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1210 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1212 ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1213 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1214 #endif /* !ENABLE_REWRITE */
1228 slap_overinst *on = (slap_overinst *) be->bd_info;
1229 struct ldaprwmap *rwmap =
1230 (struct ldaprwmap *)on->on_bi.bi_private;
1232 /* objectclass/attribute mapping */
1233 return rwm_map_config( &rwmap->rwm_oc,
1235 fname, lineno, argc, argv );
1239 rwm_response( Operation *op, SlapReply *rs )
1241 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1242 struct ldaprwmap *rwmap =
1243 (struct ldaprwmap *)on->on_bi.bi_private;
1247 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1248 return rwm_send_entry( op, rs );
1251 switch( op->o_tag ) {
1252 case LDAP_REQ_SEARCH:
1253 /* Note: the operation attrs are remapped */
1254 if ( op->ors_attrs != NULL && op->ors_attrs != rs->sr_attrs )
1256 ch_free( op->ors_attrs );
1257 op->ors_attrs = rs->sr_attrs;
1263 case LDAP_REQ_DELETE:
1264 case LDAP_REQ_MODRDN:
1265 case LDAP_REQ_MODIFY:
1266 case LDAP_REQ_COMPARE:
1267 case LDAP_REQ_EXTENDED:
1272 * Rewrite the dn of the referrals, if needed
1275 #ifdef ENABLE_REWRITE
1276 dc.conn = op->o_conn;
1278 dc.ctx = "referralDN";
1279 #else /* ! ENABLE_REWRITE */
1282 #endif /* ! ENABLE_REWRITE */
1283 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1284 if ( rc != LDAP_SUCCESS ) {
1289 rc = rwm_matched( op, rs );
1293 rc = SLAP_CB_CONTINUE;
1312 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1314 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1317 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1318 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1320 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1321 rc = rwm_m_config( be, fname, lineno, argc, argv );
1323 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1324 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1327 rc = SLAP_CONF_UNKNOWN;
1342 slap_overinst *on = (slap_overinst *) be->bd_info;
1343 struct ldapmapping *mapping = NULL;
1344 struct ldaprwmap *rwmap;
1346 rwmap = (struct ldaprwmap *)ch_malloc(sizeof(struct ldaprwmap));
1347 memset(rwmap, 0, sizeof(struct ldaprwmap));
1349 #ifdef ENABLE_REWRITE
1350 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1351 if ( rwmap->rwm_rw == NULL ) {
1359 /* this rewriteContext by default must be null;
1360 * rules can be added if required */
1361 rargv[ 0 ] = "rewriteContext";
1362 rargv[ 1 ] = "searchFilter";
1364 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1366 rargv[ 0 ] = "rewriteContext";
1367 rargv[ 1 ] = "default";
1369 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1372 #endif /* ENABLE_REWRITE */
1374 if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1375 rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1380 on->on_bi.bi_private = (void *)rwmap;
1390 slap_overinst *on = (slap_overinst *) be->bd_info;
1393 if ( on->on_bi.bi_private ) {
1394 struct ldaprwmap *rwmap =
1395 (struct ldaprwmap *)on->on_bi.bi_private;
1397 #ifdef ENABLE_REWRITE
1398 if (rwmap->rwm_rw) {
1399 rewrite_info_delete( &rwmap->rwm_rw );
1401 #else /* !ENABLE_REWRITE */
1402 if ( rwmap->rwm_suffix_massage ) {
1403 ber_bvarray_free( rwmap->rwm_suffix_massage );
1405 #endif /* !ENABLE_REWRITE */
1407 avl_free( rwmap->rwm_oc.remap, NULL );
1408 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1409 avl_free( rwmap->rwm_at.remap, NULL );
1410 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1416 static slap_overinst rwm = { { NULL } };
1421 memset( &rwm, 0, sizeof( slap_overinst ) );
1423 rwm.on_bi.bi_type = "rwm";
1425 rwm.on_bi.bi_db_init = rwm_db_init;
1426 rwm.on_bi.bi_db_config = rwm_db_config;
1427 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1429 rwm.on_bi.bi_op_bind = rwm_op_bind;
1430 rwm.on_bi.bi_op_search = rwm_op_search;
1431 rwm.on_bi.bi_op_compare = rwm_op_compare;
1432 rwm.on_bi.bi_op_modify = rwm_op_modify;
1433 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1434 rwm.on_bi.bi_op_add = rwm_op_add;
1435 rwm.on_bi.bi_op_delete = rwm_op_delete;
1436 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1437 rwm.on_bi.bi_extended = rwm_extended;
1439 rwm.on_bi.bi_operational = rwm_operational;
1440 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1442 rwm.on_response = rwm_response;
1444 return overlay_register( &rwm );
1447 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1449 init_module( int argc, char *argv[] )
1453 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1455 #endif /* SLAPD_OVER_RWM */