1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2010 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
30 #include <ac/string.h>
31 #include <ac/socket.h>
36 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
40 rwm_mapping_cmp( const void *c1, const void *c2 )
42 struct ldapmapping *map1 = (struct ldapmapping *)c1;
43 struct ldapmapping *map2 = (struct ldapmapping *)c2;
44 int rc = map1->m_src.bv_len - map2->m_src.bv_len;
50 return strcasecmp( map1->m_src.bv_val, map2->m_src.bv_val );
54 rwm_mapping_dup( void *c1, void *c2 )
56 struct ldapmapping *map1 = (struct ldapmapping *)c1;
57 struct ldapmapping *map2 = (struct ldapmapping *)c2;
58 int rc = map1->m_src.bv_len - map2->m_src.bv_len;
64 return ( ( strcasecmp( map1->m_src.bv_val, map2->m_src.bv_val ) == 0 ) ? -1 : 0 );
68 rwm_map_init( struct ldapmap *lm, struct ldapmapping **m )
70 struct ldapmapping *mapping;
78 mapping = (struct ldapmapping *)ch_calloc( 2,
79 sizeof( struct ldapmapping ) );
80 if ( mapping == NULL ) {
81 return LDAP_NO_MEMORY;
84 /* NOTE: this is needed to make sure that
86 * does not filter out all attributes including objectClass */
87 rc = slap_str2ad( "objectClass", &mapping[0].m_src_ad, &text );
88 if ( rc != LDAP_SUCCESS ) {
93 mapping[0].m_dst_ad = mapping[0].m_src_ad;
94 ber_dupbv( &mapping[0].m_src, &mapping[0].m_src_ad->ad_cname );
95 ber_dupbv( &mapping[0].m_dst, &mapping[0].m_src );
97 mapping[1].m_src = mapping[0].m_src;
98 mapping[1].m_dst = mapping[0].m_dst;
99 mapping[1].m_src_ad = mapping[0].m_src_ad;
100 mapping[1].m_dst_ad = mapping[1].m_src_ad;
102 avl_insert( &lm->map, (caddr_t)&mapping[0],
103 rwm_mapping_cmp, rwm_mapping_dup );
104 avl_insert( &lm->remap, (caddr_t)&mapping[1],
105 rwm_mapping_cmp, rwm_mapping_dup );
113 rwm_mapping( struct ldapmap *map, struct berval *s, struct ldapmapping **m, int remap )
116 struct ldapmapping fmapping;
124 /* let special attrnames slip through (ITS#5760) */
125 if ( bvmatch( s, slap_bv_no_attrs )
126 || bvmatch( s, slap_bv_all_user_attrs )
127 || bvmatch( s, slap_bv_all_operational_attrs ) )
133 if ( remap == RWM_REMAP ) {
141 *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping,
145 return map->drop_missing;
152 rwm_map( struct ldapmap *map, struct berval *s, struct berval *bv, int remap )
154 struct ldapmapping *mapping;
156 /* map->map may be NULL when mapping is configured,
157 * but map->remap can't */
158 if ( map->remap == NULL ) {
164 ( void )rwm_mapping( map, s, &mapping, remap );
165 if ( mapping != NULL ) {
166 if ( !BER_BVISNULL( &mapping->m_dst ) ) {
167 *bv = mapping->m_dst;
172 if ( !map->drop_missing ) {
178 * Map attribute names in place
183 struct ldapmap *at_map,
184 struct ldapmap *oc_map,
191 assert( anp != NULL );
195 if ( an == NULL && op->o_bd->be_extra_anlist == NULL ) {
201 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
206 if ( op->o_bd->be_extra_anlist ) {
207 for ( ; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ )
211 assert( i > 0 || x > 0 );
212 *anp = op->o_tmpalloc( ( i + x + 1 )* sizeof( AttributeName ),
214 if ( *anp == NULL ) {
215 return LDAP_NO_MEMORY;
218 for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
219 struct ldapmapping *m;
220 int at_drop_missing = 0,
223 if ( an[i].an_desc ) {
225 /* FIXME: better leave as is? */
229 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
230 if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
241 if ( remap == RWM_MAP ) {
242 (*anp)[j].an_name = m->m_dst;
243 (*anp)[j].an_desc = m->m_dst_ad;
245 (*anp)[j].an_name = m->m_src;
246 (*anp)[j].an_desc = m->m_src_ad;
253 } else if ( an[i].an_oc ) {
255 /* FIXME: better leave as is? */
259 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
261 if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
272 if ( remap == RWM_MAP ) {
273 (*anp)[j].an_name = m->m_dst;
274 (*anp)[j].an_oc = m->m_dst_oc;
276 (*anp)[j].an_name = m->m_src;
277 (*anp)[j].an_oc = m->m_src_oc;
281 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
283 if ( at_drop_missing || !m ) {
284 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
286 /* if both at_map and oc_map required to drop missing,
288 if ( oc_drop_missing && at_drop_missing ) {
292 /* if no oc_map mapping was found and at_map required
293 * to drop missing, then do it; otherwise, at_map wins
294 * and an is considered an attr and is left unchanged */
296 if ( at_drop_missing ) {
304 if ( BER_BVISNULL( &m->m_dst ) ) {
309 if ( remap == RWM_MAP ) {
310 (*anp)[j].an_name = m->m_dst;
311 (*anp)[j].an_oc = m->m_dst_oc;
313 (*anp)[j].an_name = m->m_src;
314 (*anp)[j].an_oc = m->m_src_oc;
320 if ( !BER_BVISNULL( &m->m_dst ) ) {
322 if ( remap == RWM_MAP ) {
323 (*anp)[j].an_name = m->m_dst;
324 (*anp)[j].an_desc = m->m_dst_ad;
326 (*anp)[j].an_name = m->m_src;
327 (*anp)[j].an_desc = m->m_src_ad;
335 if ( op->o_bd->be_extra_anlist != NULL ) {
336 /* we assume be_extra_anlist are already mapped */
337 for ( x = 0; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ ) {
338 BER_BVZERO( &(*anp)[j].an_name );
339 if ( op->o_bd->be_extra_anlist[x].an_desc &&
340 ad_inlist( op->o_bd->be_extra_anlist[x].an_desc, *anp ) )
345 (*anp)[j] = op->o_bd->be_extra_anlist[x];
350 if ( j == 0 && ( i != 0 || x != 0 ) ) {
351 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
352 (*anp)[0].an_name = *slap_bv_no_attrs;
355 memset( &(*anp)[j], 0, sizeof( AttributeName ) );
363 struct ldapmap *at_map,
366 char ***mapped_attrs )
372 *mapped_attrs = NULL;
376 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ); i++ )
379 na = (char **)ch_calloc( i + 1, sizeof( char * ) );
381 *mapped_attrs = NULL;
382 return LDAP_NO_MEMORY;
385 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
386 struct ldapmapping *mapping;
388 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
393 na[ j++ ] = an[ i ].an_name.bv_val;
395 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
396 na[ j++ ] = mapping->m_dst.bv_val;
400 if ( j == 0 && i != 0 ) {
401 na[ j++ ] = LDAP_NO_ATTRS;
415 AttributeDescription **adp,
416 struct berval *mapped_attr,
417 struct berval *value,
418 struct berval *mapped_value,
422 struct berval vtmp = BER_BVNULL;
424 AttributeDescription *ad = *adp;
425 struct ldapmapping *mapping = NULL;
427 rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
428 if ( mapping == NULL ) {
429 if ( dc->rwmap->rwm_at.drop_missing ) {
433 *mapped_attr = ad->ad_cname;
436 *mapped_attr = mapping->m_dst;
439 if ( value != NULL ) {
440 assert( mapped_value != NULL );
442 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
443 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
448 fdc.ctx = "searchFilterAttrDN";
451 rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
454 if ( vtmp.bv_val != value->bv_val ) {
459 case LDAP_UNWILLING_TO_PERFORM:
465 } else if ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) {
466 if ( ad->ad_type->sat_equality->smr_normalize(
467 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
468 NULL, NULL, value, &vtmp, memctx ) )
474 } else if ( ad == slap_schema.si_ad_objectClass
475 || ad == slap_schema.si_ad_structuralObjectClass )
477 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
478 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
486 filter_escape_value_x( &vtmp, mapped_value, memctx );
490 ch_free( vtmp.bv_val );
494 ber_memfree_x( vtmp.bv_val, memctx );
499 if ( mapping != NULL ) {
500 assert( mapping->m_dst_ad != NULL );
501 *adp = mapping->m_dst_ad;
508 rwm_int_filter_map_rewrite(
512 struct berval *fstr )
516 AttributeDescription *ad;
521 /* better than nothing... */
522 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
523 ber_bvtf_false = BER_BVC( "(|)" ),
524 /* better than nothing... */
525 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
526 ber_bvtf_true = BER_BVC( "(&)" ),
528 /* no longer needed; preserved for completeness */
529 ber_bvundefined = BER_BVC( "(?=undefined)" ),
531 ber_bverror = BER_BVC( "(?=error)" ),
532 ber_bvunknown = BER_BVC( "(?=unknown)" ),
533 ber_bvnone = BER_BVC( "(?=none)" );
536 assert( fstr != NULL );
540 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
544 if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
548 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
549 case LDAP_FILTER_EQUALITY:
551 if ( map_attr_value( dc, &ad, &atmp,
552 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
557 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
558 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
560 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
561 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
563 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
568 if ( map_attr_value( dc, &ad, &atmp,
569 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
574 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
575 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
577 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
578 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
580 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
585 if ( map_attr_value( dc, &ad, &atmp,
586 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
591 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
592 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
594 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
595 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
597 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
600 case LDAP_FILTER_APPROX:
602 if ( map_attr_value( dc, &ad, &atmp,
603 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
608 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
609 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
611 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
612 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
614 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
617 case LDAP_FILTER_SUBSTRINGS:
619 if ( map_attr_value( dc, &ad, &atmp,
620 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
625 /* cannot be a DN ... */
627 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
628 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
630 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
633 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
636 filter_escape_value_x( &f->f_sub_initial, &vtmp, op->o_tmpmemctx );
638 fstr->bv_len += vtmp.bv_len;
639 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
642 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
643 /* "(attr=" */ "%s*)",
644 vtmp.bv_len ? vtmp.bv_val : "" );
646 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
649 if ( f->f_sub_any != NULL ) {
650 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
652 filter_escape_value_x( &f->f_sub_any[i], &vtmp,
655 fstr->bv_len += vtmp.bv_len + 1;
656 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
659 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
660 /* "(attr=[init]*[any*]" */ "%s*)",
661 vtmp.bv_len ? vtmp.bv_val : "" );
662 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
666 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
669 filter_escape_value_x( &f->f_sub_final, &vtmp, op->o_tmpmemctx );
671 fstr->bv_len += vtmp.bv_len;
672 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
675 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
676 /* "(attr=[init*][any*]" */ "%s)",
677 vtmp.bv_len ? vtmp.bv_val : "" );
679 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
684 case LDAP_FILTER_PRESENT:
686 if ( map_attr_value( dc, &ad, &atmp,
687 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
692 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
693 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
695 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
699 case LDAP_FILTER_AND:
701 case LDAP_FILTER_NOT:
702 fstr->bv_len = STRLENOF( "(%)" );
703 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
705 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
706 f->f_choice == LDAP_FILTER_AND ? '&' :
707 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
709 for ( p = f->f_list; p != NULL; p = p->f_next ) {
714 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
715 if ( rc != LDAP_SUCCESS ) {
719 fstr->bv_len += vtmp.bv_len;
720 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
723 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
724 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
726 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
731 case LDAP_FILTER_EXT: {
732 if ( f->f_mr_desc ) {
734 if ( map_attr_value( dc, &ad, &atmp,
735 &f->f_mr_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
741 BER_BVSTR( &atmp, "" );
742 filter_escape_value_x( &f->f_mr_value, &vtmp, op->o_tmpmemctx );
746 fstr->bv_len = atmp.bv_len +
747 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
748 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
749 vtmp.bv_len + STRLENOF( "(:=)" );
750 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
752 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
754 f->f_mr_dnattrs ? ":dn" : "",
755 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
756 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
757 vtmp.bv_len ? vtmp.bv_val : "" );
758 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
764 filter_free_x( op, f, 0 );
765 f->f_choice = SLAPD_FILTER_COMPUTED;
766 f->f_result = SLAPD_COMPARE_UNDEFINED;
769 case SLAPD_FILTER_COMPUTED:
770 switch ( f->f_result ) {
771 case LDAP_COMPARE_FALSE:
772 /* FIXME: treat UNDEFINED as FALSE */
773 case SLAPD_COMPARE_UNDEFINED:
774 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
775 tmp = &ber_bvtf_false;
781 case LDAP_COMPARE_TRUE:
782 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
783 tmp = &ber_bvtf_true;
794 ber_dupbv_x( fstr, tmp, op->o_tmpmemctx );
798 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
806 rwm_filter_map_rewrite(
810 struct berval *fstr )
816 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
825 fdc.ctx = "searchFilter";
827 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
828 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
829 fdc.conn, &fstr->bv_val ) )
831 case REWRITE_REGEXEC_OK:
832 if ( !BER_BVISNULL( fstr ) ) {
833 fstr->bv_len = strlen( fstr->bv_val );
839 Debug( LDAP_DEBUG_ARGS,
840 "[rw] %s: \"%s\" -> \"%s\"\n",
841 fdc.ctx, ftmp.bv_val, fstr->bv_val );
842 if ( fstr->bv_val != ftmp.bv_val ) {
843 ber_bvreplace_x( &ftmp, fstr, op->o_tmpmemctx );
844 ch_free( fstr->bv_val );
850 case REWRITE_REGEXEC_UNWILLING:
852 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
853 fdc.rs->sr_text = "Operation not allowed";
855 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
856 rc = LDAP_UNWILLING_TO_PERFORM;
859 case REWRITE_REGEXEC_ERR:
861 fdc.rs->sr_err = LDAP_OTHER;
862 fdc.rs->sr_text = "Rewrite error";
864 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
873 * I don't like this much, but we need two different
874 * functions because different heap managers may be
875 * in use in back-ldap/meta to reduce the amount of
876 * calls to malloc routines, and some of the free()
877 * routines may be macros with args
880 rwm_referral_rewrite(
885 BerVarray *pa_nvals )
887 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
888 struct ldaprwmap *rwmap =
889 (struct ldaprwmap *)on->on_bi.bi_private;
894 struct berval dn = BER_BVNULL,
897 assert( a_vals != NULL );
900 * Rewrite the dn if needed
903 dc.conn = op->o_conn;
905 dc.ctx = (char *)cookie;
907 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
911 if ( pa_nvals != NULL ) {
912 if ( *pa_nvals == NULL ) {
913 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
914 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
918 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
919 struct berval olddn = BER_BVNULL,
925 rc = ldap_url_parse( oldval.bv_val, &ludp );
926 if ( rc != LDAP_URL_SUCCESS ) {
927 /* leave attr untouched if massage failed */
928 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
929 ber_dupbv( &(*pa_nvals)[i], &oldval );
934 /* FIXME: URLs like "ldap:///dc=suffix" if passed
935 * thru ldap_url_parse() and ldap_url_desc2str()
936 * get rewritten as "ldap:///dc=suffix??base";
937 * we don't want this to occur... */
938 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
939 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
942 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
947 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
950 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
954 case LDAP_UNWILLING_TO_PERFORM:
956 * FIXME: need to check if it may be considered
957 * legal to trim values when adding/modifying;
958 * it should be when searching (e.g. ACLs).
960 ch_free( a_vals[i].bv_val );
962 a_vals[i] = a_vals[last];
964 (*pa_nvals)[i] = (*pa_nvals)[last];
967 BER_BVZERO( &a_vals[last] );
969 BER_BVZERO( &(*pa_nvals)[last] );
975 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
978 ludp->lud_dn = dn.bv_val;
979 newurl = ldap_url_desc2str( ludp );
980 ludp->lud_dn = olddn.bv_val;
981 ch_free( dn.bv_val );
982 if ( newurl == NULL ) {
983 /* FIXME: leave attr untouched
984 * even if ldap_url_desc2str failed...
989 ber_str2bv( newurl, 0, 1, &a_vals[i] );
990 ber_memfree( newurl );
993 ludp->lud_dn = ndn.bv_val;
994 newurl = ldap_url_desc2str( ludp );
995 ludp->lud_dn = olddn.bv_val;
996 ch_free( ndn.bv_val );
997 if ( newurl == NULL ) {
998 /* FIXME: leave attr untouched
999 * even if ldap_url_desc2str failed...
1001 ch_free( a_vals[i].bv_val );
1006 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1007 ch_free( (*pa_nvals)[i].bv_val );
1009 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
1010 ber_memfree( newurl );
1013 ch_free( oldval.bv_val );
1014 ludp->lud_dn = olddn.bv_val;
1019 /* leave attr untouched if massage failed */
1020 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1021 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
1025 ldap_free_urldesc( ludp );
1032 * I don't like this much, but we need two different
1033 * functions because different heap managers may be
1034 * in use in back-ldap/meta to reduce the amount of
1035 * calls to malloc routines, and some of the free()
1036 * routines may be macros with args
1044 BerVarray *pa_nvals )
1046 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1047 struct ldaprwmap *rwmap =
1048 (struct ldaprwmap *)on->on_bi.bi_private;
1053 struct berval dn = BER_BVNULL,
1061 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1068 * Rewrite the dn if needed
1071 dc.conn = op->o_conn;
1073 dc.ctx = (char *)cookie;
1075 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1077 if ( pa_nvals != NULL ) {
1078 if ( *pa_nvals == NULL ) {
1079 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1080 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1084 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1090 ndn = (*pa_nvals)[i];
1091 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1093 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1097 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1101 case LDAP_UNWILLING_TO_PERFORM:
1103 * FIXME: need to check if it may be considered
1104 * legal to trim values when adding/modifying;
1105 * it should be when searching (e.g. ACLs).
1107 ch_free( in[i].bv_val );
1110 if ( a_vals && pa_nvals ) {
1111 (*pa_nvals)[i] = (*pa_nvals)[last];
1114 BER_BVZERO( &in[last] );
1115 if ( a_vals && pa_nvals ) {
1116 BER_BVZERO( &(*pa_nvals)[last] );
1123 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1124 ch_free( a_vals[i].bv_val );
1128 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1129 ch_free( (*pa_nvals)[i].bv_val );
1131 (*pa_nvals)[i] = ndn;
1136 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1137 ch_free( (*pa_nvals)[i].bv_val );
1138 (*pa_nvals)[i] = ndn;
1144 /* leave attr untouched if massage failed */
1145 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1146 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1156 rwm_referral_result_rewrite(
1162 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1165 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1171 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1172 if ( rc != LDAP_URL_SUCCESS ) {
1173 /* leave attr untouched if massage failed */
1177 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1178 * thru ldap_url_parse() and ldap_url_desc2str()
1179 * get rewritten as "ldap:///dc=suffix??base";
1180 * we don't want this to occur... */
1181 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1182 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1185 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1188 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1190 case LDAP_UNWILLING_TO_PERFORM:
1192 * FIXME: need to check if it may be considered
1193 * legal to trim values when adding/modifying;
1194 * it should be when searching (e.g. ACLs).
1196 ch_free( a_vals[i].bv_val );
1198 a_vals[i] = a_vals[last];
1200 BER_BVZERO( &a_vals[last] );
1206 /* leave attr untouched if massage failed */
1207 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1210 ludp->lud_dn = dn.bv_val;
1211 newurl = ldap_url_desc2str( ludp );
1212 if ( newurl == NULL ) {
1213 /* FIXME: leave attr untouched
1214 * even if ldap_url_desc2str failed...
1219 ch_free( a_vals[i].bv_val );
1220 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1221 ber_memfree( newurl );
1222 ludp->lud_dn = olddn.bv_val;
1227 ldap_free_urldesc( ludp );
1234 rwm_dnattr_result_rewrite(
1241 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1244 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1245 struct berval pdn, ndn = BER_BVNULL;
1249 rc = rwm_dn_massage_pretty_normalize( dc, &a_vals[i], &pdn, &ndn );
1251 case LDAP_UNWILLING_TO_PERFORM:
1253 * FIXME: need to check if it may be considered
1254 * legal to trim values when adding/modifying;
1255 * it should be when searching (e.g. ACLs).
1257 assert( a_vals[i].bv_val != a_nvals[i].bv_val );
1258 ch_free( a_vals[i].bv_val );
1259 ch_free( a_nvals[i].bv_val );
1261 a_vals[i] = a_vals[last];
1262 a_nvals[i] = a_nvals[last];
1264 BER_BVZERO( &a_vals[last] );
1265 BER_BVZERO( &a_nvals[last] );
1270 /* leave attr untouched if massage failed */
1271 if ( !BER_BVISNULL( &pdn ) && a_vals[i].bv_val != pdn.bv_val ) {
1272 ch_free( a_vals[i].bv_val );
1275 if ( !BER_BVISNULL( &ndn ) && a_nvals[i].bv_val != ndn.bv_val ) {
1276 ch_free( a_nvals[i].bv_val );
1287 rwm_mapping_dst_free( void *v_mapping )
1289 struct ldapmapping *mapping = v_mapping;
1291 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1292 rwm_mapping_free( &mapping[ -1 ] );
1297 rwm_mapping_free( void *v_mapping )
1299 struct ldapmapping *mapping = v_mapping;
1301 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1302 ch_free( mapping[0].m_src.bv_val );
1305 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1306 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1307 if ( mapping[0].m_src_oc ) {
1308 ch_free( mapping[0].m_src_oc );
1312 if ( mapping[0].m_src_ad ) {
1313 ch_free( mapping[0].m_src_ad );
1318 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1319 ch_free( mapping[0].m_dst.bv_val );
1322 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1323 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1324 if ( mapping[0].m_dst_oc ) {
1325 ch_free( mapping[0].m_dst_oc );
1329 if ( mapping[0].m_dst_ad ) {
1330 ch_free( mapping[0].m_dst_ad );
1339 #endif /* SLAPD_OVER_RWM */