1 /* rwm.c - rewrite/remap operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2008 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>
31 typedef struct rwm_op_state {
37 AttributeName *mapped_attrs;
42 rwm_db_destroy( BackendDB *be, ConfigReply *cr );
44 typedef struct rwm_op_cb {
50 rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
52 if ( !BER_BVISNULL( &ros->ro_dn ) ) {
53 op->o_req_dn = ros->ro_dn;
55 if ( !BER_BVISNULL( &ros->ro_ndn ) ) {
56 op->o_req_ndn = ros->ro_ndn;
59 if ( !BER_BVISNULL( &ros->r_dn )
60 && ros->r_dn.bv_val != ros->ro_dn.bv_val )
62 assert( ros->r_dn.bv_val != ros->r_ndn.bv_val );
63 ch_free( ros->r_dn.bv_val );
64 BER_BVZERO( &ros->r_dn );
67 if ( !BER_BVISNULL( &ros->r_ndn )
68 && ros->r_ndn.bv_val != ros->ro_ndn.bv_val )
70 ch_free( ros->r_ndn.bv_val );
71 BER_BVZERO( &ros->r_ndn );
74 BER_BVZERO( &ros->ro_dn );
75 BER_BVZERO( &ros->ro_ndn );
77 switch( ros->r_tag ) {
78 case LDAP_REQ_COMPARE:
79 if ( op->orc_ava->aa_value.bv_val != ros->orc_ava->aa_value.bv_val )
80 op->o_tmpfree( op->orc_ava->aa_value.bv_val, op->o_tmpmemctx );
81 op->orc_ava = ros->orc_ava;
84 slap_mods_free( op->orm_modlist, 1 );
85 op->orm_modlist = ros->orm_modlist;
88 if ( op->orr_newSup != ros->orr_newSup ) {
89 ch_free( op->orr_newSup->bv_val );
90 ch_free( op->orr_nnewSup->bv_val );
91 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
92 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
93 op->orr_newSup = ros->orr_newSup;
94 op->orr_nnewSup = ros->orr_nnewSup;
98 ch_free( ros->mapped_attrs );
99 filter_free_x( op, op->ors_filter, 1 );
100 ch_free( op->ors_filterstr.bv_val );
101 op->ors_attrs = ros->ors_attrs;
102 op->ors_filter = ros->ors_filter;
103 op->ors_filterstr = ros->ors_filterstr;
105 case LDAP_REQ_EXTENDED:
106 if ( op->ore_reqdata != ros->ore_reqdata ) {
107 ber_bvfree( op->ore_reqdata );
108 op->ore_reqdata = ros->ore_reqdata;
112 if ( rs->sr_err == LDAP_SUCCESS ) {
114 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
115 /* too late, c_mutex released */
116 fprintf( stderr, "*** DN: \"%s\" => \"%s\"\n",
117 op->o_conn->c_ndn.bv_val,
118 op->o_req_ndn.bv_val );
119 ber_bvreplace( &op->o_conn->c_ndn,
121 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
130 rwm_op_cleanup( Operation *op, SlapReply *rs )
132 slap_callback *cb = op->o_callback;
133 rwm_op_state *ros = cb->sc_private;
135 if ( rs->sr_type == REP_RESULT || rs->sr_type == REP_EXTENDED ||
136 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
138 rwm_op_rollback( op, rs, ros );
140 op->o_callback = op->o_callback->sc_next;
141 op->o_tmpfree( cb, op->o_tmpmemctx );
144 return SLAP_CB_CONTINUE;
148 rwm_callback_get( Operation *op, SlapReply *rs )
150 rwm_op_cb *roc = NULL;
152 roc = op->o_tmpalloc( sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
153 roc->cb.sc_cleanup = rwm_op_cleanup;
154 roc->cb.sc_response = NULL;
155 roc->cb.sc_next = op->o_callback;
156 roc->cb.sc_private = &roc->ros;
157 roc->ros.r_tag = op->o_tag;
158 roc->ros.ro_dn = op->o_req_dn;
159 roc->ros.ro_ndn = op->o_req_ndn;
160 roc->ros.o_request = op->o_request;
161 BER_BVZERO( &roc->ros.r_dn );
162 BER_BVZERO( &roc->ros.r_ndn );
169 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
172 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
173 struct ldaprwmap *rwmap =
174 (struct ldaprwmap *)on->on_bi.bi_private;
176 struct berval dn = BER_BVNULL,
182 * Rewrite the dn if needed
185 dc.conn = op->o_conn;
187 dc.ctx = (char *)cookie;
189 /* NOTE: in those cases where only the ndn is available,
190 * and the caller sets op->o_req_dn = op->o_req_ndn,
191 * only rewrite the op->o_req_ndn and use it as
192 * op->o_req_dn as well */
194 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
196 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
198 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
201 if ( rc != LDAP_SUCCESS ) {
205 if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
206 || ndn.bv_val == op->o_req_ndn.bv_val )
211 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
213 assert( BER_BVISNULL( &ros->r_dn ) );
219 assert( BER_BVISNULL( &ros->r_ndn ) );
226 rwm_op_add( Operation *op, SlapReply *rs )
228 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
229 struct ldaprwmap *rwmap =
230 (struct ldaprwmap *)on->on_bi.bi_private;
234 Attribute **ap = NULL;
235 char *olddn = op->o_req_dn.bv_val;
238 rwm_op_cb *roc = rwm_callback_get( op, rs );
240 rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
241 if ( rc != LDAP_SUCCESS ) {
242 op->o_bd->bd_info = (BackendInfo *)on->on_info;
243 send_ldap_error( op, rs, rc, "addDN massage error" );
247 if ( olddn != op->o_req_dn.bv_val ) {
248 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
249 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
252 /* Count number of attributes in entry */
253 isupdate = be_shadow_update( op );
254 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
257 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
258 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
262 last = (*ap)->a_numvals - 1;
263 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
264 struct ldapmapping *mapping = NULL;
266 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
268 if ( mapping == NULL ) {
269 if ( rwmap->rwm_at.drop_missing ) {
270 /* FIXME: we allow to remove objectClasses as well;
271 * if the resulting entry is inconsistent, that's
272 * the relayed database's business...
274 ch_free( (*ap)->a_vals[ j ].bv_val );
276 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
278 BER_BVZERO( &(*ap)->a_vals[ last ] );
285 ch_free( (*ap)->a_vals[ j ].bv_val );
286 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
290 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
295 struct ldapmapping *mapping = NULL;
297 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
299 if ( mapping == NULL ) {
300 if ( rwmap->rwm_at.drop_missing ) {
305 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
306 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
309 * FIXME: rewrite could fail; in this case
310 * the operation should give up, right?
312 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
314 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
319 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
320 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
322 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
323 if ( rc != LDAP_SUCCESS ) {
328 if ( mapping != NULL ) {
329 assert( mapping->m_dst_ad != NULL );
330 (*ap)->a_desc = mapping->m_dst_ad;
339 /* FIXME: leaking attribute/values? */
346 op->o_callback = &roc->cb;
348 return SLAP_CB_CONTINUE;
352 rwm_conn_init( BackendDB *be, Connection *conn )
354 slap_overinst *on = (slap_overinst *) be->bd_info;
355 struct ldaprwmap *rwmap =
356 (struct ldaprwmap *)on->on_bi.bi_private;
358 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
360 return SLAP_CB_CONTINUE;
364 rwm_conn_destroy( BackendDB *be, Connection *conn )
366 slap_overinst *on = (slap_overinst *) be->bd_info;
367 struct ldaprwmap *rwmap =
368 (struct ldaprwmap *)on->on_bi.bi_private;
370 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
372 return SLAP_CB_CONTINUE;
376 rwm_op_bind( Operation *op, SlapReply *rs )
378 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
381 rwm_op_cb *roc = rwm_callback_get( op, rs );
383 rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
384 if ( rc != LDAP_SUCCESS ) {
385 op->o_bd->bd_info = (BackendInfo *)on->on_info;
386 send_ldap_error( op, rs, rc, "bindDN massage error" );
390 overlay_callback_after_backover( op, &roc->cb, 1 );
392 return SLAP_CB_CONTINUE;
396 rwm_op_unbind( Operation *op, SlapReply *rs )
398 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
399 struct ldaprwmap *rwmap =
400 (struct ldaprwmap *)on->on_bi.bi_private;
402 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
404 return SLAP_CB_CONTINUE;
408 rwm_op_compare( 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;
415 struct berval mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
417 rwm_op_cb *roc = rwm_callback_get( op, rs );
419 rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
420 if ( rc != LDAP_SUCCESS ) {
421 op->o_bd->bd_info = (BackendInfo *)on->on_info;
422 send_ldap_error( op, rs, rc, "compareDN massage error" );
426 /* if the attribute is an objectClass, try to remap its value */
427 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
428 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
430 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
431 &mapped_vals[0], RWM_MAP );
432 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
434 op->o_bd->bd_info = (BackendInfo *)on->on_info;
435 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
438 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
439 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
444 struct ldapmapping *mapping = NULL;
445 AttributeDescription *ad = op->orc_ava->aa_desc;
447 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
449 if ( mapping == NULL ) {
450 if ( rwmap->rwm_at.drop_missing ) {
451 op->o_bd->bd_info = (BackendInfo *)on->on_info;
452 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
457 assert( mapping->m_dst_ad != NULL );
458 ad = mapping->m_dst_ad;
461 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
462 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
464 struct berval *mapped_valsp[2];
466 mapped_valsp[0] = &mapped_vals[0];
467 mapped_valsp[1] = &mapped_vals[1];
469 mapped_vals[0] = op->orc_ava->aa_value;
471 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
473 if ( rc != LDAP_SUCCESS ) {
474 op->o_bd->bd_info = (BackendInfo *)on->on_info;
475 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
479 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
480 /* NOTE: if we get here, rwm_dnattr_rewrite()
481 * already freed the old value, so now
483 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
485 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
488 op->orc_ava->aa_desc = ad;
491 op->o_callback = &roc->cb;
493 return SLAP_CB_CONTINUE;
497 rwm_op_delete( Operation *op, SlapReply *rs )
499 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
502 rwm_op_cb *roc = rwm_callback_get( op, rs );
504 rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
505 if ( rc != LDAP_SUCCESS ) {
506 op->o_bd->bd_info = (BackendInfo *)on->on_info;
507 send_ldap_error( op, rs, rc, "deleteDN massage error" );
511 op->o_callback = &roc->cb;
513 return SLAP_CB_CONTINUE;
517 rwm_op_modify( Operation *op, SlapReply *rs )
519 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
520 struct ldaprwmap *rwmap =
521 (struct ldaprwmap *)on->on_bi.bi_private;
527 rwm_op_cb *roc = rwm_callback_get( op, rs );
529 rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
530 if ( rc != LDAP_SUCCESS ) {
531 op->o_bd->bd_info = (BackendInfo *)on->on_info;
532 send_ldap_error( op, rs, rc, "modifyDN massage error" );
536 isupdate = be_shadow_update( op );
537 for ( mlp = &op->orm_modlist; *mlp; ) {
539 Modifications *ml = *mlp;
540 struct ldapmapping *mapping = NULL;
542 /* ml points to a temporary mod until needs duplication */
543 if ( ml->sml_desc == slap_schema.si_ad_objectClass
544 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
548 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
550 ml = ch_malloc( sizeof( Modifications ) );
552 if ( (*mlp)->sml_values ) {
553 ber_bvarray_dup_x( &ml->sml_values, (*mlp)->sml_values, NULL );
554 if ( (*mlp)->sml_nvalues ) {
555 ber_bvarray_dup_x( &ml->sml_nvalues, (*mlp)->sml_nvalues, NULL );
564 drop_missing = rwm_mapping( &rwmap->rwm_at,
565 &ml->sml_desc->ad_cname,
567 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
573 /* duplicate the modlist */
574 ml = ch_malloc( sizeof( Modifications ));
578 if ( ml->sml_values != NULL ) {
582 for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
585 bva = ch_malloc( (num+1) * sizeof( struct berval ));
586 for (i=0; i<num; i++)
587 ber_dupbv( &bva[i], &ml->sml_values[i] );
588 BER_BVZERO( &bva[i] );
589 ml->sml_values = bva;
591 if ( ml->sml_nvalues ) {
592 bva = ch_malloc( (num+1) * sizeof( struct berval ));
593 for (i=0; i<num; i++)
594 ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
595 BER_BVZERO( &bva[i] );
596 ml->sml_nvalues = bva;
604 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
605 struct ldapmapping *oc_mapping = NULL;
607 ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
608 &oc_mapping, RWM_MAP );
609 if ( oc_mapping == NULL ) {
610 if ( rwmap->rwm_at.drop_missing ) {
611 /* FIXME: we allow to remove objectClasses as well;
612 * if the resulting entry is inconsistent, that's
613 * the relayed database's business...
616 ch_free( ml->sml_values[ j ].bv_val );
617 ml->sml_values[ j ] = ml->sml_values[ last ];
619 BER_BVZERO( &ml->sml_values[ last ] );
625 ch_free( ml->sml_values[ j ].bv_val );
626 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
631 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
632 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
634 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
636 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
638 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
639 rc = rwm_referral_rewrite( op, rs,
642 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
643 if ( rc != LDAP_SUCCESS ) {
648 if ( rc != LDAP_SUCCESS ) {
655 if ( mapping != NULL ) {
656 /* use new attribute description */
657 assert( mapping->m_dst_ad != NULL );
658 ml->sml_desc = mapping->m_dst_ad;
666 *mlp = (*mlp)->sml_next;
667 slap_mod_free( &ml->sml_mod, 0 );
671 op->o_callback = &roc->cb;
673 return SLAP_CB_CONTINUE;
677 rwm_op_modrdn( Operation *op, SlapReply *rs )
679 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
680 struct ldaprwmap *rwmap =
681 (struct ldaprwmap *)on->on_bi.bi_private;
685 rwm_op_cb *roc = rwm_callback_get( op, rs );
687 if ( op->orr_newSup ) {
689 struct berval nnewSup = BER_BVNULL;
690 struct berval newSup = BER_BVNULL;
693 * Rewrite the new superior, if defined and required
696 dc.conn = op->o_conn;
698 dc.ctx = "newSuperiorDN";
699 newSup = *op->orr_newSup;
700 nnewSup = *op->orr_nnewSup;
701 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
702 if ( rc != LDAP_SUCCESS ) {
703 op->o_bd->bd_info = (BackendInfo *)on->on_info;
704 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
708 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
709 op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
711 op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
713 *op->orr_newSup = newSup;
714 *op->orr_nnewSup = nnewSup;
719 * Rewrite the dn, if needed
721 rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
722 if ( rc != LDAP_SUCCESS ) {
723 op->o_bd->bd_info = (BackendInfo *)on->on_info;
724 send_ldap_error( op, rs, rc, "renameDN massage error" );
725 if ( op->orr_newSup != roc->ros.orr_newSup ) {
726 ch_free( op->orr_newSup->bv_val );
727 ch_free( op->orr_nnewSup->bv_val );
728 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
729 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
730 op->orr_newSup = roc->ros.orr_newSup;
731 op->orr_nnewSup = roc->ros.orr_nnewSup;
736 /* TODO: rewrite newRDN, attribute types,
737 * values of DN-valued attributes ... */
739 op->o_callback = &roc->cb;
741 return SLAP_CB_CONTINUE;
746 rwm_swap_attrs( Operation *op, SlapReply *rs )
748 slap_callback *cb = op->o_callback;
749 rwm_op_state *ros = cb->sc_private;
751 rs->sr_attrs = ros->ors_attrs;
753 /* other overlays might have touched op->ors_attrs,
754 * so we restore the original version here, otherwise
755 * attribute-mapping might fail */
756 op->ors_attrs = ros->mapped_attrs;
758 return SLAP_CB_CONTINUE;
762 rwm_op_search( Operation *op, SlapReply *rs )
764 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
765 struct ldaprwmap *rwmap =
766 (struct ldaprwmap *)on->on_bi.bi_private;
771 struct berval fstr = BER_BVNULL;
774 AttributeName *an = NULL;
778 rwm_op_cb *roc = rwm_callback_get( op, rs );
780 rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
781 "searchFilter", op->ors_filterstr.bv_val );
782 if ( rc == LDAP_SUCCESS )
783 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
784 if ( rc != LDAP_SUCCESS ) {
785 text = "searchDN massage error";
790 * Rewrite the dn if needed
793 dc.conn = op->o_conn;
795 dc.ctx = "searchFilterAttrDN";
797 rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
798 if ( rc != LDAP_SUCCESS ) {
799 text = "searchFilter/searchFilterAttrDN massage error";
803 f = str2filter_x( op, fstr.bv_val );
806 text = "massaged filter parse error";
811 op->ors_filterstr = fstr;
813 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
814 op->ors_attrs, &an, RWM_MAP );
815 if ( rc != LDAP_SUCCESS ) {
816 text = "attribute list mapping error";
821 /* store the mapped Attributes for later usage, in
822 * the case that other overlays change op->ors_attrs */
823 roc->ros.mapped_attrs = an;
824 roc->cb.sc_response = rwm_swap_attrs;
826 op->o_callback = &roc->cb;
828 return SLAP_CB_CONTINUE;
836 filter_free_x( op, f, 1 );
839 if ( !BER_BVISNULL( &fstr ) ) {
840 ch_free( fstr.bv_val );
843 rwm_op_rollback( op, rs, &roc->ros );
844 op->oq_search = roc->ros.oq_search;
845 op->o_tmpfree( roc, op->o_tmpmemctx );
847 op->o_bd->bd_info = (BackendInfo *)on->on_info;
848 send_ldap_error( op, rs, rc, text );
855 rwm_exop_passwd( Operation *op, SlapReply *rs )
857 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
861 struct berval id = BER_BVNULL,
864 BerElement *ber = NULL;
866 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
870 if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
871 rs->sr_err = LDAP_OTHER;
875 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
876 &pwold, &pwnew, &rs->sr_text );
877 if ( rs->sr_err != LDAP_SUCCESS ) {
881 if ( !BER_BVISNULL( &id ) ) {
882 char idNul = id.bv_val[id.bv_len];
883 id.bv_val[id.bv_len] = '\0';
884 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
885 &op->o_req_ndn, op->o_tmpmemctx );
886 id.bv_val[id.bv_len] = idNul;
887 if ( rs->sr_err != LDAP_SUCCESS ) {
888 rs->sr_text = "Invalid DN";
893 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
894 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
897 roc = rwm_callback_get( op, rs );
899 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
900 if ( rc != LDAP_SUCCESS ) {
901 op->o_bd->bd_info = (BackendInfo *)on->on_info;
902 send_ldap_error( op, rs, rc, "extendedDN massage error" );
906 ber = ber_alloc_t( LBER_USE_DER );
908 rs->sr_err = LDAP_OTHER;
909 rs->sr_text = "No memory";
912 ber_printf( ber, "{" );
913 if ( !BER_BVISNULL( &id )) {
914 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
917 if ( !BER_BVISNULL( &pwold )) {
918 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
920 if ( !BER_BVISNULL( &pwnew )) {
921 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
923 ber_printf( ber, "N}" );
924 ber_flatten( ber, &op->ore_reqdata );
927 op->o_callback = &roc->cb;
929 return SLAP_CB_CONTINUE;
934 BI_op_extended *extended;
936 { BER_BVC(LDAP_EXOP_MODIFY_PASSWD), rwm_exop_passwd },
941 rwm_extended( Operation *op, SlapReply *rs )
943 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
949 for ( i = 0; exop_table[i].extended != NULL; i++ ) {
950 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
952 rc = exop_table[i].extended( op, rs );
957 case SLAP_CB_CONTINUE:
962 send_ldap_result( op, rs );
969 roc = rwm_callback_get( op, rs );
971 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
972 if ( rc != LDAP_SUCCESS ) {
973 op->o_bd->bd_info = (BackendInfo *)on->on_info;
974 send_ldap_error( op, rs, rc, "extendedDN massage error" );
978 /* TODO: rewrite/map extended data ? ... */
979 op->o_callback = &roc->cb;
981 return SLAP_CB_CONTINUE;
985 rwm_matched( Operation *op, SlapReply *rs )
987 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
988 struct ldaprwmap *rwmap =
989 (struct ldaprwmap *)on->on_bi.bi_private;
991 struct berval dn, mdn;
995 if ( rs->sr_matched == NULL ) {
996 return SLAP_CB_CONTINUE;
1000 dc.conn = op->o_conn;
1002 dc.ctx = "matchedDN";
1003 ber_str2bv( rs->sr_matched, 0, 0, &dn );
1005 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
1006 if ( rc != LDAP_SUCCESS ) {
1008 rs->sr_text = "Rewrite error";
1012 if ( mdn.bv_val != dn.bv_val ) {
1013 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
1014 ch_free( (void *)rs->sr_matched );
1017 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
1019 rs->sr_matched = mdn.bv_val;
1022 return SLAP_CB_CONTINUE;
1026 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
1028 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1029 struct ldaprwmap *rwmap =
1030 (struct ldaprwmap *)on->on_bi.bi_private;
1036 int check_duplicate_attrs = 0;
1039 * Rewrite the dn attrs, if needed
1042 dc.conn = op->o_conn;
1045 /* FIXME: the entries are in the remote mapping form;
1046 * so we need to select those attributes we are willing
1047 * to return, and remap them accordingly */
1049 /* FIXME: in principle, one could map an attribute
1050 * on top of another, which already exists.
1051 * As such, in the end there might exist more than
1052 * one instance of an attribute.
1053 * We should at least check if this occurs, and issue
1054 * an error (because multiple instances of attrs in
1055 * response are not valid), or merge the values (what
1056 * about duplicate values?) */
1057 isupdate = be_shadow_update( op );
1058 for ( ap = a_first; *ap; ) {
1059 struct ldapmapping *mapping = NULL;
1064 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
1069 if ( op->ors_attrs != NULL &&
1070 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1071 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1076 drop_missing = rwm_mapping( &rwmap->rwm_at,
1077 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1078 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1082 if ( mapping != NULL ) {
1083 assert( mapping->m_dst_ad != NULL );
1085 /* try to normalize mapped Attributes if the original
1086 * AttributeType was not normalized */
1087 if ( (!(*ap)->a_desc->ad_type->sat_equality ||
1088 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1089 mapping->m_dst_ad->ad_type->sat_equality &&
1090 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1092 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
1096 last = (*ap)->a_numvals;
1099 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1101 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1104 * check that each value is valid per syntax
1105 * and pretty if appropriate
1107 rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1108 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1109 mapping->m_dst_ad->ad_type->sat_syntax,
1110 mapping->m_dst_ad->ad_type->sat_equality,
1111 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1114 if ( rc != LDAP_SUCCESS ) {
1115 BER_BVZERO( &(*ap)->a_nvals[i] );
1118 BER_BVZERO( &(*ap)->a_nvals[i] );
1122 assert( (*ap)->a_nvals == (*ap)->a_vals );
1123 (*ap)->a_nvals = NULL;
1124 ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
1128 /* rewrite the attribute description */
1129 (*ap)->a_desc = mapping->m_dst_ad;
1131 /* will need to check for duplicate attrs */
1132 check_duplicate_attrs++;
1136 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1137 if ( stripEntryDN ) {
1138 /* will be generated by frontend */
1142 } else if ( !isupdate
1144 && (*ap)->a_desc->ad_type->sat_no_user_mod
1145 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1150 if ( last == -1 ) { /* not yet counted */
1151 last = (*ap)->a_numvals;
1155 /* empty? leave it in place because of attrsonly and vlv */
1160 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1161 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1165 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1166 struct berval mapped;
1168 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1169 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1171 ch_free( bv[0].bv_val );
1172 BER_BVZERO( &bv[0] );
1173 if ( &(*ap)->a_vals[last] > &bv[0] ) {
1174 bv[0] = (*ap)->a_vals[last];
1175 BER_BVZERO( &(*ap)->a_vals[last] );
1180 } else if ( mapped.bv_val != bv[0].bv_val
1181 && ber_bvstrcasecmp( &mapped, &bv[0] ) != 0 )
1185 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1186 if ( &(*ap)->a_vals[ i ] == bv ) {
1190 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1195 if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1200 * FIXME: after LBER_FREEing
1201 * the value is replaced by
1202 * ch_alloc'ed memory
1204 ber_bvreplace( &bv[0], &mapped );
1206 /* FIXME: will need to check
1207 * if the structuralObjectClass
1213 * It is necessary to try to rewrite attributes with
1214 * dn syntax because they might be used in ACLs as
1215 * members of groups; since ACLs are applied to the
1216 * rewritten stuff, no dn-based subject clause could
1217 * be used at the ldap backend side (see
1218 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1219 * The problem can be overcome by moving the dn-based
1220 * ACLs to the target directory server, and letting
1221 * everything pass thru the ldap backend. */
1222 /* FIXME: handle distinguishedName-like syntaxes, like
1223 * nameAndOptionalUID */
1224 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1225 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1227 dc.ctx = "searchAttrDN";
1228 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
1229 if ( rc != LDAP_SUCCESS ) {
1233 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1234 dc.ctx = "searchAttrDN";
1235 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1236 if ( rc != LDAP_SUCCESS ) {
1243 ap = &(*ap)->a_next;
1248 *ap = (*ap)->a_next;
1253 /* only check if some mapping occurred */
1254 if ( check_duplicate_attrs ) {
1255 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1258 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1259 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1261 Modification mod = { 0 };
1262 const char *text = NULL;
1263 char textbuf[ SLAP_TEXT_BUFLEN ];
1264 Attribute *next = (*tap)->a_next;
1266 BER_BVSTR( &e.e_name, "" );
1267 BER_BVSTR( &e.e_nname, "" );
1269 mod.sm_op = LDAP_MOD_ADD;
1270 mod.sm_desc = (*ap)->a_desc;
1271 mod.sm_type = mod.sm_desc->ad_cname;
1272 mod.sm_numvals = (*tap)->a_numvals;
1273 mod.sm_values = (*tap)->a_vals;
1274 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
1275 mod.sm_nvalues = (*tap)->a_nvals;
1278 (void)modify_add_values( &e, &mod,
1280 &text, textbuf, sizeof( textbuf ) );
1282 /* should not insert new attrs! */
1283 assert( e.e_attrs == *ap );
1289 tap = &(*tap)->a_next;
1299 rwm_send_entry( Operation *op, SlapReply *rs )
1301 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1302 struct ldaprwmap *rwmap =
1303 (struct ldaprwmap *)on->on_bi.bi_private;
1307 struct berval dn = BER_BVNULL,
1312 assert( rs->sr_entry != NULL );
1315 * Rewrite the dn of the result, if needed
1318 dc.conn = op->o_conn;
1320 dc.ctx = "searchEntryDN";
1323 flags = rs->sr_flags;
1324 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1325 /* FIXME: all we need to duplicate are:
1328 * - attributes that are requested
1329 * - no values if attrsonly is set
1334 rc = LDAP_NO_MEMORY;
1338 flags &= ~REP_ENTRY_MUSTRELEASE;
1339 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1343 * Note: this may fail if the target host(s) schema differs
1344 * from the one known to the meta, and a DN with unknown
1345 * attributes is returned.
1349 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1350 if ( rc != LDAP_SUCCESS ) {
1355 if ( e->e_name.bv_val != dn.bv_val ) {
1356 ch_free( e->e_name.bv_val );
1357 ch_free( e->e_nname.bv_val );
1363 /* TODO: map entry attribute types, objectclasses
1364 * and dn-valued attribute values */
1366 /* FIXME: the entries are in the remote mapping form;
1367 * so we need to select those attributes we are willing
1368 * to return, and remap them accordingly */
1369 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1371 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1372 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
1376 rs->sr_flags = flags;
1378 return SLAP_CB_CONTINUE;
1381 if ( e != NULL && e != rs->sr_entry ) {
1382 if ( e->e_name.bv_val == dn.bv_val ) {
1383 BER_BVZERO( &e->e_name );
1386 if ( e->e_nname.bv_val == ndn.bv_val ) {
1387 BER_BVZERO( &e->e_nname );
1393 if ( !BER_BVISNULL( &dn ) ) {
1394 ch_free( dn.bv_val );
1397 if ( !BER_BVISNULL( &ndn ) ) {
1398 ch_free( ndn.bv_val );
1405 rwm_operational( Operation *op, SlapReply *rs )
1407 /* FIXME: the entries are in the remote mapping form;
1408 * so we need to select those attributes we are willing
1409 * to return, and remap them accordingly */
1410 if ( rs->sr_operational_attrs ) {
1411 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1414 return SLAP_CB_CONTINUE;
1418 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1419 * rewritten for subsequent operations; fine for plain suffixmassage,
1420 * but destroys everything else */
1422 rwm_chk_referrals( Operation *op, SlapReply *rs )
1424 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1427 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1428 if ( rc != LDAP_SUCCESS ) {
1429 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1430 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1434 return SLAP_CB_CONTINUE;
1446 slap_overinst *on = (slap_overinst *) be->bd_info;
1447 struct ldaprwmap *rwmap =
1448 (struct ldaprwmap *)on->on_bi.bi_private;
1450 return rewrite_parse( rwmap->rwm_rw,
1451 fname, lineno, argc, argv );
1457 rwm_suffixmassage_config(
1464 slap_overinst *on = (slap_overinst *) be->bd_info;
1465 struct ldaprwmap *rwmap =
1466 (struct ldaprwmap *)on->on_bi.bi_private;
1468 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1475 * suffixmassage [<suffix>] <massaged suffix>
1477 * the [<suffix>] field must be defined as a valid suffix
1478 * for the current database;
1479 * the <massaged suffix> shouldn't have already been
1480 * defined as a valid suffix for the current server
1483 if ( be->be_suffix == NULL ) {
1484 fprintf( stderr, "%s: line %d: "
1485 " \"suffixMassage [<suffix>]"
1486 " <massaged suffix>\" without "
1487 "<suffix> part requires database "
1488 "suffix be defined first.\n",
1492 bvnc = be->be_suffix[ 0 ];
1495 } else if ( argc == 3 ) {
1496 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1500 fprintf( stderr, "%s: line %d: syntax is"
1501 " \"suffixMassage [<suffix>]"
1502 " <massaged suffix>\"\n",
1507 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1508 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1509 fname, lineno, bvnc.bv_val );
1513 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1514 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1515 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1516 fname, lineno, brnc.bv_val );
1517 free( nvnc.bv_val );
1518 free( pvnc.bv_val );
1523 * The suffix massaging is emulated
1524 * by means of the rewrite capabilities
1526 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1527 &pvnc, &nvnc, &prnc, &nrnc );
1528 free( nvnc.bv_val );
1529 free( pvnc.bv_val );
1530 free( nrnc.bv_val );
1531 free( prnc.bv_val );
1544 slap_overinst *on = (slap_overinst *) be->bd_info;
1545 struct ldaprwmap *rwmap =
1546 (struct ldaprwmap *)on->on_bi.bi_private;
1548 /* objectclass/attribute mapping */
1549 return rwm_map_config( &rwmap->rwm_oc,
1551 fname, lineno, argc, argv );
1555 rwm_response( Operation *op, SlapReply *rs )
1557 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1558 struct ldaprwmap *rwmap =
1559 (struct ldaprwmap *)on->on_bi.bi_private;
1563 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1564 return rwm_send_entry( op, rs );
1567 switch( op->o_tag ) {
1568 case LDAP_REQ_SEARCH:
1571 case LDAP_REQ_DELETE:
1572 case LDAP_REQ_MODRDN:
1573 case LDAP_REQ_MODIFY:
1574 case LDAP_REQ_COMPARE:
1575 case LDAP_REQ_EXTENDED:
1580 * Rewrite the dn of the referrals, if needed
1583 dc.conn = op->o_conn;
1585 dc.ctx = "referralDN";
1586 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1587 if ( rc != LDAP_SUCCESS ) {
1592 rc = rwm_matched( op, rs );
1596 rc = SLAP_CB_CONTINUE;
1611 slap_overinst *on = (slap_overinst *) be->bd_info;
1612 struct ldaprwmap *rwmap =
1613 (struct ldaprwmap *)on->on_bi.bi_private;
1618 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1620 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1623 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1624 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1626 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1627 rc = rwm_m_config( be, fname, lineno, argc, argv );
1629 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1630 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1632 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1635 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1640 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1641 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1643 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1644 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1646 /* TODO: not implemented yet */
1647 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1649 "%s: line %d: \"discover\" not supported yet "
1650 "in \"t-f-support {no|yes|discover}\".\n",
1654 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1659 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1660 fname, lineno, argv[ 1 ] );
1664 } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) == 0 ) {
1667 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1672 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1673 rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1675 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1676 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1680 rc = SLAP_CONF_UNKNOWN;
1691 * dynamic configuration...
1697 RWM_CF_SUFFIXMASSAGE,
1702 RWM_CF_NORMALIZE_MAPPED,
1707 static slap_verbmasks t_f_mode[] = {
1708 { BER_BVC( "yes" ), RWM_F_SUPPORT_T_F },
1709 { BER_BVC( "discover" ), RWM_F_SUPPORT_T_F_DISCOVER },
1710 { BER_BVC( "no" ), RWM_F_NONE },
1714 static ConfigDriver rwm_cf_gen;
1716 static ConfigTable rwmcfg[] = {
1717 { "rwm-rewrite", "rewrite",
1718 2, 0, STRLENOF("rwm-rewrite"),
1719 ARG_MAGIC|ARG_QUOTE|RWM_CF_REWRITE, rwm_cf_gen,
1720 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1721 "DESC 'Rewrites strings' "
1722 "EQUALITY caseIgnoreMatch "
1723 "SYNTAX OMsDirectoryString "
1724 "X-ORDERED 'VALUES' )",
1727 { "rwm-suffixmassage", "[virtual]> <real",
1728 2, 3, 0, ARG_MAGIC|RWM_CF_SUFFIXMASSAGE, rwm_cf_gen,
1731 { "rwm-t-f-support", "true|false|discover",
1732 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1733 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1734 "DESC 'Absolute filters support' "
1735 "SYNTAX OMsDirectoryString "
1739 { "rwm-map", "{objectClass|attribute}",
1740 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1741 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1742 "DESC 'maps attributes/objectClasses' "
1743 "SYNTAX OMsDirectoryString "
1744 "X-ORDERED 'VALUES' )",
1747 { "rwm-normalize-mapped-attrs", "true|false",
1748 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1749 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1750 "DESC 'Normalize mapped attributes/objectClasses' "
1751 "SYNTAX OMsBoolean "
1755 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1758 static ConfigOCs rwmocs[] = {
1759 { "( OLcfgOvOc:16.1 "
1760 "NAME 'olcRwmConfig' "
1761 "DESC 'Rewrite/remap configuration' "
1762 "SUP olcOverlayConfig "
1765 "olcRwmTFSupport $ "
1767 "olcRwmNormalizeMapped "
1769 Cft_Overlay, rwmcfg, NULL, NULL },
1774 slap_rewrite_unparse( BerVarray in, BerVarray *out )
1777 BerVarray bva = NULL;
1778 char ibuf[32], *ptr;
1781 assert( in != NULL );
1783 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1792 bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1793 BER_BVZERO( &bva[ 0 ] );
1795 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1796 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1797 if ( idx.bv_len >= sizeof( ibuf ) ) {
1798 ber_bvarray_free( bva );
1802 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1803 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1804 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1805 ptr = lutil_strcopy( ptr, in[i].bv_val );
1807 BER_BVZERO( &bva[ i + 1 ] );
1814 rwm_cf_gen( ConfigArgs *c )
1816 slap_overinst *on = (slap_overinst *)c->bi;
1817 struct ldaprwmap *rwmap =
1818 (struct ldaprwmap *)on->on_bi.bi_private;
1827 if ( c->op == SLAP_CONFIG_EMIT ) {
1828 struct berval bv = BER_BVNULL;
1830 switch ( c->type ) {
1831 case RWM_CF_REWRITE:
1832 if ( rwmap->rwm_bva_rewrite == NULL ) {
1836 slap_rewrite_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
1837 if ( !c->rvalue_vals ) {
1843 case RWM_CF_T_F_SUPPORT:
1844 enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
1845 if ( BER_BVISNULL( &bv ) ) {
1846 /* there's something wrong... */
1851 value_add_one( &c->rvalue_vals, &bv );
1856 if ( rwmap->rwm_bva_map == NULL ) {
1860 value_add( &c->rvalue_vals, rwmap->rwm_bva_map );
1864 case RWM_CF_NORMALIZE_MAPPED:
1865 c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
1875 } else if ( c->op == LDAP_MOD_DELETE ) {
1876 switch ( c->type ) {
1877 case RWM_CF_REWRITE:
1878 if ( c->valx >= 0 ) {
1879 /* single modification is not allowed */
1882 } else if ( rwmap->rwm_rw != NULL ) {
1883 rewrite_info_delete( &rwmap->rwm_rw );
1884 assert( rwmap->rwm_rw == NULL );
1886 ber_bvarray_free( rwmap->rwm_bva_rewrite );
1887 rwmap->rwm_bva_rewrite = NULL;
1891 case RWM_CF_T_F_SUPPORT:
1892 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1896 if ( c->valx >= 0 ) {
1897 /* single modification is not allowed */
1901 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
1902 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1903 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
1904 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1906 rwmap->rwm_oc.remap = NULL;
1907 rwmap->rwm_oc.map = NULL;
1908 rwmap->rwm_at.remap = NULL;
1909 rwmap->rwm_at.map = NULL;
1911 ber_bvarray_free( rwmap->rwm_bva_map );
1912 rwmap->rwm_bva_map = NULL;
1916 case RWM_CF_NORMALIZE_MAPPED:
1917 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
1926 switch ( c->type ) {
1927 case RWM_CF_REWRITE:
1928 argv0 = c->argv[ 0 ];
1929 c->argv[ 0 ] += STRLENOF( "rwm-" );
1930 rc = rwm_rw_config( &db, c->fname, c->lineno, c->argc, c->argv );
1931 c->argv[ 0 ] = argv0;
1939 line = ldap_charray2str( c->argv, "\" \"" );
1940 if ( line != NULL ) {
1941 int len = strlen( c->argv[ 0 ] );
1943 ber_str2bv( line, 0, 0, &bv );
1944 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1945 bv.bv_len - ( len + 1 ) );
1946 bv.bv_val[ bv.bv_len - 1 ] = '"';
1947 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1952 case RWM_CF_SUFFIXMASSAGE:
1953 argv0 = c->argv[ 0 ];
1954 c->argv[ 0 ] += STRLENOF( "rwm-" );
1955 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno, c->argc, c->argv );
1956 c->argv[ 0 ] = argv0;
1964 /* FIXME: not optimal; in fact, this keeps track
1965 * of the fact that a set of rules was added
1966 * using the rwm-suffixmassage shortcut, but the
1967 * rules are not clarified */
1969 line = ldap_charray2str( c->argv, "\" \"" );
1970 if ( line != NULL ) {
1971 int len = strlen( c->argv[ 0 ] );
1973 ber_str2bv( line, 0, 0, &bv );
1974 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1975 bv.bv_len - ( len + 1 ) );
1976 bv.bv_val[ bv.bv_len - 1 ] = '"';
1977 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1982 case RWM_CF_T_F_SUPPORT:
1983 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
1984 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
1988 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1989 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
1994 argv0 = c->argv[ 0 ];
1995 c->argv[ 0 ] += STRLENOF( "rwm-" );
1996 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
1997 c->argv[ 0 ] = argv0;
2005 line = ldap_charray2str( &c->argv[ 1 ], " " );
2006 if ( line != NULL ) {
2007 ber_str2bv( line, 0, 0, &bv );
2008 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
2013 case RWM_CF_NORMALIZE_MAPPED:
2014 if ( c->value_int ) {
2015 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
2017 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2034 slap_overinst *on = (slap_overinst *) be->bd_info;
2035 struct ldaprwmap *rwmap;
2039 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
2041 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
2042 if ( rwmap->rwm_rw == NULL ) {
2047 /* this rewriteContext by default must be null;
2048 * rules can be added if required */
2049 rargv[ 0 ] = "rewriteContext";
2050 rargv[ 1 ] = "searchFilter";
2052 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
2054 rargv[ 0 ] = "rewriteContext";
2055 rargv[ 1 ] = "default";
2057 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
2060 on->on_bi.bi_private = (void *)rwmap;
2063 (void)rwm_db_destroy( be, NULL );
2074 slap_overinst *on = (slap_overinst *) be->bd_info;
2077 if ( on->on_bi.bi_private ) {
2078 struct ldaprwmap *rwmap =
2079 (struct ldaprwmap *)on->on_bi.bi_private;
2081 if ( rwmap->rwm_rw ) {
2082 rewrite_info_delete( &rwmap->rwm_rw );
2083 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2086 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2087 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2088 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2089 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2090 ber_bvarray_free( rwmap->rwm_bva_map );
2098 static slap_overinst rwm = { { NULL } };
2100 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2102 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2104 rwm_initialize( void )
2108 /* Make sure we don't exceed the bits reserved for userland */
2109 config_check_userland( RWM_CF_LAST );
2111 memset( &rwm, 0, sizeof( slap_overinst ) );
2113 rwm.on_bi.bi_type = "rwm";
2114 rwm.on_bi.bi_flags =
2115 SLAPO_BFLAG_SINGLE |
2118 rwm.on_bi.bi_db_init = rwm_db_init;
2119 rwm.on_bi.bi_db_config = rwm_db_config;
2120 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2122 rwm.on_bi.bi_op_bind = rwm_op_bind;
2123 rwm.on_bi.bi_op_search = rwm_op_search;
2124 rwm.on_bi.bi_op_compare = rwm_op_compare;
2125 rwm.on_bi.bi_op_modify = rwm_op_modify;
2126 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2127 rwm.on_bi.bi_op_add = rwm_op_add;
2128 rwm.on_bi.bi_op_delete = rwm_op_delete;
2129 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2130 rwm.on_bi.bi_extended = rwm_extended;
2132 rwm.on_bi.bi_operational = rwm_operational;
2133 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2135 rwm.on_bi.bi_connection_init = rwm_conn_init;
2136 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2138 rwm.on_response = rwm_response;
2140 rwm.on_bi.bi_cf_ocs = rwmocs;
2142 rc = config_register_schema( rwmcfg, rwmocs );
2147 return overlay_register( &rwm );
2150 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2152 init_module( int argc, char *argv[] )
2154 return rwm_initialize();
2156 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2158 #endif /* SLAPD_OVER_RWM */