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 last = (*ap)->a_numvals - 1;
230 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
231 struct ldapmapping *mapping = NULL;
233 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
235 if ( mapping == NULL ) {
236 if ( rwmap->rwm_at.drop_missing ) {
237 /* FIXME: we allow to remove objectClasses as well;
238 * if the resulting entry is inconsistent, that's
239 * the relayed database's business...
241 ch_free( (*ap)->a_vals[ j ].bv_val );
243 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
245 BER_BVZERO( &(*ap)->a_vals[ last ] );
252 ch_free( (*ap)->a_vals[ j ].bv_val );
253 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
257 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
262 struct ldapmapping *mapping = NULL;
264 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
266 if ( mapping == NULL ) {
267 if ( rwmap->rwm_at.drop_missing ) {
272 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
273 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
276 * FIXME: rewrite could fail; in this case
277 * the operation should give up, right?
279 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
281 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
286 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
287 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
289 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
290 if ( rc != LDAP_SUCCESS ) {
295 if ( mapping != NULL ) {
296 assert( mapping->m_dst_ad != NULL );
297 (*ap)->a_desc = mapping->m_dst_ad;
306 /* FIXME: leaking attribute/values? */
313 op->o_callback = &roc->cb;
315 return SLAP_CB_CONTINUE;
319 rwm_conn_init( BackendDB *be, Connection *conn )
321 slap_overinst *on = (slap_overinst *) be->bd_info;
322 struct ldaprwmap *rwmap =
323 (struct ldaprwmap *)on->on_bi.bi_private;
325 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
327 return SLAP_CB_CONTINUE;
331 rwm_conn_destroy( BackendDB *be, Connection *conn )
333 slap_overinst *on = (slap_overinst *) be->bd_info;
334 struct ldaprwmap *rwmap =
335 (struct ldaprwmap *)on->on_bi.bi_private;
337 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
339 return SLAP_CB_CONTINUE;
343 rwm_op_bind( Operation *op, SlapReply *rs )
345 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
348 rwm_op_cb *roc = rwm_callback_get( op, rs );
350 rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
351 if ( rc != LDAP_SUCCESS ) {
352 op->o_bd->bd_info = (BackendInfo *)on->on_info;
353 send_ldap_error( op, rs, rc, "bindDN massage error" );
357 op->o_callback = &roc->cb;
359 return SLAP_CB_CONTINUE;
363 rwm_op_unbind( Operation *op, SlapReply *rs )
365 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
366 struct ldaprwmap *rwmap =
367 (struct ldaprwmap *)on->on_bi.bi_private;
369 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
371 return SLAP_CB_CONTINUE;
375 rwm_op_compare( Operation *op, SlapReply *rs )
377 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
378 struct ldaprwmap *rwmap =
379 (struct ldaprwmap *)on->on_bi.bi_private;
382 struct berval mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
384 rwm_op_cb *roc = rwm_callback_get( op, rs );
386 rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
387 if ( rc != LDAP_SUCCESS ) {
388 op->o_bd->bd_info = (BackendInfo *)on->on_info;
389 send_ldap_error( op, rs, rc, "compareDN massage error" );
393 /* if the attribute is an objectClass, try to remap its value */
394 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
395 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
397 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
398 &mapped_vals[0], RWM_MAP );
399 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
401 op->o_bd->bd_info = (BackendInfo *)on->on_info;
402 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
405 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
406 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
411 struct ldapmapping *mapping = NULL;
412 AttributeDescription *ad = op->orc_ava->aa_desc;
414 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
416 if ( mapping == NULL ) {
417 if ( rwmap->rwm_at.drop_missing ) {
418 op->o_bd->bd_info = (BackendInfo *)on->on_info;
419 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
424 assert( mapping->m_dst_ad != NULL );
425 ad = mapping->m_dst_ad;
428 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
429 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
431 struct berval *mapped_valsp[2];
433 mapped_valsp[0] = &mapped_vals[0];
434 mapped_valsp[1] = &mapped_vals[1];
436 mapped_vals[0] = op->orc_ava->aa_value;
438 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
440 if ( rc != LDAP_SUCCESS ) {
441 op->o_bd->bd_info = (BackendInfo *)on->on_info;
442 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
446 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
447 /* NOTE: if we get here, rwm_dnattr_rewrite()
448 * already freed the old value, so now
450 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
452 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
455 op->orc_ava->aa_desc = ad;
458 op->o_callback = &roc->cb;
460 return SLAP_CB_CONTINUE;
464 rwm_op_delete( Operation *op, SlapReply *rs )
466 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
469 rwm_op_cb *roc = rwm_callback_get( op, rs );
471 rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
472 if ( rc != LDAP_SUCCESS ) {
473 op->o_bd->bd_info = (BackendInfo *)on->on_info;
474 send_ldap_error( op, rs, rc, "deleteDN massage error" );
478 op->o_callback = &roc->cb;
480 return SLAP_CB_CONTINUE;
484 rwm_op_modify( Operation *op, SlapReply *rs )
486 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
487 struct ldaprwmap *rwmap =
488 (struct ldaprwmap *)on->on_bi.bi_private;
494 rwm_op_cb *roc = rwm_callback_get( op, rs );
496 rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
497 if ( rc != LDAP_SUCCESS ) {
498 op->o_bd->bd_info = (BackendInfo *)on->on_info;
499 send_ldap_error( op, rs, rc, "modifyDN massage error" );
503 isupdate = be_shadow_update( op );
504 for ( mlp = &op->orm_modlist; *mlp; ) {
506 Modifications *ml = *mlp;
507 struct ldapmapping *mapping = NULL;
509 /* ml points to a temporary mod until needs duplication */
510 if ( ml->sml_desc == slap_schema.si_ad_objectClass
511 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
515 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
517 ml = ch_malloc( sizeof( Modifications ) );
519 if ( (*mlp)->sml_values ) {
520 ber_bvarray_dup_x( &ml->sml_values, (*mlp)->sml_values, NULL );
521 if ( (*mlp)->sml_nvalues ) {
522 ber_bvarray_dup_x( &ml->sml_nvalues, (*mlp)->sml_nvalues, NULL );
531 drop_missing = rwm_mapping( &rwmap->rwm_at,
532 &ml->sml_desc->ad_cname,
534 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
540 /* duplicate the modlist */
541 ml = ch_malloc( sizeof( Modifications ));
545 if ( ml->sml_values != NULL ) {
549 for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
552 bva = ch_malloc( (num+1) * sizeof( struct berval ));
553 for (i=0; i<num; i++)
554 ber_dupbv( &bva[i], &ml->sml_values[i] );
555 BER_BVZERO( &bva[i] );
556 ml->sml_values = bva;
558 if ( ml->sml_nvalues ) {
559 bva = ch_malloc( (num+1) * sizeof( struct berval ));
560 for (i=0; i<num; i++)
561 ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
562 BER_BVZERO( &bva[i] );
563 ml->sml_nvalues = bva;
571 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
572 struct ldapmapping *oc_mapping = NULL;
574 ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
575 &oc_mapping, RWM_MAP );
576 if ( oc_mapping == NULL ) {
577 if ( rwmap->rwm_at.drop_missing ) {
578 /* FIXME: we allow to remove objectClasses as well;
579 * if the resulting entry is inconsistent, that's
580 * the relayed database's business...
583 ch_free( ml->sml_values[ j ].bv_val );
584 ml->sml_values[ j ] = ml->sml_values[ last ];
586 BER_BVZERO( &ml->sml_values[ last ] );
592 ch_free( ml->sml_values[ j ].bv_val );
593 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
598 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
599 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
601 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
603 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
605 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
606 rc = rwm_referral_rewrite( op, rs,
609 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
610 if ( rc != LDAP_SUCCESS ) {
615 if ( rc != LDAP_SUCCESS ) {
622 if ( mapping != NULL ) {
623 /* use new attribute description */
624 assert( mapping->m_dst_ad != NULL );
625 ml->sml_desc = mapping->m_dst_ad;
633 *mlp = (*mlp)->sml_next;
634 slap_mod_free( &ml->sml_mod, 0 );
638 op->o_callback = &roc->cb;
640 return SLAP_CB_CONTINUE;
644 rwm_op_modrdn( Operation *op, SlapReply *rs )
646 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
647 struct ldaprwmap *rwmap =
648 (struct ldaprwmap *)on->on_bi.bi_private;
652 rwm_op_cb *roc = rwm_callback_get( op, rs );
654 if ( op->orr_newSup ) {
656 struct berval nnewSup = BER_BVNULL;
657 struct berval newSup = BER_BVNULL;
660 * Rewrite the new superior, if defined and required
663 dc.conn = op->o_conn;
665 dc.ctx = "newSuperiorDN";
666 newSup = *op->orr_newSup;
667 nnewSup = *op->orr_nnewSup;
668 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
669 if ( rc != LDAP_SUCCESS ) {
670 op->o_bd->bd_info = (BackendInfo *)on->on_info;
671 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
675 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
676 op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
678 op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
680 *op->orr_newSup = newSup;
681 *op->orr_nnewSup = nnewSup;
686 * Rewrite the dn, if needed
688 rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
689 if ( rc != LDAP_SUCCESS ) {
690 op->o_bd->bd_info = (BackendInfo *)on->on_info;
691 send_ldap_error( op, rs, rc, "renameDN massage error" );
692 if ( op->orr_newSup != roc->ros.orr_newSup ) {
693 ch_free( op->orr_newSup->bv_val );
694 ch_free( op->orr_nnewSup->bv_val );
695 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
696 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
697 op->orr_newSup = roc->ros.orr_newSup;
698 op->orr_nnewSup = roc->ros.orr_nnewSup;
703 /* TODO: rewrite newRDN, attribute types,
704 * values of DN-valued attributes ... */
706 op->o_callback = &roc->cb;
708 return SLAP_CB_CONTINUE;
713 rwm_swap_attrs( Operation *op, SlapReply *rs )
715 slap_callback *cb = op->o_callback;
716 rwm_op_state *ros = cb->sc_private;
718 rs->sr_attrs = ros->ors_attrs;
720 /* other overlays might have touched op->ors_attrs,
721 * so we restore the original version here, otherwise
722 * attribute-mapping might fail */
723 op->ors_attrs = ros->mapped_attrs;
725 return SLAP_CB_CONTINUE;
729 rwm_op_search( Operation *op, SlapReply *rs )
731 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
732 struct ldaprwmap *rwmap =
733 (struct ldaprwmap *)on->on_bi.bi_private;
738 struct berval fstr = BER_BVNULL;
741 AttributeName *an = NULL;
745 rwm_op_cb *roc = rwm_callback_get( op, rs );
747 rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
748 "searchFilter", op->ors_filterstr.bv_val );
749 if ( rc == LDAP_SUCCESS )
750 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
751 if ( rc != LDAP_SUCCESS ) {
752 text = "searchDN massage error";
757 * Rewrite the dn if needed
760 dc.conn = op->o_conn;
762 dc.ctx = "searchFilterAttrDN";
764 rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
765 if ( rc != LDAP_SUCCESS ) {
766 text = "searchFilter/searchFilterAttrDN massage error";
770 f = str2filter_x( op, fstr.bv_val );
773 text = "massaged filter parse error";
778 op->ors_filterstr = fstr;
780 rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
781 op->ors_attrs, &an, RWM_MAP );
782 if ( rc != LDAP_SUCCESS ) {
783 text = "attribute list mapping error";
788 /* store the mapped Attributes for later usage, in
789 * the case that other overlays change op->ors_attrs */
790 roc->ros.mapped_attrs = an;
791 roc->cb.sc_response = rwm_swap_attrs;
793 op->o_callback = &roc->cb;
795 return SLAP_CB_CONTINUE;
803 filter_free_x( op, f );
806 if ( !BER_BVISNULL( &fstr ) ) {
807 ch_free( fstr.bv_val );
810 op->oq_search = roc->ros.oq_search;
812 op->o_bd->bd_info = (BackendInfo *)on->on_info;
813 send_ldap_error( op, rs, rc, text );
820 rwm_exop_passwd( Operation *op, SlapReply *rs )
822 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
826 struct berval id = BER_BVNULL,
829 BerElement *ber = NULL;
831 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
835 if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
836 rs->sr_err = LDAP_OTHER;
840 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
841 &pwold, &pwnew, &rs->sr_text );
842 if ( rs->sr_err != LDAP_SUCCESS ) {
846 if ( !BER_BVISNULL( &id ) ) {
847 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
848 &op->o_req_ndn, op->o_tmpmemctx );
849 if ( rs->sr_err != LDAP_SUCCESS ) {
850 rs->sr_text = "Invalid DN";
855 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
856 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
859 roc = rwm_callback_get( op, rs );
861 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
862 if ( rc != LDAP_SUCCESS ) {
863 op->o_bd->bd_info = (BackendInfo *)on->on_info;
864 send_ldap_error( op, rs, rc, "extendedDN massage error" );
868 ber = ber_alloc_t( LBER_USE_DER );
870 rs->sr_err = LDAP_OTHER;
871 rs->sr_text = "No memory";
874 ber_printf( ber, "{" );
875 if ( !BER_BVISNULL( &id )) {
876 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
879 if ( !BER_BVISNULL( &pwold )) {
880 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
882 if ( !BER_BVISNULL( &pwnew )) {
883 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
885 ber_printf( ber, "N}" );
886 ber_flatten( ber, &op->ore_reqdata );
889 op->o_callback = &roc->cb;
891 return SLAP_CB_CONTINUE;
896 BI_op_extended *extended;
898 { BER_BVC(LDAP_EXOP_MODIFY_PASSWD), rwm_exop_passwd },
903 rwm_extended( Operation *op, SlapReply *rs )
905 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
911 for ( i = 0; exop_table[i].extended != NULL; i++ ) {
912 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
914 rc = exop_table[i].extended( op, rs );
919 case SLAP_CB_CONTINUE:
924 send_ldap_result( op, rs );
931 roc = rwm_callback_get( op, rs );
933 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
934 if ( rc != LDAP_SUCCESS ) {
935 op->o_bd->bd_info = (BackendInfo *)on->on_info;
936 send_ldap_error( op, rs, rc, "extendedDN massage error" );
940 /* TODO: rewrite/map extended data ? ... */
941 op->o_callback = &roc->cb;
943 return SLAP_CB_CONTINUE;
947 rwm_matched( Operation *op, SlapReply *rs )
949 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
950 struct ldaprwmap *rwmap =
951 (struct ldaprwmap *)on->on_bi.bi_private;
953 struct berval dn, mdn;
957 if ( rs->sr_matched == NULL ) {
958 return SLAP_CB_CONTINUE;
962 dc.conn = op->o_conn;
964 dc.ctx = "matchedDN";
965 ber_str2bv( rs->sr_matched, 0, 0, &dn );
967 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
968 if ( rc != LDAP_SUCCESS ) {
970 rs->sr_text = "Rewrite error";
974 if ( mdn.bv_val != dn.bv_val ) {
975 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
976 ch_free( (void *)rs->sr_matched );
979 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
981 rs->sr_matched = mdn.bv_val;
984 return SLAP_CB_CONTINUE;
988 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
990 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
991 struct ldaprwmap *rwmap =
992 (struct ldaprwmap *)on->on_bi.bi_private;
998 int check_duplicate_attrs = 0;
1001 * Rewrite the dn attrs, if needed
1004 dc.conn = op->o_conn;
1007 /* FIXME: the entries are in the remote mapping form;
1008 * so we need to select those attributes we are willing
1009 * to return, and remap them accordingly */
1011 /* FIXME: in principle, one could map an attribute
1012 * on top of another, which already exists.
1013 * As such, in the end there might exist more than
1014 * one instance of an attribute.
1015 * We should at least check if this occurs, and issue
1016 * an error (because multiple instances of attrs in
1017 * response are not valid), or merge the values (what
1018 * about duplicate values?) */
1019 isupdate = be_shadow_update( op );
1020 for ( ap = a_first; *ap; ) {
1021 struct ldapmapping *mapping = NULL;
1026 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
1031 if ( op->ors_attrs != NULL &&
1032 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1033 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1038 drop_missing = rwm_mapping( &rwmap->rwm_at,
1039 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1040 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1044 if ( mapping != NULL ) {
1045 assert( mapping->m_dst_ad != NULL );
1047 /* try to normalize mapped Attributes if the original
1048 * AttributeType was not normalized */
1049 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS) &&
1050 (!(*ap)->a_desc->ad_type->sat_equality ||
1051 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1052 mapping->m_dst_ad->ad_type->sat_equality &&
1053 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1057 last = (*ap)->a_numvals;
1060 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1062 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1065 * check that each value is valid per syntax
1066 * and pretty if appropriate
1068 rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1069 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1070 mapping->m_dst_ad->ad_type->sat_syntax,
1071 mapping->m_dst_ad->ad_type->sat_equality,
1072 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1075 if ( rc != LDAP_SUCCESS ) {
1076 BER_BVZERO( &(*ap)->a_nvals[i] );
1079 BER_BVZERO( &(*ap)->a_nvals[i] );
1083 /* rewrite the attribute description */
1084 (*ap)->a_desc = mapping->m_dst_ad;
1086 /* will need to check for duplicate attrs */
1087 check_duplicate_attrs++;
1091 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1092 if ( stripEntryDN ) {
1093 /* will be generated by frontend */
1097 } else if ( !isupdate
1099 && (*ap)->a_desc->ad_type->sat_no_user_mod
1100 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1105 if ( last == -1 ) { /* not yet counted */
1106 last = (*ap)->a_numvals;
1110 /* empty? leave it in place because of attrsonly and vlv */
1115 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1116 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1120 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1121 struct berval mapped;
1123 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1124 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1126 ch_free( bv[0].bv_val );
1127 BER_BVZERO( &bv[0] );
1128 if ( &(*ap)->a_vals[last] > &bv[0] ) {
1129 bv[0] = (*ap)->a_vals[last];
1130 BER_BVZERO( &(*ap)->a_vals[last] );
1135 } else if ( mapped.bv_val != bv[0].bv_val ) {
1138 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1139 if ( &(*ap)->a_vals[ i ] == bv ) {
1143 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1148 if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1153 * FIXME: after LBER_FREEing
1154 * the value is replaced by
1155 * ch_alloc'ed memory
1157 ber_bvreplace( &bv[0], &mapped );
1159 /* FIXME: will need to check
1160 * if the structuralObjectClass
1166 * It is necessary to try to rewrite attributes with
1167 * dn syntax because they might be used in ACLs as
1168 * members of groups; since ACLs are applied to the
1169 * rewritten stuff, no dn-based subject clause could
1170 * be used at the ldap backend side (see
1171 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1172 * The problem can be overcome by moving the dn-based
1173 * ACLs to the target directory server, and letting
1174 * everything pass thru the ldap backend. */
1175 /* FIXME: handle distinguishedName-like syntaxes, like
1176 * nameAndOptionalUID */
1177 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1178 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1180 dc.ctx = "searchAttrDN";
1181 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
1182 if ( rc != LDAP_SUCCESS ) {
1186 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1187 dc.ctx = "searchAttrDN";
1188 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1189 if ( rc != LDAP_SUCCESS ) {
1196 ap = &(*ap)->a_next;
1201 *ap = (*ap)->a_next;
1206 /* only check if some mapping occurred */
1207 if ( check_duplicate_attrs ) {
1208 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1211 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1212 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1214 Modification mod = { 0 };
1215 const char *text = NULL;
1216 char textbuf[ SLAP_TEXT_BUFLEN ];
1217 Attribute *next = (*tap)->a_next;
1219 BER_BVSTR( &e.e_name, "" );
1220 BER_BVSTR( &e.e_nname, "" );
1222 mod.sm_op = LDAP_MOD_ADD;
1223 mod.sm_desc = (*ap)->a_desc;
1224 mod.sm_type = mod.sm_desc->ad_cname;
1225 mod.sm_numvals = (*tap)->a_numvals;
1226 mod.sm_values = (*tap)->a_vals;
1227 mod.sm_nvalues = (*tap)->a_nvals;
1229 (void)modify_add_values( &e, &mod,
1231 &text, textbuf, sizeof( textbuf ) );
1233 /* should not insert new attrs! */
1234 assert( e.e_attrs == *ap );
1240 tap = &(*tap)->a_next;
1250 rwm_send_entry( Operation *op, SlapReply *rs )
1252 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1253 struct ldaprwmap *rwmap =
1254 (struct ldaprwmap *)on->on_bi.bi_private;
1258 struct berval dn = BER_BVNULL,
1263 assert( rs->sr_entry != NULL );
1266 * Rewrite the dn of the result, if needed
1269 dc.conn = op->o_conn;
1271 dc.ctx = "searchEntryDN";
1274 flags = rs->sr_flags;
1275 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1276 /* FIXME: all we need to duplicate are:
1279 * - attributes that are requested
1280 * - no values if attrsonly is set
1285 rc = LDAP_NO_MEMORY;
1289 flags &= ~REP_ENTRY_MUSTRELEASE;
1290 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1294 * Note: this may fail if the target host(s) schema differs
1295 * from the one known to the meta, and a DN with unknown
1296 * attributes is returned.
1300 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1301 if ( rc != LDAP_SUCCESS ) {
1306 if ( e->e_name.bv_val != dn.bv_val ) {
1307 ch_free( e->e_name.bv_val );
1308 ch_free( e->e_nname.bv_val );
1314 /* TODO: map entry attribute types, objectclasses
1315 * and dn-valued attribute values */
1317 /* FIXME: the entries are in the remote mapping form;
1318 * so we need to select those attributes we are willing
1319 * to return, and remap them accordingly */
1320 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1322 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1323 be_entry_release_rw( op, rs->sr_entry, 0 );
1327 rs->sr_flags = flags;
1329 return SLAP_CB_CONTINUE;
1332 if ( e != NULL && e != rs->sr_entry ) {
1333 if ( e->e_name.bv_val == dn.bv_val ) {
1334 BER_BVZERO( &e->e_name );
1337 if ( e->e_nname.bv_val == ndn.bv_val ) {
1338 BER_BVZERO( &e->e_nname );
1344 if ( !BER_BVISNULL( &dn ) ) {
1345 ch_free( dn.bv_val );
1348 if ( !BER_BVISNULL( &ndn ) ) {
1349 ch_free( ndn.bv_val );
1356 rwm_operational( Operation *op, SlapReply *rs )
1358 /* FIXME: the entries are in the remote mapping form;
1359 * so we need to select those attributes we are willing
1360 * to return, and remap them accordingly */
1361 if ( rs->sr_operational_attrs ) {
1362 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1365 return SLAP_CB_CONTINUE;
1369 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1370 * rewritten for subsequent operations; fine for plain suffixmassage,
1371 * but destroys everything else */
1373 rwm_chk_referrals( Operation *op, SlapReply *rs )
1375 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1378 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1379 if ( rc != LDAP_SUCCESS ) {
1380 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1381 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1385 return SLAP_CB_CONTINUE;
1397 slap_overinst *on = (slap_overinst *) be->bd_info;
1398 struct ldaprwmap *rwmap =
1399 (struct ldaprwmap *)on->on_bi.bi_private;
1401 return rewrite_parse( rwmap->rwm_rw,
1402 fname, lineno, argc, argv );
1408 rwm_suffixmassage_config(
1415 slap_overinst *on = (slap_overinst *) be->bd_info;
1416 struct ldaprwmap *rwmap =
1417 (struct ldaprwmap *)on->on_bi.bi_private;
1419 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1426 * suffixmassage [<suffix>] <massaged suffix>
1428 * the [<suffix>] field must be defined as a valid suffix
1429 * for the current database;
1430 * the <massaged suffix> shouldn't have already been
1431 * defined as a valid suffix for the current server
1434 if ( be->be_suffix == NULL ) {
1435 fprintf( stderr, "%s: line %d: "
1436 " \"suffixMassage [<suffix>]"
1437 " <massaged suffix>\" without "
1438 "<suffix> part requires database "
1439 "suffix be defined first.\n",
1443 bvnc = be->be_suffix[ 0 ];
1446 } else if ( argc == 3 ) {
1447 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1451 fprintf( stderr, "%s: line %d: syntax is"
1452 " \"suffixMassage [<suffix>]"
1453 " <massaged suffix>\"\n",
1458 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1459 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1460 fname, lineno, bvnc.bv_val );
1464 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1465 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1466 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1467 fname, lineno, brnc.bv_val );
1468 free( nvnc.bv_val );
1469 free( pvnc.bv_val );
1474 * The suffix massaging is emulated
1475 * by means of the rewrite capabilities
1477 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1478 &pvnc, &nvnc, &prnc, &nrnc );
1479 free( nvnc.bv_val );
1480 free( pvnc.bv_val );
1481 free( nrnc.bv_val );
1482 free( prnc.bv_val );
1495 slap_overinst *on = (slap_overinst *) be->bd_info;
1496 struct ldaprwmap *rwmap =
1497 (struct ldaprwmap *)on->on_bi.bi_private;
1499 /* objectclass/attribute mapping */
1500 return rwm_map_config( &rwmap->rwm_oc,
1502 fname, lineno, argc, argv );
1506 rwm_response( Operation *op, SlapReply *rs )
1508 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1509 struct ldaprwmap *rwmap =
1510 (struct ldaprwmap *)on->on_bi.bi_private;
1514 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1515 return rwm_send_entry( op, rs );
1518 switch( op->o_tag ) {
1519 case LDAP_REQ_SEARCH:
1522 case LDAP_REQ_DELETE:
1523 case LDAP_REQ_MODRDN:
1524 case LDAP_REQ_MODIFY:
1525 case LDAP_REQ_COMPARE:
1526 case LDAP_REQ_EXTENDED:
1531 * Rewrite the dn of the referrals, if needed
1534 dc.conn = op->o_conn;
1536 dc.ctx = "referralDN";
1537 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1538 if ( rc != LDAP_SUCCESS ) {
1543 rc = rwm_matched( op, rs );
1547 rc = SLAP_CB_CONTINUE;
1562 slap_overinst *on = (slap_overinst *) be->bd_info;
1563 struct ldaprwmap *rwmap =
1564 (struct ldaprwmap *)on->on_bi.bi_private;
1569 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1571 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1574 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1575 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1577 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1578 rc = rwm_m_config( be, fname, lineno, argc, argv );
1580 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1581 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1583 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1586 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1591 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1592 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1594 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1595 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1597 /* TODO: not implemented yet */
1598 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1600 "%s: line %d: \"discover\" not supported yet "
1601 "in \"t-f-support {no|yes|discover}\".\n",
1605 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1610 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1611 fname, lineno, argv[ 1 ] );
1614 } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) == 0 ) {
1617 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1622 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1623 rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1625 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1626 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1630 rc = SLAP_CONF_UNKNOWN;
1641 * dynamic configuration...
1647 RWM_CF_SUFFIXMASSAGE,
1652 RWM_CF_NORMALIZE_MAPPED,
1657 static slap_verbmasks t_f_mode[] = {
1658 { BER_BVC( "yes" ), RWM_F_SUPPORT_T_F },
1659 { BER_BVC( "discover" ), RWM_F_SUPPORT_T_F_DISCOVER },
1660 { BER_BVC( "no" ), RWM_F_NONE },
1664 static ConfigDriver rwm_cf_gen;
1666 static ConfigTable rwmcfg[] = {
1667 { "rwm-rewrite", "rewrite",
1668 2, 0, STRLENOF("rwm-rewrite"),
1669 ARG_MAGIC|ARG_QUOTE|RWM_CF_REWRITE, rwm_cf_gen,
1670 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1671 "DESC 'Rewrites strings' "
1672 "EQUALITY caseIgnoreMatch "
1673 "SYNTAX OMsDirectoryString "
1674 "X-ORDERED 'VALUES' )",
1677 { "rwm-suffixmassage", "[virtual]> <real",
1678 2, 3, 0, ARG_MAGIC|RWM_CF_SUFFIXMASSAGE, rwm_cf_gen,
1681 { "rwm-t-f-support", "true|false|discover",
1682 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1683 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1684 "DESC 'Absolute filters support' "
1685 "SYNTAX OMsDirectoryString "
1689 { "rwm-map", "{objectClass|attribute}",
1690 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1691 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1692 "DESC 'maps attributes/objectClasses' "
1693 "SYNTAX OMsDirectoryString "
1694 "X-ORDERED 'VALUES' )",
1697 { "rwm-normalize-mapped-attrs", "true|false",
1698 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1699 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1700 "DESC 'Normalize mapped attributes/objectClasses' "
1701 "SYNTAX OMsBoolean "
1705 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1708 static ConfigOCs rwmocs[] = {
1709 { "( OLcfgOvOc:16.1 "
1710 "NAME 'olcRwmConfig' "
1711 "DESC 'Rewrite/remap configuration' "
1712 "SUP olcOverlayConfig "
1715 "olcRwmTFSupport $ "
1717 "olcRwmNormalizeMapped "
1719 Cft_Overlay, rwmcfg, NULL, NULL },
1724 slap_rewrite_unparse( BerVarray in, BerVarray *out )
1727 BerVarray bva = NULL;
1728 char ibuf[32], *ptr;
1731 assert( in != NULL );
1733 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1742 bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1743 BER_BVZERO( &bva[ 0 ] );
1745 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1746 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1747 if ( idx.bv_len >= sizeof( ibuf ) ) {
1748 ber_bvarray_free( bva );
1752 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1753 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1754 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1755 ptr = lutil_strcopy( ptr, in[i].bv_val );
1757 BER_BVZERO( &bva[ i + 1 ] );
1764 rwm_cf_gen( ConfigArgs *c )
1766 slap_overinst *on = (slap_overinst *)c->bi;
1767 struct ldaprwmap *rwmap =
1768 (struct ldaprwmap *)on->on_bi.bi_private;
1777 if ( c->op == SLAP_CONFIG_EMIT ) {
1778 struct berval bv = BER_BVNULL;
1780 switch ( c->type ) {
1781 case RWM_CF_REWRITE:
1782 if ( rwmap->rwm_bva_rewrite == NULL ) {
1786 slap_rewrite_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
1787 if ( !c->rvalue_vals ) {
1793 case RWM_CF_T_F_SUPPORT:
1794 enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
1795 if ( BER_BVISNULL( &bv ) ) {
1796 /* there's something wrong... */
1801 value_add_one( &c->rvalue_vals, &bv );
1806 if ( rwmap->rwm_bva_map == NULL ) {
1810 value_add( &c->rvalue_vals, rwmap->rwm_bva_map );
1814 case RWM_CF_NORMALIZE_MAPPED:
1815 c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
1825 } else if ( c->op == LDAP_MOD_DELETE ) {
1826 switch ( c->type ) {
1827 case RWM_CF_REWRITE:
1828 if ( c->valx >= 0 ) {
1829 /* single modification is not allowed */
1832 } else if ( rwmap->rwm_rw != NULL ) {
1833 rewrite_info_delete( &rwmap->rwm_rw );
1834 assert( rwmap->rwm_rw == NULL );
1836 ber_bvarray_free( rwmap->rwm_bva_rewrite );
1837 rwmap->rwm_bva_rewrite = NULL;
1841 case RWM_CF_T_F_SUPPORT:
1842 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1846 if ( c->valx >= 0 ) {
1847 /* single modification is not allowed */
1851 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
1852 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1853 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
1854 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1856 rwmap->rwm_oc.remap = NULL;
1857 rwmap->rwm_oc.map = NULL;
1858 rwmap->rwm_at.remap = NULL;
1859 rwmap->rwm_at.map = NULL;
1861 ber_bvarray_free( rwmap->rwm_bva_map );
1862 rwmap->rwm_bva_map = NULL;
1866 case RWM_CF_NORMALIZE_MAPPED:
1867 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
1876 switch ( c->type ) {
1877 case RWM_CF_REWRITE:
1878 argv0 = c->argv[ 0 ];
1879 c->argv[ 0 ] += STRLENOF( "rwm-" );
1880 rc = rwm_rw_config( &db, c->fname, c->lineno, c->argc, c->argv );
1881 c->argv[ 0 ] = argv0;
1889 line = ldap_charray2str( c->argv, "\" \"" );
1890 if ( line != NULL ) {
1891 int len = strlen( c->argv[ 0 ] );
1893 ber_str2bv( line, 0, 0, &bv );
1894 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1895 bv.bv_len - ( len + 1 ) );
1896 bv.bv_val[ bv.bv_len - 1 ] = '"';
1897 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1902 case RWM_CF_SUFFIXMASSAGE:
1903 argv0 = c->argv[ 0 ];
1904 c->argv[ 0 ] += STRLENOF( "rwm-" );
1905 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno, c->argc, c->argv );
1906 c->argv[ 0 ] = argv0;
1914 /* FIXME: not optimal; in fact, this keeps track
1915 * of the fact that a set of rules was added
1916 * using the rwm-suffixmassage shortcut, but the
1917 * rules are not clarified */
1919 line = ldap_charray2str( c->argv, "\" \"" );
1920 if ( line != NULL ) {
1921 int len = strlen( c->argv[ 0 ] );
1923 ber_str2bv( line, 0, 0, &bv );
1924 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1925 bv.bv_len - ( len + 1 ) );
1926 bv.bv_val[ bv.bv_len - 1 ] = '"';
1927 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1932 case RWM_CF_T_F_SUPPORT:
1933 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
1934 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
1938 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1939 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
1944 argv0 = c->argv[ 0 ];
1945 c->argv[ 0 ] += STRLENOF( "rwm-" );
1946 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
1947 c->argv[ 0 ] = argv0;
1955 line = ldap_charray2str( &c->argv[ 1 ], " " );
1956 if ( line != NULL ) {
1957 ber_str2bv( line, 0, 0, &bv );
1958 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
1963 case RWM_CF_NORMALIZE_MAPPED:
1964 if ( c->value_int ) {
1965 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1967 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
1984 slap_overinst *on = (slap_overinst *) be->bd_info;
1985 struct ldaprwmap *rwmap;
1989 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1991 rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1992 if ( rwmap->rwm_rw == NULL ) {
1997 /* this rewriteContext by default must be null;
1998 * rules can be added if required */
1999 rargv[ 0 ] = "rewriteContext";
2000 rargv[ 1 ] = "searchFilter";
2002 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
2004 rargv[ 0 ] = "rewriteContext";
2005 rargv[ 1 ] = "default";
2007 rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
2010 on->on_bi.bi_private = (void *)rwmap;
2013 (void)rwm_db_destroy( be, NULL );
2024 slap_overinst *on = (slap_overinst *) be->bd_info;
2027 if ( on->on_bi.bi_private ) {
2028 struct ldaprwmap *rwmap =
2029 (struct ldaprwmap *)on->on_bi.bi_private;
2031 if ( rwmap->rwm_rw ) {
2032 rewrite_info_delete( &rwmap->rwm_rw );
2033 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2036 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2037 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2038 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2039 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2040 ber_bvarray_free( rwmap->rwm_bva_map );
2048 static slap_overinst rwm = { { NULL } };
2050 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2052 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2054 rwm_initialize( void )
2058 /* Make sure we don't exceed the bits reserved for userland */
2059 config_check_userland( RWM_CF_LAST );
2061 memset( &rwm, 0, sizeof( slap_overinst ) );
2063 rwm.on_bi.bi_type = "rwm";
2064 rwm.on_bi.bi_flags =
2065 SLAPO_BFLAG_SINGLE |
2068 rwm.on_bi.bi_db_init = rwm_db_init;
2069 rwm.on_bi.bi_db_config = rwm_db_config;
2070 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2072 rwm.on_bi.bi_op_bind = rwm_op_bind;
2073 rwm.on_bi.bi_op_search = rwm_op_search;
2074 rwm.on_bi.bi_op_compare = rwm_op_compare;
2075 rwm.on_bi.bi_op_modify = rwm_op_modify;
2076 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2077 rwm.on_bi.bi_op_add = rwm_op_add;
2078 rwm.on_bi.bi_op_delete = rwm_op_delete;
2079 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2080 rwm.on_bi.bi_extended = rwm_extended;
2082 rwm.on_bi.bi_operational = rwm_operational;
2083 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2085 rwm.on_bi.bi_connection_init = rwm_conn_init;
2086 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2088 rwm.on_response = rwm_response;
2090 rwm.on_bi.bi_cf_ocs = rwmocs;
2092 rc = config_register_schema( rwmcfg, rwmocs );
2097 return overlay_register( &rwm );
2100 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2102 init_module( int argc, char *argv[] )
2104 return rwm_initialize();
2106 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2108 #endif /* SLAPD_OVER_RWM */