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 && (*ap)->a_desc->ad_type->sat_no_user_mod ) {
161 struct ldapmapping *mapping = NULL;
163 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
165 if ( mapping == NULL ) {
166 if ( rwmap->rwm_at.drop_missing ) {
171 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
172 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
175 * FIXME: rewrite could fail; in this case
176 * the operation should give up, right?
178 #ifdef ENABLE_REWRITE
179 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
181 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
182 #else /* ! ENABLE_REWRITE */
184 rc = rwm_dnattr_rewrite( op, rs, &rc, (*ap)->a_vals,
185 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
186 #endif /* ! ENABLE_REWRITE */
191 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
192 #ifdef ENABLE_REWRITE
193 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
195 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
196 #else /* ! ENABLE_REWRITE */
198 rc = rwm_referral_rewrite( op, rs, &rc, (*ap)->a_vals,
199 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
200 #endif /* ! ENABLE_REWRITE */
201 if ( rc != LDAP_SUCCESS ) {
206 if ( mapping != NULL ) {
207 assert( mapping->m_dst_ad != NULL );
208 (*ap)->a_desc = mapping->m_dst_ad;
217 /* FIXME: leaking attribute/values? */
224 /* TODO: map attribute types, values of DN-valued attributes ... */
225 return SLAP_CB_CONTINUE;
228 #ifdef ENABLE_REWRITE
230 rwm_conn_init( BackendDB *be, Connection *conn )
232 slap_overinst *on = (slap_overinst *) be->bd_info;
233 struct ldaprwmap *rwmap =
234 (struct ldaprwmap *)on->on_bi.bi_private;
236 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
238 return SLAP_CB_CONTINUE;
242 rwm_conn_destroy( BackendDB *be, Connection *conn )
244 slap_overinst *on = (slap_overinst *) be->bd_info;
245 struct ldaprwmap *rwmap =
246 (struct ldaprwmap *)on->on_bi.bi_private;
248 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
250 return SLAP_CB_CONTINUE;
252 #endif /* ENABLE_REWRITE */
255 rwm_op_bind( Operation *op, SlapReply *rs )
257 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
260 #ifdef ENABLE_REWRITE
261 rc = rwm_op_dn_massage( op, rs, "bindDN" );
262 #else /* ! ENABLE_REWRITE */
264 rc = rwm_op_dn_massage( op, rs, &rc );
265 #endif /* ! ENABLE_REWRITE */
266 if ( rc != LDAP_SUCCESS ) {
267 op->o_bd->bd_info = (BackendInfo *)on->on_info;
268 send_ldap_error( op, rs, rc, "bindDN massage error" );
272 return SLAP_CB_CONTINUE;
276 rwm_op_unbind( Operation *op, SlapReply *rs )
278 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
279 struct ldaprwmap *rwmap =
280 (struct ldaprwmap *)on->on_bi.bi_private;
282 #ifdef ENABLE_REWRITE
283 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
284 #endif /* ENABLE_REWRITE */
286 return SLAP_CB_CONTINUE;
290 rwm_op_compare( Operation *op, SlapReply *rs )
292 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
293 struct ldaprwmap *rwmap =
294 (struct ldaprwmap *)on->on_bi.bi_private;
297 struct berval mapped_at = BER_BVNULL,
298 mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
300 #ifdef ENABLE_REWRITE
301 rc = rwm_op_dn_massage( op, rs, "compareDN" );
302 #else /* ! ENABLE_REWRITE */
304 rc = rwm_op_dn_massage( op, rs, &rc );
305 #endif /* ! ENABLE_REWRITE */
306 if ( rc != LDAP_SUCCESS ) {
307 op->o_bd->bd_info = (BackendInfo *)on->on_info;
308 send_ldap_error( op, rs, rc, "compareDN massage error" );
312 /* if the attribute is an objectClass, try to remap its value */
313 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
314 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
316 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
317 &mapped_vals[0], RWM_MAP );
318 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
320 op->o_bd->bd_info = (BackendInfo *)on->on_info;
321 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
324 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
325 ber_bvreplace_x( &op->orc_ava->aa_value, &mapped_vals[0], op->o_tmpmemctx );
327 mapped_at = op->orc_ava->aa_desc->ad_cname;
330 struct ldapmapping *mapping = NULL;
331 AttributeDescription *ad = op->orc_ava->aa_desc;
333 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
335 if ( mapping == NULL ) {
336 if ( rwmap->rwm_at.drop_missing ) {
337 op->o_bd->bd_info = (BackendInfo *)on->on_info;
338 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
343 assert( mapping->m_dst_ad != NULL );
344 ad = mapping->m_dst_ad;
347 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
348 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
350 struct berval *mapped_valsp[2];
352 mapped_valsp[0] = &mapped_vals[0];
353 mapped_valsp[1] = &mapped_vals[1];
355 mapped_vals[0] = op->orc_ava->aa_value;
357 #ifdef ENABLE_REWRITE
358 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
359 #else /* ! ENABLE_REWRITE */
361 rc = rwm_dnattr_rewrite( op, rs, &rc, NULL, mapped_valsp );
362 #endif /* ! ENABLE_REWRITE */
364 if ( rc != LDAP_SUCCESS ) {
365 op->o_bd->bd_info = (BackendInfo *)on->on_info;
366 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
370 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
371 ber_bvreplace_x( &op->orc_ava->aa_value, &mapped_vals[0],
375 op->orc_ava->aa_desc = ad;
378 return SLAP_CB_CONTINUE;
382 rwm_op_delete( Operation *op, SlapReply *rs )
384 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
387 #ifdef ENABLE_REWRITE
388 rc = rwm_op_dn_massage( op, rs, "deleteDN" );
389 #else /* ! ENABLE_REWRITE */
391 rc = rwm_op_dn_massage( op, rs, &rc );
392 #endif /* ! ENABLE_REWRITE */
393 if ( rc != LDAP_SUCCESS ) {
394 op->o_bd->bd_info = (BackendInfo *)on->on_info;
395 send_ldap_error( op, rs, rc, "deleteDN massage error" );
399 return SLAP_CB_CONTINUE;
403 rwm_op_modify( Operation *op, SlapReply *rs )
405 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
406 struct ldaprwmap *rwmap =
407 (struct ldaprwmap *)on->on_bi.bi_private;
413 #ifdef ENABLE_REWRITE
414 rc = rwm_op_dn_massage( op, rs, "modifyDN" );
415 #else /* ! ENABLE_REWRITE */
417 rc = rwm_op_dn_massage( op, rs, &rc );
418 #endif /* ! ENABLE_REWRITE */
419 if ( rc != LDAP_SUCCESS ) {
420 op->o_bd->bd_info = (BackendInfo *)on->on_info;
421 send_ldap_error( op, rs, rc, "modifyDN massage error" );
425 isupdate = be_shadow_update( op );
426 for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
429 struct ldapmapping *mapping = NULL;
431 if ( (*mlp)->sml_desc == slap_schema.si_ad_objectClass
432 || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass )
436 } else if ( !isupdate && (*mlp)->sml_desc->ad_type->sat_no_user_mod ) {
442 drop_missing = rwm_mapping( &rwmap->rwm_at,
443 &(*mlp)->sml_desc->ad_cname,
445 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
451 if ( (*mlp)->sml_values != NULL ) {
455 for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[ last ] ); last++ )
459 for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
460 struct ldapmapping *oc_mapping = NULL;
462 ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
463 &oc_mapping, RWM_MAP );
464 if ( oc_mapping == NULL ) {
465 if ( rwmap->rwm_at.drop_missing ) {
466 /* FIXME: we allow to remove objectClasses as well;
467 * if the resulting entry is inconsistent, that's
468 * the relayed database's business...
470 ch_free( (*mlp)->sml_values[ j ].bv_val );
472 (*mlp)->sml_values[ j ] = (*mlp)->sml_values[ last ];
474 BER_BVZERO( &(*mlp)->sml_values[ last ] );
480 ch_free( (*mlp)->sml_values[ j ].bv_val );
481 ber_dupbv( &(*mlp)->sml_values[ j ], &oc_mapping->m_dst );
486 if ( (*mlp)->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
487 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
489 #ifdef ENABLE_REWRITE
490 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
492 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
493 #else /* ! ENABLE_REWRITE */
495 rc = rwm_dnattr_rewrite( op, rs, &rc,
497 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
498 #endif /* ! ENABLE_REWRITE */
500 } else if ( (*mlp)->sml_desc == slap_schema.si_ad_ref ) {
501 #ifdef ENABLE_REWRITE
502 rc = rwm_referral_rewrite( op, rs,
505 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
506 #else /* ! ENABLE_REWRITE */
508 rc = rwm_referral_rewrite( op, rs, &rc,
510 (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
511 #endif /* ! ENABLE_REWRITE */
512 if ( rc != LDAP_SUCCESS ) {
517 if ( rc != LDAP_SUCCESS ) {
524 if ( mapping != NULL ) {
525 /* use new attribute description */
526 assert( mapping->m_dst_ad != NULL );
527 (*mlp)->sml_desc = mapping->m_dst_ad;
530 mlp = &(*mlp)->sml_next;
535 *mlp = (*mlp)->sml_next;
536 slap_mod_free( &ml->sml_mod, 0 );
540 return SLAP_CB_CONTINUE;
544 rwm_op_modrdn( Operation *op, SlapReply *rs )
546 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
547 struct ldaprwmap *rwmap =
548 (struct ldaprwmap *)on->on_bi.bi_private;
552 if ( op->orr_newSup ) {
554 struct berval nnewSup = BER_BVNULL;
555 struct berval newSup = BER_BVNULL;
558 * Rewrite the new superior, if defined and required
561 #ifdef ENABLE_REWRITE
562 dc.conn = op->o_conn;
564 dc.ctx = "newSuperiorDN";
565 #else /* ! ENABLE_REWRITE */
568 #endif /* ! ENABLE_REWRITE */
569 newSup = *op->orr_newSup;
570 nnewSup = *op->orr_nnewSup;
571 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
572 if ( rc != LDAP_SUCCESS ) {
573 op->o_bd->bd_info = (BackendInfo *)on->on_info;
574 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
578 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
579 op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
580 op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
581 *op->orr_newSup = newSup;
582 *op->orr_nnewSup = nnewSup;
587 * Rewrite the dn, if needed
589 #ifdef ENABLE_REWRITE
590 rc = rwm_op_dn_massage( op, rs, "renameDN" );
591 #else /* ! ENABLE_REWRITE */
593 rc = rwm_op_dn_massage( op, rs, &rc );
594 #endif /* ! ENABLE_REWRITE */
595 if ( rc != LDAP_SUCCESS ) {
596 op->o_bd->bd_info = (BackendInfo *)on->on_info;
597 send_ldap_error( op, rs, rc, "renameDN massage error" );
601 /* TODO: rewrite newRDN, attribute types,
602 * values of DN-valued attributes ... */
603 return SLAP_CB_CONTINUE;
606 static slap_callback rwm_cb;
613 ber_memfree_x( data, NULL );
616 static slap_callback *
617 rwm_callback_get( Operation *op )
621 if ( op->o_threadctx == NULL ) {
625 ldap_pvt_thread_pool_getkey( op->o_threadctx,
626 rwm_keyfree, &data, NULL );
627 if ( data == NULL ) {
628 data = ch_calloc( sizeof( slap_callback ), 1 );
629 ldap_pvt_thread_pool_setkey( op->o_threadctx,
630 rwm_keyfree, data, rwm_keyfree );
633 return (slap_callback *)data;
637 rwm_swap_attrs( Operation *op, SlapReply *rs )
639 slap_callback *cb = op->o_callback;
640 AttributeName *an = (AttributeName *)cb->sc_private;
644 return SLAP_CB_CONTINUE;
648 rwm_op_search( Operation *op, SlapReply *rs )
650 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
651 struct ldaprwmap *rwmap =
652 (struct ldaprwmap *)on->on_bi.bi_private;
657 struct berval fstr = BER_BVNULL;
660 slap_callback *cb = NULL;
661 AttributeName *an = NULL;
665 #ifdef ENABLE_REWRITE
666 rc = rwm_op_dn_massage( op, rs, "searchDN" );
667 #else /* ! ENABLE_REWRITE */
669 rc = rwm_op_dn_massage( op, rs, &rc );
670 #endif /* ! ENABLE_REWRITE */
671 if ( rc != LDAP_SUCCESS ) {
672 text = "searchDN massage error";
677 * Rewrite the dn if needed
680 #ifdef ENABLE_REWRITE
681 dc.conn = op->o_conn;
683 dc.ctx = "searchFilterAttrDN";
684 #else /* ! ENABLE_REWRITE */
687 #endif /* ! ENABLE_REWRITE */
689 rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
690 if ( rc != LDAP_SUCCESS ) {
691 text = "searchFilter/searchFilterAttrDN massage error";
695 f = str2filter_x( op, fstr.bv_val );
698 text = "massaged filter parse error";
702 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
703 ch_free( op->ors_filterstr.bv_val );
706 if( op->ors_filter ) {
707 filter_free_x( op, op->ors_filter );
711 op->ors_filterstr = fstr;
713 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
714 op->ors_attrs, &an, RWM_MAP );
715 if ( rc != LDAP_SUCCESS ) {
716 text = "attribute list mapping error";
720 cb = rwm_callback_get( op );
722 cb->sc_response = rwm_swap_attrs;
723 cb->sc_cleanup = NULL;
724 cb->sc_private = (void *)op->ors_attrs;
725 cb->sc_next = op->o_callback;
730 return SLAP_CB_CONTINUE;
738 filter_free_x( op, f );
741 if ( !BER_BVISNULL( &fstr ) ) {
742 ch_free( fstr.bv_val );
745 op->o_bd->bd_info = (BackendInfo *)on->on_info;
746 send_ldap_error( op, rs, rc, text );
753 rwm_extended( Operation *op, SlapReply *rs )
755 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
758 #ifdef ENABLE_REWRITE
759 rc = rwm_op_dn_massage( op, rs, "extendedDN" );
760 #else /* ! ENABLE_REWRITE */
762 rc = rwm_op_dn_massage( op, rs, &rc );
763 #endif /* ! ENABLE_REWRITE */
764 if ( rc != LDAP_SUCCESS ) {
765 op->o_bd->bd_info = (BackendInfo *)on->on_info;
766 send_ldap_error( op, rs, rc, "extendedDN massage error" );
770 /* TODO: rewrite/map extended data ? ... */
771 return SLAP_CB_CONTINUE;
775 rwm_matched( Operation *op, SlapReply *rs )
777 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
778 struct ldaprwmap *rwmap =
779 (struct ldaprwmap *)on->on_bi.bi_private;
781 struct berval dn, mdn;
785 if ( rs->sr_matched == NULL ) {
786 return SLAP_CB_CONTINUE;
790 #ifdef ENABLE_REWRITE
791 dc.conn = op->o_conn;
793 dc.ctx = "matchedDN";
794 #else /* ! ENABLE_REWRITE */
797 #endif /* ! ENABLE_REWRITE */
798 ber_str2bv( rs->sr_matched, 0, 0, &dn );
800 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
801 if ( rc != LDAP_SUCCESS ) {
803 rs->sr_text = "Rewrite error";
807 if ( mdn.bv_val != dn.bv_val ) {
808 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
809 ch_free( (void *)rs->sr_matched );
812 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
814 rs->sr_matched = mdn.bv_val;
817 return SLAP_CB_CONTINUE;
821 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
823 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
824 struct ldaprwmap *rwmap =
825 (struct ldaprwmap *)on->on_bi.bi_private;
833 * Rewrite the dn attrs, if needed
836 #ifdef ENABLE_REWRITE
837 dc.conn = op->o_conn;
839 #else /* ! ENABLE_REWRITE */
842 #endif /* ! ENABLE_REWRITE */
844 /* FIXME: the entries are in the remote mapping form;
845 * so we need to select those attributes we are willing
846 * to return, and remap them accordingly */
848 /* FIXME: in principle, one could map an attribute
849 * on top of another, which already exists.
850 * As such, in the end there might exist more than
851 * one instance of an attribute.
852 * We should at least check if this occurs, and issue
853 * an error (because multiple instances of attrs in
854 * response are not valid), or merge the values (what
855 * about duplicate values?) */
856 isupdate = be_shadow_update( op );
857 for ( ap = a_first; *ap; ) {
858 struct ldapmapping *mapping = NULL;
863 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
868 if ( op->ors_attrs != NULL &&
869 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
870 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
875 drop_missing = rwm_mapping( &rwmap->rwm_at,
876 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
877 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
882 if ( mapping != NULL ) {
883 (*ap)->a_desc = mapping->m_dst_ad;
887 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
888 if ( stripEntryDN ) {
889 /* will be generated by frontend */
893 } else if ( !isupdate
894 && (*ap)->a_desc->ad_type->sat_no_user_mod
895 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
900 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
904 /* empty? leave it in place because of attrsonly and vlv */
909 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
910 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
914 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
915 struct berval mapped;
917 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
918 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
919 ch_free( bv[0].bv_val );
920 BER_BVZERO( &bv[0] );
921 if ( &(*ap)->a_vals[last] > &bv[0] ) {
922 bv[0] = (*ap)->a_vals[last];
923 BER_BVZERO( &(*ap)->a_vals[last] );
928 } else if ( mapped.bv_val != bv[0].bv_val ) {
930 * FIXME: after LBER_FREEing
931 * the value is replaced by
934 ber_bvreplace( &bv[0], &mapped );
939 * It is necessary to try to rewrite attributes with
940 * dn syntax because they might be used in ACLs as
941 * members of groups; since ACLs are applied to the
942 * rewritten stuff, no dn-based subject clause could
943 * be used at the ldap backend side (see
944 * http://www.OpenLDAP.org/faq/data/cache/452.html)
945 * The problem can be overcome by moving the dn-based
946 * ACLs to the target directory server, and letting
947 * everything pass thru the ldap backend. */
948 /* FIXME: handle distinguishedName-like syntaxes, like
949 * nameAndOptionalUID */
950 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
951 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
953 #ifdef ENABLE_REWRITE
954 dc.ctx = "searchAttrDN";
955 #endif /* ENABLE_REWRITE */
956 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
957 if ( rc != LDAP_SUCCESS ) {
961 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
962 #ifdef ENABLE_REWRITE
963 dc.ctx = "searchAttrDN";
964 #endif /* ENABLE_REWRITE */
965 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
966 if ( rc != LDAP_SUCCESS ) {
971 if ( mapping != NULL ) {
972 /* rewrite the attribute description */
973 assert( mapping->m_dst_ad != NULL );
974 (*ap)->a_desc = mapping->m_dst_ad;
992 rwm_send_entry( Operation *op, SlapReply *rs )
994 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
995 struct ldaprwmap *rwmap =
996 (struct ldaprwmap *)on->on_bi.bi_private;
1000 struct berval dn = BER_BVNULL,
1005 assert( rs->sr_entry != NULL );
1008 * Rewrite the dn of the result, if needed
1011 #ifdef ENABLE_REWRITE
1012 dc.conn = op->o_conn;
1014 dc.ctx = "searchEntryDN";
1015 #else /* ! ENABLE_REWRITE */
1018 #endif /* ! ENABLE_REWRITE */
1021 flags = rs->sr_flags;
1022 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1023 /* FIXME: all we need to duplicate are:
1026 * - attributes that are requested
1027 * - no values if attrsonly is set
1032 rc = LDAP_NO_MEMORY;
1036 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1040 * Note: this may fail if the target host(s) schema differs
1041 * from the one known to the meta, and a DN with unknown
1042 * attributes is returned.
1046 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1047 if ( rc != LDAP_SUCCESS ) {
1052 if ( e->e_name.bv_val != dn.bv_val ) {
1053 ch_free( e->e_name.bv_val );
1054 ch_free( e->e_nname.bv_val );
1060 /* TODO: map entry attribute types, objectclasses
1061 * and dn-valued attribute values */
1063 /* FIXME: the entries are in the remote mapping form;
1064 * so we need to select those attributes we are willing
1065 * to return, and remap them accordingly */
1066 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1069 if ( rs->sr_operational_attrs ) {
1070 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1075 rs->sr_flags = flags;
1077 return SLAP_CB_CONTINUE;
1080 if ( e != NULL && e != rs->sr_entry ) {
1081 if ( e->e_name.bv_val == dn.bv_val ) {
1082 BER_BVZERO( &e->e_name );
1085 if ( e->e_nname.bv_val == ndn.bv_val ) {
1086 BER_BVZERO( &e->e_nname );
1092 if ( !BER_BVISNULL( &dn ) ) {
1093 ch_free( dn.bv_val );
1096 if ( !BER_BVISNULL( &ndn ) ) {
1097 ch_free( ndn.bv_val );
1104 rwm_operational( Operation *op, SlapReply *rs )
1106 /* FIXME: the entries are in the remote mapping form;
1107 * so we need to select those attributes we are willing
1108 * to return, and remap them accordingly */
1109 if ( rs->sr_operational_attrs ) {
1110 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1113 return SLAP_CB_CONTINUE;
1117 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1118 * rewritten for subsequent operations; fine for plain suffixmassage,
1119 * but destroys everything else */
1121 rwm_chk_referrals( Operation *op, SlapReply *rs )
1123 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1126 #ifdef ENABLE_REWRITE
1127 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1128 #else /* ! ENABLE_REWRITE */
1130 rc = rwm_op_dn_massage( op, rs, &rc );
1131 #endif /* ! ENABLE_REWRITE */
1132 if ( rc != LDAP_SUCCESS ) {
1133 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1134 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1138 return SLAP_CB_CONTINUE;
1151 #ifdef ENABLE_REWRITE
1152 slap_overinst *on = (slap_overinst *) be->bd_info;
1153 struct ldaprwmap *rwmap =
1154 (struct ldaprwmap *)on->on_bi.bi_private;
1156 return rewrite_parse( rwmap->rwm_rw,
1157 fname, lineno, argc, argv );
1159 #else /* !ENABLE_REWRITE */
1160 fprintf( stderr, "%s: line %d: rewrite capabilities "
1161 "are not enabled\n", fname, lineno );
1162 #endif /* !ENABLE_REWRITE */
1168 rwm_suffixmassage_config(
1176 slap_overinst *on = (slap_overinst *) be->bd_info;
1177 struct ldaprwmap *rwmap =
1178 (struct ldaprwmap *)on->on_bi.bi_private;
1180 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1182 #ifdef ENABLE_REWRITE
1184 #endif /* ENABLE_REWRITE */
1189 * suffixmassage [<suffix>] <massaged suffix>
1191 * the [<suffix>] field must be defined as a valid suffix
1192 * for the current database;
1193 * the <massaged suffix> shouldn't have already been
1194 * defined as a valid suffix for the current server
1197 if ( be->be_suffix == NULL ) {
1198 fprintf( stderr, "%s: line %d: "
1199 " \"suffixMassage [<suffix>]"
1200 " <massaged suffix>\" without "
1201 "<suffix> part requires database "
1202 "suffix be defined first.\n",
1206 bvnc = be->be_suffix[ 0 ];
1209 } else if ( argc == 3 ) {
1210 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1214 fprintf( stderr, "%s: line %d: syntax is"
1215 " \"suffixMassage [<suffix>]"
1216 " <massaged suffix>\"\n",
1221 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1222 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1223 fname, lineno, bvnc.bv_val );
1227 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1228 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1229 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1230 fname, lineno, brnc.bv_val );
1231 free( nvnc.bv_val );
1232 free( pvnc.bv_val );
1236 #ifdef ENABLE_REWRITE
1238 * The suffix massaging is emulated
1239 * by means of the rewrite capabilities
1241 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1242 &pvnc, &nvnc, &prnc, &nrnc );
1243 free( nvnc.bv_val );
1244 free( pvnc.bv_val );
1245 free( nrnc.bv_val );
1246 free( prnc.bv_val );
1250 #else /* !ENABLE_REWRITE */
1251 ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1252 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1254 ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1255 ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1256 #endif /* !ENABLE_REWRITE */
1270 slap_overinst *on = (slap_overinst *) be->bd_info;
1271 struct ldaprwmap *rwmap =
1272 (struct ldaprwmap *)on->on_bi.bi_private;
1274 /* objectclass/attribute mapping */
1275 return rwm_map_config( &rwmap->rwm_oc,
1277 fname, lineno, argc, argv );
1281 rwm_response( Operation *op, SlapReply *rs )
1283 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1284 struct ldaprwmap *rwmap =
1285 (struct ldaprwmap *)on->on_bi.bi_private;
1289 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1290 return rwm_send_entry( op, rs );
1293 switch( op->o_tag ) {
1294 case LDAP_REQ_SEARCH:
1295 /* Note: the operation attrs are remapped */
1296 if ( rs->sr_type == REP_RESULT
1297 && op->ors_attrs != NULL
1298 && op->ors_attrs != rs->sr_attrs )
1300 ch_free( op->ors_attrs );
1301 op->ors_attrs = rs->sr_attrs;
1307 case LDAP_REQ_DELETE:
1308 case LDAP_REQ_MODRDN:
1309 case LDAP_REQ_MODIFY:
1310 case LDAP_REQ_COMPARE:
1311 case LDAP_REQ_EXTENDED:
1316 * Rewrite the dn of the referrals, if needed
1319 #ifdef ENABLE_REWRITE
1320 dc.conn = op->o_conn;
1322 dc.ctx = "referralDN";
1323 #else /* ! ENABLE_REWRITE */
1326 #endif /* ! ENABLE_REWRITE */
1327 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1328 if ( rc != LDAP_SUCCESS ) {
1333 rc = rwm_matched( op, rs );
1337 rc = SLAP_CB_CONTINUE;
1353 slap_overinst *on = (slap_overinst *) be->bd_info;
1354 struct ldaprwmap *rwmap =
1355 (struct ldaprwmap *)on->on_bi.bi_private;
1360 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1362 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1365 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1366 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1368 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1369 rc = rwm_m_config( be, fname, lineno, argc, argv );
1371 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1372 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1374 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1377 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1382 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1383 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1385 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1386 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1389 /* TODO: not implemented yet */
1390 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1391 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1396 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1397 fname, lineno, argv[ 1 ] );
1402 rc = SLAP_CONF_UNKNOWN;
1417 slap_overinst *on = (slap_overinst *) be->bd_info;
1418 struct ldapmapping *mapping = NULL;
1419 struct ldaprwmap *rwmap;
1420 #ifdef ENABLE_REWRITE
1422 #endif /* ENABLE_REWRITE */
1424 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1426 #ifdef ENABLE_REWRITE
1427 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1428 if ( rwmap->rwm_rw == NULL ) {
1433 /* this rewriteContext by default must be null;
1434 * rules can be added if required */
1435 rargv[ 0 ] = "rewriteContext";
1436 rargv[ 1 ] = "searchFilter";
1438 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1440 rargv[ 0 ] = "rewriteContext";
1441 rargv[ 1 ] = "default";
1443 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1444 #endif /* ENABLE_REWRITE */
1446 if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1447 rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1452 on->on_bi.bi_private = (void *)rwmap;
1462 slap_overinst *on = (slap_overinst *) be->bd_info;
1465 if ( on->on_bi.bi_private ) {
1466 struct ldaprwmap *rwmap =
1467 (struct ldaprwmap *)on->on_bi.bi_private;
1469 #ifdef ENABLE_REWRITE
1470 if (rwmap->rwm_rw) {
1471 rewrite_info_delete( &rwmap->rwm_rw );
1473 #else /* !ENABLE_REWRITE */
1474 if ( rwmap->rwm_suffix_massage ) {
1475 ber_bvarray_free( rwmap->rwm_suffix_massage );
1477 #endif /* !ENABLE_REWRITE */
1479 avl_free( rwmap->rwm_oc.remap, NULL );
1480 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1481 avl_free( rwmap->rwm_at.remap, NULL );
1482 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1490 static slap_overinst rwm = { { NULL } };
1495 memset( &rwm, 0, sizeof( slap_overinst ) );
1497 rwm.on_bi.bi_type = "rwm";
1499 rwm.on_bi.bi_db_init = rwm_db_init;
1500 rwm.on_bi.bi_db_config = rwm_db_config;
1501 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1503 rwm.on_bi.bi_op_bind = rwm_op_bind;
1504 rwm.on_bi.bi_op_search = rwm_op_search;
1505 rwm.on_bi.bi_op_compare = rwm_op_compare;
1506 rwm.on_bi.bi_op_modify = rwm_op_modify;
1507 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1508 rwm.on_bi.bi_op_add = rwm_op_add;
1509 rwm.on_bi.bi_op_delete = rwm_op_delete;
1510 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1511 rwm.on_bi.bi_extended = rwm_extended;
1513 rwm.on_bi.bi_operational = rwm_operational;
1514 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1516 #ifdef ENABLE_REWRITE
1517 rwm.on_bi.bi_connection_init = rwm_conn_init;
1518 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1519 #endif /* ENABLE_REWRITE */
1521 rwm.on_response = rwm_response;
1523 return overlay_register( &rwm );
1526 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1528 init_module( int argc, char *argv[] )
1532 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1534 #endif /* SLAPD_OVER_RWM */