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