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