]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmmap.c
Fix overlay aux_operational, add contextCSN as operational attribute
[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         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         case REWRITE_REGEXEC_OK:
707                 if ( !BER_BVISNULL( fstr ) ) {
708                         fstr->bv_len = strlen( fstr->bv_val );
709                         ch_free( ftmp.bv_val );
710
711                 } else {
712                         *fstr = ftmp;
713                 }
714
715                 Debug( LDAP_DEBUG_ARGS,
716                         "[rw] %s: \"%s\" -> \"%s\"\n",
717                         dc->ctx, ftmp.bv_val, fstr->bv_val );           
718                 rc = LDAP_SUCCESS;
719                 break;
720                 
721         case REWRITE_REGEXEC_UNWILLING:
722                 if ( fdc.rs ) {
723                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
724                         fdc.rs->sr_text = "Operation not allowed";
725                 }
726                 rc = LDAP_UNWILLING_TO_PERFORM;
727                 break;
728                 
729         case REWRITE_REGEXEC_ERR:
730                 if ( fdc.rs ) {
731                         fdc.rs->sr_err = LDAP_OTHER;
732                         fdc.rs->sr_text = "Rewrite error";
733                 }
734                 rc = LDAP_OTHER;
735                 break;
736         }
737
738 #endif /* ENABLE_REWRITE */
739         return rc;
740 }
741
742 /*
743  * I don't like this much, but we need two different
744  * functions because different heap managers may be
745  * in use in back-ldap/meta to reduce the amount of
746  * calls to malloc routines, and some of the free()
747  * routines may be macros with args
748  */
749 int
750 rwm_referral_rewrite(
751         Operation               *op,
752         SlapReply               *rs,
753         void                    *cookie,
754         BerVarray               a_vals,
755         BerVarray               *pa_nvals )
756 {
757         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
758         struct ldaprwmap        *rwmap = 
759                         (struct ldaprwmap *)on->on_bi.bi_private;
760
761         int                     i, last;
762
763         dncookie                dc;
764         struct berval           dn, ndn, *ndnp = NULL;
765
766         assert( a_vals );
767
768         /*
769          * Rewrite the dn if needed
770          */
771         dc.rwmap = rwmap;
772 #ifdef ENABLE_REWRITE
773         dc.conn = op->o_conn;
774         dc.rs = rs;
775         dc.ctx = (char *)cookie;
776 #else /* ! ENABLE_REWRITE */
777         dc.tofrom = ((int *)cookie)[0];
778         dc.normalized = 0;
779 #endif /* ! ENABLE_REWRITE */
780
781         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
782         if ( pa_nvals != NULL ) {
783                 ndnp = &ndn;
784
785                 if ( *pa_nvals == NULL ) {
786                         *pa_nvals = ch_malloc( last * sizeof(struct berval) );
787                         memset( *pa_nvals, 0, last * sizeof(struct berval) );
788                 }
789         }
790         last--;
791
792         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
793                 struct berval   olddn, oldval;
794                 int             rc;
795                 LDAPURLDesc     *ludp;
796
797                 oldval = a_vals[i];
798                 rc = ldap_url_parse( oldval.bv_val, &ludp );
799                 if ( rc != LDAP_URL_SUCCESS ) {
800                         /* leave attr untouched if massage failed */
801                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
802                                 ber_dupbv( &(*pa_nvals)[i], &oldval );
803                         }
804                         continue;
805                 }
806                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
807
808                 rc = rwm_dn_massage( &dc, &olddn, &dn, ndnp );
809                 switch ( rc ) {
810                 case LDAP_UNWILLING_TO_PERFORM:
811                         /*
812                          * FIXME: need to check if it may be considered 
813                          * legal to trim values when adding/modifying;
814                          * it should be when searching (e.g. ACLs).
815                          */
816                         ch_free( a_vals[i].bv_val );
817                         if (last > i ) {
818                                 a_vals[i] = a_vals[last];
819                                 if ( pa_nvals ) {
820                                         (*pa_nvals)[i] = (*pa_nvals)[last];
821                                 }
822                         }
823                         BER_BVZERO( &a_vals[last] );
824                         if ( pa_nvals ) {
825                                 BER_BVZERO( &(*pa_nvals)[last] );
826                         }
827                         last--;
828                         break;
829                 
830                 case LDAP_SUCCESS:
831                         if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
832                                 char    *newurl;
833
834                                 ludp->lud_dn = dn.bv_val;
835                                 newurl = ldap_url_desc2str( ludp );
836                                 if ( newurl == NULL ) {
837                                         /* FIXME: leave attr untouched
838                                          * even if ldap_url_desc2str failed... */
839                                         break;
840                                 }
841
842                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
843                                 LDAP_FREE( newurl );
844
845                                 if ( pa_nvals ) {
846                                         ludp->lud_dn = ndn.bv_val;
847                                         newurl = ldap_url_desc2str( ludp );
848                                         if ( newurl == NULL ) {
849                                                 /* FIXME: leave attr untouched
850                                                  * even if ldap_url_desc2str failed... */
851                                                 ch_free( a_vals[i].bv_val );
852                                                 a_vals[i] = oldval;
853                                                 break;
854                                         }
855
856                                         if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
857                                                 ch_free( (*pa_nvals)[i].bv_val );
858                                         }
859                                         ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
860                                         LDAP_FREE( newurl );
861                                 }
862
863                                 ch_free( oldval.bv_val );
864                                 ludp->lud_dn = olddn.bv_val;
865                         }
866                         break;
867
868                 default:
869                         /* leave attr untouched if massage failed */
870                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
871                                 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
872                         }
873                         break;
874                 }
875                 ldap_free_urldesc( ludp );
876         }
877         
878         return 0;
879 }
880
881 /*
882  * I don't like this much, but we need two different
883  * functions because different heap managers may be
884  * in use in back-ldap/meta to reduce the amount of
885  * calls to malloc routines, and some of the free()
886  * routines may be macros with args
887  */
888 int
889 rwm_dnattr_rewrite(
890         Operation               *op,
891         SlapReply               *rs,
892         void                    *cookie,
893         BerVarray               a_vals,
894         BerVarray               *pa_nvals )
895 {
896         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
897         struct ldaprwmap        *rwmap = 
898                         (struct ldaprwmap *)on->on_bi.bi_private;
899
900         int                     i, last;
901
902         dncookie                dc;
903         struct berval           dn, *dnp = NULL, ndn, *ndnp = NULL;
904         BerVarray               in;
905
906         if ( a_vals ) {
907                 in = a_vals;
908                 dnp = &dn;
909
910         } else {
911                 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
912                         return LDAP_OTHER;
913                 }
914                 in = *pa_nvals;
915         }
916
917         /*
918          * Rewrite the dn if needed
919          */
920         dc.rwmap = rwmap;
921 #ifdef ENABLE_REWRITE
922         dc.conn = op->o_conn;
923         dc.rs = rs;
924         dc.ctx = (char *)cookie;
925 #else /* ! ENABLE_REWRITE */
926         dc.tofrom = ((int *)cookie)[0];
927         dc.normalized = 0;
928 #endif /* ! ENABLE_REWRITE */
929
930         for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
931         if ( pa_nvals != NULL ) {
932                 ndnp = &ndn;
933
934                 if ( *pa_nvals == NULL ) {
935                         *pa_nvals = ch_malloc( last * sizeof(struct berval) );
936                         memset( *pa_nvals, 0, last * sizeof(struct berval) );
937                 }
938         }
939         last--;
940
941         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
942                 int             rc;
943
944                 rc = rwm_dn_massage( &dc, &in[i], dnp, ndnp );
945                 switch ( rc ) {
946                 case LDAP_UNWILLING_TO_PERFORM:
947                         /*
948                          * FIXME: need to check if it may be considered 
949                          * legal to trim values when adding/modifying;
950                          * it should be when searching (e.g. ACLs).
951                          */
952                         ch_free( in[i].bv_val );
953                         if (last > i ) {
954                                 in[i] = in[last];
955                                 if ( a_vals && pa_nvals ) {
956                                         (*pa_nvals)[i] = (*pa_nvals)[last];
957                                 }
958                         }
959                         BER_BVZERO( &in[last] );
960                         if ( a_vals && pa_nvals ) {
961                                 BER_BVZERO( &(*pa_nvals)[last] );
962                         }
963                         last--;
964                         break;
965                 
966                 case LDAP_SUCCESS:
967                         if ( a_vals ) {
968                                 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
969                                         ch_free( a_vals[i].bv_val );
970                                         a_vals[i] = dn;
971
972                                         if ( pa_nvals ) {
973                                                 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
974                                                         ch_free( (*pa_nvals)[i].bv_val );
975                                                 }
976                                                 (*pa_nvals)[i] = ndn;
977                                         }
978                                 }
979                                 
980                         } else {
981                                 assert( ndnp != NULL );
982
983                                 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
984                                         ch_free( (*pa_nvals)[i].bv_val );
985                                         (*pa_nvals)[i] = ndn;
986                                 }
987                         }
988                         break;
989
990                 default:
991                         /* leave attr untouched if massage failed */
992                         if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
993                                 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
994                         }
995                         break;
996                 }
997         }
998         
999         return 0;
1000 }
1001
1002 int
1003 rwm_referral_result_rewrite(
1004         dncookie                *dc,
1005         BerVarray               a_vals
1006 )
1007 {
1008         int             i, last;
1009
1010         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1011         last--;
1012
1013         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1014                 struct berval   dn, olddn;
1015                 int             rc;
1016                 LDAPURLDesc     *ludp;
1017
1018                 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1019                 if ( rc != LDAP_URL_SUCCESS ) {
1020                         /* leave attr untouched if massage failed */
1021                         continue;
1022                 }
1023
1024                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1025                 
1026                 rc = rwm_dn_massage( dc, &olddn, &dn, NULL );
1027                 switch ( rc ) {
1028                 case LDAP_UNWILLING_TO_PERFORM:
1029                         /*
1030                          * FIXME: need to check if it may be considered 
1031                          * legal to trim values when adding/modifying;
1032                          * it should be when searching (e.g. ACLs).
1033                          */
1034                         ch_free( &a_vals[i].bv_val );
1035                         if ( last > i ) {
1036                                 a_vals[i] = a_vals[last];
1037                         }
1038                         BER_BVZERO( &a_vals[last] );
1039                         last--;
1040                         break;
1041
1042                 default:
1043                         /* leave attr untouched if massage failed */
1044                         if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1045                                 char    *newurl;
1046
1047                                 ludp->lud_dn = dn.bv_val;
1048                                 newurl = ldap_url_desc2str( ludp );
1049                                 if ( newurl == NULL ) {
1050                                         /* FIXME: leave attr untouched
1051                                          * even if ldap_url_desc2str failed... */
1052                                         break;
1053                                 }
1054
1055                                 ch_free( a_vals[i].bv_val );
1056                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1057                                 LDAP_FREE( newurl );
1058                                 ludp->lud_dn = olddn.bv_val;
1059                         }
1060                         break;
1061                 }
1062
1063                 ldap_free_urldesc( ludp );
1064         }
1065
1066         return 0;
1067 }
1068
1069 int
1070 rwm_dnattr_result_rewrite(
1071         dncookie                *dc,
1072         BerVarray               a_vals
1073 )
1074 {
1075         int             i, last;
1076
1077         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1078         last--;
1079
1080         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1081                 struct berval   dn;
1082                 int             rc;
1083                 
1084                 rc = rwm_dn_massage( dc, &a_vals[i], &dn, NULL );
1085                 switch ( rc ) {
1086                 case LDAP_UNWILLING_TO_PERFORM:
1087                         /*
1088                          * FIXME: need to check if it may be considered 
1089                          * legal to trim values when adding/modifying;
1090                          * it should be when searching (e.g. ACLs).
1091                          */
1092                         ch_free( &a_vals[i].bv_val );
1093                         if ( last > i ) {
1094                                 a_vals[i] = a_vals[last];
1095                         }
1096                         BER_BVZERO( &a_vals[last] );
1097                         last--;
1098                         break;
1099
1100                 default:
1101                         /* leave attr untouched if massage failed */
1102                         if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1103                                 ch_free( a_vals[i].bv_val );
1104                                 a_vals[i] = dn;
1105                         }
1106                         break;
1107                 }
1108         }
1109
1110         return 0;
1111 }
1112
1113 void
1114 rwm_mapping_free( void *v_mapping )
1115 {
1116         struct ldapmapping *mapping = v_mapping;
1117
1118         if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1119                 ch_free( mapping[0].m_src.bv_val );
1120         }
1121
1122         if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1123                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1124                         if ( mapping[0].m_src_oc ) {
1125                                 ch_free( mapping[0].m_src_oc );
1126                         }
1127
1128                 } else {
1129                         if ( mapping[0].m_src_ad ) {
1130                                 ch_free( mapping[0].m_src_ad );
1131                         }
1132                 }
1133         }
1134
1135         if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1136                 ch_free( mapping[0].m_dst.bv_val );
1137         }
1138
1139         if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1140                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1141                         if ( mapping[0].m_dst_oc ) {
1142                                 ch_free( mapping[0].m_dst_oc );
1143                         }
1144
1145                 } else {
1146                         if ( mapping[0].m_dst_ad ) {
1147                                 ch_free( mapping[0].m_dst_ad );
1148                         }
1149                 }
1150         }
1151
1152         ch_free( mapping );
1153
1154 }
1155
1156 #endif /* SLAPD_OVER_RWM */