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