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