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