1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2014 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_tmpcalloc( ( i + x + 1 ), sizeof( AttributeName ),
214 if ( *anp == NULL ) {
215 return LDAP_NO_MEMORY;
219 for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
220 struct ldapmapping *m;
221 int at_drop_missing = 0,
224 if ( an[i].an_desc ) {
226 /* FIXME: better leave as is? */
230 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
231 if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
242 if ( remap == RWM_MAP ) {
243 (*anp)[j].an_name = m->m_dst;
244 (*anp)[j].an_desc = m->m_dst_ad;
246 (*anp)[j].an_name = m->m_src;
247 (*anp)[j].an_desc = m->m_src_ad;
254 } else if ( an[i].an_oc ) {
256 /* FIXME: better leave as is? */
260 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
262 if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
273 if ( remap == RWM_MAP ) {
274 (*anp)[j].an_name = m->m_dst;
275 (*anp)[j].an_oc = m->m_dst_oc;
277 (*anp)[j].an_name = m->m_src;
278 (*anp)[j].an_oc = m->m_src_oc;
282 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
284 if ( at_drop_missing || !m ) {
285 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
287 /* if both at_map and oc_map required to drop missing,
289 if ( oc_drop_missing && at_drop_missing ) {
293 /* if no oc_map mapping was found and at_map required
294 * to drop missing, then do it; otherwise, at_map wins
295 * and an is considered an attr and is left unchanged */
297 if ( at_drop_missing ) {
305 if ( BER_BVISNULL( &m->m_dst ) ) {
310 if ( remap == RWM_MAP ) {
311 (*anp)[j].an_name = m->m_dst;
312 (*anp)[j].an_oc = m->m_dst_oc;
314 (*anp)[j].an_name = m->m_src;
315 (*anp)[j].an_oc = m->m_src_oc;
321 if ( !BER_BVISNULL( &m->m_dst ) ) {
323 if ( remap == RWM_MAP ) {
324 (*anp)[j].an_name = m->m_dst;
325 (*anp)[j].an_desc = m->m_dst_ad;
327 (*anp)[j].an_name = m->m_src;
328 (*anp)[j].an_desc = m->m_src_ad;
337 if ( op->o_bd->be_extra_anlist != NULL ) {
338 /* we assume be_extra_anlist are already mapped */
339 for ( x = 0; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ ) {
340 BER_BVZERO( &(*anp)[j].an_name );
341 if ( op->o_bd->be_extra_anlist[x].an_desc &&
342 ad_inlist( op->o_bd->be_extra_anlist[x].an_desc, *anp ) )
347 (*anp)[j] = op->o_bd->be_extra_anlist[x];
352 if ( j == 0 && ( i != 0 || x != 0 ) ) {
353 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
354 (*anp)[0].an_name = *slap_bv_no_attrs;
357 memset( &(*anp)[j], 0, sizeof( AttributeName ) );
365 struct ldapmap *at_map,
368 char ***mapped_attrs )
374 *mapped_attrs = NULL;
378 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ); i++ )
381 na = (char **)ch_calloc( i + 1, sizeof( char * ) );
383 *mapped_attrs = NULL;
384 return LDAP_NO_MEMORY;
387 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
388 struct ldapmapping *mapping;
390 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
395 na[ j++ ] = an[ i ].an_name.bv_val;
397 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
398 na[ j++ ] = mapping->m_dst.bv_val;
402 if ( j == 0 && i != 0 ) {
403 na[ j++ ] = LDAP_NO_ATTRS;
417 AttributeDescription **adp,
418 struct berval *mapped_attr,
419 struct berval *value,
420 struct berval *mapped_value,
424 struct berval vtmp = BER_BVNULL;
426 AttributeDescription *ad = *adp;
427 struct ldapmapping *mapping = NULL;
429 rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
430 if ( mapping == NULL ) {
431 if ( dc->rwmap->rwm_at.drop_missing ) {
435 *mapped_attr = ad->ad_cname;
438 *mapped_attr = mapping->m_dst;
441 if ( value != NULL ) {
442 assert( mapped_value != NULL );
444 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
445 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
450 fdc.ctx = "searchFilterAttrDN";
453 rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
456 if ( vtmp.bv_val != value->bv_val ) {
461 case LDAP_UNWILLING_TO_PERFORM:
467 } else if ( ad->ad_type->sat_equality &&
468 ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) )
470 if ( ad->ad_type->sat_equality->smr_normalize(
471 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
472 NULL, NULL, value, &vtmp, memctx ) )
478 } else if ( ad == slap_schema.si_ad_objectClass
479 || ad == slap_schema.si_ad_structuralObjectClass )
481 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
482 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
490 filter_escape_value_x( &vtmp, mapped_value, memctx );
494 ch_free( vtmp.bv_val );
498 ber_memfree_x( vtmp.bv_val, memctx );
503 if ( mapping != NULL ) {
504 assert( mapping->m_dst_ad != NULL );
505 *adp = mapping->m_dst_ad;
512 rwm_int_filter_map_rewrite(
516 struct berval *fstr )
520 AttributeDescription *ad;
525 /* better than nothing... */
526 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
527 ber_bvtf_false = BER_BVC( "(|)" ),
528 /* better than nothing... */
529 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
530 ber_bvtf_true = BER_BVC( "(&)" ),
532 /* no longer needed; preserved for completeness */
533 ber_bvundefined = BER_BVC( "(?=undefined)" ),
535 ber_bverror = BER_BVC( "(?=error)" ),
536 ber_bvunknown = BER_BVC( "(?=unknown)" ),
537 ber_bvnone = BER_BVC( "(?=none)" );
540 assert( fstr != NULL );
544 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
549 /* ITS#6814: give the caller a chance to use undefined filters */
550 if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
555 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
556 case LDAP_FILTER_EQUALITY:
558 if ( map_attr_value( dc, &ad, &atmp,
559 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
564 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
565 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
567 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
568 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
570 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
575 if ( map_attr_value( dc, &ad, &atmp,
576 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
581 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
582 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
584 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
585 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
587 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
592 if ( map_attr_value( dc, &ad, &atmp,
593 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
598 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
599 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
601 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
602 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
604 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
607 case LDAP_FILTER_APPROX:
609 if ( map_attr_value( dc, &ad, &atmp,
610 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
615 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
616 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
618 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
619 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
621 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
624 case LDAP_FILTER_SUBSTRINGS:
626 if ( map_attr_value( dc, &ad, &atmp,
627 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
632 /* cannot be a DN ... */
634 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
635 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
637 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
640 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
643 filter_escape_value_x( &f->f_sub_initial, &vtmp, op->o_tmpmemctx );
645 fstr->bv_len += vtmp.bv_len;
646 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
649 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
650 /* "(attr=" */ "%s*)",
651 vtmp.bv_len ? vtmp.bv_val : "" );
653 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
656 if ( f->f_sub_any != NULL ) {
657 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
659 filter_escape_value_x( &f->f_sub_any[i], &vtmp,
662 fstr->bv_len += vtmp.bv_len + 1;
663 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
666 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
667 /* "(attr=[init]*[any*]" */ "%s*)",
668 vtmp.bv_len ? vtmp.bv_val : "" );
669 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
673 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
676 filter_escape_value_x( &f->f_sub_final, &vtmp, op->o_tmpmemctx );
678 fstr->bv_len += vtmp.bv_len;
679 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
682 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
683 /* "(attr=[init*][any*]" */ "%s)",
684 vtmp.bv_len ? vtmp.bv_val : "" );
686 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
691 case LDAP_FILTER_PRESENT:
693 if ( map_attr_value( dc, &ad, &atmp,
694 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
699 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
700 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
702 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
706 case LDAP_FILTER_AND:
708 case LDAP_FILTER_NOT:
709 fstr->bv_len = STRLENOF( "(%)" );
710 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
712 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
713 f->f_choice == LDAP_FILTER_AND ? '&' :
714 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
716 for ( p = f->f_list; p != NULL; p = p->f_next ) {
721 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
722 if ( rc != LDAP_SUCCESS ) {
726 fstr->bv_len += vtmp.bv_len;
727 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
730 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
731 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
733 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
738 case LDAP_FILTER_EXT: {
739 if ( f->f_mr_desc ) {
741 if ( map_attr_value( dc, &ad, &atmp,
742 &f->f_mr_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
748 BER_BVSTR( &atmp, "" );
749 filter_escape_value_x( &f->f_mr_value, &vtmp, op->o_tmpmemctx );
753 fstr->bv_len = atmp.bv_len +
754 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
755 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
756 vtmp.bv_len + STRLENOF( "(:=)" );
757 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
759 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
761 f->f_mr_dnattrs ? ":dn" : "",
762 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
763 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
764 vtmp.bv_len ? vtmp.bv_val : "" );
765 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
771 filter_free_x( op, f, 0 );
772 f->f_choice = SLAPD_FILTER_COMPUTED;
773 f->f_result = SLAPD_COMPARE_UNDEFINED;
776 case SLAPD_FILTER_COMPUTED:
777 switch ( f->f_result ) {
778 case LDAP_COMPARE_FALSE:
779 /* FIXME: treat UNDEFINED as FALSE */
780 case SLAPD_COMPARE_UNDEFINED:
781 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
782 tmp = &ber_bvtf_false;
788 case LDAP_COMPARE_TRUE:
789 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
790 tmp = &ber_bvtf_true;
801 ber_dupbv_x( fstr, tmp, op->o_tmpmemctx );
805 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
813 rwm_filter_map_rewrite(
817 struct berval *fstr )
823 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
832 fdc.ctx = "searchFilter";
834 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
835 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
836 fdc.conn, &fstr->bv_val ) )
838 case REWRITE_REGEXEC_OK:
839 if ( !BER_BVISNULL( fstr ) ) {
840 fstr->bv_len = strlen( fstr->bv_val );
846 Debug( LDAP_DEBUG_ARGS,
847 "[rw] %s: \"%s\" -> \"%s\"\n",
848 fdc.ctx, ftmp.bv_val, fstr->bv_val );
849 if ( fstr->bv_val != ftmp.bv_val ) {
850 ber_bvreplace_x( &ftmp, fstr, op->o_tmpmemctx );
851 ch_free( fstr->bv_val );
857 case REWRITE_REGEXEC_UNWILLING:
859 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
860 fdc.rs->sr_text = "Operation not allowed";
862 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
863 rc = LDAP_UNWILLING_TO_PERFORM;
866 case REWRITE_REGEXEC_ERR:
868 fdc.rs->sr_err = LDAP_OTHER;
869 fdc.rs->sr_text = "Rewrite error";
871 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
880 * I don't like this much, but we need two different
881 * functions because different heap managers may be
882 * in use in back-ldap/meta to reduce the amount of
883 * calls to malloc routines, and some of the free()
884 * routines may be macros with args
887 rwm_referral_rewrite(
892 BerVarray *pa_nvals )
894 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
895 struct ldaprwmap *rwmap =
896 (struct ldaprwmap *)on->on_bi.bi_private;
901 struct berval dn = BER_BVNULL,
904 assert( a_vals != NULL );
907 * Rewrite the dn if needed
910 dc.conn = op->o_conn;
912 dc.ctx = (char *)cookie;
914 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
918 if ( pa_nvals != NULL ) {
919 if ( *pa_nvals == NULL ) {
920 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
921 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
925 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
926 struct berval olddn = BER_BVNULL,
932 rc = ldap_url_parse( oldval.bv_val, &ludp );
933 if ( rc != LDAP_URL_SUCCESS ) {
934 /* leave attr untouched if massage failed */
935 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
936 ber_dupbv( &(*pa_nvals)[i], &oldval );
941 /* FIXME: URLs like "ldap:///dc=suffix" if passed
942 * thru ldap_url_parse() and ldap_url_desc2str()
943 * get rewritten as "ldap:///dc=suffix??base";
944 * we don't want this to occur... */
945 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
946 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
949 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
954 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
957 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
961 case LDAP_UNWILLING_TO_PERFORM:
963 * FIXME: need to check if it may be considered
964 * legal to trim values when adding/modifying;
965 * it should be when searching (e.g. ACLs).
967 ch_free( a_vals[i].bv_val );
969 a_vals[i] = a_vals[last];
971 (*pa_nvals)[i] = (*pa_nvals)[last];
974 BER_BVZERO( &a_vals[last] );
976 BER_BVZERO( &(*pa_nvals)[last] );
982 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
985 ludp->lud_dn = dn.bv_val;
986 newurl = ldap_url_desc2str( ludp );
987 ludp->lud_dn = olddn.bv_val;
988 ch_free( dn.bv_val );
989 if ( newurl == NULL ) {
990 /* FIXME: leave attr untouched
991 * even if ldap_url_desc2str failed...
996 ber_str2bv( newurl, 0, 1, &a_vals[i] );
997 ber_memfree( newurl );
1000 ludp->lud_dn = ndn.bv_val;
1001 newurl = ldap_url_desc2str( ludp );
1002 ludp->lud_dn = olddn.bv_val;
1003 ch_free( ndn.bv_val );
1004 if ( newurl == NULL ) {
1005 /* FIXME: leave attr untouched
1006 * even if ldap_url_desc2str failed...
1008 ch_free( a_vals[i].bv_val );
1013 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1014 ch_free( (*pa_nvals)[i].bv_val );
1016 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
1017 ber_memfree( newurl );
1020 ch_free( oldval.bv_val );
1021 ludp->lud_dn = olddn.bv_val;
1026 /* leave attr untouched if massage failed */
1027 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1028 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
1032 ldap_free_urldesc( ludp );
1039 * I don't like this much, but we need two different
1040 * functions because different heap managers may be
1041 * in use in back-ldap/meta to reduce the amount of
1042 * calls to malloc routines, and some of the free()
1043 * routines may be macros with args
1051 BerVarray *pa_nvals )
1053 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1054 struct ldaprwmap *rwmap =
1055 (struct ldaprwmap *)on->on_bi.bi_private;
1060 struct berval dn = BER_BVNULL,
1068 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1075 * Rewrite the dn if needed
1078 dc.conn = op->o_conn;
1080 dc.ctx = (char *)cookie;
1082 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1084 if ( pa_nvals != NULL ) {
1085 if ( *pa_nvals == NULL ) {
1086 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1087 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1091 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1097 ndn = (*pa_nvals)[i];
1098 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1100 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1104 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1108 case LDAP_UNWILLING_TO_PERFORM:
1110 * FIXME: need to check if it may be considered
1111 * legal to trim values when adding/modifying;
1112 * it should be when searching (e.g. ACLs).
1114 ch_free( in[i].bv_val );
1117 if ( a_vals && pa_nvals ) {
1118 (*pa_nvals)[i] = (*pa_nvals)[last];
1121 BER_BVZERO( &in[last] );
1122 if ( a_vals && pa_nvals ) {
1123 BER_BVZERO( &(*pa_nvals)[last] );
1130 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1131 ch_free( a_vals[i].bv_val );
1135 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1136 ch_free( (*pa_nvals)[i].bv_val );
1138 (*pa_nvals)[i] = ndn;
1143 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1144 ch_free( (*pa_nvals)[i].bv_val );
1145 (*pa_nvals)[i] = ndn;
1151 /* leave attr untouched if massage failed */
1152 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1153 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1163 rwm_referral_result_rewrite(
1169 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1172 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1178 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1179 if ( rc != LDAP_URL_SUCCESS ) {
1180 /* leave attr untouched if massage failed */
1184 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1185 * thru ldap_url_parse() and ldap_url_desc2str()
1186 * get rewritten as "ldap:///dc=suffix??base";
1187 * we don't want this to occur... */
1188 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1189 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1192 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1195 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1197 case LDAP_UNWILLING_TO_PERFORM:
1199 * FIXME: need to check if it may be considered
1200 * legal to trim values when adding/modifying;
1201 * it should be when searching (e.g. ACLs).
1203 ch_free( a_vals[i].bv_val );
1205 a_vals[i] = a_vals[last];
1207 BER_BVZERO( &a_vals[last] );
1213 /* leave attr untouched if massage failed */
1214 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1217 ludp->lud_dn = dn.bv_val;
1218 newurl = ldap_url_desc2str( ludp );
1219 if ( newurl == NULL ) {
1220 /* FIXME: leave attr untouched
1221 * even if ldap_url_desc2str failed...
1226 ch_free( a_vals[i].bv_val );
1227 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1228 ber_memfree( newurl );
1229 ludp->lud_dn = olddn.bv_val;
1234 ldap_free_urldesc( ludp );
1241 rwm_dnattr_result_rewrite(
1248 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1251 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1252 struct berval pdn, ndn = BER_BVNULL;
1256 rc = rwm_dn_massage_pretty_normalize( dc, &a_vals[i], &pdn, &ndn );
1258 case LDAP_UNWILLING_TO_PERFORM:
1260 * FIXME: need to check if it may be considered
1261 * legal to trim values when adding/modifying;
1262 * it should be when searching (e.g. ACLs).
1264 assert( a_vals[i].bv_val != a_nvals[i].bv_val );
1265 ch_free( a_vals[i].bv_val );
1266 ch_free( a_nvals[i].bv_val );
1268 a_vals[i] = a_vals[last];
1269 a_nvals[i] = a_nvals[last];
1271 BER_BVZERO( &a_vals[last] );
1272 BER_BVZERO( &a_nvals[last] );
1277 /* leave attr untouched if massage failed */
1278 if ( !BER_BVISNULL( &pdn ) && a_vals[i].bv_val != pdn.bv_val ) {
1279 ch_free( a_vals[i].bv_val );
1282 if ( !BER_BVISNULL( &ndn ) && a_nvals[i].bv_val != ndn.bv_val ) {
1283 ch_free( a_nvals[i].bv_val );
1294 rwm_mapping_dst_free( void *v_mapping )
1296 struct ldapmapping *mapping = v_mapping;
1298 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1299 rwm_mapping_free( &mapping[ -1 ] );
1304 rwm_mapping_free( void *v_mapping )
1306 struct ldapmapping *mapping = v_mapping;
1308 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1309 ch_free( mapping[0].m_src.bv_val );
1312 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1313 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1314 if ( mapping[0].m_src_oc ) {
1315 ch_free( mapping[0].m_src_oc );
1319 if ( mapping[0].m_src_ad ) {
1320 ch_free( mapping[0].m_src_ad );
1325 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1326 ch_free( mapping[0].m_dst.bv_val );
1329 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1330 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1331 if ( mapping[0].m_dst_oc ) {
1332 ch_free( mapping[0].m_dst_oc );
1336 if ( mapping[0].m_dst_ad ) {
1337 ch_free( mapping[0].m_dst_ad );
1346 #endif /* SLAPD_OVER_RWM */