1 /* rwm.c - rewrite/remap operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2007 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
24 #include <ac/string.h>
31 typedef struct rwm_op_state {
37 AttributeName *mapped_attrs;
42 rwm_db_destroy( BackendDB *be, ConfigReply *cr );
44 typedef struct rwm_op_cb {
50 rwm_op_cleanup( Operation *op, SlapReply *rs )
52 slap_callback *cb = op->o_callback;
53 rwm_op_state *ros = cb->sc_private;
55 if ( rs->sr_type == REP_RESULT || rs->sr_type == REP_EXTENDED ||
56 op->o_abandon || rs->sr_err == SLAPD_ABANDON ) {
58 op->o_req_dn = ros->ro_dn;
59 op->o_req_ndn = ros->ro_ndn;
61 if ( !BER_BVISNULL( &ros->r_dn )
62 && ros->r_dn.bv_val != ros->r_ndn.bv_val )
64 ch_free( ros->r_dn.bv_val );
65 BER_BVZERO( &ros->r_dn );
68 if ( !BER_BVISNULL( &ros->r_ndn ) ) {
69 ch_free( ros->r_ndn.bv_val );
70 BER_BVZERO( &ros->r_ndn );
73 switch( ros->r_tag ) {
74 case LDAP_REQ_COMPARE:
75 if ( op->orc_ava->aa_value.bv_val != ros->orc_ava->aa_value.bv_val )
76 op->o_tmpfree( op->orc_ava->aa_value.bv_val, op->o_tmpmemctx );
77 op->orc_ava = ros->orc_ava;
80 slap_mods_free( op->orm_modlist, 1 );
81 op->orm_modlist = ros->orm_modlist;
84 if ( op->orr_newSup != ros->orr_newSup ) {
85 ch_free( op->orr_newSup->bv_val );
86 ch_free( op->orr_nnewSup->bv_val );
87 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
88 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
89 op->orr_newSup = ros->orr_newSup;
90 op->orr_nnewSup = ros->orr_nnewSup;
94 ch_free( ros->mapped_attrs );
95 filter_free_x( op, op->ors_filter );
96 ch_free( op->ors_filterstr.bv_val );
97 op->ors_attrs = ros->ors_attrs;
98 op->ors_filter = ros->ors_filter;
99 op->ors_filterstr = ros->ors_filterstr;
101 case LDAP_REQ_EXTENDED:
102 if ( op->ore_reqdata != ros->ore_reqdata ) {
103 ber_bvfree( op->ore_reqdata );
104 op->ore_reqdata = ros->ore_reqdata;
109 op->o_callback = op->o_callback->sc_next;
110 op->o_tmpfree( cb, op->o_tmpmemctx );
113 return SLAP_CB_CONTINUE;
117 rwm_callback_get( Operation *op, SlapReply *rs )
119 rwm_op_cb *roc = NULL;
121 roc = op->o_tmpalloc( sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
122 roc->cb.sc_cleanup = rwm_op_cleanup;
123 roc->cb.sc_response = NULL;
124 roc->cb.sc_next = op->o_callback;
125 roc->cb.sc_private = &roc->ros;
126 roc->ros.r_tag = op->o_tag;
127 roc->ros.ro_dn = op->o_req_dn;
128 roc->ros.ro_ndn = op->o_req_ndn;
129 roc->ros.o_request = op->o_request;
130 BER_BVZERO( &roc->ros.r_dn );
131 BER_BVZERO( &roc->ros.r_ndn );
138 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
141 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
142 struct ldaprwmap *rwmap =
143 (struct ldaprwmap *)on->on_bi.bi_private;
145 struct berval dn = BER_BVNULL,
151 * Rewrite the dn if needed
154 dc.conn = op->o_conn;
156 dc.ctx = (char *)cookie;
158 /* NOTE: in those cases where only the ndn is available,
159 * and the caller sets op->o_req_dn = op->o_req_ndn,
160 * only rewrite the op->o_req_ndn and use it as
161 * op->o_req_dn as well */
163 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
165 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
167 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
170 if ( rc != LDAP_SUCCESS ) {
174 if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
175 || ndn.bv_val == op->o_req_ndn.bv_val )
180 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
193 rwm_op_add( Operation *op, SlapReply *rs )
195 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
196 struct ldaprwmap *rwmap =
197 (struct ldaprwmap *)on->on_bi.bi_private;
201 Attribute **ap = NULL;
202 char *olddn = op->o_req_dn.bv_val;
205 rwm_op_cb *roc = rwm_callback_get( op, rs );
207 rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
208 if ( rc != LDAP_SUCCESS ) {
209 op->o_bd->bd_info = (BackendInfo *)on->on_info;
210 send_ldap_error( op, rs, rc, "addDN massage error" );
214 if ( olddn != op->o_req_dn.bv_val ) {
215 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
216 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
219 /* Count number of attributes in entry */
220 isupdate = be_shadow_update( op );
221 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
224 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
225 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
229 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[ last ] ); last++ )
232 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
233 struct ldapmapping *mapping = NULL;
235 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
237 if ( mapping == NULL ) {
238 if ( rwmap->rwm_at.drop_missing ) {
239 /* FIXME: we allow to remove objectClasses as well;
240 * if the resulting entry is inconsistent, that's
241 * the relayed database's business...
243 ch_free( (*ap)->a_vals[ j ].bv_val );
245 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
247 BER_BVZERO( &(*ap)->a_vals[ last ] );
253 ch_free( (*ap)->a_vals[ j ].bv_val );
254 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
258 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
263 struct ldapmapping *mapping = NULL;
265 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
267 if ( mapping == NULL ) {
268 if ( rwmap->rwm_at.drop_missing ) {
273 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
274 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
277 * FIXME: rewrite could fail; in this case
278 * the operation should give up, right?
280 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
282 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
287 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
288 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
290 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
291 if ( rc != LDAP_SUCCESS ) {
296 if ( mapping != NULL ) {
297 assert( mapping->m_dst_ad != NULL );
298 (*ap)->a_desc = mapping->m_dst_ad;
307 /* FIXME: leaking attribute/values? */
314 op->o_callback = &roc->cb;
316 return SLAP_CB_CONTINUE;
320 rwm_conn_init( BackendDB *be, Connection *conn )
322 slap_overinst *on = (slap_overinst *) be->bd_info;
323 struct ldaprwmap *rwmap =
324 (struct ldaprwmap *)on->on_bi.bi_private;
326 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
328 return SLAP_CB_CONTINUE;
332 rwm_conn_destroy( BackendDB *be, Connection *conn )
334 slap_overinst *on = (slap_overinst *) be->bd_info;
335 struct ldaprwmap *rwmap =
336 (struct ldaprwmap *)on->on_bi.bi_private;
338 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
340 return SLAP_CB_CONTINUE;
344 rwm_op_bind( Operation *op, SlapReply *rs )
346 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
349 rwm_op_cb *roc = rwm_callback_get( op, rs );
351 rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
352 if ( rc != LDAP_SUCCESS ) {
353 op->o_bd->bd_info = (BackendInfo *)on->on_info;
354 send_ldap_error( op, rs, rc, "bindDN massage error" );
358 op->o_callback = &roc->cb;
360 return SLAP_CB_CONTINUE;
364 rwm_op_unbind( Operation *op, SlapReply *rs )
366 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
367 struct ldaprwmap *rwmap =
368 (struct ldaprwmap *)on->on_bi.bi_private;
370 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
372 return SLAP_CB_CONTINUE;
376 rwm_op_compare( Operation *op, SlapReply *rs )
378 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
379 struct ldaprwmap *rwmap =
380 (struct ldaprwmap *)on->on_bi.bi_private;
383 struct berval mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
385 rwm_op_cb *roc = rwm_callback_get( op, rs );
387 rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
388 if ( rc != LDAP_SUCCESS ) {
389 op->o_bd->bd_info = (BackendInfo *)on->on_info;
390 send_ldap_error( op, rs, rc, "compareDN massage error" );
394 /* if the attribute is an objectClass, try to remap its value */
395 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
396 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
398 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
399 &mapped_vals[0], RWM_MAP );
400 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
402 op->o_bd->bd_info = (BackendInfo *)on->on_info;
403 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
406 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
407 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
412 struct ldapmapping *mapping = NULL;
413 AttributeDescription *ad = op->orc_ava->aa_desc;
415 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
417 if ( mapping == NULL ) {
418 if ( rwmap->rwm_at.drop_missing ) {
419 op->o_bd->bd_info = (BackendInfo *)on->on_info;
420 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
425 assert( mapping->m_dst_ad != NULL );
426 ad = mapping->m_dst_ad;
429 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
430 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
432 struct berval *mapped_valsp[2];
434 mapped_valsp[0] = &mapped_vals[0];
435 mapped_valsp[1] = &mapped_vals[1];
437 mapped_vals[0] = op->orc_ava->aa_value;
439 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
441 if ( rc != LDAP_SUCCESS ) {
442 op->o_bd->bd_info = (BackendInfo *)on->on_info;
443 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
447 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
448 /* NOTE: if we get here, rwm_dnattr_rewrite()
449 * already freed the old value, so now
451 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
453 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
456 op->orc_ava->aa_desc = ad;
459 op->o_callback = &roc->cb;
461 return SLAP_CB_CONTINUE;
465 rwm_op_delete( Operation *op, SlapReply *rs )
467 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
470 rwm_op_cb *roc = rwm_callback_get( op, rs );
472 rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
473 if ( rc != LDAP_SUCCESS ) {
474 op->o_bd->bd_info = (BackendInfo *)on->on_info;
475 send_ldap_error( op, rs, rc, "deleteDN massage error" );
479 op->o_callback = &roc->cb;
481 return SLAP_CB_CONTINUE;
485 rwm_op_modify( Operation *op, SlapReply *rs )
487 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
488 struct ldaprwmap *rwmap =
489 (struct ldaprwmap *)on->on_bi.bi_private;
495 rwm_op_cb *roc = rwm_callback_get( op, rs );
497 rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
498 if ( rc != LDAP_SUCCESS ) {
499 op->o_bd->bd_info = (BackendInfo *)on->on_info;
500 send_ldap_error( op, rs, rc, "modifyDN massage error" );
504 isupdate = be_shadow_update( op );
505 for ( mlp = &op->orm_modlist; *mlp; ) {
507 Modifications *ml = *mlp;
508 struct ldapmapping *mapping = NULL;
510 /* ml points to a temporary mod until needs duplication */
511 if ( ml->sml_desc == slap_schema.si_ad_objectClass
512 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
516 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
518 ml = ch_malloc( sizeof( Modifications ) );
520 if ( (*mlp)->sml_values ) {
521 ber_bvarray_dup_x( &ml->sml_values, (*mlp)->sml_values, NULL );
522 if ( (*mlp)->sml_nvalues ) {
523 ber_bvarray_dup_x( &ml->sml_nvalues, (*mlp)->sml_nvalues, NULL );
532 drop_missing = rwm_mapping( &rwmap->rwm_at,
533 &ml->sml_desc->ad_cname,
535 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
541 /* duplicate the modlist */
542 ml = ch_malloc( sizeof( Modifications ));
546 if ( ml->sml_values != NULL ) {
550 for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
553 bva = ch_malloc( (num+1) * sizeof( struct berval ));
554 for (i=0; i<num; i++)
555 ber_dupbv( &bva[i], &ml->sml_values[i] );
556 BER_BVZERO( &bva[i] );
557 ml->sml_values = bva;
559 if ( ml->sml_nvalues ) {
560 bva = ch_malloc( (num+1) * sizeof( struct berval ));
561 for (i=0; i<num; i++)
562 ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
563 BER_BVZERO( &bva[i] );
564 ml->sml_nvalues = bva;
572 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
573 struct ldapmapping *oc_mapping = NULL;
575 ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
576 &oc_mapping, RWM_MAP );
577 if ( oc_mapping == NULL ) {
578 if ( rwmap->rwm_at.drop_missing ) {
579 /* FIXME: we allow to remove objectClasses as well;
580 * if the resulting entry is inconsistent, that's
581 * the relayed database's business...
584 ch_free( ml->sml_values[ j ].bv_val );
585 ml->sml_values[ j ] = ml->sml_values[ last ];
587 BER_BVZERO( &ml->sml_values[ last ] );
593 ch_free( ml->sml_values[ j ].bv_val );
594 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
599 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
600 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
602 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
604 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
606 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
607 rc = rwm_referral_rewrite( op, rs,
610 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
611 if ( rc != LDAP_SUCCESS ) {
616 if ( rc != LDAP_SUCCESS ) {
623 if ( mapping != NULL ) {
624 /* use new attribute description */
625 assert( mapping->m_dst_ad != NULL );
626 ml->sml_desc = mapping->m_dst_ad;
634 *mlp = (*mlp)->sml_next;
635 slap_mod_free( &ml->sml_mod, 0 );
639 op->o_callback = &roc->cb;
641 return SLAP_CB_CONTINUE;
645 rwm_op_modrdn( Operation *op, SlapReply *rs )
647 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
648 struct ldaprwmap *rwmap =
649 (struct ldaprwmap *)on->on_bi.bi_private;
653 rwm_op_cb *roc = rwm_callback_get( op, rs );
655 if ( op->orr_newSup ) {
657 struct berval nnewSup = BER_BVNULL;
658 struct berval newSup = BER_BVNULL;
661 * Rewrite the new superior, if defined and required
664 dc.conn = op->o_conn;
666 dc.ctx = "newSuperiorDN";
667 newSup = *op->orr_newSup;
668 nnewSup = *op->orr_nnewSup;
669 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
670 if ( rc != LDAP_SUCCESS ) {
671 op->o_bd->bd_info = (BackendInfo *)on->on_info;
672 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
676 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
677 op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
679 op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
681 *op->orr_newSup = newSup;
682 *op->orr_nnewSup = nnewSup;
687 * Rewrite the dn, if needed
689 rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
690 if ( rc != LDAP_SUCCESS ) {
691 op->o_bd->bd_info = (BackendInfo *)on->on_info;
692 send_ldap_error( op, rs, rc, "renameDN massage error" );
693 if ( op->orr_newSup != roc->ros.orr_newSup ) {
694 ch_free( op->orr_newSup->bv_val );
695 ch_free( op->orr_nnewSup->bv_val );
696 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
697 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
698 op->orr_newSup = roc->ros.orr_newSup;
699 op->orr_nnewSup = roc->ros.orr_nnewSup;
704 /* TODO: rewrite newRDN, attribute types,
705 * values of DN-valued attributes ... */
707 op->o_callback = &roc->cb;
709 return SLAP_CB_CONTINUE;
714 rwm_swap_attrs( Operation *op, SlapReply *rs )
716 slap_callback *cb = op->o_callback;
717 rwm_op_state *ros = cb->sc_private;
719 rs->sr_attrs = ros->ors_attrs;
721 /* other overlays might have touched op->ors_attrs,
722 * so we restore the original version here, otherwise
723 * attribute-mapping might fail */
724 op->ors_attrs = ros->mapped_attrs;
726 return SLAP_CB_CONTINUE;
730 rwm_op_search( Operation *op, SlapReply *rs )
732 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
733 struct ldaprwmap *rwmap =
734 (struct ldaprwmap *)on->on_bi.bi_private;
739 struct berval fstr = BER_BVNULL;
742 AttributeName *an = NULL;
746 rwm_op_cb *roc = rwm_callback_get( op, rs );
748 rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
749 "searchFilter", op->ors_filterstr.bv_val );
750 if ( rc == LDAP_SUCCESS )
751 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
752 if ( rc != LDAP_SUCCESS ) {
753 text = "searchDN massage error";
758 * Rewrite the dn if needed
761 dc.conn = op->o_conn;
763 dc.ctx = "searchFilterAttrDN";
765 rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
766 if ( rc != LDAP_SUCCESS ) {
767 text = "searchFilter/searchFilterAttrDN massage error";
771 f = str2filter_x( op, fstr.bv_val );
774 text = "massaged filter parse error";
779 op->ors_filterstr = fstr;
781 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
782 op->ors_attrs, &an, RWM_MAP );
783 if ( rc != LDAP_SUCCESS ) {
784 text = "attribute list mapping error";
789 /* store the mapped Attributes for later usage, in
790 * the case that other overlays change op->ors_attrs */
791 roc->ros.mapped_attrs = an;
792 roc->cb.sc_response = rwm_swap_attrs;
794 op->o_callback = &roc->cb;
796 return SLAP_CB_CONTINUE;
804 filter_free_x( op, f );
807 if ( !BER_BVISNULL( &fstr ) ) {
808 ch_free( fstr.bv_val );
811 op->oq_search = roc->ros.oq_search;
813 op->o_bd->bd_info = (BackendInfo *)on->on_info;
814 send_ldap_error( op, rs, rc, text );
821 rwm_exop_passwd( Operation *op, SlapReply *rs )
823 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
827 struct berval id = BER_BVNULL,
830 BerElement *ber = NULL;
832 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
836 if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
837 rs->sr_err = LDAP_OTHER;
841 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
842 &pwold, &pwnew, &rs->sr_text );
843 if ( rs->sr_err != LDAP_SUCCESS ) {
847 if ( !BER_BVISNULL( &id ) ) {
848 char idNul = id.bv_val[id.bv_len];
849 id.bv_val[id.bv_len] = '\0';
850 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
851 &op->o_req_ndn, op->o_tmpmemctx );
852 id.bv_val[id.bv_len] = idNul;
853 if ( rs->sr_err != LDAP_SUCCESS ) {
854 rs->sr_text = "Invalid DN";
859 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
860 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
863 roc = rwm_callback_get( op, rs );
865 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
866 if ( rc != LDAP_SUCCESS ) {
867 op->o_bd->bd_info = (BackendInfo *)on->on_info;
868 send_ldap_error( op, rs, rc, "extendedDN massage error" );
872 ber = ber_alloc_t( LBER_USE_DER );
874 rs->sr_err = LDAP_OTHER;
875 rs->sr_text = "No memory";
878 ber_printf( ber, "{" );
879 if ( !BER_BVISNULL( &id )) {
880 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
883 if ( !BER_BVISNULL( &pwold )) {
884 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
886 if ( !BER_BVISNULL( &pwnew )) {
887 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
889 ber_printf( ber, "N}" );
890 ber_flatten( ber, &op->ore_reqdata );
893 op->o_callback = &roc->cb;
895 return SLAP_CB_CONTINUE;
900 BI_op_extended *extended;
902 { BER_BVC(LDAP_EXOP_MODIFY_PASSWD), rwm_exop_passwd },
907 rwm_extended( Operation *op, SlapReply *rs )
909 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
915 for ( i = 0; exop_table[i].extended != NULL; i++ ) {
916 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
918 rc = exop_table[i].extended( op, rs );
923 case SLAP_CB_CONTINUE:
928 send_ldap_result( op, rs );
935 roc = rwm_callback_get( op, rs );
937 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
938 if ( rc != LDAP_SUCCESS ) {
939 op->o_bd->bd_info = (BackendInfo *)on->on_info;
940 send_ldap_error( op, rs, rc, "extendedDN massage error" );
944 /* TODO: rewrite/map extended data ? ... */
945 op->o_callback = &roc->cb;
947 return SLAP_CB_CONTINUE;
951 rwm_matched( Operation *op, SlapReply *rs )
953 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
954 struct ldaprwmap *rwmap =
955 (struct ldaprwmap *)on->on_bi.bi_private;
957 struct berval dn, mdn;
961 if ( rs->sr_matched == NULL ) {
962 return SLAP_CB_CONTINUE;
966 dc.conn = op->o_conn;
968 dc.ctx = "matchedDN";
969 ber_str2bv( rs->sr_matched, 0, 0, &dn );
971 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
972 if ( rc != LDAP_SUCCESS ) {
974 rs->sr_text = "Rewrite error";
978 if ( mdn.bv_val != dn.bv_val ) {
979 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
980 ch_free( (void *)rs->sr_matched );
983 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
985 rs->sr_matched = mdn.bv_val;
988 return SLAP_CB_CONTINUE;
992 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
994 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
995 struct ldaprwmap *rwmap =
996 (struct ldaprwmap *)on->on_bi.bi_private;
1002 int check_duplicate_attrs = 0;
1005 * Rewrite the dn attrs, if needed
1008 dc.conn = op->o_conn;
1011 /* FIXME: the entries are in the remote mapping form;
1012 * so we need to select those attributes we are willing
1013 * to return, and remap them accordingly */
1015 /* FIXME: in principle, one could map an attribute
1016 * on top of another, which already exists.
1017 * As such, in the end there might exist more than
1018 * one instance of an attribute.
1019 * We should at least check if this occurs, and issue
1020 * an error (because multiple instances of attrs in
1021 * response are not valid), or merge the values (what
1022 * about duplicate values?) */
1023 isupdate = be_shadow_update( op );
1024 for ( ap = a_first; *ap; ) {
1025 struct ldapmapping *mapping = NULL;
1030 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
1035 if ( op->ors_attrs != NULL &&
1036 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1037 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1042 drop_missing = rwm_mapping( &rwmap->rwm_at,
1043 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1044 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1048 if ( mapping != NULL ) {
1049 assert( mapping->m_dst_ad != NULL );
1051 /* try to normalize mapped Attributes if the original
1052 * AttributeType was not normalized */
1053 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS) &&
1054 (!(*ap)->a_desc->ad_type->sat_equality ||
1055 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1056 mapping->m_dst_ad->ad_type->sat_equality &&
1057 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1060 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
1065 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1067 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1070 * check that each value is valid per syntax
1071 * and pretty if appropriate
1073 rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1074 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1075 mapping->m_dst_ad->ad_type->sat_syntax,
1076 mapping->m_dst_ad->ad_type->sat_equality,
1077 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1080 if ( rc != LDAP_SUCCESS ) {
1081 BER_BVZERO( &(*ap)->a_nvals[i] );
1084 BER_BVZERO( &(*ap)->a_nvals[i] );
1088 /* rewrite the attribute description */
1089 (*ap)->a_desc = mapping->m_dst_ad;
1091 /* will need to check for duplicate attrs */
1092 check_duplicate_attrs++;
1096 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1097 if ( stripEntryDN ) {
1098 /* will be generated by frontend */
1102 } else if ( !isupdate
1104 && (*ap)->a_desc->ad_type->sat_no_user_mod
1105 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1110 if ( last == -1 ) { /* not yet counted */
1111 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
1116 /* empty? leave it in place because of attrsonly and vlv */
1121 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1122 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1126 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1127 struct berval mapped;
1129 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1130 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1132 ch_free( bv[0].bv_val );
1133 BER_BVZERO( &bv[0] );
1134 if ( &(*ap)->a_vals[last] > &bv[0] ) {
1135 bv[0] = (*ap)->a_vals[last];
1136 BER_BVZERO( &(*ap)->a_vals[last] );
1141 } else if ( mapped.bv_val != bv[0].bv_val ) {
1144 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1145 if ( &(*ap)->a_vals[ i ] == bv ) {
1149 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1154 if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1159 * FIXME: after LBER_FREEing
1160 * the value is replaced by
1161 * ch_alloc'ed memory
1163 ber_bvreplace( &bv[0], &mapped );
1165 /* FIXME: will need to check
1166 * if the structuralObjectClass
1172 * It is necessary to try to rewrite attributes with
1173 * dn syntax because they might be used in ACLs as
1174 * members of groups; since ACLs are applied to the
1175 * rewritten stuff, no dn-based subject clause could
1176 * be used at the ldap backend side (see
1177 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1178 * The problem can be overcome by moving the dn-based
1179 * ACLs to the target directory server, and letting
1180 * everything pass thru the ldap backend. */
1181 /* FIXME: handle distinguishedName-like syntaxes, like
1182 * nameAndOptionalUID */
1183 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1184 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1186 dc.ctx = "searchAttrDN";
1187 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
1188 if ( rc != LDAP_SUCCESS ) {
1192 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1193 dc.ctx = "searchAttrDN";
1194 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1195 if ( rc != LDAP_SUCCESS ) {
1202 ap = &(*ap)->a_next;
1207 *ap = (*ap)->a_next;
1212 /* only check if some mapping occurred */
1213 if ( check_duplicate_attrs ) {
1214 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1217 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1218 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1220 Modification mod = { 0 };
1221 const char *text = NULL;
1222 char textbuf[ SLAP_TEXT_BUFLEN ];
1223 Attribute *next = (*tap)->a_next;
1225 BER_BVSTR( &e.e_name, "" );
1226 BER_BVSTR( &e.e_nname, "" );
1228 mod.sm_op = LDAP_MOD_ADD;
1229 mod.sm_desc = (*ap)->a_desc;
1230 mod.sm_type = mod.sm_desc->ad_cname;
1231 mod.sm_values = (*tap)->a_vals;
1232 mod.sm_nvalues = (*tap)->a_nvals;
1234 (void)modify_add_values( &e, &mod,
1236 &text, textbuf, sizeof( textbuf ) );
1238 /* should not insert new attrs! */
1239 assert( e.e_attrs == *ap );
1245 tap = &(*tap)->a_next;
1255 rwm_send_entry( Operation *op, SlapReply *rs )
1257 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1258 struct ldaprwmap *rwmap =
1259 (struct ldaprwmap *)on->on_bi.bi_private;
1263 struct berval dn = BER_BVNULL,
1268 assert( rs->sr_entry != NULL );
1271 * Rewrite the dn of the result, if needed
1274 dc.conn = op->o_conn;
1276 dc.ctx = "searchEntryDN";
1279 flags = rs->sr_flags;
1280 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1281 /* FIXME: all we need to duplicate are:
1284 * - attributes that are requested
1285 * - no values if attrsonly is set
1290 rc = LDAP_NO_MEMORY;
1294 flags &= ~REP_ENTRY_MUSTRELEASE;
1295 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1299 * Note: this may fail if the target host(s) schema differs
1300 * from the one known to the meta, and a DN with unknown
1301 * attributes is returned.
1305 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1306 if ( rc != LDAP_SUCCESS ) {
1311 if ( e->e_name.bv_val != dn.bv_val ) {
1312 ch_free( e->e_name.bv_val );
1313 ch_free( e->e_nname.bv_val );
1319 /* TODO: map entry attribute types, objectclasses
1320 * and dn-valued attribute values */
1322 /* FIXME: the entries are in the remote mapping form;
1323 * so we need to select those attributes we are willing
1324 * to return, and remap them accordingly */
1325 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1327 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1328 be_entry_release_rw( op, rs->sr_entry, 0 );
1332 rs->sr_flags = flags;
1334 return SLAP_CB_CONTINUE;
1337 if ( e != NULL && e != rs->sr_entry ) {
1338 if ( e->e_name.bv_val == dn.bv_val ) {
1339 BER_BVZERO( &e->e_name );
1342 if ( e->e_nname.bv_val == ndn.bv_val ) {
1343 BER_BVZERO( &e->e_nname );
1349 if ( !BER_BVISNULL( &dn ) ) {
1350 ch_free( dn.bv_val );
1353 if ( !BER_BVISNULL( &ndn ) ) {
1354 ch_free( ndn.bv_val );
1361 rwm_operational( Operation *op, SlapReply *rs )
1363 /* FIXME: the entries are in the remote mapping form;
1364 * so we need to select those attributes we are willing
1365 * to return, and remap them accordingly */
1366 if ( rs->sr_operational_attrs ) {
1367 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1370 return SLAP_CB_CONTINUE;
1374 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1375 * rewritten for subsequent operations; fine for plain suffixmassage,
1376 * but destroys everything else */
1378 rwm_chk_referrals( Operation *op, SlapReply *rs )
1380 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1383 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1384 if ( rc != LDAP_SUCCESS ) {
1385 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1386 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1390 return SLAP_CB_CONTINUE;
1402 slap_overinst *on = (slap_overinst *) be->bd_info;
1403 struct ldaprwmap *rwmap =
1404 (struct ldaprwmap *)on->on_bi.bi_private;
1406 return rewrite_parse( rwmap->rwm_rw,
1407 fname, lineno, argc, argv );
1413 rwm_suffixmassage_config(
1420 slap_overinst *on = (slap_overinst *) be->bd_info;
1421 struct ldaprwmap *rwmap =
1422 (struct ldaprwmap *)on->on_bi.bi_private;
1424 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1431 * suffixmassage [<suffix>] <massaged suffix>
1433 * the [<suffix>] field must be defined as a valid suffix
1434 * for the current database;
1435 * the <massaged suffix> shouldn't have already been
1436 * defined as a valid suffix for the current server
1439 if ( be->be_suffix == NULL ) {
1440 fprintf( stderr, "%s: line %d: "
1441 " \"suffixMassage [<suffix>]"
1442 " <massaged suffix>\" without "
1443 "<suffix> part requires database "
1444 "suffix be defined first.\n",
1448 bvnc = be->be_suffix[ 0 ];
1451 } else if ( argc == 3 ) {
1452 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1456 fprintf( stderr, "%s: line %d: syntax is"
1457 " \"suffixMassage [<suffix>]"
1458 " <massaged suffix>\"\n",
1463 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1464 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1465 fname, lineno, bvnc.bv_val );
1469 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1470 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1471 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1472 fname, lineno, brnc.bv_val );
1473 free( nvnc.bv_val );
1474 free( pvnc.bv_val );
1479 * The suffix massaging is emulated
1480 * by means of the rewrite capabilities
1482 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1483 &pvnc, &nvnc, &prnc, &nrnc );
1484 free( nvnc.bv_val );
1485 free( pvnc.bv_val );
1486 free( nrnc.bv_val );
1487 free( prnc.bv_val );
1500 slap_overinst *on = (slap_overinst *) be->bd_info;
1501 struct ldaprwmap *rwmap =
1502 (struct ldaprwmap *)on->on_bi.bi_private;
1504 /* objectclass/attribute mapping */
1505 return rwm_map_config( &rwmap->rwm_oc,
1507 fname, lineno, argc, argv );
1511 rwm_response( Operation *op, SlapReply *rs )
1513 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1514 struct ldaprwmap *rwmap =
1515 (struct ldaprwmap *)on->on_bi.bi_private;
1519 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1520 return rwm_send_entry( op, rs );
1523 switch( op->o_tag ) {
1524 case LDAP_REQ_SEARCH:
1527 case LDAP_REQ_DELETE:
1528 case LDAP_REQ_MODRDN:
1529 case LDAP_REQ_MODIFY:
1530 case LDAP_REQ_COMPARE:
1531 case LDAP_REQ_EXTENDED:
1536 * Rewrite the dn of the referrals, if needed
1539 dc.conn = op->o_conn;
1541 dc.ctx = "referralDN";
1542 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1543 if ( rc != LDAP_SUCCESS ) {
1548 rc = rwm_matched( op, rs );
1552 rc = SLAP_CB_CONTINUE;
1567 slap_overinst *on = (slap_overinst *) be->bd_info;
1568 struct ldaprwmap *rwmap =
1569 (struct ldaprwmap *)on->on_bi.bi_private;
1574 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1576 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1579 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1580 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1582 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1583 rc = rwm_m_config( be, fname, lineno, argc, argv );
1585 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1586 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1588 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1591 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1596 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1597 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1599 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1600 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1602 /* TODO: not implemented yet */
1603 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1605 "%s: line %d: \"discover\" not supported yet "
1606 "in \"t-f-support {no|yes|discover}\".\n",
1610 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1615 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1616 fname, lineno, argv[ 1 ] );
1619 } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) == 0 ) {
1622 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1627 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1628 rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1630 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1631 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1635 rc = SLAP_CONF_UNKNOWN;
1646 * dynamic configuration...
1652 RWM_CF_SUFFIXMASSAGE,
1657 RWM_CF_NORMALIZE_MAPPED,
1662 static slap_verbmasks t_f_mode[] = {
1663 { BER_BVC( "yes" ), RWM_F_SUPPORT_T_F },
1664 { BER_BVC( "discover" ), RWM_F_SUPPORT_T_F_DISCOVER },
1665 { BER_BVC( "no" ), RWM_F_NONE },
1669 static ConfigDriver rwm_cf_gen;
1671 static ConfigTable rwmcfg[] = {
1672 { "rwm-rewrite", "rewrite",
1673 2, 0, STRLENOF("rwm-rewrite"),
1674 ARG_MAGIC|ARG_QUOTE|RWM_CF_REWRITE, rwm_cf_gen,
1675 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1676 "DESC 'Rewrites strings' "
1677 "EQUALITY caseIgnoreMatch "
1678 "SYNTAX OMsDirectoryString "
1679 "X-ORDERED 'VALUES' )",
1682 { "rwm-suffixmassage", "[virtual]> <real",
1683 2, 3, 0, ARG_MAGIC|RWM_CF_SUFFIXMASSAGE, rwm_cf_gen,
1686 { "rwm-t-f-support", "true|false|discover",
1687 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1688 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1689 "DESC 'Absolute filters support' "
1690 "SYNTAX OMsDirectoryString "
1694 { "rwm-map", "{objectClass|attribute}",
1695 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1696 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1697 "DESC 'maps attributes/objectClasses' "
1698 "SYNTAX OMsDirectoryString "
1699 "X-ORDERED 'VALUES' )",
1702 { "rwm-normalize-mapped-attrs", "true|false",
1703 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1704 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1705 "DESC 'Normalize mapped attributes/objectClasses' "
1706 "SYNTAX OMsBoolean "
1710 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1713 static ConfigOCs rwmocs[] = {
1714 { "( OLcfgOvOc:16.1 "
1715 "NAME 'olcRwmConfig' "
1716 "DESC 'Rewrite/remap configuration' "
1717 "SUP olcOverlayConfig "
1720 "olcRwmTFSupport $ "
1722 "olcRwmNormalizeMapped "
1724 Cft_Overlay, rwmcfg, NULL, NULL },
1729 slap_rewrite_unparse( BerVarray in, BerVarray *out )
1732 BerVarray bva = NULL;
1733 char ibuf[32], *ptr;
1736 assert( in != NULL );
1738 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1747 bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1748 BER_BVZERO( &bva[ 0 ] );
1750 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1751 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1752 if ( idx.bv_len >= sizeof( ibuf ) ) {
1753 ber_bvarray_free( bva );
1757 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1758 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1759 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1760 ptr = lutil_strcopy( ptr, in[i].bv_val );
1762 BER_BVZERO( &bva[ i + 1 ] );
1769 rwm_cf_gen( ConfigArgs *c )
1771 slap_overinst *on = (slap_overinst *)c->bi;
1772 struct ldaprwmap *rwmap =
1773 (struct ldaprwmap *)on->on_bi.bi_private;
1782 if ( c->op == SLAP_CONFIG_EMIT ) {
1783 struct berval bv = BER_BVNULL;
1785 switch ( c->type ) {
1786 case RWM_CF_REWRITE:
1787 if ( rwmap->rwm_bva_rewrite == NULL ) {
1791 slap_rewrite_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
1792 if ( !c->rvalue_vals ) {
1798 case RWM_CF_T_F_SUPPORT:
1799 enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
1800 if ( BER_BVISNULL( &bv ) ) {
1801 /* there's something wrong... */
1806 value_add_one( &c->rvalue_vals, &bv );
1811 if ( rwmap->rwm_bva_map == NULL ) {
1815 value_add( &c->rvalue_vals, rwmap->rwm_bva_map );
1819 case RWM_CF_NORMALIZE_MAPPED:
1820 c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
1830 } else if ( c->op == LDAP_MOD_DELETE ) {
1831 switch ( c->type ) {
1832 case RWM_CF_REWRITE:
1833 if ( c->valx >= 0 ) {
1834 /* single modification is not allowed */
1837 } else if ( rwmap->rwm_rw != NULL ) {
1838 rewrite_info_delete( &rwmap->rwm_rw );
1839 assert( rwmap->rwm_rw == NULL );
1841 ber_bvarray_free( rwmap->rwm_bva_rewrite );
1842 rwmap->rwm_bva_rewrite = NULL;
1846 case RWM_CF_T_F_SUPPORT:
1847 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1851 if ( c->valx >= 0 ) {
1852 /* single modification is not allowed */
1856 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
1857 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1858 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
1859 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1861 rwmap->rwm_oc.remap = NULL;
1862 rwmap->rwm_oc.map = NULL;
1863 rwmap->rwm_at.remap = NULL;
1864 rwmap->rwm_at.map = NULL;
1866 ber_bvarray_free( rwmap->rwm_bva_map );
1867 rwmap->rwm_bva_map = NULL;
1871 case RWM_CF_NORMALIZE_MAPPED:
1872 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
1881 switch ( c->type ) {
1882 case RWM_CF_REWRITE:
1883 argv0 = c->argv[ 0 ];
1884 c->argv[ 0 ] += STRLENOF( "rwm-" );
1885 rc = rwm_rw_config( &db, c->fname, c->lineno, c->argc, c->argv );
1886 c->argv[ 0 ] = argv0;
1894 line = ldap_charray2str( c->argv, "\" \"" );
1895 if ( line != NULL ) {
1896 int len = strlen( c->argv[ 0 ] );
1898 ber_str2bv( line, 0, 0, &bv );
1899 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1900 bv.bv_len - ( len + 1 ) );
1901 bv.bv_val[ bv.bv_len - 1 ] = '"';
1902 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1907 case RWM_CF_SUFFIXMASSAGE:
1908 argv0 = c->argv[ 0 ];
1909 c->argv[ 0 ] += STRLENOF( "rwm-" );
1910 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno, c->argc, c->argv );
1911 c->argv[ 0 ] = argv0;
1919 /* FIXME: not optimal; in fact, this keeps track
1920 * of the fact that a set of rules was added
1921 * using the rwm-suffixmassage shortcut, but the
1922 * rules are not clarified */
1924 line = ldap_charray2str( c->argv, "\" \"" );
1925 if ( line != NULL ) {
1926 int len = strlen( c->argv[ 0 ] );
1928 ber_str2bv( line, 0, 0, &bv );
1929 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1930 bv.bv_len - ( len + 1 ) );
1931 bv.bv_val[ bv.bv_len - 1 ] = '"';
1932 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1937 case RWM_CF_T_F_SUPPORT:
1938 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
1939 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
1943 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1944 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
1949 argv0 = c->argv[ 0 ];
1950 c->argv[ 0 ] += STRLENOF( "rwm-" );
1951 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
1952 c->argv[ 0 ] = argv0;
1960 line = ldap_charray2str( &c->argv[ 1 ], " " );
1961 if ( line != NULL ) {
1962 ber_str2bv( line, 0, 0, &bv );
1963 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
1968 case RWM_CF_NORMALIZE_MAPPED:
1969 if ( c->value_int ) {
1970 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1972 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
1989 slap_overinst *on = (slap_overinst *) be->bd_info;
1990 struct ldaprwmap *rwmap;
1994 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1996 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1997 if ( rwmap->rwm_rw == NULL ) {
2002 /* this rewriteContext by default must be null;
2003 * rules can be added if required */
2004 rargv[ 0 ] = "rewriteContext";
2005 rargv[ 1 ] = "searchFilter";
2007 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
2009 rargv[ 0 ] = "rewriteContext";
2010 rargv[ 1 ] = "default";
2012 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
2015 on->on_bi.bi_private = (void *)rwmap;
2018 (void)rwm_db_destroy( be, NULL );
2029 slap_overinst *on = (slap_overinst *) be->bd_info;
2032 if ( on->on_bi.bi_private ) {
2033 struct ldaprwmap *rwmap =
2034 (struct ldaprwmap *)on->on_bi.bi_private;
2036 if ( rwmap->rwm_rw ) {
2037 rewrite_info_delete( &rwmap->rwm_rw );
2038 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2041 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2042 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2043 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2044 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2045 ber_bvarray_free( rwmap->rwm_bva_map );
2053 static slap_overinst rwm = { { NULL } };
2055 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2057 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2059 rwm_initialize( void )
2063 /* Make sure we don't exceed the bits reserved for userland */
2064 config_check_userland( RWM_CF_LAST );
2066 memset( &rwm, 0, sizeof( slap_overinst ) );
2068 rwm.on_bi.bi_type = "rwm";
2069 rwm.on_bi.bi_flags =
2070 SLAPO_BFLAG_SINGLE |
2073 rwm.on_bi.bi_db_init = rwm_db_init;
2074 rwm.on_bi.bi_db_config = rwm_db_config;
2075 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2077 rwm.on_bi.bi_op_bind = rwm_op_bind;
2078 rwm.on_bi.bi_op_search = rwm_op_search;
2079 rwm.on_bi.bi_op_compare = rwm_op_compare;
2080 rwm.on_bi.bi_op_modify = rwm_op_modify;
2081 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2082 rwm.on_bi.bi_op_add = rwm_op_add;
2083 rwm.on_bi.bi_op_delete = rwm_op_delete;
2084 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2085 rwm.on_bi.bi_extended = rwm_extended;
2087 rwm.on_bi.bi_operational = rwm_operational;
2088 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2090 rwm.on_bi.bi_connection_init = rwm_conn_init;
2091 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2093 rwm.on_response = rwm_response;
2095 rwm.on_bi.bi_cf_ocs = rwmocs;
2097 rc = config_register_schema( rwmcfg, rwmocs );
2102 return overlay_register( &rwm );
2105 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2107 init_module( int argc, char *argv[] )
2109 return rwm_initialize();
2111 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2113 #endif /* SLAPD_OVER_RWM */