1 /* rwm.c - rewrite/remap operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2011 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 {
39 AttributeName *mapped_attrs;
43 typedef struct rwm_op_cb {
49 rwm_db_destroy( BackendDB *be, ConfigReply *cr );
52 rwm_send_entry( Operation *op, SlapReply *rs );
55 rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
57 /* in case of successful extended operation cleanup
58 * gets called *after* (ITS#6632); this hack counts
59 * on others to cleanup our o_req_dn/o_req_ndn,
60 * while we cleanup theirs. */
61 if ( ros->r_tag == LDAP_REQ_EXTENDED && rs->sr_err == LDAP_SUCCESS ) {
62 if ( !BER_BVISNULL( &ros->rx_dn ) ) {
63 ch_free( ros->rx_dn.bv_val );
65 if ( !BER_BVISNULL( &ros->rx_ndn ) ) {
66 ch_free( ros->rx_ndn.bv_val );
70 if ( !BER_BVISNULL( &ros->ro_dn ) ) {
71 op->o_req_dn = ros->ro_dn;
73 if ( !BER_BVISNULL( &ros->ro_ndn ) ) {
74 op->o_req_ndn = ros->ro_ndn;
77 if ( !BER_BVISNULL( &ros->r_dn )
78 && ros->r_dn.bv_val != ros->ro_dn.bv_val )
80 assert( ros->r_dn.bv_val != ros->r_ndn.bv_val );
81 ch_free( ros->r_dn.bv_val );
84 if ( !BER_BVISNULL( &ros->r_ndn )
85 && ros->r_ndn.bv_val != ros->ro_ndn.bv_val )
87 ch_free( ros->r_ndn.bv_val );
91 BER_BVZERO( &ros->r_dn );
92 BER_BVZERO( &ros->r_ndn );
93 BER_BVZERO( &ros->ro_dn );
94 BER_BVZERO( &ros->ro_ndn );
95 BER_BVZERO( &ros->rx_dn );
96 BER_BVZERO( &ros->rx_ndn );
98 switch( ros->r_tag ) {
99 case LDAP_REQ_COMPARE:
100 if ( op->orc_ava->aa_value.bv_val != ros->orc_ava->aa_value.bv_val )
101 op->o_tmpfree( op->orc_ava->aa_value.bv_val, op->o_tmpmemctx );
102 op->orc_ava = ros->orc_ava;
104 case LDAP_REQ_MODIFY:
105 slap_mods_free( op->orm_modlist, 1 );
106 op->orm_modlist = ros->orm_modlist;
108 case LDAP_REQ_MODRDN:
109 if ( op->orr_newSup != ros->orr_newSup ) {
110 ch_free( op->orr_newSup->bv_val );
111 ch_free( op->orr_nnewSup->bv_val );
112 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
113 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
114 op->orr_newSup = ros->orr_newSup;
115 op->orr_nnewSup = ros->orr_nnewSup;
117 if ( op->orr_newrdn.bv_val != ros->orr_newrdn.bv_val ) {
118 ch_free( op->orr_newrdn.bv_val );
119 ch_free( op->orr_nnewrdn.bv_val );
120 op->orr_newrdn = ros->orr_newrdn;
121 op->orr_nnewrdn = ros->orr_nnewrdn;
124 case LDAP_REQ_SEARCH:
125 op->o_tmpfree( ros->mapped_attrs, op->o_tmpmemctx );
126 filter_free_x( op, op->ors_filter, 1 );
127 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
128 op->ors_attrs = ros->ors_attrs;
129 op->ors_filter = ros->ors_filter;
130 op->ors_filterstr = ros->ors_filterstr;
132 case LDAP_REQ_EXTENDED:
133 if ( op->ore_reqdata != ros->ore_reqdata ) {
134 ber_bvfree( op->ore_reqdata );
135 op->ore_reqdata = ros->ore_reqdata;
139 if ( rs->sr_err == LDAP_SUCCESS ) {
141 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
142 /* too late, c_mutex released */
143 Debug( LDAP_DEBUG_ANY, "*** DN: \"%s\" => \"%s\"\n",
144 op->o_conn->c_ndn.bv_val,
145 op->o_req_ndn.bv_val );
146 ber_bvreplace( &op->o_conn->c_ndn,
148 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
157 rwm_op_cleanup( Operation *op, SlapReply *rs )
159 slap_callback *cb = op->o_callback;
160 rwm_op_state *ros = cb->sc_private;
162 if ( rs->sr_type == REP_RESULT || rs->sr_type == REP_EXTENDED ||
163 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
165 rwm_op_rollback( op, rs, ros );
167 op->o_callback = op->o_callback->sc_next;
168 op->o_tmpfree( cb, op->o_tmpmemctx );
171 return SLAP_CB_CONTINUE;
175 rwm_callback_get( Operation *op )
179 roc = op->o_tmpalloc( sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
180 roc->cb.sc_cleanup = rwm_op_cleanup;
181 roc->cb.sc_response = NULL;
182 roc->cb.sc_next = op->o_callback;
183 roc->cb.sc_private = &roc->ros;
184 roc->ros.r_tag = op->o_tag;
185 roc->ros.ro_dn = op->o_req_dn;
186 roc->ros.ro_ndn = op->o_req_ndn;
187 BER_BVZERO( &roc->ros.r_dn );
188 BER_BVZERO( &roc->ros.r_ndn );
189 BER_BVZERO( &roc->ros.rx_dn );
190 BER_BVZERO( &roc->ros.rx_ndn );
191 roc->ros.mapped_attrs = NULL;
192 roc->ros.o_request = op->o_request;
199 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
202 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
203 struct ldaprwmap *rwmap =
204 (struct ldaprwmap *)on->on_bi.bi_private;
206 struct berval dn = BER_BVNULL,
212 * Rewrite the dn if needed
215 dc.conn = op->o_conn;
217 dc.ctx = (char *)cookie;
219 /* NOTE: in those cases where only the ndn is available,
220 * and the caller sets op->o_req_dn = op->o_req_ndn,
221 * only rewrite the op->o_req_ndn and use it as
222 * op->o_req_dn as well */
224 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
226 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
228 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
231 if ( rc != LDAP_SUCCESS ) {
235 if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
236 || ndn.bv_val == op->o_req_ndn.bv_val )
241 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
243 assert( BER_BVISNULL( &ros->r_dn ) );
249 assert( BER_BVISNULL( &ros->r_ndn ) );
252 if ( ros->r_tag == LDAP_REQ_EXTENDED ) {
253 ros->rx_dn = ros->r_dn;
254 ros->rx_ndn = ros->r_ndn;
261 rwm_op_add( Operation *op, SlapReply *rs )
263 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
264 struct ldaprwmap *rwmap =
265 (struct ldaprwmap *)on->on_bi.bi_private;
269 Attribute **ap = NULL;
270 char *olddn = op->o_req_dn.bv_val;
273 rwm_op_cb *roc = rwm_callback_get( op );
275 rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
276 if ( rc != LDAP_SUCCESS ) {
277 op->o_bd->bd_info = (BackendInfo *)on->on_info;
278 send_ldap_error( op, rs, rc, "addDN massage error" );
282 if ( olddn != op->o_req_dn.bv_val ) {
283 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
284 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
287 /* Count number of attributes in entry */
288 isupdate = be_shadow_update( op );
289 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
292 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
293 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
297 last = (*ap)->a_numvals - 1;
298 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
299 struct ldapmapping *mapping = NULL;
301 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
303 if ( mapping == NULL ) {
304 if ( rwmap->rwm_at.drop_missing ) {
305 /* FIXME: we allow to remove objectClasses as well;
306 * if the resulting entry is inconsistent, that's
307 * the relayed database's business...
309 ch_free( (*ap)->a_vals[ j ].bv_val );
311 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
313 BER_BVZERO( &(*ap)->a_vals[ last ] );
320 ch_free( (*ap)->a_vals[ j ].bv_val );
321 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
325 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
330 struct ldapmapping *mapping = NULL;
332 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
334 if ( mapping == NULL ) {
335 if ( rwmap->rwm_at.drop_missing ) {
340 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
341 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
344 * FIXME: rewrite could fail; in this case
345 * the operation should give up, right?
347 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
349 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
354 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
355 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
357 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
358 if ( rc != LDAP_SUCCESS ) {
363 if ( mapping != NULL ) {
364 assert( mapping->m_dst_ad != NULL );
365 (*ap)->a_desc = mapping->m_dst_ad;
374 /* FIXME: leaking attribute/values? */
381 op->o_callback = &roc->cb;
383 return SLAP_CB_CONTINUE;
387 rwm_conn_init( BackendDB *be, Connection *conn )
389 slap_overinst *on = (slap_overinst *) be->bd_info;
390 struct ldaprwmap *rwmap =
391 (struct ldaprwmap *)on->on_bi.bi_private;
393 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
395 return SLAP_CB_CONTINUE;
399 rwm_conn_destroy( BackendDB *be, Connection *conn )
401 slap_overinst *on = (slap_overinst *) be->bd_info;
402 struct ldaprwmap *rwmap =
403 (struct ldaprwmap *)on->on_bi.bi_private;
405 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
407 return SLAP_CB_CONTINUE;
411 rwm_op_bind( Operation *op, SlapReply *rs )
413 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
416 rwm_op_cb *roc = rwm_callback_get( op );
418 rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
419 if ( rc != LDAP_SUCCESS ) {
420 op->o_bd->bd_info = (BackendInfo *)on->on_info;
421 send_ldap_error( op, rs, rc, "bindDN massage error" );
425 overlay_callback_after_backover( op, &roc->cb, 1 );
427 return SLAP_CB_CONTINUE;
431 rwm_op_unbind( Operation *op, SlapReply *rs )
433 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
434 struct ldaprwmap *rwmap =
435 (struct ldaprwmap *)on->on_bi.bi_private;
437 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
439 return SLAP_CB_CONTINUE;
443 rwm_op_compare( Operation *op, SlapReply *rs )
445 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
446 struct ldaprwmap *rwmap =
447 (struct ldaprwmap *)on->on_bi.bi_private;
450 struct berval mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
452 rwm_op_cb *roc = rwm_callback_get( op );
454 rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
455 if ( rc != LDAP_SUCCESS ) {
456 op->o_bd->bd_info = (BackendInfo *)on->on_info;
457 send_ldap_error( op, rs, rc, "compareDN massage error" );
461 /* if the attribute is an objectClass, try to remap its value */
462 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
463 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
465 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
466 &mapped_vals[0], RWM_MAP );
467 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
469 op->o_bd->bd_info = (BackendInfo *)on->on_info;
470 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
473 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
474 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
479 struct ldapmapping *mapping = NULL;
480 AttributeDescription *ad = op->orc_ava->aa_desc;
482 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
484 if ( mapping == NULL ) {
485 if ( rwmap->rwm_at.drop_missing ) {
486 op->o_bd->bd_info = (BackendInfo *)on->on_info;
487 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
492 assert( mapping->m_dst_ad != NULL );
493 ad = mapping->m_dst_ad;
496 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
497 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
499 struct berval *mapped_valsp[2];
501 mapped_valsp[0] = &mapped_vals[0];
502 mapped_valsp[1] = &mapped_vals[1];
504 mapped_vals[0] = op->orc_ava->aa_value;
506 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
508 if ( rc != LDAP_SUCCESS ) {
509 op->o_bd->bd_info = (BackendInfo *)on->on_info;
510 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
514 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
515 /* NOTE: if we get here, rwm_dnattr_rewrite()
516 * already freed the old value, so now
518 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
520 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
523 op->orc_ava->aa_desc = ad;
526 op->o_callback = &roc->cb;
528 return SLAP_CB_CONTINUE;
532 rwm_op_delete( Operation *op, SlapReply *rs )
534 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
537 rwm_op_cb *roc = rwm_callback_get( op );
539 rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
540 if ( rc != LDAP_SUCCESS ) {
541 op->o_bd->bd_info = (BackendInfo *)on->on_info;
542 send_ldap_error( op, rs, rc, "deleteDN massage error" );
546 op->o_callback = &roc->cb;
548 return SLAP_CB_CONTINUE;
552 rwm_op_modify( Operation *op, SlapReply *rs )
554 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
555 struct ldaprwmap *rwmap =
556 (struct ldaprwmap *)on->on_bi.bi_private;
562 rwm_op_cb *roc = rwm_callback_get( op );
564 rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
565 if ( rc != LDAP_SUCCESS ) {
566 op->o_bd->bd_info = (BackendInfo *)on->on_info;
567 send_ldap_error( op, rs, rc, "modifyDN massage error" );
571 isupdate = be_shadow_update( op );
572 for ( mlp = &op->orm_modlist; *mlp; ) {
574 Modifications *ml = *mlp;
575 struct ldapmapping *mapping = NULL;
577 /* ml points to a temporary mod until needs duplication */
578 if ( ml->sml_desc == slap_schema.si_ad_objectClass
579 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
583 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
585 ml = ch_malloc( sizeof( Modifications ) );
587 if ( (*mlp)->sml_values ) {
588 ber_bvarray_dup_x( &ml->sml_values, (*mlp)->sml_values, NULL );
589 if ( (*mlp)->sml_nvalues ) {
590 ber_bvarray_dup_x( &ml->sml_nvalues, (*mlp)->sml_nvalues, NULL );
599 drop_missing = rwm_mapping( &rwmap->rwm_at,
600 &ml->sml_desc->ad_cname,
602 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
608 /* duplicate the modlist */
609 ml = ch_malloc( sizeof( Modifications ));
613 if ( ml->sml_values != NULL ) {
617 for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
620 bva = ch_malloc( (num+1) * sizeof( struct berval ));
621 for (i=0; i<num; i++)
622 ber_dupbv( &bva[i], &ml->sml_values[i] );
623 BER_BVZERO( &bva[i] );
624 ml->sml_values = bva;
626 if ( ml->sml_nvalues ) {
627 bva = ch_malloc( (num+1) * sizeof( struct berval ));
628 for (i=0; i<num; i++)
629 ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
630 BER_BVZERO( &bva[i] );
631 ml->sml_nvalues = bva;
639 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
640 struct ldapmapping *oc_mapping = NULL;
642 ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
643 &oc_mapping, RWM_MAP );
644 if ( oc_mapping == NULL ) {
645 if ( rwmap->rwm_at.drop_missing ) {
646 /* FIXME: we allow to remove objectClasses as well;
647 * if the resulting entry is inconsistent, that's
648 * the relayed database's business...
651 ch_free( ml->sml_values[ j ].bv_val );
652 ml->sml_values[ j ] = ml->sml_values[ last ];
654 BER_BVZERO( &ml->sml_values[ last ] );
660 ch_free( ml->sml_values[ j ].bv_val );
661 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
666 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
667 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
669 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
671 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
673 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
674 rc = rwm_referral_rewrite( op, rs,
677 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
678 if ( rc != LDAP_SUCCESS ) {
683 if ( rc != LDAP_SUCCESS ) {
690 if ( mapping != NULL ) {
691 /* use new attribute description */
692 assert( mapping->m_dst_ad != NULL );
693 ml->sml_desc = mapping->m_dst_ad;
700 *mlp = (*mlp)->sml_next;
705 *mlp = (*mlp)->sml_next;
706 slap_mod_free( &ml->sml_mod, 0 );
710 op->o_callback = &roc->cb;
712 return SLAP_CB_CONTINUE;
716 rwm_op_modrdn( Operation *op, SlapReply *rs )
718 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
719 struct ldaprwmap *rwmap =
720 (struct ldaprwmap *)on->on_bi.bi_private;
725 rwm_op_cb *roc = rwm_callback_get( op );
727 if ( op->orr_newSup ) {
728 struct berval nnewSup = BER_BVNULL;
729 struct berval newSup = BER_BVNULL;
732 * Rewrite the new superior, if defined and required
735 dc.conn = op->o_conn;
737 dc.ctx = "newSuperiorDN";
738 newSup = *op->orr_newSup;
739 nnewSup = *op->orr_nnewSup;
740 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
741 if ( rc != LDAP_SUCCESS ) {
742 op->o_bd->bd_info = (BackendInfo *)on->on_info;
743 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
747 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
748 op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
750 op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
752 *op->orr_newSup = newSup;
753 *op->orr_nnewSup = nnewSup;
758 * Rewrite the newRDN, if needed
761 struct berval newrdn = BER_BVNULL;
762 struct berval nnewrdn = BER_BVNULL;
765 dc.conn = op->o_conn;
768 newrdn = op->orr_newrdn;
769 nnewrdn = op->orr_nnewrdn;
770 rc = rwm_dn_massage_pretty_normalize( &dc, &op->orr_newrdn, &newrdn, &nnewrdn );
771 if ( rc != LDAP_SUCCESS ) {
772 op->o_bd->bd_info = (BackendInfo *)on->on_info;
773 send_ldap_error( op, rs, rc, "newRDN massage error" );
777 if ( op->orr_newrdn.bv_val != newrdn.bv_val ) {
778 op->orr_newrdn = newrdn;
779 op->orr_nnewrdn = nnewrdn;
784 * Rewrite the dn, if needed
786 rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
787 if ( rc != LDAP_SUCCESS ) {
788 op->o_bd->bd_info = (BackendInfo *)on->on_info;
789 send_ldap_error( op, rs, rc, "renameDN massage error" );
793 op->o_callback = &roc->cb;
795 rc = SLAP_CB_CONTINUE;
799 if ( op->orr_newSup != roc->ros.orr_newSup ) {
800 ch_free( op->orr_newSup->bv_val );
801 ch_free( op->orr_nnewSup->bv_val );
802 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
803 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
804 op->orr_newSup = roc->ros.orr_newSup;
805 op->orr_nnewSup = roc->ros.orr_nnewSup;
808 if ( op->orr_newrdn.bv_val != roc->ros.orr_newrdn.bv_val ) {
809 ch_free( op->orr_newrdn.bv_val );
810 ch_free( op->orr_nnewrdn.bv_val );
811 op->orr_newrdn = roc->ros.orr_newrdn;
812 op->orr_nnewrdn = roc->ros.orr_nnewrdn;
821 rwm_swap_attrs( Operation *op, SlapReply *rs )
823 slap_callback *cb = op->o_callback;
824 rwm_op_state *ros = cb->sc_private;
826 rs->sr_attrs = ros->ors_attrs;
828 /* other overlays might have touched op->ors_attrs,
829 * so we restore the original version here, otherwise
830 * attribute-mapping might fail */
831 op->ors_attrs = ros->mapped_attrs;
833 return SLAP_CB_CONTINUE;
837 * NOTE: this implementation of get/release entry is probably far from
838 * optimal. The rationale consists in intercepting the request directed
839 * to the underlying database, in order to rewrite/remap the request,
840 * perform it using the modified data, duplicate the resulting entry
841 * and finally free it when release is called.
842 * This implies that subsequent overlays are not called, as the request
843 * is directly shunted to the underlying database.
846 rwm_entry_release_rw( Operation *op, Entry *e, int rw )
848 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
851 if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
852 return SLAP_CB_CONTINUE;
855 /* just free entry if (probably) ours */
856 if ( e->e_private == NULL && BER_BVISNULL( &e->e_bv ) ) {
861 return SLAP_CB_CONTINUE;
865 rwm_entry_get_rw( Operation *op, struct berval *ndn,
866 ObjectClass *oc, AttributeDescription *at, int rw, Entry **ep )
868 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
872 SlapReply rs = { REP_SEARCH };
874 rwm_op_state ros = { 0 };
875 struct berval mndn = BER_BVNULL;
877 if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
878 return SLAP_CB_CONTINUE;
882 op2.o_tag = LDAP_REQ_SEARCH;
885 op2.o_req_ndn = *ndn;
886 rc = rwm_op_dn_massage( &op2, &rs, "searchDN", &ros );
887 if ( rc != LDAP_SUCCESS ) {
891 mndn = BER_BVISNULL( &ros.r_ndn ) ? *ndn : ros.r_ndn;
893 /* map attribute & objectClass */
903 op2.o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
904 op2.ors_attrs = slap_anlist_all_attributes;
905 rc = op2.o_bd->bd_info->bi_entry_get_rw( &op2, &mndn, oc, at, rw, ep );
906 if ( rc == LDAP_SUCCESS && *ep != NULL ) {
907 /* we assume be_entry_release() needs to be called */
908 rs.sr_flags = REP_ENTRY_MUSTRELEASE;
911 /* duplicate & release */
912 op2.o_bd->bd_info = (BackendInfo *)on;
913 rc = rwm_send_entry( &op2, &rs );
914 RS_ASSERT( rs.sr_flags & REP_ENTRY_MUSTFLUSH );
915 if ( rc == SLAP_CB_CONTINUE ) {
919 assert( rc != LDAP_SUCCESS && rs.sr_entry == *ep );
921 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
922 be_entry_release_r( &op2, rs.sr_entry );
923 op2.o_bd->bd_info = (BackendInfo *)on;
927 if ( !BER_BVISNULL( &ros.r_ndn) && ros.r_ndn.bv_val != ndn->bv_val ) {
928 op->o_tmpfree( ros.r_ndn.bv_val, op->o_tmpmemctx );
935 rwm_op_search( Operation *op, SlapReply *rs )
937 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
938 struct ldaprwmap *rwmap =
939 (struct ldaprwmap *)on->on_bi.bi_private;
944 struct berval fstr = BER_BVNULL;
947 AttributeName *an = NULL;
951 rwm_op_cb *roc = rwm_callback_get( op );
953 rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
954 "searchFilter", op->ors_filterstr.bv_val );
955 if ( rc == LDAP_SUCCESS )
956 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
957 if ( rc != LDAP_SUCCESS ) {
958 text = "searchDN massage error";
963 * Rewrite the dn if needed
966 dc.conn = op->o_conn;
968 dc.ctx = "searchFilterAttrDN";
970 rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
971 if ( rc != LDAP_SUCCESS ) {
972 text = "searchFilter/searchFilterAttrDN massage error";
976 f = str2filter_x( op, fstr.bv_val );
979 text = "massaged filter parse error";
984 op->ors_filterstr = fstr;
986 rc = rwm_map_attrnames( op, &rwmap->rwm_at, &rwmap->rwm_oc,
987 op->ors_attrs, &an, RWM_MAP );
988 if ( rc != LDAP_SUCCESS ) {
989 text = "attribute list mapping error";
994 /* store the mapped Attributes for later usage, in
995 * the case that other overlays change op->ors_attrs */
996 roc->ros.mapped_attrs = an;
997 roc->cb.sc_response = rwm_swap_attrs;
999 op->o_callback = &roc->cb;
1001 return SLAP_CB_CONTINUE;
1009 filter_free_x( op, f, 1 );
1012 if ( !BER_BVISNULL( &fstr ) ) {
1013 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1016 rwm_op_rollback( op, rs, &roc->ros );
1017 op->oq_search = roc->ros.oq_search;
1018 op->o_tmpfree( roc, op->o_tmpmemctx );
1020 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1021 send_ldap_error( op, rs, rc, text );
1028 rwm_exop_passwd( Operation *op, SlapReply *rs )
1030 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1034 struct berval id = BER_BVNULL,
1037 BerElement *ber = NULL;
1039 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
1040 return LDAP_SUCCESS;
1043 if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
1044 rs->sr_err = LDAP_OTHER;
1048 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
1049 &pwold, &pwnew, &rs->sr_text );
1050 if ( rs->sr_err != LDAP_SUCCESS ) {
1054 if ( !BER_BVISNULL( &id ) ) {
1055 char idNul = id.bv_val[id.bv_len];
1056 id.bv_val[id.bv_len] = '\0';
1057 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
1058 &op->o_req_ndn, op->o_tmpmemctx );
1059 id.bv_val[id.bv_len] = idNul;
1060 if ( rs->sr_err != LDAP_SUCCESS ) {
1061 rs->sr_text = "Invalid DN";
1066 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
1067 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
1070 roc = rwm_callback_get( op );
1072 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1073 if ( rc != LDAP_SUCCESS ) {
1074 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1075 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1079 ber = ber_alloc_t( LBER_USE_DER );
1081 rs->sr_err = LDAP_OTHER;
1082 rs->sr_text = "No memory";
1085 ber_printf( ber, "{" );
1086 if ( !BER_BVISNULL( &id )) {
1087 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1090 if ( !BER_BVISNULL( &pwold )) {
1091 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
1093 if ( !BER_BVISNULL( &pwnew )) {
1094 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
1096 ber_printf( ber, "N}" );
1097 ber_flatten( ber, &op->ore_reqdata );
1100 op->o_callback = &roc->cb;
1102 return SLAP_CB_CONTINUE;
1105 static struct exop {
1107 BI_op_extended *extended;
1109 { BER_BVC(LDAP_EXOP_MODIFY_PASSWD), rwm_exop_passwd },
1110 { BER_BVNULL, NULL }
1114 rwm_extended( Operation *op, SlapReply *rs )
1116 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1122 for ( i = 0; exop_table[i].extended != NULL; i++ ) {
1123 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
1125 rc = exop_table[i].extended( op, rs );
1130 case SLAP_CB_CONTINUE:
1135 send_ldap_result( op, rs );
1142 roc = rwm_callback_get( op );
1144 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1145 if ( rc != LDAP_SUCCESS ) {
1146 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1147 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1151 /* TODO: rewrite/map extended data ? ... */
1152 op->o_callback = &roc->cb;
1154 return SLAP_CB_CONTINUE;
1158 rwm_matched( Operation *op, SlapReply *rs )
1160 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1161 struct ldaprwmap *rwmap =
1162 (struct ldaprwmap *)on->on_bi.bi_private;
1164 struct berval dn, mdn;
1168 if ( rs->sr_matched == NULL ) {
1173 dc.conn = op->o_conn;
1175 dc.ctx = "matchedDN";
1176 ber_str2bv( rs->sr_matched, 0, 0, &dn );
1178 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
1179 if ( rc != LDAP_SUCCESS ) {
1181 rs->sr_text = "Rewrite error";
1183 } else if ( mdn.bv_val != dn.bv_val ) {
1184 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
1185 ch_free( (void *)rs->sr_matched );
1188 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
1190 rs->sr_matched = mdn.bv_val;
1195 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
1197 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1198 struct ldaprwmap *rwmap =
1199 (struct ldaprwmap *)on->on_bi.bi_private;
1205 int check_duplicate_attrs = 0;
1208 * Rewrite the dn attrs, if needed
1211 dc.conn = op->o_conn;
1214 /* FIXME: the entries are in the remote mapping form;
1215 * so we need to select those attributes we are willing
1216 * to return, and remap them accordingly */
1218 /* FIXME: in principle, one could map an attribute
1219 * on top of another, which already exists.
1220 * As such, in the end there might exist more than
1221 * one instance of an attribute.
1222 * We should at least check if this occurs, and issue
1223 * an error (because multiple instances of attrs in
1224 * response are not valid), or merge the values (what
1225 * about duplicate values?) */
1226 isupdate = be_shadow_update( op );
1227 for ( ap = a_first; *ap; ) {
1228 struct ldapmapping *mapping = NULL;
1233 if ( ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS ) &&
1234 op->ors_attrs != NULL &&
1235 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1236 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1241 drop_missing = rwm_mapping( &rwmap->rwm_at,
1242 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1243 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1247 if ( mapping != NULL ) {
1248 assert( mapping->m_dst_ad != NULL );
1250 /* try to normalize mapped Attributes if the original
1251 * AttributeType was not normalized */
1252 if ( (!(*ap)->a_desc->ad_type->sat_equality ||
1253 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1254 mapping->m_dst_ad->ad_type->sat_equality &&
1255 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1257 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
1261 last = (*ap)->a_numvals;
1264 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1266 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1269 * check that each value is valid per syntax
1270 * and pretty if appropriate
1272 rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1273 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1274 mapping->m_dst_ad->ad_type->sat_syntax,
1275 mapping->m_dst_ad->ad_type->sat_equality,
1276 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1279 if ( rc != LDAP_SUCCESS ) {
1280 BER_BVZERO( &(*ap)->a_nvals[i] );
1283 BER_BVZERO( &(*ap)->a_nvals[i] );
1287 assert( (*ap)->a_nvals == (*ap)->a_vals );
1288 (*ap)->a_nvals = NULL;
1289 ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
1293 /* rewrite the attribute description */
1294 (*ap)->a_desc = mapping->m_dst_ad;
1296 /* will need to check for duplicate attrs */
1297 check_duplicate_attrs++;
1300 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1301 if ( stripEntryDN ) {
1302 /* will be generated by frontend */
1306 } else if ( !isupdate
1308 && (*ap)->a_desc->ad_type->sat_no_user_mod
1309 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1314 if ( last == -1 ) { /* not yet counted */
1315 last = (*ap)->a_numvals;
1319 /* empty? leave it in place because of attrsonly and vlv */
1324 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1325 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1329 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1330 struct berval mapped;
1332 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1333 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1335 ch_free( bv[0].bv_val );
1336 BER_BVZERO( &bv[0] );
1337 if ( &(*ap)->a_vals[last] > &bv[0] ) {
1338 bv[0] = (*ap)->a_vals[last];
1339 BER_BVZERO( &(*ap)->a_vals[last] );
1344 } else if ( mapped.bv_val != bv[0].bv_val
1345 && ber_bvstrcasecmp( &mapped, &bv[0] ) != 0 )
1349 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1350 if ( &(*ap)->a_vals[ i ] == bv ) {
1354 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1359 if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1364 * FIXME: after LBER_FREEing
1365 * the value is replaced by
1366 * ch_alloc'ed memory
1368 ber_bvreplace( &bv[0], &mapped );
1370 /* FIXME: will need to check
1371 * if the structuralObjectClass
1377 * It is necessary to try to rewrite attributes with
1378 * dn syntax because they might be used in ACLs as
1379 * members of groups; since ACLs are applied to the
1380 * rewritten stuff, no dn-based subject clause could
1381 * be used at the ldap backend side (see
1382 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1383 * The problem can be overcome by moving the dn-based
1384 * ACLs to the target directory server, and letting
1385 * everything pass thru the ldap backend. */
1386 /* FIXME: handle distinguishedName-like syntaxes, like
1387 * nameAndOptionalUID */
1388 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1389 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1391 dc.ctx = "searchAttrDN";
1392 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals, (*ap)->a_nvals );
1393 if ( rc != LDAP_SUCCESS ) {
1397 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1398 dc.ctx = "searchAttrDN";
1399 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1400 if ( rc != LDAP_SUCCESS ) {
1407 ap = &(*ap)->a_next;
1412 *ap = (*ap)->a_next;
1417 /* only check if some mapping occurred */
1418 if ( check_duplicate_attrs ) {
1419 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1422 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1423 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1425 Modification mod = { 0 };
1426 const char *text = NULL;
1427 char textbuf[ SLAP_TEXT_BUFLEN ];
1428 Attribute *next = (*tap)->a_next;
1430 BER_BVSTR( &e.e_name, "" );
1431 BER_BVSTR( &e.e_nname, "" );
1433 mod.sm_op = LDAP_MOD_ADD;
1434 mod.sm_desc = (*ap)->a_desc;
1435 mod.sm_type = mod.sm_desc->ad_cname;
1436 mod.sm_numvals = (*tap)->a_numvals;
1437 mod.sm_values = (*tap)->a_vals;
1438 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
1439 mod.sm_nvalues = (*tap)->a_nvals;
1442 (void)modify_add_values( &e, &mod,
1444 &text, textbuf, sizeof( textbuf ) );
1446 /* should not insert new attrs! */
1447 assert( e.e_attrs == *ap );
1453 tap = &(*tap)->a_next;
1462 /* Should return SLAP_CB_CONTINUE or failure, never LDAP_SUCCESS. */
1464 rwm_send_entry( Operation *op, SlapReply *rs )
1466 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1467 struct ldaprwmap *rwmap =
1468 (struct ldaprwmap *)on->on_bi.bi_private;
1471 struct berval dn = BER_BVNULL,
1476 assert( rs->sr_entry != NULL );
1479 * Rewrite the dn of the result, if needed
1482 dc.conn = op->o_conn;
1484 dc.ctx = "searchEntryDN";
1487 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1488 /* FIXME: all we need to duplicate are:
1491 * - attributes that are requested
1492 * - no values if attrsonly is set
1496 rc = LDAP_NO_MEMORY;
1499 } else if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1500 /* ITS#6423: REP_ENTRY_MUSTRELEASE incompatible
1501 * with REP_ENTRY_MODIFIABLE */
1508 * Note: this may fail if the target host(s) schema differs
1509 * from the one known to the meta, and a DN with unknown
1510 * attributes is returned.
1514 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1515 if ( rc != LDAP_SUCCESS ) {
1520 if ( e->e_name.bv_val != dn.bv_val ) {
1521 ch_free( e->e_name.bv_val );
1522 ch_free( e->e_nname.bv_val );
1528 /* TODO: map entry attribute types, objectclasses
1529 * and dn-valued attribute values */
1531 /* FIXME: the entries are in the remote mapping form;
1532 * so we need to select those attributes we are willing
1533 * to return, and remap them accordingly */
1534 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1536 if ( e != rs->sr_entry ) {
1537 /* Reimplementing rs_replace_entry(), I suppose to
1538 * bypass our own dubious rwm_entry_release_rw() */
1539 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1540 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1541 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1542 be_entry_release_r( op, rs->sr_entry );
1543 op->o_bd->bd_info = (BackendInfo *)on;
1544 } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
1545 entry_free( rs->sr_entry );
1548 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
1551 return SLAP_CB_CONTINUE;
1554 if ( e != NULL && e != rs->sr_entry ) {
1555 if ( e->e_name.bv_val == dn.bv_val ) {
1556 BER_BVZERO( &e->e_name );
1559 if ( e->e_nname.bv_val == ndn.bv_val ) {
1560 BER_BVZERO( &e->e_nname );
1566 if ( !BER_BVISNULL( &dn ) ) {
1567 ch_free( dn.bv_val );
1570 if ( !BER_BVISNULL( &ndn ) ) {
1571 ch_free( ndn.bv_val );
1578 rwm_operational( Operation *op, SlapReply *rs )
1580 /* FIXME: the entries are in the remote mapping form;
1581 * so we need to select those attributes we are willing
1582 * to return, and remap them accordingly */
1583 if ( rs->sr_operational_attrs ) {
1584 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1587 return SLAP_CB_CONTINUE;
1591 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1592 * rewritten for subsequent operations; fine for plain suffixmassage,
1593 * but destroys everything else */
1595 rwm_chk_referrals( Operation *op, SlapReply *rs )
1597 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1600 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1601 if ( rc != LDAP_SUCCESS ) {
1602 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1603 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1607 return SLAP_CB_CONTINUE;
1619 slap_overinst *on = (slap_overinst *) be->bd_info;
1620 struct ldaprwmap *rwmap =
1621 (struct ldaprwmap *)on->on_bi.bi_private;
1623 return rewrite_parse( rwmap->rwm_rw,
1624 fname, lineno, argc, argv );
1630 rwm_suffixmassage_config(
1637 slap_overinst *on = (slap_overinst *) be->bd_info;
1638 struct ldaprwmap *rwmap =
1639 (struct ldaprwmap *)on->on_bi.bi_private;
1641 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1648 * suffixmassage [<suffix>] <massaged suffix>
1650 * the [<suffix>] field must be defined as a valid suffix
1651 * for the current database;
1652 * the <massaged suffix> shouldn't have already been
1653 * defined as a valid suffix for the current server
1656 if ( be->be_suffix == NULL ) {
1657 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1658 " \"suffixMassage [<suffix>]"
1659 " <massaged suffix>\" without "
1660 "<suffix> part requires database "
1661 "suffix be defined first.\n",
1665 bvnc = be->be_suffix[ 0 ];
1668 } else if ( argc == 3 ) {
1669 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1673 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is"
1674 " \"suffixMassage [<suffix>]"
1675 " <massaged suffix>\"\n",
1680 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1681 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1682 fname, lineno, bvnc.bv_val );
1686 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1687 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1688 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1689 fname, lineno, brnc.bv_val );
1690 free( nvnc.bv_val );
1691 free( pvnc.bv_val );
1696 * The suffix massaging is emulated
1697 * by means of the rewrite capabilities
1699 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1700 &pvnc, &nvnc, &prnc, &nrnc );
1701 free( nvnc.bv_val );
1702 free( pvnc.bv_val );
1703 free( nrnc.bv_val );
1704 free( prnc.bv_val );
1717 slap_overinst *on = (slap_overinst *) be->bd_info;
1718 struct ldaprwmap *rwmap =
1719 (struct ldaprwmap *)on->on_bi.bi_private;
1721 /* objectclass/attribute mapping */
1722 return rwm_map_config( &rwmap->rwm_oc,
1724 fname, lineno, argc, argv );
1728 rwm_response( Operation *op, SlapReply *rs )
1730 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1731 struct ldaprwmap *rwmap =
1732 (struct ldaprwmap *)on->on_bi.bi_private;
1736 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1737 return rwm_send_entry( op, rs );
1740 switch( op->o_tag ) {
1741 case LDAP_REQ_SEARCH:
1744 case LDAP_REQ_DELETE:
1745 case LDAP_REQ_MODRDN:
1746 case LDAP_REQ_MODIFY:
1747 case LDAP_REQ_COMPARE:
1748 case LDAP_REQ_EXTENDED:
1753 * Rewrite the dn of the referrals, if needed
1756 dc.conn = op->o_conn;
1758 dc.ctx = "referralDN";
1759 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1760 /* FIXME: impossible, so far */
1761 if ( rc != LDAP_SUCCESS ) {
1769 rwm_matched( op, rs );
1773 return SLAP_CB_CONTINUE;
1784 slap_overinst *on = (slap_overinst *) be->bd_info;
1785 struct ldaprwmap *rwmap =
1786 (struct ldaprwmap *)on->on_bi.bi_private;
1791 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1793 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1796 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1797 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1799 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1800 rc = rwm_m_config( be, fname, lineno, argc, argv );
1802 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1803 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1805 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1807 Debug( LDAP_DEBUG_ANY,
1808 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1813 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1814 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1816 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1817 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1819 /* TODO: not implemented yet */
1820 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1821 Debug( LDAP_DEBUG_ANY,
1822 "%s: line %d: \"discover\" not supported yet "
1823 "in \"t-f-support {no|yes|discover}\".\n",
1827 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1831 Debug( LDAP_DEBUG_ANY,
1832 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1833 fname, lineno, argv[ 1 ] );
1837 } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) == 0 ) {
1839 Debug( LDAP_DEBUG_ANY,
1840 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1845 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1846 rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1848 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1849 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1853 rc = SLAP_CONF_UNKNOWN;
1864 * dynamic configuration...
1874 RWM_CF_NORMALIZE_MAPPED,
1875 RWM_CF_DROP_UNREQUESTED,
1880 static slap_verbmasks t_f_mode[] = {
1881 { BER_BVC( "true" ), RWM_F_SUPPORT_T_F },
1882 { BER_BVC( "yes" ), RWM_F_SUPPORT_T_F },
1883 { BER_BVC( "discover" ), RWM_F_SUPPORT_T_F_DISCOVER },
1884 { BER_BVC( "false" ), RWM_F_NONE },
1885 { BER_BVC( "no" ), RWM_F_NONE },
1889 static ConfigDriver rwm_cf_gen;
1891 static ConfigTable rwmcfg[] = {
1892 { "rwm-rewrite", "rewrite",
1893 2, 0, STRLENOF("rwm-rewrite"),
1894 ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1895 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1896 "DESC 'Rewrites strings' "
1897 "EQUALITY caseIgnoreMatch "
1898 "SYNTAX OMsDirectoryString "
1899 "X-ORDERED 'VALUES' )",
1902 { "rwm-suffixmassage", "[virtual]> <real",
1903 2, 3, 0, ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1906 { "rwm-t-f-support", "true|false|discover",
1907 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1908 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1909 "DESC 'Absolute filters support' "
1910 "SYNTAX OMsDirectoryString "
1914 { "rwm-map", "{objectClass|attribute}",
1915 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1916 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1917 "DESC 'maps attributes/objectClasses' "
1918 "EQUALITY caseIgnoreMatch "
1919 "SYNTAX OMsDirectoryString "
1920 "X-ORDERED 'VALUES' )",
1923 { "rwm-normalize-mapped-attrs", "true|false",
1924 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1925 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1926 "DESC 'Normalize mapped attributes/objectClasses' "
1927 "SYNTAX OMsBoolean "
1931 { "rwm-drop-unrequested-attrs", "true|false",
1932 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_DROP_UNREQUESTED, rwm_cf_gen,
1933 "( OLcfgOvAt:16.5 NAME 'olcRwmDropUnrequested' "
1934 "DESC 'Drop unrequested attributes' "
1935 "SYNTAX OMsBoolean "
1939 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1942 static ConfigOCs rwmocs[] = {
1943 { "( OLcfgOvOc:16.1 "
1944 "NAME 'olcRwmConfig' "
1945 "DESC 'Rewrite/remap configuration' "
1946 "SUP olcOverlayConfig "
1949 "olcRwmTFSupport $ "
1951 "olcRwmNormalizeMapped "
1953 Cft_Overlay, rwmcfg, NULL, NULL },
1958 slap_bv_x_ordered_unparse( BerVarray in, BerVarray *out )
1961 BerVarray bva = NULL;
1962 char ibuf[32], *ptr;
1965 assert( in != NULL );
1967 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1976 bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1977 BER_BVZERO( &bva[ 0 ] );
1979 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1980 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1981 if ( idx.bv_len >= sizeof( ibuf ) ) {
1982 ber_bvarray_free( bva );
1986 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1987 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1988 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1989 ptr = lutil_strcopy( ptr, in[i].bv_val );
1991 BER_BVZERO( &bva[ i + 1 ] );
2006 line = ldap_charray2str( argv, "\" \"" );
2007 if ( line != NULL ) {
2008 int len = strlen( argv[ 0 ] );
2010 ber_str2bv( line, 0, 0, &bv );
2011 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
2012 bv.bv_len - ( len + 1 ) );
2013 bv.bv_val[ bv.bv_len - 1 ] = '"';
2016 ber_bvarray_add( bva, &bv );
2029 rwm_bva_rewrite_add(
2030 struct ldaprwmap *rwmap,
2034 return rwm_bva_add( &rwmap->rwm_bva_rewrite, idx, argv );
2040 struct ldaprwmap *rwmap,
2044 return rwm_bva_add( &rwmap->rwm_bva_map, idx, argv );
2049 rwm_info_init( struct rewrite_info ** rwm_rw )
2053 *rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
2054 if ( *rwm_rw == NULL ) {
2058 /* this rewriteContext by default must be null;
2059 * rules can be added if required */
2060 rargv[ 0 ] = "rewriteContext";
2061 rargv[ 1 ] = "searchFilter";
2063 rewrite_parse( *rwm_rw, "<suffix massage>", 1, 2, rargv );
2065 rargv[ 0 ] = "rewriteContext";
2066 rargv[ 1 ] = "default";
2068 rewrite_parse( *rwm_rw, "<suffix massage>", 2, 2, rargv );
2074 rwm_cf_gen( ConfigArgs *c )
2076 slap_overinst *on = (slap_overinst *)c->bi;
2077 struct ldaprwmap *rwmap =
2078 (struct ldaprwmap *)on->on_bi.bi_private;
2088 if ( c->op == SLAP_CONFIG_EMIT ) {
2089 struct berval bv = BER_BVNULL;
2091 switch ( c->type ) {
2092 case RWM_CF_REWRITE:
2093 if ( rwmap->rwm_bva_rewrite == NULL ) {
2097 slap_bv_x_ordered_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
2098 if ( !c->rvalue_vals ) {
2104 case RWM_CF_T_F_SUPPORT:
2105 enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
2106 if ( BER_BVISNULL( &bv ) ) {
2107 /* there's something wrong... */
2112 value_add_one( &c->rvalue_vals, &bv );
2117 if ( rwmap->rwm_bva_map == NULL ) {
2121 slap_bv_x_ordered_unparse( rwmap->rwm_bva_map, &c->rvalue_vals );
2122 if ( !c->rvalue_vals ) {
2128 case RWM_CF_NORMALIZE_MAPPED:
2129 c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
2132 case RWM_CF_DROP_UNREQUESTED:
2133 c->value_int = ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS );
2143 } else if ( c->op == LDAP_MOD_DELETE ) {
2144 switch ( c->type ) {
2145 case RWM_CF_REWRITE:
2146 if ( c->valx >= 0 ) {
2149 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2152 if ( c->valx >= i ) {
2157 ber_memfree( rwmap->rwm_bva_rewrite[ c->valx ].bv_val );
2158 for ( i = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i + 1 ] ); i++ )
2160 rwmap->rwm_bva_rewrite[ i ] = rwmap->rwm_bva_rewrite[ i + 1 ];
2162 BER_BVZERO( &rwmap->rwm_bva_rewrite[ i ] );
2164 rewrite_info_delete( &rwmap->rwm_rw );
2165 assert( rwmap->rwm_rw == NULL );
2167 rc = rwm_info_init( &rwmap->rwm_rw );
2169 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2171 ConfigArgs ca = { 0 };
2173 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2175 config_fp_parse_line( &ca );
2177 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2178 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2182 rc = rwm_rw_config( &db, c->fname, c->lineno,
2186 ch_free( ca.tline );
2192 } else if ( rwmap->rwm_rw != NULL ) {
2193 rewrite_info_delete( &rwmap->rwm_rw );
2194 assert( rwmap->rwm_rw == NULL );
2196 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2197 rwmap->rwm_bva_rewrite = NULL;
2199 rc = rwm_info_init( &rwmap->rwm_rw );
2203 case RWM_CF_T_F_SUPPORT:
2204 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2208 if ( c->valx >= 0 ) {
2209 struct ldapmap rwm_oc = rwmap->rwm_oc;
2210 struct ldapmap rwm_at = rwmap->rwm_at;
2214 if ( rwmap->rwm_bva_map ) {
2215 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2219 if ( c->valx >= cnt ) {
2224 memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2225 memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2227 /* re-parse all mappings except the one
2228 * that needs to be eliminated */
2230 for ( cnt = 0; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2231 ConfigArgs ca = { 0 };
2233 if ( cnt == c->valx ) {
2237 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2239 config_fp_parse_line( &ca );
2241 argv[1] = ca.argv[0];
2242 argv[2] = ca.argv[1];
2243 argv[3] = ca.argv[2];
2244 argv[4] = ca.argv[3];
2246 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2248 ch_free( ca.tline );
2251 /* in case of failure, restore
2252 * the existing mapping */
2254 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2255 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2256 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2257 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2258 rwmap->rwm_oc = rwm_oc;
2259 rwmap->rwm_at = rwm_at;
2264 /* in case of success, destroy the old mapping
2265 * and eliminate the deleted one */
2267 avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2268 avl_free( rwm_oc.map, rwm_mapping_free );
2269 avl_free( rwm_at.remap, rwm_mapping_dst_free );
2270 avl_free( rwm_at.map, rwm_mapping_free );
2272 ber_memfree( rwmap->rwm_bva_map[ c->valx ].bv_val );
2273 for ( cnt = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2274 rwmap->rwm_bva_map[ cnt ] = rwmap->rwm_bva_map[ cnt + 1 ];
2279 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2280 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2281 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2282 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2284 rwmap->rwm_oc.remap = NULL;
2285 rwmap->rwm_oc.map = NULL;
2286 rwmap->rwm_at.remap = NULL;
2287 rwmap->rwm_at.map = NULL;
2289 ber_bvarray_free( rwmap->rwm_bva_map );
2290 rwmap->rwm_bva_map = NULL;
2294 case RWM_CF_NORMALIZE_MAPPED:
2295 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2298 case RWM_CF_DROP_UNREQUESTED:
2299 rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2308 if ( strncasecmp( c->argv[ 0 ], "olcRwm", STRLENOF( "olcRwm" ) ) == 0 ) {
2312 switch ( c->type ) {
2313 case RWM_CF_REWRITE:
2314 if ( c->valx >= 0 ) {
2315 struct rewrite_info *rwm_rw = rwmap->rwm_rw;
2318 for ( last = 0; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ last ] ); last++ )
2321 if ( c->valx > last ) {
2325 rwmap->rwm_rw = NULL;
2326 rc = rwm_info_init( &rwmap->rwm_rw );
2328 for ( i = 0; i < c->valx; i++ ) {
2329 ConfigArgs ca = { 0 };
2331 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2333 config_fp_parse_line( &ca );
2335 argv0 = ca.argv[ 0 ];
2336 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2338 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2339 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2343 rc = rwm_rw_config( &db, c->fname, c->lineno,
2347 ca.argv[ 0 ] = argv0;
2349 ch_free( ca.tline );
2355 argv0 = c->argv[ idx0 ];
2356 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2359 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2360 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2361 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2362 c->argc - idx0, &c->argv[ idx0 ] );
2365 rc = rwm_rw_config( &db, c->fname, c->lineno,
2366 c->argc - idx0, &c->argv[ idx0 ] );
2368 c->argv[ idx0 ] = argv0;
2370 rewrite_info_delete( &rwmap->rwm_rw );
2371 assert( rwmap->rwm_rw == NULL );
2373 rwmap->rwm_rw = rwm_rw;
2377 for ( i = c->valx; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2379 ConfigArgs ca = { 0 };
2381 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2383 config_fp_parse_line( &ca );
2385 argv0 = ca.argv[ 0 ];
2386 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2388 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2389 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2393 rc = rwm_rw_config( &db, c->fname, c->lineno,
2397 ca.argv[ 0 ] = argv0;
2399 ch_free( ca.tline );
2405 rwmap->rwm_bva_rewrite = ch_realloc( rwmap->rwm_bva_rewrite,
2406 ( last + 2 )*sizeof( struct berval ) );
2407 BER_BVZERO( &rwmap->rwm_bva_rewrite[last+1] );
2409 for ( i = last - 1; i >= c->valx; i-- )
2411 rwmap->rwm_bva_rewrite[ i + 1 ] = rwmap->rwm_bva_rewrite[ i ];
2414 rwm_bva_rewrite_add( rwmap, c->valx, &c->argv[ idx0 ] );
2416 rewrite_info_delete( &rwm_rw );
2417 assert( rwm_rw == NULL );
2422 argv0 = c->argv[ idx0 ];
2423 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2426 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2427 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2428 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2429 c->argc - idx0, &c->argv[ idx0 ] );
2432 rc = rwm_rw_config( &db, c->fname, c->lineno,
2433 c->argc - idx0, &c->argv[ idx0 ] );
2435 c->argv[ idx0 ] = argv0;
2440 rwm_bva_rewrite_add( rwmap, -1, &c->argv[ idx0 ] );
2444 case RWM_CF_T_F_SUPPORT:
2445 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
2446 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
2450 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2451 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
2456 if ( c->valx >= 0 ) {
2457 struct ldapmap rwm_oc = rwmap->rwm_oc;
2458 struct ldapmap rwm_at = rwmap->rwm_at;
2462 if ( rwmap->rwm_bva_map ) {
2463 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2467 if ( c->valx >= cnt ) {
2471 memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2472 memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2474 /* re-parse all mappings, including the one
2475 * that needs to be added */
2477 for ( cnt = 0; cnt < c->valx; cnt++ ) {
2478 ConfigArgs ca = { 0 };
2480 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2482 config_fp_parse_line( &ca );
2484 argv[1] = ca.argv[0];
2485 argv[2] = ca.argv[1];
2486 argv[3] = ca.argv[2];
2487 argv[4] = ca.argv[3];
2489 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2491 ch_free( ca.tline );
2494 /* in case of failure, restore
2495 * the existing mapping */
2503 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2509 if ( rwmap->rwm_bva_map ) {
2510 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2511 ConfigArgs ca = { 0 };
2513 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2515 config_fp_parse_line( &ca );
2517 argv[1] = ca.argv[0];
2518 argv[2] = ca.argv[1];
2519 argv[3] = ca.argv[2];
2520 argv[4] = ca.argv[3];
2522 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2524 ch_free( ca.tline );
2527 /* in case of failure, restore
2528 * the existing mapping */
2535 /* in case of success, destroy the old mapping
2536 * and add the new one */
2539 struct berval bv, *bvp = &bv;
2541 if ( rwm_bva_add( &bvp, 0, &c->argv[ idx0 ] ) ) {
2546 tmp = ber_memrealloc( rwmap->rwm_bva_map,
2547 sizeof( struct berval )*( cnt + 2 ) );
2548 if ( tmp == NULL ) {
2549 ber_memfree( bv.bv_val );
2553 rwmap->rwm_bva_map = tmp;
2554 BER_BVZERO( &rwmap->rwm_bva_map[ cnt + 1 ] );
2556 avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2557 avl_free( rwm_oc.map, rwm_mapping_free );
2558 avl_free( rwm_at.remap, rwm_mapping_dst_free );
2559 avl_free( rwm_at.map, rwm_mapping_free );
2561 for ( ; cnt-- > c->valx; ) {
2562 rwmap->rwm_bva_map[ cnt + 1 ] = rwmap->rwm_bva_map[ cnt ];
2564 rwmap->rwm_bva_map[ c->valx ] = bv;
2568 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2569 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2570 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2571 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2572 rwmap->rwm_oc = rwm_oc;
2573 rwmap->rwm_at = rwm_at;
2579 argv0 = c->argv[ 0 ];
2580 c->argv[ 0 ] += STRLENOF( "rwm-" );
2581 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2582 c->argv[ 0 ] = argv0;
2590 line = ldap_charray2str( &c->argv[ 1 ], " " );
2591 if ( line != NULL ) {
2592 ber_str2bv( line, 0, 0, &bv );
2593 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
2598 case RWM_CF_NORMALIZE_MAPPED:
2599 if ( c->value_int ) {
2600 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
2602 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2606 case RWM_CF_DROP_UNREQUESTED:
2607 if ( c->value_int ) {
2608 rwmap->rwm_flags |= RWM_F_DROP_UNREQUESTED_ATTRS;
2610 rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2627 slap_overinst *on = (slap_overinst *) be->bd_info;
2628 struct ldaprwmap *rwmap;
2631 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
2634 rwmap->rwm_flags = RWM_F_DROP_UNREQUESTED_ATTRS;
2636 rc = rwm_info_init( &rwmap->rwm_rw );
2638 on->on_bi.bi_private = (void *)rwmap;
2641 (void)rwm_db_destroy( be, NULL );
2652 slap_overinst *on = (slap_overinst *) be->bd_info;
2655 if ( on->on_bi.bi_private ) {
2656 struct ldaprwmap *rwmap =
2657 (struct ldaprwmap *)on->on_bi.bi_private;
2659 if ( rwmap->rwm_rw ) {
2660 rewrite_info_delete( &rwmap->rwm_rw );
2661 if ( rwmap->rwm_bva_rewrite )
2662 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2665 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2666 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2667 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2668 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2669 ber_bvarray_free( rwmap->rwm_bva_map );
2677 static slap_overinst rwm = { { NULL } };
2679 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2681 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2683 rwm_initialize( void )
2687 /* Make sure we don't exceed the bits reserved for userland */
2688 config_check_userland( RWM_CF_LAST );
2690 memset( &rwm, 0, sizeof( slap_overinst ) );
2692 rwm.on_bi.bi_type = "rwm";
2693 rwm.on_bi.bi_flags =
2694 SLAPO_BFLAG_SINGLE |
2697 rwm.on_bi.bi_db_init = rwm_db_init;
2698 rwm.on_bi.bi_db_config = rwm_db_config;
2699 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2701 rwm.on_bi.bi_op_bind = rwm_op_bind;
2702 rwm.on_bi.bi_op_search = rwm_op_search;
2703 rwm.on_bi.bi_op_compare = rwm_op_compare;
2704 rwm.on_bi.bi_op_modify = rwm_op_modify;
2705 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2706 rwm.on_bi.bi_op_add = rwm_op_add;
2707 rwm.on_bi.bi_op_delete = rwm_op_delete;
2708 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2709 rwm.on_bi.bi_extended = rwm_extended;
2711 rwm.on_bi.bi_entry_release_rw = rwm_entry_release_rw;
2712 rwm.on_bi.bi_entry_get_rw = rwm_entry_get_rw;
2715 rwm.on_bi.bi_operational = rwm_operational;
2716 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2718 rwm.on_bi.bi_connection_init = rwm_conn_init;
2719 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2721 rwm.on_response = rwm_response;
2723 rwm.on_bi.bi_cf_ocs = rwmocs;
2725 rc = config_register_schema( rwmcfg, rwmocs );
2730 return overlay_register( &rwm );
2733 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2735 init_module( int argc, char *argv[] )
2737 return rwm_initialize();
2739 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2741 #endif /* SLAPD_OVER_RWM */