1 /* rwm.c - rewrite/remap operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
24 #include <ac/string.h>
30 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie )
32 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
33 struct ldaprwmap *rwmap =
34 (struct ldaprwmap *)on->on_bi.bi_private;
36 struct berval dn = BER_BVNULL,
42 * Rewrite the dn if needed
48 dc.ctx = (char *)cookie;
49 #else /* ! ENABLE_REWRITE */
50 dc.tofrom = ((int *)cookie)[0];
52 #endif /* ! ENABLE_REWRITE */
54 /* NOTE: in those cases where only the ndn is available,
55 * and the caller sets op->o_req_dn = op->o_req_ndn,
56 * only rewrite the op->o_req_ndn and use it as
57 * op->o_req_dn as well */
59 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
61 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
63 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
66 if ( rc != LDAP_SUCCESS ) {
70 if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
71 || ndn.bv_val == op->o_req_ndn.bv_val )
76 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
77 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
82 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
89 rwm_op_add( Operation *op, SlapReply *rs )
91 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
92 struct ldaprwmap *rwmap =
93 (struct ldaprwmap *)on->on_bi.bi_private;
97 Attribute **ap = NULL;
98 char *olddn = op->o_req_dn.bv_val;
101 #ifdef ENABLE_REWRITE
102 rc = rwm_op_dn_massage( op, rs, "addDN" );
103 #else /* ! ENABLE_REWRITE */
105 rc = rwm_op_dn_massage( op, rs, &rc );
106 #endif /* ! ENABLE_REWRITE */
107 if ( rc != LDAP_SUCCESS ) {
108 op->o_bd->bd_info = (BackendInfo *)on->on_info;
109 send_ldap_error( op, rs, rc, "addDN massage error" );
113 if ( olddn != op->o_req_dn.bv_val ) {
114 ch_free( op->ora_e->e_name.bv_val );
115 ch_free( op->ora_e->e_nname.bv_val );
117 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
118 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
121 /* Count number of attributes in entry */
122 isupdate = be_shadow_update( op );
123 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
126 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
127 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
131 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[ last ] ); last++ )
134 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
135 struct ldapmapping *mapping = NULL;
137 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
139 if ( mapping == NULL ) {
140 if ( rwmap->rwm_at.drop_missing ) {
141 /* FIXME: we allow to remove objectClasses as well;
142 * if the resulting entry is inconsistent, that's
143 * the relayed database's business...
145 ch_free( (*ap)->a_vals[ j ].bv_val );
147 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
149 BER_BVZERO( &(*ap)->a_vals[ last ] );
155 ch_free( (*ap)->a_vals[ j ].bv_val );
156 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
160 } else if ( !isupdate && (*ap)->a_desc->ad_type->sat_no_user_mod ) {
164 struct ldapmapping *mapping = NULL;
166 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
168 if ( mapping == NULL ) {
169 if ( rwmap->rwm_at.drop_missing ) {
174 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
175 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
178 * FIXME: rewrite could fail; in this case
179 * the operation should give up, right?
181 #ifdef ENABLE_REWRITE
182 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
184 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
185 #else /* ! ENABLE_REWRITE */
187 rc = rwm_dnattr_rewrite( op, rs, &rc, (*ap)->a_vals,
188 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
189 #endif /* ! ENABLE_REWRITE */
194 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
195 #ifdef ENABLE_REWRITE
196 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
198 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
199 #else /* ! ENABLE_REWRITE */
201 rc = rwm_referral_rewrite( op, rs, &rc, (*ap)->a_vals,
202 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
203 #endif /* ! ENABLE_REWRITE */
204 if ( rc != LDAP_SUCCESS ) {
209 if ( mapping != NULL ) {
210 assert( mapping->m_dst_ad );
211 (*ap)->a_desc = mapping->m_dst_ad;
220 /* FIXME: leaking attribute/values? */
227 /* TODO: map attribute types, values of DN-valued attributes ... */
228 return SLAP_CB_CONTINUE;
231 #ifdef ENABLE_REWRITE
233 rwm_conn_init( BackendDB *be, Connection *conn )
235 slap_overinst *on = (slap_overinst *) be->bd_info;
236 struct ldaprwmap *rwmap =
237 (struct ldaprwmap *)on->on_bi.bi_private;
239 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
241 return SLAP_CB_CONTINUE;
245 rwm_conn_destroy( BackendDB *be, Connection *conn )
247 slap_overinst *on = (slap_overinst *) be->bd_info;
248 struct ldaprwmap *rwmap =
249 (struct ldaprwmap *)on->on_bi.bi_private;
251 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
253 return SLAP_CB_CONTINUE;
255 #endif /* ENABLE_REWRITE */
258 rwm_op_bind( Operation *op, SlapReply *rs )
260 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
261 struct ldaprwmap *rwmap =
262 (struct ldaprwmap *)on->on_bi.bi_private;
265 #ifdef ENABLE_REWRITE
266 rc = rwm_op_dn_massage( op, rs, "bindDN" );
267 #else /* ! ENABLE_REWRITE */
269 rc = rwm_op_dn_massage( op, rs, &rc );
270 #endif /* ! ENABLE_REWRITE */
271 if ( rc != LDAP_SUCCESS ) {
272 op->o_bd->bd_info = (BackendInfo *)on->on_info;
273 send_ldap_error( op, rs, rc, "bindDN massage error" );
277 return SLAP_CB_CONTINUE;
281 rwm_op_unbind( Operation *op, SlapReply *rs )
283 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
284 struct ldaprwmap *rwmap =
285 (struct ldaprwmap *)on->on_bi.bi_private;
287 #ifdef ENABLE_REWRITE
288 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
289 #endif /* ENABLE_REWRITE */
291 return SLAP_CB_CONTINUE;
295 rwm_op_compare( Operation *op, SlapReply *rs )
297 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
298 struct ldaprwmap *rwmap =
299 (struct ldaprwmap *)on->on_bi.bi_private;
302 struct berval mapped_at = BER_BVNULL,
303 mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
305 #ifdef ENABLE_REWRITE
306 rc = rwm_op_dn_massage( op, rs, "compareDN" );
307 #else /* ! ENABLE_REWRITE */
309 rc = rwm_op_dn_massage( op, rs, &rc );
310 #endif /* ! ENABLE_REWRITE */
311 if ( rc != LDAP_SUCCESS ) {
312 op->o_bd->bd_info = (BackendInfo *)on->on_info;
313 send_ldap_error( op, rs, rc, "compareDN massage error" );
317 /* if the attribute is an objectClass, try to remap its value */
318 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
319 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
321 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
322 &mapped_vals[0], RWM_MAP );
323 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
325 op->o_bd->bd_info = (BackendInfo *)on->on_info;
326 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
329 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
330 free( op->orc_ava->aa_value.bv_val );
331 op->orc_ava->aa_value = mapped_vals[0];
333 mapped_at = op->orc_ava->aa_desc->ad_cname;
336 struct ldapmapping *mapping = NULL;
337 AttributeDescription *ad = op->orc_ava->aa_desc;
339 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
341 if ( mapping == NULL ) {
342 if ( rwmap->rwm_at.drop_missing ) {
343 op->o_bd->bd_info = (BackendInfo *)on->on_info;
344 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
349 assert( mapping->m_dst_ad );
350 ad = mapping->m_dst_ad;
353 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
354 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
356 struct berval *mapped_valsp[2];
358 mapped_valsp[0] = &mapped_vals[0];
359 mapped_valsp[1] = &mapped_vals[1];
361 mapped_vals[0] = op->orc_ava->aa_value;
363 #ifdef ENABLE_REWRITE
364 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
365 #else /* ! ENABLE_REWRITE */
367 rc = rwm_dnattr_rewrite( op, rs, &rc, NULL, mapped_valsp );
368 #endif /* ! ENABLE_REWRITE */
370 if ( rc != LDAP_SUCCESS ) {
371 op->o_bd->bd_info = (BackendInfo *)on->on_info;
372 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
376 op->orc_ava->aa_value = mapped_vals[0];
378 op->orc_ava->aa_desc = ad;
381 return SLAP_CB_CONTINUE;
385 rwm_op_delete( Operation *op, SlapReply *rs )
387 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
390 #ifdef ENABLE_REWRITE
391 rc = rwm_op_dn_massage( op, rs, "deleteDN" );
392 #else /* ! ENABLE_REWRITE */
394 rc = rwm_op_dn_massage( op, rs, &rc );
395 #endif /* ! ENABLE_REWRITE */
396 if ( rc != LDAP_SUCCESS ) {
397 op->o_bd->bd_info = (BackendInfo *)on->on_info;
398 send_ldap_error( op, rs, rc, "deleteDN massage error" );
402 return SLAP_CB_CONTINUE;
406 rwm_op_modify( Operation *op, SlapReply *rs )
408 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
409 struct ldaprwmap *rwmap =
410 (struct ldaprwmap *)on->on_bi.bi_private;
416 #ifdef ENABLE_REWRITE
417 rc = rwm_op_dn_massage( op, rs, "modifyDN" );
418 #else /* ! ENABLE_REWRITE */
420 rc = rwm_op_dn_massage( op, rs, &rc );
421 #endif /* ! ENABLE_REWRITE */
422 if ( rc != LDAP_SUCCESS ) {
423 op->o_bd->bd_info = (BackendInfo *)on->on_info;
424 send_ldap_error( op, rs, rc, "modifyDN massage error" );
428 isupdate = be_shadow_update( op );
429 for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
432 struct ldapmapping *mapping = NULL;
434 if ( (*mlp)->sml_desc == slap_schema.si_ad_objectClass
435 || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass )
439 } else if ( !isupdate && (*mlp)->sml_desc->ad_type->sat_no_user_mod ) {
445 drop_missing = rwm_mapping( &rwmap->rwm_at,
446 &(*mlp)->sml_desc->ad_cname,
448 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
454 if ( (*mlp)->sml_values != NULL ) {
458 for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[ last ] ); last++ )
462 for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
463 struct ldapmapping *oc_mapping = NULL;
465 ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
466 &oc_mapping, RWM_MAP );
467 if ( oc_mapping == NULL ) {
468 if ( rwmap->rwm_at.drop_missing ) {
469 /* FIXME: we allow to remove objectClasses as well;
470 * if the resulting entry is inconsistent, that's
471 * the relayed database's business...
473 ch_free( (*mlp)->sml_values[ j ].bv_val );
475 (*mlp)->sml_values[ j ] = (*mlp)->sml_values[ last ];
477 BER_BVZERO( &(*mlp)->sml_values[ last ] );
483 ch_free( (*mlp)->sml_values[ j ].bv_val );
484 ber_dupbv( &(*mlp)->sml_values[ j ], &oc_mapping->m_dst );
489 if ( (*mlp)->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
490 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
492 #ifdef ENABLE_REWRITE
493 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
495 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
496 #else /* ! ENABLE_REWRITE */
498 rc = rwm_dnattr_rewrite( op, rs, &rc,
500 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
501 #endif /* ! ENABLE_REWRITE */
503 } else if ( (*mlp)->sml_desc == slap_schema.si_ad_ref ) {
504 #ifdef ENABLE_REWRITE
505 rc = rwm_referral_rewrite( op, rs,
508 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
509 #else /* ! ENABLE_REWRITE */
511 rc = rwm_referral_rewrite( op, rs, &rc,
513 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
514 #endif /* ! ENABLE_REWRITE */
515 if ( rc != LDAP_SUCCESS ) {
520 if ( rc != LDAP_SUCCESS ) {
527 if ( mapping != NULL ) {
528 /* use new attribute description */
529 assert( mapping->m_dst_ad );
530 (*mlp)->sml_desc = mapping->m_dst_ad;
533 mlp = &(*mlp)->sml_next;
538 *mlp = (*mlp)->sml_next;
539 slap_mod_free( &ml->sml_mod, 0 );
543 return SLAP_CB_CONTINUE;
547 rwm_op_modrdn( Operation *op, SlapReply *rs )
549 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
550 struct ldaprwmap *rwmap =
551 (struct ldaprwmap *)on->on_bi.bi_private;
555 if ( op->orr_newSup ) {
557 struct berval nnewSup = BER_BVNULL;
558 struct berval newSup = BER_BVNULL;
561 * Rewrite the new superior, if defined and required
564 #ifdef ENABLE_REWRITE
565 dc.conn = op->o_conn;
567 dc.ctx = "newSuperiorDN";
568 #else /* ! ENABLE_REWRITE */
571 #endif /* ! ENABLE_REWRITE */
572 newSup = *op->orr_newSup;
573 nnewSup = *op->orr_nnewSup;
574 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
575 if ( rc != LDAP_SUCCESS ) {
576 op->o_bd->bd_info = (BackendInfo *)on->on_info;
577 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
581 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
582 op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
583 op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
584 *op->orr_newSup = newSup;
585 *op->orr_nnewSup = nnewSup;
590 * Rewrite the dn, if needed
592 #ifdef ENABLE_REWRITE
593 rc = rwm_op_dn_massage( op, rs, "renameDN" );
594 #else /* ! ENABLE_REWRITE */
596 rc = rwm_op_dn_massage( op, rs, &rc );
597 #endif /* ! ENABLE_REWRITE */
598 if ( rc != LDAP_SUCCESS ) {
599 op->o_bd->bd_info = (BackendInfo *)on->on_info;
600 send_ldap_error( op, rs, rc, "renameDN massage error" );
604 /* TODO: rewrite newRDN, attribute types,
605 * values of DN-valued attributes ... */
606 return SLAP_CB_CONTINUE;
609 static slap_callback rwm_cb;
616 ber_memfree_x( data, NULL );
619 static slap_callback *
620 rwm_callback_get( Operation *op )
624 if ( op->o_threadctx == NULL ) {
628 ldap_pvt_thread_pool_getkey( op->o_threadctx,
629 rwm_keyfree, &data, NULL );
630 if ( data == NULL ) {
631 data = ch_calloc( sizeof( slap_callback ), 1 );
632 ldap_pvt_thread_pool_setkey( op->o_threadctx,
633 rwm_keyfree, data, rwm_keyfree );
636 return (slap_callback *)data;
640 rwm_swap_attrs( Operation *op, SlapReply *rs )
642 slap_callback *cb = op->o_callback;
643 AttributeName *an = (AttributeName *)cb->sc_private;
647 return SLAP_CB_CONTINUE;
651 rwm_op_search( Operation *op, SlapReply *rs )
653 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
654 struct ldaprwmap *rwmap =
655 (struct ldaprwmap *)on->on_bi.bi_private;
660 struct berval fstr = BER_BVNULL;
663 slap_callback *cb = NULL;
664 AttributeName *an = NULL;
668 #ifdef ENABLE_REWRITE
669 rc = rwm_op_dn_massage( op, rs, "searchDN" );
670 #else /* ! ENABLE_REWRITE */
672 rc = rwm_op_dn_massage( op, rs, &rc );
673 #endif /* ! ENABLE_REWRITE */
674 if ( rc != LDAP_SUCCESS ) {
675 text = "searchDN massage error";
680 * Rewrite the dn if needed
683 #ifdef ENABLE_REWRITE
684 dc.conn = op->o_conn;
686 dc.ctx = "searchFilterAttrDN";
687 #else /* ! ENABLE_REWRITE */
690 #endif /* ! ENABLE_REWRITE */
692 rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
693 if ( rc != LDAP_SUCCESS ) {
694 text = "searchFilter/searchFilterAttrDN massage error";
698 f = str2filter_x( op, fstr.bv_val );
701 text = "massaged filter parse error";
705 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
706 ch_free( op->ors_filterstr.bv_val );
709 if( op->ors_filter ) {
710 filter_free_x( op, op->ors_filter );
714 op->ors_filterstr = fstr;
716 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
717 op->ors_attrs, &an, RWM_MAP );
718 if ( rc != LDAP_SUCCESS ) {
719 text = "attribute list mapping error";
723 cb = rwm_callback_get( op );
725 cb->sc_response = rwm_swap_attrs;
726 cb->sc_cleanup = NULL;
727 cb->sc_private = (void *)op->ors_attrs;
728 cb->sc_next = op->o_callback;
733 return SLAP_CB_CONTINUE;
741 filter_free_x( op, f );
744 if ( !BER_BVISNULL( &fstr ) ) {
745 ch_free( fstr.bv_val );
748 op->o_bd->bd_info = (BackendInfo *)on->on_info;
749 send_ldap_error( op, rs, rc, text );
756 rwm_extended( Operation *op, SlapReply *rs )
758 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
761 #ifdef ENABLE_REWRITE
762 rc = rwm_op_dn_massage( op, rs, "extendedDN" );
763 #else /* ! ENABLE_REWRITE */
765 rc = rwm_op_dn_massage( op, rs, &rc );
766 #endif /* ! ENABLE_REWRITE */
767 if ( rc != LDAP_SUCCESS ) {
768 op->o_bd->bd_info = (BackendInfo *)on->on_info;
769 send_ldap_error( op, rs, rc, "extendedDN massage error" );
773 /* TODO: rewrite/map extended data ? ... */
774 return SLAP_CB_CONTINUE;
778 rwm_matched( Operation *op, SlapReply *rs )
780 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
781 struct ldaprwmap *rwmap =
782 (struct ldaprwmap *)on->on_bi.bi_private;
784 struct berval dn, mdn;
788 if ( rs->sr_matched == NULL ) {
789 return SLAP_CB_CONTINUE;
793 #ifdef ENABLE_REWRITE
794 dc.conn = op->o_conn;
796 dc.ctx = "matchedDN";
797 #else /* ! ENABLE_REWRITE */
800 #endif /* ! ENABLE_REWRITE */
801 ber_str2bv( rs->sr_matched, 0, 0, &dn );
803 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
804 if ( rc != LDAP_SUCCESS ) {
806 rs->sr_text = "Rewrite error";
810 if ( mdn.bv_val != dn.bv_val ) {
811 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
812 ch_free( (void *)rs->sr_matched );
815 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
817 rs->sr_matched = mdn.bv_val;
820 return SLAP_CB_CONTINUE;
824 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
826 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
827 struct ldaprwmap *rwmap =
828 (struct ldaprwmap *)on->on_bi.bi_private;
836 * Rewrite the dn attrs, if needed
839 #ifdef ENABLE_REWRITE
840 dc.conn = op->o_conn;
842 #else /* ! ENABLE_REWRITE */
845 #endif /* ! ENABLE_REWRITE */
847 /* FIXME: the entries are in the remote mapping form;
848 * so we need to select those attributes we are willing
849 * to return, and remap them accordingly */
851 /* FIXME: in principle, one could map an attribute
852 * on top of another, which already exists.
853 * As such, in the end there might exist more than
854 * one instance of an attribute.
855 * We should at least check if this occurs, and issue
856 * an error (because multiple instances of attrs in
857 * response are not valid), or merge the values (what
858 * about duplicate values?) */
859 isupdate = be_shadow_update( op );
860 for ( ap = a_first; *ap; ) {
861 struct ldapmapping *mapping = NULL;
866 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
871 if ( op->ors_attrs != NULL &&
872 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
873 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
878 drop_missing = rwm_mapping( &rwmap->rwm_at,
879 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
880 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
885 if ( mapping != NULL ) {
886 (*ap)->a_desc = mapping->m_dst_ad;
890 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
891 if ( stripEntryDN ) {
892 /* will be generated by frontend */
896 } else if ( !isupdate
897 && (*ap)->a_desc->ad_type->sat_no_user_mod
898 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
903 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
907 /* empty? leave it in place because of attrsonly and vlv */
912 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
913 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
917 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
918 struct berval mapped;
920 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
921 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
922 ch_free( bv[0].bv_val );
923 BER_BVZERO( &bv[0] );
924 if ( &(*ap)->a_vals[last] > &bv[0] ) {
925 bv[0] = (*ap)->a_vals[last];
926 BER_BVZERO( &(*ap)->a_vals[last] );
931 } else if ( mapped.bv_val != bv[0].bv_val ) {
933 * FIXME: after LBER_FREEing
934 * the value is replaced by
937 ch_free( bv[0].bv_val );
938 ber_dupbv( &bv[0], &mapped );
943 * It is necessary to try to rewrite attributes with
944 * dn syntax because they might be used in ACLs as
945 * members of groups; since ACLs are applied to the
946 * rewritten stuff, no dn-based subject clause could
947 * be used at the ldap backend side (see
948 * http://www.OpenLDAP.org/faq/data/cache/452.html)
949 * The problem can be overcome by moving the dn-based
950 * ACLs to the target directory server, and letting
951 * everything pass thru the ldap backend. */
952 /* FIXME: handle distinguishedName-like syntaxes, like
953 * nameAndOptionalUID */
954 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
955 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
957 #ifdef ENABLE_REWRITE
958 dc.ctx = "searchAttrDN";
959 #endif /* ENABLE_REWRITE */
960 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
961 if ( rc != LDAP_SUCCESS ) {
965 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
966 #ifdef ENABLE_REWRITE
967 dc.ctx = "searchAttrDN";
968 #endif /* ENABLE_REWRITE */
969 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
970 if ( rc != LDAP_SUCCESS ) {
975 if ( mapping != NULL ) {
976 /* rewrite the attribute description */
977 assert( mapping->m_dst_ad );
978 (*ap)->a_desc = mapping->m_dst_ad;
996 rwm_send_entry( Operation *op, SlapReply *rs )
998 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
999 struct ldaprwmap *rwmap =
1000 (struct ldaprwmap *)on->on_bi.bi_private;
1004 struct berval dn = BER_BVNULL,
1009 assert( rs->sr_entry );
1012 * Rewrite the dn of the result, if needed
1015 #ifdef ENABLE_REWRITE
1016 dc.conn = op->o_conn;
1018 dc.ctx = "searchEntryDN";
1019 #else /* ! ENABLE_REWRITE */
1022 #endif /* ! ENABLE_REWRITE */
1025 flags = rs->sr_flags;
1026 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1027 /* FIXME: all we need to duplicate are:
1030 * - attributes that are requested
1031 * - no values if attrsonly is set
1036 rc = LDAP_NO_MEMORY;
1040 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1044 * Note: this may fail if the target host(s) schema differs
1045 * from the one known to the meta, and a DN with unknown
1046 * attributes is returned.
1050 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1051 if ( rc != LDAP_SUCCESS ) {
1056 if ( e->e_name.bv_val != dn.bv_val ) {
1057 ch_free( e->e_name.bv_val );
1058 ch_free( e->e_nname.bv_val );
1064 /* TODO: map entry attribute types, objectclasses
1065 * and dn-valued attribute values */
1067 /* FIXME: the entries are in the remote mapping form;
1068 * so we need to select those attributes we are willing
1069 * to return, and remap them accordingly */
1070 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1073 if ( rs->sr_operational_attrs ) {
1074 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1079 rs->sr_flags = flags;
1081 return SLAP_CB_CONTINUE;
1084 if ( e != NULL && e != rs->sr_entry ) {
1085 if ( e->e_name.bv_val == dn.bv_val ) {
1086 BER_BVZERO( &e->e_name );
1089 if ( e->e_nname.bv_val == ndn.bv_val ) {
1090 BER_BVZERO( &e->e_nname );
1096 if ( !BER_BVISNULL( &dn ) ) {
1097 ch_free( dn.bv_val );
1100 if ( !BER_BVISNULL( &ndn ) ) {
1101 ch_free( ndn.bv_val );
1108 rwm_operational( Operation *op, SlapReply *rs )
1110 /* FIXME: the entries are in the remote mapping form;
1111 * so we need to select those attributes we are willing
1112 * to return, and remap them accordingly */
1113 if ( rs->sr_operational_attrs ) {
1114 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1117 return SLAP_CB_CONTINUE;
1121 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1122 * rewritten for subsequent operations; fine for plain suffixmassage,
1123 * but destroys everything else */
1125 rwm_chk_referrals( Operation *op, SlapReply *rs )
1127 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1130 #ifdef ENABLE_REWRITE
1131 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1132 #else /* ! ENABLE_REWRITE */
1134 rc = rwm_op_dn_massage( op, rs, &rc );
1135 #endif /* ! ENABLE_REWRITE */
1136 if ( rc != LDAP_SUCCESS ) {
1137 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1138 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1142 return SLAP_CB_CONTINUE;
1155 #ifdef ENABLE_REWRITE
1156 slap_overinst *on = (slap_overinst *) be->bd_info;
1157 struct ldaprwmap *rwmap =
1158 (struct ldaprwmap *)on->on_bi.bi_private;
1160 return rewrite_parse( rwmap->rwm_rw,
1161 fname, lineno, argc, argv );
1163 #else /* !ENABLE_REWRITE */
1164 fprintf( stderr, "%s: line %d: rewrite capabilities "
1165 "are not enabled\n", fname, lineno );
1166 #endif /* !ENABLE_REWRITE */
1172 rwm_suffixmassage_config(
1180 slap_overinst *on = (slap_overinst *) be->bd_info;
1181 struct ldaprwmap *rwmap =
1182 (struct ldaprwmap *)on->on_bi.bi_private;
1184 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1186 #ifdef ENABLE_REWRITE
1188 #endif /* ENABLE_REWRITE */
1193 * suffixmassage [<suffix>] <massaged suffix>
1195 * the [<suffix>] field must be defined as a valid suffix
1196 * for the current database;
1197 * the <massaged suffix> shouldn't have already been
1198 * defined as a valid suffix for the current server
1201 if ( be->be_suffix == NULL ) {
1202 fprintf( stderr, "%s: line %d: "
1203 " \"suffixMassage [<suffix>]"
1204 " <massaged suffix>\" without "
1205 "<suffix> part requires database "
1206 "suffix be defined first.\n",
1210 bvnc = be->be_suffix[ 0 ];
1213 } else if ( argc == 3 ) {
1214 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1218 fprintf( stderr, "%s: line %d: syntax is"
1219 " \"suffixMassage [<suffix>]"
1220 " <massaged suffix>\"\n",
1225 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1226 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1227 fname, lineno, bvnc.bv_val );
1231 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1232 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1233 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1234 fname, lineno, brnc.bv_val );
1235 free( nvnc.bv_val );
1236 free( pvnc.bv_val );
1240 #ifdef ENABLE_REWRITE
1242 * The suffix massaging is emulated
1243 * by means of the rewrite capabilities
1245 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1246 &pvnc, &nvnc, &prnc, &nrnc );
1247 free( nvnc.bv_val );
1248 free( pvnc.bv_val );
1249 free( nrnc.bv_val );
1250 free( prnc.bv_val );
1254 #else /* !ENABLE_REWRITE */
1255 ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1256 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1258 ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1259 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1260 #endif /* !ENABLE_REWRITE */
1274 slap_overinst *on = (slap_overinst *) be->bd_info;
1275 struct ldaprwmap *rwmap =
1276 (struct ldaprwmap *)on->on_bi.bi_private;
1278 /* objectclass/attribute mapping */
1279 return rwm_map_config( &rwmap->rwm_oc,
1281 fname, lineno, argc, argv );
1285 rwm_response( Operation *op, SlapReply *rs )
1287 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1288 struct ldaprwmap *rwmap =
1289 (struct ldaprwmap *)on->on_bi.bi_private;
1293 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1294 return rwm_send_entry( op, rs );
1297 switch( op->o_tag ) {
1298 case LDAP_REQ_SEARCH:
1299 /* Note: the operation attrs are remapped */
1300 if ( rs->sr_type == REP_RESULT
1301 && op->ors_attrs != NULL
1302 && op->ors_attrs != rs->sr_attrs )
1304 ch_free( op->ors_attrs );
1305 op->ors_attrs = rs->sr_attrs;
1311 case LDAP_REQ_DELETE:
1312 case LDAP_REQ_MODRDN:
1313 case LDAP_REQ_MODIFY:
1314 case LDAP_REQ_COMPARE:
1315 case LDAP_REQ_EXTENDED:
1320 * Rewrite the dn of the referrals, if needed
1323 #ifdef ENABLE_REWRITE
1324 dc.conn = op->o_conn;
1326 dc.ctx = "referralDN";
1327 #else /* ! ENABLE_REWRITE */
1330 #endif /* ! ENABLE_REWRITE */
1331 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1332 if ( rc != LDAP_SUCCESS ) {
1337 rc = rwm_matched( op, rs );
1341 rc = SLAP_CB_CONTINUE;
1357 slap_overinst *on = (slap_overinst *) be->bd_info;
1358 struct ldaprwmap *rwmap =
1359 (struct ldaprwmap *)on->on_bi.bi_private;
1364 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1366 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1369 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1370 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1372 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1373 rc = rwm_m_config( be, fname, lineno, argc, argv );
1375 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1376 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1378 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1381 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1386 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1387 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1389 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1390 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1393 /* TODO: not implemented yet */
1394 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1395 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1400 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1401 fname, lineno, argv[ 1 ] );
1406 rc = SLAP_CONF_UNKNOWN;
1421 slap_overinst *on = (slap_overinst *) be->bd_info;
1422 struct ldapmapping *mapping = NULL;
1423 struct ldaprwmap *rwmap;
1425 rwmap = (struct ldaprwmap *)ch_malloc(sizeof(struct ldaprwmap));
1426 memset(rwmap, 0, sizeof(struct ldaprwmap));
1428 #ifdef ENABLE_REWRITE
1429 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1430 if ( rwmap->rwm_rw == NULL ) {
1438 /* this rewriteContext by default must be null;
1439 * rules can be added if required */
1440 rargv[ 0 ] = "rewriteContext";
1441 rargv[ 1 ] = "searchFilter";
1443 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1445 rargv[ 0 ] = "rewriteContext";
1446 rargv[ 1 ] = "default";
1448 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 );
1495 static slap_overinst rwm = { { NULL } };
1500 memset( &rwm, 0, sizeof( slap_overinst ) );
1502 rwm.on_bi.bi_type = "rwm";
1504 rwm.on_bi.bi_db_init = rwm_db_init;
1505 rwm.on_bi.bi_db_config = rwm_db_config;
1506 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1508 rwm.on_bi.bi_op_bind = rwm_op_bind;
1509 rwm.on_bi.bi_op_search = rwm_op_search;
1510 rwm.on_bi.bi_op_compare = rwm_op_compare;
1511 rwm.on_bi.bi_op_modify = rwm_op_modify;
1512 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1513 rwm.on_bi.bi_op_add = rwm_op_add;
1514 rwm.on_bi.bi_op_delete = rwm_op_delete;
1515 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1516 rwm.on_bi.bi_extended = rwm_extended;
1518 rwm.on_bi.bi_operational = rwm_operational;
1519 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1521 #ifdef ENABLE_REWRITE
1522 rwm.on_bi.bi_connection_init = rwm_conn_init;
1523 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1524 #endif /* ENABLE_REWRITE */
1526 rwm.on_response = rwm_response;
1528 return overlay_register( &rwm );
1531 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1533 init_module( int argc, char *argv[] )
1537 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1539 #endif /* SLAPD_OVER_RWM */