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