1 /* map.c - ldap backend mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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>
60 #include "../back-ldap/back-ldap.h"
61 #include "back-meta.h"
63 #undef ldap_debug /* silence a warning in ldap-int.h */
64 #include "../../../libraries/libldap/ldap-int.h"
67 mapping_cmp ( const void *c1, const void *c2 )
69 struct ldapmapping *map1 = (struct ldapmapping *)c1;
70 struct ldapmapping *map2 = (struct ldapmapping *)c2;
71 int rc = map1->src.bv_len - map2->src.bv_len;
73 return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) );
77 mapping_dup ( void *c1, void *c2 )
79 struct ldapmapping *map1 = (struct ldapmapping *)c1;
80 struct ldapmapping *map2 = (struct ldapmapping *)c2;
82 return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 );
86 ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
88 struct ldapmapping *mapping;
94 mapping = (struct ldapmapping *)ch_calloc( 2,
95 sizeof( struct ldapmapping ) );
96 if ( mapping == NULL ) {
100 ber_str2bv( "objectclass", sizeof("objectclass")-1, 1, &mapping->src);
101 ber_dupbv( &mapping->dst, &mapping->src );
102 mapping[1].src = mapping->src;
103 mapping[1].dst = mapping->dst;
105 avl_insert( &lm->map, (caddr_t)mapping,
106 mapping_cmp, mapping_dup );
107 avl_insert( &lm->remap, (caddr_t)&mapping[1],
108 mapping_cmp, mapping_dup );
113 ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,
117 struct ldapmapping fmapping;
121 if ( remap == BACKLDAP_REMAP ) {
128 *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
130 return map->drop_missing;
137 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
140 struct ldapmapping *mapping;
143 ( void )ldap_back_mapping( map, s, &mapping, remap );
144 if ( mapping != NULL ) {
145 if ( !BER_BVISNULL( &mapping->dst ) ) {
151 if ( !map->drop_missing ) {
158 struct ldapmap *at_map,
166 struct berval mapped;
169 *mapped_attrs = NULL;
173 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
176 na = (char **)ch_calloc( i + 1, sizeof(char *) );
178 *mapped_attrs = NULL;
179 return LDAP_NO_MEMORY;
182 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
183 ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
184 if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
185 na[j++] = mapped.bv_val;
188 if ( j == 0 && i != 0 ) {
189 na[j++] = LDAP_NO_ATTRS;
200 AttributeDescription *ad,
201 struct berval *mapped_attr,
202 struct berval *value,
203 struct berval *mapped_value,
209 ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap );
210 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
213 * FIXME: are we sure we need to search oc_map if at_map fails?
215 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap );
216 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
217 *mapped_attr = ad->ad_cname;
220 if ( dc->target->mt_rwmap.rwm_at.drop_missing ) {
224 *mapped_attr = ad->ad_cname;
227 if ( value == NULL ) {
231 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
235 #ifdef ENABLE_REWRITE
236 fdc.ctx = "searchFilterAttrDN";
239 switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
241 if ( vtmp.bv_val != value->bv_val ) {
246 case LDAP_UNWILLING_TO_PERFORM:
253 } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
254 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap );
255 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
263 filter_escape_value( &vtmp, mapped_value );
266 ber_memfree( vtmp.bv_val );
273 ldap_back_int_filter_map_rewrite(
285 /* better than nothing... */
286 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
287 ber_bvtf_false = BER_BVC( "(|)" ),
288 /* better than nothing... */
289 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
290 ber_bvtf_true = BER_BVC( "(&)" ),
292 /* no longer needed; preserved for completeness */
293 ber_bvundefined = BER_BVC( "(?=undefined)" ),
295 ber_bverror = BER_BVC( "(?=error)" ),
296 ber_bvunknown = BER_BVC( "(?=unknown)" ),
297 ber_bvnone = BER_BVC( "(?=none)" );
300 assert( fstr != NULL );
304 ber_dupbv( fstr, &ber_bvnone );
308 switch ( f->f_choice ) {
309 case LDAP_FILTER_EQUALITY:
310 if ( map_attr_value( dc, f->f_av_desc, &atmp,
311 &f->f_av_value, &vtmp, remap ) )
316 fstr->bv_len = atmp.bv_len + vtmp.bv_len
317 + ( sizeof("(=)") - 1 );
318 fstr->bv_val = malloc( fstr->bv_len + 1 );
320 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
321 atmp.bv_val, vtmp.bv_val );
323 ber_memfree( vtmp.bv_val );
327 if ( map_attr_value( dc, f->f_av_desc, &atmp,
328 &f->f_av_value, &vtmp, remap ) )
333 fstr->bv_len = atmp.bv_len + vtmp.bv_len
334 + ( sizeof("(>=)") - 1 );
335 fstr->bv_val = malloc( fstr->bv_len + 1 );
337 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
338 atmp.bv_val, vtmp.bv_val );
340 ber_memfree( vtmp.bv_val );
344 if ( map_attr_value( dc, f->f_av_desc, &atmp,
345 &f->f_av_value, &vtmp, remap ) )
350 fstr->bv_len = atmp.bv_len + vtmp.bv_len
351 + ( sizeof("(<=)") - 1 );
352 fstr->bv_val = malloc( fstr->bv_len + 1 );
354 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
355 atmp.bv_val, vtmp.bv_val );
357 ber_memfree( vtmp.bv_val );
360 case LDAP_FILTER_APPROX:
361 if ( map_attr_value( dc, f->f_av_desc, &atmp,
362 &f->f_av_value, &vtmp, remap ) )
367 fstr->bv_len = atmp.bv_len + vtmp.bv_len
368 + ( sizeof("(~=)") - 1 );
369 fstr->bv_val = malloc( fstr->bv_len + 1 );
371 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
372 atmp.bv_val, vtmp.bv_val );
374 ber_memfree( vtmp.bv_val );
377 case LDAP_FILTER_SUBSTRINGS:
378 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
379 NULL, NULL, remap ) )
384 /* cannot be a DN ... */
386 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
387 fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */
389 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
392 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
395 filter_escape_value( &f->f_sub_initial, &vtmp );
397 fstr->bv_len += vtmp.bv_len;
398 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
400 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
401 /* "(attr=" */ "%s*)",
404 ber_memfree( vtmp.bv_val );
407 if ( f->f_sub_any != NULL ) {
408 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
410 filter_escape_value( &f->f_sub_any[i], &vtmp );
412 fstr->bv_len += vtmp.bv_len + 1;
413 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
415 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
416 /* "(attr=[init]*[any*]" */ "%s*)",
418 ber_memfree( vtmp.bv_val );
422 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
425 filter_escape_value( &f->f_sub_final, &vtmp );
427 fstr->bv_len += vtmp.bv_len;
428 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
430 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
431 /* "(attr=[init*][any*]" */ "%s)",
434 ber_memfree( vtmp.bv_val );
439 case LDAP_FILTER_PRESENT:
440 if ( map_attr_value( dc, f->f_desc, &atmp,
441 NULL, NULL, remap ) )
446 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
447 fstr->bv_val = malloc( fstr->bv_len + 1 );
449 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
453 case LDAP_FILTER_AND:
455 case LDAP_FILTER_NOT:
456 fstr->bv_len = STRLENOF( "(%)" );
457 fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128? */
459 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
460 f->f_choice == LDAP_FILTER_AND ? '&' :
461 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
463 for ( p = f->f_list; p != NULL; p = p->f_next ) {
468 rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap );
469 if ( rc != LDAP_SUCCESS ) {
473 fstr->bv_len += vtmp.bv_len;
474 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
476 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
477 /*"("*/ "%s)", vtmp.bv_val );
479 ch_free( vtmp.bv_val );
484 case LDAP_FILTER_EXT:
485 if ( f->f_mr_desc ) {
486 if ( map_attr_value( dc, f->f_mr_desc, &atmp,
487 &f->f_mr_value, &vtmp, remap ) )
493 BER_BVSTR( &atmp, "" );
494 filter_escape_value( &f->f_mr_value, &vtmp );
497 /* FIXME: cleanup (less ?: operators...) */
498 fstr->bv_len = atmp.bv_len +
499 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
500 ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
501 vtmp.bv_len + ( STRLENOF( "(:=)" ) );
502 fstr->bv_val = malloc( fstr->bv_len + 1 );
504 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
506 f->f_mr_dnattrs ? ":dn" : "",
507 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
508 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
510 ber_memfree( vtmp.bv_val );
513 case SLAPD_FILTER_COMPUTED:
514 switch ( f->f_result ) {
515 case LDAP_COMPARE_FALSE:
516 /* FIXME: treat UNDEFINED as FALSE */
517 case SLAPD_COMPARE_UNDEFINED:
519 if ( dc->target->mt_flags & LDAP_BACK_F_SUPPORT_T_F ) {
520 tmp = &ber_bvtf_false;
526 case LDAP_COMPARE_TRUE:
527 if ( dc->target->mt_flags & LDAP_BACK_F_SUPPORT_T_F ) {
528 tmp = &ber_bvtf_true;
540 ber_dupbv( fstr, tmp );
544 ber_dupbv( fstr, &ber_bvunknown );
552 ldap_back_filter_map_rewrite(
561 static char *dmy = "";
563 rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap );
565 #ifdef ENABLE_REWRITE
566 if ( rc != LDAP_SUCCESS ) {
573 fdc.ctx = "searchFilter";
575 switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx,
576 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ),
577 fdc.conn, &fstr->bv_val ) )
579 case REWRITE_REGEXEC_OK:
580 if ( !BER_BVISNULL( fstr ) ) {
581 fstr->bv_len = strlen( fstr->bv_val );
586 Debug( LDAP_DEBUG_ARGS,
587 "[rw] %s: \"%s\" -> \"%s\"\n",
588 fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val,
589 BER_BVISNULL( fstr ) ? "" : fstr->bv_val );
593 case REWRITE_REGEXEC_UNWILLING:
595 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
596 fdc.rs->sr_text = "Operation not allowed";
598 rc = LDAP_UNWILLING_TO_PERFORM;
601 case REWRITE_REGEXEC_ERR:
603 fdc.rs->sr_err = LDAP_OTHER;
604 fdc.rs->sr_text = "Rewrite error";
610 if ( fstr->bv_val == dmy ) {
613 #endif /* ENABLE_REWRITE */
619 ldap_back_referral_result_rewrite(
626 assert( dc != NULL );
627 assert( a_vals != NULL );
629 for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
633 for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
639 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
640 if ( rc != LDAP_URL_SUCCESS ) {
641 /* leave attr untouched if massage failed */
645 /* FIXME: URLs like "ldap:///dc=suffix" if passed
646 * thru ldap_url_parse() and ldap_url_desc2str()
647 * get rewritten as "ldap:///dc=suffix??base";
648 * we don't want this to occur... */
649 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
650 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
653 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
655 rc = ldap_back_dn_massage( dc, &olddn, &dn );
657 case LDAP_UNWILLING_TO_PERFORM:
659 * FIXME: need to check if it may be considered
660 * legal to trim values when adding/modifying;
661 * it should be when searching (e.g. ACLs).
663 LBER_FREE( a_vals[ i ].bv_val );
665 a_vals[ i ] = a_vals[ last ];
667 BER_BVZERO( &a_vals[ last ] );
673 /* leave attr untouched if massage failed */
674 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val )
678 ludp->lud_dn = dn.bv_val;
679 newurl = ldap_url_desc2str( ludp );
681 if ( newurl == NULL ) {
682 /* FIXME: leave attr untouched
683 * even if ldap_url_desc2str failed...
688 LBER_FREE( a_vals[ i ].bv_val );
689 ber_str2bv( newurl, 0, 1, &a_vals[ i ] );
691 ludp->lud_dn = olddn.bv_val;
696 ldap_free_urldesc( ludp );
703 * I don't like this much, but we need two different
704 * functions because different heap managers may be
705 * in use in back-ldap/meta to reduce the amount of
706 * calls to malloc routines, and some of the free()
707 * routines may be macros with args
718 assert( a_vals != NULL );
720 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
724 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
725 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
726 case LDAP_UNWILLING_TO_PERFORM:
728 * FIXME: need to check if it may be considered
729 * legal to trim values when adding/modifying;
730 * it should be when searching (e.g. ACLs).
732 ch_free( a_vals[i].bv_val );
734 a_vals[i] = a_vals[last];
736 BER_BVZERO( &a_vals[last] );
741 /* leave attr untouched if massage failed */
742 if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
743 ch_free( a_vals[i].bv_val );
754 ldap_dnattr_result_rewrite(
762 assert( a_vals != NULL );
764 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
768 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
769 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
770 case LDAP_UNWILLING_TO_PERFORM:
772 * FIXME: need to check if it may be considered
773 * legal to trim values when adding/modifying;
774 * it should be when searching (e.g. ACLs).
776 LBER_FREE( a_vals[i].bv_val );
778 a_vals[i] = a_vals[last];
780 BER_BVZERO( &a_vals[last] );
785 /* leave attr untouched if massage failed */
786 if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
787 LBER_FREE( a_vals[i].bv_val );