1 /* map.c - ldap backend mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2011 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"
65 mapping_cmp ( const void *c1, const void *c2 )
67 struct ldapmapping *map1 = (struct ldapmapping *)c1;
68 struct ldapmapping *map2 = (struct ldapmapping *)c2;
69 int rc = map1->src.bv_len - map2->src.bv_len;
71 return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) );
75 mapping_dup ( void *c1, void *c2 )
77 struct ldapmapping *map1 = (struct ldapmapping *)c1;
78 struct ldapmapping *map2 = (struct ldapmapping *)c2;
80 return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 );
84 ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
86 struct ldapmapping *mapping;
92 mapping = (struct ldapmapping *)ch_calloc( 2,
93 sizeof( struct ldapmapping ) );
94 if ( mapping == NULL ) {
98 ber_str2bv( "objectclass", STRLENOF("objectclass"), 1, &mapping[0].src);
99 ber_dupbv( &mapping[0].dst, &mapping[0].src );
100 mapping[1].src = mapping[0].src;
101 mapping[1].dst = mapping[0].dst;
103 avl_insert( &lm->map, (caddr_t)&mapping[0],
104 mapping_cmp, mapping_dup );
105 avl_insert( &lm->remap, (caddr_t)&mapping[1],
106 mapping_cmp, mapping_dup );
111 ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,
115 struct ldapmapping fmapping;
119 /* let special attrnames slip through (ITS#5760) */
120 if ( bvmatch( s, slap_bv_no_attrs )
121 || bvmatch( s, slap_bv_all_user_attrs )
122 || bvmatch( s, slap_bv_all_operational_attrs ) )
128 if ( remap == BACKLDAP_REMAP ) {
136 *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
138 return map->drop_missing;
145 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
148 struct ldapmapping *mapping;
151 /* map->map may be NULL when mapping is configured,
152 * but map->remap can't */
153 if ( map->remap == NULL ) {
159 drop_missing = ldap_back_mapping( map, s, &mapping, remap );
160 if ( mapping != NULL ) {
161 if ( !BER_BVISNULL( &mapping->dst ) ) {
167 if ( !drop_missing ) {
175 struct ldapmap *at_map,
178 char ***mapped_attrs )
182 struct berval mapped;
184 if ( an == NULL && op->o_bd->be_extra_anlist == NULL ) {
185 *mapped_attrs = NULL;
191 for ( ; !BER_BVISNULL( &an[i].an_name ); i++ )
196 if ( op->o_bd->be_extra_anlist != NULL ) {
197 for ( ; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ )
201 assert( i > 0 || x > 0 );
203 na = (char **)ber_memcalloc_x( i + x + 1, sizeof(char *), op->o_tmpmemctx );
205 *mapped_attrs = NULL;
206 return LDAP_NO_MEMORY;
211 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
212 ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
213 if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
214 na[j++] = mapped.bv_val;
220 for ( x = 0; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ ) {
221 if ( op->o_bd->be_extra_anlist[x].an_desc &&
222 ad_inlist( op->o_bd->be_extra_anlist[x].an_desc, an ) )
227 ldap_back_map( at_map, &op->o_bd->be_extra_anlist[x].an_name, &mapped, remap );
228 if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
229 na[j++] = mapped.bv_val;
234 if ( j == 0 && ( i > 0 || x > 0 ) ) {
235 na[j++] = LDAP_NO_ATTRS;
247 AttributeDescription *ad,
248 struct berval *mapped_attr,
249 struct berval *value,
250 struct berval *mapped_value,
257 ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap );
258 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
261 * FIXME: are we sure we need to search oc_map if at_map fails?
263 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap );
264 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
265 *mapped_attr = ad->ad_cname;
268 if ( dc->target->mt_rwmap.rwm_at.drop_missing ) {
272 *mapped_attr = ad->ad_cname;
275 if ( value == NULL ) {
279 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
283 #ifdef ENABLE_REWRITE
284 fdc.ctx = "searchFilterAttrDN";
287 switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
289 if ( vtmp.bv_val != value->bv_val ) {
294 case LDAP_UNWILLING_TO_PERFORM:
301 } else if ( ad->ad_type->sat_equality &&
302 ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER )
304 if ( ad->ad_type->sat_equality->smr_normalize(
305 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
306 NULL, NULL, value, &vtmp, memctx ) )
312 } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
313 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap );
314 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
322 filter_escape_value_x( &vtmp, mapped_value, memctx );
326 ber_memfree( vtmp.bv_val );
329 ber_memfree_x( vtmp.bv_val, memctx );
337 ldap_back_int_filter_map_rewrite(
350 /* better than nothing... */
351 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
352 ber_bvtf_false = BER_BVC( "(|)" ),
353 /* better than nothing... */
354 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
355 ber_bvtf_true = BER_BVC( "(&)" ),
357 /* no longer needed; preserved for completeness */
358 ber_bvundefined = BER_BVC( "(?=undefined)" ),
360 ber_bverror = BER_BVC( "(?=error)" ),
361 ber_bvunknown = BER_BVC( "(?=unknown)" ),
362 ber_bvnone = BER_BVC( "(?=none)" );
365 assert( fstr != NULL );
369 ber_dupbv_x( fstr, &ber_bvnone, memctx );
373 switch ( ( f->f_choice & SLAPD_FILTER_MASK ) ) {
374 case LDAP_FILTER_EQUALITY:
375 if ( map_attr_value( dc, f->f_av_desc, &atmp,
376 &f->f_av_value, &vtmp, remap, memctx ) )
381 fstr->bv_len = atmp.bv_len + vtmp.bv_len
382 + ( sizeof("(=)") - 1 );
383 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx );
385 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
386 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
388 ber_memfree_x( vtmp.bv_val, memctx );
392 if ( map_attr_value( dc, f->f_av_desc, &atmp,
393 &f->f_av_value, &vtmp, remap, memctx ) )
398 fstr->bv_len = atmp.bv_len + vtmp.bv_len
399 + ( sizeof("(>=)") - 1 );
400 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx );
402 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
403 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
405 ber_memfree_x( vtmp.bv_val, memctx );
409 if ( map_attr_value( dc, f->f_av_desc, &atmp,
410 &f->f_av_value, &vtmp, remap, memctx ) )
415 fstr->bv_len = atmp.bv_len + vtmp.bv_len
416 + ( sizeof("(<=)") - 1 );
417 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx );
419 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
420 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
422 ber_memfree_x( vtmp.bv_val, memctx );
425 case LDAP_FILTER_APPROX:
426 if ( map_attr_value( dc, f->f_av_desc, &atmp,
427 &f->f_av_value, &vtmp, remap, memctx ) )
432 fstr->bv_len = atmp.bv_len + vtmp.bv_len
433 + ( sizeof("(~=)") - 1 );
434 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx );
436 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
437 atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
439 ber_memfree_x( vtmp.bv_val, memctx );
442 case LDAP_FILTER_SUBSTRINGS:
443 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
444 NULL, NULL, remap, memctx ) )
449 /* cannot be a DN ... */
451 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
452 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 128, memctx ); /* FIXME: why 128 ? */
454 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
457 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
460 filter_escape_value_x( &f->f_sub_initial, &vtmp, memctx );
462 fstr->bv_len += vtmp.bv_len;
463 fstr->bv_val = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx );
465 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
466 /* "(attr=" */ "%s*)",
467 vtmp.bv_len ? vtmp.bv_val : "" );
469 ber_memfree_x( vtmp.bv_val, memctx );
472 if ( f->f_sub_any != NULL ) {
473 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
475 filter_escape_value_x( &f->f_sub_any[i], &vtmp, memctx );
477 fstr->bv_len += vtmp.bv_len + 1;
478 fstr->bv_val = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx );
480 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
481 /* "(attr=[init]*[any*]" */ "%s*)",
482 vtmp.bv_len ? vtmp.bv_val : "" );
483 ber_memfree_x( vtmp.bv_val, memctx );
487 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
490 filter_escape_value_x( &f->f_sub_final, &vtmp, memctx );
492 fstr->bv_len += vtmp.bv_len;
493 fstr->bv_val = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx );
495 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
496 /* "(attr=[init*][any*]" */ "%s)",
497 vtmp.bv_len ? vtmp.bv_val : "" );
499 ber_memfree_x( vtmp.bv_val, memctx );
504 case LDAP_FILTER_PRESENT:
505 if ( map_attr_value( dc, f->f_desc, &atmp,
506 NULL, NULL, remap, memctx ) )
511 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
512 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx );
514 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
518 case LDAP_FILTER_AND:
520 case LDAP_FILTER_NOT:
521 fstr->bv_len = STRLENOF( "(%)" );
522 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 128, memctx ); /* FIXME: why 128? */
524 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
525 f->f_choice == LDAP_FILTER_AND ? '&' :
526 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
528 for ( p = f->f_list; p != NULL; p = p->f_next ) {
533 rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap, memctx );
534 if ( rc != LDAP_SUCCESS ) {
538 fstr->bv_len += vtmp.bv_len;
539 fstr->bv_val = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx );
541 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
542 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
544 ber_memfree_x( vtmp.bv_val, memctx );
549 case LDAP_FILTER_EXT:
550 if ( f->f_mr_desc ) {
551 if ( map_attr_value( dc, f->f_mr_desc, &atmp,
552 &f->f_mr_value, &vtmp, remap, memctx ) )
558 BER_BVSTR( &atmp, "" );
559 filter_escape_value_x( &f->f_mr_value, &vtmp, memctx );
562 /* FIXME: cleanup (less ?: operators...) */
563 fstr->bv_len = atmp.bv_len +
564 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
565 ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
566 vtmp.bv_len + ( STRLENOF( "(:=)" ) );
567 fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx );
569 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
571 f->f_mr_dnattrs ? ":dn" : "",
572 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
573 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
574 vtmp.bv_len ? vtmp.bv_val : "" );
575 ber_memfree_x( vtmp.bv_val, memctx );
578 case SLAPD_FILTER_COMPUTED:
579 switch ( f->f_result ) {
580 /* FIXME: treat UNDEFINED as FALSE */
581 case SLAPD_COMPARE_UNDEFINED:
583 if ( META_BACK_TGT_NOUNDEFFILTER( dc->target ) ) {
584 return LDAP_COMPARE_FALSE;
588 case LDAP_COMPARE_FALSE:
589 if ( META_BACK_TGT_T_F( dc->target ) ) {
590 tmp = &ber_bvtf_false;
596 case LDAP_COMPARE_TRUE:
597 if ( META_BACK_TGT_T_F( dc->target ) ) {
598 tmp = &ber_bvtf_true;
610 ber_dupbv_x( fstr, tmp, memctx );
614 ber_dupbv_x( fstr, &ber_bvunknown, memctx );
622 ldap_back_filter_map_rewrite(
632 static char *dmy = "";
634 rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap, memctx );
636 #ifdef ENABLE_REWRITE
637 if ( rc != LDAP_SUCCESS ) {
644 fdc.ctx = "searchFilter";
646 switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx,
647 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ),
648 fdc.conn, &fstr->bv_val ) )
650 case REWRITE_REGEXEC_OK:
651 if ( !BER_BVISNULL( fstr ) ) {
652 fstr->bv_len = strlen( fstr->bv_val );
657 Debug( LDAP_DEBUG_ARGS,
658 "[rw] %s: \"%s\" -> \"%s\"\n",
659 fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val,
660 BER_BVISNULL( fstr ) ? "" : fstr->bv_val );
664 case REWRITE_REGEXEC_UNWILLING:
666 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
667 fdc.rs->sr_text = "Operation not allowed";
669 rc = LDAP_UNWILLING_TO_PERFORM;
672 case REWRITE_REGEXEC_ERR:
674 fdc.rs->sr_err = LDAP_OTHER;
675 fdc.rs->sr_text = "Rewrite error";
681 if ( fstr->bv_val == dmy ) {
684 } else if ( fstr->bv_val != ftmp.bv_val ) {
685 /* NOTE: need to realloc mapped filter on slab
686 * and free the original one, until librewrite
689 ber_dupbv_x( &ftmp, fstr, memctx );
690 ch_free( fstr->bv_val );
693 #endif /* ENABLE_REWRITE */
699 ldap_back_referral_result_rewrite(
707 assert( dc != NULL );
708 assert( a_vals != NULL );
710 for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
714 for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
720 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
721 if ( rc != LDAP_URL_SUCCESS ) {
722 /* leave attr untouched if massage failed */
726 /* FIXME: URLs like "ldap:///dc=suffix" if passed
727 * thru ldap_url_parse() and ldap_url_desc2str()
728 * get rewritten as "ldap:///dc=suffix??base";
729 * we don't want this to occur... */
730 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
731 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
734 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
736 rc = ldap_back_dn_massage( dc, &olddn, &dn );
738 case LDAP_UNWILLING_TO_PERFORM:
740 * FIXME: need to check if it may be considered
741 * legal to trim values when adding/modifying;
742 * it should be when searching (e.g. ACLs).
744 ber_memfree( a_vals[ i ].bv_val );
746 a_vals[ i ] = a_vals[ last ];
748 BER_BVZERO( &a_vals[ last ] );
754 /* leave attr untouched if massage failed */
755 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val )
759 ludp->lud_dn = dn.bv_val;
760 newurl = ldap_url_desc2str( ludp );
762 if ( newurl == NULL ) {
763 /* FIXME: leave attr untouched
764 * even if ldap_url_desc2str failed...
769 ber_memfree_x( a_vals[ i ].bv_val, memctx );
770 ber_str2bv_x( newurl, 0, 1, &a_vals[ i ], memctx );
771 ber_memfree( newurl );
772 ludp->lud_dn = olddn.bv_val;
777 ldap_free_urldesc( ludp );
784 * I don't like this much, but we need two different
785 * functions because different heap managers may be
786 * in use in back-ldap/meta to reduce the amount of
787 * calls to malloc routines, and some of the free()
788 * routines may be macros with args
799 assert( a_vals != NULL );
801 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
805 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
806 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
807 case LDAP_UNWILLING_TO_PERFORM:
809 * FIXME: need to check if it may be considered
810 * legal to trim values when adding/modifying;
811 * it should be when searching (e.g. ACLs).
813 ch_free( a_vals[i].bv_val );
815 a_vals[i] = a_vals[last];
817 BER_BVZERO( &a_vals[last] );
822 /* leave attr untouched if massage failed */
823 if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
824 ch_free( a_vals[i].bv_val );
835 ldap_dnattr_result_rewrite(
843 assert( a_vals != NULL );
845 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
849 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
850 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
851 case LDAP_UNWILLING_TO_PERFORM:
853 * FIXME: need to check if it may be considered
854 * legal to trim values when adding/modifying;
855 * it should be when searching (e.g. ACLs).
857 ber_memfree( a_vals[i].bv_val );
859 a_vals[i] = a_vals[last];
861 BER_BVZERO( &a_vals[last] );
866 /* leave attr untouched if massage failed */
867 if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
868 ber_memfree( a_vals[i].bv_val );