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 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
115 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
118 /* Count number of attributes in entry */
119 isupdate = be_shadow_update( op );
120 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
123 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
124 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
128 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[ last ] ); last++ )
131 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
132 struct ldapmapping *mapping = NULL;
134 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
136 if ( mapping == NULL ) {
137 if ( rwmap->rwm_at.drop_missing ) {
138 /* FIXME: we allow to remove objectClasses as well;
139 * if the resulting entry is inconsistent, that's
140 * the relayed database's business...
142 ch_free( (*ap)->a_vals[ j ].bv_val );
144 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
146 BER_BVZERO( &(*ap)->a_vals[ last ] );
152 ch_free( (*ap)->a_vals[ j ].bv_val );
153 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
157 } else if ( !isupdate && !get_manageDIT( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
162 struct ldapmapping *mapping = NULL;
164 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
166 if ( mapping == NULL ) {
167 if ( rwmap->rwm_at.drop_missing ) {
172 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
173 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
176 * FIXME: rewrite could fail; in this case
177 * the operation should give up, right?
179 #ifdef ENABLE_REWRITE
180 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
182 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
183 #else /* ! ENABLE_REWRITE */
185 rc = rwm_dnattr_rewrite( op, rs, &rc, (*ap)->a_vals,
186 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
187 #endif /* ! ENABLE_REWRITE */
192 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
193 #ifdef ENABLE_REWRITE
194 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
196 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
197 #else /* ! ENABLE_REWRITE */
199 rc = rwm_referral_rewrite( op, rs, &rc, (*ap)->a_vals,
200 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
201 #endif /* ! ENABLE_REWRITE */
202 if ( rc != LDAP_SUCCESS ) {
207 if ( mapping != NULL ) {
208 assert( mapping->m_dst_ad != NULL );
209 (*ap)->a_desc = mapping->m_dst_ad;
218 /* FIXME: leaking attribute/values? */
225 /* TODO: map attribute types, values of DN-valued attributes ... */
226 return SLAP_CB_CONTINUE;
229 #ifdef ENABLE_REWRITE
231 rwm_conn_init( BackendDB *be, Connection *conn )
233 slap_overinst *on = (slap_overinst *) be->bd_info;
234 struct ldaprwmap *rwmap =
235 (struct ldaprwmap *)on->on_bi.bi_private;
237 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
239 return SLAP_CB_CONTINUE;
243 rwm_conn_destroy( BackendDB *be, Connection *conn )
245 slap_overinst *on = (slap_overinst *) be->bd_info;
246 struct ldaprwmap *rwmap =
247 (struct ldaprwmap *)on->on_bi.bi_private;
249 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
251 return SLAP_CB_CONTINUE;
253 #endif /* ENABLE_REWRITE */
256 rwm_op_bind( Operation *op, SlapReply *rs )
258 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
261 #ifdef ENABLE_REWRITE
262 rc = rwm_op_dn_massage( op, rs, "bindDN" );
263 #else /* ! ENABLE_REWRITE */
265 rc = rwm_op_dn_massage( op, rs, &rc );
266 #endif /* ! ENABLE_REWRITE */
267 if ( rc != LDAP_SUCCESS ) {
268 op->o_bd->bd_info = (BackendInfo *)on->on_info;
269 send_ldap_error( op, rs, rc, "bindDN massage error" );
273 return SLAP_CB_CONTINUE;
277 rwm_op_unbind( Operation *op, SlapReply *rs )
279 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
280 struct ldaprwmap *rwmap =
281 (struct ldaprwmap *)on->on_bi.bi_private;
283 #ifdef ENABLE_REWRITE
284 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
285 #endif /* ENABLE_REWRITE */
287 return SLAP_CB_CONTINUE;
291 rwm_op_compare( Operation *op, SlapReply *rs )
293 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
294 struct ldaprwmap *rwmap =
295 (struct ldaprwmap *)on->on_bi.bi_private;
298 struct berval mapped_at = BER_BVNULL,
299 mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
301 #ifdef ENABLE_REWRITE
302 rc = rwm_op_dn_massage( op, rs, "compareDN" );
303 #else /* ! ENABLE_REWRITE */
305 rc = rwm_op_dn_massage( op, rs, &rc );
306 #endif /* ! ENABLE_REWRITE */
307 if ( rc != LDAP_SUCCESS ) {
308 op->o_bd->bd_info = (BackendInfo *)on->on_info;
309 send_ldap_error( op, rs, rc, "compareDN massage error" );
313 /* if the attribute is an objectClass, try to remap its value */
314 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
315 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
317 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
318 &mapped_vals[0], RWM_MAP );
319 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
321 op->o_bd->bd_info = (BackendInfo *)on->on_info;
322 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
325 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
326 ber_bvreplace_x( &op->orc_ava->aa_value, &mapped_vals[0], op->o_tmpmemctx );
328 mapped_at = op->orc_ava->aa_desc->ad_cname;
331 struct ldapmapping *mapping = NULL;
332 AttributeDescription *ad = op->orc_ava->aa_desc;
334 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
336 if ( mapping == NULL ) {
337 if ( rwmap->rwm_at.drop_missing ) {
338 op->o_bd->bd_info = (BackendInfo *)on->on_info;
339 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
344 assert( mapping->m_dst_ad != NULL );
345 ad = mapping->m_dst_ad;
348 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
349 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
351 struct berval *mapped_valsp[2];
353 mapped_valsp[0] = &mapped_vals[0];
354 mapped_valsp[1] = &mapped_vals[1];
356 mapped_vals[0] = op->orc_ava->aa_value;
358 #ifdef ENABLE_REWRITE
359 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
360 #else /* ! ENABLE_REWRITE */
362 rc = rwm_dnattr_rewrite( op, rs, &rc, NULL, mapped_valsp );
363 #endif /* ! ENABLE_REWRITE */
365 if ( rc != LDAP_SUCCESS ) {
366 op->o_bd->bd_info = (BackendInfo *)on->on_info;
367 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
371 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
372 /* NOTE: if we get here, rwm_dnattr_rewrite()
373 * already freed the old value, so now
375 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
377 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
380 op->orc_ava->aa_desc = ad;
383 return SLAP_CB_CONTINUE;
387 rwm_op_delete( Operation *op, SlapReply *rs )
389 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
392 #ifdef ENABLE_REWRITE
393 rc = rwm_op_dn_massage( op, rs, "deleteDN" );
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, "deleteDN massage error" );
404 return SLAP_CB_CONTINUE;
408 rwm_op_modify( Operation *op, SlapReply *rs )
410 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
411 struct ldaprwmap *rwmap =
412 (struct ldaprwmap *)on->on_bi.bi_private;
418 #ifdef ENABLE_REWRITE
419 rc = rwm_op_dn_massage( op, rs, "modifyDN" );
420 #else /* ! ENABLE_REWRITE */
422 rc = rwm_op_dn_massage( op, rs, &rc );
423 #endif /* ! ENABLE_REWRITE */
424 if ( rc != LDAP_SUCCESS ) {
425 op->o_bd->bd_info = (BackendInfo *)on->on_info;
426 send_ldap_error( op, rs, rc, "modifyDN massage error" );
430 isupdate = be_shadow_update( op );
431 for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
434 struct ldapmapping *mapping = NULL;
436 if ( (*mlp)->sml_desc == slap_schema.si_ad_objectClass
437 || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass )
441 } else if ( !isupdate && !get_manageDIT( op ) && (*mlp)->sml_desc->ad_type->sat_no_user_mod )
448 drop_missing = rwm_mapping( &rwmap->rwm_at,
449 &(*mlp)->sml_desc->ad_cname,
451 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
457 if ( (*mlp)->sml_values != NULL ) {
461 for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[ last ] ); last++ )
465 for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
466 struct ldapmapping *oc_mapping = NULL;
468 ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
469 &oc_mapping, RWM_MAP );
470 if ( oc_mapping == NULL ) {
471 if ( rwmap->rwm_at.drop_missing ) {
472 /* FIXME: we allow to remove objectClasses as well;
473 * if the resulting entry is inconsistent, that's
474 * the relayed database's business...
476 ch_free( (*mlp)->sml_values[ j ].bv_val );
478 (*mlp)->sml_values[ j ] = (*mlp)->sml_values[ last ];
480 BER_BVZERO( &(*mlp)->sml_values[ last ] );
486 ch_free( (*mlp)->sml_values[ j ].bv_val );
487 ber_dupbv( &(*mlp)->sml_values[ j ], &oc_mapping->m_dst );
492 if ( (*mlp)->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
493 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
495 #ifdef ENABLE_REWRITE
496 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
498 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
499 #else /* ! ENABLE_REWRITE */
501 rc = rwm_dnattr_rewrite( op, rs, &rc,
503 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
504 #endif /* ! ENABLE_REWRITE */
506 } else if ( (*mlp)->sml_desc == slap_schema.si_ad_ref ) {
507 #ifdef ENABLE_REWRITE
508 rc = rwm_referral_rewrite( op, rs,
511 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
512 #else /* ! ENABLE_REWRITE */
514 rc = rwm_referral_rewrite( op, rs, &rc,
516 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
517 #endif /* ! ENABLE_REWRITE */
518 if ( rc != LDAP_SUCCESS ) {
523 if ( rc != LDAP_SUCCESS ) {
530 if ( mapping != NULL ) {
531 /* use new attribute description */
532 assert( mapping->m_dst_ad != NULL );
533 (*mlp)->sml_desc = mapping->m_dst_ad;
536 mlp = &(*mlp)->sml_next;
541 *mlp = (*mlp)->sml_next;
542 slap_mod_free( &ml->sml_mod, 0 );
546 return SLAP_CB_CONTINUE;
550 rwm_op_modrdn( Operation *op, SlapReply *rs )
552 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
553 struct ldaprwmap *rwmap =
554 (struct ldaprwmap *)on->on_bi.bi_private;
558 if ( op->orr_newSup ) {
560 struct berval nnewSup = BER_BVNULL;
561 struct berval newSup = BER_BVNULL;
564 * Rewrite the new superior, if defined and required
567 #ifdef ENABLE_REWRITE
568 dc.conn = op->o_conn;
570 dc.ctx = "newSuperiorDN";
571 #else /* ! ENABLE_REWRITE */
574 #endif /* ! ENABLE_REWRITE */
575 newSup = *op->orr_newSup;
576 nnewSup = *op->orr_nnewSup;
577 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
578 if ( rc != LDAP_SUCCESS ) {
579 op->o_bd->bd_info = (BackendInfo *)on->on_info;
580 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
584 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
585 op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
586 op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
587 *op->orr_newSup = newSup;
588 *op->orr_nnewSup = nnewSup;
593 * Rewrite the dn, if needed
595 #ifdef ENABLE_REWRITE
596 rc = rwm_op_dn_massage( op, rs, "renameDN" );
597 #else /* ! ENABLE_REWRITE */
599 rc = rwm_op_dn_massage( op, rs, &rc );
600 #endif /* ! ENABLE_REWRITE */
601 if ( rc != LDAP_SUCCESS ) {
602 op->o_bd->bd_info = (BackendInfo *)on->on_info;
603 send_ldap_error( op, rs, rc, "renameDN massage error" );
607 /* TODO: rewrite newRDN, attribute types,
608 * values of DN-valued attributes ... */
609 return SLAP_CB_CONTINUE;
612 static slap_callback rwm_cb;
619 ber_memfree_x( data, NULL );
622 static slap_callback *
623 rwm_callback_get( Operation *op )
627 if ( op->o_threadctx == NULL ) {
631 ldap_pvt_thread_pool_getkey( op->o_threadctx,
632 rwm_keyfree, &data, NULL );
633 if ( data == NULL ) {
634 data = ch_calloc( sizeof( slap_callback ), 1 );
635 ldap_pvt_thread_pool_setkey( op->o_threadctx,
636 rwm_keyfree, data, rwm_keyfree );
639 return (slap_callback *)data;
643 rwm_swap_attrs( Operation *op, SlapReply *rs )
645 slap_callback *cb = op->o_callback;
646 AttributeName *an = (AttributeName *)cb->sc_private;
650 return SLAP_CB_CONTINUE;
654 rwm_op_search( Operation *op, SlapReply *rs )
656 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
657 struct ldaprwmap *rwmap =
658 (struct ldaprwmap *)on->on_bi.bi_private;
663 struct berval fstr = BER_BVNULL;
666 slap_callback *cb = NULL;
667 AttributeName *an = NULL;
671 #ifdef ENABLE_REWRITE
672 rc = rwm_op_dn_massage( op, rs, "searchDN" );
673 #else /* ! ENABLE_REWRITE */
675 rc = rwm_op_dn_massage( op, rs, &rc );
676 #endif /* ! ENABLE_REWRITE */
677 if ( rc != LDAP_SUCCESS ) {
678 text = "searchDN massage error";
683 * Rewrite the dn if needed
686 #ifdef ENABLE_REWRITE
687 dc.conn = op->o_conn;
689 dc.ctx = "searchFilterAttrDN";
690 #else /* ! ENABLE_REWRITE */
693 #endif /* ! ENABLE_REWRITE */
695 rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
696 if ( rc != LDAP_SUCCESS ) {
697 text = "searchFilter/searchFilterAttrDN massage error";
701 f = str2filter_x( op, fstr.bv_val );
704 text = "massaged filter parse error";
708 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
709 ch_free( op->ors_filterstr.bv_val );
712 if( op->ors_filter ) {
713 filter_free_x( op, op->ors_filter );
717 op->ors_filterstr = fstr;
719 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
720 op->ors_attrs, &an, RWM_MAP );
721 if ( rc != LDAP_SUCCESS ) {
722 text = "attribute list mapping error";
726 cb = rwm_callback_get( op );
728 cb->sc_response = rwm_swap_attrs;
729 cb->sc_cleanup = NULL;
730 cb->sc_private = (void *)op->ors_attrs;
731 cb->sc_next = op->o_callback;
736 return SLAP_CB_CONTINUE;
744 filter_free_x( op, f );
747 if ( !BER_BVISNULL( &fstr ) ) {
748 ch_free( fstr.bv_val );
751 op->o_bd->bd_info = (BackendInfo *)on->on_info;
752 send_ldap_error( op, rs, rc, text );
759 rwm_extended( Operation *op, SlapReply *rs )
761 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
764 #ifdef ENABLE_REWRITE
765 rc = rwm_op_dn_massage( op, rs, "extendedDN" );
766 #else /* ! ENABLE_REWRITE */
768 rc = rwm_op_dn_massage( op, rs, &rc );
769 #endif /* ! ENABLE_REWRITE */
770 if ( rc != LDAP_SUCCESS ) {
771 op->o_bd->bd_info = (BackendInfo *)on->on_info;
772 send_ldap_error( op, rs, rc, "extendedDN massage error" );
776 /* TODO: rewrite/map extended data ? ... */
777 return SLAP_CB_CONTINUE;
781 rwm_matched( Operation *op, SlapReply *rs )
783 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
784 struct ldaprwmap *rwmap =
785 (struct ldaprwmap *)on->on_bi.bi_private;
787 struct berval dn, mdn;
791 if ( rs->sr_matched == NULL ) {
792 return SLAP_CB_CONTINUE;
796 #ifdef ENABLE_REWRITE
797 dc.conn = op->o_conn;
799 dc.ctx = "matchedDN";
800 #else /* ! ENABLE_REWRITE */
803 #endif /* ! ENABLE_REWRITE */
804 ber_str2bv( rs->sr_matched, 0, 0, &dn );
806 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
807 if ( rc != LDAP_SUCCESS ) {
809 rs->sr_text = "Rewrite error";
813 if ( mdn.bv_val != dn.bv_val ) {
814 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
815 ch_free( (void *)rs->sr_matched );
818 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
820 rs->sr_matched = mdn.bv_val;
823 return SLAP_CB_CONTINUE;
827 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
829 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
830 struct ldaprwmap *rwmap =
831 (struct ldaprwmap *)on->on_bi.bi_private;
839 * Rewrite the dn attrs, if needed
842 #ifdef ENABLE_REWRITE
843 dc.conn = op->o_conn;
845 #else /* ! ENABLE_REWRITE */
848 #endif /* ! ENABLE_REWRITE */
850 /* FIXME: the entries are in the remote mapping form;
851 * so we need to select those attributes we are willing
852 * to return, and remap them accordingly */
854 /* FIXME: in principle, one could map an attribute
855 * on top of another, which already exists.
856 * As such, in the end there might exist more than
857 * one instance of an attribute.
858 * We should at least check if this occurs, and issue
859 * an error (because multiple instances of attrs in
860 * response are not valid), or merge the values (what
861 * about duplicate values?) */
862 isupdate = be_shadow_update( op );
863 for ( ap = a_first; *ap; ) {
864 struct ldapmapping *mapping = NULL;
869 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
874 if ( op->ors_attrs != NULL &&
875 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
876 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
881 drop_missing = rwm_mapping( &rwmap->rwm_at,
882 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
883 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
888 if ( mapping != NULL ) {
889 (*ap)->a_desc = mapping->m_dst_ad;
893 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
894 if ( stripEntryDN ) {
895 /* will be generated by frontend */
899 } else if ( !isupdate
900 && !get_manageDIT( op )
901 && (*ap)->a_desc->ad_type->sat_no_user_mod
902 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
907 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
911 /* empty? leave it in place because of attrsonly and vlv */
916 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
917 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
921 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
922 struct berval mapped;
924 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
925 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
926 ch_free( bv[0].bv_val );
927 BER_BVZERO( &bv[0] );
928 if ( &(*ap)->a_vals[last] > &bv[0] ) {
929 bv[0] = (*ap)->a_vals[last];
930 BER_BVZERO( &(*ap)->a_vals[last] );
935 } else if ( mapped.bv_val != bv[0].bv_val ) {
937 * FIXME: after LBER_FREEing
938 * the value is replaced by
941 ber_bvreplace( &bv[0], &mapped );
946 * It is necessary to try to rewrite attributes with
947 * dn syntax because they might be used in ACLs as
948 * members of groups; since ACLs are applied to the
949 * rewritten stuff, no dn-based subject clause could
950 * be used at the ldap backend side (see
951 * http://www.OpenLDAP.org/faq/data/cache/452.html)
952 * The problem can be overcome by moving the dn-based
953 * ACLs to the target directory server, and letting
954 * everything pass thru the ldap backend. */
955 /* FIXME: handle distinguishedName-like syntaxes, like
956 * nameAndOptionalUID */
957 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
958 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
960 #ifdef ENABLE_REWRITE
961 dc.ctx = "searchAttrDN";
962 #endif /* ENABLE_REWRITE */
963 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
964 if ( rc != LDAP_SUCCESS ) {
968 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
969 #ifdef ENABLE_REWRITE
970 dc.ctx = "searchAttrDN";
971 #endif /* ENABLE_REWRITE */
972 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
973 if ( rc != LDAP_SUCCESS ) {
978 if ( mapping != NULL ) {
979 /* rewrite the attribute description */
980 assert( mapping->m_dst_ad != NULL );
981 (*ap)->a_desc = mapping->m_dst_ad;
999 rwm_send_entry( Operation *op, SlapReply *rs )
1001 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1002 struct ldaprwmap *rwmap =
1003 (struct ldaprwmap *)on->on_bi.bi_private;
1007 struct berval dn = BER_BVNULL,
1012 assert( rs->sr_entry != NULL );
1015 * Rewrite the dn of the result, if needed
1018 #ifdef ENABLE_REWRITE
1019 dc.conn = op->o_conn;
1021 dc.ctx = "searchEntryDN";
1022 #else /* ! ENABLE_REWRITE */
1025 #endif /* ! ENABLE_REWRITE */
1028 flags = rs->sr_flags;
1029 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1030 /* FIXME: all we need to duplicate are:
1033 * - attributes that are requested
1034 * - no values if attrsonly is set
1039 rc = LDAP_NO_MEMORY;
1043 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1047 * Note: this may fail if the target host(s) schema differs
1048 * from the one known to the meta, and a DN with unknown
1049 * attributes is returned.
1053 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1054 if ( rc != LDAP_SUCCESS ) {
1059 if ( e->e_name.bv_val != dn.bv_val ) {
1060 ch_free( e->e_name.bv_val );
1061 ch_free( e->e_nname.bv_val );
1067 /* TODO: map entry attribute types, objectclasses
1068 * and dn-valued attribute values */
1070 /* FIXME: the entries are in the remote mapping form;
1071 * so we need to select those attributes we are willing
1072 * to return, and remap them accordingly */
1073 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1076 if ( rs->sr_operational_attrs ) {
1077 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1082 rs->sr_flags = flags;
1084 return SLAP_CB_CONTINUE;
1087 if ( e != NULL && e != rs->sr_entry ) {
1088 if ( e->e_name.bv_val == dn.bv_val ) {
1089 BER_BVZERO( &e->e_name );
1092 if ( e->e_nname.bv_val == ndn.bv_val ) {
1093 BER_BVZERO( &e->e_nname );
1099 if ( !BER_BVISNULL( &dn ) ) {
1100 ch_free( dn.bv_val );
1103 if ( !BER_BVISNULL( &ndn ) ) {
1104 ch_free( ndn.bv_val );
1111 rwm_operational( Operation *op, SlapReply *rs )
1113 /* FIXME: the entries are in the remote mapping form;
1114 * so we need to select those attributes we are willing
1115 * to return, and remap them accordingly */
1116 if ( rs->sr_operational_attrs ) {
1117 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1120 return SLAP_CB_CONTINUE;
1124 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1125 * rewritten for subsequent operations; fine for plain suffixmassage,
1126 * but destroys everything else */
1128 rwm_chk_referrals( Operation *op, SlapReply *rs )
1130 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1133 #ifdef ENABLE_REWRITE
1134 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1135 #else /* ! ENABLE_REWRITE */
1137 rc = rwm_op_dn_massage( op, rs, &rc );
1138 #endif /* ! ENABLE_REWRITE */
1139 if ( rc != LDAP_SUCCESS ) {
1140 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1141 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1145 return SLAP_CB_CONTINUE;
1158 #ifdef ENABLE_REWRITE
1159 slap_overinst *on = (slap_overinst *) be->bd_info;
1160 struct ldaprwmap *rwmap =
1161 (struct ldaprwmap *)on->on_bi.bi_private;
1163 return rewrite_parse( rwmap->rwm_rw,
1164 fname, lineno, argc, argv );
1166 #else /* !ENABLE_REWRITE */
1167 fprintf( stderr, "%s: line %d: rewrite capabilities "
1168 "are not enabled\n", fname, lineno );
1169 #endif /* !ENABLE_REWRITE */
1175 rwm_suffixmassage_config(
1183 slap_overinst *on = (slap_overinst *) be->bd_info;
1184 struct ldaprwmap *rwmap =
1185 (struct ldaprwmap *)on->on_bi.bi_private;
1187 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1189 #ifdef ENABLE_REWRITE
1191 #endif /* ENABLE_REWRITE */
1196 * suffixmassage [<suffix>] <massaged suffix>
1198 * the [<suffix>] field must be defined as a valid suffix
1199 * for the current database;
1200 * the <massaged suffix> shouldn't have already been
1201 * defined as a valid suffix for the current server
1204 if ( be->be_suffix == NULL ) {
1205 fprintf( stderr, "%s: line %d: "
1206 " \"suffixMassage [<suffix>]"
1207 " <massaged suffix>\" without "
1208 "<suffix> part requires database "
1209 "suffix be defined first.\n",
1213 bvnc = be->be_suffix[ 0 ];
1216 } else if ( argc == 3 ) {
1217 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1221 fprintf( stderr, "%s: line %d: syntax is"
1222 " \"suffixMassage [<suffix>]"
1223 " <massaged suffix>\"\n",
1228 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1229 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1230 fname, lineno, bvnc.bv_val );
1234 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1235 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1236 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1237 fname, lineno, brnc.bv_val );
1238 free( nvnc.bv_val );
1239 free( pvnc.bv_val );
1243 #ifdef ENABLE_REWRITE
1245 * The suffix massaging is emulated
1246 * by means of the rewrite capabilities
1248 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1249 &pvnc, &nvnc, &prnc, &nrnc );
1250 free( nvnc.bv_val );
1251 free( pvnc.bv_val );
1252 free( nrnc.bv_val );
1253 free( prnc.bv_val );
1257 #else /* !ENABLE_REWRITE */
1258 ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1259 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1261 ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1262 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1263 #endif /* !ENABLE_REWRITE */
1277 slap_overinst *on = (slap_overinst *) be->bd_info;
1278 struct ldaprwmap *rwmap =
1279 (struct ldaprwmap *)on->on_bi.bi_private;
1281 /* objectclass/attribute mapping */
1282 return rwm_map_config( &rwmap->rwm_oc,
1284 fname, lineno, argc, argv );
1288 rwm_response( Operation *op, SlapReply *rs )
1290 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1291 struct ldaprwmap *rwmap =
1292 (struct ldaprwmap *)on->on_bi.bi_private;
1296 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1297 return rwm_send_entry( op, rs );
1300 switch( op->o_tag ) {
1301 case LDAP_REQ_SEARCH:
1302 /* Note: the operation attrs are remapped */
1303 if ( rs->sr_type == REP_RESULT
1304 && op->ors_attrs != NULL
1305 && op->ors_attrs != rs->sr_attrs )
1307 ch_free( op->ors_attrs );
1308 op->ors_attrs = rs->sr_attrs;
1314 case LDAP_REQ_DELETE:
1315 case LDAP_REQ_MODRDN:
1316 case LDAP_REQ_MODIFY:
1317 case LDAP_REQ_COMPARE:
1318 case LDAP_REQ_EXTENDED:
1323 * Rewrite the dn of the referrals, if needed
1326 #ifdef ENABLE_REWRITE
1327 dc.conn = op->o_conn;
1329 dc.ctx = "referralDN";
1330 #else /* ! ENABLE_REWRITE */
1333 #endif /* ! ENABLE_REWRITE */
1334 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1335 if ( rc != LDAP_SUCCESS ) {
1340 rc = rwm_matched( op, rs );
1344 rc = SLAP_CB_CONTINUE;
1360 slap_overinst *on = (slap_overinst *) be->bd_info;
1361 struct ldaprwmap *rwmap =
1362 (struct ldaprwmap *)on->on_bi.bi_private;
1367 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1369 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1372 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1373 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1375 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1376 rc = rwm_m_config( be, fname, lineno, argc, argv );
1378 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1379 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1381 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1384 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1389 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1390 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1392 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1393 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1396 /* TODO: not implemented yet */
1397 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1398 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1403 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1404 fname, lineno, argv[ 1 ] );
1409 rc = SLAP_CONF_UNKNOWN;
1424 slap_overinst *on = (slap_overinst *) be->bd_info;
1425 struct ldapmapping *mapping = NULL;
1426 struct ldaprwmap *rwmap;
1427 #ifdef ENABLE_REWRITE
1429 #endif /* ENABLE_REWRITE */
1431 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1433 #ifdef ENABLE_REWRITE
1434 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1435 if ( rwmap->rwm_rw == NULL ) {
1440 /* this rewriteContext by default must be null;
1441 * rules can be added if required */
1442 rargv[ 0 ] = "rewriteContext";
1443 rargv[ 1 ] = "searchFilter";
1445 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1447 rargv[ 0 ] = "rewriteContext";
1448 rargv[ 1 ] = "default";
1450 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1451 #endif /* ENABLE_REWRITE */
1453 if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1454 rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1459 on->on_bi.bi_private = (void *)rwmap;
1469 slap_overinst *on = (slap_overinst *) be->bd_info;
1472 if ( on->on_bi.bi_private ) {
1473 struct ldaprwmap *rwmap =
1474 (struct ldaprwmap *)on->on_bi.bi_private;
1476 #ifdef ENABLE_REWRITE
1477 if (rwmap->rwm_rw) {
1478 rewrite_info_delete( &rwmap->rwm_rw );
1480 #else /* !ENABLE_REWRITE */
1481 if ( rwmap->rwm_suffix_massage ) {
1482 ber_bvarray_free( rwmap->rwm_suffix_massage );
1484 #endif /* !ENABLE_REWRITE */
1486 avl_free( rwmap->rwm_oc.remap, NULL );
1487 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1488 avl_free( rwmap->rwm_at.remap, NULL );
1489 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1497 static slap_overinst rwm = { { NULL } };
1502 memset( &rwm, 0, sizeof( slap_overinst ) );
1504 rwm.on_bi.bi_type = "rwm";
1506 rwm.on_bi.bi_db_init = rwm_db_init;
1507 rwm.on_bi.bi_db_config = rwm_db_config;
1508 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1510 rwm.on_bi.bi_op_bind = rwm_op_bind;
1511 rwm.on_bi.bi_op_search = rwm_op_search;
1512 rwm.on_bi.bi_op_compare = rwm_op_compare;
1513 rwm.on_bi.bi_op_modify = rwm_op_modify;
1514 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1515 rwm.on_bi.bi_op_add = rwm_op_add;
1516 rwm.on_bi.bi_op_delete = rwm_op_delete;
1517 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1518 rwm.on_bi.bi_extended = rwm_extended;
1520 rwm.on_bi.bi_operational = rwm_operational;
1521 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1523 #ifdef ENABLE_REWRITE
1524 rwm.on_bi.bi_connection_init = rwm_conn_init;
1525 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1526 #endif /* ENABLE_REWRITE */
1528 rwm.on_response = rwm_response;
1530 return overlay_register( &rwm );
1533 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1535 init_module( int argc, char *argv[] )
1539 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1541 #endif /* SLAPD_OVER_RWM */