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