]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmmap.c
improve previous commit; now attribute values are rewritten either if they were DN...
[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      *mapping;
336                 
337                 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
338                         continue;
339                 }
340
341                 if ( !mapping ) {
342                         na[ j++ ] = an[ i ].an_name.bv_val;
343                         
344                 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
345                         na[ j++ ] = mapping->m_dst.bv_val;
346                 }
347         }
348
349         if ( j == 0 && i != 0 ) {
350                 na[ j++ ] = LDAP_NO_ATTRS;
351         }
352
353         na[ j ] = NULL;
354
355         *mapped_attrs = na;
356
357         return LDAP_SUCCESS;
358 }
359
360 static int
361 map_attr_value(
362                 dncookie                *dc,
363                 AttributeDescription    **adp,
364                 struct berval           *mapped_attr,
365                 struct berval           *value,
366                 struct berval           *mapped_value,
367                 int                     remap )
368 {
369         struct berval           vtmp = BER_BVNULL;
370         int                     freeval = 0;
371         AttributeDescription    *ad = *adp;
372         struct ldapmapping      *mapping = NULL;
373
374         rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
375         if ( mapping == NULL ) {
376                 if ( dc->rwmap->rwm_at.drop_missing ) {
377                         return -1;
378                 }
379
380                 *mapped_attr = ad->ad_cname;
381
382         } else {
383                 *mapped_attr = mapping->m_dst;
384         }
385
386         if ( value != NULL ) {
387                 assert( mapped_value != NULL );
388
389                 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
390                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
391                 {
392                         dncookie        fdc = *dc;
393                         int             rc;
394
395 #ifdef ENABLE_REWRITE
396                         fdc.ctx = "searchFilterAttrDN";
397 #endif /* ENABLE_REWRITE */
398
399                         vtmp = *value;
400                         rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
401                         switch ( rc ) {
402                         case LDAP_SUCCESS:
403                                 if ( vtmp.bv_val != value->bv_val ) {
404                                         freeval = 1;
405                                 }
406                                 break;
407                 
408                         case LDAP_UNWILLING_TO_PERFORM:
409                         case LDAP_OTHER:
410                         default:
411                                 return -1;
412                         }
413
414                 } else if ( ad == slap_schema.si_ad_objectClass
415                                 || ad == slap_schema.si_ad_structuralObjectClass )
416                 {
417                         rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
418                         if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
419                                 vtmp = *value;
420                         }
421                 
422                 } else {
423                         vtmp = *value;
424                 }
425
426                 filter_escape_value( &vtmp, mapped_value );
427
428                 if ( freeval ) {
429                         ch_free( vtmp.bv_val );
430                 }
431         }
432         
433         if ( mapping != NULL ) {
434                 assert( mapping->m_dst_ad != NULL );
435                 *adp = mapping->m_dst_ad;
436         }
437
438         return 0;
439 }
440
441 static int
442 rwm_int_filter_map_rewrite(
443                 dncookie                *dc,
444                 Filter                  *f,
445                 struct berval           *fstr )
446 {
447         int             i;
448         Filter          *p;
449         struct berval   atmp,
450                         vtmp,
451                         tmp;
452         static struct berval
453                         ber_bvfalse = BER_BVC( "(?=false)" ),
454                         ber_bvtrue = BER_BVC( "(?=true)" ),
455                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
456                         ber_bverror = BER_BVC( "(?=error)" ),
457                         ber_bvunknown = BER_BVC( "(?=unknown)" ),
458                         ber_bvnone = BER_BVC( "(?=none)" );
459         ber_len_t       len;
460
461         if ( f == NULL ) {
462                 ber_dupbv( fstr, &ber_bvnone );
463                 return -1;
464         }
465
466         switch ( f->f_choice ) {
467         case LDAP_FILTER_EQUALITY:
468                 if ( map_attr_value( dc, &f->f_av_desc, &atmp,
469                                         &f->f_av_value, &vtmp, RWM_MAP ) )
470                 {
471                         return -1;
472                 }
473
474                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
475                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
476
477                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
478                         atmp.bv_val, vtmp.bv_val );
479
480                 ch_free( vtmp.bv_val );
481                 break;
482
483         case LDAP_FILTER_GE:
484                 if ( map_attr_value( dc, &f->f_av_desc, &atmp,
485                                         &f->f_av_value, &vtmp, RWM_MAP ) )
486                 {
487                         return -1;
488                 }
489
490                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
491                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
492
493                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
494                         atmp.bv_val, vtmp.bv_val );
495
496                 ch_free( vtmp.bv_val );
497                 break;
498
499         case LDAP_FILTER_LE:
500                 if ( map_attr_value( dc, &f->f_av_desc, &atmp,
501                                         &f->f_av_value, &vtmp, RWM_MAP ) )
502                 {
503                         return -1;
504                 }
505
506                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
507                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
508
509                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
510                         atmp.bv_val, vtmp.bv_val );
511
512                 ch_free( vtmp.bv_val );
513                 break;
514
515         case LDAP_FILTER_APPROX:
516                 if ( map_attr_value( dc, &f->f_av_desc, &atmp,
517                                         &f->f_av_value, &vtmp, RWM_MAP ) )
518                 {
519                         return -1;
520                 }
521
522                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
523                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
524
525                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
526                         atmp.bv_val, vtmp.bv_val );
527
528                 ch_free( vtmp.bv_val );
529                 break;
530
531         case LDAP_FILTER_SUBSTRINGS:
532                 if ( map_attr_value( dc, &f->f_sub_desc, &atmp,
533                                         NULL, NULL, RWM_MAP ) )
534                 {
535                         return -1;
536                 }
537
538                 /* cannot be a DN ... */
539
540                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
541                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
542
543                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
544                         atmp.bv_val );
545
546                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
547                         len = fstr->bv_len;
548
549                         filter_escape_value( &f->f_sub_initial, &vtmp );
550
551                         fstr->bv_len += vtmp.bv_len;
552                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
553
554                         snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
555                                 /* "(attr=" */ "%s*)",
556                                 vtmp.bv_val );
557
558                         ch_free( vtmp.bv_val );
559                 }
560
561                 if ( f->f_sub_any != NULL ) {
562                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
563                                 len = fstr->bv_len;
564                                 filter_escape_value( &f->f_sub_any[i], &vtmp );
565
566                                 fstr->bv_len += vtmp.bv_len + 1;
567                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
568
569                                 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
570                                         /* "(attr=[init]*[any*]" */ "%s*)",
571                                         vtmp.bv_val );
572                                 ch_free( vtmp.bv_val );
573                         }
574                 }
575
576                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
577                         len = fstr->bv_len;
578
579                         filter_escape_value( &f->f_sub_final, &vtmp );
580
581                         fstr->bv_len += vtmp.bv_len;
582                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
583
584                         snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
585                                 /* "(attr=[init*][any*]" */ "%s)",
586                                 vtmp.bv_val );
587
588                         ch_free( vtmp.bv_val );
589                 }
590
591                 break;
592
593         case LDAP_FILTER_PRESENT:
594                 if ( map_attr_value( dc, &f->f_desc, &atmp,
595                                         NULL, NULL, RWM_MAP ) )
596                 {
597                         return -1;
598                 }
599
600                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
601                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
602
603                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
604                         atmp.bv_val );
605                 break;
606
607         case LDAP_FILTER_AND:
608         case LDAP_FILTER_OR:
609         case LDAP_FILTER_NOT:
610                 fstr->bv_len = STRLENOF( "(%)" );
611                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
612
613                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
614                         f->f_choice == LDAP_FILTER_AND ? '&' :
615                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
616
617                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
618                         len = fstr->bv_len;
619
620                         if ( rwm_int_filter_map_rewrite( dc, p, &vtmp ) )
621                         {
622                                 return -1;
623                         }
624                         
625                         fstr->bv_len += vtmp.bv_len;
626                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
627
628                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
629                                 /*"("*/ "%s)", vtmp.bv_val );
630
631                         ch_free( vtmp.bv_val );
632                 }
633
634                 break;
635
636         case LDAP_FILTER_EXT: {
637                 if ( f->f_mr_desc ) {
638                         if ( map_attr_value( dc, &f->f_mr_desc, &atmp,
639                                                 &f->f_mr_value, &vtmp, RWM_MAP ) )
640                         {
641                                 return -1;
642                         }
643
644                 } else {
645                         BER_BVSTR( &atmp, "" );
646                         filter_escape_value( &f->f_mr_value, &vtmp );
647                 }
648                         
649
650                 fstr->bv_len = atmp.bv_len +
651                         ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
652                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
653                         vtmp.bv_len + STRLENOF( "(:=)" );
654                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
655
656                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
657                         atmp.bv_val,
658                         f->f_mr_dnattrs ? ":dn" : "",
659                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
660                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
661                         vtmp.bv_val );
662                 ch_free( vtmp.bv_val );
663                 break;
664         }
665
666         case SLAPD_FILTER_COMPUTED:
667                 switch ( f->f_result ) {
668                 case LDAP_COMPARE_FALSE:
669                         tmp = ber_bvfalse;
670                         break;
671
672                 case LDAP_COMPARE_TRUE:
673                         tmp = ber_bvtrue;
674                         break;
675                         
676                 case SLAPD_COMPARE_UNDEFINED:
677                         tmp = ber_bvundefined;
678                         break;
679                         
680                 default:
681                         tmp = ber_bverror;
682                         break;
683                 }
684
685                 ber_dupbv( fstr, &tmp );
686                 break;
687                 
688         default:
689                 ber_dupbv( fstr, &ber_bvunknown );
690                 break;
691         }
692
693         return 0;
694 }
695
696 int
697 rwm_filter_map_rewrite(
698                 dncookie                *dc,
699                 Filter                  *f,
700                 struct berval           *fstr )
701 {
702         int             rc;
703         dncookie        fdc;
704         struct berval   ftmp;
705
706         rc = rwm_int_filter_map_rewrite( dc, f, fstr );
707
708 #ifdef ENABLE_REWRITE
709         if ( rc != LDAP_SUCCESS ) {
710                 return rc;
711         }
712
713         fdc = *dc;
714         ftmp = *fstr;
715
716         fdc.ctx = "searchFilter";
717
718         switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx, 
719                                 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ), 
720                                 fdc.conn, &fstr->bv_val ) )
721         {
722         case REWRITE_REGEXEC_OK:
723                 if ( !BER_BVISNULL( fstr ) ) {
724                         fstr->bv_len = strlen( fstr->bv_val );
725                         ch_free( ftmp.bv_val );
726
727                 } else {
728                         *fstr = ftmp;
729                 }
730
731                 Debug( LDAP_DEBUG_ARGS,
732                         "[rw] %s: \"%s\" -> \"%s\"\n",
733                         fdc.ctx, ftmp.bv_val, fstr->bv_val );           
734                 rc = LDAP_SUCCESS;
735                 break;
736                 
737         case REWRITE_REGEXEC_UNWILLING:
738                 if ( fdc.rs ) {
739                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
740                         fdc.rs->sr_text = "Operation not allowed";
741                 }
742                 rc = LDAP_UNWILLING_TO_PERFORM;
743                 break;
744                 
745         case REWRITE_REGEXEC_ERR:
746                 if ( fdc.rs ) {
747                         fdc.rs->sr_err = LDAP_OTHER;
748                         fdc.rs->sr_text = "Rewrite error";
749                 }
750                 rc = LDAP_OTHER;
751                 break;
752         }
753
754 #endif /* ENABLE_REWRITE */
755         return rc;
756 }
757
758 /*
759  * I don't like this much, but we need two different
760  * functions because different heap managers may be
761  * in use in back-ldap/meta to reduce the amount of
762  * calls to malloc routines, and some of the free()
763  * routines may be macros with args
764  */
765 int
766 rwm_referral_rewrite(
767         Operation               *op,
768         SlapReply               *rs,
769         void                    *cookie,
770         BerVarray               a_vals,
771         BerVarray               *pa_nvals )
772 {
773         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
774         struct ldaprwmap        *rwmap = 
775                         (struct ldaprwmap *)on->on_bi.bi_private;
776
777         int                     i, last;
778
779         dncookie                dc;
780         struct berval           dn = BER_BVNULL,
781                                 ndn = BER_BVNULL;
782
783         assert( a_vals );
784
785         /*
786          * Rewrite the dn if needed
787          */
788         dc.rwmap = rwmap;
789 #ifdef ENABLE_REWRITE
790         dc.conn = op->o_conn;
791         dc.rs = rs;
792         dc.ctx = (char *)cookie;
793 #else /* ! ENABLE_REWRITE */
794         dc.tofrom = ((int *)cookie)[0];
795         dc.normalized = 0;
796 #endif /* ! ENABLE_REWRITE */
797
798         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
799                 ;
800         last--;
801         
802         if ( pa_nvals != NULL ) {
803                 if ( *pa_nvals == NULL ) {
804                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
805                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
806                 }
807         }
808
809         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
810                 struct berval   olddn, oldval;
811                 int             rc;
812                 LDAPURLDesc     *ludp;
813
814                 oldval = a_vals[i];
815                 rc = ldap_url_parse( oldval.bv_val, &ludp );
816                 if ( rc != LDAP_URL_SUCCESS ) {
817                         /* leave attr untouched if massage failed */
818                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
819                                 ber_dupbv( &(*pa_nvals)[i], &oldval );
820                         }
821                         continue;
822                 }
823
824                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
825                  * thru ldap_url_parse() and ldap_url_desc2str() 
826                  * get rewritten as "ldap:///dc=suffix??base";
827                  * we don't want this to occur... */
828                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
829                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
830                 }
831
832                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
833
834                 dn = olddn;
835                 if ( pa_nvals ) {
836                         ndn = olddn;
837                         rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
838                                         &dn, &ndn );
839                 } else {
840                         rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
841                 }
842
843                 switch ( rc ) {
844                 case LDAP_UNWILLING_TO_PERFORM:
845                         /*
846                          * FIXME: need to check if it may be considered 
847                          * legal to trim values when adding/modifying;
848                          * it should be when searching (e.g. ACLs).
849                          */
850                         ch_free( a_vals[i].bv_val );
851                         if (last > i ) {
852                                 a_vals[i] = a_vals[last];
853                                 if ( pa_nvals ) {
854                                         (*pa_nvals)[i] = (*pa_nvals)[last];
855                                 }
856                         }
857                         BER_BVZERO( &a_vals[last] );
858                         if ( pa_nvals ) {
859                                 BER_BVZERO( &(*pa_nvals)[last] );
860                         }
861                         last--;
862                         break;
863                 
864                 case LDAP_SUCCESS:
865                         if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
866                                 char    *newurl;
867
868                                 ludp->lud_dn = dn.bv_val;
869                                 newurl = ldap_url_desc2str( ludp );
870                                 ludp->lud_dn = olddn.bv_val;
871                                 ch_free( dn.bv_val );
872                                 if ( newurl == NULL ) {
873                                         /* FIXME: leave attr untouched
874                                          * even if ldap_url_desc2str failed...
875                                          */
876                                         break;
877                                 }
878
879                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
880                                 LDAP_FREE( newurl );
881
882                                 if ( pa_nvals ) {
883                                         ludp->lud_dn = ndn.bv_val;
884                                         newurl = ldap_url_desc2str( ludp );
885                                         ludp->lud_dn = olddn.bv_val;
886                                         ch_free( ndn.bv_val );
887                                         if ( newurl == NULL ) {
888                                                 /* FIXME: leave attr untouched
889                                                  * even if ldap_url_desc2str failed...
890                                                  */
891                                                 ch_free( a_vals[i].bv_val );
892                                                 a_vals[i] = oldval;
893                                                 break;
894                                         }
895
896                                         if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
897                                                 ch_free( (*pa_nvals)[i].bv_val );
898                                         }
899                                         ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
900                                         LDAP_FREE( newurl );
901                                 }
902
903                                 ch_free( oldval.bv_val );
904                                 ludp->lud_dn = olddn.bv_val;
905                         }
906                         break;
907
908                 default:
909                         /* leave attr untouched if massage failed */
910                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
911                                 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
912                         }
913                         break;
914                 }
915                 ldap_free_urldesc( ludp );
916         }
917         
918         return 0;
919 }
920
921 /*
922  * I don't like this much, but we need two different
923  * functions because different heap managers may be
924  * in use in back-ldap/meta to reduce the amount of
925  * calls to malloc routines, and some of the free()
926  * routines may be macros with args
927  */
928 int
929 rwm_dnattr_rewrite(
930         Operation               *op,
931         SlapReply               *rs,
932         void                    *cookie,
933         BerVarray               a_vals,
934         BerVarray               *pa_nvals )
935 {
936         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
937         struct ldaprwmap        *rwmap = 
938                         (struct ldaprwmap *)on->on_bi.bi_private;
939
940         int                     i, last;
941
942         dncookie                dc;
943         struct berval           dn = BER_BVNULL,
944                                 ndn = BER_BVNULL;
945         BerVarray               in;
946
947         if ( a_vals ) {
948                 in = a_vals;
949
950         } else {
951                 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
952                         return LDAP_OTHER;
953                 }
954                 in = *pa_nvals;
955         }
956
957         /*
958          * Rewrite the dn if needed
959          */
960         dc.rwmap = rwmap;
961 #ifdef ENABLE_REWRITE
962         dc.conn = op->o_conn;
963         dc.rs = rs;
964         dc.ctx = (char *)cookie;
965 #else /* ! ENABLE_REWRITE */
966         dc.tofrom = ((int *)cookie)[0];
967         dc.normalized = 0;
968 #endif /* ! ENABLE_REWRITE */
969
970         for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
971         last--;
972         if ( pa_nvals != NULL ) {
973                 if ( *pa_nvals == NULL ) {
974                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
975                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
976                 }
977         }
978
979         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
980                 int             rc;
981
982                 if ( a_vals ) {
983                         dn = in[i];
984                         if ( pa_nvals ) {
985                                 ndn = (*pa_nvals)[i];
986                                 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
987                         } else {
988                                 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
989                         }
990                 } else {
991                         ndn = in[i];
992                         rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
993                 }
994
995                 switch ( rc ) {
996                 case LDAP_UNWILLING_TO_PERFORM:
997                         /*
998                          * FIXME: need to check if it may be considered 
999                          * legal to trim values when adding/modifying;
1000                          * it should be when searching (e.g. ACLs).
1001                          */
1002                         ch_free( in[i].bv_val );
1003                         if (last > i ) {
1004                                 in[i] = in[last];
1005                                 if ( a_vals && pa_nvals ) {
1006                                         (*pa_nvals)[i] = (*pa_nvals)[last];
1007                                 }
1008                         }
1009                         BER_BVZERO( &in[last] );
1010                         if ( a_vals && pa_nvals ) {
1011                                 BER_BVZERO( &(*pa_nvals)[last] );
1012                         }
1013                         last--;
1014                         break;
1015                 
1016                 case LDAP_SUCCESS:
1017                         if ( a_vals ) {
1018                                 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1019                                         ch_free( a_vals[i].bv_val );
1020                                         a_vals[i] = dn;
1021
1022                                         if ( pa_nvals ) {
1023                                                 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1024                                                         ch_free( (*pa_nvals)[i].bv_val );
1025                                                 }
1026                                                 (*pa_nvals)[i] = ndn;
1027                                         }
1028                                 }
1029                                 
1030                         } else {
1031                                 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1032                                         ch_free( (*pa_nvals)[i].bv_val );
1033                                         (*pa_nvals)[i] = ndn;
1034                                 }
1035                         }
1036                         break;
1037
1038                 default:
1039                         /* leave attr untouched if massage failed */
1040                         if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1041                                 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1042                         }
1043                         break;
1044                 }
1045         }
1046         
1047         return 0;
1048 }
1049
1050 int
1051 rwm_referral_result_rewrite(
1052         dncookie                *dc,
1053         BerVarray               a_vals
1054 )
1055 {
1056         int             i, last;
1057
1058         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1059         last--;
1060
1061         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1062                 struct berval   dn, olddn;
1063                 int             rc;
1064                 LDAPURLDesc     *ludp;
1065
1066                 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1067                 if ( rc != LDAP_URL_SUCCESS ) {
1068                         /* leave attr untouched if massage failed */
1069                         continue;
1070                 }
1071
1072                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1073                  * thru ldap_url_parse() and ldap_url_desc2str()
1074                  * get rewritten as "ldap:///dc=suffix??base";
1075                  * we don't want this to occur... */
1076                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1077                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1078                 }
1079
1080                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1081
1082                 dn = olddn;
1083                 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1084                 switch ( rc ) {
1085                 case LDAP_UNWILLING_TO_PERFORM:
1086                         /*
1087                          * FIXME: need to check if it may be considered 
1088                          * legal to trim values when adding/modifying;
1089                          * it should be when searching (e.g. ACLs).
1090                          */
1091                         ch_free( a_vals[i].bv_val );
1092                         if ( last > i ) {
1093                                 a_vals[i] = a_vals[last];
1094                         }
1095                         BER_BVZERO( &a_vals[last] );
1096                         last--;
1097                         i--;
1098                         break;
1099
1100                 default:
1101                         /* leave attr untouched if massage failed */
1102                         if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1103                                 char    *newurl;
1104
1105                                 ludp->lud_dn = dn.bv_val;
1106                                 newurl = ldap_url_desc2str( ludp );
1107                                 if ( newurl == NULL ) {
1108                                         /* FIXME: leave attr untouched
1109                                          * even if ldap_url_desc2str failed...
1110                                          */
1111                                         break;
1112                                 }
1113
1114                                 ch_free( a_vals[i].bv_val );
1115                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1116                                 LDAP_FREE( newurl );
1117                                 ludp->lud_dn = olddn.bv_val;
1118                         }
1119                         break;
1120                 }
1121
1122                 ldap_free_urldesc( ludp );
1123         }
1124
1125         return 0;
1126 }
1127
1128 int
1129 rwm_dnattr_result_rewrite(
1130         dncookie                *dc,
1131         BerVarray               a_vals
1132 )
1133 {
1134         int             i, last;
1135
1136         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1137         last--;
1138
1139         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1140                 struct berval   dn;
1141                 int             rc;
1142                 
1143                 dn = a_vals[i];
1144                 rc = rwm_dn_massage_pretty( dc, &a_vals[i], &dn );
1145                 switch ( rc ) {
1146                 case LDAP_UNWILLING_TO_PERFORM:
1147                         /*
1148                          * FIXME: need to check if it may be considered 
1149                          * legal to trim values when adding/modifying;
1150                          * it should be when searching (e.g. ACLs).
1151                          */
1152                         ch_free( a_vals[i].bv_val );
1153                         if ( last > i ) {
1154                                 a_vals[i] = a_vals[last];
1155                         }
1156                         BER_BVZERO( &a_vals[last] );
1157                         last--;
1158                         break;
1159
1160                 default:
1161                         /* leave attr untouched if massage failed */
1162                         if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1163                                 ch_free( a_vals[i].bv_val );
1164                                 a_vals[i] = dn;
1165                         }
1166                         break;
1167                 }
1168         }
1169
1170         return 0;
1171 }
1172
1173 void
1174 rwm_mapping_free( void *v_mapping )
1175 {
1176         struct ldapmapping *mapping = v_mapping;
1177
1178         if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1179                 ch_free( mapping[0].m_src.bv_val );
1180         }
1181
1182         if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1183                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1184                         if ( mapping[0].m_src_oc ) {
1185                                 ch_free( mapping[0].m_src_oc );
1186                         }
1187
1188                 } else {
1189                         if ( mapping[0].m_src_ad ) {
1190                                 ch_free( mapping[0].m_src_ad );
1191                         }
1192                 }
1193         }
1194
1195         if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1196                 ch_free( mapping[0].m_dst.bv_val );
1197         }
1198
1199         if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1200                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1201                         if ( mapping[0].m_dst_oc ) {
1202                                 ch_free( mapping[0].m_dst_oc );
1203                         }
1204
1205                 } else {
1206                         if ( mapping[0].m_dst_ad ) {
1207                                 ch_free( mapping[0].m_dst_ad );
1208                         }
1209                 }
1210         }
1211
1212         ch_free( mapping );
1213
1214 }
1215
1216 #endif /* SLAPD_OVER_RWM */