]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmmap.c
Happy New Year!
[openldap] / servers / slapd / overlays / rwmmap.c
1 /* rwmmap.c - rewrite/mapping routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #ifdef SLAPD_OVER_RWM
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34 #include "rwm.h"
35
36 #undef ldap_debug       /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
38
39 int
40 rwm_mapping_cmp( const void *c1, const void *c2 )
41 {
42         struct ldapmapping *map1 = (struct ldapmapping *)c1;
43         struct ldapmapping *map2 = (struct ldapmapping *)c2;
44         int rc = map1->m_src.bv_len - map2->m_src.bv_len;
45         
46         if ( rc ) {
47                 return rc;
48         }
49
50         return strcasecmp( map1->m_src.bv_val, map2->m_src.bv_val );
51 }
52
53 int
54 rwm_mapping_dup( void *c1, void *c2 )
55 {
56         struct ldapmapping *map1 = (struct ldapmapping *)c1;
57         struct ldapmapping *map2 = (struct ldapmapping *)c2;
58         int rc = map1->m_src.bv_len - map2->m_src.bv_len;
59
60         if ( rc ) {
61                 return 0;
62         }
63
64         return ( ( strcasecmp( map1->m_src.bv_val, map2->m_src.bv_val ) == 0 ) ? -1 : 0 );
65 }
66
67 int
68 rwm_map_init( struct ldapmap *lm, struct ldapmapping **m )
69 {
70         struct ldapmapping      *mapping;
71         const char              *text;
72         int                     rc;
73
74         assert( m );
75
76         *m = NULL;
77         
78         mapping = (struct ldapmapping *)ch_calloc( 2, 
79                         sizeof( struct ldapmapping ) );
80         if ( mapping == NULL ) {
81                 return LDAP_NO_MEMORY;
82         }
83
84         /* FIXME: I don't think this is needed any more... */
85         rc = slap_str2ad( "objectClass", &mapping->m_src_ad, &text );
86         if ( rc != LDAP_SUCCESS ) {
87                 return rc;
88         }
89
90         mapping->m_dst_ad = mapping->m_src_ad;
91         ber_dupbv( &mapping->m_dst, &mapping->m_src_ad->ad_cname );
92         ber_dupbv( &mapping->m_dst, &mapping->m_src );
93
94         mapping[1].m_src = mapping->m_src;
95         mapping[1].m_dst = mapping->m_dst;
96
97         avl_insert( &lm->map, (caddr_t)mapping, 
98                         rwm_mapping_cmp, rwm_mapping_dup );
99         avl_insert( &lm->remap, (caddr_t)&mapping[1], 
100                         rwm_mapping_cmp, rwm_mapping_dup );
101
102         *m = mapping;
103
104         return rc;
105 }
106
107 int
108 rwm_mapping( struct ldapmap *map, struct berval *s, struct ldapmapping **m, int remap )
109 {
110         Avlnode *tree;
111         struct ldapmapping fmapping;
112
113         assert( m );
114
115         if ( remap == RWM_REMAP ) {
116                 tree = map->remap;
117
118         } else {
119                 tree = map->map;
120         }
121
122         fmapping.m_src = *s;
123         *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping,
124                         rwm_mapping_cmp );
125
126         if ( *m == NULL ) {
127                 return map->drop_missing;
128         }
129
130         return 0;
131 }
132
133 void
134 rwm_map( struct ldapmap *map, struct berval *s, struct berval *bv, int remap )
135 {
136         struct ldapmapping *mapping;
137
138         BER_BVZERO( bv );
139         ( void )rwm_mapping( map, s, &mapping, remap );
140         if ( mapping != NULL ) {
141                 if ( !BER_BVISNULL( &mapping->m_dst ) ) {
142                         *bv = mapping->m_dst;
143                 }
144                 return;
145         }
146
147         if ( !map->drop_missing ) {
148                 *bv = *s;
149         }
150 }
151
152 /*
153  * Map attribute names in place
154  */
155 int
156 rwm_map_attrnames(
157                 struct ldapmap *at_map,
158                 struct ldapmap *oc_map,
159                 AttributeName *an,
160                 AttributeName **anp,
161                 int remap
162 )
163 {
164         int             i, j;
165
166         assert( anp );
167
168         *anp = NULL;
169
170         if ( an == NULL ) {
171                 return LDAP_SUCCESS;
172         }
173
174         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
175                 /* just count */ ;
176         *anp = ch_malloc( ( i + 1 )* sizeof( AttributeName ) );
177         if ( *anp == NULL ) {
178                 return LDAP_NO_MEMORY;
179         }
180
181         for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
182                 struct ldapmapping      *m;
183                 int                     at_drop_missing = 0,
184                                         oc_drop_missing = 0;
185
186                 if ( an[i].an_desc ) {
187                         if ( !at_map ) {
188                                 /* FIXME: better leave as is? */
189                                 continue;
190                         }
191                                 
192                         at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
193                         if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
194                                 continue;
195                         }
196
197                         if ( !m ) {
198                                 (*anp)[j] = an[i];
199                                 j++;
200                                 continue;
201                         }
202
203                         (*anp)[j] = an[i];
204                         if ( remap == RWM_MAP ) {
205                                 (*anp)[j].an_name = m->m_dst;
206                                 (*anp)[j].an_desc = m->m_dst_ad;
207                         } else {
208                                 (*anp)[j].an_name = m->m_src;
209                                 (*anp)[j].an_desc = m->m_src_ad;
210
211                         }
212
213                         j++;
214                         continue;
215
216                 } else if ( an[i].an_oc ) {
217                         if ( !oc_map ) {
218                                 /* FIXME: better leave as is? */
219                                 continue;
220                         }
221
222                         oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
223
224                         if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
225                                 continue;
226                         }
227
228                         if ( !m ) {
229                                 (*anp)[j] = an[i];
230                                 j++;
231                                 continue;
232                         }
233
234                         (*anp)[j] = an[i];
235                         if ( remap == RWM_MAP ) {
236                                 (*anp)[j].an_name = m->m_dst;
237                                 (*anp)[j].an_oc = m->m_dst_oc;
238                         } else {
239                                 (*anp)[j].an_name = m->m_src;
240                                 (*anp)[j].an_oc = m->m_src_oc;
241                         }
242
243                 } else {
244                         at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
245                 
246                         if ( at_drop_missing || !m ) {
247
248                                 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
249
250                                 /* if both at_map and oc_map required to drop missing,
251                                  * then do it */
252                                 if ( oc_drop_missing && at_drop_missing ) {
253                                         continue;
254                                 }
255
256                                 /* if no oc_map mapping was found and at_map required
257                                  * to drop missing, then do it; otherwise, at_map wins
258                                  * and an is considered an attr and is left unchanged */
259                                 if ( !m ) {
260                                         if ( at_drop_missing ) {
261                                                 continue;
262                                         }
263                                         (*anp)[j] = an[i];
264                                         j++;
265                                         continue;
266                                 }
267         
268                                 if ( BER_BVISNULL( &m->m_dst ) ) {
269                                         continue;
270                                 }
271
272                                 (*anp)[j] = an[i];
273                                 if ( remap == RWM_MAP ) {
274                                         (*anp)[j].an_name = m->m_dst;
275                                         (*anp)[j].an_oc = m->m_dst_oc;
276                                 } else {
277                                         (*anp)[j].an_name = m->m_src;
278                                         (*anp)[j].an_oc = m->m_src_oc;
279                                 }
280                                 j++;
281                                 continue;
282                         }
283
284                         if ( !BER_BVISNULL( &m->m_dst ) ) {
285                                 (*anp)[j] = an[i];
286                                 if ( remap == RWM_MAP ) {
287                                         (*anp)[j].an_name = m->m_dst;
288                                         (*anp)[j].an_desc = m->m_dst_ad;
289                                 } else {
290                                         (*anp)[j].an_name = m->m_src;
291                                         (*anp)[j].an_desc = m->m_src_ad;
292                                 }
293                                 j++;
294                                 continue;
295                         }
296                 }
297         }
298
299         if ( j == 0 && i != 0 ) {
300                 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
301                 BER_BVSTR( &(*anp)[0].an_name, LDAP_NO_ATTRS );
302         }
303         memset( &(*anp)[j], 0, sizeof( AttributeName ) );
304
305         return LDAP_SUCCESS;
306 }
307
308 int
309 rwm_map_attrs(
310                 struct ldapmap *at_map,
311                 AttributeName *an,
312                 int remap,
313                 char ***mapped_attrs
314 )
315 {
316         int i, j;
317         char **na;
318
319         if ( an == NULL ) {
320                 *mapped_attrs = NULL;
321                 return LDAP_SUCCESS;
322         }
323
324         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
325                 /*  */
326         }
327
328         na = (char **)ch_calloc( i + 1, sizeof( char * ) );
329         if (na == NULL) {
330                 *mapped_attrs = NULL;
331                 return LDAP_NO_MEMORY;
332         }
333
334         for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
335                 struct ldapmapping      *m;
336                 
337                 if ( rwm_mapping( at_map, &an[i].an_name, &m, remap ) ) {
338                         continue;
339                 }
340
341                 if ( !m || ( m && !BER_BVISNULL( &m->m_dst ) ) ) {
342                         na[j++] = m->m_dst.bv_val;
343                 }
344         }
345         if ( j == 0 && i != 0 ) {
346                 na[j++] = LDAP_NO_ATTRS;
347         }
348         na[j] = NULL;
349
350         *mapped_attrs = na;
351         return LDAP_SUCCESS;
352 }
353
354 static int
355 map_attr_value(
356                 dncookie                *dc,
357                 AttributeDescription    *ad,
358                 struct berval           *mapped_attr,
359                 struct berval           *value,
360                 struct berval           *mapped_value,
361                 int                     remap )
362 {
363         struct berval           vtmp = BER_BVNULL;
364         int                     freeval = 0;
365
366         rwm_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
367         if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
368                 /*
369                  * FIXME: are we sure we need to search oc_map if at_map fails?
370                  */
371                 rwm_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
372                 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) )
373                 {
374                         *mapped_attr = ad->ad_cname;
375                 }
376         }
377
378         if ( value == NULL ) {
379                 return 0;
380         }
381
382         if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
383         {
384                 dncookie        fdc = *dc;
385                 int             rc;
386
387 #ifdef ENABLE_REWRITE
388                 fdc.ctx = "searchFilterAttrDN";
389 #endif /* ENABLE_REWRITE */
390
391                 vtmp = *value;
392                 rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
393                 switch ( rc ) {
394                 case LDAP_SUCCESS:
395                         if ( vtmp.bv_val != value->bv_val ) {
396                                 freeval = 1;
397                         }
398                         break;
399                 
400                 case LDAP_UNWILLING_TO_PERFORM:
401                 case LDAP_OTHER:
402                 default:
403                         return -1;
404                 }
405
406         } else if ( ad == slap_schema.si_ad_objectClass
407                         || ad == slap_schema.si_ad_structuralObjectClass )
408         {
409                 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
410                 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
411                         vtmp = *value;
412                 }
413                 
414         } else {
415                 vtmp = *value;
416         }
417
418         filter_escape_value( &vtmp, mapped_value );
419
420         if ( freeval ) {
421                 ch_free( vtmp.bv_val );
422         }
423         
424         return 0;
425 }
426
427 static int
428 rwm_int_filter_map_rewrite(
429                 dncookie                *dc,
430                 Filter                  *f,
431                 struct berval           *fstr )
432 {
433         int             i;
434         Filter          *p;
435         struct berval   atmp,
436                         vtmp,
437                         tmp;
438         static struct berval
439                         ber_bvfalse = BER_BVC( "(?=false)" ),
440                         ber_bvtrue = BER_BVC( "(?=true)" ),
441                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
442                         ber_bverror = BER_BVC( "(?=error)" ),
443                         ber_bvunknown = BER_BVC( "(?=unknown)" ),
444                         ber_bvnone = BER_BVC( "(?=none)" );
445         ber_len_t       len;
446
447         if ( f == NULL ) {
448                 ber_dupbv( fstr, &ber_bvnone );
449                 return -1;
450         }
451
452         switch ( f->f_choice ) {
453         case LDAP_FILTER_EQUALITY:
454                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
455                                         &f->f_av_value, &vtmp, RWM_MAP ) )
456                 {
457                         return -1;
458                 }
459
460                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
461                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
462
463                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
464                         atmp.bv_val, vtmp.bv_val );
465
466                 ch_free( vtmp.bv_val );
467                 break;
468
469         case LDAP_FILTER_GE:
470                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
471                                         &f->f_av_value, &vtmp, RWM_MAP ) )
472                 {
473                         return -1;
474                 }
475
476                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
477                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
478
479                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
480                         atmp.bv_val, vtmp.bv_val );
481
482                 ch_free( vtmp.bv_val );
483                 break;
484
485         case LDAP_FILTER_LE:
486                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
487                                         &f->f_av_value, &vtmp, RWM_MAP ) )
488                 {
489                         return -1;
490                 }
491
492                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
493                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
494
495                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
496                         atmp.bv_val, vtmp.bv_val );
497
498                 ch_free( vtmp.bv_val );
499                 break;
500
501         case LDAP_FILTER_APPROX:
502                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
503                                         &f->f_av_value, &vtmp, RWM_MAP ) )
504                 {
505                         return -1;
506                 }
507
508                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
509                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
510
511                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
512                         atmp.bv_val, vtmp.bv_val );
513
514                 ch_free( vtmp.bv_val );
515                 break;
516
517         case LDAP_FILTER_SUBSTRINGS:
518                 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
519                                         NULL, NULL, RWM_MAP ) )
520                 {
521                         return -1;
522                 }
523
524                 /* cannot be a DN ... */
525
526                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
527                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
528
529                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
530                         atmp.bv_val );
531
532                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
533                         len = fstr->bv_len;
534
535                         filter_escape_value( &f->f_sub_initial, &vtmp );
536
537                         fstr->bv_len += vtmp.bv_len;
538                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
539
540                         snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
541                                 /* "(attr=" */ "%s*)",
542                                 vtmp.bv_val );
543
544                         ch_free( vtmp.bv_val );
545                 }
546
547                 if ( f->f_sub_any != NULL ) {
548                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
549                                 len = fstr->bv_len;
550                                 filter_escape_value( &f->f_sub_any[i], &vtmp );
551
552                                 fstr->bv_len += vtmp.bv_len + 1;
553                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
554
555                                 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
556                                         /* "(attr=[init]*[any*]" */ "%s*)",
557                                         vtmp.bv_val );
558                                 ch_free( vtmp.bv_val );
559                         }
560                 }
561
562                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
563                         len = fstr->bv_len;
564
565                         filter_escape_value( &f->f_sub_final, &vtmp );
566
567                         fstr->bv_len += vtmp.bv_len;
568                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
569
570                         snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
571                                 /* "(attr=[init*][any*]" */ "%s)",
572                                 vtmp.bv_val );
573
574                         ch_free( vtmp.bv_val );
575                 }
576
577                 break;
578
579         case LDAP_FILTER_PRESENT:
580                 if ( map_attr_value( dc, f->f_desc, &atmp,
581                                         NULL, NULL, RWM_MAP ) )
582                 {
583                         return -1;
584                 }
585
586                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
587                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
588
589                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
590                         atmp.bv_val );
591                 break;
592
593         case LDAP_FILTER_AND:
594         case LDAP_FILTER_OR:
595         case LDAP_FILTER_NOT:
596                 fstr->bv_len = STRLENOF( "(%)" );
597                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
598
599                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
600                         f->f_choice == LDAP_FILTER_AND ? '&' :
601                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
602
603                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
604                         len = fstr->bv_len;
605
606                         if ( rwm_int_filter_map_rewrite( dc, p, &vtmp ) )
607                         {
608                                 return -1;
609                         }
610                         
611                         fstr->bv_len += vtmp.bv_len;
612                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
613
614                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
615                                 /*"("*/ "%s)", vtmp.bv_val );
616
617                         ch_free( vtmp.bv_val );
618                 }
619
620                 break;
621
622         case LDAP_FILTER_EXT: {
623                 if ( f->f_mr_desc ) {
624                         if ( map_attr_value( dc, f->f_mr_desc, &atmp,
625                                                 &f->f_mr_value, &vtmp, RWM_MAP ) )
626                         {
627                                 return -1;
628                         }
629
630                 } else {
631                         BER_BVSTR( &atmp, "" );
632                         filter_escape_value( &f->f_mr_value, &vtmp );
633                 }
634                         
635
636                 fstr->bv_len = atmp.bv_len +
637                         ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
638                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
639                         vtmp.bv_len + STRLENOF( "(:=)" );
640                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
641
642                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
643                         atmp.bv_val,
644                         f->f_mr_dnattrs ? ":dn" : "",
645                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
646                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
647                         vtmp.bv_val );
648                 ch_free( vtmp.bv_val );
649                 break;
650         }
651
652         case SLAPD_FILTER_COMPUTED:
653                 switch ( f->f_result ) {
654                 case LDAP_COMPARE_FALSE:
655                         tmp = ber_bvfalse;
656                         break;
657
658                 case LDAP_COMPARE_TRUE:
659                         tmp = ber_bvtrue;
660                         break;
661                         
662                 case SLAPD_COMPARE_UNDEFINED:
663                         tmp = ber_bvundefined;
664                         break;
665                         
666                 default:
667                         tmp = ber_bverror;
668                         break;
669                 }
670
671                 ber_dupbv( fstr, &tmp );
672                 break;
673                 
674         default:
675                 ber_dupbv( fstr, &ber_bvunknown );
676                 break;
677         }
678
679         return 0;
680 }
681
682 int
683 rwm_filter_map_rewrite(
684                 dncookie                *dc,
685                 Filter                  *f,
686                 struct berval           *fstr )
687 {
688         int             rc;
689         dncookie        fdc;
690         struct berval   ftmp;
691
692         rc = rwm_int_filter_map_rewrite( dc, f, fstr );
693
694 #ifdef ENABLE_REWRITE
695         if ( rc != LDAP_SUCCESS ) {
696                 return rc;
697         }
698
699         fdc = *dc;
700         ftmp = *fstr;
701
702         fdc.ctx = "searchFilter";
703
704         switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx, 
705                                 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ), 
706                                 fdc.conn, &fstr->bv_val ) )
707         {
708         case REWRITE_REGEXEC_OK:
709                 if ( !BER_BVISNULL( fstr ) ) {
710                         fstr->bv_len = strlen( fstr->bv_val );
711                         ch_free( ftmp.bv_val );
712
713                 } else {
714                         *fstr = ftmp;
715                 }
716
717                 Debug( LDAP_DEBUG_ARGS,
718                         "[rw] %s: \"%s\" -> \"%s\"\n",
719                         fdc.ctx, ftmp.bv_val, fstr->bv_val );           
720                 rc = LDAP_SUCCESS;
721                 break;
722                 
723         case REWRITE_REGEXEC_UNWILLING:
724                 if ( fdc.rs ) {
725                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
726                         fdc.rs->sr_text = "Operation not allowed";
727                 }
728                 rc = LDAP_UNWILLING_TO_PERFORM;
729                 break;
730                 
731         case REWRITE_REGEXEC_ERR:
732                 if ( fdc.rs ) {
733                         fdc.rs->sr_err = LDAP_OTHER;
734                         fdc.rs->sr_text = "Rewrite error";
735                 }
736                 rc = LDAP_OTHER;
737                 break;
738         }
739
740 #endif /* ENABLE_REWRITE */
741         return rc;
742 }
743
744 /*
745  * I don't like this much, but we need two different
746  * functions because different heap managers may be
747  * in use in back-ldap/meta to reduce the amount of
748  * calls to malloc routines, and some of the free()
749  * routines may be macros with args
750  */
751 int
752 rwm_referral_rewrite(
753         Operation               *op,
754         SlapReply               *rs,
755         void                    *cookie,
756         BerVarray               a_vals,
757         BerVarray               *pa_nvals )
758 {
759         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
760         struct ldaprwmap        *rwmap = 
761                         (struct ldaprwmap *)on->on_bi.bi_private;
762
763         int                     i, last;
764
765         dncookie                dc;
766         struct berval           dn = BER_BVNULL,
767                                 ndn = BER_BVNULL;
768
769         assert( a_vals );
770
771         /*
772          * Rewrite the dn if needed
773          */
774         dc.rwmap = rwmap;
775 #ifdef ENABLE_REWRITE
776         dc.conn = op->o_conn;
777         dc.rs = rs;
778         dc.ctx = (char *)cookie;
779 #else /* ! ENABLE_REWRITE */
780         dc.tofrom = ((int *)cookie)[0];
781         dc.normalized = 0;
782 #endif /* ! ENABLE_REWRITE */
783
784         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
785                 ;
786         last--;
787         
788         if ( pa_nvals != NULL ) {
789                 if ( *pa_nvals == NULL ) {
790                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
791                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
792                 }
793         }
794
795         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
796                 struct berval   olddn, oldval;
797                 int             rc;
798                 LDAPURLDesc     *ludp;
799
800                 oldval = a_vals[i];
801                 rc = ldap_url_parse( oldval.bv_val, &ludp );
802                 if ( rc != LDAP_URL_SUCCESS ) {
803                         /* leave attr untouched if massage failed */
804                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
805                                 ber_dupbv( &(*pa_nvals)[i], &oldval );
806                         }
807                         continue;
808                 }
809
810                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
811                  * thru ldap_url_parse() and ldap_url_desc2str() 
812                  * get rewritten as "ldap:///dc=suffix??base";
813                  * we don't want this to occur... */
814                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
815                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
816                 }
817
818                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
819
820                 dn = olddn;
821                 if ( pa_nvals ) {
822                         ndn = olddn;
823                         rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
824                                         &dn, &ndn );
825                 } else {
826                         rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
827                 }
828
829                 switch ( rc ) {
830                 case LDAP_UNWILLING_TO_PERFORM:
831                         /*
832                          * FIXME: need to check if it may be considered 
833                          * legal to trim values when adding/modifying;
834                          * it should be when searching (e.g. ACLs).
835                          */
836                         ch_free( a_vals[i].bv_val );
837                         if (last > i ) {
838                                 a_vals[i] = a_vals[last];
839                                 if ( pa_nvals ) {
840                                         (*pa_nvals)[i] = (*pa_nvals)[last];
841                                 }
842                         }
843                         BER_BVZERO( &a_vals[last] );
844                         if ( pa_nvals ) {
845                                 BER_BVZERO( &(*pa_nvals)[last] );
846                         }
847                         last--;
848                         break;
849                 
850                 case LDAP_SUCCESS:
851                         if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
852                                 char    *newurl;
853
854                                 ludp->lud_dn = dn.bv_val;
855                                 newurl = ldap_url_desc2str( ludp );
856                                 ludp->lud_dn = olddn.bv_val;
857                                 ch_free( dn.bv_val );
858                                 if ( newurl == NULL ) {
859                                         /* FIXME: leave attr untouched
860                                          * even if ldap_url_desc2str failed...
861                                          */
862                                         break;
863                                 }
864
865                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
866                                 LDAP_FREE( newurl );
867
868                                 if ( pa_nvals ) {
869                                         ludp->lud_dn = ndn.bv_val;
870                                         newurl = ldap_url_desc2str( ludp );
871                                         ludp->lud_dn = olddn.bv_val;
872                                         ch_free( ndn.bv_val );
873                                         if ( newurl == NULL ) {
874                                                 /* FIXME: leave attr untouched
875                                                  * even if ldap_url_desc2str failed...
876                                                  */
877                                                 ch_free( a_vals[i].bv_val );
878                                                 a_vals[i] = oldval;
879                                                 break;
880                                         }
881
882                                         if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
883                                                 ch_free( (*pa_nvals)[i].bv_val );
884                                         }
885                                         ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
886                                         LDAP_FREE( newurl );
887                                 }
888
889                                 ch_free( oldval.bv_val );
890                                 ludp->lud_dn = olddn.bv_val;
891                         }
892                         break;
893
894                 default:
895                         /* leave attr untouched if massage failed */
896                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
897                                 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
898                         }
899                         break;
900                 }
901                 ldap_free_urldesc( ludp );
902         }
903         
904         return 0;
905 }
906
907 /*
908  * I don't like this much, but we need two different
909  * functions because different heap managers may be
910  * in use in back-ldap/meta to reduce the amount of
911  * calls to malloc routines, and some of the free()
912  * routines may be macros with args
913  */
914 int
915 rwm_dnattr_rewrite(
916         Operation               *op,
917         SlapReply               *rs,
918         void                    *cookie,
919         BerVarray               a_vals,
920         BerVarray               *pa_nvals )
921 {
922         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
923         struct ldaprwmap        *rwmap = 
924                         (struct ldaprwmap *)on->on_bi.bi_private;
925
926         int                     i, last;
927
928         dncookie                dc;
929         struct berval           dn = BER_BVNULL,
930                                 ndn = BER_BVNULL;
931         BerVarray               in;
932
933         if ( a_vals ) {
934                 in = a_vals;
935
936         } else {
937                 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
938                         return LDAP_OTHER;
939                 }
940                 in = *pa_nvals;
941         }
942
943         /*
944          * Rewrite the dn if needed
945          */
946         dc.rwmap = rwmap;
947 #ifdef ENABLE_REWRITE
948         dc.conn = op->o_conn;
949         dc.rs = rs;
950         dc.ctx = (char *)cookie;
951 #else /* ! ENABLE_REWRITE */
952         dc.tofrom = ((int *)cookie)[0];
953         dc.normalized = 0;
954 #endif /* ! ENABLE_REWRITE */
955
956         for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
957         last--;
958         if ( pa_nvals != NULL ) {
959                 if ( *pa_nvals == NULL ) {
960                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
961                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
962                 }
963         }
964
965         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
966                 int             rc;
967
968                 if ( a_vals ) {
969                         dn = in[i];
970                         if ( pa_nvals ) {
971                                 ndn = (*pa_nvals)[i];
972                                 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
973                         } else {
974                                 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
975                         }
976                 } else {
977                         ndn = in[i];
978                         rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
979                 }
980
981                 switch ( rc ) {
982                 case LDAP_UNWILLING_TO_PERFORM:
983                         /*
984                          * FIXME: need to check if it may be considered 
985                          * legal to trim values when adding/modifying;
986                          * it should be when searching (e.g. ACLs).
987                          */
988                         ch_free( in[i].bv_val );
989                         if (last > i ) {
990                                 in[i] = in[last];
991                                 if ( a_vals && pa_nvals ) {
992                                         (*pa_nvals)[i] = (*pa_nvals)[last];
993                                 }
994                         }
995                         BER_BVZERO( &in[last] );
996                         if ( a_vals && pa_nvals ) {
997                                 BER_BVZERO( &(*pa_nvals)[last] );
998                         }
999                         last--;
1000                         break;
1001                 
1002                 case LDAP_SUCCESS:
1003                         if ( a_vals ) {
1004                                 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1005                                         ch_free( a_vals[i].bv_val );
1006                                         a_vals[i] = dn;
1007
1008                                         if ( pa_nvals ) {
1009                                                 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1010                                                         ch_free( (*pa_nvals)[i].bv_val );
1011                                                 }
1012                                                 (*pa_nvals)[i] = ndn;
1013                                         }
1014                                 }
1015                                 
1016                         } else {
1017                                 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1018                                         ch_free( (*pa_nvals)[i].bv_val );
1019                                         (*pa_nvals)[i] = ndn;
1020                                 }
1021                         }
1022                         break;
1023
1024                 default:
1025                         /* leave attr untouched if massage failed */
1026                         if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1027                                 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1028                         }
1029                         break;
1030                 }
1031         }
1032         
1033         return 0;
1034 }
1035
1036 int
1037 rwm_referral_result_rewrite(
1038         dncookie                *dc,
1039         BerVarray               a_vals
1040 )
1041 {
1042         int             i, last;
1043
1044         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1045         last--;
1046
1047         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1048                 struct berval   dn, olddn;
1049                 int             rc;
1050                 LDAPURLDesc     *ludp;
1051
1052                 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1053                 if ( rc != LDAP_URL_SUCCESS ) {
1054                         /* leave attr untouched if massage failed */
1055                         continue;
1056                 }
1057
1058                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1059                  * thru ldap_url_parse() and ldap_url_desc2str()
1060                  * get rewritten as "ldap:///dc=suffix??base";
1061                  * we don't want this to occur... */
1062                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1063                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1064                 }
1065
1066                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1067
1068                 dn = olddn;
1069                 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1070                 switch ( rc ) {
1071                 case LDAP_UNWILLING_TO_PERFORM:
1072                         /*
1073                          * FIXME: need to check if it may be considered 
1074                          * legal to trim values when adding/modifying;
1075                          * it should be when searching (e.g. ACLs).
1076                          */
1077                         ch_free( a_vals[i].bv_val );
1078                         if ( last > i ) {
1079                                 a_vals[i] = a_vals[last];
1080                         }
1081                         BER_BVZERO( &a_vals[last] );
1082                         last--;
1083                         i--;
1084                         break;
1085
1086                 default:
1087                         /* leave attr untouched if massage failed */
1088                         if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1089                                 char    *newurl;
1090
1091                                 ludp->lud_dn = dn.bv_val;
1092                                 newurl = ldap_url_desc2str( ludp );
1093                                 if ( newurl == NULL ) {
1094                                         /* FIXME: leave attr untouched
1095                                          * even if ldap_url_desc2str failed...
1096                                          */
1097                                         break;
1098                                 }
1099
1100                                 ch_free( a_vals[i].bv_val );
1101                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1102                                 LDAP_FREE( newurl );
1103                                 ludp->lud_dn = olddn.bv_val;
1104                         }
1105                         break;
1106                 }
1107
1108                 ldap_free_urldesc( ludp );
1109         }
1110
1111         return 0;
1112 }
1113
1114 int
1115 rwm_dnattr_result_rewrite(
1116         dncookie                *dc,
1117         BerVarray               a_vals
1118 )
1119 {
1120         int             i, last;
1121
1122         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1123         last--;
1124
1125         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1126                 struct berval   dn;
1127                 int             rc;
1128                 
1129                 dn = a_vals[i];
1130                 rc = rwm_dn_massage_pretty( dc, &a_vals[i], &dn );
1131                 switch ( rc ) {
1132                 case LDAP_UNWILLING_TO_PERFORM:
1133                         /*
1134                          * FIXME: need to check if it may be considered 
1135                          * legal to trim values when adding/modifying;
1136                          * it should be when searching (e.g. ACLs).
1137                          */
1138                         ch_free( a_vals[i].bv_val );
1139                         if ( last > i ) {
1140                                 a_vals[i] = a_vals[last];
1141                         }
1142                         BER_BVZERO( &a_vals[last] );
1143                         last--;
1144                         break;
1145
1146                 default:
1147                         /* leave attr untouched if massage failed */
1148                         if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1149                                 ch_free( a_vals[i].bv_val );
1150                                 a_vals[i] = dn;
1151                         }
1152                         break;
1153                 }
1154         }
1155
1156         return 0;
1157 }
1158
1159 void
1160 rwm_mapping_free( void *v_mapping )
1161 {
1162         struct ldapmapping *mapping = v_mapping;
1163
1164         if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1165                 ch_free( mapping[0].m_src.bv_val );
1166         }
1167
1168         if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1169                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1170                         if ( mapping[0].m_src_oc ) {
1171                                 ch_free( mapping[0].m_src_oc );
1172                         }
1173
1174                 } else {
1175                         if ( mapping[0].m_src_ad ) {
1176                                 ch_free( mapping[0].m_src_ad );
1177                         }
1178                 }
1179         }
1180
1181         if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1182                 ch_free( mapping[0].m_dst.bv_val );
1183         }
1184
1185         if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1186                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1187                         if ( mapping[0].m_dst_oc ) {
1188                                 ch_free( mapping[0].m_dst_oc );
1189                         }
1190
1191                 } else {
1192                         if ( mapping[0].m_dst_ad ) {
1193                                 ch_free( mapping[0].m_dst_ad );
1194                         }
1195                 }
1196         }
1197
1198         ch_free( mapping );
1199
1200 }
1201
1202 #endif /* SLAPD_OVER_RWM */