1 /* rwmmap.c - rewrite/mapping routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2004 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
30 #include <ac/string.h>
31 #include <ac/socket.h>
36 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
40 rwm_mapping_cmp ( const void *c1, const void *c2 )
42 struct ldapmapping *map1 = (struct ldapmapping *)c1;
43 struct ldapmapping *map2 = (struct ldapmapping *)c2;
44 int rc = map1->src.bv_len - map2->src.bv_len;
46 return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
50 rwm_mapping_dup ( void *c1, void *c2 )
52 struct ldapmapping *map1 = (struct ldapmapping *)c1;
53 struct ldapmapping *map2 = (struct ldapmapping *)c2;
55 return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
59 rwm_map_init ( struct ldapmap *lm, struct ldapmapping **m )
61 struct ldapmapping *mapping;
67 mapping = (struct ldapmapping *)ch_calloc( 2,
68 sizeof( struct ldapmapping ) );
69 if ( mapping == NULL ) {
73 ber_str2bv( "objectclass", sizeof("objectclass")-1, 1, &mapping->src);
74 ber_dupbv( &mapping->dst, &mapping->src );
75 mapping[1].src = mapping->src;
76 mapping[1].dst = mapping->dst;
78 avl_insert( &lm->map, (caddr_t)mapping,
79 rwm_mapping_cmp, rwm_mapping_dup );
80 avl_insert( &lm->remap, (caddr_t)&mapping[1],
81 rwm_mapping_cmp, rwm_mapping_dup );
86 rwm_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
90 struct ldapmapping *mapping, fmapping;
92 if (remap == BACKLDAP_REMAP)
100 mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, rwm_mapping_cmp );
101 if (mapping != NULL) {
102 if ( mapping->dst.bv_val )
107 if (!map->drop_missing)
115 struct ldapmap *at_map,
123 struct berval mapped;
126 *mapped_attrs = NULL;
130 for (i = 0; an[i].an_name.bv_val; i++) {
134 na = (char **)ch_calloc( i + 1, sizeof(char *) );
136 *mapped_attrs = NULL;
137 return LDAP_NO_MEMORY;
140 for (i = j = 0; an[i].an_name.bv_val; i++) {
141 rwm_map(at_map, &an[i].an_name, &mapped, remap);
142 if (mapped.bv_val != NULL && mapped.bv_val != '\0')
143 na[j++] = mapped.bv_val;
145 if (j == 0 && i != 0)
146 na[j++] = LDAP_NO_ATTRS;
156 AttributeDescription *ad,
157 struct berval *mapped_attr,
158 struct berval *value,
159 struct berval *mapped_value,
165 rwm_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
166 if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0') {
168 * FIXME: are we sure we need to search oc_map if at_map fails?
170 rwm_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
171 if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0' ) {
172 *mapped_attr = ad->ad_cname;
176 if ( value == NULL ) {
180 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
184 #ifdef ENABLE_REWRITE
185 fdc.ctx = "searchFilter";
188 switch ( rwm_dn_massage( &fdc, value, &vtmp ) ) {
190 if ( vtmp.bv_val != value->bv_val ) {
195 case LDAP_UNWILLING_TO_PERFORM:
202 } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
203 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
204 if ( vtmp.bv_val == NULL || vtmp.bv_val[0] == '\0' ) {
212 filter_escape_value( &vtmp, mapped_value );
215 ber_memfree( vtmp.bv_val );
222 rwm_filter_map_rewrite(
235 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
239 switch ( f->f_choice ) {
240 case LDAP_FILTER_EQUALITY:
241 if ( map_attr_value( dc, f->f_av_desc, &atmp,
242 &f->f_av_value, &vtmp, remap ) )
247 fstr->bv_len = atmp.bv_len + vtmp.bv_len
248 + ( sizeof("(=)") - 1 );
249 fstr->bv_val = malloc( fstr->bv_len + 1 );
251 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
252 atmp.bv_val, vtmp.bv_val );
254 ber_memfree( vtmp.bv_val );
258 if ( map_attr_value( dc, f->f_av_desc, &atmp,
259 &f->f_av_value, &vtmp, remap ) )
264 fstr->bv_len = atmp.bv_len + vtmp.bv_len
265 + ( sizeof("(>=)") - 1 );
266 fstr->bv_val = malloc( fstr->bv_len + 1 );
268 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
269 atmp.bv_val, vtmp.bv_val );
271 ber_memfree( vtmp.bv_val );
275 if ( map_attr_value( dc, f->f_av_desc, &atmp,
276 &f->f_av_value, &vtmp, remap ) )
281 fstr->bv_len = atmp.bv_len + vtmp.bv_len
282 + ( sizeof("(<=)") - 1 );
283 fstr->bv_val = malloc( fstr->bv_len + 1 );
285 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
286 atmp.bv_val, vtmp.bv_val );
288 ber_memfree( vtmp.bv_val );
291 case LDAP_FILTER_APPROX:
292 if ( map_attr_value( dc, f->f_av_desc, &atmp,
293 &f->f_av_value, &vtmp, remap ) )
298 fstr->bv_len = atmp.bv_len + vtmp.bv_len
299 + ( sizeof("(~=)") - 1 );
300 fstr->bv_val = malloc( fstr->bv_len + 1 );
302 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
303 atmp.bv_val, vtmp.bv_val );
305 ber_memfree( vtmp.bv_val );
308 case LDAP_FILTER_SUBSTRINGS:
309 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
310 NULL, NULL, remap ) )
315 /* cannot be a DN ... */
317 fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
318 fstr->bv_val = malloc( fstr->bv_len + 128 );
320 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
323 if ( f->f_sub_initial.bv_val != NULL ) {
326 filter_escape_value( &f->f_sub_initial, &vtmp );
328 fstr->bv_len += vtmp.bv_len;
329 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
331 snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
332 /* "(attr=" */ "%s*)",
335 ber_memfree( vtmp.bv_val );
338 if ( f->f_sub_any != NULL ) {
339 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
341 filter_escape_value( &f->f_sub_any[i], &vtmp );
343 fstr->bv_len += vtmp.bv_len + 1;
344 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
346 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
347 /* "(attr=[init]*[any*]" */ "%s*)",
349 ber_memfree( vtmp.bv_val );
353 if ( f->f_sub_final.bv_val != NULL ) {
356 filter_escape_value( &f->f_sub_final, &vtmp );
358 fstr->bv_len += vtmp.bv_len;
359 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
361 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
362 /* "(attr=[init*][any*]" */ "%s)",
365 ber_memfree( vtmp.bv_val );
370 case LDAP_FILTER_PRESENT:
371 if ( map_attr_value( dc, f->f_desc, &atmp,
372 NULL, NULL, remap ) )
377 fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
378 fstr->bv_val = malloc( fstr->bv_len + 1 );
380 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
384 case LDAP_FILTER_AND:
386 case LDAP_FILTER_NOT:
387 fstr->bv_len = sizeof("(%)") - 1;
388 fstr->bv_val = malloc( fstr->bv_len + 128 );
390 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
391 f->f_choice == LDAP_FILTER_AND ? '&' :
392 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
394 for ( p = f->f_list; p != NULL; p = p->f_next ) {
397 if ( rwm_filter_map_rewrite( dc, p, &vtmp, remap ) )
402 fstr->bv_len += vtmp.bv_len;
403 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
405 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
406 /*"("*/ "%s)", vtmp.bv_val );
408 ch_free( vtmp.bv_val );
413 case LDAP_FILTER_EXT: {
414 if ( f->f_mr_desc ) {
415 if ( map_attr_value( dc, f->f_mr_desc, &atmp,
416 &f->f_mr_value, &vtmp, remap ) )
425 filter_escape_value( &f->f_mr_value, &vtmp );
429 fstr->bv_len = atmp.bv_len +
430 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
431 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
432 vtmp.bv_len + ( sizeof("(:=)") - 1 );
433 fstr->bv_val = malloc( fstr->bv_len + 1 );
435 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
437 f->f_mr_dnattrs ? ":dn" : "",
438 f->f_mr_rule_text.bv_len ? ":" : "",
439 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
441 ber_memfree( vtmp.bv_val );
444 case SLAPD_FILTER_COMPUTED:
446 f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
447 f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
448 f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
450 f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
451 f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
452 f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
453 sizeof("(?=error)")-1,
458 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
466 * I don't like this much, but we need two different
467 * functions because different heap managers may be
468 * in use in back-ldap/meta to reduce the amount of
469 * calls to malloc routines, and some of the free()
470 * routines may be macros with args
481 for ( last = 0; a_vals[last].bv_val != NULL; last++ );
484 for ( i = 0; a_vals[i].bv_val != NULL; i++ ) {
485 switch ( rwm_dn_massage( dc, &a_vals[i], &bv ) ) {
486 case LDAP_UNWILLING_TO_PERFORM:
488 * FIXME: need to check if it may be considered
489 * legal to trim values when adding/modifying;
490 * it should be when searching (e.g. ACLs).
492 ch_free( a_vals[i].bv_val );
494 a_vals[i] = a_vals[last];
496 a_vals[last].bv_len = 0;
497 a_vals[last].bv_val = NULL;
502 /* leave attr untouched if massage failed */
503 if ( bv.bv_val && bv.bv_val != a_vals[i].bv_val ) {
504 ch_free( a_vals[i].bv_val );
515 rwm_dnattr_result_rewrite(
523 for ( last = 0; a_vals[last].bv_val; last++ );
526 for ( i = 0; a_vals[i].bv_val; i++ ) {
527 switch ( rwm_dn_massage( dc, &a_vals[i], &bv ) ) {
528 case LDAP_UNWILLING_TO_PERFORM:
530 * FIXME: need to check if it may be considered
531 * legal to trim values when adding/modifying;
532 * it should be when searching (e.g. ACLs).
534 LBER_FREE( &a_vals[i].bv_val );
536 a_vals[i] = a_vals[last];
538 a_vals[last].bv_val = NULL;
539 a_vals[last].bv_len = 0;
544 /* leave attr untouched if massage failed */
545 if ( bv.bv_val && a_vals[i].bv_val != bv.bv_val ) {
546 LBER_FREE( a_vals[i].bv_val );
557 rwm_mapping_free( void *v_mapping )
559 struct ldapmapping *mapping = v_mapping;
560 ch_free( mapping->src.bv_val );
561 ch_free( mapping->dst.bv_val );
565 #endif /* SLAPD_OVER_RWM */