]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmmap.c
9d79679be31ccc694c49d70f784e5e74b5bb1e0b
[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;
82         }
83
84         rc = slap_str2ad( "objectClass", &mapping->m_src_ad, &text );
85         if ( rc != LDAP_SUCCESS ) {
86                 return rc;
87         }
88
89         mapping->m_dst_ad = mapping->m_src_ad;
90         ber_dupbv( &mapping->m_dst, &mapping->m_src_ad->ad_cname );
91         ber_dupbv( &mapping->m_dst, &mapping->m_src );
92
93         mapping[1].m_src = mapping->m_src;
94         mapping[1].m_dst = mapping->m_dst;
95
96         avl_insert( &lm->map, (caddr_t)mapping, 
97                         rwm_mapping_cmp, rwm_mapping_dup );
98         avl_insert( &lm->remap, (caddr_t)&mapping[1], 
99                         rwm_mapping_cmp, rwm_mapping_dup );
100
101         *m = mapping;
102
103         return rc;
104 }
105
106 int
107 rwm_mapping( struct ldapmap *map, struct berval *s, struct ldapmapping **m, int remap )
108 {
109         Avlnode *tree;
110         struct ldapmapping fmapping;
111
112         assert( m );
113
114         if ( remap == RWM_REMAP ) {
115                 tree = map->remap;
116
117         } else {
118                 tree = map->map;
119         }
120
121         fmapping.m_src = *s;
122         *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping,
123                         rwm_mapping_cmp );
124
125         if ( *m == NULL ) {
126                 return map->drop_missing;
127         }
128
129         return 0;
130 }
131
132 void
133 rwm_map( struct ldapmap *map, struct berval *s, struct berval *bv, int remap )
134 {
135         struct ldapmapping *mapping;
136
137         BER_BVZERO( bv );
138         rwm_mapping( map, s, &mapping, remap );
139         if ( mapping != NULL ) {
140                 if ( !BER_BVISNULL( &mapping->m_dst ) ) {
141                         *bv = mapping->m_dst;
142                 }
143                 return;
144         }
145
146         if ( !map->drop_missing ) {
147                 *bv = *s;
148         }
149 }
150
151 /*
152  * Map attribute names in place
153  */
154 int
155 rwm_map_attrnames(
156                 struct ldapmap *at_map,
157                 struct ldapmap *oc_map,
158                 AttributeName *an,
159                 AttributeName **anp,
160                 int remap
161 )
162 {
163         int             i, j;
164
165         if ( an == NULL ) {
166                 return LDAP_SUCCESS;
167         }
168
169         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
170                 /* just count */ ;
171         *anp = ch_malloc( ( i + 1 )* sizeof( AttributeName ) );
172
173         for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
174                 struct ldapmapping      *m;
175                 int                     at_drop_missing = 0,
176                                         oc_drop_missing = 0;
177
178                 if ( an[i].an_desc ) {
179                         if ( !at_map ) {
180                                 /* FIXME: better leave as is? */
181                                 continue;
182                         }
183                                 
184                         at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
185                         if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
186                                 continue;
187                         }
188
189                         if ( !m ) {
190                                 (*anp)[j] = an[i];
191                                 j++;
192                                 continue;
193                         }
194
195                         (*anp)[j] = an[i];
196                         if ( remap == RWM_MAP ) {
197                                 (*anp)[j].an_name = m->m_dst;
198                                 (*anp)[j].an_desc = m->m_dst_ad;
199                         } else {
200                                 (*anp)[j].an_name = m->m_src;
201                                 (*anp)[j].an_desc = m->m_src_ad;
202
203                         }
204
205                         j++;
206                         continue;
207
208                 } else if ( an[i].an_oc ) {
209                         if ( !oc_map ) {
210                                 /* FIXME: better leave as is? */
211                                 continue;
212                         }
213
214                         oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
215
216                         if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
217                                 continue;
218                         }
219
220                         if ( !m ) {
221                                 (*anp)[j] = an[i];
222                                 j++;
223                                 continue;
224                         }
225
226                         (*anp)[j] = an[i];
227                         if ( remap == RWM_MAP ) {
228                                 (*anp)[j].an_name = m->m_dst;
229                                 (*anp)[j].an_oc = m->m_dst_oc;
230                         } else {
231                                 (*anp)[j].an_name = m->m_src;
232                                 (*anp)[j].an_oc = m->m_src_oc;
233                         }
234
235                 } else {
236                         at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
237                 
238                         if ( at_drop_missing || !m ) {
239
240                                 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
241
242                                 /* if both at_map and oc_map required to drop missing,
243                                  * then do it */
244                                 if ( oc_drop_missing && at_drop_missing ) {
245                                         continue;
246                                 }
247
248                                 /* if no oc_map mapping was found and at_map required
249                                  * to drop missing, then do it; otherwise, at_map wins
250                                  * and an is considered an attr and is left unchanged */
251                                 if ( !m ) {
252                                         if ( at_drop_missing ) {
253                                                 continue;
254                                         }
255                                         (*anp)[j] = an[i];
256                                         j++;
257                                         continue;
258                                 }
259         
260                                 if ( BER_BVISNULL( &m->m_dst ) ) {
261                                         continue;
262                                 }
263
264                                 (*anp)[j] = an[i];
265                                 if ( remap == RWM_MAP ) {
266                                         (*anp)[j].an_name = m->m_dst;
267                                         (*anp)[j].an_oc = m->m_dst_oc;
268                                 } else {
269                                         (*anp)[j].an_name = m->m_src;
270                                         (*anp)[j].an_oc = m->m_src_oc;
271                                 }
272                                 j++;
273                                 continue;
274                         }
275
276                         if ( !BER_BVISNULL( &m->m_dst ) ) {
277                                 (*anp)[j] = an[i];
278                                 if ( remap == RWM_MAP ) {
279                                         (*anp)[j].an_name = m->m_dst;
280                                         (*anp)[j].an_desc = m->m_dst_ad;
281                                 } else {
282                                         (*anp)[j].an_name = m->m_src;
283                                         (*anp)[j].an_desc = m->m_src_ad;
284                                 }
285                                 j++;
286                                 continue;
287                         }
288                 }
289         }
290
291         if ( j == 0 && i != 0 ) {
292                 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
293                 BER_BVSTR( &(*anp)[0].an_name, LDAP_NO_ATTRS );
294         }
295         memset( &(*anp)[j], 0, sizeof( AttributeName ) );
296
297         return LDAP_SUCCESS;
298 }
299
300 int
301 rwm_map_attrs(
302                 struct ldapmap *at_map,
303                 AttributeName *an,
304                 int remap,
305                 char ***mapped_attrs
306 )
307 {
308         int i, j;
309         char **na;
310
311         if ( an == NULL ) {
312                 *mapped_attrs = NULL;
313                 return LDAP_SUCCESS;
314         }
315
316         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
317                 /*  */
318         }
319
320         na = (char **)ch_calloc( i + 1, sizeof( char * ) );
321         if (na == NULL) {
322                 *mapped_attrs = NULL;
323                 return LDAP_NO_MEMORY;
324         }
325
326         for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
327                 struct ldapmapping      *m;
328                 
329                 if ( rwm_mapping( at_map, &an[i].an_name, &m, remap ) ) {
330                         continue;
331                 }
332
333                 if ( !m || ( m && !BER_BVISNULL( &m->m_dst ) ) ) {
334                         na[j++] = m->m_dst.bv_val;
335                 }
336         }
337         if ( j == 0 && i != 0 ) {
338                 na[j++] = LDAP_NO_ATTRS;
339         }
340         na[j] = NULL;
341
342         *mapped_attrs = na;
343         return LDAP_SUCCESS;
344 }
345
346 static int
347 map_attr_value(
348                 dncookie                *dc,
349                 AttributeDescription    *ad,
350                 struct berval           *mapped_attr,
351                 struct berval           *value,
352                 struct berval           *mapped_value,
353                 int                     remap )
354 {
355         struct berval           vtmp;
356         int                     freeval = 0;
357
358         rwm_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
359         if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
360                 /*
361                  * FIXME: are we sure we need to search oc_map if at_map fails?
362                  */
363                 rwm_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
364                 if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) )
365                 {
366                         *mapped_attr = ad->ad_cname;
367                 }
368         }
369
370         if ( value == NULL ) {
371                 return 0;
372         }
373
374         if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
375         {
376                 dncookie        fdc = *dc;
377                 int             rc;
378
379 #ifdef ENABLE_REWRITE
380                 fdc.ctx = "searchFilterAttrDN";
381 #endif
382
383                 rc = rwm_dn_massage( &fdc, value, &vtmp, NULL );
384                 switch ( rc ) {
385                 case LDAP_SUCCESS:
386                         if ( vtmp.bv_val != value->bv_val ) {
387                                 freeval = 1;
388                         }
389                         break;
390                 
391                 case LDAP_UNWILLING_TO_PERFORM:
392                 case LDAP_OTHER:
393                 default:
394                         return -1;
395                 }
396
397         } else if ( ad == slap_schema.si_ad_objectClass
398                         || ad == slap_schema.si_ad_structuralObjectClass )
399         {
400                 rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
401                 if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
402                         vtmp = *value;
403                 }
404                 
405         } else {
406                 vtmp = *value;
407         }
408
409         filter_escape_value( &vtmp, mapped_value );
410
411         if ( freeval ) {
412                 ber_memfree( vtmp.bv_val );
413         }
414         
415         return 0;
416 }
417
418 static int
419 rwm_int_filter_map_rewrite(
420                 dncookie                *dc,
421                 Filter                  *f,
422                 struct berval           *fstr )
423 {
424         int             i;
425         Filter          *p;
426         struct berval   atmp,
427                         vtmp,
428                         tmp;
429         static struct berval
430                         ber_bvfalse = BER_BVC( "(?=false)" ),
431                         ber_bvtrue = BER_BVC( "(?=true)" ),
432                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
433                         ber_bverror = BER_BVC( "(?=error)" ),
434                         ber_bvunknown = BER_BVC( "(?=unknown)" ),
435                         ber_bvnone = BER_BVC( "(?=none)" );
436         ber_len_t       len;
437
438         if ( f == NULL ) {
439                 ber_dupbv( fstr, &ber_bvnone );
440                 return -1;
441         }
442
443         switch ( f->f_choice ) {
444         case LDAP_FILTER_EQUALITY:
445                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
446                                         &f->f_av_value, &vtmp, RWM_MAP ) )
447                 {
448                         return -1;
449                 }
450
451                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
452                 fstr->bv_val = malloc( fstr->bv_len + 1 );
453
454                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
455                         atmp.bv_val, vtmp.bv_val );
456
457                 ber_memfree( vtmp.bv_val );
458                 break;
459
460         case LDAP_FILTER_GE:
461                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
462                                         &f->f_av_value, &vtmp, RWM_MAP ) )
463                 {
464                         return -1;
465                 }
466
467                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
468                 fstr->bv_val = malloc( fstr->bv_len + 1 );
469
470                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
471                         atmp.bv_val, vtmp.bv_val );
472
473                 ber_memfree( vtmp.bv_val );
474                 break;
475
476         case LDAP_FILTER_LE:
477                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
478                                         &f->f_av_value, &vtmp, RWM_MAP ) )
479                 {
480                         return -1;
481                 }
482
483                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
484                 fstr->bv_val = malloc( fstr->bv_len + 1 );
485
486                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
487                         atmp.bv_val, vtmp.bv_val );
488
489                 ber_memfree( vtmp.bv_val );
490                 break;
491
492         case LDAP_FILTER_APPROX:
493                 if ( map_attr_value( dc, f->f_av_desc, &atmp,
494                                         &f->f_av_value, &vtmp, RWM_MAP ) )
495                 {
496                         return -1;
497                 }
498
499                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
500                 fstr->bv_val = malloc( fstr->bv_len + 1 );
501
502                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
503                         atmp.bv_val, vtmp.bv_val );
504
505                 ber_memfree( vtmp.bv_val );
506                 break;
507
508         case LDAP_FILTER_SUBSTRINGS:
509                 if ( map_attr_value( dc, f->f_sub_desc, &atmp,
510                                         NULL, NULL, RWM_MAP ) )
511                 {
512                         return -1;
513                 }
514
515                 /* cannot be a DN ... */
516
517                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
518                 fstr->bv_val = malloc( fstr->bv_len + 128 );
519
520                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
521                         atmp.bv_val );
522
523                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
524                         len = fstr->bv_len;
525
526                         filter_escape_value( &f->f_sub_initial, &vtmp );
527
528                         fstr->bv_len += vtmp.bv_len;
529                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
530
531                         snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
532                                 /* "(attr=" */ "%s*)",
533                                 vtmp.bv_val );
534
535                         ber_memfree( vtmp.bv_val );
536                 }
537
538                 if ( f->f_sub_any != NULL ) {
539                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
540                                 len = fstr->bv_len;
541                                 filter_escape_value( &f->f_sub_any[i], &vtmp );
542
543                                 fstr->bv_len += vtmp.bv_len + 1;
544                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
545
546                                 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
547                                         /* "(attr=[init]*[any*]" */ "%s*)",
548                                         vtmp.bv_val );
549                                 ber_memfree( vtmp.bv_val );
550                         }
551                 }
552
553                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
554                         len = fstr->bv_len;
555
556                         filter_escape_value( &f->f_sub_final, &vtmp );
557
558                         fstr->bv_len += vtmp.bv_len;
559                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
560
561                         snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
562                                 /* "(attr=[init*][any*]" */ "%s)",
563                                 vtmp.bv_val );
564
565                         ber_memfree( vtmp.bv_val );
566                 }
567
568                 break;
569
570         case LDAP_FILTER_PRESENT:
571                 if ( map_attr_value( dc, f->f_desc, &atmp,
572                                         NULL, NULL, RWM_MAP ) )
573                 {
574                         return -1;
575                 }
576
577                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
578                 fstr->bv_val = malloc( fstr->bv_len + 1 );
579
580                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
581                         atmp.bv_val );
582                 break;
583
584         case LDAP_FILTER_AND:
585         case LDAP_FILTER_OR:
586         case LDAP_FILTER_NOT:
587                 fstr->bv_len = STRLENOF( "(%)" );
588                 fstr->bv_val = malloc( fstr->bv_len + 128 );
589
590                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
591                         f->f_choice == LDAP_FILTER_AND ? '&' :
592                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
593
594                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
595                         len = fstr->bv_len;
596
597                         if ( rwm_int_filter_map_rewrite( dc, p, &vtmp ) )
598                         {
599                                 return -1;
600                         }
601                         
602                         fstr->bv_len += vtmp.bv_len;
603                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
604
605                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
606                                 /*"("*/ "%s)", vtmp.bv_val );
607
608                         ch_free( vtmp.bv_val );
609                 }
610
611                 break;
612
613         case LDAP_FILTER_EXT: {
614                 if ( f->f_mr_desc ) {
615                         if ( map_attr_value( dc, f->f_mr_desc, &atmp,
616                                                 &f->f_mr_value, &vtmp, RWM_MAP ) )
617                         {
618                                 return -1;
619                         }
620
621                 } else {
622                         BER_BVSTR( &atmp, "" );
623                         filter_escape_value( &f->f_mr_value, &vtmp );
624                 }
625                         
626
627                 fstr->bv_len = atmp.bv_len +
628                         ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
629                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
630                         vtmp.bv_len + STRLENOF( "(:=)" );
631                 fstr->bv_val = malloc( fstr->bv_len + 1 );
632
633                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
634                         atmp.bv_val,
635                         f->f_mr_dnattrs ? ":dn" : "",
636                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
637                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
638                         vtmp.bv_val );
639                 ber_memfree( vtmp.bv_val );
640                 } break;
641
642         case SLAPD_FILTER_COMPUTED:
643                 switch ( f->f_result ) {
644                 case LDAP_COMPARE_FALSE:
645                         tmp = ber_bvfalse;
646                         break;
647
648                 case LDAP_COMPARE_TRUE:
649                         tmp = ber_bvtrue;
650                         break;
651                         
652                 case SLAPD_COMPARE_UNDEFINED:
653                         tmp = ber_bvundefined;
654                         break;
655                         
656                 default:
657                         tmp = ber_bverror;
658                         break;
659                 }
660
661                 ber_dupbv( fstr, &tmp );
662                 break;
663                 
664         default:
665                 ber_dupbv( fstr, &ber_bvunknown );
666                 break;
667         }
668
669         return 0;
670 }
671
672 int
673 rwm_filter_map_rewrite(
674                 dncookie                *dc,
675                 Filter                  *f,
676                 struct berval           *fstr )
677 {
678         int             rc;
679         dncookie        fdc;
680         struct berval   ftmp;
681
682         rc = rwm_int_filter_map_rewrite( dc, f, fstr );
683
684 #ifdef ENABLE_REWRITE
685         if ( rc != LDAP_SUCCESS ) {
686                 return rc;
687         }
688
689         fdc = *dc;
690         ftmp = *fstr;
691
692         fdc.ctx = "searchFilter";
693
694         switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx, 
695                                 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ), 
696                                 fdc.conn, &fstr->bv_val )) {
697         case REWRITE_REGEXEC_OK:
698                 if ( !BER_BVISNULL( fstr ) ) {
699                         fstr->bv_len = strlen( fstr->bv_val );
700                         free( ftmp.bv_val );
701
702                 } else {
703                         *fstr = ftmp;
704                 }
705
706 #ifdef NEW_LOGGING
707                 LDAP_LOG( BACK_LDAP, DETAIL1, 
708                         "[rw] %s: \"%s\" -> \"%s\"\n",
709                         dc->ctx, ftmp.bv_val, fstr->bv_val );           
710 #else /* !NEW_LOGGING */
711                 Debug( LDAP_DEBUG_ARGS,
712                         "[rw] %s: \"%s\" -> \"%s\"\n",
713                         dc->ctx, ftmp.bv_val, fstr->bv_val );           
714 #endif /* !NEW_LOGGING */
715                 rc = LDAP_SUCCESS;
716                 break;
717                 
718         case REWRITE_REGEXEC_UNWILLING:
719                 if ( fdc.rs ) {
720                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
721                         fdc.rs->sr_text = "Operation not allowed";
722                 }
723                 rc = LDAP_UNWILLING_TO_PERFORM;
724                 break;
725                 
726         case REWRITE_REGEXEC_ERR:
727                 if ( fdc.rs ) {
728                         fdc.rs->sr_err = LDAP_OTHER;
729                         fdc.rs->sr_text = "Rewrite error";
730                 }
731                 rc = LDAP_OTHER;
732                 break;
733         }
734
735 #endif /* ENABLE_REWRITE */
736         return rc;
737 }
738
739 /*
740  * I don't like this much, but we need two different
741  * functions because different heap managers may be
742  * in use in back-ldap/meta to reduce the amount of
743  * calls to malloc routines, and some of the free()
744  * routines may be macros with args
745  */
746 int
747 rwm_dnattr_rewrite(
748         Operation               *op,
749         SlapReply               *rs,
750         void                    *cookie,
751         BerVarray               a_vals,
752         BerVarray               *pa_nvals )
753 {
754         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
755         struct ldaprwmap        *rwmap = 
756                         (struct ldaprwmap *)on->on_bi.bi_private;
757
758         int                     i, last;
759
760         dncookie                dc;
761         struct berval           dn, ndn, *pndn = NULL;
762
763         assert( a_vals );
764
765         /*
766          * Rewrite the bind dn if needed
767          */
768         dc.rwmap = rwmap;
769 #ifdef ENABLE_REWRITE
770         dc.conn = op->o_conn;
771         dc.rs = rs;
772         dc.ctx = (char *)cookie;
773 #else
774         dc.tofrom = ((int *)cookie)[0];
775         dc.normalized = 0;
776 #endif
777
778         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
779         if ( pa_nvals != NULL ) {
780                 pndn = &ndn;
781
782                 if ( *pa_nvals == NULL ) {
783                         *pa_nvals = ch_malloc( last * sizeof(struct berval) );
784                         memset( *pa_nvals, 0, last * sizeof(struct berval) );
785                 }
786         }
787         last--;
788
789         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
790                 int             rc;
791
792                 rc = rwm_dn_massage( &dc, &a_vals[i], &dn, pndn );
793                 switch ( rc ) {
794                 case LDAP_UNWILLING_TO_PERFORM:
795                         /*
796                          * FIXME: need to check if it may be considered 
797                          * legal to trim values when adding/modifying;
798                          * it should be when searching (e.g. ACLs).
799                          */
800                         ch_free( a_vals[i].bv_val );
801                         if (last > i ) {
802                                 a_vals[i] = a_vals[last];
803                                 if ( pa_nvals ) {
804                                         (*pa_nvals)[i] = (*pa_nvals)[last];
805                                 }
806                         }
807                         BER_BVZERO( &a_vals[last] );
808                         if ( pa_nvals ) {
809                                 BER_BVZERO( &(*pa_nvals)[last] );
810                         }
811                         last--;
812                         break;
813                 
814                 case LDAP_SUCCESS:
815                         if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
816                                 ch_free( a_vals[i].bv_val );
817                                 a_vals[i] = dn;
818                                 if ( pa_nvals ) {
819                                         if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
820                                                 ch_free( (*pa_nvals)[i].bv_val );
821                                         }
822                                         (*pa_nvals)[i] = *pndn;
823                                 }
824                         }
825                         break;
826
827                 default:
828                         /* leave attr untouched if massage failed */
829                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
830                                 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
831                         }
832                         break;
833                 }
834         }
835         
836         return 0;
837 }
838
839 int
840 rwm_dnattr_result_rewrite(
841         dncookie                *dc,
842         BerVarray               a_vals
843 )
844 {
845         int             i, last;
846
847         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
848         last--;
849
850         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
851                 struct berval   dn;
852                 int             rc;
853                 
854                 rc = rwm_dn_massage( dc, &a_vals[i], &dn, NULL );
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                         LBER_FREE( &a_vals[i].bv_val );
863                         if ( last > i ) {
864                                 a_vals[i] = a_vals[last];
865                         }
866                         BER_BVZERO( &a_vals[last] );
867                         last--;
868                         break;
869
870                 default:
871                         /* leave attr untouched if massage failed */
872                         if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
873                                 LBER_FREE( a_vals[i].bv_val );
874                                 a_vals[i] = dn;
875                         }
876                         break;
877                 }
878         }
879
880         return 0;
881 }
882
883 void
884 rwm_mapping_free( void *v_mapping )
885 {
886         struct ldapmapping *mapping = v_mapping;
887
888         if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
889                 ch_free( mapping[0].m_src.bv_val );
890         }
891
892         if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
893                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
894                         if ( mapping[0].m_src_oc ) {
895                                 ch_free( mapping[0].m_src_oc );
896                         }
897
898                 } else {
899                         if ( mapping[0].m_src_ad ) {
900                                 ch_free( mapping[0].m_src_ad );
901                         }
902                 }
903         }
904
905         if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
906                 ch_free( mapping[0].m_dst.bv_val );
907         }
908
909         if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
910                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
911                         if ( mapping[0].m_dst_oc ) {
912                                 ch_free( mapping[0].m_dst_oc );
913                         }
914
915                 } else {
916                         if ( mapping[0].m_dst_ad ) {
917                                 ch_free( mapping[0].m_dst_ad );
918                         }
919                 }
920         }
921
922         ch_free( mapping );
923
924 }
925
926 #endif /* SLAPD_OVER_RWM */