1 /* rwm.c - rewrite/remap operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2009 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;
41 typedef struct rwm_op_cb {
47 rwm_db_destroy( BackendDB *be, ConfigReply *cr );
50 rwm_send_entry( Operation *op, SlapReply *rs );
53 rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
55 if ( !BER_BVISNULL( &ros->ro_dn ) ) {
56 op->o_req_dn = ros->ro_dn;
58 if ( !BER_BVISNULL( &ros->ro_ndn ) ) {
59 op->o_req_ndn = ros->ro_ndn;
62 if ( !BER_BVISNULL( &ros->r_dn )
63 && ros->r_dn.bv_val != ros->ro_dn.bv_val )
65 assert( ros->r_dn.bv_val != ros->r_ndn.bv_val );
66 ch_free( ros->r_dn.bv_val );
67 BER_BVZERO( &ros->r_dn );
70 if ( !BER_BVISNULL( &ros->r_ndn )
71 && ros->r_ndn.bv_val != ros->ro_ndn.bv_val )
73 ch_free( ros->r_ndn.bv_val );
74 BER_BVZERO( &ros->r_ndn );
77 BER_BVZERO( &ros->ro_dn );
78 BER_BVZERO( &ros->ro_ndn );
80 switch( ros->r_tag ) {
81 case LDAP_REQ_COMPARE:
82 if ( op->orc_ava->aa_value.bv_val != ros->orc_ava->aa_value.bv_val )
83 op->o_tmpfree( op->orc_ava->aa_value.bv_val, op->o_tmpmemctx );
84 op->orc_ava = ros->orc_ava;
87 slap_mods_free( op->orm_modlist, 1 );
88 op->orm_modlist = ros->orm_modlist;
91 if ( op->orr_newSup != ros->orr_newSup ) {
92 ch_free( op->orr_newSup->bv_val );
93 ch_free( op->orr_nnewSup->bv_val );
94 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
95 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
96 op->orr_newSup = ros->orr_newSup;
97 op->orr_nnewSup = ros->orr_nnewSup;
99 if ( op->orr_newrdn.bv_val != ros->orr_newrdn.bv_val ) {
100 ch_free( op->orr_newrdn.bv_val );
101 ch_free( op->orr_nnewrdn.bv_val );
102 op->orr_newrdn = ros->orr_newrdn;
103 op->orr_nnewrdn = ros->orr_nnewrdn;
106 case LDAP_REQ_SEARCH:
107 ch_free( ros->mapped_attrs );
108 filter_free_x( op, op->ors_filter, 1 );
109 ch_free( op->ors_filterstr.bv_val );
110 op->ors_attrs = ros->ors_attrs;
111 op->ors_filter = ros->ors_filter;
112 op->ors_filterstr = ros->ors_filterstr;
114 case LDAP_REQ_EXTENDED:
115 if ( op->ore_reqdata != ros->ore_reqdata ) {
116 ber_bvfree( op->ore_reqdata );
117 op->ore_reqdata = ros->ore_reqdata;
121 if ( rs->sr_err == LDAP_SUCCESS ) {
123 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
124 /* too late, c_mutex released */
125 fprintf( stderr, "*** DN: \"%s\" => \"%s\"\n",
126 op->o_conn->c_ndn.bv_val,
127 op->o_req_ndn.bv_val );
128 ber_bvreplace( &op->o_conn->c_ndn,
130 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
139 rwm_op_cleanup( Operation *op, SlapReply *rs )
141 slap_callback *cb = op->o_callback;
142 rwm_op_state *ros = cb->sc_private;
144 if ( rs->sr_type == REP_RESULT || rs->sr_type == REP_EXTENDED ||
145 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
147 rwm_op_rollback( op, rs, ros );
149 op->o_callback = op->o_callback->sc_next;
150 op->o_tmpfree( cb, op->o_tmpmemctx );
153 return SLAP_CB_CONTINUE;
157 rwm_callback_get( Operation *op, SlapReply *rs )
159 rwm_op_cb *roc = NULL;
161 roc = op->o_tmpalloc( sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
162 roc->cb.sc_cleanup = rwm_op_cleanup;
163 roc->cb.sc_response = NULL;
164 roc->cb.sc_next = op->o_callback;
165 roc->cb.sc_private = &roc->ros;
166 roc->ros.r_tag = op->o_tag;
167 roc->ros.ro_dn = op->o_req_dn;
168 roc->ros.ro_ndn = op->o_req_ndn;
169 roc->ros.o_request = op->o_request;
170 BER_BVZERO( &roc->ros.r_dn );
171 BER_BVZERO( &roc->ros.r_ndn );
178 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
181 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
182 struct ldaprwmap *rwmap =
183 (struct ldaprwmap *)on->on_bi.bi_private;
185 struct berval dn = BER_BVNULL,
191 * Rewrite the dn if needed
194 dc.conn = op->o_conn;
196 dc.ctx = (char *)cookie;
198 /* NOTE: in those cases where only the ndn is available,
199 * and the caller sets op->o_req_dn = op->o_req_ndn,
200 * only rewrite the op->o_req_ndn and use it as
201 * op->o_req_dn as well */
203 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
205 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
207 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
210 if ( rc != LDAP_SUCCESS ) {
214 if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
215 || ndn.bv_val == op->o_req_ndn.bv_val )
220 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
222 assert( BER_BVISNULL( &ros->r_dn ) );
228 assert( BER_BVISNULL( &ros->r_ndn ) );
235 rwm_op_add( Operation *op, SlapReply *rs )
237 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
238 struct ldaprwmap *rwmap =
239 (struct ldaprwmap *)on->on_bi.bi_private;
243 Attribute **ap = NULL;
244 char *olddn = op->o_req_dn.bv_val;
247 rwm_op_cb *roc = rwm_callback_get( op, rs );
249 rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
250 if ( rc != LDAP_SUCCESS ) {
251 op->o_bd->bd_info = (BackendInfo *)on->on_info;
252 send_ldap_error( op, rs, rc, "addDN massage error" );
256 if ( olddn != op->o_req_dn.bv_val ) {
257 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
258 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
261 /* Count number of attributes in entry */
262 isupdate = be_shadow_update( op );
263 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
266 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
267 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
271 last = (*ap)->a_numvals - 1;
272 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
273 struct ldapmapping *mapping = NULL;
275 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
277 if ( mapping == NULL ) {
278 if ( rwmap->rwm_at.drop_missing ) {
279 /* FIXME: we allow to remove objectClasses as well;
280 * if the resulting entry is inconsistent, that's
281 * the relayed database's business...
283 ch_free( (*ap)->a_vals[ j ].bv_val );
285 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
287 BER_BVZERO( &(*ap)->a_vals[ last ] );
294 ch_free( (*ap)->a_vals[ j ].bv_val );
295 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
299 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
304 struct ldapmapping *mapping = NULL;
306 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
308 if ( mapping == NULL ) {
309 if ( rwmap->rwm_at.drop_missing ) {
314 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
315 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
318 * FIXME: rewrite could fail; in this case
319 * the operation should give up, right?
321 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
323 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
328 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
329 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
331 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
332 if ( rc != LDAP_SUCCESS ) {
337 if ( mapping != NULL ) {
338 assert( mapping->m_dst_ad != NULL );
339 (*ap)->a_desc = mapping->m_dst_ad;
348 /* FIXME: leaking attribute/values? */
355 op->o_callback = &roc->cb;
357 return SLAP_CB_CONTINUE;
361 rwm_conn_init( BackendDB *be, Connection *conn )
363 slap_overinst *on = (slap_overinst *) be->bd_info;
364 struct ldaprwmap *rwmap =
365 (struct ldaprwmap *)on->on_bi.bi_private;
367 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
369 return SLAP_CB_CONTINUE;
373 rwm_conn_destroy( BackendDB *be, Connection *conn )
375 slap_overinst *on = (slap_overinst *) be->bd_info;
376 struct ldaprwmap *rwmap =
377 (struct ldaprwmap *)on->on_bi.bi_private;
379 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
381 return SLAP_CB_CONTINUE;
385 rwm_op_bind( Operation *op, SlapReply *rs )
387 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
390 rwm_op_cb *roc = rwm_callback_get( op, rs );
392 rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
393 if ( rc != LDAP_SUCCESS ) {
394 op->o_bd->bd_info = (BackendInfo *)on->on_info;
395 send_ldap_error( op, rs, rc, "bindDN massage error" );
399 overlay_callback_after_backover( op, &roc->cb, 1 );
401 return SLAP_CB_CONTINUE;
405 rwm_op_unbind( Operation *op, SlapReply *rs )
407 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
408 struct ldaprwmap *rwmap =
409 (struct ldaprwmap *)on->on_bi.bi_private;
411 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
413 return SLAP_CB_CONTINUE;
417 rwm_op_compare( Operation *op, SlapReply *rs )
419 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
420 struct ldaprwmap *rwmap =
421 (struct ldaprwmap *)on->on_bi.bi_private;
424 struct berval mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
426 rwm_op_cb *roc = rwm_callback_get( op, rs );
428 rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
429 if ( rc != LDAP_SUCCESS ) {
430 op->o_bd->bd_info = (BackendInfo *)on->on_info;
431 send_ldap_error( op, rs, rc, "compareDN massage error" );
435 /* if the attribute is an objectClass, try to remap its value */
436 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
437 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
439 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
440 &mapped_vals[0], RWM_MAP );
441 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
443 op->o_bd->bd_info = (BackendInfo *)on->on_info;
444 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
447 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
448 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
453 struct ldapmapping *mapping = NULL;
454 AttributeDescription *ad = op->orc_ava->aa_desc;
456 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
458 if ( mapping == NULL ) {
459 if ( rwmap->rwm_at.drop_missing ) {
460 op->o_bd->bd_info = (BackendInfo *)on->on_info;
461 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
466 assert( mapping->m_dst_ad != NULL );
467 ad = mapping->m_dst_ad;
470 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
471 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
473 struct berval *mapped_valsp[2];
475 mapped_valsp[0] = &mapped_vals[0];
476 mapped_valsp[1] = &mapped_vals[1];
478 mapped_vals[0] = op->orc_ava->aa_value;
480 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
482 if ( rc != LDAP_SUCCESS ) {
483 op->o_bd->bd_info = (BackendInfo *)on->on_info;
484 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
488 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
489 /* NOTE: if we get here, rwm_dnattr_rewrite()
490 * already freed the old value, so now
492 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
494 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
497 op->orc_ava->aa_desc = ad;
500 op->o_callback = &roc->cb;
502 return SLAP_CB_CONTINUE;
506 rwm_op_delete( Operation *op, SlapReply *rs )
508 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
511 rwm_op_cb *roc = rwm_callback_get( op, rs );
513 rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
514 if ( rc != LDAP_SUCCESS ) {
515 op->o_bd->bd_info = (BackendInfo *)on->on_info;
516 send_ldap_error( op, rs, rc, "deleteDN massage error" );
520 op->o_callback = &roc->cb;
522 return SLAP_CB_CONTINUE;
526 rwm_op_modify( Operation *op, SlapReply *rs )
528 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
529 struct ldaprwmap *rwmap =
530 (struct ldaprwmap *)on->on_bi.bi_private;
536 rwm_op_cb *roc = rwm_callback_get( op, rs );
538 rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
539 if ( rc != LDAP_SUCCESS ) {
540 op->o_bd->bd_info = (BackendInfo *)on->on_info;
541 send_ldap_error( op, rs, rc, "modifyDN massage error" );
545 isupdate = be_shadow_update( op );
546 for ( mlp = &op->orm_modlist; *mlp; ) {
548 Modifications *ml = *mlp;
549 struct ldapmapping *mapping = NULL;
551 /* ml points to a temporary mod until needs duplication */
552 if ( ml->sml_desc == slap_schema.si_ad_objectClass
553 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
557 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
559 ml = ch_malloc( sizeof( Modifications ) );
561 if ( (*mlp)->sml_values ) {
562 ber_bvarray_dup_x( &ml->sml_values, (*mlp)->sml_values, NULL );
563 if ( (*mlp)->sml_nvalues ) {
564 ber_bvarray_dup_x( &ml->sml_nvalues, (*mlp)->sml_nvalues, NULL );
573 drop_missing = rwm_mapping( &rwmap->rwm_at,
574 &ml->sml_desc->ad_cname,
576 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
582 /* duplicate the modlist */
583 ml = ch_malloc( sizeof( Modifications ));
587 if ( ml->sml_values != NULL ) {
591 for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
594 bva = ch_malloc( (num+1) * sizeof( struct berval ));
595 for (i=0; i<num; i++)
596 ber_dupbv( &bva[i], &ml->sml_values[i] );
597 BER_BVZERO( &bva[i] );
598 ml->sml_values = bva;
600 if ( ml->sml_nvalues ) {
601 bva = ch_malloc( (num+1) * sizeof( struct berval ));
602 for (i=0; i<num; i++)
603 ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
604 BER_BVZERO( &bva[i] );
605 ml->sml_nvalues = bva;
613 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
614 struct ldapmapping *oc_mapping = NULL;
616 ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
617 &oc_mapping, RWM_MAP );
618 if ( oc_mapping == NULL ) {
619 if ( rwmap->rwm_at.drop_missing ) {
620 /* FIXME: we allow to remove objectClasses as well;
621 * if the resulting entry is inconsistent, that's
622 * the relayed database's business...
625 ch_free( ml->sml_values[ j ].bv_val );
626 ml->sml_values[ j ] = ml->sml_values[ last ];
628 BER_BVZERO( &ml->sml_values[ last ] );
634 ch_free( ml->sml_values[ j ].bv_val );
635 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
640 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
641 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
643 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
645 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
647 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
648 rc = rwm_referral_rewrite( op, rs,
651 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
652 if ( rc != LDAP_SUCCESS ) {
657 if ( rc != LDAP_SUCCESS ) {
664 if ( mapping != NULL ) {
665 /* use new attribute description */
666 assert( mapping->m_dst_ad != NULL );
667 ml->sml_desc = mapping->m_dst_ad;
675 *mlp = (*mlp)->sml_next;
676 slap_mod_free( &ml->sml_mod, 0 );
680 op->o_callback = &roc->cb;
682 return SLAP_CB_CONTINUE;
686 rwm_op_modrdn( Operation *op, SlapReply *rs )
688 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
689 struct ldaprwmap *rwmap =
690 (struct ldaprwmap *)on->on_bi.bi_private;
695 rwm_op_cb *roc = rwm_callback_get( op, rs );
697 if ( op->orr_newSup ) {
698 struct berval nnewSup = BER_BVNULL;
699 struct berval newSup = BER_BVNULL;
702 * Rewrite the new superior, if defined and required
705 dc.conn = op->o_conn;
707 dc.ctx = "newSuperiorDN";
708 newSup = *op->orr_newSup;
709 nnewSup = *op->orr_nnewSup;
710 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
711 if ( rc != LDAP_SUCCESS ) {
712 op->o_bd->bd_info = (BackendInfo *)on->on_info;
713 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
717 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
718 op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
720 op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
722 *op->orr_newSup = newSup;
723 *op->orr_nnewSup = nnewSup;
728 * Rewrite the newRDN, if needed
731 struct berval newrdn = BER_BVNULL;
732 struct berval nnewrdn = BER_BVNULL;
735 dc.conn = op->o_conn;
738 newrdn = op->orr_newrdn;
739 nnewrdn = op->orr_nnewrdn;
740 rc = rwm_dn_massage_pretty_normalize( &dc, &op->orr_newrdn, &newrdn, &nnewrdn );
741 if ( rc != LDAP_SUCCESS ) {
742 op->o_bd->bd_info = (BackendInfo *)on->on_info;
743 send_ldap_error( op, rs, rc, "newRDN massage error" );
747 if ( op->orr_newrdn.bv_val != newrdn.bv_val ) {
748 op->orr_newrdn = newrdn;
749 op->orr_nnewrdn = nnewrdn;
754 * Rewrite the dn, if needed
756 rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
757 if ( rc != LDAP_SUCCESS ) {
758 op->o_bd->bd_info = (BackendInfo *)on->on_info;
759 send_ldap_error( op, rs, rc, "renameDN massage error" );
763 op->o_callback = &roc->cb;
765 rc = SLAP_CB_CONTINUE;
769 if ( op->orr_newSup != roc->ros.orr_newSup ) {
770 ch_free( op->orr_newSup->bv_val );
771 ch_free( op->orr_nnewSup->bv_val );
772 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
773 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
774 op->orr_newSup = roc->ros.orr_newSup;
775 op->orr_nnewSup = roc->ros.orr_nnewSup;
778 if ( op->orr_newrdn.bv_val != roc->ros.orr_newrdn.bv_val ) {
779 ch_free( op->orr_newrdn.bv_val );
780 ch_free( op->orr_nnewrdn.bv_val );
781 op->orr_newrdn = roc->ros.orr_newrdn;
782 op->orr_nnewrdn = roc->ros.orr_nnewrdn;
791 rwm_swap_attrs( Operation *op, SlapReply *rs )
793 slap_callback *cb = op->o_callback;
794 rwm_op_state *ros = cb->sc_private;
796 rs->sr_attrs = ros->ors_attrs;
798 /* other overlays might have touched op->ors_attrs,
799 * so we restore the original version here, otherwise
800 * attribute-mapping might fail */
801 op->ors_attrs = ros->mapped_attrs;
803 return SLAP_CB_CONTINUE;
807 * NOTE: this implementation of get/release entry is probably far from
808 * optimal. The rationale consists in intercepting the request directed
809 * to the underlying database, in order to rewrite/remap the request,
810 * perform it using the modified data, duplicate the resulting entry
811 * and finally free it when release is called.
812 * This implies that subsequent overlays are not called, as the request
813 * is directly shunted to the underlying database.
816 rwm_entry_release_rw( Operation *op, Entry *e, int rw )
818 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
821 if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
822 return SLAP_CB_CONTINUE;
825 /* just free entry if (probably) ours */
826 if ( e->e_private == NULL && BER_BVISNULL( &e->e_bv ) ) {
831 return SLAP_CB_CONTINUE;
835 rwm_entry_get_rw( Operation *op, struct berval *ndn,
836 ObjectClass *oc, AttributeDescription *at, int rw, Entry **ep )
838 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
839 struct ldaprwmap *rwmap =
840 (struct ldaprwmap *)on->on_bi.bi_private;
847 SlapReply rs = { REP_SEARCH };
849 rwm_op_state ros = { 0 };
850 struct berval mndn = BER_BVNULL;
852 if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
853 return SLAP_CB_CONTINUE;
857 op2.o_tag = LDAP_REQ_SEARCH;
860 op2.o_req_ndn = *ndn;
861 rc = rwm_op_dn_massage( &op2, &rs, "searchDN", &ros );
862 if ( rc != LDAP_SUCCESS ) {
866 mndn = BER_BVISNULL( &ros.r_ndn ) ? *ndn : ros.r_ndn;
868 /* map attribute & objectClass */
878 op2.o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
879 op2.ors_attrs = slap_anlist_all_attributes;
880 rc = op2.o_bd->bd_info->bi_entry_get_rw( &op2, &mndn, oc, at, rw, ep );
881 if ( rc == LDAP_SUCCESS && *ep != NULL ) {
882 /* we assume be_entry_release() needs to be called */
883 rs.sr_flags = REP_ENTRY_MUSTRELEASE;
886 /* duplicate & release */
887 op2.o_bd->bd_info = (BackendInfo *)on;
888 rc = rwm_send_entry( &op2, &rs );
889 if ( rc == SLAP_CB_CONTINUE ) {
895 if ( !BER_BVISNULL( &ros.r_ndn) && ros.r_ndn.bv_val != ndn->bv_val ) {
896 op->o_tmpfree( ros.r_ndn.bv_val, op->o_tmpmemctx );
903 rwm_op_search( Operation *op, SlapReply *rs )
905 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
906 struct ldaprwmap *rwmap =
907 (struct ldaprwmap *)on->on_bi.bi_private;
912 struct berval fstr = BER_BVNULL;
915 AttributeName *an = NULL;
919 rwm_op_cb *roc = rwm_callback_get( op, rs );
921 rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
922 "searchFilter", op->ors_filterstr.bv_val );
923 if ( rc == LDAP_SUCCESS )
924 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
925 if ( rc != LDAP_SUCCESS ) {
926 text = "searchDN massage error";
931 * Rewrite the dn if needed
934 dc.conn = op->o_conn;
936 dc.ctx = "searchFilterAttrDN";
938 rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
939 if ( rc != LDAP_SUCCESS ) {
940 text = "searchFilter/searchFilterAttrDN massage error";
944 f = str2filter_x( op, fstr.bv_val );
947 text = "massaged filter parse error";
952 op->ors_filterstr = fstr;
954 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
955 op->ors_attrs, &an, RWM_MAP );
956 if ( rc != LDAP_SUCCESS ) {
957 text = "attribute list mapping error";
962 /* store the mapped Attributes for later usage, in
963 * the case that other overlays change op->ors_attrs */
964 roc->ros.mapped_attrs = an;
965 roc->cb.sc_response = rwm_swap_attrs;
967 op->o_callback = &roc->cb;
969 return SLAP_CB_CONTINUE;
977 filter_free_x( op, f, 1 );
980 if ( !BER_BVISNULL( &fstr ) ) {
981 ch_free( fstr.bv_val );
984 rwm_op_rollback( op, rs, &roc->ros );
985 op->oq_search = roc->ros.oq_search;
986 op->o_tmpfree( roc, op->o_tmpmemctx );
988 op->o_bd->bd_info = (BackendInfo *)on->on_info;
989 send_ldap_error( op, rs, rc, text );
996 rwm_exop_passwd( Operation *op, SlapReply *rs )
998 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1002 struct berval id = BER_BVNULL,
1005 BerElement *ber = NULL;
1007 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
1008 return LDAP_SUCCESS;
1011 if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
1012 rs->sr_err = LDAP_OTHER;
1016 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
1017 &pwold, &pwnew, &rs->sr_text );
1018 if ( rs->sr_err != LDAP_SUCCESS ) {
1022 if ( !BER_BVISNULL( &id ) ) {
1023 char idNul = id.bv_val[id.bv_len];
1024 id.bv_val[id.bv_len] = '\0';
1025 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
1026 &op->o_req_ndn, op->o_tmpmemctx );
1027 id.bv_val[id.bv_len] = idNul;
1028 if ( rs->sr_err != LDAP_SUCCESS ) {
1029 rs->sr_text = "Invalid DN";
1034 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
1035 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
1038 roc = rwm_callback_get( op, rs );
1040 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1041 if ( rc != LDAP_SUCCESS ) {
1042 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1043 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1047 ber = ber_alloc_t( LBER_USE_DER );
1049 rs->sr_err = LDAP_OTHER;
1050 rs->sr_text = "No memory";
1053 ber_printf( ber, "{" );
1054 if ( !BER_BVISNULL( &id )) {
1055 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1058 if ( !BER_BVISNULL( &pwold )) {
1059 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
1061 if ( !BER_BVISNULL( &pwnew )) {
1062 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
1064 ber_printf( ber, "N}" );
1065 ber_flatten( ber, &op->ore_reqdata );
1068 op->o_callback = &roc->cb;
1070 return SLAP_CB_CONTINUE;
1073 static struct exop {
1075 BI_op_extended *extended;
1077 { BER_BVC(LDAP_EXOP_MODIFY_PASSWD), rwm_exop_passwd },
1078 { BER_BVNULL, NULL }
1082 rwm_extended( Operation *op, SlapReply *rs )
1084 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1090 for ( i = 0; exop_table[i].extended != NULL; i++ ) {
1091 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
1093 rc = exop_table[i].extended( op, rs );
1098 case SLAP_CB_CONTINUE:
1103 send_ldap_result( op, rs );
1110 roc = rwm_callback_get( op, rs );
1112 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1113 if ( rc != LDAP_SUCCESS ) {
1114 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1115 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1119 /* TODO: rewrite/map extended data ? ... */
1120 op->o_callback = &roc->cb;
1122 return SLAP_CB_CONTINUE;
1126 rwm_matched( Operation *op, SlapReply *rs )
1128 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1129 struct ldaprwmap *rwmap =
1130 (struct ldaprwmap *)on->on_bi.bi_private;
1132 struct berval dn, mdn;
1136 if ( rs->sr_matched == NULL ) {
1137 return SLAP_CB_CONTINUE;
1141 dc.conn = op->o_conn;
1143 dc.ctx = "matchedDN";
1144 ber_str2bv( rs->sr_matched, 0, 0, &dn );
1146 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
1147 if ( rc != LDAP_SUCCESS ) {
1149 rs->sr_text = "Rewrite error";
1153 if ( mdn.bv_val != dn.bv_val ) {
1154 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
1155 ch_free( (void *)rs->sr_matched );
1158 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
1160 rs->sr_matched = mdn.bv_val;
1163 return SLAP_CB_CONTINUE;
1167 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
1169 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1170 struct ldaprwmap *rwmap =
1171 (struct ldaprwmap *)on->on_bi.bi_private;
1177 int check_duplicate_attrs = 0;
1180 * Rewrite the dn attrs, if needed
1183 dc.conn = op->o_conn;
1186 /* FIXME: the entries are in the remote mapping form;
1187 * so we need to select those attributes we are willing
1188 * to return, and remap them accordingly */
1190 /* FIXME: in principle, one could map an attribute
1191 * on top of another, which already exists.
1192 * As such, in the end there might exist more than
1193 * one instance of an attribute.
1194 * We should at least check if this occurs, and issue
1195 * an error (because multiple instances of attrs in
1196 * response are not valid), or merge the values (what
1197 * about duplicate values?) */
1198 isupdate = be_shadow_update( op );
1199 for ( ap = a_first; *ap; ) {
1200 struct ldapmapping *mapping = NULL;
1205 if ( ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS ) &&
1206 op->ors_attrs != NULL &&
1207 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1208 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1213 drop_missing = rwm_mapping( &rwmap->rwm_at,
1214 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1215 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1219 if ( mapping != NULL ) {
1220 assert( mapping->m_dst_ad != NULL );
1222 /* try to normalize mapped Attributes if the original
1223 * AttributeType was not normalized */
1224 if ( (!(*ap)->a_desc->ad_type->sat_equality ||
1225 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1226 mapping->m_dst_ad->ad_type->sat_equality &&
1227 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1229 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
1233 last = (*ap)->a_numvals;
1236 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1238 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1241 * check that each value is valid per syntax
1242 * and pretty if appropriate
1244 rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1245 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1246 mapping->m_dst_ad->ad_type->sat_syntax,
1247 mapping->m_dst_ad->ad_type->sat_equality,
1248 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1251 if ( rc != LDAP_SUCCESS ) {
1252 BER_BVZERO( &(*ap)->a_nvals[i] );
1255 BER_BVZERO( &(*ap)->a_nvals[i] );
1259 assert( (*ap)->a_nvals == (*ap)->a_vals );
1260 (*ap)->a_nvals = NULL;
1261 ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
1265 /* rewrite the attribute description */
1266 (*ap)->a_desc = mapping->m_dst_ad;
1268 /* will need to check for duplicate attrs */
1269 check_duplicate_attrs++;
1272 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1273 if ( stripEntryDN ) {
1274 /* will be generated by frontend */
1278 } else if ( !isupdate
1280 && (*ap)->a_desc->ad_type->sat_no_user_mod
1281 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1286 if ( last == -1 ) { /* not yet counted */
1287 last = (*ap)->a_numvals;
1291 /* empty? leave it in place because of attrsonly and vlv */
1296 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1297 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1301 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1302 struct berval mapped;
1304 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1305 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1307 ch_free( bv[0].bv_val );
1308 BER_BVZERO( &bv[0] );
1309 if ( &(*ap)->a_vals[last] > &bv[0] ) {
1310 bv[0] = (*ap)->a_vals[last];
1311 BER_BVZERO( &(*ap)->a_vals[last] );
1316 } else if ( mapped.bv_val != bv[0].bv_val
1317 && ber_bvstrcasecmp( &mapped, &bv[0] ) != 0 )
1321 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1322 if ( &(*ap)->a_vals[ i ] == bv ) {
1326 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1331 if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1336 * FIXME: after LBER_FREEing
1337 * the value is replaced by
1338 * ch_alloc'ed memory
1340 ber_bvreplace( &bv[0], &mapped );
1342 /* FIXME: will need to check
1343 * if the structuralObjectClass
1349 * It is necessary to try to rewrite attributes with
1350 * dn syntax because they might be used in ACLs as
1351 * members of groups; since ACLs are applied to the
1352 * rewritten stuff, no dn-based subject clause could
1353 * be used at the ldap backend side (see
1354 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1355 * The problem can be overcome by moving the dn-based
1356 * ACLs to the target directory server, and letting
1357 * everything pass thru the ldap backend. */
1358 /* FIXME: handle distinguishedName-like syntaxes, like
1359 * nameAndOptionalUID */
1360 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1361 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1363 dc.ctx = "searchAttrDN";
1364 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals, (*ap)->a_nvals );
1365 if ( rc != LDAP_SUCCESS ) {
1369 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1370 dc.ctx = "searchAttrDN";
1371 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1372 if ( rc != LDAP_SUCCESS ) {
1379 ap = &(*ap)->a_next;
1384 *ap = (*ap)->a_next;
1389 /* only check if some mapping occurred */
1390 if ( check_duplicate_attrs ) {
1391 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1394 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1395 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1397 Modification mod = { 0 };
1398 const char *text = NULL;
1399 char textbuf[ SLAP_TEXT_BUFLEN ];
1400 Attribute *next = (*tap)->a_next;
1402 BER_BVSTR( &e.e_name, "" );
1403 BER_BVSTR( &e.e_nname, "" );
1405 mod.sm_op = LDAP_MOD_ADD;
1406 mod.sm_desc = (*ap)->a_desc;
1407 mod.sm_type = mod.sm_desc->ad_cname;
1408 mod.sm_numvals = (*tap)->a_numvals;
1409 mod.sm_values = (*tap)->a_vals;
1410 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
1411 mod.sm_nvalues = (*tap)->a_nvals;
1414 (void)modify_add_values( &e, &mod,
1416 &text, textbuf, sizeof( textbuf ) );
1418 /* should not insert new attrs! */
1419 assert( e.e_attrs == *ap );
1425 tap = &(*tap)->a_next;
1435 rwm_send_entry( Operation *op, SlapReply *rs )
1437 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1438 struct ldaprwmap *rwmap =
1439 (struct ldaprwmap *)on->on_bi.bi_private;
1443 struct berval dn = BER_BVNULL,
1448 assert( rs->sr_entry != NULL );
1451 * Rewrite the dn of the result, if needed
1454 dc.conn = op->o_conn;
1456 dc.ctx = "searchEntryDN";
1459 flags = rs->sr_flags;
1460 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1461 /* FIXME: all we need to duplicate are:
1464 * - attributes that are requested
1465 * - no values if attrsonly is set
1470 rc = LDAP_NO_MEMORY;
1474 flags &= ~REP_ENTRY_MUSTRELEASE;
1475 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1479 * Note: this may fail if the target host(s) schema differs
1480 * from the one known to the meta, and a DN with unknown
1481 * attributes is returned.
1485 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1486 if ( rc != LDAP_SUCCESS ) {
1491 if ( e->e_name.bv_val != dn.bv_val ) {
1492 ch_free( e->e_name.bv_val );
1493 ch_free( e->e_nname.bv_val );
1499 /* TODO: map entry attribute types, objectclasses
1500 * and dn-valued attribute values */
1502 /* FIXME: the entries are in the remote mapping form;
1503 * so we need to select those attributes we are willing
1504 * to return, and remap them accordingly */
1505 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1507 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1508 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
1512 rs->sr_flags = flags;
1514 return SLAP_CB_CONTINUE;
1517 if ( e != NULL && e != rs->sr_entry ) {
1518 if ( e->e_name.bv_val == dn.bv_val ) {
1519 BER_BVZERO( &e->e_name );
1522 if ( e->e_nname.bv_val == ndn.bv_val ) {
1523 BER_BVZERO( &e->e_nname );
1529 if ( !BER_BVISNULL( &dn ) ) {
1530 ch_free( dn.bv_val );
1533 if ( !BER_BVISNULL( &ndn ) ) {
1534 ch_free( ndn.bv_val );
1541 rwm_operational( Operation *op, SlapReply *rs )
1543 /* FIXME: the entries are in the remote mapping form;
1544 * so we need to select those attributes we are willing
1545 * to return, and remap them accordingly */
1546 if ( rs->sr_operational_attrs ) {
1547 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1550 return SLAP_CB_CONTINUE;
1554 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1555 * rewritten for subsequent operations; fine for plain suffixmassage,
1556 * but destroys everything else */
1558 rwm_chk_referrals( Operation *op, SlapReply *rs )
1560 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1563 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1564 if ( rc != LDAP_SUCCESS ) {
1565 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1566 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1570 return SLAP_CB_CONTINUE;
1582 slap_overinst *on = (slap_overinst *) be->bd_info;
1583 struct ldaprwmap *rwmap =
1584 (struct ldaprwmap *)on->on_bi.bi_private;
1586 return rewrite_parse( rwmap->rwm_rw,
1587 fname, lineno, argc, argv );
1593 rwm_suffixmassage_config(
1600 slap_overinst *on = (slap_overinst *) be->bd_info;
1601 struct ldaprwmap *rwmap =
1602 (struct ldaprwmap *)on->on_bi.bi_private;
1604 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1611 * suffixmassage [<suffix>] <massaged suffix>
1613 * the [<suffix>] field must be defined as a valid suffix
1614 * for the current database;
1615 * the <massaged suffix> shouldn't have already been
1616 * defined as a valid suffix for the current server
1619 if ( be->be_suffix == NULL ) {
1620 fprintf( stderr, "%s: line %d: "
1621 " \"suffixMassage [<suffix>]"
1622 " <massaged suffix>\" without "
1623 "<suffix> part requires database "
1624 "suffix be defined first.\n",
1628 bvnc = be->be_suffix[ 0 ];
1631 } else if ( argc == 3 ) {
1632 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1636 fprintf( stderr, "%s: line %d: syntax is"
1637 " \"suffixMassage [<suffix>]"
1638 " <massaged suffix>\"\n",
1643 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1644 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1645 fname, lineno, bvnc.bv_val );
1649 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1650 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1651 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1652 fname, lineno, brnc.bv_val );
1653 free( nvnc.bv_val );
1654 free( pvnc.bv_val );
1659 * The suffix massaging is emulated
1660 * by means of the rewrite capabilities
1662 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1663 &pvnc, &nvnc, &prnc, &nrnc );
1664 free( nvnc.bv_val );
1665 free( pvnc.bv_val );
1666 free( nrnc.bv_val );
1667 free( prnc.bv_val );
1680 slap_overinst *on = (slap_overinst *) be->bd_info;
1681 struct ldaprwmap *rwmap =
1682 (struct ldaprwmap *)on->on_bi.bi_private;
1684 /* objectclass/attribute mapping */
1685 return rwm_map_config( &rwmap->rwm_oc,
1687 fname, lineno, argc, argv );
1691 rwm_response( Operation *op, SlapReply *rs )
1693 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1694 struct ldaprwmap *rwmap =
1695 (struct ldaprwmap *)on->on_bi.bi_private;
1699 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1700 return rwm_send_entry( op, rs );
1703 switch( op->o_tag ) {
1704 case LDAP_REQ_SEARCH:
1707 case LDAP_REQ_DELETE:
1708 case LDAP_REQ_MODRDN:
1709 case LDAP_REQ_MODIFY:
1710 case LDAP_REQ_COMPARE:
1711 case LDAP_REQ_EXTENDED:
1716 * Rewrite the dn of the referrals, if needed
1719 dc.conn = op->o_conn;
1721 dc.ctx = "referralDN";
1722 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1723 if ( rc != LDAP_SUCCESS ) {
1728 rc = rwm_matched( op, rs );
1732 rc = SLAP_CB_CONTINUE;
1747 slap_overinst *on = (slap_overinst *) be->bd_info;
1748 struct ldaprwmap *rwmap =
1749 (struct ldaprwmap *)on->on_bi.bi_private;
1754 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1756 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1759 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1760 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1762 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1763 rc = rwm_m_config( be, fname, lineno, argc, argv );
1765 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1766 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1768 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1771 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1776 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1777 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1779 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1780 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1782 /* TODO: not implemented yet */
1783 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1785 "%s: line %d: \"discover\" not supported yet "
1786 "in \"t-f-support {no|yes|discover}\".\n",
1790 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1795 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1796 fname, lineno, argv[ 1 ] );
1800 } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) == 0 ) {
1803 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1808 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1809 rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1811 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1812 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1816 rc = SLAP_CONF_UNKNOWN;
1827 * dynamic configuration...
1837 RWM_CF_NORMALIZE_MAPPED,
1838 RWM_CF_DROP_UNREQUESTED,
1843 static slap_verbmasks t_f_mode[] = {
1844 { BER_BVC( "true" ), RWM_F_SUPPORT_T_F },
1845 { BER_BVC( "yes" ), RWM_F_SUPPORT_T_F },
1846 { BER_BVC( "discover" ), RWM_F_SUPPORT_T_F_DISCOVER },
1847 { BER_BVC( "false" ), RWM_F_NONE },
1848 { BER_BVC( "no" ), RWM_F_NONE },
1852 static ConfigDriver rwm_cf_gen;
1854 static ConfigTable rwmcfg[] = {
1855 { "rwm-rewrite", "rewrite",
1856 2, 0, STRLENOF("rwm-rewrite"),
1857 ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1858 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1859 "DESC 'Rewrites strings' "
1860 "EQUALITY caseIgnoreMatch "
1861 "SYNTAX OMsDirectoryString "
1862 "X-ORDERED 'VALUES' )",
1865 { "rwm-suffixmassage", "[virtual]> <real",
1866 2, 3, 0, ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1869 { "rwm-t-f-support", "true|false|discover",
1870 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1871 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1872 "DESC 'Absolute filters support' "
1873 "SYNTAX OMsDirectoryString "
1877 { "rwm-map", "{objectClass|attribute}",
1878 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1879 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1880 "DESC 'maps attributes/objectClasses' "
1881 "SYNTAX OMsDirectoryString "
1882 "X-ORDERED 'VALUES' )",
1885 { "rwm-normalize-mapped-attrs", "true|false",
1886 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1887 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1888 "DESC 'Normalize mapped attributes/objectClasses' "
1889 "SYNTAX OMsBoolean "
1893 { "rwm-drop-unrequested-attrs", "true|false",
1894 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_DROP_UNREQUESTED, rwm_cf_gen,
1895 "( OLcfgOvAt:16.5 NAME 'olcRwmDropUnrequested' "
1896 "DESC 'Drop unrequested attributes' "
1897 "SYNTAX OMsBoolean "
1901 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1904 static ConfigOCs rwmocs[] = {
1905 { "( OLcfgOvOc:16.1 "
1906 "NAME 'olcRwmConfig' "
1907 "DESC 'Rewrite/remap configuration' "
1908 "SUP olcOverlayConfig "
1911 "olcRwmTFSupport $ "
1913 "olcRwmNormalizeMapped "
1915 Cft_Overlay, rwmcfg, NULL, NULL },
1920 slap_rewrite_unparse( BerVarray in, BerVarray *out )
1923 BerVarray bva = NULL;
1924 char ibuf[32], *ptr;
1927 assert( in != NULL );
1929 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1938 bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1939 BER_BVZERO( &bva[ 0 ] );
1941 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1942 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1943 if ( idx.bv_len >= sizeof( ibuf ) ) {
1944 ber_bvarray_free( bva );
1948 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1949 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1950 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1951 ptr = lutil_strcopy( ptr, in[i].bv_val );
1953 BER_BVZERO( &bva[ i + 1 ] );
1960 rwm_bva_rewrite_add(
1961 struct ldaprwmap *rwmap,
1968 line = ldap_charray2str( argv, "\" \"" );
1969 if ( line != NULL ) {
1970 int len = strlen( argv[ 0 ] );
1972 ber_str2bv( line, 0, 0, &bv );
1973 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1974 bv.bv_len - ( len + 1 ) );
1975 bv.bv_val[ bv.bv_len - 1 ] = '"';
1978 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1981 rwmap->rwm_bva_rewrite[ idx ] = bv;
1989 rwm_info_init( struct rewrite_info ** rwm_rw )
1993 *rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1994 if ( *rwm_rw == NULL ) {
1998 /* this rewriteContext by default must be null;
1999 * rules can be added if required */
2000 rargv[ 0 ] = "rewriteContext";
2001 rargv[ 1 ] = "searchFilter";
2003 rewrite_parse( *rwm_rw, "<suffix massage>", 1, 2, rargv );
2005 rargv[ 0 ] = "rewriteContext";
2006 rargv[ 1 ] = "default";
2008 rewrite_parse( *rwm_rw, "<suffix massage>", 2, 2, rargv );
2014 rwm_cf_gen( ConfigArgs *c )
2016 slap_overinst *on = (slap_overinst *)c->bi;
2017 struct ldaprwmap *rwmap =
2018 (struct ldaprwmap *)on->on_bi.bi_private;
2028 if ( c->op == SLAP_CONFIG_EMIT ) {
2029 struct berval bv = BER_BVNULL;
2031 switch ( c->type ) {
2032 case RWM_CF_REWRITE:
2033 if ( rwmap->rwm_bva_rewrite == NULL ) {
2037 slap_rewrite_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
2038 if ( !c->rvalue_vals ) {
2044 case RWM_CF_T_F_SUPPORT:
2045 enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
2046 if ( BER_BVISNULL( &bv ) ) {
2047 /* there's something wrong... */
2052 value_add_one( &c->rvalue_vals, &bv );
2057 if ( rwmap->rwm_bva_map == NULL ) {
2061 value_add( &c->rvalue_vals, rwmap->rwm_bva_map );
2065 case RWM_CF_NORMALIZE_MAPPED:
2066 c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
2069 case RWM_CF_DROP_UNREQUESTED:
2070 c->value_int = ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS );
2080 } else if ( c->op == LDAP_MOD_DELETE ) {
2081 switch ( c->type ) {
2082 case RWM_CF_REWRITE:
2083 if ( c->valx >= 0 ) {
2084 ConfigArgs ca = { 0 };
2087 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2090 if ( i >= c->valx ) {
2095 ber_memfree( rwmap->rwm_bva_rewrite[ c->valx ].bv_val );
2096 for ( i = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i + 1 ] ); i++ )
2098 rwmap->rwm_bva_rewrite[ i ] = rwmap->rwm_bva_rewrite[ i + 1 ];
2100 BER_BVZERO( &rwmap->rwm_bva_rewrite[ i ] );
2102 rewrite_info_delete( &rwmap->rwm_rw );
2103 assert( rwmap->rwm_rw == NULL );
2105 rc = rwm_info_init( &rwmap->rwm_rw );
2107 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2109 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2111 config_fp_parse_line( &ca );
2113 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2114 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2118 rc = rwm_rw_config( &db, c->fname, c->lineno,
2122 ch_free( ca.tline );
2127 } else if ( rwmap->rwm_rw != NULL ) {
2128 rewrite_info_delete( &rwmap->rwm_rw );
2129 assert( rwmap->rwm_rw == NULL );
2131 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2132 rwmap->rwm_bva_rewrite = NULL;
2134 rc = rwm_info_init( &rwmap->rwm_rw );
2138 case RWM_CF_T_F_SUPPORT:
2139 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2143 if ( c->valx >= 0 ) {
2144 /* single modification is not allowed */
2148 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2149 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2150 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2151 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2153 rwmap->rwm_oc.remap = NULL;
2154 rwmap->rwm_oc.map = NULL;
2155 rwmap->rwm_at.remap = NULL;
2156 rwmap->rwm_at.map = NULL;
2158 ber_bvarray_free( rwmap->rwm_bva_map );
2159 rwmap->rwm_bva_map = NULL;
2163 case RWM_CF_NORMALIZE_MAPPED:
2164 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2167 case RWM_CF_DROP_UNREQUESTED:
2168 rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2177 if ( strncasecmp( c->argv[ 0 ], "olcRwm", STRLENOF( "olcRwm" ) ) == 0 ) {
2181 switch ( c->type ) {
2182 case RWM_CF_REWRITE:
2183 if ( c->valx >= 0 ) {
2184 struct rewrite_info *rwm_rw = rwmap->rwm_rw;
2185 ConfigArgs ca = { 0 };
2188 for ( last = 0; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ last ] ); last++ )
2191 if ( c->valx > last ) {
2195 rwmap->rwm_rw = NULL;
2196 rc = rwm_info_init( &rwmap->rwm_rw );
2198 for ( i = 0; i < c->valx; i++ ) {
2199 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2201 config_fp_parse_line( &ca );
2203 argv0 = ca.argv[ 0 ];
2204 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2206 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2207 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2211 rc = rwm_rw_config( &db, c->fname, c->lineno,
2215 ca.argv[ 0 ] = argv0;
2217 ch_free( ca.tline );
2222 argv0 = c->argv[ idx0 ];
2223 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2226 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2227 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2228 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2229 c->argc - idx0, &c->argv[ idx0 ] );
2232 rc = rwm_rw_config( &db, c->fname, c->lineno,
2233 c->argc - idx0, &c->argv[ idx0 ] );
2235 c->argv[ idx0 ] = argv0;
2237 rewrite_info_delete( &rwmap->rwm_rw );
2238 assert( rwmap->rwm_rw == NULL );
2240 rwmap->rwm_rw = rwm_rw;
2244 for ( i = c->valx; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2246 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2248 config_fp_parse_line( &ca );
2250 argv0 = ca.argv[ 0 ];
2251 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2253 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2254 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2258 rc = rwm_rw_config( &db, c->fname, c->lineno,
2262 ca.argv[ 0 ] = argv0;
2264 ch_free( ca.tline );
2269 rwmap->rwm_bva_rewrite = ch_realloc( rwmap->rwm_bva_rewrite,
2270 ( last + 2 )*sizeof( struct berval ) );
2271 BER_BVZERO( &rwmap->rwm_bva_rewrite[last+1] );
2273 for ( i = last - 1; i >= c->valx; i-- )
2275 rwmap->rwm_bva_rewrite[ i + 1 ] = rwmap->rwm_bva_rewrite[ i ];
2278 rwm_bva_rewrite_add( rwmap, c->valx, &c->argv[ idx0 ] );
2280 rewrite_info_delete( &rwm_rw );
2281 assert( rwm_rw == NULL );
2286 argv0 = c->argv[ idx0 ];
2287 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2290 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2291 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2292 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2293 c->argc - idx0, &c->argv[ idx0 ] );
2296 rc = rwm_rw_config( &db, c->fname, c->lineno,
2297 c->argc - idx0, &c->argv[ idx0 ] );
2299 c->argv[ idx0 ] = argv0;
2304 rwm_bva_rewrite_add( rwmap, -1, &c->argv[ idx0 ] );
2308 case RWM_CF_T_F_SUPPORT:
2309 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
2310 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
2314 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2315 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
2320 if ( c->valx >= 0 ) {
2324 argv0 = c->argv[ 0 ];
2325 c->argv[ 0 ] += STRLENOF( "rwm-" );
2326 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2327 c->argv[ 0 ] = argv0;
2335 line = ldap_charray2str( &c->argv[ 1 ], " " );
2336 if ( line != NULL ) {
2337 ber_str2bv( line, 0, 0, &bv );
2338 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
2343 case RWM_CF_NORMALIZE_MAPPED:
2344 if ( c->value_int ) {
2345 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
2347 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2351 case RWM_CF_DROP_UNREQUESTED:
2352 if ( c->value_int ) {
2353 rwmap->rwm_flags |= RWM_F_DROP_UNREQUESTED_ATTRS;
2355 rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2372 slap_overinst *on = (slap_overinst *) be->bd_info;
2373 struct ldaprwmap *rwmap;
2376 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
2379 rwmap->rwm_flags = RWM_F_DROP_UNREQUESTED_ATTRS;
2381 rc = rwm_info_init( &rwmap->rwm_rw );
2383 on->on_bi.bi_private = (void *)rwmap;
2386 (void)rwm_db_destroy( be, NULL );
2397 slap_overinst *on = (slap_overinst *) be->bd_info;
2400 if ( on->on_bi.bi_private ) {
2401 struct ldaprwmap *rwmap =
2402 (struct ldaprwmap *)on->on_bi.bi_private;
2404 if ( rwmap->rwm_rw ) {
2405 rewrite_info_delete( &rwmap->rwm_rw );
2406 if ( rwmap->rwm_bva_rewrite )
2407 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2410 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2411 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2412 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2413 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2414 ber_bvarray_free( rwmap->rwm_bva_map );
2422 static slap_overinst rwm = { { NULL } };
2424 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2426 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2428 rwm_initialize( void )
2432 /* Make sure we don't exceed the bits reserved for userland */
2433 config_check_userland( RWM_CF_LAST );
2435 memset( &rwm, 0, sizeof( slap_overinst ) );
2437 rwm.on_bi.bi_type = "rwm";
2438 rwm.on_bi.bi_flags =
2439 SLAPO_BFLAG_SINGLE |
2442 rwm.on_bi.bi_db_init = rwm_db_init;
2443 rwm.on_bi.bi_db_config = rwm_db_config;
2444 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2446 rwm.on_bi.bi_op_bind = rwm_op_bind;
2447 rwm.on_bi.bi_op_search = rwm_op_search;
2448 rwm.on_bi.bi_op_compare = rwm_op_compare;
2449 rwm.on_bi.bi_op_modify = rwm_op_modify;
2450 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2451 rwm.on_bi.bi_op_add = rwm_op_add;
2452 rwm.on_bi.bi_op_delete = rwm_op_delete;
2453 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2454 rwm.on_bi.bi_extended = rwm_extended;
2456 rwm.on_bi.bi_entry_release_rw = rwm_entry_release_rw;
2457 rwm.on_bi.bi_entry_get_rw = rwm_entry_get_rw;
2460 rwm.on_bi.bi_operational = rwm_operational;
2461 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2463 rwm.on_bi.bi_connection_init = rwm_conn_init;
2464 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2466 rwm.on_response = rwm_response;
2468 rwm.on_bi.bi_cf_ocs = rwmocs;
2470 rc = config_register_schema( rwmcfg, rwmocs );
2475 return overlay_register( &rwm );
2478 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2480 init_module( int argc, char *argv[] )
2482 return rwm_initialize();
2484 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2486 #endif /* SLAPD_OVER_RWM */