1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2009 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
182 struct ldapmap *at_map,
183 struct ldapmap *oc_map,
190 assert( anp != NULL );
198 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
200 *anp = ch_malloc( ( i + 1 )* sizeof( AttributeName ) );
201 if ( *anp == NULL ) {
202 return LDAP_NO_MEMORY;
205 for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
206 struct ldapmapping *m;
207 int at_drop_missing = 0,
210 if ( an[i].an_desc ) {
212 /* FIXME: better leave as is? */
216 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
217 if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
228 if ( remap == RWM_MAP ) {
229 (*anp)[j].an_name = m->m_dst;
230 (*anp)[j].an_desc = m->m_dst_ad;
232 (*anp)[j].an_name = m->m_src;
233 (*anp)[j].an_desc = m->m_src_ad;
240 } else if ( an[i].an_oc ) {
242 /* FIXME: better leave as is? */
246 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
248 if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
259 if ( remap == RWM_MAP ) {
260 (*anp)[j].an_name = m->m_dst;
261 (*anp)[j].an_oc = m->m_dst_oc;
263 (*anp)[j].an_name = m->m_src;
264 (*anp)[j].an_oc = m->m_src_oc;
268 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
270 if ( at_drop_missing || !m ) {
271 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
273 /* if both at_map and oc_map required to drop missing,
275 if ( oc_drop_missing && at_drop_missing ) {
279 /* if no oc_map mapping was found and at_map required
280 * to drop missing, then do it; otherwise, at_map wins
281 * and an is considered an attr and is left unchanged */
283 if ( at_drop_missing ) {
291 if ( BER_BVISNULL( &m->m_dst ) ) {
296 if ( remap == RWM_MAP ) {
297 (*anp)[j].an_name = m->m_dst;
298 (*anp)[j].an_oc = m->m_dst_oc;
300 (*anp)[j].an_name = m->m_src;
301 (*anp)[j].an_oc = m->m_src_oc;
307 if ( !BER_BVISNULL( &m->m_dst ) ) {
309 if ( remap == RWM_MAP ) {
310 (*anp)[j].an_name = m->m_dst;
311 (*anp)[j].an_desc = m->m_dst_ad;
313 (*anp)[j].an_name = m->m_src;
314 (*anp)[j].an_desc = m->m_src_ad;
322 if ( j == 0 && i != 0 ) {
323 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
324 (*anp)[0].an_name = *slap_bv_no_attrs;
327 memset( &(*anp)[j], 0, sizeof( AttributeName ) );
334 struct ldapmap *at_map,
337 char ***mapped_attrs )
343 *mapped_attrs = NULL;
347 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ); i++ )
350 na = (char **)ch_calloc( i + 1, sizeof( char * ) );
352 *mapped_attrs = NULL;
353 return LDAP_NO_MEMORY;
356 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
357 struct ldapmapping *mapping;
359 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
364 na[ j++ ] = an[ i ].an_name.bv_val;
366 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
367 na[ j++ ] = mapping->m_dst.bv_val;
371 if ( j == 0 && i != 0 ) {
372 na[ j++ ] = LDAP_NO_ATTRS;
385 AttributeDescription **adp,
386 struct berval *mapped_attr,
387 struct berval *value,
388 struct berval *mapped_value,
391 struct berval vtmp = BER_BVNULL;
393 AttributeDescription *ad = *adp;
394 struct ldapmapping *mapping = NULL;
396 rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
397 if ( mapping == NULL ) {
398 if ( dc->rwmap->rwm_at.drop_missing ) {
402 *mapped_attr = ad->ad_cname;
405 *mapped_attr = mapping->m_dst;
408 if ( value != NULL ) {
409 assert( mapped_value != NULL );
411 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
412 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
417 fdc.ctx = "searchFilterAttrDN";
420 rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
423 if ( vtmp.bv_val != value->bv_val ) {
428 case LDAP_UNWILLING_TO_PERFORM:
434 } else if ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) {
435 if ( ad->ad_type->sat_equality->smr_normalize(
436 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
437 NULL, NULL, value, &vtmp, NULL ) )
443 } else if ( ad == slap_schema.si_ad_objectClass
444 || ad == slap_schema.si_ad_structuralObjectClass )
446 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
447 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
455 filter_escape_value( &vtmp, mapped_value );
458 ch_free( vtmp.bv_val );
462 if ( mapping != NULL ) {
463 assert( mapping->m_dst_ad != NULL );
464 *adp = mapping->m_dst_ad;
471 rwm_int_filter_map_rewrite(
475 struct berval *fstr )
479 AttributeDescription *ad;
484 /* better than nothing... */
485 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
486 ber_bvtf_false = BER_BVC( "(|)" ),
487 /* better than nothing... */
488 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
489 ber_bvtf_true = BER_BVC( "(&)" ),
491 /* no longer needed; preserved for completeness */
492 ber_bvundefined = BER_BVC( "(?=undefined)" ),
494 ber_bverror = BER_BVC( "(?=error)" ),
495 ber_bvunknown = BER_BVC( "(?=unknown)" ),
496 ber_bvnone = BER_BVC( "(?=none)" );
499 assert( fstr != NULL );
503 ber_dupbv( fstr, &ber_bvnone );
507 if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
511 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
512 case LDAP_FILTER_EQUALITY:
514 if ( map_attr_value( dc, &ad, &atmp,
515 &f->f_av_value, &vtmp, RWM_MAP ) )
520 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
521 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
523 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
524 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
526 ch_free( vtmp.bv_val );
531 if ( map_attr_value( dc, &ad, &atmp,
532 &f->f_av_value, &vtmp, RWM_MAP ) )
537 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
538 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
540 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
541 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
543 ch_free( vtmp.bv_val );
548 if ( map_attr_value( dc, &ad, &atmp,
549 &f->f_av_value, &vtmp, RWM_MAP ) )
554 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
555 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
557 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
558 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
560 ch_free( vtmp.bv_val );
563 case LDAP_FILTER_APPROX:
565 if ( map_attr_value( dc, &ad, &atmp,
566 &f->f_av_value, &vtmp, RWM_MAP ) )
571 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
572 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
574 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
575 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
577 ch_free( vtmp.bv_val );
580 case LDAP_FILTER_SUBSTRINGS:
582 if ( map_attr_value( dc, &ad, &atmp,
583 NULL, NULL, RWM_MAP ) )
588 /* cannot be a DN ... */
590 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
591 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
593 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
596 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
599 filter_escape_value( &f->f_sub_initial, &vtmp );
601 fstr->bv_len += vtmp.bv_len;
602 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
604 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
605 /* "(attr=" */ "%s*)",
606 vtmp.bv_len ? vtmp.bv_val : "" );
608 ch_free( vtmp.bv_val );
611 if ( f->f_sub_any != NULL ) {
612 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
614 filter_escape_value( &f->f_sub_any[i], &vtmp );
616 fstr->bv_len += vtmp.bv_len + 1;
617 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
619 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
620 /* "(attr=[init]*[any*]" */ "%s*)",
621 vtmp.bv_len ? vtmp.bv_val : "" );
622 ch_free( vtmp.bv_val );
626 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
629 filter_escape_value( &f->f_sub_final, &vtmp );
631 fstr->bv_len += vtmp.bv_len;
632 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
634 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
635 /* "(attr=[init*][any*]" */ "%s)",
636 vtmp.bv_len ? vtmp.bv_val : "" );
638 ch_free( vtmp.bv_val );
643 case LDAP_FILTER_PRESENT:
645 if ( map_attr_value( dc, &ad, &atmp,
646 NULL, NULL, RWM_MAP ) )
651 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
652 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
654 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
658 case LDAP_FILTER_AND:
660 case LDAP_FILTER_NOT:
661 fstr->bv_len = STRLENOF( "(%)" );
662 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
664 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
665 f->f_choice == LDAP_FILTER_AND ? '&' :
666 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
668 for ( p = f->f_list; p != NULL; p = p->f_next ) {
673 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
674 if ( rc != LDAP_SUCCESS ) {
678 fstr->bv_len += vtmp.bv_len;
679 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
681 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
682 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
684 ch_free( vtmp.bv_val );
689 case LDAP_FILTER_EXT: {
690 if ( f->f_mr_desc ) {
692 if ( map_attr_value( dc, &ad, &atmp,
693 &f->f_mr_value, &vtmp, RWM_MAP ) )
699 BER_BVSTR( &atmp, "" );
700 filter_escape_value( &f->f_mr_value, &vtmp );
704 fstr->bv_len = atmp.bv_len +
705 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
706 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
707 vtmp.bv_len + STRLENOF( "(:=)" );
708 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
710 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
712 f->f_mr_dnattrs ? ":dn" : "",
713 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
714 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
715 vtmp.bv_len ? vtmp.bv_val : "" );
716 ch_free( vtmp.bv_val );
722 filter_free_x( op, f, 0 );
723 f->f_choice = SLAPD_FILTER_COMPUTED;
724 f->f_result = SLAPD_COMPARE_UNDEFINED;
727 case SLAPD_FILTER_COMPUTED:
728 switch ( f->f_result ) {
729 case LDAP_COMPARE_FALSE:
730 /* FIXME: treat UNDEFINED as FALSE */
731 case SLAPD_COMPARE_UNDEFINED:
732 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
733 tmp = &ber_bvtf_false;
739 case LDAP_COMPARE_TRUE:
740 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
741 tmp = &ber_bvtf_true;
752 ber_dupbv( fstr, tmp );
756 ber_dupbv( fstr, &ber_bvunknown );
764 rwm_filter_map_rewrite(
768 struct berval *fstr )
774 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
783 fdc.ctx = "searchFilter";
785 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
786 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
787 fdc.conn, &fstr->bv_val ) )
789 case REWRITE_REGEXEC_OK:
790 if ( !BER_BVISNULL( fstr ) ) {
791 fstr->bv_len = strlen( fstr->bv_val );
792 if ( fstr->bv_val != ftmp.bv_val ) {
793 ch_free( ftmp.bv_val );
800 Debug( LDAP_DEBUG_ARGS,
801 "[rw] %s: \"%s\" -> \"%s\"\n",
802 fdc.ctx, ftmp.bv_val, fstr->bv_val );
806 case REWRITE_REGEXEC_UNWILLING:
808 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
809 fdc.rs->sr_text = "Operation not allowed";
811 rc = LDAP_UNWILLING_TO_PERFORM;
814 case REWRITE_REGEXEC_ERR:
816 fdc.rs->sr_err = LDAP_OTHER;
817 fdc.rs->sr_text = "Rewrite error";
827 * I don't like this much, but we need two different
828 * functions because different heap managers may be
829 * in use in back-ldap/meta to reduce the amount of
830 * calls to malloc routines, and some of the free()
831 * routines may be macros with args
834 rwm_referral_rewrite(
839 BerVarray *pa_nvals )
841 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
842 struct ldaprwmap *rwmap =
843 (struct ldaprwmap *)on->on_bi.bi_private;
848 struct berval dn = BER_BVNULL,
851 assert( a_vals != NULL );
854 * Rewrite the dn if needed
857 dc.conn = op->o_conn;
859 dc.ctx = (char *)cookie;
861 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
865 if ( pa_nvals != NULL ) {
866 if ( *pa_nvals == NULL ) {
867 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
868 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
872 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
873 struct berval olddn = BER_BVNULL,
879 rc = ldap_url_parse( oldval.bv_val, &ludp );
880 if ( rc != LDAP_URL_SUCCESS ) {
881 /* leave attr untouched if massage failed */
882 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
883 ber_dupbv( &(*pa_nvals)[i], &oldval );
888 /* FIXME: URLs like "ldap:///dc=suffix" if passed
889 * thru ldap_url_parse() and ldap_url_desc2str()
890 * get rewritten as "ldap:///dc=suffix??base";
891 * we don't want this to occur... */
892 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
893 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
896 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
901 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
904 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
908 case LDAP_UNWILLING_TO_PERFORM:
910 * FIXME: need to check if it may be considered
911 * legal to trim values when adding/modifying;
912 * it should be when searching (e.g. ACLs).
914 ch_free( a_vals[i].bv_val );
916 a_vals[i] = a_vals[last];
918 (*pa_nvals)[i] = (*pa_nvals)[last];
921 BER_BVZERO( &a_vals[last] );
923 BER_BVZERO( &(*pa_nvals)[last] );
929 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
932 ludp->lud_dn = dn.bv_val;
933 newurl = ldap_url_desc2str( ludp );
934 ludp->lud_dn = olddn.bv_val;
935 ch_free( dn.bv_val );
936 if ( newurl == NULL ) {
937 /* FIXME: leave attr untouched
938 * even if ldap_url_desc2str failed...
943 ber_str2bv( newurl, 0, 1, &a_vals[i] );
947 ludp->lud_dn = ndn.bv_val;
948 newurl = ldap_url_desc2str( ludp );
949 ludp->lud_dn = olddn.bv_val;
950 ch_free( ndn.bv_val );
951 if ( newurl == NULL ) {
952 /* FIXME: leave attr untouched
953 * even if ldap_url_desc2str failed...
955 ch_free( a_vals[i].bv_val );
960 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
961 ch_free( (*pa_nvals)[i].bv_val );
963 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
967 ch_free( oldval.bv_val );
968 ludp->lud_dn = olddn.bv_val;
973 /* leave attr untouched if massage failed */
974 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
975 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
979 ldap_free_urldesc( ludp );
986 * I don't like this much, but we need two different
987 * functions because different heap managers may be
988 * in use in back-ldap/meta to reduce the amount of
989 * calls to malloc routines, and some of the free()
990 * routines may be macros with args
998 BerVarray *pa_nvals )
1000 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1001 struct ldaprwmap *rwmap =
1002 (struct ldaprwmap *)on->on_bi.bi_private;
1007 struct berval dn = BER_BVNULL,
1015 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1022 * Rewrite the dn if needed
1025 dc.conn = op->o_conn;
1027 dc.ctx = (char *)cookie;
1029 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1031 if ( pa_nvals != NULL ) {
1032 if ( *pa_nvals == NULL ) {
1033 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1034 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1038 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1044 ndn = (*pa_nvals)[i];
1045 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1047 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1051 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1055 case LDAP_UNWILLING_TO_PERFORM:
1057 * FIXME: need to check if it may be considered
1058 * legal to trim values when adding/modifying;
1059 * it should be when searching (e.g. ACLs).
1061 ch_free( in[i].bv_val );
1064 if ( a_vals && pa_nvals ) {
1065 (*pa_nvals)[i] = (*pa_nvals)[last];
1068 BER_BVZERO( &in[last] );
1069 if ( a_vals && pa_nvals ) {
1070 BER_BVZERO( &(*pa_nvals)[last] );
1077 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1078 ch_free( a_vals[i].bv_val );
1082 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1083 ch_free( (*pa_nvals)[i].bv_val );
1085 (*pa_nvals)[i] = ndn;
1090 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1091 ch_free( (*pa_nvals)[i].bv_val );
1092 (*pa_nvals)[i] = ndn;
1098 /* leave attr untouched if massage failed */
1099 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1100 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1110 rwm_referral_result_rewrite(
1116 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1119 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1125 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1126 if ( rc != LDAP_URL_SUCCESS ) {
1127 /* leave attr untouched if massage failed */
1131 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1132 * thru ldap_url_parse() and ldap_url_desc2str()
1133 * get rewritten as "ldap:///dc=suffix??base";
1134 * we don't want this to occur... */
1135 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1136 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1139 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1142 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1144 case LDAP_UNWILLING_TO_PERFORM:
1146 * FIXME: need to check if it may be considered
1147 * legal to trim values when adding/modifying;
1148 * it should be when searching (e.g. ACLs).
1150 ch_free( a_vals[i].bv_val );
1152 a_vals[i] = a_vals[last];
1154 BER_BVZERO( &a_vals[last] );
1160 /* leave attr untouched if massage failed */
1161 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1164 ludp->lud_dn = dn.bv_val;
1165 newurl = ldap_url_desc2str( ludp );
1166 if ( newurl == NULL ) {
1167 /* FIXME: leave attr untouched
1168 * even if ldap_url_desc2str failed...
1173 ch_free( a_vals[i].bv_val );
1174 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1175 LDAP_FREE( newurl );
1176 ludp->lud_dn = olddn.bv_val;
1181 ldap_free_urldesc( ludp );
1188 rwm_dnattr_result_rewrite(
1195 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1198 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1199 struct berval pdn, ndn = BER_BVNULL;
1203 rc = rwm_dn_massage_pretty_normalize( dc, &a_vals[i], &pdn, &ndn );
1205 case LDAP_UNWILLING_TO_PERFORM:
1207 * FIXME: need to check if it may be considered
1208 * legal to trim values when adding/modifying;
1209 * it should be when searching (e.g. ACLs).
1211 assert( a_vals[i].bv_val != a_nvals[i].bv_val );
1212 ch_free( a_vals[i].bv_val );
1213 ch_free( a_nvals[i].bv_val );
1215 a_vals[i] = a_vals[last];
1216 a_nvals[i] = a_nvals[last];
1218 BER_BVZERO( &a_vals[last] );
1219 BER_BVZERO( &a_nvals[last] );
1224 /* leave attr untouched if massage failed */
1225 if ( !BER_BVISNULL( &pdn ) && a_vals[i].bv_val != pdn.bv_val ) {
1226 ch_free( a_vals[i].bv_val );
1229 if ( !BER_BVISNULL( &ndn ) && a_nvals[i].bv_val != ndn.bv_val ) {
1230 ch_free( a_nvals[i].bv_val );
1241 rwm_mapping_dst_free( void *v_mapping )
1243 struct ldapmapping *mapping = v_mapping;
1245 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1246 rwm_mapping_free( &mapping[ -1 ] );
1251 rwm_mapping_free( void *v_mapping )
1253 struct ldapmapping *mapping = v_mapping;
1255 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1256 ch_free( mapping[0].m_src.bv_val );
1259 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1260 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1261 if ( mapping[0].m_src_oc ) {
1262 ch_free( mapping[0].m_src_oc );
1266 if ( mapping[0].m_src_ad ) {
1267 ch_free( mapping[0].m_src_ad );
1272 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1273 ch_free( mapping[0].m_dst.bv_val );
1276 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1277 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1278 if ( mapping[0].m_dst_oc ) {
1279 ch_free( mapping[0].m_dst_oc );
1283 if ( mapping[0].m_dst_ad ) {
1284 ch_free( mapping[0].m_dst_ad );
1293 #endif /* SLAPD_OVER_RWM */