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_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
117 struct ldapmapping *mapping, fmapping;
119 if (remap == BACKLDAP_REMAP)
127 mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
128 if (mapping != NULL) {
129 if ( mapping->dst.bv_val )
134 if (!map->drop_missing)
142 struct ldapmap *at_map,
150 struct berval mapped;
153 *mapped_attrs = NULL;
157 for (i = 0; an[i].an_name.bv_val; i++) {
161 na = (char **)ch_calloc( i + 1, sizeof(char *) );
163 *mapped_attrs = NULL;
164 return LDAP_NO_MEMORY;
167 for (i = j = 0; an[i].an_name.bv_val; i++) {
168 ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
169 if (mapped.bv_val != NULL && mapped.bv_val != '\0')
170 na[j++] = mapped.bv_val;
172 if (j == 0 && i != 0)
173 na[j++] = LDAP_NO_ATTRS;
183 AttributeDescription *ad,
184 struct berval *mapped_attr,
185 struct berval *value,
186 struct berval *mapped_value,
192 ldap_back_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
193 if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0') {
195 * FIXME: are we sure we need to search oc_map if at_map fails?
197 ldap_back_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
198 if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0' ) {
199 *mapped_attr = ad->ad_cname;
203 if ( value == NULL ) {
207 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
211 #ifdef ENABLE_REWRITE
212 fdc.ctx = "searchFilterAttrDN";
215 switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
217 if ( vtmp.bv_val != value->bv_val ) {
222 case LDAP_UNWILLING_TO_PERFORM:
229 } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
230 ldap_back_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
231 if ( vtmp.bv_val == NULL || vtmp.bv_val[0] == '\0' ) {
239 filter_escape_value( &vtmp, mapped_value );
242 ber_memfree( vtmp.bv_val );
249 ldap_back_int_filter_map_rewrite(
262 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
266 switch ( f->f_choice ) {
267 case LDAP_FILTER_EQUALITY:
268 if ( map_attr_value( dc, f->f_av_desc, &atmp,
269 &f->f_av_value, &vtmp, remap ) )
274 fstr->bv_len = atmp.bv_len + vtmp.bv_len
275 + ( sizeof("(=)") - 1 );
276 fstr->bv_val = malloc( fstr->bv_len + 1 );
278 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
279 atmp.bv_val, vtmp.bv_val );
281 ber_memfree( vtmp.bv_val );
285 if ( map_attr_value( dc, f->f_av_desc, &atmp,
286 &f->f_av_value, &vtmp, remap ) )
291 fstr->bv_len = atmp.bv_len + vtmp.bv_len
292 + ( sizeof("(>=)") - 1 );
293 fstr->bv_val = malloc( fstr->bv_len + 1 );
295 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
296 atmp.bv_val, vtmp.bv_val );
298 ber_memfree( vtmp.bv_val );
302 if ( map_attr_value( dc, f->f_av_desc, &atmp,
303 &f->f_av_value, &vtmp, remap ) )
308 fstr->bv_len = atmp.bv_len + vtmp.bv_len
309 + ( sizeof("(<=)") - 1 );
310 fstr->bv_val = malloc( fstr->bv_len + 1 );
312 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
313 atmp.bv_val, vtmp.bv_val );
315 ber_memfree( vtmp.bv_val );
318 case LDAP_FILTER_APPROX:
319 if ( map_attr_value( dc, f->f_av_desc, &atmp,
320 &f->f_av_value, &vtmp, remap ) )
325 fstr->bv_len = atmp.bv_len + vtmp.bv_len
326 + ( sizeof("(~=)") - 1 );
327 fstr->bv_val = malloc( fstr->bv_len + 1 );
329 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
330 atmp.bv_val, vtmp.bv_val );
332 ber_memfree( vtmp.bv_val );
335 case LDAP_FILTER_SUBSTRINGS:
336 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
337 NULL, NULL, remap ) )
342 /* cannot be a DN ... */
344 fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
345 fstr->bv_val = malloc( fstr->bv_len + 128 );
347 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
350 if ( f->f_sub_initial.bv_val != NULL ) {
353 filter_escape_value( &f->f_sub_initial, &vtmp );
355 fstr->bv_len += vtmp.bv_len;
356 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
358 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
359 /* "(attr=" */ "%s*)",
362 ber_memfree( vtmp.bv_val );
365 if ( f->f_sub_any != NULL ) {
366 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
368 filter_escape_value( &f->f_sub_any[i], &vtmp );
370 fstr->bv_len += vtmp.bv_len + 1;
371 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
373 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
374 /* "(attr=[init]*[any*]" */ "%s*)",
376 ber_memfree( vtmp.bv_val );
380 if ( f->f_sub_final.bv_val != NULL ) {
383 filter_escape_value( &f->f_sub_final, &vtmp );
385 fstr->bv_len += vtmp.bv_len;
386 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
388 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
389 /* "(attr=[init*][any*]" */ "%s)",
392 ber_memfree( vtmp.bv_val );
397 case LDAP_FILTER_PRESENT:
398 if ( map_attr_value( dc, f->f_desc, &atmp,
399 NULL, NULL, remap ) )
404 fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
405 fstr->bv_val = malloc( fstr->bv_len + 1 );
407 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
411 case LDAP_FILTER_AND:
413 case LDAP_FILTER_NOT:
414 fstr->bv_len = sizeof("(%)") - 1;
415 fstr->bv_val = malloc( fstr->bv_len + 128 );
417 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
418 f->f_choice == LDAP_FILTER_AND ? '&' :
419 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
421 for ( p = f->f_list; p != NULL; p = p->f_next ) {
424 if ( ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap ) )
429 fstr->bv_len += vtmp.bv_len;
430 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
432 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
433 /*"("*/ "%s)", vtmp.bv_val );
435 ch_free( vtmp.bv_val );
440 case LDAP_FILTER_EXT: {
441 if ( f->f_mr_desc ) {
442 if ( map_attr_value( dc, f->f_mr_desc, &atmp,
443 &f->f_mr_value, &vtmp, remap ) )
452 filter_escape_value( &f->f_mr_value, &vtmp );
456 fstr->bv_len = atmp.bv_len +
457 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
458 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
459 vtmp.bv_len + ( sizeof("(:=)") - 1 );
460 fstr->bv_val = malloc( fstr->bv_len + 1 );
462 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
464 f->f_mr_dnattrs ? ":dn" : "",
465 f->f_mr_rule_text.bv_len ? ":" : "",
466 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
468 ber_memfree( vtmp.bv_val );
471 case SLAPD_FILTER_COMPUTED:
472 switch ( f->f_result ) {
473 case LDAP_COMPARE_FALSE:
474 ber_str2bv( "(?=false)", STRLENOF( "(?=false)" ), 1, fstr );
476 case LDAP_COMPARE_TRUE:
477 ber_str2bv( "(?=true)", STRLENOF( "(?=true)" ), 1, fstr );
479 case SLAPD_COMPARE_UNDEFINED:
480 ber_str2bv( "(?=undefined)", STRLENOF( "(?=undefined)" ), 1, fstr );
483 ber_str2bv( "(?=error)", STRLENOF( "(?=error)" ), 1, fstr );
489 ber_str2bv( "(?=unknown)", STRLENOF( "(?=unknown)" ), 1, fstr );
497 ldap_back_filter_map_rewrite(
507 rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap );
509 #ifdef ENABLE_REWRITE
510 if ( rc != LDAP_SUCCESS ) {
517 fdc.ctx = "searchFilter";
519 switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
520 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
521 fdc.conn, &fstr->bv_val ) )
523 case REWRITE_REGEXEC_OK:
524 if ( !BER_BVISNULL( fstr ) ) {
525 fstr->bv_len = strlen( fstr->bv_val );
530 Debug( LDAP_DEBUG_ARGS,
531 "[rw] %s: \"%s\" -> \"%s\"\n",
532 fdc.ctx, ftmp.bv_val, fstr->bv_val );
536 case REWRITE_REGEXEC_UNWILLING:
538 fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
539 fdc.rs->sr_text = "Operation not allowed";
541 rc = LDAP_UNWILLING_TO_PERFORM;
544 case REWRITE_REGEXEC_ERR:
546 fdc.rs->sr_err = LDAP_OTHER;
547 fdc.rs->sr_text = "Rewrite error";
552 #endif /* ENABLE_REWRITE */
558 ldap_back_referral_result_rewrite(
568 for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
572 for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
573 struct berval dn, olddn;
577 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
578 if ( rc != LDAP_URL_SUCCESS ) {
579 /* leave attr untouched if massage failed */
583 /* FIXME: URLs like "ldap:///dc=suffix" if passed
584 * thru ldap_url_parse() and ldap_url_desc2str()
585 * get rewritten as "ldap:///dc=suffix??base";
586 * we don't want this to occur... */
587 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
588 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
591 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
593 rc = ldap_back_dn_massage( dc, &olddn, &dn );
595 case LDAP_UNWILLING_TO_PERFORM:
597 * FIXME: need to check if it may be considered
598 * legal to trim values when adding/modifying;
599 * it should be when searching (e.g. ACLs).
601 LBER_FREE( a_vals[ i ].bv_val );
603 a_vals[ i ] = a_vals[ last ];
605 BER_BVZERO( &a_vals[ last ] );
611 /* leave attr untouched if massage failed */
612 if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val )
616 ludp->lud_dn = dn.bv_val;
617 newurl = ldap_url_desc2str( ludp );
618 if ( newurl == NULL ) {
619 /* FIXME: leave attr untouched
620 * even if ldap_url_desc2str failed...
625 LBER_FREE( a_vals[ i ].bv_val );
626 ber_str2bv( newurl, 0, 1, &a_vals[ i ] );
628 ludp->lud_dn = olddn.bv_val;
633 ldap_free_urldesc( ludp );
640 * I don't like this much, but we need two different
641 * functions because different heap managers may be
642 * in use in back-ldap/meta to reduce the amount of
643 * calls to malloc routines, and some of the free()
644 * routines may be macros with args
655 for ( last = 0; a_vals[last].bv_val != NULL; last++ );
658 for ( i = 0; a_vals[i].bv_val != NULL; i++ ) {
659 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
660 case LDAP_UNWILLING_TO_PERFORM:
662 * FIXME: need to check if it may be considered
663 * legal to trim values when adding/modifying;
664 * it should be when searching (e.g. ACLs).
666 ch_free( a_vals[i].bv_val );
668 a_vals[i] = a_vals[last];
670 a_vals[last].bv_len = 0;
671 a_vals[last].bv_val = NULL;
676 /* leave attr untouched if massage failed */
677 if ( bv.bv_val && bv.bv_val != a_vals[i].bv_val ) {
678 ch_free( a_vals[i].bv_val );
689 ldap_dnattr_result_rewrite(
697 for ( last = 0; a_vals[last].bv_val; last++ );
700 for ( i = 0; a_vals[i].bv_val; i++ ) {
701 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
702 case LDAP_UNWILLING_TO_PERFORM:
704 * FIXME: need to check if it may be considered
705 * legal to trim values when adding/modifying;
706 * it should be when searching (e.g. ACLs).
708 LBER_FREE( a_vals[i].bv_val );
710 a_vals[i] = a_vals[last];
712 BER_BVZERO( &a_vals[last] );
717 /* leave attr untouched if massage failed */
718 if ( bv.bv_val && a_vals[i].bv_val != bv.bv_val ) {
719 LBER_FREE( a_vals[i].bv_val );