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 if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
502 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
503 case LDAP_FILTER_EQUALITY:
505 if ( map_attr_value( dc, &ad, &atmp,
506 &f->f_av_value, &vtmp, RWM_MAP ) )
511 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
512 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
514 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
515 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
517 ch_free( vtmp.bv_val );
522 if ( map_attr_value( dc, &ad, &atmp,
523 &f->f_av_value, &vtmp, RWM_MAP ) )
528 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
529 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
531 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
532 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
534 ch_free( vtmp.bv_val );
539 if ( map_attr_value( dc, &ad, &atmp,
540 &f->f_av_value, &vtmp, RWM_MAP ) )
545 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
546 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
548 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
549 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
551 ch_free( vtmp.bv_val );
554 case LDAP_FILTER_APPROX:
556 if ( map_attr_value( dc, &ad, &atmp,
557 &f->f_av_value, &vtmp, RWM_MAP ) )
562 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
563 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
565 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
566 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
568 ch_free( vtmp.bv_val );
571 case LDAP_FILTER_SUBSTRINGS:
573 if ( map_attr_value( dc, &ad, &atmp,
574 NULL, NULL, RWM_MAP ) )
579 /* cannot be a DN ... */
581 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
582 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
584 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
587 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
590 filter_escape_value( &f->f_sub_initial, &vtmp );
592 fstr->bv_len += vtmp.bv_len;
593 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
595 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
596 /* "(attr=" */ "%s*)",
597 vtmp.bv_len ? vtmp.bv_val : "" );
599 ch_free( vtmp.bv_val );
602 if ( f->f_sub_any != NULL ) {
603 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
605 filter_escape_value( &f->f_sub_any[i], &vtmp );
607 fstr->bv_len += vtmp.bv_len + 1;
608 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
610 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
611 /* "(attr=[init]*[any*]" */ "%s*)",
612 vtmp.bv_len ? vtmp.bv_val : "" );
613 ch_free( vtmp.bv_val );
617 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
620 filter_escape_value( &f->f_sub_final, &vtmp );
622 fstr->bv_len += vtmp.bv_len;
623 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
625 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
626 /* "(attr=[init*][any*]" */ "%s)",
627 vtmp.bv_len ? vtmp.bv_val : "" );
629 ch_free( vtmp.bv_val );
634 case LDAP_FILTER_PRESENT:
636 if ( map_attr_value( dc, &ad, &atmp,
637 NULL, NULL, RWM_MAP ) )
642 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
643 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
645 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
649 case LDAP_FILTER_AND:
651 case LDAP_FILTER_NOT:
652 fstr->bv_len = STRLENOF( "(%)" );
653 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
655 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
656 f->f_choice == LDAP_FILTER_AND ? '&' :
657 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
659 for ( p = f->f_list; p != NULL; p = p->f_next ) {
664 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
665 if ( rc != LDAP_SUCCESS ) {
669 fstr->bv_len += vtmp.bv_len;
670 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
672 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
673 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
675 ch_free( vtmp.bv_val );
680 case LDAP_FILTER_EXT: {
681 if ( f->f_mr_desc ) {
683 if ( map_attr_value( dc, &ad, &atmp,
684 &f->f_mr_value, &vtmp, RWM_MAP ) )
690 BER_BVSTR( &atmp, "" );
691 filter_escape_value( &f->f_mr_value, &vtmp );
695 fstr->bv_len = atmp.bv_len +
696 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
697 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
698 vtmp.bv_len + STRLENOF( "(:=)" );
699 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
701 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
703 f->f_mr_dnattrs ? ":dn" : "",
704 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
705 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
706 vtmp.bv_len ? vtmp.bv_val : "" );
707 ch_free( vtmp.bv_val );
713 filter_free_x( op, f, 0 );
714 f->f_choice = SLAPD_FILTER_COMPUTED;
715 f->f_result = SLAPD_COMPARE_UNDEFINED;
718 case SLAPD_FILTER_COMPUTED:
719 switch ( f->f_result ) {
720 case LDAP_COMPARE_FALSE:
721 /* FIXME: treat UNDEFINED as FALSE */
722 case SLAPD_COMPARE_UNDEFINED:
723 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
724 tmp = &ber_bvtf_false;
730 case LDAP_COMPARE_TRUE:
731 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
732 tmp = &ber_bvtf_true;
743 ber_dupbv( fstr, tmp );
747 ber_dupbv( fstr, &ber_bvunknown );
755 rwm_filter_map_rewrite(
759 struct berval *fstr )
765 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
774 fdc.ctx = "searchFilter";
776 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
777 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
778 fdc.conn, &fstr->bv_val ) )
780 case REWRITE_REGEXEC_OK:
781 if ( !BER_BVISNULL( fstr ) ) {
782 fstr->bv_len = strlen( fstr->bv_val );
783 if ( fstr->bv_val != ftmp.bv_val ) {
784 ch_free( ftmp.bv_val );
791 Debug( LDAP_DEBUG_ARGS,
792 "[rw] %s: \"%s\" -> \"%s\"\n",
793 fdc.ctx, ftmp.bv_val, fstr->bv_val );
797 case REWRITE_REGEXEC_UNWILLING:
799 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
800 fdc.rs->sr_text = "Operation not allowed";
802 rc = LDAP_UNWILLING_TO_PERFORM;
805 case REWRITE_REGEXEC_ERR:
807 fdc.rs->sr_err = LDAP_OTHER;
808 fdc.rs->sr_text = "Rewrite error";
818 * I don't like this much, but we need two different
819 * functions because different heap managers may be
820 * in use in back-ldap/meta to reduce the amount of
821 * calls to malloc routines, and some of the free()
822 * routines may be macros with args
825 rwm_referral_rewrite(
830 BerVarray *pa_nvals )
832 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
833 struct ldaprwmap *rwmap =
834 (struct ldaprwmap *)on->on_bi.bi_private;
839 struct berval dn = BER_BVNULL,
842 assert( a_vals != NULL );
845 * Rewrite the dn if needed
848 dc.conn = op->o_conn;
850 dc.ctx = (char *)cookie;
852 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
856 if ( pa_nvals != NULL ) {
857 if ( *pa_nvals == NULL ) {
858 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
859 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
863 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
864 struct berval olddn = BER_BVNULL,
870 rc = ldap_url_parse( oldval.bv_val, &ludp );
871 if ( rc != LDAP_URL_SUCCESS ) {
872 /* leave attr untouched if massage failed */
873 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
874 ber_dupbv( &(*pa_nvals)[i], &oldval );
879 /* FIXME: URLs like "ldap:///dc=suffix" if passed
880 * thru ldap_url_parse() and ldap_url_desc2str()
881 * get rewritten as "ldap:///dc=suffix??base";
882 * we don't want this to occur... */
883 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
884 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
887 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
892 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
895 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
899 case LDAP_UNWILLING_TO_PERFORM:
901 * FIXME: need to check if it may be considered
902 * legal to trim values when adding/modifying;
903 * it should be when searching (e.g. ACLs).
905 ch_free( a_vals[i].bv_val );
907 a_vals[i] = a_vals[last];
909 (*pa_nvals)[i] = (*pa_nvals)[last];
912 BER_BVZERO( &a_vals[last] );
914 BER_BVZERO( &(*pa_nvals)[last] );
920 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
923 ludp->lud_dn = dn.bv_val;
924 newurl = ldap_url_desc2str( ludp );
925 ludp->lud_dn = olddn.bv_val;
926 ch_free( dn.bv_val );
927 if ( newurl == NULL ) {
928 /* FIXME: leave attr untouched
929 * even if ldap_url_desc2str failed...
934 ber_str2bv( newurl, 0, 1, &a_vals[i] );
938 ludp->lud_dn = ndn.bv_val;
939 newurl = ldap_url_desc2str( ludp );
940 ludp->lud_dn = olddn.bv_val;
941 ch_free( ndn.bv_val );
942 if ( newurl == NULL ) {
943 /* FIXME: leave attr untouched
944 * even if ldap_url_desc2str failed...
946 ch_free( a_vals[i].bv_val );
951 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
952 ch_free( (*pa_nvals)[i].bv_val );
954 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
958 ch_free( oldval.bv_val );
959 ludp->lud_dn = olddn.bv_val;
964 /* leave attr untouched if massage failed */
965 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
966 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
970 ldap_free_urldesc( ludp );
977 * I don't like this much, but we need two different
978 * functions because different heap managers may be
979 * in use in back-ldap/meta to reduce the amount of
980 * calls to malloc routines, and some of the free()
981 * routines may be macros with args
989 BerVarray *pa_nvals )
991 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
992 struct ldaprwmap *rwmap =
993 (struct ldaprwmap *)on->on_bi.bi_private;
998 struct berval dn = BER_BVNULL,
1006 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1013 * Rewrite the dn if needed
1016 dc.conn = op->o_conn;
1018 dc.ctx = (char *)cookie;
1020 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1022 if ( pa_nvals != NULL ) {
1023 if ( *pa_nvals == NULL ) {
1024 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1025 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1029 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1035 ndn = (*pa_nvals)[i];
1036 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1038 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1042 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1046 case LDAP_UNWILLING_TO_PERFORM:
1048 * FIXME: need to check if it may be considered
1049 * legal to trim values when adding/modifying;
1050 * it should be when searching (e.g. ACLs).
1052 ch_free( in[i].bv_val );
1055 if ( a_vals && pa_nvals ) {
1056 (*pa_nvals)[i] = (*pa_nvals)[last];
1059 BER_BVZERO( &in[last] );
1060 if ( a_vals && pa_nvals ) {
1061 BER_BVZERO( &(*pa_nvals)[last] );
1068 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1069 ch_free( a_vals[i].bv_val );
1073 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1074 ch_free( (*pa_nvals)[i].bv_val );
1076 (*pa_nvals)[i] = ndn;
1081 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1082 ch_free( (*pa_nvals)[i].bv_val );
1083 (*pa_nvals)[i] = ndn;
1089 /* leave attr untouched if massage failed */
1090 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1091 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1101 rwm_referral_result_rewrite(
1107 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1110 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1116 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1117 if ( rc != LDAP_URL_SUCCESS ) {
1118 /* leave attr untouched if massage failed */
1122 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1123 * thru ldap_url_parse() and ldap_url_desc2str()
1124 * get rewritten as "ldap:///dc=suffix??base";
1125 * we don't want this to occur... */
1126 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1127 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1130 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1133 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1135 case LDAP_UNWILLING_TO_PERFORM:
1137 * FIXME: need to check if it may be considered
1138 * legal to trim values when adding/modifying;
1139 * it should be when searching (e.g. ACLs).
1141 ch_free( a_vals[i].bv_val );
1143 a_vals[i] = a_vals[last];
1145 BER_BVZERO( &a_vals[last] );
1151 /* leave attr untouched if massage failed */
1152 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1155 ludp->lud_dn = dn.bv_val;
1156 newurl = ldap_url_desc2str( ludp );
1157 if ( newurl == NULL ) {
1158 /* FIXME: leave attr untouched
1159 * even if ldap_url_desc2str failed...
1164 ch_free( a_vals[i].bv_val );
1165 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1166 LDAP_FREE( newurl );
1167 ludp->lud_dn = olddn.bv_val;
1172 ldap_free_urldesc( ludp );
1179 rwm_dnattr_result_rewrite(
1185 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1188 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1193 rc = rwm_dn_massage_pretty( dc, &a_vals[i], &dn );
1195 case LDAP_UNWILLING_TO_PERFORM:
1197 * FIXME: need to check if it may be considered
1198 * legal to trim values when adding/modifying;
1199 * it should be when searching (e.g. ACLs).
1201 ch_free( a_vals[i].bv_val );
1203 a_vals[i] = a_vals[last];
1205 BER_BVZERO( &a_vals[last] );
1210 /* leave attr untouched if massage failed */
1211 if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1212 ch_free( a_vals[i].bv_val );
1223 rwm_mapping_dst_free( void *v_mapping )
1225 struct ldapmapping *mapping = v_mapping;
1227 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1228 rwm_mapping_free( &mapping[ -1 ] );
1233 rwm_mapping_free( void *v_mapping )
1235 struct ldapmapping *mapping = v_mapping;
1237 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1238 ch_free( mapping[0].m_src.bv_val );
1241 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1242 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1243 if ( mapping[0].m_src_oc ) {
1244 ch_free( mapping[0].m_src_oc );
1248 if ( mapping[0].m_src_ad ) {
1249 ch_free( mapping[0].m_src_ad );
1254 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1255 ch_free( mapping[0].m_dst.bv_val );
1258 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1259 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1260 if ( mapping[0].m_dst_oc ) {
1261 ch_free( mapping[0].m_dst_oc );
1265 if ( mapping[0].m_dst_ad ) {
1266 ch_free( mapping[0].m_dst_ad );
1275 #endif /* SLAPD_OVER_RWM */