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