1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2011 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;
218 for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
219 struct ldapmapping *m;
220 int at_drop_missing = 0,
223 if ( an[i].an_desc ) {
225 /* FIXME: better leave as is? */
229 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
230 if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
241 if ( remap == RWM_MAP ) {
242 (*anp)[j].an_name = m->m_dst;
243 (*anp)[j].an_desc = m->m_dst_ad;
245 (*anp)[j].an_name = m->m_src;
246 (*anp)[j].an_desc = m->m_src_ad;
253 } else if ( an[i].an_oc ) {
255 /* FIXME: better leave as is? */
259 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
261 if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
272 if ( remap == RWM_MAP ) {
273 (*anp)[j].an_name = m->m_dst;
274 (*anp)[j].an_oc = m->m_dst_oc;
276 (*anp)[j].an_name = m->m_src;
277 (*anp)[j].an_oc = m->m_src_oc;
281 at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
283 if ( at_drop_missing || !m ) {
284 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
286 /* if both at_map and oc_map required to drop missing,
288 if ( oc_drop_missing && at_drop_missing ) {
292 /* if no oc_map mapping was found and at_map required
293 * to drop missing, then do it; otherwise, at_map wins
294 * and an is considered an attr and is left unchanged */
296 if ( at_drop_missing ) {
304 if ( BER_BVISNULL( &m->m_dst ) ) {
309 if ( remap == RWM_MAP ) {
310 (*anp)[j].an_name = m->m_dst;
311 (*anp)[j].an_oc = m->m_dst_oc;
313 (*anp)[j].an_name = m->m_src;
314 (*anp)[j].an_oc = m->m_src_oc;
320 if ( !BER_BVISNULL( &m->m_dst ) ) {
322 if ( remap == RWM_MAP ) {
323 (*anp)[j].an_name = m->m_dst;
324 (*anp)[j].an_desc = m->m_dst_ad;
326 (*anp)[j].an_name = m->m_src;
327 (*anp)[j].an_desc = m->m_src_ad;
335 if ( op->o_bd->be_extra_anlist != NULL ) {
336 /* we assume be_extra_anlist are already mapped */
337 for ( x = 0; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ ) {
338 BER_BVZERO( &(*anp)[j].an_name );
339 if ( op->o_bd->be_extra_anlist[x].an_desc &&
340 ad_inlist( op->o_bd->be_extra_anlist[x].an_desc, *anp ) )
345 (*anp)[j] = op->o_bd->be_extra_anlist[x];
350 if ( j == 0 && ( i != 0 || x != 0 ) ) {
351 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
352 (*anp)[0].an_name = *slap_bv_no_attrs;
355 memset( &(*anp)[j], 0, sizeof( AttributeName ) );
363 struct ldapmap *at_map,
366 char ***mapped_attrs )
372 *mapped_attrs = NULL;
376 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ); i++ )
379 na = (char **)ch_calloc( i + 1, sizeof( char * ) );
381 *mapped_attrs = NULL;
382 return LDAP_NO_MEMORY;
385 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
386 struct ldapmapping *mapping;
388 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
393 na[ j++ ] = an[ i ].an_name.bv_val;
395 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
396 na[ j++ ] = mapping->m_dst.bv_val;
400 if ( j == 0 && i != 0 ) {
401 na[ j++ ] = LDAP_NO_ATTRS;
415 AttributeDescription **adp,
416 struct berval *mapped_attr,
417 struct berval *value,
418 struct berval *mapped_value,
422 struct berval vtmp = BER_BVNULL;
424 AttributeDescription *ad = *adp;
425 struct ldapmapping *mapping = NULL;
427 rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
428 if ( mapping == NULL ) {
429 if ( dc->rwmap->rwm_at.drop_missing ) {
433 *mapped_attr = ad->ad_cname;
436 *mapped_attr = mapping->m_dst;
439 if ( value != NULL ) {
440 assert( mapped_value != NULL );
442 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
443 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
448 fdc.ctx = "searchFilterAttrDN";
451 rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
454 if ( vtmp.bv_val != value->bv_val ) {
459 case LDAP_UNWILLING_TO_PERFORM:
465 } else if ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) {
466 if ( ad->ad_type->sat_equality->smr_normalize(
467 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
468 NULL, NULL, value, &vtmp, memctx ) )
474 } else if ( ad == slap_schema.si_ad_objectClass
475 || ad == slap_schema.si_ad_structuralObjectClass )
477 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
478 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
486 filter_escape_value_x( &vtmp, mapped_value, memctx );
490 ch_free( vtmp.bv_val );
494 ber_memfree_x( vtmp.bv_val, memctx );
499 if ( mapping != NULL ) {
500 assert( mapping->m_dst_ad != NULL );
501 *adp = mapping->m_dst_ad;
508 rwm_int_filter_map_rewrite(
512 struct berval *fstr )
516 AttributeDescription *ad;
521 /* better than nothing... */
522 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
523 ber_bvtf_false = BER_BVC( "(|)" ),
524 /* better than nothing... */
525 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
526 ber_bvtf_true = BER_BVC( "(&)" ),
528 /* no longer needed; preserved for completeness */
529 ber_bvundefined = BER_BVC( "(?=undefined)" ),
531 ber_bverror = BER_BVC( "(?=error)" ),
532 ber_bvunknown = BER_BVC( "(?=unknown)" ),
533 ber_bvnone = BER_BVC( "(?=none)" );
536 assert( fstr != NULL );
540 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
545 /* ITS#6814: give the caller a chance to use undefined filters */
546 if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
551 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
552 case LDAP_FILTER_EQUALITY:
554 if ( map_attr_value( dc, &ad, &atmp,
555 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
560 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
561 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
563 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
564 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
566 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
571 if ( map_attr_value( dc, &ad, &atmp,
572 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
577 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
578 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
580 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
581 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
583 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
588 if ( map_attr_value( dc, &ad, &atmp,
589 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
594 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
595 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
597 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
598 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
600 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
603 case LDAP_FILTER_APPROX:
605 if ( map_attr_value( dc, &ad, &atmp,
606 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
611 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
612 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
614 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
615 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
617 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
620 case LDAP_FILTER_SUBSTRINGS:
622 if ( map_attr_value( dc, &ad, &atmp,
623 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
628 /* cannot be a DN ... */
630 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
631 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
633 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
636 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
639 filter_escape_value_x( &f->f_sub_initial, &vtmp, op->o_tmpmemctx );
641 fstr->bv_len += vtmp.bv_len;
642 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
645 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
646 /* "(attr=" */ "%s*)",
647 vtmp.bv_len ? vtmp.bv_val : "" );
649 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
652 if ( f->f_sub_any != NULL ) {
653 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
655 filter_escape_value_x( &f->f_sub_any[i], &vtmp,
658 fstr->bv_len += vtmp.bv_len + 1;
659 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
662 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
663 /* "(attr=[init]*[any*]" */ "%s*)",
664 vtmp.bv_len ? vtmp.bv_val : "" );
665 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
669 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
672 filter_escape_value_x( &f->f_sub_final, &vtmp, op->o_tmpmemctx );
674 fstr->bv_len += vtmp.bv_len;
675 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
678 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
679 /* "(attr=[init*][any*]" */ "%s)",
680 vtmp.bv_len ? vtmp.bv_val : "" );
682 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
687 case LDAP_FILTER_PRESENT:
689 if ( map_attr_value( dc, &ad, &atmp,
690 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
695 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
696 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
698 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
702 case LDAP_FILTER_AND:
704 case LDAP_FILTER_NOT:
705 fstr->bv_len = STRLENOF( "(%)" );
706 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
708 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
709 f->f_choice == LDAP_FILTER_AND ? '&' :
710 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
712 for ( p = f->f_list; p != NULL; p = p->f_next ) {
717 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
718 if ( rc != LDAP_SUCCESS ) {
722 fstr->bv_len += vtmp.bv_len;
723 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
726 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
727 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
729 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
734 case LDAP_FILTER_EXT: {
735 if ( f->f_mr_desc ) {
737 if ( map_attr_value( dc, &ad, &atmp,
738 &f->f_mr_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
744 BER_BVSTR( &atmp, "" );
745 filter_escape_value_x( &f->f_mr_value, &vtmp, op->o_tmpmemctx );
749 fstr->bv_len = atmp.bv_len +
750 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
751 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
752 vtmp.bv_len + STRLENOF( "(:=)" );
753 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
755 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
757 f->f_mr_dnattrs ? ":dn" : "",
758 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
759 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
760 vtmp.bv_len ? vtmp.bv_val : "" );
761 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
767 filter_free_x( op, f, 0 );
768 f->f_choice = SLAPD_FILTER_COMPUTED;
769 f->f_result = SLAPD_COMPARE_UNDEFINED;
772 case SLAPD_FILTER_COMPUTED:
773 switch ( f->f_result ) {
774 case LDAP_COMPARE_FALSE:
775 /* FIXME: treat UNDEFINED as FALSE */
776 case SLAPD_COMPARE_UNDEFINED:
777 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
778 tmp = &ber_bvtf_false;
784 case LDAP_COMPARE_TRUE:
785 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
786 tmp = &ber_bvtf_true;
797 ber_dupbv_x( fstr, tmp, op->o_tmpmemctx );
801 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
809 rwm_filter_map_rewrite(
813 struct berval *fstr )
819 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
828 fdc.ctx = "searchFilter";
830 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
831 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
832 fdc.conn, &fstr->bv_val ) )
834 case REWRITE_REGEXEC_OK:
835 if ( !BER_BVISNULL( fstr ) ) {
836 fstr->bv_len = strlen( fstr->bv_val );
842 Debug( LDAP_DEBUG_ARGS,
843 "[rw] %s: \"%s\" -> \"%s\"\n",
844 fdc.ctx, ftmp.bv_val, fstr->bv_val );
845 if ( fstr->bv_val != ftmp.bv_val ) {
846 ber_bvreplace_x( &ftmp, fstr, op->o_tmpmemctx );
847 ch_free( fstr->bv_val );
853 case REWRITE_REGEXEC_UNWILLING:
855 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
856 fdc.rs->sr_text = "Operation not allowed";
858 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
859 rc = LDAP_UNWILLING_TO_PERFORM;
862 case REWRITE_REGEXEC_ERR:
864 fdc.rs->sr_err = LDAP_OTHER;
865 fdc.rs->sr_text = "Rewrite error";
867 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
876 * I don't like this much, but we need two different
877 * functions because different heap managers may be
878 * in use in back-ldap/meta to reduce the amount of
879 * calls to malloc routines, and some of the free()
880 * routines may be macros with args
883 rwm_referral_rewrite(
888 BerVarray *pa_nvals )
890 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
891 struct ldaprwmap *rwmap =
892 (struct ldaprwmap *)on->on_bi.bi_private;
897 struct berval dn = BER_BVNULL,
900 assert( a_vals != NULL );
903 * Rewrite the dn if needed
906 dc.conn = op->o_conn;
908 dc.ctx = (char *)cookie;
910 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
914 if ( pa_nvals != NULL ) {
915 if ( *pa_nvals == NULL ) {
916 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
917 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
921 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
922 struct berval olddn = BER_BVNULL,
928 rc = ldap_url_parse( oldval.bv_val, &ludp );
929 if ( rc != LDAP_URL_SUCCESS ) {
930 /* leave attr untouched if massage failed */
931 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
932 ber_dupbv( &(*pa_nvals)[i], &oldval );
937 /* FIXME: URLs like "ldap:///dc=suffix" if passed
938 * thru ldap_url_parse() and ldap_url_desc2str()
939 * get rewritten as "ldap:///dc=suffix??base";
940 * we don't want this to occur... */
941 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
942 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
945 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
950 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
953 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
957 case LDAP_UNWILLING_TO_PERFORM:
959 * FIXME: need to check if it may be considered
960 * legal to trim values when adding/modifying;
961 * it should be when searching (e.g. ACLs).
963 ch_free( a_vals[i].bv_val );
965 a_vals[i] = a_vals[last];
967 (*pa_nvals)[i] = (*pa_nvals)[last];
970 BER_BVZERO( &a_vals[last] );
972 BER_BVZERO( &(*pa_nvals)[last] );
978 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
981 ludp->lud_dn = dn.bv_val;
982 newurl = ldap_url_desc2str( ludp );
983 ludp->lud_dn = olddn.bv_val;
984 ch_free( dn.bv_val );
985 if ( newurl == NULL ) {
986 /* FIXME: leave attr untouched
987 * even if ldap_url_desc2str failed...
992 ber_str2bv( newurl, 0, 1, &a_vals[i] );
993 ber_memfree( newurl );
996 ludp->lud_dn = ndn.bv_val;
997 newurl = ldap_url_desc2str( ludp );
998 ludp->lud_dn = olddn.bv_val;
999 ch_free( ndn.bv_val );
1000 if ( newurl == NULL ) {
1001 /* FIXME: leave attr untouched
1002 * even if ldap_url_desc2str failed...
1004 ch_free( a_vals[i].bv_val );
1009 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1010 ch_free( (*pa_nvals)[i].bv_val );
1012 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
1013 ber_memfree( newurl );
1016 ch_free( oldval.bv_val );
1017 ludp->lud_dn = olddn.bv_val;
1022 /* leave attr untouched if massage failed */
1023 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1024 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
1028 ldap_free_urldesc( ludp );
1035 * I don't like this much, but we need two different
1036 * functions because different heap managers may be
1037 * in use in back-ldap/meta to reduce the amount of
1038 * calls to malloc routines, and some of the free()
1039 * routines may be macros with args
1047 BerVarray *pa_nvals )
1049 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1050 struct ldaprwmap *rwmap =
1051 (struct ldaprwmap *)on->on_bi.bi_private;
1056 struct berval dn = BER_BVNULL,
1064 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1071 * Rewrite the dn if needed
1074 dc.conn = op->o_conn;
1076 dc.ctx = (char *)cookie;
1078 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1080 if ( pa_nvals != NULL ) {
1081 if ( *pa_nvals == NULL ) {
1082 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1083 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1087 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1093 ndn = (*pa_nvals)[i];
1094 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1096 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1100 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1104 case LDAP_UNWILLING_TO_PERFORM:
1106 * FIXME: need to check if it may be considered
1107 * legal to trim values when adding/modifying;
1108 * it should be when searching (e.g. ACLs).
1110 ch_free( in[i].bv_val );
1113 if ( a_vals && pa_nvals ) {
1114 (*pa_nvals)[i] = (*pa_nvals)[last];
1117 BER_BVZERO( &in[last] );
1118 if ( a_vals && pa_nvals ) {
1119 BER_BVZERO( &(*pa_nvals)[last] );
1126 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1127 ch_free( a_vals[i].bv_val );
1131 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1132 ch_free( (*pa_nvals)[i].bv_val );
1134 (*pa_nvals)[i] = ndn;
1139 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1140 ch_free( (*pa_nvals)[i].bv_val );
1141 (*pa_nvals)[i] = ndn;
1147 /* leave attr untouched if massage failed */
1148 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1149 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1159 rwm_referral_result_rewrite(
1165 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1168 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1174 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1175 if ( rc != LDAP_URL_SUCCESS ) {
1176 /* leave attr untouched if massage failed */
1180 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1181 * thru ldap_url_parse() and ldap_url_desc2str()
1182 * get rewritten as "ldap:///dc=suffix??base";
1183 * we don't want this to occur... */
1184 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1185 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1188 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1191 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1193 case LDAP_UNWILLING_TO_PERFORM:
1195 * FIXME: need to check if it may be considered
1196 * legal to trim values when adding/modifying;
1197 * it should be when searching (e.g. ACLs).
1199 ch_free( a_vals[i].bv_val );
1201 a_vals[i] = a_vals[last];
1203 BER_BVZERO( &a_vals[last] );
1209 /* leave attr untouched if massage failed */
1210 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1213 ludp->lud_dn = dn.bv_val;
1214 newurl = ldap_url_desc2str( ludp );
1215 if ( newurl == NULL ) {
1216 /* FIXME: leave attr untouched
1217 * even if ldap_url_desc2str failed...
1222 ch_free( a_vals[i].bv_val );
1223 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1224 ber_memfree( newurl );
1225 ludp->lud_dn = olddn.bv_val;
1230 ldap_free_urldesc( ludp );
1237 rwm_dnattr_result_rewrite(
1244 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1247 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1248 struct berval pdn, ndn = BER_BVNULL;
1252 rc = rwm_dn_massage_pretty_normalize( dc, &a_vals[i], &pdn, &ndn );
1254 case LDAP_UNWILLING_TO_PERFORM:
1256 * FIXME: need to check if it may be considered
1257 * legal to trim values when adding/modifying;
1258 * it should be when searching (e.g. ACLs).
1260 assert( a_vals[i].bv_val != a_nvals[i].bv_val );
1261 ch_free( a_vals[i].bv_val );
1262 ch_free( a_nvals[i].bv_val );
1264 a_vals[i] = a_vals[last];
1265 a_nvals[i] = a_nvals[last];
1267 BER_BVZERO( &a_vals[last] );
1268 BER_BVZERO( &a_nvals[last] );
1273 /* leave attr untouched if massage failed */
1274 if ( !BER_BVISNULL( &pdn ) && a_vals[i].bv_val != pdn.bv_val ) {
1275 ch_free( a_vals[i].bv_val );
1278 if ( !BER_BVISNULL( &ndn ) && a_nvals[i].bv_val != ndn.bv_val ) {
1279 ch_free( a_nvals[i].bv_val );
1290 rwm_mapping_dst_free( void *v_mapping )
1292 struct ldapmapping *mapping = v_mapping;
1294 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1295 rwm_mapping_free( &mapping[ -1 ] );
1300 rwm_mapping_free( void *v_mapping )
1302 struct ldapmapping *mapping = v_mapping;
1304 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1305 ch_free( mapping[0].m_src.bv_val );
1308 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1309 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1310 if ( mapping[0].m_src_oc ) {
1311 ch_free( mapping[0].m_src_oc );
1315 if ( mapping[0].m_src_ad ) {
1316 ch_free( mapping[0].m_src_ad );
1321 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1322 ch_free( mapping[0].m_dst.bv_val );
1325 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1326 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1327 if ( mapping[0].m_dst_oc ) {
1328 ch_free( mapping[0].m_dst_oc );
1332 if ( mapping[0].m_dst_ad ) {
1333 ch_free( mapping[0].m_dst_ad );
1342 #endif /* SLAPD_OVER_RWM */