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