1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2013 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 &&
466 ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) )
468 if ( ad->ad_type->sat_equality->smr_normalize(
469 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
470 NULL, NULL, value, &vtmp, memctx ) )
476 } else if ( ad == slap_schema.si_ad_objectClass
477 || ad == slap_schema.si_ad_structuralObjectClass )
479 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
480 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
488 filter_escape_value_x( &vtmp, mapped_value, memctx );
492 ch_free( vtmp.bv_val );
496 ber_memfree_x( vtmp.bv_val, memctx );
501 if ( mapping != NULL ) {
502 assert( mapping->m_dst_ad != NULL );
503 *adp = mapping->m_dst_ad;
510 rwm_int_filter_map_rewrite(
514 struct berval *fstr )
518 AttributeDescription *ad;
523 /* better than nothing... */
524 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
525 ber_bvtf_false = BER_BVC( "(|)" ),
526 /* better than nothing... */
527 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
528 ber_bvtf_true = BER_BVC( "(&)" ),
530 /* no longer needed; preserved for completeness */
531 ber_bvundefined = BER_BVC( "(?=undefined)" ),
533 ber_bverror = BER_BVC( "(?=error)" ),
534 ber_bvunknown = BER_BVC( "(?=unknown)" ),
535 ber_bvnone = BER_BVC( "(?=none)" );
538 assert( fstr != NULL );
542 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
547 /* ITS#6814: give the caller a chance to use undefined filters */
548 if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
553 switch ( f->f_choice & SLAPD_FILTER_MASK ) {
554 case LDAP_FILTER_EQUALITY:
556 if ( map_attr_value( dc, &ad, &atmp,
557 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
562 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
563 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
565 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
566 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
568 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
573 if ( map_attr_value( dc, &ad, &atmp,
574 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
579 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
580 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
582 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
583 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
585 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
590 if ( map_attr_value( dc, &ad, &atmp,
591 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
596 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
597 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
599 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
600 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
602 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
605 case LDAP_FILTER_APPROX:
607 if ( map_attr_value( dc, &ad, &atmp,
608 &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
613 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
614 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
616 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
617 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
619 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
622 case LDAP_FILTER_SUBSTRINGS:
624 if ( map_attr_value( dc, &ad, &atmp,
625 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
630 /* cannot be a DN ... */
632 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
633 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
635 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
638 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
641 filter_escape_value_x( &f->f_sub_initial, &vtmp, op->o_tmpmemctx );
643 fstr->bv_len += vtmp.bv_len;
644 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
647 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
648 /* "(attr=" */ "%s*)",
649 vtmp.bv_len ? vtmp.bv_val : "" );
651 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
654 if ( f->f_sub_any != NULL ) {
655 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
657 filter_escape_value_x( &f->f_sub_any[i], &vtmp,
660 fstr->bv_len += vtmp.bv_len + 1;
661 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
664 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
665 /* "(attr=[init]*[any*]" */ "%s*)",
666 vtmp.bv_len ? vtmp.bv_val : "" );
667 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
671 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
674 filter_escape_value_x( &f->f_sub_final, &vtmp, op->o_tmpmemctx );
676 fstr->bv_len += vtmp.bv_len;
677 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
680 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
681 /* "(attr=[init*][any*]" */ "%s)",
682 vtmp.bv_len ? vtmp.bv_val : "" );
684 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
689 case LDAP_FILTER_PRESENT:
691 if ( map_attr_value( dc, &ad, &atmp,
692 NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
697 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
698 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
700 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
704 case LDAP_FILTER_AND:
706 case LDAP_FILTER_NOT:
707 fstr->bv_len = STRLENOF( "(%)" );
708 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
710 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
711 f->f_choice == LDAP_FILTER_AND ? '&' :
712 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
714 for ( p = f->f_list; p != NULL; p = p->f_next ) {
719 rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
720 if ( rc != LDAP_SUCCESS ) {
724 fstr->bv_len += vtmp.bv_len;
725 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
728 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
729 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
731 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
736 case LDAP_FILTER_EXT: {
737 if ( f->f_mr_desc ) {
739 if ( map_attr_value( dc, &ad, &atmp,
740 &f->f_mr_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
746 BER_BVSTR( &atmp, "" );
747 filter_escape_value_x( &f->f_mr_value, &vtmp, op->o_tmpmemctx );
751 fstr->bv_len = atmp.bv_len +
752 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
753 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
754 vtmp.bv_len + STRLENOF( "(:=)" );
755 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
757 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
759 f->f_mr_dnattrs ? ":dn" : "",
760 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
761 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
762 vtmp.bv_len ? vtmp.bv_val : "" );
763 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
769 filter_free_x( op, f, 0 );
770 f->f_choice = SLAPD_FILTER_COMPUTED;
771 f->f_result = SLAPD_COMPARE_UNDEFINED;
774 case SLAPD_FILTER_COMPUTED:
775 switch ( f->f_result ) {
776 case LDAP_COMPARE_FALSE:
777 /* FIXME: treat UNDEFINED as FALSE */
778 case SLAPD_COMPARE_UNDEFINED:
779 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
780 tmp = &ber_bvtf_false;
786 case LDAP_COMPARE_TRUE:
787 if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
788 tmp = &ber_bvtf_true;
799 ber_dupbv_x( fstr, tmp, op->o_tmpmemctx );
803 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
811 rwm_filter_map_rewrite(
815 struct berval *fstr )
821 rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
830 fdc.ctx = "searchFilter";
832 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
833 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
834 fdc.conn, &fstr->bv_val ) )
836 case REWRITE_REGEXEC_OK:
837 if ( !BER_BVISNULL( fstr ) ) {
838 fstr->bv_len = strlen( fstr->bv_val );
844 Debug( LDAP_DEBUG_ARGS,
845 "[rw] %s: \"%s\" -> \"%s\"\n",
846 fdc.ctx, ftmp.bv_val, fstr->bv_val );
847 if ( fstr->bv_val != ftmp.bv_val ) {
848 ber_bvreplace_x( &ftmp, fstr, op->o_tmpmemctx );
849 ch_free( fstr->bv_val );
855 case REWRITE_REGEXEC_UNWILLING:
857 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
858 fdc.rs->sr_text = "Operation not allowed";
860 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
861 rc = LDAP_UNWILLING_TO_PERFORM;
864 case REWRITE_REGEXEC_ERR:
866 fdc.rs->sr_err = LDAP_OTHER;
867 fdc.rs->sr_text = "Rewrite error";
869 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
878 * I don't like this much, but we need two different
879 * functions because different heap managers may be
880 * in use in back-ldap/meta to reduce the amount of
881 * calls to malloc routines, and some of the free()
882 * routines may be macros with args
885 rwm_referral_rewrite(
890 BerVarray *pa_nvals )
892 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
893 struct ldaprwmap *rwmap =
894 (struct ldaprwmap *)on->on_bi.bi_private;
899 struct berval dn = BER_BVNULL,
902 assert( a_vals != NULL );
905 * Rewrite the dn if needed
908 dc.conn = op->o_conn;
910 dc.ctx = (char *)cookie;
912 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
916 if ( pa_nvals != NULL ) {
917 if ( *pa_nvals == NULL ) {
918 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
919 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
923 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
924 struct berval olddn = BER_BVNULL,
930 rc = ldap_url_parse( oldval.bv_val, &ludp );
931 if ( rc != LDAP_URL_SUCCESS ) {
932 /* leave attr untouched if massage failed */
933 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
934 ber_dupbv( &(*pa_nvals)[i], &oldval );
939 /* FIXME: URLs like "ldap:///dc=suffix" if passed
940 * thru ldap_url_parse() and ldap_url_desc2str()
941 * get rewritten as "ldap:///dc=suffix??base";
942 * we don't want this to occur... */
943 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
944 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
947 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
952 rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
955 rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
959 case LDAP_UNWILLING_TO_PERFORM:
961 * FIXME: need to check if it may be considered
962 * legal to trim values when adding/modifying;
963 * it should be when searching (e.g. ACLs).
965 ch_free( a_vals[i].bv_val );
967 a_vals[i] = a_vals[last];
969 (*pa_nvals)[i] = (*pa_nvals)[last];
972 BER_BVZERO( &a_vals[last] );
974 BER_BVZERO( &(*pa_nvals)[last] );
980 if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
983 ludp->lud_dn = dn.bv_val;
984 newurl = ldap_url_desc2str( ludp );
985 ludp->lud_dn = olddn.bv_val;
986 ch_free( dn.bv_val );
987 if ( newurl == NULL ) {
988 /* FIXME: leave attr untouched
989 * even if ldap_url_desc2str failed...
994 ber_str2bv( newurl, 0, 1, &a_vals[i] );
995 ber_memfree( newurl );
998 ludp->lud_dn = ndn.bv_val;
999 newurl = ldap_url_desc2str( ludp );
1000 ludp->lud_dn = olddn.bv_val;
1001 ch_free( ndn.bv_val );
1002 if ( newurl == NULL ) {
1003 /* FIXME: leave attr untouched
1004 * even if ldap_url_desc2str failed...
1006 ch_free( a_vals[i].bv_val );
1011 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1012 ch_free( (*pa_nvals)[i].bv_val );
1014 ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
1015 ber_memfree( newurl );
1018 ch_free( oldval.bv_val );
1019 ludp->lud_dn = olddn.bv_val;
1024 /* leave attr untouched if massage failed */
1025 if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1026 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
1030 ldap_free_urldesc( ludp );
1037 * I don't like this much, but we need two different
1038 * functions because different heap managers may be
1039 * in use in back-ldap/meta to reduce the amount of
1040 * calls to malloc routines, and some of the free()
1041 * routines may be macros with args
1049 BerVarray *pa_nvals )
1051 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1052 struct ldaprwmap *rwmap =
1053 (struct ldaprwmap *)on->on_bi.bi_private;
1058 struct berval dn = BER_BVNULL,
1066 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1073 * Rewrite the dn if needed
1076 dc.conn = op->o_conn;
1078 dc.ctx = (char *)cookie;
1080 for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1082 if ( pa_nvals != NULL ) {
1083 if ( *pa_nvals == NULL ) {
1084 *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1085 memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1089 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1095 ndn = (*pa_nvals)[i];
1096 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1098 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1102 rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1106 case LDAP_UNWILLING_TO_PERFORM:
1108 * FIXME: need to check if it may be considered
1109 * legal to trim values when adding/modifying;
1110 * it should be when searching (e.g. ACLs).
1112 ch_free( in[i].bv_val );
1115 if ( a_vals && pa_nvals ) {
1116 (*pa_nvals)[i] = (*pa_nvals)[last];
1119 BER_BVZERO( &in[last] );
1120 if ( a_vals && pa_nvals ) {
1121 BER_BVZERO( &(*pa_nvals)[last] );
1128 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1129 ch_free( a_vals[i].bv_val );
1133 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1134 ch_free( (*pa_nvals)[i].bv_val );
1136 (*pa_nvals)[i] = ndn;
1141 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1142 ch_free( (*pa_nvals)[i].bv_val );
1143 (*pa_nvals)[i] = ndn;
1149 /* leave attr untouched if massage failed */
1150 if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1151 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1161 rwm_referral_result_rewrite(
1167 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1170 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1176 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1177 if ( rc != LDAP_URL_SUCCESS ) {
1178 /* leave attr untouched if massage failed */
1182 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1183 * thru ldap_url_parse() and ldap_url_desc2str()
1184 * get rewritten as "ldap:///dc=suffix??base";
1185 * we don't want this to occur... */
1186 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1187 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1190 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1193 rc = rwm_dn_massage_pretty( dc, &olddn, &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] );
1211 /* leave attr untouched if massage failed */
1212 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1215 ludp->lud_dn = dn.bv_val;
1216 newurl = ldap_url_desc2str( ludp );
1217 if ( newurl == NULL ) {
1218 /* FIXME: leave attr untouched
1219 * even if ldap_url_desc2str failed...
1224 ch_free( a_vals[i].bv_val );
1225 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1226 ber_memfree( newurl );
1227 ludp->lud_dn = olddn.bv_val;
1232 ldap_free_urldesc( ludp );
1239 rwm_dnattr_result_rewrite(
1246 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1249 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1250 struct berval pdn, ndn = BER_BVNULL;
1254 rc = rwm_dn_massage_pretty_normalize( dc, &a_vals[i], &pdn, &ndn );
1256 case LDAP_UNWILLING_TO_PERFORM:
1258 * FIXME: need to check if it may be considered
1259 * legal to trim values when adding/modifying;
1260 * it should be when searching (e.g. ACLs).
1262 assert( a_vals[i].bv_val != a_nvals[i].bv_val );
1263 ch_free( a_vals[i].bv_val );
1264 ch_free( a_nvals[i].bv_val );
1266 a_vals[i] = a_vals[last];
1267 a_nvals[i] = a_nvals[last];
1269 BER_BVZERO( &a_vals[last] );
1270 BER_BVZERO( &a_nvals[last] );
1275 /* leave attr untouched if massage failed */
1276 if ( !BER_BVISNULL( &pdn ) && a_vals[i].bv_val != pdn.bv_val ) {
1277 ch_free( a_vals[i].bv_val );
1280 if ( !BER_BVISNULL( &ndn ) && a_nvals[i].bv_val != ndn.bv_val ) {
1281 ch_free( a_nvals[i].bv_val );
1292 rwm_mapping_dst_free( void *v_mapping )
1294 struct ldapmapping *mapping = v_mapping;
1296 if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1297 rwm_mapping_free( &mapping[ -1 ] );
1302 rwm_mapping_free( void *v_mapping )
1304 struct ldapmapping *mapping = v_mapping;
1306 if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1307 ch_free( mapping[0].m_src.bv_val );
1310 if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1311 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1312 if ( mapping[0].m_src_oc ) {
1313 ch_free( mapping[0].m_src_oc );
1317 if ( mapping[0].m_src_ad ) {
1318 ch_free( mapping[0].m_src_ad );
1323 if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1324 ch_free( mapping[0].m_dst.bv_val );
1327 if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1328 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1329 if ( mapping[0].m_dst_oc ) {
1330 ch_free( mapping[0].m_dst_oc );
1334 if ( mapping[0].m_dst_ad ) {
1335 ch_free( mapping[0].m_dst_ad );
1344 #endif /* SLAPD_OVER_RWM */