1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2007 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 /* FIXME: I don't think this is needed any more... */
85 rc = slap_str2ad( "objectClass", &mapping[0].m_src_ad, &text );
86 if ( rc != LDAP_SUCCESS ) {
91 mapping[0].m_dst_ad = mapping[0].m_src_ad;
92 ber_dupbv( &mapping[0].m_src, &mapping[0].m_src_ad->ad_cname );
93 ber_dupbv( &mapping[0].m_dst, &mapping[0].m_src );
95 mapping[1].m_src = mapping[0].m_src;
96 mapping[1].m_dst = mapping[0].m_dst;
97 mapping[1].m_src_ad = mapping[0].m_src_ad;
98 mapping[1].m_dst_ad = mapping[1].m_src_ad;
100 avl_insert( &lm->map, (caddr_t)&mapping[0],
101 rwm_mapping_cmp, rwm_mapping_dup );
102 avl_insert( &lm->remap, (caddr_t)&mapping[1],
103 rwm_mapping_cmp, rwm_mapping_dup );
111 rwm_mapping( struct ldapmap *map, struct berval *s, struct ldapmapping **m, int remap )
114 struct ldapmapping fmapping;
122 if ( remap == RWM_REMAP ) {
130 *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping,
134 return map->drop_missing;
141 rwm_map( struct ldapmap *map, struct berval *s, struct berval *bv, int remap )
143 struct ldapmapping *mapping;
145 /* map->map may be NULL when mapping is configured,
146 * but map->remap can't */
147 if ( map->remap == NULL ) {
153 ( void )rwm_mapping( map, s, &mapping, remap );
154 if ( mapping != NULL ) {
155 if ( !BER_BVISNULL( &mapping->m_dst ) ) {
156 *bv = mapping->m_dst;
161 if ( !map->drop_missing ) {
167 * Map attribute names in place
171 struct ldapmap *at_map,
172 struct ldapmap *oc_map,
179 assert( anp != NULL );
187 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
189 *anp = ch_malloc( ( i + 1 )* sizeof( AttributeName ) );
190 if ( *anp == NULL ) {
191 return LDAP_NO_MEMORY;
194 for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
195 struct ldapmapping *m;
196 int at_drop_missing = 0,
199 if ( an[i].an_desc ) {
201 /* FIXME: better leave as is? */
205 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
206 if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
217 if ( remap == RWM_MAP ) {
218 (*anp)[j].an_name = m->m_dst;
219 (*anp)[j].an_desc = m->m_dst_ad;
221 (*anp)[j].an_name = m->m_src;
222 (*anp)[j].an_desc = m->m_src_ad;
229 } else if ( an[i].an_oc ) {
231 /* FIXME: better leave as is? */
235 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
237 if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
248 if ( remap == RWM_MAP ) {
249 (*anp)[j].an_name = m->m_dst;
250 (*anp)[j].an_oc = m->m_dst_oc;
252 (*anp)[j].an_name = m->m_src;
253 (*anp)[j].an_oc = m->m_src_oc;
257 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
259 if ( at_drop_missing || !m ) {
260 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
262 /* if both at_map and oc_map required to drop missing,
264 if ( oc_drop_missing && at_drop_missing ) {
268 /* if no oc_map mapping was found and at_map required
269 * to drop missing, then do it; otherwise, at_map wins
270 * and an is considered an attr and is left unchanged */
272 if ( at_drop_missing ) {
280 if ( BER_BVISNULL( &m->m_dst ) ) {
285 if ( remap == RWM_MAP ) {
286 (*anp)[j].an_name = m->m_dst;
287 (*anp)[j].an_oc = m->m_dst_oc;
289 (*anp)[j].an_name = m->m_src;
290 (*anp)[j].an_oc = m->m_src_oc;
296 if ( !BER_BVISNULL( &m->m_dst ) ) {
298 if ( remap == RWM_MAP ) {
299 (*anp)[j].an_name = m->m_dst;
300 (*anp)[j].an_desc = m->m_dst_ad;
302 (*anp)[j].an_name = m->m_src;
303 (*anp)[j].an_desc = m->m_src_ad;
311 if ( j == 0 && i != 0 ) {
312 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
313 BER_BVSTR( &(*anp)[0].an_name, LDAP_NO_ATTRS );
316 memset( &(*anp)[j], 0, sizeof( AttributeName ) );
323 struct ldapmap *at_map,
326 char ***mapped_attrs )
332 *mapped_attrs = NULL;
336 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ); i++ )
339 na = (char **)ch_calloc( i + 1, sizeof( char * ) );
341 *mapped_attrs = NULL;
342 return LDAP_NO_MEMORY;
345 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
346 struct ldapmapping *mapping;
348 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
353 na[ j++ ] = an[ i ].an_name.bv_val;
355 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
356 na[ j++ ] = mapping->m_dst.bv_val;
360 if ( j == 0 && i != 0 ) {
361 na[ j++ ] = LDAP_NO_ATTRS;
374 AttributeDescription **adp,
375 struct berval *mapped_attr,
376 struct berval *value,
377 struct berval *mapped_value,
380 struct berval vtmp = BER_BVNULL;
382 AttributeDescription *ad = *adp;
383 struct ldapmapping *mapping = NULL;
385 rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
386 if ( mapping == NULL ) {
387 if ( dc->rwmap->rwm_at.drop_missing ) {
391 *mapped_attr = ad->ad_cname;
394 *mapped_attr = mapping->m_dst;
397 if ( value != NULL ) {
398 assert( mapped_value != NULL );
400 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
401 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
406 #ifdef ENABLE_REWRITE
407 fdc.ctx = "searchFilterAttrDN";
408 #endif /* ENABLE_REWRITE */
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 == slap_schema.si_ad_objectClass
426 || ad == slap_schema.si_ad_structuralObjectClass )
428 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
429 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
437 filter_escape_value( &vtmp, mapped_value );
440 ch_free( vtmp.bv_val );
444 if ( mapping != NULL ) {
445 assert( mapping->m_dst_ad != NULL );
446 *adp = mapping->m_dst_ad;
453 rwm_int_filter_map_rewrite(
457 struct berval *fstr )
461 AttributeDescription *ad;
466 /* better than nothing... */
467 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
468 ber_bvtf_false = BER_BVC( "(|)" ),
469 /* better than nothing... */
470 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
471 ber_bvtf_true = BER_BVC( "(&)" ),
473 /* no longer needed; preserved for completeness */
474 ber_bvundefined = BER_BVC( "(?=undefined)" ),
476 ber_bverror = BER_BVC( "(?=error)" ),
477 ber_bvunknown = BER_BVC( "(?=unknown)" ),
478 ber_bvnone = BER_BVC( "(?=none)" );
481 assert( fstr != NULL );
485 ber_dupbv( fstr, &ber_bvnone );
489 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
490 case LDAP_FILTER_EQUALITY:
492 if ( map_attr_value( dc, &ad, &atmp,
493 &f->f_av_value, &vtmp, RWM_MAP ) )
498 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
499 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
501 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
502 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
504 ch_free( vtmp.bv_val );
509 if ( map_attr_value( dc, &ad, &atmp,
510 &f->f_av_value, &vtmp, RWM_MAP ) )
515 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
516 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
518 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
519 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
521 ch_free( vtmp.bv_val );
526 if ( map_attr_value( dc, &ad, &atmp,
527 &f->f_av_value, &vtmp, RWM_MAP ) )
532 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
533 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
535 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
536 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
538 ch_free( vtmp.bv_val );
541 case LDAP_FILTER_APPROX:
543 if ( map_attr_value( dc, &ad, &atmp,
544 &f->f_av_value, &vtmp, RWM_MAP ) )
549 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
550 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
552 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
553 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
555 ch_free( vtmp.bv_val );
558 case LDAP_FILTER_SUBSTRINGS:
560 if ( map_attr_value( dc, &ad, &atmp,
561 NULL, NULL, RWM_MAP ) )
566 /* cannot be a DN ... */
568 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
569 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
571 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
574 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
577 filter_escape_value( &f->f_sub_initial, &vtmp );
579 fstr->bv_len += vtmp.bv_len;
580 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
582 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
583 /* "(attr=" */ "%s*)",
584 vtmp.bv_len ? vtmp.bv_val : "" );
586 ch_free( vtmp.bv_val );
589 if ( f->f_sub_any != NULL ) {
590 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
592 filter_escape_value( &f->f_sub_any[i], &vtmp );
594 fstr->bv_len += vtmp.bv_len + 1;
595 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
597 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
598 /* "(attr=[init]*[any*]" */ "%s*)",
599 vtmp.bv_len ? vtmp.bv_val : "" );
600 ch_free( vtmp.bv_val );
604 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
607 filter_escape_value( &f->f_sub_final, &vtmp );
609 fstr->bv_len += vtmp.bv_len;
610 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
612 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
613 /* "(attr=[init*][any*]" */ "%s)",
614 vtmp.bv_len ? vtmp.bv_val : "" );
616 ch_free( vtmp.bv_val );
621 case LDAP_FILTER_PRESENT:
623 if ( map_attr_value( dc, &ad, &atmp,
624 NULL, NULL, RWM_MAP ) )
629 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
630 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
632 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
636 case LDAP_FILTER_AND:
638 case LDAP_FILTER_NOT:
639 fstr->bv_len = STRLENOF( "(%)" );
640 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
642 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
643 f->f_choice == LDAP_FILTER_AND ? '&' :
644 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
646 for ( p = f->f_list; p != NULL; p = p->f_next ) {
651 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
652 if ( rc != LDAP_SUCCESS ) {
656 fstr->bv_len += vtmp.bv_len;
657 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
659 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
660 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
662 ch_free( vtmp.bv_val );
667 case LDAP_FILTER_EXT: {
668 if ( f->f_mr_desc ) {
670 if ( map_attr_value( dc, &ad, &atmp,
671 &f->f_mr_value, &vtmp, RWM_MAP ) )
677 BER_BVSTR( &atmp, "" );
678 filter_escape_value( &f->f_mr_value, &vtmp );
682 fstr->bv_len = atmp.bv_len +
683 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
684 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
685 vtmp.bv_len + STRLENOF( "(:=)" );
686 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
688 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
690 f->f_mr_dnattrs ? ":dn" : "",
691 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
692 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
693 vtmp.bv_len ? vtmp.bv_val : "" );
694 ch_free( vtmp.bv_val );
700 filter_free_x( op, f );
701 f->f_choice = SLAPD_FILTER_COMPUTED;
702 f->f_result = SLAPD_COMPARE_UNDEFINED;
705 case SLAPD_FILTER_COMPUTED:
706 switch ( f->f_result ) {
707 case LDAP_COMPARE_FALSE:
708 /* FIXME: treat UNDEFINED as FALSE */
709 case SLAPD_COMPARE_UNDEFINED:
710 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
711 tmp = &ber_bvtf_false;
717 case LDAP_COMPARE_TRUE:
718 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
719 tmp = &ber_bvtf_true;
730 ber_dupbv( fstr, tmp );
734 ber_dupbv( fstr, &ber_bvunknown );
742 rwm_filter_map_rewrite(
746 struct berval *fstr )
752 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
754 #ifdef ENABLE_REWRITE
762 fdc.ctx = "searchFilter";
764 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
765 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
766 fdc.conn, &fstr->bv_val ) )
768 case REWRITE_REGEXEC_OK:
769 if ( !BER_BVISNULL( fstr ) ) {
770 fstr->bv_len = strlen( fstr->bv_val );
771 if ( fstr->bv_val != ftmp.bv_val ) {
772 ch_free( ftmp.bv_val );
779 Debug( LDAP_DEBUG_ARGS,
780 "[rw] %s: \"%s\" -> \"%s\"\n",
781 fdc.ctx, ftmp.bv_val, fstr->bv_val );
785 case REWRITE_REGEXEC_UNWILLING:
787 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
788 fdc.rs->sr_text = "Operation not allowed";
790 rc = LDAP_UNWILLING_TO_PERFORM;
793 case REWRITE_REGEXEC_ERR:
795 fdc.rs->sr_err = LDAP_OTHER;
796 fdc.rs->sr_text = "Rewrite error";
801 #endif /* ENABLE_REWRITE */
807 * I don't like this much, but we need two different
808 * functions because different heap managers may be
809 * in use in back-ldap/meta to reduce the amount of
810 * calls to malloc routines, and some of the free()
811 * routines may be macros with args
814 rwm_referral_rewrite(
819 BerVarray *pa_nvals )
821 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
822 struct ldaprwmap *rwmap =
823 (struct ldaprwmap *)on->on_bi.bi_private;
828 struct berval dn = BER_BVNULL,
831 assert( a_vals != NULL );
834 * Rewrite the dn if needed
837 #ifdef ENABLE_REWRITE
838 dc.conn = op->o_conn;
840 dc.ctx = (char *)cookie;
841 #else /* ! ENABLE_REWRITE */
842 dc.tofrom = ((int *)cookie)[0];
844 #endif /* ! ENABLE_REWRITE */
846 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
850 if ( pa_nvals != NULL ) {
851 if ( *pa_nvals == NULL ) {
852 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
853 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
857 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
858 struct berval olddn = BER_BVNULL,
864 rc = ldap_url_parse( oldval.bv_val, &ludp );
865 if ( rc != LDAP_URL_SUCCESS ) {
866 /* leave attr untouched if massage failed */
867 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
868 ber_dupbv( &(*pa_nvals)[i], &oldval );
873 /* FIXME: URLs like "ldap:///dc=suffix" if passed
874 * thru ldap_url_parse() and ldap_url_desc2str()
875 * get rewritten as "ldap:///dc=suffix??base";
876 * we don't want this to occur... */
877 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
878 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
881 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
886 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
889 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
893 case LDAP_UNWILLING_TO_PERFORM:
895 * FIXME: need to check if it may be considered
896 * legal to trim values when adding/modifying;
897 * it should be when searching (e.g. ACLs).
899 ch_free( a_vals[i].bv_val );
901 a_vals[i] = a_vals[last];
903 (*pa_nvals)[i] = (*pa_nvals)[last];
906 BER_BVZERO( &a_vals[last] );
908 BER_BVZERO( &(*pa_nvals)[last] );
914 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
917 ludp->lud_dn = dn.bv_val;
918 newurl = ldap_url_desc2str( ludp );
919 ludp->lud_dn = olddn.bv_val;
920 ch_free( dn.bv_val );
921 if ( newurl == NULL ) {
922 /* FIXME: leave attr untouched
923 * even if ldap_url_desc2str failed...
928 ber_str2bv( newurl, 0, 1, &a_vals[i] );
932 ludp->lud_dn = ndn.bv_val;
933 newurl = ldap_url_desc2str( ludp );
934 ludp->lud_dn = olddn.bv_val;
935 ch_free( ndn.bv_val );
936 if ( newurl == NULL ) {
937 /* FIXME: leave attr untouched
938 * even if ldap_url_desc2str failed...
940 ch_free( a_vals[i].bv_val );
945 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
946 ch_free( (*pa_nvals)[i].bv_val );
948 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
952 ch_free( oldval.bv_val );
953 ludp->lud_dn = olddn.bv_val;
958 /* leave attr untouched if massage failed */
959 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
960 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
964 ldap_free_urldesc( ludp );
971 * I don't like this much, but we need two different
972 * functions because different heap managers may be
973 * in use in back-ldap/meta to reduce the amount of
974 * calls to malloc routines, and some of the free()
975 * routines may be macros with args
983 BerVarray *pa_nvals )
985 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
986 struct ldaprwmap *rwmap =
987 (struct ldaprwmap *)on->on_bi.bi_private;
992 struct berval dn = BER_BVNULL,
1000 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1007 * Rewrite the dn if needed
1010 #ifdef ENABLE_REWRITE
1011 dc.conn = op->o_conn;
1013 dc.ctx = (char *)cookie;
1014 #else /* ! ENABLE_REWRITE */
1015 dc.tofrom = ((int *)cookie)[0];
1017 #endif /* ! ENABLE_REWRITE */
1019 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1021 if ( pa_nvals != NULL ) {
1022 if ( *pa_nvals == NULL ) {
1023 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1024 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1028 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1034 ndn = (*pa_nvals)[i];
1035 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1037 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1041 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1045 case LDAP_UNWILLING_TO_PERFORM:
1047 * FIXME: need to check if it may be considered
1048 * legal to trim values when adding/modifying;
1049 * it should be when searching (e.g. ACLs).
1051 ch_free( in[i].bv_val );
1054 if ( a_vals && pa_nvals ) {
1055 (*pa_nvals)[i] = (*pa_nvals)[last];
1058 BER_BVZERO( &in[last] );
1059 if ( a_vals && pa_nvals ) {
1060 BER_BVZERO( &(*pa_nvals)[last] );
1067 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1068 ch_free( a_vals[i].bv_val );
1072 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1073 ch_free( (*pa_nvals)[i].bv_val );
1075 (*pa_nvals)[i] = ndn;
1080 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1081 ch_free( (*pa_nvals)[i].bv_val );
1082 (*pa_nvals)[i] = ndn;
1088 /* leave attr untouched if massage failed */
1089 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1090 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1100 rwm_referral_result_rewrite(
1106 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1109 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1115 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1116 if ( rc != LDAP_URL_SUCCESS ) {
1117 /* leave attr untouched if massage failed */
1121 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1122 * thru ldap_url_parse() and ldap_url_desc2str()
1123 * get rewritten as "ldap:///dc=suffix??base";
1124 * we don't want this to occur... */
1125 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1126 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1129 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1132 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1134 case LDAP_UNWILLING_TO_PERFORM:
1136 * FIXME: need to check if it may be considered
1137 * legal to trim values when adding/modifying;
1138 * it should be when searching (e.g. ACLs).
1140 ch_free( a_vals[i].bv_val );
1142 a_vals[i] = a_vals[last];
1144 BER_BVZERO( &a_vals[last] );
1150 /* leave attr untouched if massage failed */
1151 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1154 ludp->lud_dn = dn.bv_val;
1155 newurl = ldap_url_desc2str( ludp );
1156 if ( newurl == NULL ) {
1157 /* FIXME: leave attr untouched
1158 * even if ldap_url_desc2str failed...
1163 ch_free( a_vals[i].bv_val );
1164 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1165 LDAP_FREE( newurl );
1166 ludp->lud_dn = olddn.bv_val;
1171 ldap_free_urldesc( ludp );
1178 rwm_dnattr_result_rewrite(
1184 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1187 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1192 rc = rwm_dn_massage_pretty( dc, &a_vals[i], &dn );
1194 case LDAP_UNWILLING_TO_PERFORM:
1196 * FIXME: need to check if it may be considered
1197 * legal to trim values when adding/modifying;
1198 * it should be when searching (e.g. ACLs).
1200 ch_free( a_vals[i].bv_val );
1202 a_vals[i] = a_vals[last];
1204 BER_BVZERO( &a_vals[last] );
1209 /* leave attr untouched if massage failed */
1210 if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1211 ch_free( a_vals[i].bv_val );
1222 rwm_mapping_dst_free( void *v_mapping )
1224 struct ldapmapping *mapping = v_mapping;
1226 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1227 rwm_mapping_free( &mapping[ -1 ] );
1232 rwm_mapping_free( void *v_mapping )
1234 struct ldapmapping *mapping = v_mapping;
1236 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1237 ch_free( mapping[0].m_src.bv_val );
1240 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1241 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1242 if ( mapping[0].m_src_oc ) {
1243 ch_free( mapping[0].m_src_oc );
1247 if ( mapping[0].m_src_ad ) {
1248 ch_free( mapping[0].m_src_ad );
1253 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1254 ch_free( mapping[0].m_dst.bv_val );
1257 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1258 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1259 if ( mapping[0].m_dst_oc ) {
1260 ch_free( mapping[0].m_dst_oc );
1264 if ( mapping[0].m_dst_ad ) {
1265 ch_free( mapping[0].m_dst_ad );
1274 #endif /* SLAPD_OVER_RWM */