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