1 /* map.c - ldap backend mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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)" );
301 ber_dupbv( fstr, &ber_bvnone );
305 switch ( f->f_choice ) {
306 case LDAP_FILTER_EQUALITY:
307 if ( map_attr_value( dc, f->f_av_desc, &atmp,
308 &f->f_av_value, &vtmp, remap ) )
313 fstr->bv_len = atmp.bv_len + vtmp.bv_len
314 + ( sizeof("(=)") - 1 );
315 fstr->bv_val = malloc( fstr->bv_len + 1 );
317 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
318 atmp.bv_val, vtmp.bv_val );
320 ber_memfree( vtmp.bv_val );
324 if ( map_attr_value( dc, f->f_av_desc, &atmp,
325 &f->f_av_value, &vtmp, remap ) )
330 fstr->bv_len = atmp.bv_len + vtmp.bv_len
331 + ( sizeof("(>=)") - 1 );
332 fstr->bv_val = malloc( fstr->bv_len + 1 );
334 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
335 atmp.bv_val, vtmp.bv_val );
337 ber_memfree( vtmp.bv_val );
341 if ( map_attr_value( dc, f->f_av_desc, &atmp,
342 &f->f_av_value, &vtmp, remap ) )
347 fstr->bv_len = atmp.bv_len + vtmp.bv_len
348 + ( sizeof("(<=)") - 1 );
349 fstr->bv_val = malloc( fstr->bv_len + 1 );
351 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
352 atmp.bv_val, vtmp.bv_val );
354 ber_memfree( vtmp.bv_val );
357 case LDAP_FILTER_APPROX:
358 if ( map_attr_value( dc, f->f_av_desc, &atmp,
359 &f->f_av_value, &vtmp, remap ) )
364 fstr->bv_len = atmp.bv_len + vtmp.bv_len
365 + ( sizeof("(~=)") - 1 );
366 fstr->bv_val = malloc( fstr->bv_len + 1 );
368 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
369 atmp.bv_val, vtmp.bv_val );
371 ber_memfree( vtmp.bv_val );
374 case LDAP_FILTER_SUBSTRINGS:
375 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
376 NULL, NULL, remap ) )
381 /* cannot be a DN ... */
383 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
384 fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */
386 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
389 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
392 filter_escape_value( &f->f_sub_initial, &vtmp );
394 fstr->bv_len += vtmp.bv_len;
395 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
397 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
398 /* "(attr=" */ "%s*)",
401 ber_memfree( vtmp.bv_val );
404 if ( f->f_sub_any != NULL ) {
405 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
407 filter_escape_value( &f->f_sub_any[i], &vtmp );
409 fstr->bv_len += vtmp.bv_len + 1;
410 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
412 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
413 /* "(attr=[init]*[any*]" */ "%s*)",
415 ber_memfree( vtmp.bv_val );
419 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
422 filter_escape_value( &f->f_sub_final, &vtmp );
424 fstr->bv_len += vtmp.bv_len;
425 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
427 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
428 /* "(attr=[init*][any*]" */ "%s)",
431 ber_memfree( vtmp.bv_val );
436 case LDAP_FILTER_PRESENT:
437 if ( map_attr_value( dc, f->f_desc, &atmp,
438 NULL, NULL, remap ) )
443 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
444 fstr->bv_val = malloc( fstr->bv_len + 1 );
446 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
450 case LDAP_FILTER_AND:
452 case LDAP_FILTER_NOT:
453 fstr->bv_len = STRLENOF( "(%)" );
454 fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128? */
456 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
457 f->f_choice == LDAP_FILTER_AND ? '&' :
458 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
460 for ( p = f->f_list; p != NULL; p = p->f_next ) {
465 rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap );
466 if ( rc != LDAP_SUCCESS ) {
470 fstr->bv_len += vtmp.bv_len;
471 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
473 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
474 /*"("*/ "%s)", vtmp.bv_val );
476 ch_free( vtmp.bv_val );
481 case LDAP_FILTER_EXT:
482 if ( f->f_mr_desc ) {
483 if ( map_attr_value( dc, f->f_mr_desc, &atmp,
484 &f->f_mr_value, &vtmp, remap ) )
490 BER_BVSTR( &atmp, "" );
491 filter_escape_value( &f->f_mr_value, &vtmp );
494 /* FIXME: cleanup (less ?: operators...) */
495 fstr->bv_len = atmp.bv_len +
496 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
497 ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
498 vtmp.bv_len + ( STRLENOF( "(:=)" ) );
499 fstr->bv_val = malloc( fstr->bv_len + 1 );
501 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
503 f->f_mr_dnattrs ? ":dn" : "",
504 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
505 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
507 ber_memfree( vtmp.bv_val );
510 case SLAPD_FILTER_COMPUTED:
511 switch ( f->f_result ) {
512 case LDAP_COMPARE_FALSE:
513 /* FIXME: treat UNDEFINED as FALSE */
514 case SLAPD_COMPARE_UNDEFINED:
516 if ( dc->target->mt_flags & LDAP_BACK_F_SUPPORT_T_F ) {
517 tmp = &ber_bvtf_false;
523 case LDAP_COMPARE_TRUE:
524 if ( dc->target->mt_flags & LDAP_BACK_F_SUPPORT_T_F ) {
525 tmp = &ber_bvtf_true;
537 ber_dupbv( fstr, tmp );
541 ber_dupbv( fstr, &ber_bvunknown );
549 ldap_back_filter_map_rewrite(
558 static char *dmy = "";
560 rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap );
562 #ifdef ENABLE_REWRITE
563 if ( rc != LDAP_SUCCESS ) {
570 fdc.ctx = "searchFilter";
572 switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx,
573 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ),
574 fdc.conn, &fstr->bv_val ) )
576 case REWRITE_REGEXEC_OK:
577 if ( !BER_BVISNULL( fstr ) ) {
578 fstr->bv_len = strlen( fstr->bv_val );
583 Debug( LDAP_DEBUG_ARGS,
584 "[rw] %s: \"%s\" -> \"%s\"\n",
585 fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val,
586 BER_BVISNULL( fstr ) ? "" : fstr->bv_val );
590 case REWRITE_REGEXEC_UNWILLING:
592 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
593 fdc.rs->sr_text = "Operation not allowed";
595 rc = LDAP_UNWILLING_TO_PERFORM;
598 case REWRITE_REGEXEC_ERR:
600 fdc.rs->sr_err = LDAP_OTHER;
601 fdc.rs->sr_text = "Rewrite error";
607 if ( fstr->bv_val == dmy ) {
610 #endif /* ENABLE_REWRITE */
616 ldap_back_referral_result_rewrite(
623 assert( dc != NULL );
624 assert( a_vals != NULL );
626 for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
630 for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
636 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
637 if ( rc != LDAP_URL_SUCCESS ) {
638 /* leave attr untouched if massage failed */
642 /* FIXME: URLs like "ldap:///dc=suffix" if passed
643 * thru ldap_url_parse() and ldap_url_desc2str()
644 * get rewritten as "ldap:///dc=suffix??base";
645 * we don't want this to occur... */
646 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
647 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
650 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
652 rc = ldap_back_dn_massage( dc, &olddn, &dn );
654 case LDAP_UNWILLING_TO_PERFORM:
656 * FIXME: need to check if it may be considered
657 * legal to trim values when adding/modifying;
658 * it should be when searching (e.g. ACLs).
660 LBER_FREE( a_vals[ i ].bv_val );
662 a_vals[ i ] = a_vals[ last ];
664 BER_BVZERO( &a_vals[ last ] );
670 /* leave attr untouched if massage failed */
671 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val )
675 ludp->lud_dn = dn.bv_val;
676 newurl = ldap_url_desc2str( ludp );
678 if ( newurl == NULL ) {
679 /* FIXME: leave attr untouched
680 * even if ldap_url_desc2str failed...
685 LBER_FREE( a_vals[ i ].bv_val );
686 ber_str2bv( newurl, 0, 1, &a_vals[ i ] );
688 ludp->lud_dn = olddn.bv_val;
693 ldap_free_urldesc( ludp );
700 * I don't like this much, but we need two different
701 * functions because different heap managers may be
702 * in use in back-ldap/meta to reduce the amount of
703 * calls to malloc routines, and some of the free()
704 * routines may be macros with args
715 assert( a_vals != NULL );
717 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
721 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
722 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
723 case LDAP_UNWILLING_TO_PERFORM:
725 * FIXME: need to check if it may be considered
726 * legal to trim values when adding/modifying;
727 * it should be when searching (e.g. ACLs).
729 ch_free( a_vals[i].bv_val );
731 a_vals[i] = a_vals[last];
733 BER_BVZERO( &a_vals[last] );
738 /* leave attr untouched if massage failed */
739 if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
740 ch_free( a_vals[i].bv_val );
751 ldap_dnattr_result_rewrite(
759 assert( a_vals != NULL );
761 for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
765 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
766 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
767 case LDAP_UNWILLING_TO_PERFORM:
769 * FIXME: need to check if it may be considered
770 * legal to trim values when adding/modifying;
771 * it should be when searching (e.g. ACLs).
773 LBER_FREE( a_vals[i].bv_val );
775 a_vals[i] = a_vals[last];
777 BER_BVZERO( &a_vals[last] );
782 /* leave attr untouched if massage failed */
783 if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
784 LBER_FREE( a_vals[i].bv_val );