]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/map.c
ITS#5959 just drop illegal values, keep remainder if any. fix a_numvals.
[openldap] / servers / slapd / back-meta / map.c
1 /* map.c - ldap backend mapping routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2009 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by the Howard Chu for inclusion
18  * in OpenLDAP Software and subsequently enhanced by Pierangelo
19  * Masarati.
20  */
21 /* This is an altered version */
22 /*
23  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
24  * 
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:
28  * 
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.
31  * 
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.
35  * 
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.
39  * 
40  * 4. This notice may not be removed or altered.
41  *
42  *
43  *
44  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
45  * 
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.
50  */
51
52 #include "portable.h"
53
54 #include <stdio.h>
55
56 #include <ac/string.h>
57 #include <ac/socket.h>
58
59 #include "slap.h"
60 #include "lutil.h"
61 #include "../back-ldap/back-ldap.h"
62 #include "back-meta.h"
63
64 #undef ldap_debug       /* silence a warning in ldap-int.h */
65 #include "../../../libraries/libldap/ldap-int.h"
66
67 int
68 mapping_cmp ( const void *c1, const void *c2 )
69 {
70         struct ldapmapping *map1 = (struct ldapmapping *)c1;
71         struct ldapmapping *map2 = (struct ldapmapping *)c2;
72         int rc = map1->src.bv_len - map2->src.bv_len;
73         if (rc) return rc;
74         return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) );
75 }
76
77 int
78 mapping_dup ( void *c1, void *c2 )
79 {
80         struct ldapmapping *map1 = (struct ldapmapping *)c1;
81         struct ldapmapping *map2 = (struct ldapmapping *)c2;
82
83         return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 );
84 }
85
86 void
87 ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
88 {
89         struct ldapmapping *mapping;
90
91         assert( m != NULL );
92
93         *m = NULL;
94
95         mapping = (struct ldapmapping *)ch_calloc( 2, 
96                         sizeof( struct ldapmapping ) );
97         if ( mapping == NULL ) {
98                 return;
99         }
100
101         ber_str2bv( "objectclass", STRLENOF("objectclass"), 1, &mapping[0].src);
102         ber_dupbv( &mapping[0].dst, &mapping[0].src );
103         mapping[1].src = mapping[0].src;
104         mapping[1].dst = mapping[0].dst;
105
106         avl_insert( &lm->map, (caddr_t)&mapping[0], 
107                         mapping_cmp, mapping_dup );
108         avl_insert( &lm->remap, (caddr_t)&mapping[1], 
109                         mapping_cmp, mapping_dup );
110         *m = mapping;
111 }
112
113 int
114 ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,
115         int remap )
116 {
117         Avlnode *tree;
118         struct ldapmapping fmapping;
119
120         assert( m != NULL );
121
122         /* let special attrnames slip through (ITS#5760) */
123         if ( bvmatch( s, slap_bv_no_attrs )
124                 || bvmatch( s, slap_bv_all_user_attrs )
125                 || bvmatch( s, slap_bv_all_operational_attrs ) )
126         {
127                 *m = NULL;
128                 return 0;
129         }
130
131         if ( remap == BACKLDAP_REMAP ) {
132                 tree = map->remap;
133
134         } else {
135                 tree = map->map;
136         }
137
138         fmapping.src = *s;
139         *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
140         if ( *m == NULL ) {
141                 return map->drop_missing;
142         }
143
144         return 0;
145 }
146
147 void
148 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
149         int remap )
150 {
151         struct ldapmapping *mapping;
152         int drop_missing;
153
154         /* map->map may be NULL when mapping is configured,
155          * but map->remap can't */
156         if ( map->remap == NULL ) {
157                 *bv = *s;
158                 return;
159         }
160
161         BER_BVZERO( bv );
162         drop_missing = ldap_back_mapping( map, s, &mapping, remap );
163         if ( mapping != NULL ) {
164                 if ( !BER_BVISNULL( &mapping->dst ) ) {
165                         *bv = mapping->dst;
166                 }
167                 return;
168         }
169
170         if ( !drop_missing ) {
171                 *bv = *s;
172         }
173 }
174
175 int
176 ldap_back_map_attrs(
177                 struct ldapmap *at_map,
178                 AttributeName *an,
179                 int remap,
180                 char ***mapped_attrs
181 )
182 {
183         int i, j;
184         char **na;
185         struct berval mapped;
186
187         if ( an == NULL ) {
188                 *mapped_attrs = NULL;
189                 return LDAP_SUCCESS;
190         }
191
192         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
193                 /*  */ ;
194
195         na = (char **)ch_calloc( i + 1, sizeof(char *) );
196         if ( na == NULL ) {
197                 *mapped_attrs = NULL;
198                 return LDAP_NO_MEMORY;
199         }
200
201         for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
202                 ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
203                 if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
204                         na[j++] = mapped.bv_val;
205                 }
206         }
207         if ( j == 0 && i != 0 ) {
208                 na[j++] = LDAP_NO_ATTRS;
209         }
210         na[j] = NULL;
211
212         *mapped_attrs = na;
213         return LDAP_SUCCESS;
214 }
215
216 int
217 map_attr_value(
218                 dncookie                *dc,
219                 AttributeDescription    *ad,
220                 struct berval           *mapped_attr,
221                 struct berval           *value,
222                 struct berval           *mapped_value,
223                 int                     remap )
224 {
225         struct berval           vtmp;
226         int                     freeval = 0;
227
228         ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap );
229         if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
230 #if 0
231                 /*
232                  * FIXME: are we sure we need to search oc_map if at_map fails?
233                  */
234                 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap );
235                 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
236                         *mapped_attr = ad->ad_cname;
237                 }
238 #endif
239                 if ( dc->target->mt_rwmap.rwm_at.drop_missing ) {
240                         return -1;
241                 }
242
243                 *mapped_attr = ad->ad_cname;
244         }
245
246         if ( value == NULL ) {
247                 return 0;
248         }
249
250         if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
251         {
252                 dncookie fdc = *dc;
253
254 #ifdef ENABLE_REWRITE
255                 fdc.ctx = "searchFilterAttrDN";
256 #endif
257
258                 switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
259                 case LDAP_SUCCESS:
260                         if ( vtmp.bv_val != value->bv_val ) {
261                                 freeval = 1;
262                         }
263                         break;
264                 
265                 case LDAP_UNWILLING_TO_PERFORM:
266                         return -1;
267
268                 case LDAP_OTHER:
269                         return -1;
270                 }
271
272         } else if ( ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) {
273                 if ( ad->ad_type->sat_equality->smr_normalize(
274                         (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
275                         NULL, NULL, value, &vtmp, NULL ) )
276                 {
277                         return -1;
278                 }
279                 freeval = 1;
280
281         } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
282                 ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap );
283                 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
284                         vtmp = *value;
285                 }
286                 
287         } else {
288                 vtmp = *value;
289         }
290
291         filter_escape_value( &vtmp, mapped_value );
292
293         if ( freeval ) {
294                 ber_memfree( vtmp.bv_val );
295         }
296         
297         return 0;
298 }
299
300 static int
301 ldap_back_int_filter_map_rewrite(
302                 dncookie                *dc,
303                 Filter                  *f,
304                 struct berval           *fstr,
305                 int                     remap )
306 {
307         int             i;
308         Filter          *p;
309         struct berval   atmp,
310                         vtmp,
311                         *tmp;
312         static struct berval
313                         /* better than nothing... */
314                         ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
315                         ber_bvtf_false = BER_BVC( "(|)" ),
316                         /* better than nothing... */
317                         ber_bvtrue = BER_BVC( "(objectClass=*)" ),
318                         ber_bvtf_true = BER_BVC( "(&)" ),
319 #if 0
320                         /* no longer needed; preserved for completeness */
321                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
322 #endif
323                         ber_bverror = BER_BVC( "(?=error)" ),
324                         ber_bvunknown = BER_BVC( "(?=unknown)" ),
325                         ber_bvnone = BER_BVC( "(?=none)" );
326         ber_len_t       len;
327
328         assert( fstr != NULL );
329         BER_BVZERO( fstr );
330
331         if ( f == NULL ) {
332                 ber_dupbv( fstr, &ber_bvnone );
333                 return LDAP_OTHER;
334         }
335
336         switch ( ( f->f_choice & SLAPD_FILTER_MASK ) ) {
337         case LDAP_FILTER_EQUALITY:
338                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
339                                         &f->f_av_value, &vtmp, remap ) )
340                 {
341                         goto computed;
342                 }
343
344                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
345                         + ( sizeof("(=)") - 1 );
346                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
347
348                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
349                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
350
351                 ber_memfree( vtmp.bv_val );
352                 break;
353
354         case LDAP_FILTER_GE:
355                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
356                                         &f->f_av_value, &vtmp, remap ) )
357                 {
358                         goto computed;
359                 }
360
361                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
362                         + ( sizeof("(>=)") - 1 );
363                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
364
365                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
366                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
367
368                 ber_memfree( vtmp.bv_val );
369                 break;
370
371         case LDAP_FILTER_LE:
372                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
373                                         &f->f_av_value, &vtmp, remap ) )
374                 {
375                         goto computed;
376                 }
377
378                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
379                         + ( sizeof("(<=)") - 1 );
380                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
381
382                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
383                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
384
385                 ber_memfree( vtmp.bv_val );
386                 break;
387
388         case LDAP_FILTER_APPROX:
389                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
390                                         &f->f_av_value, &vtmp, remap ) )
391                 {
392                         goto computed;
393                 }
394
395                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
396                         + ( sizeof("(~=)") - 1 );
397                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
398
399                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
400                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
401
402                 ber_memfree( vtmp.bv_val );
403                 break;
404
405         case LDAP_FILTER_SUBSTRINGS:
406                 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
407                                         NULL, NULL, remap ) )
408                 {
409                         goto computed;
410                 }
411
412                 /* cannot be a DN ... */
413
414                 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
415                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */
416
417                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
418                         atmp.bv_val );
419
420                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
421                         len = fstr->bv_len;
422
423                         filter_escape_value( &f->f_sub_initial, &vtmp );
424
425                         fstr->bv_len += vtmp.bv_len;
426                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
427
428                         snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
429                                 /* "(attr=" */ "%s*)",
430                                 vtmp.bv_len ? vtmp.bv_val : "" );
431
432                         ber_memfree( vtmp.bv_val );
433                 }
434
435                 if ( f->f_sub_any != NULL ) {
436                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
437                                 len = fstr->bv_len;
438                                 filter_escape_value( &f->f_sub_any[i], &vtmp );
439
440                                 fstr->bv_len += vtmp.bv_len + 1;
441                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
442
443                                 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
444                                         /* "(attr=[init]*[any*]" */ "%s*)",
445                                         vtmp.bv_len ? vtmp.bv_val : "" );
446                                 ber_memfree( vtmp.bv_val );
447                         }
448                 }
449
450                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
451                         len = fstr->bv_len;
452
453                         filter_escape_value( &f->f_sub_final, &vtmp );
454
455                         fstr->bv_len += vtmp.bv_len;
456                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
457
458                         snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
459                                 /* "(attr=[init*][any*]" */ "%s)",
460                                 vtmp.bv_len ? vtmp.bv_val : "" );
461
462                         ber_memfree( vtmp.bv_val );
463                 }
464
465                 break;
466
467         case LDAP_FILTER_PRESENT:
468                 if ( map_attr_value( dc, f->f_desc, &atmp,
469                                         NULL, NULL, remap ) )
470                 {
471                         goto computed;
472                 }
473
474                 fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
475                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
476
477                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
478                         atmp.bv_val );
479                 break;
480
481         case LDAP_FILTER_AND:
482         case LDAP_FILTER_OR:
483         case LDAP_FILTER_NOT:
484                 fstr->bv_len = STRLENOF( "(%)" );
485                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 ); /* FIXME: why 128? */
486
487                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
488                         f->f_choice == LDAP_FILTER_AND ? '&' :
489                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
490
491                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
492                         int     rc;
493
494                         len = fstr->bv_len;
495
496                         rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap );
497                         if ( rc != LDAP_SUCCESS ) {
498                                 return rc;
499                         }
500                         
501                         fstr->bv_len += vtmp.bv_len;
502                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
503
504                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
505                                 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
506
507                         ch_free( vtmp.bv_val );
508                 }
509
510                 break;
511
512         case LDAP_FILTER_EXT:
513                 if ( f->f_mr_desc ) {
514                         if ( map_attr_value( dc, f->f_mr_desc, &atmp,
515                                                 &f->f_mr_value, &vtmp, remap ) )
516                         {
517                                 goto computed;
518                         }
519
520                 } else {
521                         BER_BVSTR( &atmp, "" );
522                         filter_escape_value( &f->f_mr_value, &vtmp );
523                 }
524
525                 /* FIXME: cleanup (less ?: operators...) */
526                 fstr->bv_len = atmp.bv_len +
527                         ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
528                         ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
529                         vtmp.bv_len + ( STRLENOF( "(:=)" ) );
530                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
531
532                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
533                         atmp.bv_val,
534                         f->f_mr_dnattrs ? ":dn" : "",
535                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
536                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
537                         vtmp.bv_len ? vtmp.bv_val : "" );
538                 ber_memfree( vtmp.bv_val );
539                 break;
540
541         case SLAPD_FILTER_COMPUTED:
542                 switch ( f->f_result ) {
543                 /* FIXME: treat UNDEFINED as FALSE */
544                 case SLAPD_COMPARE_UNDEFINED:
545 computed:;
546                         if ( META_BACK_TGT_NOUNDEFFILTER( dc->target ) ) {
547                                 return LDAP_COMPARE_FALSE;
548                         }
549                         /* fallthru */
550
551                 case LDAP_COMPARE_FALSE:
552                         if ( META_BACK_TGT_T_F( dc->target ) ) {
553                                 tmp = &ber_bvtf_false;
554                                 break;
555                         }
556                         tmp = &ber_bvfalse;
557                         break;
558
559                 case LDAP_COMPARE_TRUE:
560                         if ( META_BACK_TGT_T_F( dc->target ) ) {
561                                 tmp = &ber_bvtf_true;
562                                 break;
563                         }
564
565                         tmp = &ber_bvtrue;
566                         break;
567
568                 default:
569                         tmp = &ber_bverror;
570                         break;
571                 }
572
573                 ber_dupbv( fstr, tmp );
574                 break;
575
576         default:
577                 ber_dupbv( fstr, &ber_bvunknown );
578                 break;
579         }
580
581         return 0;
582 }
583
584 int
585 ldap_back_filter_map_rewrite(
586                 dncookie                *dc,
587                 Filter                  *f,
588                 struct berval           *fstr,
589                 int                     remap )
590 {
591         int             rc;
592         dncookie        fdc;
593         struct berval   ftmp;
594         static char     *dmy = "";
595
596         rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap );
597
598 #ifdef ENABLE_REWRITE
599         if ( rc != LDAP_SUCCESS ) {
600                 return rc;
601         }
602
603         fdc = *dc;
604         ftmp = *fstr;
605
606         fdc.ctx = "searchFilter";
607         
608         switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx,
609                                 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ),
610                                 fdc.conn, &fstr->bv_val ) )
611         {
612         case REWRITE_REGEXEC_OK:
613                 if ( !BER_BVISNULL( fstr ) ) {
614                         fstr->bv_len = strlen( fstr->bv_val );
615
616                 } else {
617                         *fstr = ftmp;
618                 }
619                 Debug( LDAP_DEBUG_ARGS,
620                         "[rw] %s: \"%s\" -> \"%s\"\n",
621                         fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val,
622                         BER_BVISNULL( fstr ) ? "" : fstr->bv_val );             
623                 rc = LDAP_SUCCESS;
624                 break;
625                 
626         case REWRITE_REGEXEC_UNWILLING:
627                 if ( fdc.rs ) {
628                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
629                         fdc.rs->sr_text = "Operation not allowed";
630                 }
631                 rc = LDAP_UNWILLING_TO_PERFORM;
632                 break;
633                 
634         case REWRITE_REGEXEC_ERR:
635                 if ( fdc.rs ) {
636                         fdc.rs->sr_err = LDAP_OTHER;
637                         fdc.rs->sr_text = "Rewrite error";
638                 }
639                 rc = LDAP_OTHER;
640                 break;
641         }
642
643         if ( fstr->bv_val == dmy ) {
644                 BER_BVZERO( fstr );
645         }
646 #endif /* ENABLE_REWRITE */
647
648         return rc;
649 }
650
651 int
652 ldap_back_referral_result_rewrite(
653         dncookie                *dc,
654         BerVarray               a_vals
655 )
656 {
657         int             i, last;
658
659         assert( dc != NULL );
660         assert( a_vals != NULL );
661
662         for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
663                 ;
664         last--;
665
666         for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
667                 struct berval   dn,
668                                 olddn = BER_BVNULL;
669                 int             rc;
670                 LDAPURLDesc     *ludp;
671
672                 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
673                 if ( rc != LDAP_URL_SUCCESS ) {
674                         /* leave attr untouched if massage failed */
675                         continue;
676                 }
677
678                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
679                  * thru ldap_url_parse() and ldap_url_desc2str()
680                  * get rewritten as "ldap:///dc=suffix??base";
681                  * we don't want this to occur... */
682                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
683                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
684                 }
685
686                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
687                 
688                 rc = ldap_back_dn_massage( dc, &olddn, &dn );
689                 switch ( rc ) {
690                 case LDAP_UNWILLING_TO_PERFORM:
691                         /*
692                          * FIXME: need to check if it may be considered 
693                          * legal to trim values when adding/modifying;
694                          * it should be when searching (e.g. ACLs).
695                          */
696                         LBER_FREE( a_vals[ i ].bv_val );
697                         if ( last > i ) {
698                                 a_vals[ i ] = a_vals[ last ];
699                         }
700                         BER_BVZERO( &a_vals[ last ] );
701                         last--;
702                         i--;
703                         break;
704
705                 default:
706                         /* leave attr untouched if massage failed */
707                         if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val )
708                         {
709                                 char    *newurl;
710
711                                 ludp->lud_dn = dn.bv_val;
712                                 newurl = ldap_url_desc2str( ludp );
713                                 free( dn.bv_val );
714                                 if ( newurl == NULL ) {
715                                         /* FIXME: leave attr untouched
716                                          * even if ldap_url_desc2str failed...
717                                          */
718                                         break;
719                                 }
720
721                                 LBER_FREE( a_vals[ i ].bv_val );
722                                 ber_str2bv( newurl, 0, 1, &a_vals[ i ] );
723                                 LDAP_FREE( newurl );
724                                 ludp->lud_dn = olddn.bv_val;
725                         }
726                         break;
727                 }
728
729                 ldap_free_urldesc( ludp );
730         }
731
732         return 0;
733 }
734
735 /*
736  * I don't like this much, but we need two different
737  * functions because different heap managers may be
738  * in use in back-ldap/meta to reduce the amount of
739  * calls to malloc routines, and some of the free()
740  * routines may be macros with args
741  */
742 int
743 ldap_dnattr_rewrite(
744         dncookie                *dc,
745         BerVarray               a_vals
746 )
747 {
748         struct berval   bv;
749         int             i, last;
750
751         assert( a_vals != NULL );
752
753         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
754                 ;
755         last--;
756
757         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
758                 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
759                 case LDAP_UNWILLING_TO_PERFORM:
760                         /*
761                          * FIXME: need to check if it may be considered 
762                          * legal to trim values when adding/modifying;
763                          * it should be when searching (e.g. ACLs).
764                          */
765                         ch_free( a_vals[i].bv_val );
766                         if ( last > i ) {
767                                 a_vals[i] = a_vals[last];
768                         }
769                         BER_BVZERO( &a_vals[last] );
770                         last--;
771                         break;
772
773                 default:
774                         /* leave attr untouched if massage failed */
775                         if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
776                                 ch_free( a_vals[i].bv_val );
777                                 a_vals[i] = bv;
778                         }
779                         break;
780                 }
781         }
782         
783         return 0;
784 }
785
786 int
787 ldap_dnattr_result_rewrite(
788         dncookie                *dc,
789         BerVarray               a_vals
790 )
791 {
792         struct berval   bv;
793         int             i, last;
794
795         assert( a_vals != NULL );
796
797         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
798                 ;
799         last--;
800
801         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
802                 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
803                 case LDAP_UNWILLING_TO_PERFORM:
804                         /*
805                          * FIXME: need to check if it may be considered 
806                          * legal to trim values when adding/modifying;
807                          * it should be when searching (e.g. ACLs).
808                          */
809                         LBER_FREE( a_vals[i].bv_val );
810                         if ( last > i ) {
811                                 a_vals[i] = a_vals[last];
812                         }
813                         BER_BVZERO( &a_vals[last] );
814                         last--;
815                         break;
816
817                 default:
818                         /* leave attr untouched if massage failed */
819                         if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
820                                 LBER_FREE( a_vals[i].bv_val );
821                                 a_vals[i] = bv;
822                         }
823                         break;
824                 }
825         }
826
827         return 0;
828 }
829