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