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