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