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