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