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