]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmmap.c
remap compare attribute; fix erroneous pointers; minor cleanup
[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-2004 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      *m;
336                 
337                 if ( rwm_mapping( at_map, &an[i].an_name, &m, remap ) ) {
338                         continue;
339                 }
340
341                 if ( !m || ( m && !BER_BVISNULL( &m->m_dst ) ) ) {
342                         na[j++] = m->m_dst.bv_val;
343                 }
344         }
345         if ( j == 0 && i != 0 ) {
346                 na[j++] = LDAP_NO_ATTRS;
347         }
348         na[j] = NULL;
349
350         *mapped_attrs = na;
351         return LDAP_SUCCESS;
352 }
353
354 static int
355 map_attr_value(
356                 dncookie                *dc,
357                 AttributeDescription    *ad,
358                 struct berval           *mapped_attr,
359                 struct berval           *value,
360                 struct berval           *mapped_value,
361                 int                     remap )
362 {
363         struct berval           vtmp;
364         int                     freeval = 0;
365
366         rwm_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
367         if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
368                 /*
369                  * FIXME: are we sure we need to search oc_map if at_map fails?
370                  */
371                 rwm_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
372                 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) )
373                 {
374                         *mapped_attr = ad->ad_cname;
375                 }
376         }
377
378         if ( value == NULL ) {
379                 return 0;
380         }
381
382         if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
383         {
384                 dncookie        fdc = *dc;
385                 int             rc;
386
387 #ifdef ENABLE_REWRITE
388                 fdc.ctx = "searchFilterAttrDN";
389 #endif /* ENABLE_REWRITE */
390
391                 rc = rwm_dn_massage( &fdc, value, NULL, &vtmp );
392                 switch ( rc ) {
393                 case LDAP_SUCCESS:
394                         if ( vtmp.bv_val != value->bv_val ) {
395                                 freeval = 1;
396                         }
397                         break;
398                 
399                 case LDAP_UNWILLING_TO_PERFORM:
400                 case LDAP_OTHER:
401                 default:
402                         return -1;
403                 }
404
405         } else if ( ad == slap_schema.si_ad_objectClass
406                         || ad == slap_schema.si_ad_structuralObjectClass )
407         {
408                 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
409                 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
410                         vtmp = *value;
411                 }
412                 
413         } else {
414                 vtmp = *value;
415         }
416
417         filter_escape_value( &vtmp, mapped_value );
418
419         if ( freeval ) {
420                 ch_free( vtmp.bv_val );
421         }
422         
423         return 0;
424 }
425
426 static int
427 rwm_int_filter_map_rewrite(
428                 dncookie                *dc,
429                 Filter                  *f,
430                 struct berval           *fstr )
431 {
432         int             i;
433         Filter          *p;
434         struct berval   atmp,
435                         vtmp,
436                         tmp;
437         static struct berval
438                         ber_bvfalse = BER_BVC( "(?=false)" ),
439                         ber_bvtrue = BER_BVC( "(?=true)" ),
440                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
441                         ber_bverror = BER_BVC( "(?=error)" ),
442                         ber_bvunknown = BER_BVC( "(?=unknown)" ),
443                         ber_bvnone = BER_BVC( "(?=none)" );
444         ber_len_t       len;
445
446         if ( f == NULL ) {
447                 ber_dupbv( fstr, &ber_bvnone );
448                 return -1;
449         }
450
451         switch ( f->f_choice ) {
452         case LDAP_FILTER_EQUALITY:
453                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
454                                         &f->f_av_value, &vtmp, RWM_MAP ) )
455                 {
456                         return -1;
457                 }
458
459                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
460                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
461
462                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
463                         atmp.bv_val, vtmp.bv_val );
464
465                 ch_free( vtmp.bv_val );
466                 break;
467
468         case LDAP_FILTER_GE:
469                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
470                                         &f->f_av_value, &vtmp, RWM_MAP ) )
471                 {
472                         return -1;
473                 }
474
475                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
476                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
477
478                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
479                         atmp.bv_val, vtmp.bv_val );
480
481                 ch_free( vtmp.bv_val );
482                 break;
483
484         case LDAP_FILTER_LE:
485                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
486                                         &f->f_av_value, &vtmp, RWM_MAP ) )
487                 {
488                         return -1;
489                 }
490
491                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
492                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
493
494                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
495                         atmp.bv_val, vtmp.bv_val );
496
497                 ch_free( vtmp.bv_val );
498                 break;
499
500         case LDAP_FILTER_APPROX:
501                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
502                                         &f->f_av_value, &vtmp, RWM_MAP ) )
503                 {
504                         return -1;
505                 }
506
507                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
508                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
509
510                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
511                         atmp.bv_val, vtmp.bv_val );
512
513                 ch_free( vtmp.bv_val );
514                 break;
515
516         case LDAP_FILTER_SUBSTRINGS:
517                 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
518                                         NULL, NULL, RWM_MAP ) )
519                 {
520                         return -1;
521                 }
522
523                 /* cannot be a DN ... */
524
525                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
526                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
527
528                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
529                         atmp.bv_val );
530
531                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
532                         len = fstr->bv_len;
533
534                         filter_escape_value( &f->f_sub_initial, &vtmp );
535
536                         fstr->bv_len += vtmp.bv_len;
537                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
538
539                         snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
540                                 /* "(attr=" */ "%s*)",
541                                 vtmp.bv_val );
542
543                         ch_free( vtmp.bv_val );
544                 }
545
546                 if ( f->f_sub_any != NULL ) {
547                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
548                                 len = fstr->bv_len;
549                                 filter_escape_value( &f->f_sub_any[i], &vtmp );
550
551                                 fstr->bv_len += vtmp.bv_len + 1;
552                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
553
554                                 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
555                                         /* "(attr=[init]*[any*]" */ "%s*)",
556                                         vtmp.bv_val );
557                                 ch_free( vtmp.bv_val );
558                         }
559                 }
560
561                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
562                         len = fstr->bv_len;
563
564                         filter_escape_value( &f->f_sub_final, &vtmp );
565
566                         fstr->bv_len += vtmp.bv_len;
567                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
568
569                         snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
570                                 /* "(attr=[init*][any*]" */ "%s)",
571                                 vtmp.bv_val );
572
573                         ch_free( vtmp.bv_val );
574                 }
575
576                 break;
577
578         case LDAP_FILTER_PRESENT:
579                 if ( map_attr_value( dc, f->f_desc, &atmp,
580                                         NULL, NULL, RWM_MAP ) )
581                 {
582                         return -1;
583                 }
584
585                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
586                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
587
588                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
589                         atmp.bv_val );
590                 break;
591
592         case LDAP_FILTER_AND:
593         case LDAP_FILTER_OR:
594         case LDAP_FILTER_NOT:
595                 fstr->bv_len = STRLENOF( "(%)" );
596                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
597
598                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
599                         f->f_choice == LDAP_FILTER_AND ? '&' :
600                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
601
602                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
603                         len = fstr->bv_len;
604
605                         if ( rwm_int_filter_map_rewrite( dc, p, &vtmp ) )
606                         {
607                                 return -1;
608                         }
609                         
610                         fstr->bv_len += vtmp.bv_len;
611                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
612
613                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
614                                 /*"("*/ "%s)", vtmp.bv_val );
615
616                         ch_free( vtmp.bv_val );
617                 }
618
619                 break;
620
621         case LDAP_FILTER_EXT: {
622                 if ( f->f_mr_desc ) {
623                         if ( map_attr_value( dc, f->f_mr_desc, &atmp,
624                                                 &f->f_mr_value, &vtmp, RWM_MAP ) )
625                         {
626                                 return -1;
627                         }
628
629                 } else {
630                         BER_BVSTR( &atmp, "" );
631                         filter_escape_value( &f->f_mr_value, &vtmp );
632                 }
633                         
634
635                 fstr->bv_len = atmp.bv_len +
636                         ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
637                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
638                         vtmp.bv_len + STRLENOF( "(:=)" );
639                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
640
641                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
642                         atmp.bv_val,
643                         f->f_mr_dnattrs ? ":dn" : "",
644                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
645                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
646                         vtmp.bv_val );
647                 ch_free( vtmp.bv_val );
648                 break;
649         }
650
651         case SLAPD_FILTER_COMPUTED:
652                 switch ( f->f_result ) {
653                 case LDAP_COMPARE_FALSE:
654                         tmp = ber_bvfalse;
655                         break;
656
657                 case LDAP_COMPARE_TRUE:
658                         tmp = ber_bvtrue;
659                         break;
660                         
661                 case SLAPD_COMPARE_UNDEFINED:
662                         tmp = ber_bvundefined;
663                         break;
664                         
665                 default:
666                         tmp = ber_bverror;
667                         break;
668                 }
669
670                 ber_dupbv( fstr, &tmp );
671                 break;
672                 
673         default:
674                 ber_dupbv( fstr, &ber_bvunknown );
675                 break;
676         }
677
678         return 0;
679 }
680
681 int
682 rwm_filter_map_rewrite(
683                 dncookie                *dc,
684                 Filter                  *f,
685                 struct berval           *fstr )
686 {
687         int             rc;
688         dncookie        fdc;
689         struct berval   ftmp;
690
691         rc = rwm_int_filter_map_rewrite( dc, f, fstr );
692
693 #ifdef ENABLE_REWRITE
694         if ( rc != LDAP_SUCCESS ) {
695                 return rc;
696         }
697
698         fdc = *dc;
699         ftmp = *fstr;
700
701         fdc.ctx = "searchFilter";
702
703         switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx, 
704                                 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ), 
705                                 fdc.conn, &fstr->bv_val ) )
706         {
707         case REWRITE_REGEXEC_OK:
708                 if ( !BER_BVISNULL( fstr ) ) {
709                         fstr->bv_len = strlen( fstr->bv_val );
710                         ch_free( ftmp.bv_val );
711
712                 } else {
713                         *fstr = ftmp;
714                 }
715
716                 Debug( LDAP_DEBUG_ARGS,
717                         "[rw] %s: \"%s\" -> \"%s\"\n",
718                         fdc.ctx, ftmp.bv_val, fstr->bv_val );           
719                 rc = LDAP_SUCCESS;
720                 break;
721                 
722         case REWRITE_REGEXEC_UNWILLING:
723                 if ( fdc.rs ) {
724                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
725                         fdc.rs->sr_text = "Operation not allowed";
726                 }
727                 rc = LDAP_UNWILLING_TO_PERFORM;
728                 break;
729                 
730         case REWRITE_REGEXEC_ERR:
731                 if ( fdc.rs ) {
732                         fdc.rs->sr_err = LDAP_OTHER;
733                         fdc.rs->sr_text = "Rewrite error";
734                 }
735                 rc = LDAP_OTHER;
736                 break;
737         }
738
739 #endif /* ENABLE_REWRITE */
740         return rc;
741 }
742
743 /*
744  * I don't like this much, but we need two different
745  * functions because different heap managers may be
746  * in use in back-ldap/meta to reduce the amount of
747  * calls to malloc routines, and some of the free()
748  * routines may be macros with args
749  */
750 int
751 rwm_referral_rewrite(
752         Operation               *op,
753         SlapReply               *rs,
754         void                    *cookie,
755         BerVarray               a_vals,
756         BerVarray               *pa_nvals )
757 {
758         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
759         struct ldaprwmap        *rwmap = 
760                         (struct ldaprwmap *)on->on_bi.bi_private;
761
762         int                     i, last;
763
764         dncookie                dc;
765         struct berval           dn, ndn, *ndnp = NULL;
766
767         assert( a_vals );
768
769         /*
770          * Rewrite the dn if needed
771          */
772         dc.rwmap = rwmap;
773 #ifdef ENABLE_REWRITE
774         dc.conn = op->o_conn;
775         dc.rs = rs;
776         dc.ctx = (char *)cookie;
777 #else /* ! ENABLE_REWRITE */
778         dc.tofrom = ((int *)cookie)[0];
779         dc.normalized = 0;
780 #endif /* ! ENABLE_REWRITE */
781
782         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
783         if ( pa_nvals != NULL ) {
784                 ndnp = &ndn;
785
786                 if ( *pa_nvals == NULL ) {
787                         *pa_nvals = ch_malloc( last * sizeof(struct berval) );
788                         memset( *pa_nvals, 0, last * sizeof(struct berval) );
789                 }
790         }
791         last--;
792
793         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
794                 struct berval   olddn, oldval;
795                 int             rc;
796                 LDAPURLDesc     *ludp;
797
798                 oldval = a_vals[i];
799                 rc = ldap_url_parse( oldval.bv_val, &ludp );
800                 if ( rc != LDAP_URL_SUCCESS ) {
801                         /* leave attr untouched if massage failed */
802                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
803                                 ber_dupbv( &(*pa_nvals)[i], &oldval );
804                         }
805                         continue;
806                 }
807                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
808
809                 rc = rwm_dn_massage( &dc, &olddn, &dn, ndnp );
810                 switch ( rc ) {
811                 case LDAP_UNWILLING_TO_PERFORM:
812                         /*
813                          * FIXME: need to check if it may be considered 
814                          * legal to trim values when adding/modifying;
815                          * it should be when searching (e.g. ACLs).
816                          */
817                         ch_free( a_vals[i].bv_val );
818                         if (last > i ) {
819                                 a_vals[i] = a_vals[last];
820                                 if ( pa_nvals ) {
821                                         (*pa_nvals)[i] = (*pa_nvals)[last];
822                                 }
823                         }
824                         BER_BVZERO( &a_vals[last] );
825                         if ( pa_nvals ) {
826                                 BER_BVZERO( &(*pa_nvals)[last] );
827                         }
828                         last--;
829                         break;
830                 
831                 case LDAP_SUCCESS:
832                         if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
833                                 char    *newurl;
834
835                                 ludp->lud_dn = dn.bv_val;
836                                 newurl = ldap_url_desc2str( ludp );
837                                 if ( newurl == NULL ) {
838                                         /* FIXME: leave attr untouched
839                                          * even if ldap_url_desc2str failed... */
840                                         break;
841                                 }
842
843                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
844                                 LDAP_FREE( newurl );
845
846                                 if ( pa_nvals ) {
847                                         ludp->lud_dn = ndn.bv_val;
848                                         newurl = ldap_url_desc2str( ludp );
849                                         if ( newurl == NULL ) {
850                                                 /* FIXME: leave attr untouched
851                                                  * even if ldap_url_desc2str failed... */
852                                                 ch_free( a_vals[i].bv_val );
853                                                 a_vals[i] = oldval;
854                                                 break;
855                                         }
856
857                                         if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
858                                                 ch_free( (*pa_nvals)[i].bv_val );
859                                         }
860                                         ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
861                                         LDAP_FREE( newurl );
862                                 }
863
864                                 ch_free( oldval.bv_val );
865                                 ludp->lud_dn = olddn.bv_val;
866                         }
867                         break;
868
869                 default:
870                         /* leave attr untouched if massage failed */
871                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
872                                 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
873                         }
874                         break;
875                 }
876                 ldap_free_urldesc( ludp );
877         }
878         
879         return 0;
880 }
881
882 /*
883  * I don't like this much, but we need two different
884  * functions because different heap managers may be
885  * in use in back-ldap/meta to reduce the amount of
886  * calls to malloc routines, and some of the free()
887  * routines may be macros with args
888  */
889 int
890 rwm_dnattr_rewrite(
891         Operation               *op,
892         SlapReply               *rs,
893         void                    *cookie,
894         BerVarray               a_vals,
895         BerVarray               *pa_nvals )
896 {
897         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
898         struct ldaprwmap        *rwmap = 
899                         (struct ldaprwmap *)on->on_bi.bi_private;
900
901         int                     i, last;
902
903         dncookie                dc;
904         struct berval           dn, *dnp = NULL, ndn, *ndnp = NULL;
905         BerVarray               in;
906
907         if ( a_vals ) {
908                 in = a_vals;
909                 dnp = &dn;
910
911         } else {
912                 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
913                         return LDAP_OTHER;
914                 }
915                 in = *pa_nvals;
916         }
917
918         /*
919          * Rewrite the dn if needed
920          */
921         dc.rwmap = rwmap;
922 #ifdef ENABLE_REWRITE
923         dc.conn = op->o_conn;
924         dc.rs = rs;
925         dc.ctx = (char *)cookie;
926 #else /* ! ENABLE_REWRITE */
927         dc.tofrom = ((int *)cookie)[0];
928         dc.normalized = 0;
929 #endif /* ! ENABLE_REWRITE */
930
931         for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
932         if ( pa_nvals != NULL ) {
933                 ndnp = &ndn;
934
935                 if ( *pa_nvals == NULL ) {
936                         *pa_nvals = ch_malloc( last * sizeof(struct berval) );
937                         memset( *pa_nvals, 0, last * sizeof(struct berval) );
938                 }
939         }
940         last--;
941
942         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
943                 int             rc;
944
945                 rc = rwm_dn_massage( &dc, &in[i], dnp, ndnp );
946                 switch ( rc ) {
947                 case LDAP_UNWILLING_TO_PERFORM:
948                         /*
949                          * FIXME: need to check if it may be considered 
950                          * legal to trim values when adding/modifying;
951                          * it should be when searching (e.g. ACLs).
952                          */
953                         ch_free( in[i].bv_val );
954                         if (last > i ) {
955                                 in[i] = in[last];
956                                 if ( a_vals && pa_nvals ) {
957                                         (*pa_nvals)[i] = (*pa_nvals)[last];
958                                 }
959                         }
960                         BER_BVZERO( &in[last] );
961                         if ( a_vals && pa_nvals ) {
962                                 BER_BVZERO( &(*pa_nvals)[last] );
963                         }
964                         last--;
965                         break;
966                 
967                 case LDAP_SUCCESS:
968                         if ( a_vals ) {
969                                 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
970                                         ch_free( a_vals[i].bv_val );
971                                         a_vals[i] = dn;
972
973                                         if ( pa_nvals ) {
974                                                 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
975                                                         ch_free( (*pa_nvals)[i].bv_val );
976                                                 }
977                                                 (*pa_nvals)[i] = ndn;
978                                         }
979                                 }
980                                 
981                         } else {
982                                 assert( ndnp != NULL );
983
984                                 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
985                                         ch_free( (*pa_nvals)[i].bv_val );
986                                         (*pa_nvals)[i] = ndn;
987                                 }
988                         }
989                         break;
990
991                 default:
992                         /* leave attr untouched if massage failed */
993                         if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
994                                 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
995                         }
996                         break;
997                 }
998         }
999         
1000         return 0;
1001 }
1002
1003 int
1004 rwm_referral_result_rewrite(
1005         dncookie                *dc,
1006         BerVarray               a_vals
1007 )
1008 {
1009         int             i, last;
1010
1011         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1012         last--;
1013
1014         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1015                 struct berval   dn, olddn;
1016                 int             rc;
1017                 LDAPURLDesc     *ludp;
1018
1019                 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1020                 if ( rc != LDAP_URL_SUCCESS ) {
1021                         /* leave attr untouched if massage failed */
1022                         continue;
1023                 }
1024
1025                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1026                 
1027                 rc = rwm_dn_massage( dc, &olddn, &dn, NULL );
1028                 switch ( rc ) {
1029                 case LDAP_UNWILLING_TO_PERFORM:
1030                         /*
1031                          * FIXME: need to check if it may be considered 
1032                          * legal to trim values when adding/modifying;
1033                          * it should be when searching (e.g. ACLs).
1034                          */
1035                         ch_free( a_vals[i].bv_val );
1036                         if ( last > i ) {
1037                                 a_vals[i] = a_vals[last];
1038                         }
1039                         BER_BVZERO( &a_vals[last] );
1040                         last--;
1041                         i--;
1042                         break;
1043
1044                 default:
1045                         /* leave attr untouched if massage failed */
1046                         if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1047                                 char    *newurl;
1048
1049                                 ludp->lud_dn = dn.bv_val;
1050                                 newurl = ldap_url_desc2str( ludp );
1051                                 if ( newurl == NULL ) {
1052                                         /* FIXME: leave attr untouched
1053                                          * even if ldap_url_desc2str failed... */
1054                                         break;
1055                                 }
1056
1057                                 ch_free( a_vals[i].bv_val );
1058                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1059                                 LDAP_FREE( newurl );
1060                                 ludp->lud_dn = olddn.bv_val;
1061                         }
1062                         break;
1063                 }
1064
1065                 ldap_free_urldesc( ludp );
1066         }
1067
1068         return 0;
1069 }
1070
1071 int
1072 rwm_dnattr_result_rewrite(
1073         dncookie                *dc,
1074         BerVarray               a_vals
1075 )
1076 {
1077         int             i, last;
1078
1079         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1080         last--;
1081
1082         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1083                 struct berval   dn;
1084                 int             rc;
1085                 
1086                 rc = rwm_dn_massage( dc, &a_vals[i], &dn, NULL );
1087                 switch ( rc ) {
1088                 case LDAP_UNWILLING_TO_PERFORM:
1089                         /*
1090                          * FIXME: need to check if it may be considered 
1091                          * legal to trim values when adding/modifying;
1092                          * it should be when searching (e.g. ACLs).
1093                          */
1094                         ch_free( a_vals[i].bv_val );
1095                         if ( last > i ) {
1096                                 a_vals[i] = a_vals[last];
1097                         }
1098                         BER_BVZERO( &a_vals[last] );
1099                         last--;
1100                         break;
1101
1102                 default:
1103                         /* leave attr untouched if massage failed */
1104                         if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1105                                 ch_free( a_vals[i].bv_val );
1106                                 a_vals[i] = dn;
1107                         }
1108                         break;
1109                 }
1110         }
1111
1112         return 0;
1113 }
1114
1115 void
1116 rwm_mapping_free( void *v_mapping )
1117 {
1118         struct ldapmapping *mapping = v_mapping;
1119
1120         if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1121                 ch_free( mapping[0].m_src.bv_val );
1122         }
1123
1124         if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1125                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1126                         if ( mapping[0].m_src_oc ) {
1127                                 ch_free( mapping[0].m_src_oc );
1128                         }
1129
1130                 } else {
1131                         if ( mapping[0].m_src_ad ) {
1132                                 ch_free( mapping[0].m_src_ad );
1133                         }
1134                 }
1135         }
1136
1137         if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1138                 ch_free( mapping[0].m_dst.bv_val );
1139         }
1140
1141         if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1142                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1143                         if ( mapping[0].m_dst_oc ) {
1144                                 ch_free( mapping[0].m_dst_oc );
1145                         }
1146
1147                 } else {
1148                         if ( mapping[0].m_dst_ad ) {
1149                                 ch_free( mapping[0].m_dst_ad );
1150                         }
1151                 }
1152         }
1153
1154         ch_free( mapping );
1155
1156 }
1157
1158 #endif /* SLAPD_OVER_RWM */