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