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