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