1 /* map.c - ldap backend mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Howard Chu for inclusion
18 * in OpenLDAP Software and subsequently enhanced by Pierangelo
21 /* This is an altered version */
23 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
25 * Permission is granted to anyone to use this software for any purpose
26 * on any computer system, and to alter it and redistribute it, subject
27 * to the following restrictions:
29 * 1. The author is not responsible for the consequences of use of this
30 * software, no matter how awful, even if they arise from flaws in it.
32 * 2. The origin of this software must not be misrepresented, either by
33 * explicit claim or by omission. Since few users ever read sources,
34 * credits should appear in the documentation.
36 * 3. Altered versions must be plainly marked as such, and must not be
37 * misrepresented as being the original software. Since few users
38 * ever read sources, credits should appear in the documentation.
40 * 4. This notice may not be removed or altered.
44 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
46 * This software is being modified by Pierangelo Masarati.
47 * The previously reported conditions apply to the modified code as well.
48 * Changes in the original code are highlighted where required.
49 * Credits for the original code go to the author, Howard Chu.
56 #include <ac/string.h>
57 #include <ac/socket.h>
61 #include "../back-ldap/back-ldap.h"
62 #include "back-meta.h"
64 #undef ldap_debug /* silence a warning in ldap-int.h */
65 #include "../../../libraries/libldap/ldap-int.h"
68 mapping_cmp ( const void *c1, const void *c2 )
70 struct ldapmapping *map1 = (struct ldapmapping *)c1;
71 struct ldapmapping *map2 = (struct ldapmapping *)c2;
72 int rc = map1->src.bv_len - map2->src.bv_len;
74 return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) );
78 mapping_dup ( void *c1, void *c2 )
80 struct ldapmapping *map1 = (struct ldapmapping *)c1;
81 struct ldapmapping *map2 = (struct ldapmapping *)c2;
83 return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 );
87 ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
89 struct ldapmapping *mapping;
95 mapping = (struct ldapmapping *)ch_calloc( 2,
96 sizeof( struct ldapmapping ) );
97 if ( mapping == NULL ) {
101 ber_str2bv( "objectclass", STRLENOF("objectclass"), 1, &mapping[0].src);
102 ber_dupbv( &mapping[0].dst, &mapping[0].src );
103 mapping[1].src = mapping[0].src;
104 mapping[1].dst = mapping[0].dst;
106 avl_insert( &lm->map, (caddr_t)&mapping[0],
107 mapping_cmp, mapping_dup );
108 avl_insert( &lm->remap, (caddr_t)&mapping[1],
109 mapping_cmp, mapping_dup );
114 ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,
118 struct ldapmapping fmapping;
122 /* let special attrnames slip through (ITS#5760) */
123 if ( bvmatch( s, slap_bv_no_attrs )
124 || bvmatch( s, slap_bv_all_user_attrs )
125 || bvmatch( s, slap_bv_all_operational_attrs ) )
131 if ( remap == BACKLDAP_REMAP ) {
139 *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
141 return map->drop_missing;
148 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
151 struct ldapmapping *mapping;
154 /* map->map may be NULL when mapping is configured,
155 * but map->remap can't */
156 if ( map->remap == NULL ) {
162 drop_missing = ldap_back_mapping( map, s, &mapping, remap );
163 if ( mapping != NULL ) {
164 if ( !BER_BVISNULL( &mapping->dst ) ) {
170 if ( !drop_missing ) {
177 struct ldapmap *at_map,
185 struct berval mapped;
188 *mapped_attrs = NULL;
192 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
195 na = (char **)ch_calloc( i + 1, sizeof(char *) );
197 *mapped_attrs = NULL;
198 return LDAP_NO_MEMORY;
201 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
202 ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
203 if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
204 na[j++] = mapped.bv_val;
207 if ( j == 0 && i != 0 ) {
208 na[j++] = LDAP_NO_ATTRS;
219 AttributeDescription *ad,
220 struct berval *mapped_attr,
221 struct berval *value,
222 struct berval *mapped_value,
228 ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap );
229 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
232 * FIXME: are we sure we need to search oc_map if at_map fails?
234 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap );
235 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
236 *mapped_attr = ad->ad_cname;
239 if ( dc->target->mt_rwmap.rwm_at.drop_missing ) {
243 *mapped_attr = ad->ad_cname;
246 if ( value == NULL ) {
250 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
254 #ifdef ENABLE_REWRITE
255 fdc.ctx = "searchFilterAttrDN";
258 switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
260 if ( vtmp.bv_val != value->bv_val ) {
265 case LDAP_UNWILLING_TO_PERFORM:
272 } else if ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) {
273 if ( ad->ad_type->sat_equality->smr_normalize(
274 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
275 NULL, NULL, value, &vtmp, NULL ) )
281 } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
282 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap );
283 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
291 filter_escape_value( &vtmp, mapped_value );
294 ber_memfree( vtmp.bv_val );
301 ldap_back_int_filter_map_rewrite(
313 /* better than nothing... */
314 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
315 ber_bvtf_false = BER_BVC( "(|)" ),
316 /* better than nothing... */
317 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
318 ber_bvtf_true = BER_BVC( "(&)" ),
320 /* no longer needed; preserved for completeness */
321 ber_bvundefined = BER_BVC( "(?=undefined)" ),
323 ber_bverror = BER_BVC( "(?=error)" ),
324 ber_bvunknown = BER_BVC( "(?=unknown)" ),
325 ber_bvnone = BER_BVC( "(?=none)" );
328 assert( fstr != NULL );
332 ber_dupbv( fstr, &ber_bvnone );
336 switch ( ( f->f_choice & SLAPD_FILTER_MASK ) ) {
337 case LDAP_FILTER_EQUALITY:
338 if ( map_attr_value( dc, f->f_av_desc, &atmp,
339 &f->f_av_value, &vtmp, remap ) )
344 fstr->bv_len = atmp.bv_len + vtmp.bv_len
345 + ( sizeof("(=)") - 1 );
346 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
348 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
349 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
351 ber_memfree( vtmp.bv_val );
355 if ( map_attr_value( dc, f->f_av_desc, &atmp,
356 &f->f_av_value, &vtmp, remap ) )
361 fstr->bv_len = atmp.bv_len + vtmp.bv_len
362 + ( sizeof("(>=)") - 1 );
363 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
365 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
366 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
368 ber_memfree( vtmp.bv_val );
372 if ( map_attr_value( dc, f->f_av_desc, &atmp,
373 &f->f_av_value, &vtmp, remap ) )
378 fstr->bv_len = atmp.bv_len + vtmp.bv_len
379 + ( sizeof("(<=)") - 1 );
380 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
382 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
383 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
385 ber_memfree( vtmp.bv_val );
388 case LDAP_FILTER_APPROX:
389 if ( map_attr_value( dc, f->f_av_desc, &atmp,
390 &f->f_av_value, &vtmp, remap ) )
395 fstr->bv_len = atmp.bv_len + vtmp.bv_len
396 + ( sizeof("(~=)") - 1 );
397 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
399 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
400 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
402 ber_memfree( vtmp.bv_val );
405 case LDAP_FILTER_SUBSTRINGS:
406 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
407 NULL, NULL, remap ) )
412 /* cannot be a DN ... */
414 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
415 fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */
417 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
420 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
423 filter_escape_value( &f->f_sub_initial, &vtmp );
425 fstr->bv_len += vtmp.bv_len;
426 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
428 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
429 /* "(attr=" */ "%s*)",
430 vtmp.bv_len ? vtmp.bv_val : "" );
432 ber_memfree( vtmp.bv_val );
435 if ( f->f_sub_any != NULL ) {
436 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
438 filter_escape_value( &f->f_sub_any[i], &vtmp );
440 fstr->bv_len += vtmp.bv_len + 1;
441 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
443 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
444 /* "(attr=[init]*[any*]" */ "%s*)",
445 vtmp.bv_len ? vtmp.bv_val : "" );
446 ber_memfree( vtmp.bv_val );
450 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
453 filter_escape_value( &f->f_sub_final, &vtmp );
455 fstr->bv_len += vtmp.bv_len;
456 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
458 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
459 /* "(attr=[init*][any*]" */ "%s)",
460 vtmp.bv_len ? vtmp.bv_val : "" );
462 ber_memfree( vtmp.bv_val );
467 case LDAP_FILTER_PRESENT:
468 if ( map_attr_value( dc, f->f_desc, &atmp,
469 NULL, NULL, remap ) )
474 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
475 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
477 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
481 case LDAP_FILTER_AND:
483 case LDAP_FILTER_NOT:
484 fstr->bv_len = STRLENOF( "(%)" );
485 fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); /* FIXME: why 128? */
487 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
488 f->f_choice == LDAP_FILTER_AND ? '&' :
489 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
491 for ( p = f->f_list; p != NULL; p = p->f_next ) {
496 rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap );
497 if ( rc != LDAP_SUCCESS ) {
501 fstr->bv_len += vtmp.bv_len;
502 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
504 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
505 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
507 ch_free( vtmp.bv_val );
512 case LDAP_FILTER_EXT:
513 if ( f->f_mr_desc ) {
514 if ( map_attr_value( dc, f->f_mr_desc, &atmp,
515 &f->f_mr_value, &vtmp, remap ) )
521 BER_BVSTR( &atmp, "" );
522 filter_escape_value( &f->f_mr_value, &vtmp );
525 /* FIXME: cleanup (less ?: operators...) */
526 fstr->bv_len = atmp.bv_len +
527 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
528 ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
529 vtmp.bv_len + ( STRLENOF( "(:=)" ) );
530 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
532 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
534 f->f_mr_dnattrs ? ":dn" : "",
535 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
536 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
537 vtmp.bv_len ? vtmp.bv_val : "" );
538 ber_memfree( vtmp.bv_val );
541 case SLAPD_FILTER_COMPUTED:
542 switch ( f->f_result ) {
543 /* FIXME: treat UNDEFINED as FALSE */
544 case SLAPD_COMPARE_UNDEFINED:
546 if ( META_BACK_TGT_NOUNDEFFILTER( dc->target ) ) {
547 return LDAP_COMPARE_FALSE;
551 case LDAP_COMPARE_FALSE:
552 if ( META_BACK_TGT_T_F( dc->target ) ) {
553 tmp = &ber_bvtf_false;
559 case LDAP_COMPARE_TRUE:
560 if ( META_BACK_TGT_T_F( dc->target ) ) {
561 tmp = &ber_bvtf_true;
573 ber_dupbv( fstr, tmp );
577 ber_dupbv( fstr, &ber_bvunknown );
585 ldap_back_filter_map_rewrite(
594 static char *dmy = "";
596 rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap );
598 #ifdef ENABLE_REWRITE
599 if ( rc != LDAP_SUCCESS ) {
606 fdc.ctx = "searchFilter";
608 switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx,
609 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ),
610 fdc.conn, &fstr->bv_val ) )
612 case REWRITE_REGEXEC_OK:
613 if ( !BER_BVISNULL( fstr ) ) {
614 fstr->bv_len = strlen( fstr->bv_val );
619 Debug( LDAP_DEBUG_ARGS,
620 "[rw] %s: \"%s\" -> \"%s\"\n",
621 fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val,
622 BER_BVISNULL( fstr ) ? "" : fstr->bv_val );
626 case REWRITE_REGEXEC_UNWILLING:
628 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
629 fdc.rs->sr_text = "Operation not allowed";
631 rc = LDAP_UNWILLING_TO_PERFORM;
634 case REWRITE_REGEXEC_ERR:
636 fdc.rs->sr_err = LDAP_OTHER;
637 fdc.rs->sr_text = "Rewrite error";
643 if ( fstr->bv_val == dmy ) {
646 #endif /* ENABLE_REWRITE */
652 ldap_back_referral_result_rewrite(
659 assert( dc != NULL );
660 assert( a_vals != NULL );
662 for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
666 for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
672 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
673 if ( rc != LDAP_URL_SUCCESS ) {
674 /* leave attr untouched if massage failed */
678 /* FIXME: URLs like "ldap:///dc=suffix" if passed
679 * thru ldap_url_parse() and ldap_url_desc2str()
680 * get rewritten as "ldap:///dc=suffix??base";
681 * we don't want this to occur... */
682 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
683 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
686 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
688 rc = ldap_back_dn_massage( dc, &olddn, &dn );
690 case LDAP_UNWILLING_TO_PERFORM:
692 * FIXME: need to check if it may be considered
693 * legal to trim values when adding/modifying;
694 * it should be when searching (e.g. ACLs).
696 LBER_FREE( a_vals[ i ].bv_val );
698 a_vals[ i ] = a_vals[ last ];
700 BER_BVZERO( &a_vals[ last ] );
706 /* leave attr untouched if massage failed */
707 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val )
711 ludp->lud_dn = dn.bv_val;
712 newurl = ldap_url_desc2str( ludp );
714 if ( newurl == NULL ) {
715 /* FIXME: leave attr untouched
716 * even if ldap_url_desc2str failed...
721 LBER_FREE( a_vals[ i ].bv_val );
722 ber_str2bv( newurl, 0, 1, &a_vals[ i ] );
724 ludp->lud_dn = olddn.bv_val;
729 ldap_free_urldesc( ludp );
736 * I don't like this much, but we need two different
737 * functions because different heap managers may be
738 * in use in back-ldap/meta to reduce the amount of
739 * calls to malloc routines, and some of the free()
740 * routines may be macros with args
751 assert( a_vals != NULL );
753 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
757 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
758 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
759 case LDAP_UNWILLING_TO_PERFORM:
761 * FIXME: need to check if it may be considered
762 * legal to trim values when adding/modifying;
763 * it should be when searching (e.g. ACLs).
765 ch_free( a_vals[i].bv_val );
767 a_vals[i] = a_vals[last];
769 BER_BVZERO( &a_vals[last] );
774 /* leave attr untouched if massage failed */
775 if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
776 ch_free( a_vals[i].bv_val );
787 ldap_dnattr_result_rewrite(
795 assert( a_vals != NULL );
797 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
801 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
802 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
803 case LDAP_UNWILLING_TO_PERFORM:
805 * FIXME: need to check if it may be considered
806 * legal to trim values when adding/modifying;
807 * it should be when searching (e.g. ACLs).
809 LBER_FREE( a_vals[i].bv_val );
811 a_vals[i] = a_vals[last];
813 BER_BVZERO( &a_vals[last] );
818 /* leave attr untouched if massage failed */
819 if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
820 LBER_FREE( a_vals[i].bv_val );