1 /* map.c - ldap backend mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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", STRLENOF("objectclass"), 1, &mapping[0].src);
101 ber_dupbv( &mapping[0].dst, &mapping[0].src );
102 mapping[1].src = mapping[0].src;
103 mapping[1].dst = mapping[0].dst;
105 avl_insert( &lm->map, (caddr_t)&mapping[0],
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 ) {
129 *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
131 return map->drop_missing;
138 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
141 struct ldapmapping *mapping;
143 /* map->map may be NULL when mapping is configured,
144 * but map->remap can't */
145 if ( map->remap == NULL ) {
151 ( void )ldap_back_mapping( map, s, &mapping, remap );
152 if ( mapping != NULL ) {
153 if ( !BER_BVISNULL( &mapping->dst ) ) {
159 if ( !map->drop_missing ) {
166 struct ldapmap *at_map,
174 struct berval mapped;
177 *mapped_attrs = NULL;
181 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
184 na = (char **)ch_calloc( i + 1, sizeof(char *) );
186 *mapped_attrs = NULL;
187 return LDAP_NO_MEMORY;
190 for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
191 ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
192 if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
193 na[j++] = mapped.bv_val;
196 if ( j == 0 && i != 0 ) {
197 na[j++] = LDAP_NO_ATTRS;
208 AttributeDescription *ad,
209 struct berval *mapped_attr,
210 struct berval *value,
211 struct berval *mapped_value,
217 ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap );
218 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
221 * FIXME: are we sure we need to search oc_map if at_map fails?
223 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap );
224 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
225 *mapped_attr = ad->ad_cname;
228 if ( dc->target->mt_rwmap.rwm_at.drop_missing ) {
232 *mapped_attr = ad->ad_cname;
235 if ( value == NULL ) {
239 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
243 #ifdef ENABLE_REWRITE
244 fdc.ctx = "searchFilterAttrDN";
247 switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
249 if ( vtmp.bv_val != value->bv_val ) {
254 case LDAP_UNWILLING_TO_PERFORM:
261 } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
262 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap );
263 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
271 filter_escape_value( &vtmp, mapped_value );
274 ber_memfree( vtmp.bv_val );
281 ldap_back_int_filter_map_rewrite(
293 /* better than nothing... */
294 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
295 ber_bvtf_false = BER_BVC( "(|)" ),
296 /* better than nothing... */
297 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
298 ber_bvtf_true = BER_BVC( "(&)" ),
300 /* no longer needed; preserved for completeness */
301 ber_bvundefined = BER_BVC( "(?=undefined)" ),
303 ber_bverror = BER_BVC( "(?=error)" ),
304 ber_bvunknown = BER_BVC( "(?=unknown)" ),
305 ber_bvnone = BER_BVC( "(?=none)" );
308 assert( fstr != NULL );
312 ber_dupbv( fstr, &ber_bvnone );
316 switch ( f->f_choice ) {
317 case LDAP_FILTER_EQUALITY:
318 if ( map_attr_value( dc, f->f_av_desc, &atmp,
319 &f->f_av_value, &vtmp, remap ) )
324 fstr->bv_len = atmp.bv_len + vtmp.bv_len
325 + ( sizeof("(=)") - 1 );
326 fstr->bv_val = malloc( fstr->bv_len + 1 );
328 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
329 atmp.bv_val, vtmp.bv_val );
331 ber_memfree( vtmp.bv_val );
335 if ( map_attr_value( dc, f->f_av_desc, &atmp,
336 &f->f_av_value, &vtmp, remap ) )
341 fstr->bv_len = atmp.bv_len + vtmp.bv_len
342 + ( sizeof("(>=)") - 1 );
343 fstr->bv_val = malloc( fstr->bv_len + 1 );
345 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
346 atmp.bv_val, vtmp.bv_val );
348 ber_memfree( vtmp.bv_val );
352 if ( map_attr_value( dc, f->f_av_desc, &atmp,
353 &f->f_av_value, &vtmp, remap ) )
358 fstr->bv_len = atmp.bv_len + vtmp.bv_len
359 + ( sizeof("(<=)") - 1 );
360 fstr->bv_val = malloc( fstr->bv_len + 1 );
362 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
363 atmp.bv_val, vtmp.bv_val );
365 ber_memfree( vtmp.bv_val );
368 case LDAP_FILTER_APPROX:
369 if ( map_attr_value( dc, f->f_av_desc, &atmp,
370 &f->f_av_value, &vtmp, remap ) )
375 fstr->bv_len = atmp.bv_len + vtmp.bv_len
376 + ( sizeof("(~=)") - 1 );
377 fstr->bv_val = malloc( fstr->bv_len + 1 );
379 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
380 atmp.bv_val, vtmp.bv_val );
382 ber_memfree( vtmp.bv_val );
385 case LDAP_FILTER_SUBSTRINGS:
386 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
387 NULL, NULL, remap ) )
392 /* cannot be a DN ... */
394 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
395 fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */
397 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
400 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
403 filter_escape_value( &f->f_sub_initial, &vtmp );
405 fstr->bv_len += vtmp.bv_len;
406 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
408 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
409 /* "(attr=" */ "%s*)",
412 ber_memfree( vtmp.bv_val );
415 if ( f->f_sub_any != NULL ) {
416 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
418 filter_escape_value( &f->f_sub_any[i], &vtmp );
420 fstr->bv_len += vtmp.bv_len + 1;
421 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
423 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
424 /* "(attr=[init]*[any*]" */ "%s*)",
426 ber_memfree( vtmp.bv_val );
430 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
433 filter_escape_value( &f->f_sub_final, &vtmp );
435 fstr->bv_len += vtmp.bv_len;
436 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
438 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
439 /* "(attr=[init*][any*]" */ "%s)",
442 ber_memfree( vtmp.bv_val );
447 case LDAP_FILTER_PRESENT:
448 if ( map_attr_value( dc, f->f_desc, &atmp,
449 NULL, NULL, remap ) )
454 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
455 fstr->bv_val = malloc( fstr->bv_len + 1 );
457 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
461 case LDAP_FILTER_AND:
463 case LDAP_FILTER_NOT:
464 fstr->bv_len = STRLENOF( "(%)" );
465 fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128? */
467 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
468 f->f_choice == LDAP_FILTER_AND ? '&' :
469 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
471 for ( p = f->f_list; p != NULL; p = p->f_next ) {
476 rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap );
477 if ( rc != LDAP_SUCCESS ) {
481 fstr->bv_len += vtmp.bv_len;
482 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
484 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
485 /*"("*/ "%s)", vtmp.bv_val );
487 ch_free( vtmp.bv_val );
492 case LDAP_FILTER_EXT:
493 if ( f->f_mr_desc ) {
494 if ( map_attr_value( dc, f->f_mr_desc, &atmp,
495 &f->f_mr_value, &vtmp, remap ) )
501 BER_BVSTR( &atmp, "" );
502 filter_escape_value( &f->f_mr_value, &vtmp );
505 /* FIXME: cleanup (less ?: operators...) */
506 fstr->bv_len = atmp.bv_len +
507 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
508 ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
509 vtmp.bv_len + ( STRLENOF( "(:=)" ) );
510 fstr->bv_val = malloc( fstr->bv_len + 1 );
512 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
514 f->f_mr_dnattrs ? ":dn" : "",
515 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
516 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
518 ber_memfree( vtmp.bv_val );
521 case SLAPD_FILTER_COMPUTED:
522 switch ( f->f_result ) {
523 case LDAP_COMPARE_FALSE:
524 /* FIXME: treat UNDEFINED as FALSE */
525 case SLAPD_COMPARE_UNDEFINED:
527 if ( META_BACK_TGT_T_F( dc->target ) ) {
528 tmp = &ber_bvtf_false;
534 case LDAP_COMPARE_TRUE:
535 if ( META_BACK_TGT_T_F( dc->target ) ) {
536 tmp = &ber_bvtf_true;
548 ber_dupbv( fstr, tmp );
552 ber_dupbv( fstr, &ber_bvunknown );
560 ldap_back_filter_map_rewrite(
569 static char *dmy = "";
571 rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap );
573 #ifdef ENABLE_REWRITE
574 if ( rc != LDAP_SUCCESS ) {
581 fdc.ctx = "searchFilter";
583 switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx,
584 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ),
585 fdc.conn, &fstr->bv_val ) )
587 case REWRITE_REGEXEC_OK:
588 if ( !BER_BVISNULL( fstr ) ) {
589 fstr->bv_len = strlen( fstr->bv_val );
594 Debug( LDAP_DEBUG_ARGS,
595 "[rw] %s: \"%s\" -> \"%s\"\n",
596 fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val,
597 BER_BVISNULL( fstr ) ? "" : fstr->bv_val );
601 case REWRITE_REGEXEC_UNWILLING:
603 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
604 fdc.rs->sr_text = "Operation not allowed";
606 rc = LDAP_UNWILLING_TO_PERFORM;
609 case REWRITE_REGEXEC_ERR:
611 fdc.rs->sr_err = LDAP_OTHER;
612 fdc.rs->sr_text = "Rewrite error";
618 if ( fstr->bv_val == dmy ) {
621 #endif /* ENABLE_REWRITE */
627 ldap_back_referral_result_rewrite(
634 assert( dc != NULL );
635 assert( a_vals != NULL );
637 for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
641 for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
647 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
648 if ( rc != LDAP_URL_SUCCESS ) {
649 /* leave attr untouched if massage failed */
653 /* FIXME: URLs like "ldap:///dc=suffix" if passed
654 * thru ldap_url_parse() and ldap_url_desc2str()
655 * get rewritten as "ldap:///dc=suffix??base";
656 * we don't want this to occur... */
657 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
658 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
661 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
663 rc = ldap_back_dn_massage( dc, &olddn, &dn );
665 case LDAP_UNWILLING_TO_PERFORM:
667 * FIXME: need to check if it may be considered
668 * legal to trim values when adding/modifying;
669 * it should be when searching (e.g. ACLs).
671 LBER_FREE( a_vals[ i ].bv_val );
673 a_vals[ i ] = a_vals[ last ];
675 BER_BVZERO( &a_vals[ last ] );
681 /* leave attr untouched if massage failed */
682 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val )
686 ludp->lud_dn = dn.bv_val;
687 newurl = ldap_url_desc2str( ludp );
689 if ( newurl == NULL ) {
690 /* FIXME: leave attr untouched
691 * even if ldap_url_desc2str failed...
696 LBER_FREE( a_vals[ i ].bv_val );
697 ber_str2bv( newurl, 0, 1, &a_vals[ i ] );
699 ludp->lud_dn = olddn.bv_val;
704 ldap_free_urldesc( ludp );
711 * I don't like this much, but we need two different
712 * functions because different heap managers may be
713 * in use in back-ldap/meta to reduce the amount of
714 * calls to malloc routines, and some of the free()
715 * routines may be macros with args
726 assert( a_vals != NULL );
728 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
732 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
733 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
734 case LDAP_UNWILLING_TO_PERFORM:
736 * FIXME: need to check if it may be considered
737 * legal to trim values when adding/modifying;
738 * it should be when searching (e.g. ACLs).
740 ch_free( a_vals[i].bv_val );
742 a_vals[i] = a_vals[last];
744 BER_BVZERO( &a_vals[last] );
749 /* leave attr untouched if massage failed */
750 if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
751 ch_free( a_vals[i].bv_val );
762 ldap_dnattr_result_rewrite(
770 assert( a_vals != NULL );
772 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
776 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
777 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
778 case LDAP_UNWILLING_TO_PERFORM:
780 * FIXME: need to check if it may be considered
781 * legal to trim values when adding/modifying;
782 * it should be when searching (e.g. ACLs).
784 LBER_FREE( a_vals[i].bv_val );
786 a_vals[i] = a_vals[last];
788 BER_BVZERO( &a_vals[last] );
793 /* leave attr untouched if massage failed */
794 if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
795 LBER_FREE( a_vals[i].bv_val );