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 = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
673 "searchFilter", op->ors_filterstr.bv_val );
674 if ( rc == LDAP_SUCCESS )
675 rc = rwm_op_dn_massage( op, rs, "searchDN" );
676 #else /* ! ENABLE_REWRITE */
678 rc = rwm_op_dn_massage( op, rs, &rc );
679 #endif /* ! ENABLE_REWRITE */
680 if ( rc != LDAP_SUCCESS ) {
681 text = "searchDN massage error";
686 * Rewrite the dn if needed
689 #ifdef ENABLE_REWRITE
690 dc.conn = op->o_conn;
692 dc.ctx = "searchFilterAttrDN";
693 #else /* ! ENABLE_REWRITE */
696 #endif /* ! ENABLE_REWRITE */
698 rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
699 if ( rc != LDAP_SUCCESS ) {
700 text = "searchFilter/searchFilterAttrDN massage error";
704 f = str2filter_x( op, fstr.bv_val );
707 text = "massaged filter parse error";
711 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
712 ch_free( op->ors_filterstr.bv_val );
715 if( op->ors_filter ) {
716 filter_free_x( op, op->ors_filter );
720 op->ors_filterstr = fstr;
722 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
723 op->ors_attrs, &an, RWM_MAP );
724 if ( rc != LDAP_SUCCESS ) {
725 text = "attribute list mapping error";
729 cb = rwm_callback_get( op );
731 cb->sc_response = rwm_swap_attrs;
732 cb->sc_cleanup = NULL;
733 cb->sc_private = (void *)op->ors_attrs;
734 cb->sc_next = op->o_callback;
739 return SLAP_CB_CONTINUE;
747 filter_free_x( op, f );
750 if ( !BER_BVISNULL( &fstr ) ) {
751 ch_free( fstr.bv_val );
754 op->o_bd->bd_info = (BackendInfo *)on->on_info;
755 send_ldap_error( op, rs, rc, text );
762 rwm_extended( Operation *op, SlapReply *rs )
764 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
767 #ifdef ENABLE_REWRITE
768 rc = rwm_op_dn_massage( op, rs, "extendedDN" );
769 #else /* ! ENABLE_REWRITE */
771 rc = rwm_op_dn_massage( op, rs, &rc );
772 #endif /* ! ENABLE_REWRITE */
773 if ( rc != LDAP_SUCCESS ) {
774 op->o_bd->bd_info = (BackendInfo *)on->on_info;
775 send_ldap_error( op, rs, rc, "extendedDN massage error" );
779 /* TODO: rewrite/map extended data ? ... */
780 return SLAP_CB_CONTINUE;
784 rwm_matched( Operation *op, SlapReply *rs )
786 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
787 struct ldaprwmap *rwmap =
788 (struct ldaprwmap *)on->on_bi.bi_private;
790 struct berval dn, mdn;
794 if ( rs->sr_matched == NULL ) {
795 return SLAP_CB_CONTINUE;
799 #ifdef ENABLE_REWRITE
800 dc.conn = op->o_conn;
802 dc.ctx = "matchedDN";
803 #else /* ! ENABLE_REWRITE */
806 #endif /* ! ENABLE_REWRITE */
807 ber_str2bv( rs->sr_matched, 0, 0, &dn );
809 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
810 if ( rc != LDAP_SUCCESS ) {
812 rs->sr_text = "Rewrite error";
816 if ( mdn.bv_val != dn.bv_val ) {
817 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
818 ch_free( (void *)rs->sr_matched );
821 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
823 rs->sr_matched = mdn.bv_val;
826 return SLAP_CB_CONTINUE;
830 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
832 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
833 struct ldaprwmap *rwmap =
834 (struct ldaprwmap *)on->on_bi.bi_private;
842 * Rewrite the dn attrs, if needed
845 #ifdef ENABLE_REWRITE
846 dc.conn = op->o_conn;
848 #else /* ! ENABLE_REWRITE */
851 #endif /* ! ENABLE_REWRITE */
853 /* FIXME: the entries are in the remote mapping form;
854 * so we need to select those attributes we are willing
855 * to return, and remap them accordingly */
857 /* FIXME: in principle, one could map an attribute
858 * on top of another, which already exists.
859 * As such, in the end there might exist more than
860 * one instance of an attribute.
861 * We should at least check if this occurs, and issue
862 * an error (because multiple instances of attrs in
863 * response are not valid), or merge the values (what
864 * about duplicate values?) */
865 isupdate = be_shadow_update( op );
866 for ( ap = a_first; *ap; ) {
867 struct ldapmapping *mapping = NULL;
872 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
877 if ( op->ors_attrs != NULL &&
878 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
879 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
884 drop_missing = rwm_mapping( &rwmap->rwm_at,
885 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
886 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
891 if ( mapping != NULL ) {
892 (*ap)->a_desc = mapping->m_dst_ad;
896 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
897 if ( stripEntryDN ) {
898 /* will be generated by frontend */
902 } else if ( !isupdate
903 && !get_manageDIT( op )
904 && (*ap)->a_desc->ad_type->sat_no_user_mod
905 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
910 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
914 /* empty? leave it in place because of attrsonly and vlv */
919 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
920 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
924 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
925 struct berval mapped;
927 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
928 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
929 ch_free( bv[0].bv_val );
930 BER_BVZERO( &bv[0] );
931 if ( &(*ap)->a_vals[last] > &bv[0] ) {
932 bv[0] = (*ap)->a_vals[last];
933 BER_BVZERO( &(*ap)->a_vals[last] );
938 } else if ( mapped.bv_val != bv[0].bv_val ) {
940 * FIXME: after LBER_FREEing
941 * the value is replaced by
944 ber_bvreplace( &bv[0], &mapped );
949 * It is necessary to try to rewrite attributes with
950 * dn syntax because they might be used in ACLs as
951 * members of groups; since ACLs are applied to the
952 * rewritten stuff, no dn-based subject clause could
953 * be used at the ldap backend side (see
954 * http://www.OpenLDAP.org/faq/data/cache/452.html)
955 * The problem can be overcome by moving the dn-based
956 * ACLs to the target directory server, and letting
957 * everything pass thru the ldap backend. */
958 /* FIXME: handle distinguishedName-like syntaxes, like
959 * nameAndOptionalUID */
960 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
961 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
963 #ifdef ENABLE_REWRITE
964 dc.ctx = "searchAttrDN";
965 #endif /* ENABLE_REWRITE */
966 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
967 if ( rc != LDAP_SUCCESS ) {
971 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
972 #ifdef ENABLE_REWRITE
973 dc.ctx = "searchAttrDN";
974 #endif /* ENABLE_REWRITE */
975 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
976 if ( rc != LDAP_SUCCESS ) {
981 if ( mapping != NULL ) {
982 /* rewrite the attribute description */
983 assert( mapping->m_dst_ad != NULL );
984 (*ap)->a_desc = mapping->m_dst_ad;
1002 rwm_send_entry( Operation *op, SlapReply *rs )
1004 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1005 struct ldaprwmap *rwmap =
1006 (struct ldaprwmap *)on->on_bi.bi_private;
1010 struct berval dn = BER_BVNULL,
1015 assert( rs->sr_entry != NULL );
1018 * Rewrite the dn of the result, if needed
1021 #ifdef ENABLE_REWRITE
1022 dc.conn = op->o_conn;
1024 dc.ctx = "searchEntryDN";
1025 #else /* ! ENABLE_REWRITE */
1028 #endif /* ! ENABLE_REWRITE */
1031 flags = rs->sr_flags;
1032 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1033 /* FIXME: all we need to duplicate are:
1036 * - attributes that are requested
1037 * - no values if attrsonly is set
1042 rc = LDAP_NO_MEMORY;
1046 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1050 * Note: this may fail if the target host(s) schema differs
1051 * from the one known to the meta, and a DN with unknown
1052 * attributes is returned.
1056 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1057 if ( rc != LDAP_SUCCESS ) {
1062 if ( e->e_name.bv_val != dn.bv_val ) {
1063 ch_free( e->e_name.bv_val );
1064 ch_free( e->e_nname.bv_val );
1070 /* TODO: map entry attribute types, objectclasses
1071 * and dn-valued attribute values */
1073 /* FIXME: the entries are in the remote mapping form;
1074 * so we need to select those attributes we are willing
1075 * to return, and remap them accordingly */
1076 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1079 if ( rs->sr_operational_attrs ) {
1080 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1085 rs->sr_flags = flags;
1087 return SLAP_CB_CONTINUE;
1090 if ( e != NULL && e != rs->sr_entry ) {
1091 if ( e->e_name.bv_val == dn.bv_val ) {
1092 BER_BVZERO( &e->e_name );
1095 if ( e->e_nname.bv_val == ndn.bv_val ) {
1096 BER_BVZERO( &e->e_nname );
1102 if ( !BER_BVISNULL( &dn ) ) {
1103 ch_free( dn.bv_val );
1106 if ( !BER_BVISNULL( &ndn ) ) {
1107 ch_free( ndn.bv_val );
1114 rwm_operational( Operation *op, SlapReply *rs )
1116 /* FIXME: the entries are in the remote mapping form;
1117 * so we need to select those attributes we are willing
1118 * to return, and remap them accordingly */
1119 if ( rs->sr_operational_attrs ) {
1120 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1123 return SLAP_CB_CONTINUE;
1127 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1128 * rewritten for subsequent operations; fine for plain suffixmassage,
1129 * but destroys everything else */
1131 rwm_chk_referrals( Operation *op, SlapReply *rs )
1133 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1136 #ifdef ENABLE_REWRITE
1137 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1138 #else /* ! ENABLE_REWRITE */
1140 rc = rwm_op_dn_massage( op, rs, &rc );
1141 #endif /* ! ENABLE_REWRITE */
1142 if ( rc != LDAP_SUCCESS ) {
1143 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1144 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1148 return SLAP_CB_CONTINUE;
1161 #ifdef ENABLE_REWRITE
1162 slap_overinst *on = (slap_overinst *) be->bd_info;
1163 struct ldaprwmap *rwmap =
1164 (struct ldaprwmap *)on->on_bi.bi_private;
1166 return rewrite_parse( rwmap->rwm_rw,
1167 fname, lineno, argc, argv );
1169 #else /* !ENABLE_REWRITE */
1170 fprintf( stderr, "%s: line %d: rewrite capabilities "
1171 "are not enabled\n", fname, lineno );
1172 #endif /* !ENABLE_REWRITE */
1178 rwm_suffixmassage_config(
1186 slap_overinst *on = (slap_overinst *) be->bd_info;
1187 struct ldaprwmap *rwmap =
1188 (struct ldaprwmap *)on->on_bi.bi_private;
1190 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1192 #ifdef ENABLE_REWRITE
1194 #endif /* ENABLE_REWRITE */
1199 * suffixmassage [<suffix>] <massaged suffix>
1201 * the [<suffix>] field must be defined as a valid suffix
1202 * for the current database;
1203 * the <massaged suffix> shouldn't have already been
1204 * defined as a valid suffix for the current server
1207 if ( be->be_suffix == NULL ) {
1208 fprintf( stderr, "%s: line %d: "
1209 " \"suffixMassage [<suffix>]"
1210 " <massaged suffix>\" without "
1211 "<suffix> part requires database "
1212 "suffix be defined first.\n",
1216 bvnc = be->be_suffix[ 0 ];
1219 } else if ( argc == 3 ) {
1220 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1224 fprintf( stderr, "%s: line %d: syntax is"
1225 " \"suffixMassage [<suffix>]"
1226 " <massaged suffix>\"\n",
1231 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1232 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1233 fname, lineno, bvnc.bv_val );
1237 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1238 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1239 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1240 fname, lineno, brnc.bv_val );
1241 free( nvnc.bv_val );
1242 free( pvnc.bv_val );
1246 #ifdef ENABLE_REWRITE
1248 * The suffix massaging is emulated
1249 * by means of the rewrite capabilities
1251 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1252 &pvnc, &nvnc, &prnc, &nrnc );
1253 free( nvnc.bv_val );
1254 free( pvnc.bv_val );
1255 free( nrnc.bv_val );
1256 free( prnc.bv_val );
1260 #else /* !ENABLE_REWRITE */
1261 ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1262 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1264 ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1265 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1266 #endif /* !ENABLE_REWRITE */
1280 slap_overinst *on = (slap_overinst *) be->bd_info;
1281 struct ldaprwmap *rwmap =
1282 (struct ldaprwmap *)on->on_bi.bi_private;
1284 /* objectclass/attribute mapping */
1285 return rwm_map_config( &rwmap->rwm_oc,
1287 fname, lineno, argc, argv );
1291 rwm_response( Operation *op, SlapReply *rs )
1293 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1294 struct ldaprwmap *rwmap =
1295 (struct ldaprwmap *)on->on_bi.bi_private;
1299 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1300 return rwm_send_entry( op, rs );
1303 switch( op->o_tag ) {
1304 case LDAP_REQ_SEARCH:
1305 /* Note: the operation attrs are remapped */
1306 if ( rs->sr_type == REP_RESULT
1307 && op->ors_attrs != NULL
1308 && op->ors_attrs != rs->sr_attrs )
1310 ch_free( op->ors_attrs );
1311 op->ors_attrs = rs->sr_attrs;
1317 case LDAP_REQ_DELETE:
1318 case LDAP_REQ_MODRDN:
1319 case LDAP_REQ_MODIFY:
1320 case LDAP_REQ_COMPARE:
1321 case LDAP_REQ_EXTENDED:
1326 * Rewrite the dn of the referrals, if needed
1329 #ifdef ENABLE_REWRITE
1330 dc.conn = op->o_conn;
1332 dc.ctx = "referralDN";
1333 #else /* ! ENABLE_REWRITE */
1336 #endif /* ! ENABLE_REWRITE */
1337 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1338 if ( rc != LDAP_SUCCESS ) {
1343 rc = rwm_matched( op, rs );
1347 rc = SLAP_CB_CONTINUE;
1363 slap_overinst *on = (slap_overinst *) be->bd_info;
1364 struct ldaprwmap *rwmap =
1365 (struct ldaprwmap *)on->on_bi.bi_private;
1370 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1372 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1375 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1376 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1378 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1379 rc = rwm_m_config( be, fname, lineno, argc, argv );
1381 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1382 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1384 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1387 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1392 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1393 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1395 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1396 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1399 /* TODO: not implemented yet */
1400 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1401 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1406 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1407 fname, lineno, argv[ 1 ] );
1412 rc = SLAP_CONF_UNKNOWN;
1427 slap_overinst *on = (slap_overinst *) be->bd_info;
1428 struct ldapmapping *mapping = NULL;
1429 struct ldaprwmap *rwmap;
1430 #ifdef ENABLE_REWRITE
1432 #endif /* ENABLE_REWRITE */
1434 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1436 #ifdef ENABLE_REWRITE
1437 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1438 if ( rwmap->rwm_rw == NULL ) {
1443 /* this rewriteContext by default must be null;
1444 * rules can be added if required */
1445 rargv[ 0 ] = "rewriteContext";
1446 rargv[ 1 ] = "searchFilter";
1448 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1450 rargv[ 0 ] = "rewriteContext";
1451 rargv[ 1 ] = "default";
1453 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1454 #endif /* ENABLE_REWRITE */
1456 if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1457 rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1462 on->on_bi.bi_private = (void *)rwmap;
1472 slap_overinst *on = (slap_overinst *) be->bd_info;
1475 if ( on->on_bi.bi_private ) {
1476 struct ldaprwmap *rwmap =
1477 (struct ldaprwmap *)on->on_bi.bi_private;
1479 #ifdef ENABLE_REWRITE
1480 if (rwmap->rwm_rw) {
1481 rewrite_info_delete( &rwmap->rwm_rw );
1483 #else /* !ENABLE_REWRITE */
1484 if ( rwmap->rwm_suffix_massage ) {
1485 ber_bvarray_free( rwmap->rwm_suffix_massage );
1487 #endif /* !ENABLE_REWRITE */
1489 avl_free( rwmap->rwm_oc.remap, NULL );
1490 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1491 avl_free( rwmap->rwm_at.remap, NULL );
1492 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1500 static slap_overinst rwm = { { NULL } };
1505 memset( &rwm, 0, sizeof( slap_overinst ) );
1507 rwm.on_bi.bi_type = "rwm";
1509 rwm.on_bi.bi_db_init = rwm_db_init;
1510 rwm.on_bi.bi_db_config = rwm_db_config;
1511 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1513 rwm.on_bi.bi_op_bind = rwm_op_bind;
1514 rwm.on_bi.bi_op_search = rwm_op_search;
1515 rwm.on_bi.bi_op_compare = rwm_op_compare;
1516 rwm.on_bi.bi_op_modify = rwm_op_modify;
1517 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1518 rwm.on_bi.bi_op_add = rwm_op_add;
1519 rwm.on_bi.bi_op_delete = rwm_op_delete;
1520 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1521 rwm.on_bi.bi_extended = rwm_extended;
1523 rwm.on_bi.bi_operational = rwm_operational;
1524 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1526 #ifdef ENABLE_REWRITE
1527 rwm.on_bi.bi_connection_init = rwm_conn_init;
1528 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1529 #endif /* ENABLE_REWRITE */
1531 rwm.on_response = rwm_response;
1533 return overlay_register( &rwm );
1536 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1538 init_module( int argc, char *argv[] )
1542 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1544 #endif /* SLAPD_OVER_RWM */