]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmmap.c
1fa9c506d4631bb23a7256911447af6166f2c06f
[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 != NULL );
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[0].m_src_ad, &text );
86         if ( rc != LDAP_SUCCESS ) {
87                 return rc;
88         }
89
90         mapping[0].m_dst_ad = mapping[0].m_src_ad;
91         ber_dupbv( &mapping[0].m_src, &mapping[0].m_src_ad->ad_cname );
92         ber_dupbv( &mapping[0].m_dst, &mapping[0].m_src );
93
94         mapping[1].m_src = mapping[0].m_src;
95         mapping[1].m_dst = mapping[0].m_dst;
96         mapping[1].m_src_ad = mapping[0].m_src_ad;
97         mapping[1].m_dst_ad = mapping[1].m_src_ad;
98
99         avl_insert( &lm->map, (caddr_t)&mapping[0], 
100                         rwm_mapping_cmp, rwm_mapping_dup );
101         avl_insert( &lm->remap, (caddr_t)&mapping[1], 
102                         rwm_mapping_cmp, rwm_mapping_dup );
103
104         *m = mapping;
105
106         return rc;
107 }
108
109 int
110 rwm_mapping( struct ldapmap *map, struct berval *s, struct ldapmapping **m, int remap )
111 {
112         Avlnode *tree;
113         struct ldapmapping fmapping;
114
115         assert( m != NULL );
116
117         if ( remap == RWM_REMAP ) {
118                 tree = map->remap;
119
120         } else {
121                 tree = map->map;
122         }
123
124         fmapping.m_src = *s;
125         *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping,
126                         rwm_mapping_cmp );
127
128         if ( *m == NULL ) {
129                 return map->drop_missing;
130         }
131
132         return 0;
133 }
134
135 void
136 rwm_map( struct ldapmap *map, struct berval *s, struct berval *bv, int remap )
137 {
138         struct ldapmapping *mapping;
139
140         BER_BVZERO( bv );
141         ( void )rwm_mapping( map, s, &mapping, remap );
142         if ( mapping != NULL ) {
143                 if ( !BER_BVISNULL( &mapping->m_dst ) ) {
144                         *bv = mapping->m_dst;
145                 }
146                 return;
147         }
148
149         if ( !map->drop_missing ) {
150                 *bv = *s;
151         }
152 }
153
154 /*
155  * Map attribute names in place
156  */
157 int
158 rwm_map_attrnames(
159                 struct ldapmap *at_map,
160                 struct ldapmap *oc_map,
161                 AttributeName *an,
162                 AttributeName **anp,
163                 int remap
164 )
165 {
166         int             i, j;
167
168         assert( anp != NULL );
169
170         *anp = NULL;
171
172         if ( an == NULL ) {
173                 return LDAP_SUCCESS;
174         }
175
176         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
177                 /* just count */ ;
178         *anp = ch_malloc( ( i + 1 )* sizeof( AttributeName ) );
179         if ( *anp == NULL ) {
180                 return LDAP_NO_MEMORY;
181         }
182
183         for ( i = 0, j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
184                 struct ldapmapping      *m;
185                 int                     at_drop_missing = 0,
186                                         oc_drop_missing = 0;
187
188                 if ( an[i].an_desc ) {
189                         if ( !at_map ) {
190                                 /* FIXME: better leave as is? */
191                                 continue;
192                         }
193                                 
194                         at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
195                         if ( at_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
196                                 continue;
197                         }
198
199                         if ( !m ) {
200                                 (*anp)[j] = an[i];
201                                 j++;
202                                 continue;
203                         }
204
205                         (*anp)[j] = an[i];
206                         if ( remap == RWM_MAP ) {
207                                 (*anp)[j].an_name = m->m_dst;
208                                 (*anp)[j].an_desc = m->m_dst_ad;
209                         } else {
210                                 (*anp)[j].an_name = m->m_src;
211                                 (*anp)[j].an_desc = m->m_src_ad;
212
213                         }
214
215                         j++;
216                         continue;
217
218                 } else if ( an[i].an_oc ) {
219                         if ( !oc_map ) {
220                                 /* FIXME: better leave as is? */
221                                 continue;
222                         }
223
224                         oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
225
226                         if ( oc_drop_missing || ( m && BER_BVISNULL( &m->m_dst ) ) ) {
227                                 continue;
228                         }
229
230                         if ( !m ) {
231                                 (*anp)[j] = an[i];
232                                 j++;
233                                 continue;
234                         }
235
236                         (*anp)[j] = an[i];
237                         if ( remap == RWM_MAP ) {
238                                 (*anp)[j].an_name = m->m_dst;
239                                 (*anp)[j].an_oc = m->m_dst_oc;
240                         } else {
241                                 (*anp)[j].an_name = m->m_src;
242                                 (*anp)[j].an_oc = m->m_src_oc;
243                         }
244
245                 } else {
246                         at_drop_missing = rwm_mapping( at_map, &an[i].an_name, &m, remap );
247                 
248                         if ( at_drop_missing || !m ) {
249
250                                 oc_drop_missing = rwm_mapping( oc_map, &an[i].an_name, &m, remap );
251
252                                 /* if both at_map and oc_map required to drop missing,
253                                  * then do it */
254                                 if ( oc_drop_missing && at_drop_missing ) {
255                                         continue;
256                                 }
257
258                                 /* if no oc_map mapping was found and at_map required
259                                  * to drop missing, then do it; otherwise, at_map wins
260                                  * and an is considered an attr and is left unchanged */
261                                 if ( !m ) {
262                                         if ( at_drop_missing ) {
263                                                 continue;
264                                         }
265                                         (*anp)[j] = an[i];
266                                         j++;
267                                         continue;
268                                 }
269         
270                                 if ( BER_BVISNULL( &m->m_dst ) ) {
271                                         continue;
272                                 }
273
274                                 (*anp)[j] = an[i];
275                                 if ( remap == RWM_MAP ) {
276                                         (*anp)[j].an_name = m->m_dst;
277                                         (*anp)[j].an_oc = m->m_dst_oc;
278                                 } else {
279                                         (*anp)[j].an_name = m->m_src;
280                                         (*anp)[j].an_oc = m->m_src_oc;
281                                 }
282                                 j++;
283                                 continue;
284                         }
285
286                         if ( !BER_BVISNULL( &m->m_dst ) ) {
287                                 (*anp)[j] = an[i];
288                                 if ( remap == RWM_MAP ) {
289                                         (*anp)[j].an_name = m->m_dst;
290                                         (*anp)[j].an_desc = m->m_dst_ad;
291                                 } else {
292                                         (*anp)[j].an_name = m->m_src;
293                                         (*anp)[j].an_desc = m->m_src_ad;
294                                 }
295                                 j++;
296                                 continue;
297                         }
298                 }
299         }
300
301         if ( j == 0 && i != 0 ) {
302                 memset( &(*anp)[0], 0, sizeof( AttributeName ) );
303                 BER_BVSTR( &(*anp)[0].an_name, LDAP_NO_ATTRS );
304         }
305         memset( &(*anp)[j], 0, sizeof( AttributeName ) );
306
307         return LDAP_SUCCESS;
308 }
309
310 int
311 rwm_map_attrs(
312                 struct ldapmap *at_map,
313                 AttributeName *an,
314                 int remap,
315                 char ***mapped_attrs
316 )
317 {
318         int i, j;
319         char **na;
320
321         if ( an == NULL ) {
322                 *mapped_attrs = NULL;
323                 return LDAP_SUCCESS;
324         }
325
326         for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ); i++ ) {
327                 /*  */
328         }
329
330         na = (char **)ch_calloc( i + 1, sizeof( char * ) );
331         if ( na == NULL ) {
332                 *mapped_attrs = NULL;
333                 return LDAP_NO_MEMORY;
334         }
335
336         for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
337                 struct ldapmapping      *mapping;
338                 
339                 if ( rwm_mapping( at_map, &an[i].an_name, &mapping, remap ) ) {
340                         continue;
341                 }
342
343                 if ( !mapping ) {
344                         na[ j++ ] = an[ i ].an_name.bv_val;
345                         
346                 } else if ( !BER_BVISNULL( &mapping->m_dst ) ) {
347                         na[ j++ ] = mapping->m_dst.bv_val;
348                 }
349         }
350
351         if ( j == 0 && i != 0 ) {
352                 na[ j++ ] = LDAP_NO_ATTRS;
353         }
354
355         na[ j ] = NULL;
356
357         *mapped_attrs = na;
358
359         return LDAP_SUCCESS;
360 }
361
362 static int
363 map_attr_value(
364                 dncookie                *dc,
365                 AttributeDescription    **adp,
366                 struct berval           *mapped_attr,
367                 struct berval           *value,
368                 struct berval           *mapped_value,
369                 int                     remap )
370 {
371         struct berval           vtmp = BER_BVNULL;
372         int                     freeval = 0;
373         AttributeDescription    *ad = *adp;
374         struct ldapmapping      *mapping = NULL;
375
376         rwm_mapping( &dc->rwmap->rwm_at, &ad->ad_cname, &mapping, remap );
377         if ( mapping == NULL ) {
378                 if ( dc->rwmap->rwm_at.drop_missing ) {
379                         return -1;
380                 }
381
382                 *mapped_attr = ad->ad_cname;
383
384         } else {
385                 *mapped_attr = mapping->m_dst;
386         }
387
388         if ( value != NULL ) {
389                 assert( mapped_value != NULL );
390
391                 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
392                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
393                 {
394                         dncookie        fdc = *dc;
395                         int             rc;
396
397 #ifdef ENABLE_REWRITE
398                         fdc.ctx = "searchFilterAttrDN";
399 #endif /* ENABLE_REWRITE */
400
401                         vtmp = *value;
402                         rc = rwm_dn_massage_normalize( &fdc, value, &vtmp );
403                         switch ( rc ) {
404                         case LDAP_SUCCESS:
405                                 if ( vtmp.bv_val != value->bv_val ) {
406                                         freeval = 1;
407                                 }
408                                 break;
409                 
410                         case LDAP_UNWILLING_TO_PERFORM:
411                         case LDAP_OTHER:
412                         default:
413                                 return -1;
414                         }
415
416                 } else if ( ad == slap_schema.si_ad_objectClass
417                                 || ad == slap_schema.si_ad_structuralObjectClass )
418                 {
419                         rwm_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
420                         if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
421                                 vtmp = *value;
422                         }
423                 
424                 } else {
425                         vtmp = *value;
426                 }
427
428                 filter_escape_value( &vtmp, mapped_value );
429
430                 if ( freeval ) {
431                         ch_free( vtmp.bv_val );
432                 }
433         }
434         
435         if ( mapping != NULL ) {
436                 assert( mapping->m_dst_ad != NULL );
437                 *adp = mapping->m_dst_ad;
438         }
439
440         return 0;
441 }
442
443 static int
444 rwm_int_filter_map_rewrite(
445                 Operation               *op,
446                 dncookie                *dc,
447                 Filter                  *f,
448                 struct berval           *fstr )
449 {
450         int             i;
451         Filter          *p;
452         struct berval   atmp,
453                         vtmp,
454                         *tmp;
455         static struct berval
456                         /* better than nothing... */
457                         ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
458                         ber_bvtf_false = BER_BVC( "(|)" ),
459                         /* better than nothing... */
460                         ber_bvtrue = BER_BVC( "(objectClass=*)" ),
461                         ber_bvtf_true = BER_BVC( "(&)" ),
462 #if 0
463                         /* no longer needed; preserved for completeness */
464                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
465 #endif
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 LDAP_OTHER;
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                         goto computed;
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                         goto computed;
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                         goto computed;
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                         goto computed;
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                         goto computed;
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                         goto computed;
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                         int     rc;
629
630                         len = fstr->bv_len;
631
632                         rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
633                         if ( rc != LDAP_SUCCESS ) {
634                                 return rc;
635                         }
636                         
637                         fstr->bv_len += vtmp.bv_len;
638                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
639
640                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
641                                 /*"("*/ "%s)", vtmp.bv_val );
642
643                         ch_free( vtmp.bv_val );
644                 }
645
646                 break;
647
648         case LDAP_FILTER_EXT: {
649                 if ( f->f_mr_desc ) {
650                         if ( map_attr_value( dc, &f->f_mr_desc, &atmp,
651                                                 &f->f_mr_value, &vtmp, RWM_MAP ) )
652                         {
653                                 goto computed;
654                         }
655
656                 } else {
657                         BER_BVSTR( &atmp, "" );
658                         filter_escape_value( &f->f_mr_value, &vtmp );
659                 }
660                         
661
662                 fstr->bv_len = atmp.bv_len +
663                         ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
664                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
665                         vtmp.bv_len + STRLENOF( "(:=)" );
666                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
667
668                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
669                         atmp.bv_val,
670                         f->f_mr_dnattrs ? ":dn" : "",
671                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
672                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
673                         vtmp.bv_val );
674                 ch_free( vtmp.bv_val );
675                 break;
676         }
677
678         case 0:
679 computed:;
680                 filter_free_x( op, f );
681                 f->f_choice = SLAPD_FILTER_COMPUTED;
682                 f->f_result = SLAPD_COMPARE_UNDEFINED;
683                 /* fallthru */
684
685         case SLAPD_FILTER_COMPUTED:
686                 switch ( f->f_result ) {
687                 case LDAP_COMPARE_FALSE:
688                 /* FIXME: treat UNDEFINED as FALSE */
689                 case SLAPD_COMPARE_UNDEFINED:
690                         if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
691                                 tmp = &ber_bvtf_false;
692                                 break;
693                         }
694                         tmp = &ber_bvfalse;
695                         break;
696
697                 case LDAP_COMPARE_TRUE:
698                         if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
699                                 tmp = &ber_bvtf_true;
700                                 break;
701                         }
702                         tmp = &ber_bvtrue;
703                         break;
704                         
705                 default:
706                         tmp = &ber_bverror;
707                         break;
708                 }
709
710                 ber_dupbv( fstr, tmp );
711                 break;
712                 
713         default:
714                 ber_dupbv( fstr, &ber_bvunknown );
715                 break;
716         }
717
718         return LDAP_SUCCESS;
719 }
720
721 int
722 rwm_filter_map_rewrite(
723                 Operation               *op,
724                 dncookie                *dc,
725                 Filter                  *f,
726                 struct berval           *fstr )
727 {
728         int             rc;
729         dncookie        fdc;
730         struct berval   ftmp;
731
732         rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
733
734 #ifdef ENABLE_REWRITE
735         if ( rc != 0 ) {
736                 return rc;
737         }
738
739         fdc = *dc;
740         ftmp = *fstr;
741
742         fdc.ctx = "searchFilter";
743
744         switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx, 
745                                 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ), 
746                                 fdc.conn, &fstr->bv_val ) )
747         {
748         case REWRITE_REGEXEC_OK:
749                 if ( !BER_BVISNULL( fstr ) ) {
750                         fstr->bv_len = strlen( fstr->bv_val );
751                         ch_free( ftmp.bv_val );
752
753                 } else {
754                         *fstr = ftmp;
755                 }
756
757                 Debug( LDAP_DEBUG_ARGS,
758                         "[rw] %s: \"%s\" -> \"%s\"\n",
759                         fdc.ctx, ftmp.bv_val, fstr->bv_val );           
760                 rc = LDAP_SUCCESS;
761                 break;
762                 
763         case REWRITE_REGEXEC_UNWILLING:
764                 if ( fdc.rs ) {
765                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
766                         fdc.rs->sr_text = "Operation not allowed";
767                 }
768                 rc = LDAP_UNWILLING_TO_PERFORM;
769                 break;
770                 
771         case REWRITE_REGEXEC_ERR:
772                 if ( fdc.rs ) {
773                         fdc.rs->sr_err = LDAP_OTHER;
774                         fdc.rs->sr_text = "Rewrite error";
775                 }
776                 rc = LDAP_OTHER;
777                 break;
778         }
779 #endif /* ENABLE_REWRITE */
780
781         return rc;
782 }
783
784 /*
785  * I don't like this much, but we need two different
786  * functions because different heap managers may be
787  * in use in back-ldap/meta to reduce the amount of
788  * calls to malloc routines, and some of the free()
789  * routines may be macros with args
790  */
791 int
792 rwm_referral_rewrite(
793         Operation               *op,
794         SlapReply               *rs,
795         void                    *cookie,
796         BerVarray               a_vals,
797         BerVarray               *pa_nvals )
798 {
799         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
800         struct ldaprwmap        *rwmap = 
801                         (struct ldaprwmap *)on->on_bi.bi_private;
802
803         int                     i, last;
804
805         dncookie                dc;
806         struct berval           dn = BER_BVNULL,
807                                 ndn = BER_BVNULL;
808
809         assert( a_vals != NULL );
810
811         /*
812          * Rewrite the dn if needed
813          */
814         dc.rwmap = rwmap;
815 #ifdef ENABLE_REWRITE
816         dc.conn = op->o_conn;
817         dc.rs = rs;
818         dc.ctx = (char *)cookie;
819 #else /* ! ENABLE_REWRITE */
820         dc.tofrom = ((int *)cookie)[0];
821         dc.normalized = 0;
822 #endif /* ! ENABLE_REWRITE */
823
824         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
825                 ;
826         last--;
827         
828         if ( pa_nvals != NULL ) {
829                 if ( *pa_nvals == NULL ) {
830                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
831                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
832                 }
833         }
834
835         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
836                 struct berval   olddn = BER_BVNULL,
837                                 oldval;
838                 int             rc;
839                 LDAPURLDesc     *ludp;
840
841                 oldval = a_vals[i];
842                 rc = ldap_url_parse( oldval.bv_val, &ludp );
843                 if ( rc != LDAP_URL_SUCCESS ) {
844                         /* leave attr untouched if massage failed */
845                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
846                                 ber_dupbv( &(*pa_nvals)[i], &oldval );
847                         }
848                         continue;
849                 }
850
851                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
852                  * thru ldap_url_parse() and ldap_url_desc2str() 
853                  * get rewritten as "ldap:///dc=suffix??base";
854                  * we don't want this to occur... */
855                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
856                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
857                 }
858
859                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
860
861                 dn = olddn;
862                 if ( pa_nvals ) {
863                         ndn = olddn;
864                         rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
865                                         &dn, &ndn );
866                 } else {
867                         rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
868                 }
869
870                 switch ( rc ) {
871                 case LDAP_UNWILLING_TO_PERFORM:
872                         /*
873                          * FIXME: need to check if it may be considered 
874                          * legal to trim values when adding/modifying;
875                          * it should be when searching (e.g. ACLs).
876                          */
877                         ch_free( a_vals[i].bv_val );
878                         if (last > i ) {
879                                 a_vals[i] = a_vals[last];
880                                 if ( pa_nvals ) {
881                                         (*pa_nvals)[i] = (*pa_nvals)[last];
882                                 }
883                         }
884                         BER_BVZERO( &a_vals[last] );
885                         if ( pa_nvals ) {
886                                 BER_BVZERO( &(*pa_nvals)[last] );
887                         }
888                         last--;
889                         break;
890                 
891                 case LDAP_SUCCESS:
892                         if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
893                                 char    *newurl;
894
895                                 ludp->lud_dn = dn.bv_val;
896                                 newurl = ldap_url_desc2str( ludp );
897                                 ludp->lud_dn = olddn.bv_val;
898                                 ch_free( dn.bv_val );
899                                 if ( newurl == NULL ) {
900                                         /* FIXME: leave attr untouched
901                                          * even if ldap_url_desc2str failed...
902                                          */
903                                         break;
904                                 }
905
906                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
907                                 LDAP_FREE( newurl );
908
909                                 if ( pa_nvals ) {
910                                         ludp->lud_dn = ndn.bv_val;
911                                         newurl = ldap_url_desc2str( ludp );
912                                         ludp->lud_dn = olddn.bv_val;
913                                         ch_free( ndn.bv_val );
914                                         if ( newurl == NULL ) {
915                                                 /* FIXME: leave attr untouched
916                                                  * even if ldap_url_desc2str failed...
917                                                  */
918                                                 ch_free( a_vals[i].bv_val );
919                                                 a_vals[i] = oldval;
920                                                 break;
921                                         }
922
923                                         if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
924                                                 ch_free( (*pa_nvals)[i].bv_val );
925                                         }
926                                         ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
927                                         LDAP_FREE( newurl );
928                                 }
929
930                                 ch_free( oldval.bv_val );
931                                 ludp->lud_dn = olddn.bv_val;
932                         }
933                         break;
934
935                 default:
936                         /* leave attr untouched if massage failed */
937                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
938                                 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
939                         }
940                         break;
941                 }
942                 ldap_free_urldesc( ludp );
943         }
944         
945         return 0;
946 }
947
948 /*
949  * I don't like this much, but we need two different
950  * functions because different heap managers may be
951  * in use in back-ldap/meta to reduce the amount of
952  * calls to malloc routines, and some of the free()
953  * routines may be macros with args
954  */
955 int
956 rwm_dnattr_rewrite(
957         Operation               *op,
958         SlapReply               *rs,
959         void                    *cookie,
960         BerVarray               a_vals,
961         BerVarray               *pa_nvals )
962 {
963         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
964         struct ldaprwmap        *rwmap = 
965                         (struct ldaprwmap *)on->on_bi.bi_private;
966
967         int                     i, last;
968
969         dncookie                dc;
970         struct berval           dn = BER_BVNULL,
971                                 ndn = BER_BVNULL;
972         BerVarray               in;
973
974         if ( a_vals ) {
975                 in = a_vals;
976
977         } else {
978                 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
979                         return LDAP_OTHER;
980                 }
981                 in = *pa_nvals;
982         }
983
984         /*
985          * Rewrite the dn if needed
986          */
987         dc.rwmap = rwmap;
988 #ifdef ENABLE_REWRITE
989         dc.conn = op->o_conn;
990         dc.rs = rs;
991         dc.ctx = (char *)cookie;
992 #else /* ! ENABLE_REWRITE */
993         dc.tofrom = ((int *)cookie)[0];
994         dc.normalized = 0;
995 #endif /* ! ENABLE_REWRITE */
996
997         for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
998         last--;
999         if ( pa_nvals != NULL ) {
1000                 if ( *pa_nvals == NULL ) {
1001                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1002                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1003                 }
1004         }
1005
1006         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1007                 int             rc;
1008
1009                 if ( a_vals ) {
1010                         dn = in[i];
1011                         if ( pa_nvals ) {
1012                                 ndn = (*pa_nvals)[i];
1013                                 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1014                         } else {
1015                                 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1016                         }
1017                 } else {
1018                         ndn = in[i];
1019                         rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1020                 }
1021
1022                 switch ( rc ) {
1023                 case LDAP_UNWILLING_TO_PERFORM:
1024                         /*
1025                          * FIXME: need to check if it may be considered 
1026                          * legal to trim values when adding/modifying;
1027                          * it should be when searching (e.g. ACLs).
1028                          */
1029                         ch_free( in[i].bv_val );
1030                         if (last > i ) {
1031                                 in[i] = in[last];
1032                                 if ( a_vals && pa_nvals ) {
1033                                         (*pa_nvals)[i] = (*pa_nvals)[last];
1034                                 }
1035                         }
1036                         BER_BVZERO( &in[last] );
1037                         if ( a_vals && pa_nvals ) {
1038                                 BER_BVZERO( &(*pa_nvals)[last] );
1039                         }
1040                         last--;
1041                         break;
1042                 
1043                 case LDAP_SUCCESS:
1044                         if ( a_vals ) {
1045                                 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1046                                         ch_free( a_vals[i].bv_val );
1047                                         a_vals[i] = dn;
1048
1049                                         if ( pa_nvals ) {
1050                                                 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1051                                                         ch_free( (*pa_nvals)[i].bv_val );
1052                                                 }
1053                                                 (*pa_nvals)[i] = ndn;
1054                                         }
1055                                 }
1056                                 
1057                         } else {
1058                                 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1059                                         ch_free( (*pa_nvals)[i].bv_val );
1060                                         (*pa_nvals)[i] = ndn;
1061                                 }
1062                         }
1063                         break;
1064
1065                 default:
1066                         /* leave attr untouched if massage failed */
1067                         if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1068                                 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1069                         }
1070                         break;
1071                 }
1072         }
1073         
1074         return 0;
1075 }
1076
1077 int
1078 rwm_referral_result_rewrite(
1079         dncookie                *dc,
1080         BerVarray               a_vals
1081 )
1082 {
1083         int             i, last;
1084
1085         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1086         last--;
1087
1088         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1089                 struct berval   dn,
1090                                 olddn = BER_BVNULL;
1091                 int             rc;
1092                 LDAPURLDesc     *ludp;
1093
1094                 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1095                 if ( rc != LDAP_URL_SUCCESS ) {
1096                         /* leave attr untouched if massage failed */
1097                         continue;
1098                 }
1099
1100                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1101                  * thru ldap_url_parse() and ldap_url_desc2str()
1102                  * get rewritten as "ldap:///dc=suffix??base";
1103                  * we don't want this to occur... */
1104                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1105                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1106                 }
1107
1108                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1109
1110                 dn = olddn;
1111                 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1112                 switch ( rc ) {
1113                 case LDAP_UNWILLING_TO_PERFORM:
1114                         /*
1115                          * FIXME: need to check if it may be considered 
1116                          * legal to trim values when adding/modifying;
1117                          * it should be when searching (e.g. ACLs).
1118                          */
1119                         ch_free( a_vals[i].bv_val );
1120                         if ( last > i ) {
1121                                 a_vals[i] = a_vals[last];
1122                         }
1123                         BER_BVZERO( &a_vals[last] );
1124                         last--;
1125                         i--;
1126                         break;
1127
1128                 default:
1129                         /* leave attr untouched if massage failed */
1130                         if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1131                                 char    *newurl;
1132
1133                                 ludp->lud_dn = dn.bv_val;
1134                                 newurl = ldap_url_desc2str( ludp );
1135                                 if ( newurl == NULL ) {
1136                                         /* FIXME: leave attr untouched
1137                                          * even if ldap_url_desc2str failed...
1138                                          */
1139                                         break;
1140                                 }
1141
1142                                 ch_free( a_vals[i].bv_val );
1143                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1144                                 LDAP_FREE( newurl );
1145                                 ludp->lud_dn = olddn.bv_val;
1146                         }
1147                         break;
1148                 }
1149
1150                 ldap_free_urldesc( ludp );
1151         }
1152
1153         return 0;
1154 }
1155
1156 int
1157 rwm_dnattr_result_rewrite(
1158         dncookie                *dc,
1159         BerVarray               a_vals
1160 )
1161 {
1162         int             i, last;
1163
1164         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1165         last--;
1166
1167         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1168                 struct berval   dn;
1169                 int             rc;
1170                 
1171                 dn = a_vals[i];
1172                 rc = rwm_dn_massage_pretty( dc, &a_vals[i], &dn );
1173                 switch ( rc ) {
1174                 case LDAP_UNWILLING_TO_PERFORM:
1175                         /*
1176                          * FIXME: need to check if it may be considered 
1177                          * legal to trim values when adding/modifying;
1178                          * it should be when searching (e.g. ACLs).
1179                          */
1180                         ch_free( a_vals[i].bv_val );
1181                         if ( last > i ) {
1182                                 a_vals[i] = a_vals[last];
1183                         }
1184                         BER_BVZERO( &a_vals[last] );
1185                         last--;
1186                         break;
1187
1188                 default:
1189                         /* leave attr untouched if massage failed */
1190                         if ( !BER_BVISNULL( &dn ) && a_vals[i].bv_val != dn.bv_val ) {
1191                                 ch_free( a_vals[i].bv_val );
1192                                 a_vals[i] = dn;
1193                         }
1194                         break;
1195                 }
1196         }
1197
1198         return 0;
1199 }
1200
1201 void
1202 rwm_mapping_dst_free( void *v_mapping )
1203 {
1204         struct ldapmapping *mapping = v_mapping;
1205
1206         if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1207                 rwm_mapping_free( &mapping[ -1 ] );
1208         }
1209 }
1210
1211 void
1212 rwm_mapping_free( void *v_mapping )
1213 {
1214         struct ldapmapping *mapping = v_mapping;
1215
1216         if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1217                 ch_free( mapping[0].m_src.bv_val );
1218         }
1219
1220         if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1221                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1222                         if ( mapping[0].m_src_oc ) {
1223                                 ch_free( mapping[0].m_src_oc );
1224                         }
1225
1226                 } else {
1227                         if ( mapping[0].m_src_ad ) {
1228                                 ch_free( mapping[0].m_src_ad );
1229                         }
1230                 }
1231         }
1232
1233         if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1234                 ch_free( mapping[0].m_dst.bv_val );
1235         }
1236
1237         if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1238                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1239                         if ( mapping[0].m_dst_oc ) {
1240                                 ch_free( mapping[0].m_dst_oc );
1241                         }
1242
1243                 } else {
1244                         if ( mapping[0].m_dst_ad ) {
1245                                 ch_free( mapping[0].m_dst_ad );
1246                         }
1247                 }
1248         }
1249
1250         ch_free( mapping );
1251
1252 }
1253
1254 #endif /* SLAPD_OVER_RWM */