1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2008 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 if ( remap == RWM_REMAP ) {
132 *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping,
136 return map->drop_missing;
143 rwm_map( struct ldapmap *map, struct berval *s, struct berval *bv, int remap )
145 struct ldapmapping *mapping;
147 /* map->map may be NULL when mapping is configured,
148 * but map->remap can't */
149 if ( map->remap == NULL ) {
155 ( void )rwm_mapping( map, s, &mapping, remap );
156 if ( mapping != NULL ) {
157 if ( !BER_BVISNULL( &mapping->m_dst ) ) {
158 *bv = mapping->m_dst;
163 if ( !map->drop_missing ) {
169 * Map attribute names in place
173 struct ldapmap *at_map,
174 struct ldapmap *oc_map,
181 assert( anp != NULL );
189 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
191 *anp = ch_malloc( ( i + 1 )* sizeof( AttributeName ) );
192 if ( *anp == NULL ) {
193 return LDAP_NO_MEMORY;
196 for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
197 struct ldapmapping *m;
198 int at_drop_missing = 0,
201 if ( an[i].an_desc ) {
203 /* FIXME: better leave as is? */
207 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
208 if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
219 if ( remap == RWM_MAP ) {
220 (*anp)[j].an_name = m->m_dst;
221 (*anp)[j].an_desc = m->m_dst_ad;
223 (*anp)[j].an_name = m->m_src;
224 (*anp)[j].an_desc = m->m_src_ad;
231 } else if ( an[i].an_oc ) {
233 /* FIXME: better leave as is? */
237 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
239 if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
250 if ( remap == RWM_MAP ) {
251 (*anp)[j].an_name = m->m_dst;
252 (*anp)[j].an_oc = m->m_dst_oc;
254 (*anp)[j].an_name = m->m_src;
255 (*anp)[j].an_oc = m->m_src_oc;
259 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
261 if ( at_drop_missing || !m ) {
262 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
264 /* if both at_map and oc_map required to drop missing,
266 if ( oc_drop_missing && at_drop_missing ) {
270 /* if no oc_map mapping was found and at_map required
271 * to drop missing, then do it; otherwise, at_map wins
272 * and an is considered an attr and is left unchanged */
274 if ( at_drop_missing ) {
282 if ( BER_BVISNULL( &m->m_dst ) ) {
287 if ( remap == RWM_MAP ) {
288 (*anp)[j].an_name = m->m_dst;
289 (*anp)[j].an_oc = m->m_dst_oc;
291 (*anp)[j].an_name = m->m_src;
292 (*anp)[j].an_oc = m->m_src_oc;
298 if ( !BER_BVISNULL( &m->m_dst ) ) {
300 if ( remap == RWM_MAP ) {
301 (*anp)[j].an_name = m->m_dst;
302 (*anp)[j].an_desc = m->m_dst_ad;
304 (*anp)[j].an_name = m->m_src;
305 (*anp)[j].an_desc = m->m_src_ad;
313 if ( j == 0 && i != 0 ) {
314 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
315 BER_BVSTR( &(*anp)[0].an_name, LDAP_NO_ATTRS );
318 memset( &(*anp)[j], 0, sizeof( AttributeName ) );
325 struct ldapmap *at_map,
328 char ***mapped_attrs )
334 *mapped_attrs = NULL;
338 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ); i++ )
341 na = (char **)ch_calloc( i + 1, sizeof( char * ) );
343 *mapped_attrs = NULL;
344 return LDAP_NO_MEMORY;
347 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
348 struct ldapmapping *mapping;
350 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
355 na[ j++ ] = an[ i ].an_name.bv_val;
357 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
358 na[ j++ ] = mapping->m_dst.bv_val;
362 if ( j == 0 && i != 0 ) {
363 na[ j++ ] = LDAP_NO_ATTRS;
376 AttributeDescription **adp,
377 struct berval *mapped_attr,
378 struct berval *value,
379 struct berval *mapped_value,
382 struct berval vtmp = BER_BVNULL;
384 AttributeDescription *ad = *adp;
385 struct ldapmapping *mapping = NULL;
387 rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
388 if ( mapping == NULL ) {
389 if ( dc->rwmap->rwm_at.drop_missing ) {
393 *mapped_attr = ad->ad_cname;
396 *mapped_attr = mapping->m_dst;
399 if ( value != NULL ) {
400 assert( mapped_value != NULL );
402 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
403 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
408 fdc.ctx = "searchFilterAttrDN";
411 rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
414 if ( vtmp.bv_val != value->bv_val ) {
419 case LDAP_UNWILLING_TO_PERFORM:
425 } else if ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) {
426 if ( ad->ad_type->sat_equality->smr_normalize(
427 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
428 NULL, NULL, value, &vtmp, NULL ) )
434 } else if ( ad == slap_schema.si_ad_objectClass
435 || ad == slap_schema.si_ad_structuralObjectClass )
437 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
438 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
446 filter_escape_value( &vtmp, mapped_value );
449 ch_free( vtmp.bv_val );
453 if ( mapping != NULL ) {
454 assert( mapping->m_dst_ad != NULL );
455 *adp = mapping->m_dst_ad;
462 rwm_int_filter_map_rewrite(
466 struct berval *fstr )
470 AttributeDescription *ad;
475 /* better than nothing... */
476 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
477 ber_bvtf_false = BER_BVC( "(|)" ),
478 /* better than nothing... */
479 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
480 ber_bvtf_true = BER_BVC( "(&)" ),
482 /* no longer needed; preserved for completeness */
483 ber_bvundefined = BER_BVC( "(?=undefined)" ),
485 ber_bverror = BER_BVC( "(?=error)" ),
486 ber_bvunknown = BER_BVC( "(?=unknown)" ),
487 ber_bvnone = BER_BVC( "(?=none)" );
490 assert( fstr != NULL );
494 ber_dupbv( fstr, &ber_bvnone );
498 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
499 case LDAP_FILTER_EQUALITY:
501 if ( map_attr_value( dc, &ad, &atmp,
502 &f->f_av_value, &vtmp, RWM_MAP ) )
507 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
508 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
510 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
511 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
513 ch_free( vtmp.bv_val );
518 if ( map_attr_value( dc, &ad, &atmp,
519 &f->f_av_value, &vtmp, RWM_MAP ) )
524 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
525 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
527 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
528 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
530 ch_free( vtmp.bv_val );
535 if ( map_attr_value( dc, &ad, &atmp,
536 &f->f_av_value, &vtmp, RWM_MAP ) )
541 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
542 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
544 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
545 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
547 ch_free( vtmp.bv_val );
550 case LDAP_FILTER_APPROX:
552 if ( map_attr_value( dc, &ad, &atmp,
553 &f->f_av_value, &vtmp, RWM_MAP ) )
558 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
559 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
561 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
562 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
564 ch_free( vtmp.bv_val );
567 case LDAP_FILTER_SUBSTRINGS:
569 if ( map_attr_value( dc, &ad, &atmp,
570 NULL, NULL, RWM_MAP ) )
575 /* cannot be a DN ... */
577 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
578 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
580 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
583 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
586 filter_escape_value( &f->f_sub_initial, &vtmp );
588 fstr->bv_len += vtmp.bv_len;
589 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
591 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
592 /* "(attr=" */ "%s*)",
593 vtmp.bv_len ? vtmp.bv_val : "" );
595 ch_free( vtmp.bv_val );
598 if ( f->f_sub_any != NULL ) {
599 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
601 filter_escape_value( &f->f_sub_any[i], &vtmp );
603 fstr->bv_len += vtmp.bv_len + 1;
604 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
606 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
607 /* "(attr=[init]*[any*]" */ "%s*)",
608 vtmp.bv_len ? vtmp.bv_val : "" );
609 ch_free( vtmp.bv_val );
613 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
616 filter_escape_value( &f->f_sub_final, &vtmp );
618 fstr->bv_len += vtmp.bv_len;
619 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
621 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
622 /* "(attr=[init*][any*]" */ "%s)",
623 vtmp.bv_len ? vtmp.bv_val : "" );
625 ch_free( vtmp.bv_val );
630 case LDAP_FILTER_PRESENT:
632 if ( map_attr_value( dc, &ad, &atmp,
633 NULL, NULL, RWM_MAP ) )
638 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
639 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
641 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
645 case LDAP_FILTER_AND:
647 case LDAP_FILTER_NOT:
648 fstr->bv_len = STRLENOF( "(%)" );
649 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
651 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
652 f->f_choice == LDAP_FILTER_AND ? '&' :
653 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
655 for ( p = f->f_list; p != NULL; p = p->f_next ) {
660 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
661 if ( rc != LDAP_SUCCESS ) {
665 fstr->bv_len += vtmp.bv_len;
666 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
668 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
669 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
671 ch_free( vtmp.bv_val );
676 case LDAP_FILTER_EXT: {
677 if ( f->f_mr_desc ) {
679 if ( map_attr_value( dc, &ad, &atmp,
680 &f->f_mr_value, &vtmp, RWM_MAP ) )
686 BER_BVSTR( &atmp, "" );
687 filter_escape_value( &f->f_mr_value, &vtmp );
691 fstr->bv_len = atmp.bv_len +
692 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
693 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
694 vtmp.bv_len + STRLENOF( "(:=)" );
695 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
697 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
699 f->f_mr_dnattrs ? ":dn" : "",
700 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
701 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
702 vtmp.bv_len ? vtmp.bv_val : "" );
703 ch_free( vtmp.bv_val );
709 filter_free_x( op, f );
710 f->f_choice = SLAPD_FILTER_COMPUTED;
711 f->f_result = SLAPD_COMPARE_UNDEFINED;
714 case SLAPD_FILTER_COMPUTED:
715 switch ( f->f_result ) {
716 case LDAP_COMPARE_FALSE:
717 /* FIXME: treat UNDEFINED as FALSE */
718 case SLAPD_COMPARE_UNDEFINED:
719 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
720 tmp = &ber_bvtf_false;
726 case LDAP_COMPARE_TRUE:
727 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
728 tmp = &ber_bvtf_true;
739 ber_dupbv( fstr, tmp );
743 ber_dupbv( fstr, &ber_bvunknown );
751 rwm_filter_map_rewrite(
755 struct berval *fstr )
761 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
770 fdc.ctx = "searchFilter";
772 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
773 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
774 fdc.conn, &fstr->bv_val ) )
776 case REWRITE_REGEXEC_OK:
777 if ( !BER_BVISNULL( fstr ) ) {
778 fstr->bv_len = strlen( fstr->bv_val );
779 if ( fstr->bv_val != ftmp.bv_val ) {
780 ch_free( ftmp.bv_val );
787 Debug( LDAP_DEBUG_ARGS,
788 "[rw] %s: \"%s\" -> \"%s\"\n",
789 fdc.ctx, ftmp.bv_val, fstr->bv_val );
793 case REWRITE_REGEXEC_UNWILLING:
795 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
796 fdc.rs->sr_text = "Operation not allowed";
798 rc = LDAP_UNWILLING_TO_PERFORM;
801 case REWRITE_REGEXEC_ERR:
803 fdc.rs->sr_err = LDAP_OTHER;
804 fdc.rs->sr_text = "Rewrite error";
814 * I don't like this much, but we need two different
815 * functions because different heap managers may be
816 * in use in back-ldap/meta to reduce the amount of
817 * calls to malloc routines, and some of the free()
818 * routines may be macros with args
821 rwm_referral_rewrite(
826 BerVarray *pa_nvals )
828 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
829 struct ldaprwmap *rwmap =
830 (struct ldaprwmap *)on->on_bi.bi_private;
835 struct berval dn = BER_BVNULL,
838 assert( a_vals != NULL );
841 * Rewrite the dn if needed
844 dc.conn = op->o_conn;
846 dc.ctx = (char *)cookie;
848 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
852 if ( pa_nvals != NULL ) {
853 if ( *pa_nvals == NULL ) {
854 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
855 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
859 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
860 struct berval olddn = BER_BVNULL,
866 rc = ldap_url_parse( oldval.bv_val, &ludp );
867 if ( rc != LDAP_URL_SUCCESS ) {
868 /* leave attr untouched if massage failed */
869 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
870 ber_dupbv( &(*pa_nvals)[i], &oldval );
875 /* FIXME: URLs like "ldap:///dc=suffix" if passed
876 * thru ldap_url_parse() and ldap_url_desc2str()
877 * get rewritten as "ldap:///dc=suffix??base";
878 * we don't want this to occur... */
879 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
880 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
883 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
888 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
891 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
895 case LDAP_UNWILLING_TO_PERFORM:
897 * FIXME: need to check if it may be considered
898 * legal to trim values when adding/modifying;
899 * it should be when searching (e.g. ACLs).
901 ch_free( a_vals[i].bv_val );
903 a_vals[i] = a_vals[last];
905 (*pa_nvals)[i] = (*pa_nvals)[last];
908 BER_BVZERO( &a_vals[last] );
910 BER_BVZERO( &(*pa_nvals)[last] );
916 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
919 ludp->lud_dn = dn.bv_val;
920 newurl = ldap_url_desc2str( ludp );
921 ludp->lud_dn = olddn.bv_val;
922 ch_free( dn.bv_val );
923 if ( newurl == NULL ) {
924 /* FIXME: leave attr untouched
925 * even if ldap_url_desc2str failed...
930 ber_str2bv( newurl, 0, 1, &a_vals[i] );
934 ludp->lud_dn = ndn.bv_val;
935 newurl = ldap_url_desc2str( ludp );
936 ludp->lud_dn = olddn.bv_val;
937 ch_free( ndn.bv_val );
938 if ( newurl == NULL ) {
939 /* FIXME: leave attr untouched
940 * even if ldap_url_desc2str failed...
942 ch_free( a_vals[i].bv_val );
947 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
948 ch_free( (*pa_nvals)[i].bv_val );
950 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
954 ch_free( oldval.bv_val );
955 ludp->lud_dn = olddn.bv_val;
960 /* leave attr untouched if massage failed */
961 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
962 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
966 ldap_free_urldesc( ludp );
973 * I don't like this much, but we need two different
974 * functions because different heap managers may be
975 * in use in back-ldap/meta to reduce the amount of
976 * calls to malloc routines, and some of the free()
977 * routines may be macros with args
985 BerVarray *pa_nvals )
987 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
988 struct ldaprwmap *rwmap =
989 (struct ldaprwmap *)on->on_bi.bi_private;
994 struct berval dn = BER_BVNULL,
1002 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1009 * Rewrite the dn if needed
1012 dc.conn = op->o_conn;
1014 dc.ctx = (char *)cookie;
1016 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1018 if ( pa_nvals != NULL ) {
1019 if ( *pa_nvals == NULL ) {
1020 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1021 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1025 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1031 ndn = (*pa_nvals)[i];
1032 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1034 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1038 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1042 case LDAP_UNWILLING_TO_PERFORM:
1044 * FIXME: need to check if it may be considered
1045 * legal to trim values when adding/modifying;
1046 * it should be when searching (e.g. ACLs).
1048 ch_free( in[i].bv_val );
1051 if ( a_vals && pa_nvals ) {
1052 (*pa_nvals)[i] = (*pa_nvals)[last];
1055 BER_BVZERO( &in[last] );
1056 if ( a_vals && pa_nvals ) {
1057 BER_BVZERO( &(*pa_nvals)[last] );
1064 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1065 ch_free( a_vals[i].bv_val );
1069 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1070 ch_free( (*pa_nvals)[i].bv_val );
1072 (*pa_nvals)[i] = ndn;
1077 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1078 ch_free( (*pa_nvals)[i].bv_val );
1079 (*pa_nvals)[i] = ndn;
1085 /* leave attr untouched if massage failed */
1086 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1087 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1097 rwm_referral_result_rewrite(
1103 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1106 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1112 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1113 if ( rc != LDAP_URL_SUCCESS ) {
1114 /* leave attr untouched if massage failed */
1118 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1119 * thru ldap_url_parse() and ldap_url_desc2str()
1120 * get rewritten as "ldap:///dc=suffix??base";
1121 * we don't want this to occur... */
1122 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1123 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1126 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1129 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1131 case LDAP_UNWILLING_TO_PERFORM:
1133 * FIXME: need to check if it may be considered
1134 * legal to trim values when adding/modifying;
1135 * it should be when searching (e.g. ACLs).
1137 ch_free( a_vals[i].bv_val );
1139 a_vals[i] = a_vals[last];
1141 BER_BVZERO( &a_vals[last] );
1147 /* leave attr untouched if massage failed */
1148 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1151 ludp->lud_dn = dn.bv_val;
1152 newurl = ldap_url_desc2str( ludp );
1153 if ( newurl == NULL ) {
1154 /* FIXME: leave attr untouched
1155 * even if ldap_url_desc2str failed...
1160 ch_free( a_vals[i].bv_val );
1161 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1162 LDAP_FREE( newurl );
1163 ludp->lud_dn = olddn.bv_val;
1168 ldap_free_urldesc( ludp );
1175 rwm_dnattr_result_rewrite(
1181 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1184 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1189 rc = rwm_dn_massage_pretty( dc, &a_vals[i], &dn );
1191 case LDAP_UNWILLING_TO_PERFORM:
1193 * FIXME: need to check if it may be considered
1194 * legal to trim values when adding/modifying;
1195 * it should be when searching (e.g. ACLs).
1197 ch_free( a_vals[i].bv_val );
1199 a_vals[i] = a_vals[last];
1201 BER_BVZERO( &a_vals[last] );
1206 /* leave attr untouched if massage failed */
1207 if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1208 ch_free( a_vals[i].bv_val );
1219 rwm_mapping_dst_free( void *v_mapping )
1221 struct ldapmapping *mapping = v_mapping;
1223 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1224 rwm_mapping_free( &mapping[ -1 ] );
1229 rwm_mapping_free( void *v_mapping )
1231 struct ldapmapping *mapping = v_mapping;
1233 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1234 ch_free( mapping[0].m_src.bv_val );
1237 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1238 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1239 if ( mapping[0].m_src_oc ) {
1240 ch_free( mapping[0].m_src_oc );
1244 if ( mapping[0].m_src_ad ) {
1245 ch_free( mapping[0].m_src_ad );
1250 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1251 ch_free( mapping[0].m_dst.bv_val );
1254 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1255 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1256 if ( mapping[0].m_dst_oc ) {
1257 ch_free( mapping[0].m_dst_oc );
1261 if ( mapping[0].m_dst_ad ) {
1262 ch_free( mapping[0].m_dst_ad );
1271 #endif /* SLAPD_OVER_RWM */