]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmmap.c
Happy New Year
[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 ( f->f_choice & SLAPD_FILTER_UNDEFINED ) {
545                 goto computed;
546         }
547
548         switch ( f->f_choice & SLAPD_FILTER_MASK ) {
549         case LDAP_FILTER_EQUALITY:
550                 ad = f->f_av_desc;
551                 if ( map_attr_value( dc, &ad, &atmp,
552                         &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
553                 {
554                         goto computed;
555                 }
556
557                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(=)" );
558                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
559
560                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
561                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
562
563                 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
564                 break;
565
566         case LDAP_FILTER_GE:
567                 ad = f->f_av_desc;
568                 if ( map_attr_value( dc, &ad, &atmp,
569                         &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
570                 {
571                         goto computed;
572                 }
573
574                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(>=)" );
575                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
576
577                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
578                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
579
580                 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
581                 break;
582
583         case LDAP_FILTER_LE:
584                 ad = f->f_av_desc;
585                 if ( map_attr_value( dc, &ad, &atmp,
586                         &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
587                 {
588                         goto computed;
589                 }
590
591                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(<=)" );
592                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
593
594                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
595                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
596
597                 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
598                 break;
599
600         case LDAP_FILTER_APPROX:
601                 ad = f->f_av_desc;
602                 if ( map_attr_value( dc, &ad, &atmp,
603                         &f->f_av_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
604                 {
605                         goto computed;
606                 }
607
608                 fstr->bv_len = atmp.bv_len + vtmp.bv_len + STRLENOF( "(~=)" );
609                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
610
611                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
612                         atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
613
614                 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
615                 break;
616
617         case LDAP_FILTER_SUBSTRINGS:
618                 ad = f->f_sub_desc;
619                 if ( map_attr_value( dc, &ad, &atmp,
620                         NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
621                 {
622                         goto computed;
623                 }
624
625                 /* cannot be a DN ... */
626
627                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
628                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
629
630                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
631                         atmp.bv_val );
632
633                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
634                         len = fstr->bv_len;
635
636                         filter_escape_value_x( &f->f_sub_initial, &vtmp, op->o_tmpmemctx );
637
638                         fstr->bv_len += vtmp.bv_len;
639                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
640                                 op->o_tmpmemctx );
641
642                         snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
643                                 /* "(attr=" */ "%s*)",
644                                 vtmp.bv_len ? vtmp.bv_val : "" );
645
646                         op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
647                 }
648
649                 if ( f->f_sub_any != NULL ) {
650                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
651                                 len = fstr->bv_len;
652                                 filter_escape_value_x( &f->f_sub_any[i], &vtmp,
653                                         op->o_tmpmemctx );
654
655                                 fstr->bv_len += vtmp.bv_len + 1;
656                                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
657                                         op->o_tmpmemctx );
658
659                                 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
660                                         /* "(attr=[init]*[any*]" */ "%s*)",
661                                         vtmp.bv_len ? vtmp.bv_val : "" );
662                                 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
663                         }
664                 }
665
666                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
667                         len = fstr->bv_len;
668
669                         filter_escape_value_x( &f->f_sub_final, &vtmp, op->o_tmpmemctx );
670
671                         fstr->bv_len += vtmp.bv_len;
672                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
673                                 op->o_tmpmemctx );
674
675                         snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
676                                 /* "(attr=[init*][any*]" */ "%s)",
677                                 vtmp.bv_len ? vtmp.bv_val : "" );
678
679                         op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
680                 }
681
682                 break;
683
684         case LDAP_FILTER_PRESENT:
685                 ad = f->f_desc;
686                 if ( map_attr_value( dc, &ad, &atmp,
687                         NULL, NULL, RWM_MAP, op->o_tmpmemctx ) )
688                 {
689                         goto computed;
690                 }
691
692                 fstr->bv_len = atmp.bv_len + STRLENOF( "(=*)" );
693                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
694
695                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
696                         atmp.bv_val );
697                 break;
698
699         case LDAP_FILTER_AND:
700         case LDAP_FILTER_OR:
701         case LDAP_FILTER_NOT:
702                 fstr->bv_len = STRLENOF( "(%)" );
703                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
704
705                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
706                         f->f_choice == LDAP_FILTER_AND ? '&' :
707                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
708
709                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
710                         int     rc;
711
712                         len = fstr->bv_len;
713
714                         rc = rwm_int_filter_map_rewrite( op, dc, p, &vtmp );
715                         if ( rc != LDAP_SUCCESS ) {
716                                 return rc;
717                         }
718                         
719                         fstr->bv_len += vtmp.bv_len;
720                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
721                                 op->o_tmpmemctx );
722
723                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
724                                 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
725
726                         op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
727                 }
728
729                 break;
730
731         case LDAP_FILTER_EXT: {
732                 if ( f->f_mr_desc ) {
733                         ad = f->f_mr_desc;
734                         if ( map_attr_value( dc, &ad, &atmp,
735                                 &f->f_mr_value, &vtmp, RWM_MAP, op->o_tmpmemctx ) )
736                         {
737                                 goto computed;
738                         }
739
740                 } else {
741                         BER_BVSTR( &atmp, "" );
742                         filter_escape_value_x( &f->f_mr_value, &vtmp, op->o_tmpmemctx );
743                 }
744                         
745
746                 fstr->bv_len = atmp.bv_len +
747                         ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
748                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
749                         vtmp.bv_len + STRLENOF( "(:=)" );
750                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
751
752                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
753                         atmp.bv_val,
754                         f->f_mr_dnattrs ? ":dn" : "",
755                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
756                         !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
757                         vtmp.bv_len ? vtmp.bv_val : "" );
758                 op->o_tmpfree( vtmp.bv_val, op->o_tmpmemctx );
759                 break;
760         }
761
762         case -1:
763 computed:;
764                 filter_free_x( op, f, 0 );
765                 f->f_choice = SLAPD_FILTER_COMPUTED;
766                 f->f_result = SLAPD_COMPARE_UNDEFINED;
767                 /* fallthru */
768
769         case SLAPD_FILTER_COMPUTED:
770                 switch ( f->f_result ) {
771                 case LDAP_COMPARE_FALSE:
772                 /* FIXME: treat UNDEFINED as FALSE */
773                 case SLAPD_COMPARE_UNDEFINED:
774                         if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
775                                 tmp = &ber_bvtf_false;
776                                 break;
777                         }
778                         tmp = &ber_bvfalse;
779                         break;
780
781                 case LDAP_COMPARE_TRUE:
782                         if ( dc->rwmap->rwm_flags & RWM_F_SUPPORT_T_F ) {
783                                 tmp = &ber_bvtf_true;
784                                 break;
785                         }
786                         tmp = &ber_bvtrue;
787                         break;
788                         
789                 default:
790                         tmp = &ber_bverror;
791                         break;
792                 }
793
794                 ber_dupbv_x( fstr, tmp, op->o_tmpmemctx );
795                 break;
796                 
797         default:
798                 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
799                 break;
800         }
801
802         return LDAP_SUCCESS;
803 }
804
805 int
806 rwm_filter_map_rewrite(
807         Operation               *op,
808         dncookie                *dc,
809         Filter                  *f,
810         struct berval           *fstr )
811 {
812         int             rc;
813         dncookie        fdc;
814         struct berval   ftmp;
815
816         rc = rwm_int_filter_map_rewrite( op, dc, f, fstr );
817
818         if ( rc != 0 ) {
819                 return rc;
820         }
821
822         fdc = *dc;
823         ftmp = *fstr;
824
825         fdc.ctx = "searchFilter";
826
827         switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx, 
828                                 ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ), 
829                                 fdc.conn, &fstr->bv_val ) )
830         {
831         case REWRITE_REGEXEC_OK:
832                 if ( !BER_BVISNULL( fstr ) ) {
833                         fstr->bv_len = strlen( fstr->bv_val );
834
835                 } else {
836                         *fstr = ftmp;
837                 }
838
839                 Debug( LDAP_DEBUG_ARGS,
840                         "[rw] %s: \"%s\" -> \"%s\"\n",
841                         fdc.ctx, ftmp.bv_val, fstr->bv_val );           
842                 if ( fstr->bv_val != ftmp.bv_val ) {
843                         ber_bvreplace_x( &ftmp, fstr, op->o_tmpmemctx );
844                         ch_free( fstr->bv_val );
845                         *fstr = ftmp;
846                 }
847                 rc = LDAP_SUCCESS;
848                 break;
849                 
850         case REWRITE_REGEXEC_UNWILLING:
851                 if ( fdc.rs ) {
852                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
853                         fdc.rs->sr_text = "Operation not allowed";
854                 }
855                 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
856                 rc = LDAP_UNWILLING_TO_PERFORM;
857                 break;
858                 
859         case REWRITE_REGEXEC_ERR:
860                 if ( fdc.rs ) {
861                         fdc.rs->sr_err = LDAP_OTHER;
862                         fdc.rs->sr_text = "Rewrite error";
863                 }
864                 op->o_tmpfree( ftmp.bv_val, op->o_tmpmemctx );
865                 rc = LDAP_OTHER;
866                 break;
867         }
868
869         return rc;
870 }
871
872 /*
873  * I don't like this much, but we need two different
874  * functions because different heap managers may be
875  * in use in back-ldap/meta to reduce the amount of
876  * calls to malloc routines, and some of the free()
877  * routines may be macros with args
878  */
879 int
880 rwm_referral_rewrite(
881         Operation               *op,
882         SlapReply               *rs,
883         void                    *cookie,
884         BerVarray               a_vals,
885         BerVarray               *pa_nvals )
886 {
887         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
888         struct ldaprwmap        *rwmap = 
889                         (struct ldaprwmap *)on->on_bi.bi_private;
890
891         int                     i, last;
892
893         dncookie                dc;
894         struct berval           dn = BER_BVNULL,
895                                 ndn = BER_BVNULL;
896
897         assert( a_vals != NULL );
898
899         /*
900          * Rewrite the dn if needed
901          */
902         dc.rwmap = rwmap;
903         dc.conn = op->o_conn;
904         dc.rs = rs;
905         dc.ctx = (char *)cookie;
906
907         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
908                 ;
909         last--;
910         
911         if ( pa_nvals != NULL ) {
912                 if ( *pa_nvals == NULL ) {
913                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
914                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
915                 }
916         }
917
918         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
919                 struct berval   olddn = BER_BVNULL,
920                                 oldval;
921                 int             rc;
922                 LDAPURLDesc     *ludp;
923
924                 oldval = a_vals[i];
925                 rc = ldap_url_parse( oldval.bv_val, &ludp );
926                 if ( rc != LDAP_URL_SUCCESS ) {
927                         /* leave attr untouched if massage failed */
928                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
929                                 ber_dupbv( &(*pa_nvals)[i], &oldval );
930                         }
931                         continue;
932                 }
933
934                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
935                  * thru ldap_url_parse() and ldap_url_desc2str() 
936                  * get rewritten as "ldap:///dc=suffix??base";
937                  * we don't want this to occur... */
938                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
939                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
940                 }
941
942                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
943
944                 dn = olddn;
945                 if ( pa_nvals ) {
946                         ndn = olddn;
947                         rc = rwm_dn_massage_pretty_normalize( &dc, &olddn,
948                                         &dn, &ndn );
949                 } else {
950                         rc = rwm_dn_massage_pretty( &dc, &olddn, &dn );
951                 }
952
953                 switch ( rc ) {
954                 case LDAP_UNWILLING_TO_PERFORM:
955                         /*
956                          * FIXME: need to check if it may be considered 
957                          * legal to trim values when adding/modifying;
958                          * it should be when searching (e.g. ACLs).
959                          */
960                         ch_free( a_vals[i].bv_val );
961                         if (last > i ) {
962                                 a_vals[i] = a_vals[last];
963                                 if ( pa_nvals ) {
964                                         (*pa_nvals)[i] = (*pa_nvals)[last];
965                                 }
966                         }
967                         BER_BVZERO( &a_vals[last] );
968                         if ( pa_nvals ) {
969                                 BER_BVZERO( &(*pa_nvals)[last] );
970                         }
971                         last--;
972                         break;
973                 
974                 case LDAP_SUCCESS:
975                         if ( !BER_BVISNULL( &dn ) && dn.bv_val != olddn.bv_val ) {
976                                 char    *newurl;
977
978                                 ludp->lud_dn = dn.bv_val;
979                                 newurl = ldap_url_desc2str( ludp );
980                                 ludp->lud_dn = olddn.bv_val;
981                                 ch_free( dn.bv_val );
982                                 if ( newurl == NULL ) {
983                                         /* FIXME: leave attr untouched
984                                          * even if ldap_url_desc2str failed...
985                                          */
986                                         break;
987                                 }
988
989                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
990                                 ber_memfree( newurl );
991
992                                 if ( pa_nvals ) {
993                                         ludp->lud_dn = ndn.bv_val;
994                                         newurl = ldap_url_desc2str( ludp );
995                                         ludp->lud_dn = olddn.bv_val;
996                                         ch_free( ndn.bv_val );
997                                         if ( newurl == NULL ) {
998                                                 /* FIXME: leave attr untouched
999                                                  * even if ldap_url_desc2str failed...
1000                                                  */
1001                                                 ch_free( a_vals[i].bv_val );
1002                                                 a_vals[i] = oldval;
1003                                                 break;
1004                                         }
1005
1006                                         if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1007                                                 ch_free( (*pa_nvals)[i].bv_val );
1008                                         }
1009                                         ber_str2bv( newurl, 0, 1, &(*pa_nvals)[i] );
1010                                         ber_memfree( newurl );
1011                                 }
1012
1013                                 ch_free( oldval.bv_val );
1014                                 ludp->lud_dn = olddn.bv_val;
1015                         }
1016                         break;
1017
1018                 default:
1019                         /* leave attr untouched if massage failed */
1020                         if ( pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1021                                 ber_dupbv( &(*pa_nvals)[i], &a_vals[i] );
1022                         }
1023                         break;
1024                 }
1025                 ldap_free_urldesc( ludp );
1026         }
1027         
1028         return 0;
1029 }
1030
1031 /*
1032  * I don't like this much, but we need two different
1033  * functions because different heap managers may be
1034  * in use in back-ldap/meta to reduce the amount of
1035  * calls to malloc routines, and some of the free()
1036  * routines may be macros with args
1037  */
1038 int
1039 rwm_dnattr_rewrite(
1040         Operation               *op,
1041         SlapReply               *rs,
1042         void                    *cookie,
1043         BerVarray               a_vals,
1044         BerVarray               *pa_nvals )
1045 {
1046         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1047         struct ldaprwmap        *rwmap = 
1048                         (struct ldaprwmap *)on->on_bi.bi_private;
1049
1050         int                     i, last;
1051
1052         dncookie                dc;
1053         struct berval           dn = BER_BVNULL,
1054                                 ndn = BER_BVNULL;
1055         BerVarray               in;
1056
1057         if ( a_vals ) {
1058                 in = a_vals;
1059
1060         } else {
1061                 if ( pa_nvals == NULL || *pa_nvals == NULL ) {
1062                         return LDAP_OTHER;
1063                 }
1064                 in = *pa_nvals;
1065         }
1066
1067         /*
1068          * Rewrite the dn if needed
1069          */
1070         dc.rwmap = rwmap;
1071         dc.conn = op->o_conn;
1072         dc.rs = rs;
1073         dc.ctx = (char *)cookie;
1074
1075         for ( last = 0; !BER_BVISNULL( &in[last] ); last++ );
1076         last--;
1077         if ( pa_nvals != NULL ) {
1078                 if ( *pa_nvals == NULL ) {
1079                         *pa_nvals = ch_malloc( ( last + 2 ) * sizeof(struct berval) );
1080                         memset( *pa_nvals, 0, ( last + 2 ) * sizeof(struct berval) );
1081                 }
1082         }
1083
1084         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1085                 int             rc;
1086
1087                 if ( a_vals ) {
1088                         dn = in[i];
1089                         if ( pa_nvals ) {
1090                                 ndn = (*pa_nvals)[i];
1091                                 rc = rwm_dn_massage_pretty_normalize( &dc, &in[i], &dn, &ndn );
1092                         } else {
1093                                 rc = rwm_dn_massage_pretty( &dc, &in[i], &dn );
1094                         }
1095                 } else {
1096                         ndn = in[i];
1097                         rc = rwm_dn_massage_normalize( &dc, &in[i], &ndn );
1098                 }
1099
1100                 switch ( rc ) {
1101                 case LDAP_UNWILLING_TO_PERFORM:
1102                         /*
1103                          * FIXME: need to check if it may be considered 
1104                          * legal to trim values when adding/modifying;
1105                          * it should be when searching (e.g. ACLs).
1106                          */
1107                         ch_free( in[i].bv_val );
1108                         if (last > i ) {
1109                                 in[i] = in[last];
1110                                 if ( a_vals && pa_nvals ) {
1111                                         (*pa_nvals)[i] = (*pa_nvals)[last];
1112                                 }
1113                         }
1114                         BER_BVZERO( &in[last] );
1115                         if ( a_vals && pa_nvals ) {
1116                                 BER_BVZERO( &(*pa_nvals)[last] );
1117                         }
1118                         last--;
1119                         break;
1120                 
1121                 case LDAP_SUCCESS:
1122                         if ( a_vals ) {
1123                                 if ( !BER_BVISNULL( &dn ) && dn.bv_val != a_vals[i].bv_val ) {
1124                                         ch_free( a_vals[i].bv_val );
1125                                         a_vals[i] = dn;
1126
1127                                         if ( pa_nvals ) {
1128                                                 if ( !BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1129                                                         ch_free( (*pa_nvals)[i].bv_val );
1130                                                 }
1131                                                 (*pa_nvals)[i] = ndn;
1132                                         }
1133                                 }
1134                                 
1135                         } else {
1136                                 if ( !BER_BVISNULL( &ndn ) && ndn.bv_val != (*pa_nvals)[i].bv_val ) {
1137                                         ch_free( (*pa_nvals)[i].bv_val );
1138                                         (*pa_nvals)[i] = ndn;
1139                                 }
1140                         }
1141                         break;
1142
1143                 default:
1144                         /* leave attr untouched if massage failed */
1145                         if ( a_vals && pa_nvals && BER_BVISNULL( &(*pa_nvals)[i] ) ) {
1146                                 dnNormalize( 0, NULL, NULL, &a_vals[i], &(*pa_nvals)[i], NULL );
1147                         }
1148                         break;
1149                 }
1150         }
1151         
1152         return 0;
1153 }
1154
1155 int
1156 rwm_referral_result_rewrite(
1157         dncookie                *dc,
1158         BerVarray               a_vals )
1159 {
1160         int             i, last;
1161
1162         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1163         last--;
1164
1165         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1166                 struct berval   dn,
1167                                 olddn = BER_BVNULL;
1168                 int             rc;
1169                 LDAPURLDesc     *ludp;
1170
1171                 rc = ldap_url_parse( a_vals[i].bv_val, &ludp );
1172                 if ( rc != LDAP_URL_SUCCESS ) {
1173                         /* leave attr untouched if massage failed */
1174                         continue;
1175                 }
1176
1177                 /* FIXME: URLs like "ldap:///dc=suffix" if passed
1178                  * thru ldap_url_parse() and ldap_url_desc2str()
1179                  * get rewritten as "ldap:///dc=suffix??base";
1180                  * we don't want this to occur... */
1181                 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
1182                         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
1183                 }
1184
1185                 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
1186
1187                 dn = olddn;
1188                 rc = rwm_dn_massage_pretty( dc, &olddn, &dn );
1189                 switch ( rc ) {
1190                 case LDAP_UNWILLING_TO_PERFORM:
1191                         /*
1192                          * FIXME: need to check if it may be considered 
1193                          * legal to trim values when adding/modifying;
1194                          * it should be when searching (e.g. ACLs).
1195                          */
1196                         ch_free( a_vals[i].bv_val );
1197                         if ( last > i ) {
1198                                 a_vals[i] = a_vals[last];
1199                         }
1200                         BER_BVZERO( &a_vals[last] );
1201                         last--;
1202                         i--;
1203                         break;
1204
1205                 default:
1206                         /* leave attr untouched if massage failed */
1207                         if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) {
1208                                 char    *newurl;
1209
1210                                 ludp->lud_dn = dn.bv_val;
1211                                 newurl = ldap_url_desc2str( ludp );
1212                                 if ( newurl == NULL ) {
1213                                         /* FIXME: leave attr untouched
1214                                          * even if ldap_url_desc2str failed...
1215                                          */
1216                                         break;
1217                                 }
1218
1219                                 ch_free( a_vals[i].bv_val );
1220                                 ber_str2bv( newurl, 0, 1, &a_vals[i] );
1221                                 ber_memfree( newurl );
1222                                 ludp->lud_dn = olddn.bv_val;
1223                         }
1224                         break;
1225                 }
1226
1227                 ldap_free_urldesc( ludp );
1228         }
1229
1230         return 0;
1231 }
1232
1233 int
1234 rwm_dnattr_result_rewrite(
1235         dncookie                *dc,
1236         BerVarray               a_vals,
1237         BerVarray               a_nvals )
1238 {
1239         int             i, last;
1240
1241         for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ );
1242         last--;
1243
1244         for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
1245                 struct berval   pdn, ndn = BER_BVNULL;
1246                 int             rc;
1247                 
1248                 pdn = a_vals[i];
1249                 rc = rwm_dn_massage_pretty_normalize( dc, &a_vals[i], &pdn, &ndn );
1250                 switch ( rc ) {
1251                 case LDAP_UNWILLING_TO_PERFORM:
1252                         /*
1253                          * FIXME: need to check if it may be considered 
1254                          * legal to trim values when adding/modifying;
1255                          * it should be when searching (e.g. ACLs).
1256                          */
1257                         assert( a_vals[i].bv_val != a_nvals[i].bv_val );
1258                         ch_free( a_vals[i].bv_val );
1259                         ch_free( a_nvals[i].bv_val );
1260                         if ( last > i ) {
1261                                 a_vals[i] = a_vals[last];
1262                                 a_nvals[i] = a_nvals[last];
1263                         }
1264                         BER_BVZERO( &a_vals[last] );
1265                         BER_BVZERO( &a_nvals[last] );
1266                         last--;
1267                         break;
1268
1269                 default:
1270                         /* leave attr untouched if massage failed */
1271                         if ( !BER_BVISNULL( &pdn ) && a_vals[i].bv_val != pdn.bv_val ) {
1272                                 ch_free( a_vals[i].bv_val );
1273                                 a_vals[i] = pdn;
1274                         }
1275                         if ( !BER_BVISNULL( &ndn ) && a_nvals[i].bv_val != ndn.bv_val ) {
1276                                 ch_free( a_nvals[i].bv_val );
1277                                 a_nvals[i] = ndn;
1278                         }
1279                         break;
1280                 }
1281         }
1282
1283         return 0;
1284 }
1285
1286 void
1287 rwm_mapping_dst_free( void *v_mapping )
1288 {
1289         struct ldapmapping *mapping = v_mapping;
1290
1291         if ( BER_BVISEMPTY( &mapping[0].m_dst ) ) {
1292                 rwm_mapping_free( &mapping[ -1 ] );
1293         }
1294 }
1295
1296 void
1297 rwm_mapping_free( void *v_mapping )
1298 {
1299         struct ldapmapping *mapping = v_mapping;
1300
1301         if ( !BER_BVISNULL( &mapping[0].m_src ) ) {
1302                 ch_free( mapping[0].m_src.bv_val );
1303         }
1304
1305         if ( mapping[0].m_flags & RWMMAP_F_FREE_SRC ) {
1306                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1307                         if ( mapping[0].m_src_oc ) {
1308                                 ch_free( mapping[0].m_src_oc );
1309                         }
1310
1311                 } else {
1312                         if ( mapping[0].m_src_ad ) {
1313                                 ch_free( mapping[0].m_src_ad );
1314                         }
1315                 }
1316         }
1317
1318         if ( !BER_BVISNULL( &mapping[0].m_dst ) ) {
1319                 ch_free( mapping[0].m_dst.bv_val );
1320         }
1321
1322         if ( mapping[0].m_flags & RWMMAP_F_FREE_DST ) {
1323                 if ( mapping[0].m_flags & RWMMAP_F_IS_OC ) {
1324                         if ( mapping[0].m_dst_oc ) {
1325                                 ch_free( mapping[0].m_dst_oc );
1326                         }
1327
1328                 } else {
1329                         if ( mapping[0].m_dst_ad ) {
1330                                 ch_free( mapping[0].m_dst_ad );
1331                         }
1332                 }
1333         }
1334
1335         ch_free( mapping );
1336
1337 }
1338
1339 #endif /* SLAPD_OVER_RWM */