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