]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/map.c
ITS#3647 additional hdb dn2idl fixes
[openldap] / servers / slapd / back-ldap / map.c
1 /* map.c - ldap backend mapping routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-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 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 #undef ldap_debug       /* silence a warning in ldap-int.h */
35 #include "../../../libraries/libldap/ldap-int.h"
36
37 int
38 mapping_cmp ( const void *c1, const void *c2 )
39 {
40         struct ldapmapping *map1 = (struct ldapmapping *)c1;
41         struct ldapmapping *map2 = (struct ldapmapping *)c2;
42         int rc = map1->src.bv_len - map2->src.bv_len;
43         if (rc) return rc;
44         return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
45 }
46
47 int
48 mapping_dup ( void *c1, void *c2 )
49 {
50         struct ldapmapping *map1 = (struct ldapmapping *)c1;
51         struct ldapmapping *map2 = (struct ldapmapping *)c2;
52
53         return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
54 }
55
56 void
57 ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
58 {
59         struct ldapmapping *mapping;
60
61         assert( m );
62
63         *m = NULL;
64         
65         mapping = (struct ldapmapping *)ch_calloc( 2, 
66                         sizeof( struct ldapmapping ) );
67         if ( mapping == NULL ) {
68                 return;
69         }
70
71         ber_str2bv( "objectclass", sizeof("objectclass")-1, 1, &mapping->src);
72         ber_dupbv( &mapping->dst, &mapping->src );
73         mapping[1].src = mapping->src;
74         mapping[1].dst = mapping->dst;
75
76         avl_insert( &lm->map, (caddr_t)mapping, 
77                         mapping_cmp, mapping_dup );
78         avl_insert( &lm->remap, (caddr_t)&mapping[1], 
79                         mapping_cmp, mapping_dup );
80         *m = mapping;
81 }
82
83 int
84 ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,
85         int remap )
86 {
87         Avlnode *tree;
88         struct ldapmapping fmapping;
89
90         assert( m );
91
92         if ( remap == BACKLDAP_REMAP ) {
93                 tree = map->remap;
94         } else {
95                 tree = map->map;
96         }
97
98         fmapping.src = *s;
99         *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
100         if ( *m == NULL ) {
101                 return map->drop_missing;
102         }
103
104         return 0;
105 }
106
107 void
108 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
109         int remap )
110 {
111         struct ldapmapping *mapping;
112
113         BER_BVZERO( bv );
114         ( void )ldap_back_mapping( map, s, &mapping, remap );
115         if ( mapping != NULL ) {
116                 if ( !BER_BVISNULL( &mapping->dst ) ) {
117                         *bv = mapping->dst;
118                 }
119                 return;
120         }
121
122         if ( !map->drop_missing ) {
123                 *bv = *s;
124         }
125 }
126
127 int
128 ldap_back_map_attrs(
129                 struct ldapmap *at_map,
130                 AttributeName *an,
131                 int remap,
132                 char ***mapped_attrs
133 )
134 {
135         int i, j;
136         char **na;
137         struct berval mapped;
138
139         if (an == NULL) {
140                 *mapped_attrs = NULL;
141                 return LDAP_SUCCESS;
142         }
143
144         for (i = 0; an[i].an_name.bv_val; i++) {
145                 /*  */
146         }
147
148         na = (char **)ch_calloc( i + 1, sizeof(char *) );
149         if (na == NULL) {
150                 *mapped_attrs = NULL;
151                 return LDAP_NO_MEMORY;
152         }
153
154         for (i = j = 0; an[i].an_name.bv_val; i++) {
155                 ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
156                 if (mapped.bv_val != NULL && mapped.bv_val != '\0')
157                         na[j++] = mapped.bv_val;
158         }
159         if (j == 0 && i != 0)
160                 na[j++] = LDAP_NO_ATTRS;
161         na[j] = NULL;
162
163         *mapped_attrs = na;
164         return LDAP_SUCCESS;
165 }
166
167 int
168 map_attr_value(
169                 dncookie                *dc,
170                 AttributeDescription    *ad,
171                 struct berval           *mapped_attr,
172                 struct berval           *value,
173                 struct berval           *mapped_value,
174                 int                     remap )
175 {
176         struct berval           vtmp;
177         int                     freeval = 0;
178
179         ldap_back_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
180         if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0') {
181                 /*
182                  * FIXME: are we sure we need to search oc_map if at_map fails?
183                  */
184                 ldap_back_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
185                 if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0' ) {
186                         *mapped_attr = ad->ad_cname;
187                 }
188         }
189
190         if ( value == NULL ) {
191                 return LDAP_SUCCESS;
192         }
193
194         if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
195         {
196                 dncookie fdc = *dc;
197
198 #ifdef ENABLE_REWRITE
199                 fdc.ctx = "searchFilterAttrDN";
200 #endif
201
202                 switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
203                 case LDAP_SUCCESS:
204                         if ( vtmp.bv_val != value->bv_val ) {
205                                 freeval = 1;
206                         }
207                         break;
208                 
209                 case LDAP_UNWILLING_TO_PERFORM:
210                 case LDAP_OTHER:
211                         return LDAP_OTHER;
212                 }
213
214         } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
215                 ldap_back_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
216                 if ( vtmp.bv_val == NULL || vtmp.bv_val[0] == '\0' ) {
217                         vtmp = *value;
218                 }
219                 
220         } else {
221                 vtmp = *value;
222         }
223
224         filter_escape_value( &vtmp, mapped_value );
225
226         if ( freeval ) {
227                 ber_memfree( vtmp.bv_val );
228         }
229         
230         return LDAP_SUCCESS;
231 }
232
233 static int
234 ldap_int_back_filter_map_rewrite(
235                 dncookie                *dc,
236                 Filter                  *f,
237                 struct berval           *fstr,
238                 int                     remap )
239 {
240         int             i, rc;
241         Filter          *p;
242         struct berval   atmp;
243         struct berval   vtmp;
244         ber_len_t       len;
245
246         if ( f == NULL ) {
247                 ber_str2bv( "(?=error)", sizeof("(?=error)")-1, 1, fstr );
248                 return LDAP_OTHER;
249         }
250
251         switch ( f->f_choice ) {
252         case LDAP_FILTER_EQUALITY:
253                 rc = map_attr_value( dc, f->f_av_desc, &atmp,
254                                         &f->f_av_value, &vtmp, remap );
255                 if ( rc ) {
256                         return rc;
257                 }
258
259                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
260                         + ( sizeof("(=)") - 1 );
261                 fstr->bv_val = malloc( fstr->bv_len + 1 );
262
263                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
264                         atmp.bv_val, vtmp.bv_val );
265
266                 ber_memfree( vtmp.bv_val );
267                 break;
268
269         case LDAP_FILTER_GE:
270                 rc = map_attr_value( dc, f->f_av_desc, &atmp,
271                                         &f->f_av_value, &vtmp, remap );
272                 if ( rc ) {
273                         return rc;
274                 }
275
276                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
277                         + ( sizeof("(>=)") - 1 );
278                 fstr->bv_val = malloc( fstr->bv_len + 1 );
279
280                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
281                         atmp.bv_val, vtmp.bv_val );
282
283                 ber_memfree( vtmp.bv_val );
284                 break;
285
286         case LDAP_FILTER_LE:
287                 rc = map_attr_value( dc, f->f_av_desc, &atmp,
288                                         &f->f_av_value, &vtmp, remap );
289                 if ( rc ) {
290                         return rc;
291                 }
292
293                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
294                         + ( sizeof("(<=)") - 1 );
295                 fstr->bv_val = malloc( fstr->bv_len + 1 );
296
297                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
298                         atmp.bv_val, vtmp.bv_val );
299
300                 ber_memfree( vtmp.bv_val );
301                 break;
302
303         case LDAP_FILTER_APPROX:
304                 rc = map_attr_value( dc, f->f_av_desc, &atmp,
305                                         &f->f_av_value, &vtmp, remap );
306                 if ( rc ) {
307                         return rc;
308                 }
309
310                 fstr->bv_len = atmp.bv_len + vtmp.bv_len
311                         + ( sizeof("(~=)") - 1 );
312                 fstr->bv_val = malloc( fstr->bv_len + 1 );
313
314                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
315                         atmp.bv_val, vtmp.bv_val );
316
317                 ber_memfree( vtmp.bv_val );
318                 break;
319
320         case LDAP_FILTER_SUBSTRINGS:
321                 rc = map_attr_value( dc, f->f_sub_desc, &atmp,
322                                         NULL, NULL, remap );
323                 if ( rc ) {
324                         return rc;
325                 }
326
327                 /* cannot be a DN ... */
328
329                 fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
330                 fstr->bv_val = malloc( fstr->bv_len + 128 );
331
332                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
333                         atmp.bv_val );
334
335                 if ( f->f_sub_initial.bv_val != NULL ) {
336                         len = fstr->bv_len;
337
338                         filter_escape_value( &f->f_sub_initial, &vtmp );
339
340                         fstr->bv_len += vtmp.bv_len;
341                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
342
343                         snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
344                                 /* "(attr=" */ "%s*)",
345                                 vtmp.bv_val );
346
347                         ber_memfree( vtmp.bv_val );
348                 }
349
350                 if ( f->f_sub_any != NULL ) {
351                         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
352                                 len = fstr->bv_len;
353                                 filter_escape_value( &f->f_sub_any[i], &vtmp );
354
355                                 fstr->bv_len += vtmp.bv_len + 1;
356                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
357
358                                 snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
359                                         /* "(attr=[init]*[any*]" */ "%s*)",
360                                         vtmp.bv_val );
361                                 ber_memfree( vtmp.bv_val );
362                         }
363                 }
364
365                 if ( f->f_sub_final.bv_val != NULL ) {
366                         len = fstr->bv_len;
367
368                         filter_escape_value( &f->f_sub_final, &vtmp );
369
370                         fstr->bv_len += vtmp.bv_len;
371                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
372
373                         snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
374                                 /* "(attr=[init*][any*]" */ "%s)",
375                                 vtmp.bv_val );
376
377                         ber_memfree( vtmp.bv_val );
378                 }
379
380                 break;
381
382         case LDAP_FILTER_PRESENT:
383                 rc = map_attr_value( dc, f->f_desc, &atmp,
384                                         NULL, NULL, remap );
385                 if ( rc ) {
386                         return rc;
387                 }
388
389                 fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
390                 fstr->bv_val = malloc( fstr->bv_len + 1 );
391
392                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
393                         atmp.bv_val );
394                 break;
395
396         case LDAP_FILTER_AND:
397         case LDAP_FILTER_OR:
398         case LDAP_FILTER_NOT:
399                 fstr->bv_len = sizeof("(%)") - 1;
400                 fstr->bv_val = malloc( fstr->bv_len + 128 );
401
402                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
403                         f->f_choice == LDAP_FILTER_AND ? '&' :
404                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
405
406                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
407                         len = fstr->bv_len;
408
409                         rc = ldap_int_back_filter_map_rewrite( dc, p, &vtmp, remap );
410                         if ( rc != LDAP_SUCCESS ) {
411                                 return rc;
412                         }
413                         
414                         fstr->bv_len += vtmp.bv_len;
415                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
416
417                         snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
418                                 /*"("*/ "%s)", vtmp.bv_val );
419
420                         ch_free( vtmp.bv_val );
421                 }
422
423                 break;
424
425         case LDAP_FILTER_EXT: {
426                 if ( f->f_mr_desc ) {
427                         rc = map_attr_value( dc, f->f_mr_desc, &atmp,
428                                                 &f->f_mr_value, &vtmp, remap );
429                         if ( rc ) {
430                                 return rc;
431                         }
432
433                 } else {
434                         atmp.bv_len = 0;
435                         atmp.bv_val = "";
436                         
437                         filter_escape_value( &f->f_mr_value, &vtmp );
438                 }
439                         
440
441                 fstr->bv_len = atmp.bv_len +
442                         ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
443                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
444                         vtmp.bv_len + ( sizeof("(:=)") - 1 );
445                 fstr->bv_val = malloc( fstr->bv_len + 1 );
446
447                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
448                         atmp.bv_val,
449                         f->f_mr_dnattrs ? ":dn" : "",
450                         f->f_mr_rule_text.bv_len ? ":" : "",
451                         f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
452                         vtmp.bv_val );
453                 ber_memfree( vtmp.bv_val );
454                 } break;
455
456         case SLAPD_FILTER_COMPUTED:
457                 ber_str2bv(
458                         f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
459                         f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
460                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
461                         "(?=error)",
462                         f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
463                         f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
464                         f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
465                         sizeof("(?=error)")-1,
466                         1, fstr );
467                 return LDAP_COMPARE_FALSE;
468
469         default:
470                 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
471                 return LDAP_COMPARE_FALSE;
472         }
473
474         return LDAP_SUCCESS;
475 }
476
477 int
478 ldap_back_filter_map_rewrite(
479                 dncookie                *dc,
480                 Filter                  *f,
481                 struct berval           *fstr,
482                 int                     remap )
483 {
484         int             rc;
485         dncookie        fdc;
486         struct berval   ftmp;
487
488         rc = ldap_int_back_filter_map_rewrite( dc, f, fstr, remap );
489
490 #ifdef ENABLE_REWRITE
491         if ( rc != LDAP_SUCCESS ) {
492                 return rc;
493         }
494
495         fdc = *dc;
496         ftmp = *fstr;
497
498         fdc.ctx = "searchFilter";
499
500         switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx, 
501                                 ( ftmp.bv_len ? ftmp.bv_val : "" ), 
502                                 fdc.conn, &fstr->bv_val )) {
503         case REWRITE_REGEXEC_OK:
504                 if ( fstr->bv_val != NULL ) {
505                         fstr->bv_len = strlen( fstr->bv_val );
506                         free( ftmp.bv_val );
507                 } else {
508                         *fstr = ftmp;
509                 }
510
511 #ifdef NEW_LOGGING
512                 LDAP_LOG( BACK_LDAP, DETAIL1, 
513                         "[rw] %s: \"%s\" -> \"%s\"\n",
514                         dc->ctx, ftmp.bv_val, fstr->bv_val );           
515 #else /* !NEW_LOGGING */
516                 Debug( LDAP_DEBUG_ARGS,
517                         "[rw] %s: \"%s\" -> \"%s\"\n",
518                         dc->ctx, ftmp.bv_val, fstr->bv_val );           
519 #endif /* !NEW_LOGGING */
520                 rc = LDAP_SUCCESS;
521                 break;
522                 
523         case REWRITE_REGEXEC_UNWILLING:
524                 if ( fdc.rs ) {
525                         fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
526                         fdc.rs->sr_text = "Operation not allowed";
527                 }
528                 rc = LDAP_UNWILLING_TO_PERFORM;
529                 break;
530                 
531         case REWRITE_REGEXEC_ERR:
532                 if ( fdc.rs ) {
533                         fdc.rs->sr_err = LDAP_OTHER;
534                         fdc.rs->sr_text = "Rewrite error";
535                 }
536                 rc = LDAP_OTHER;
537                 break;
538         }
539
540 #endif /* ENABLE_REWRITE */
541         return rc;
542 }
543
544 /*
545  * I don't like this much, but we need two different
546  * functions because different heap managers may be
547  * in use in back-ldap/meta to reduce the amount of
548  * calls to malloc routines, and some of the free()
549  * routines may be macros with args
550  */
551 int
552 ldap_dnattr_rewrite(
553         dncookie                *dc,
554         BerVarray               a_vals
555 )
556 {
557         struct berval   bv;
558         int             i, last;
559
560         assert( a_vals != NULL );
561
562         for ( last = 0; a_vals[last].bv_val != NULL; last++ );
563         last--;
564
565         for ( i = 0; a_vals[i].bv_val != NULL; i++ ) {
566                 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
567                 case LDAP_UNWILLING_TO_PERFORM:
568                         /*
569                          * FIXME: need to check if it may be considered 
570                          * legal to trim values when adding/modifying;
571                          * it should be when searching (e.g. ACLs).
572                          */
573                         ch_free( a_vals[i].bv_val );
574                         if (last > i ) {
575                                 a_vals[i] = a_vals[last];
576                         }
577                         a_vals[last].bv_len = 0;
578                         a_vals[last].bv_val = NULL;
579                         last--;
580                         break;
581
582                 default:
583                         /* leave attr untouched if massage failed */
584                         if ( bv.bv_val && bv.bv_val != a_vals[i].bv_val ) {
585                                 ch_free( a_vals[i].bv_val );
586                                 a_vals[i] = bv;
587                         }
588                         break;
589                 }
590         }
591         
592         return 0;
593 }
594
595 int
596 ldap_dnattr_result_rewrite(
597         dncookie                *dc,
598         BerVarray               a_vals
599 )
600 {
601         struct berval   bv;
602         int             i, last;
603
604         assert( a_vals != NULL );
605
606         for ( last = 0; a_vals[last].bv_val; last++ );
607         last--;
608
609         for ( i = 0; a_vals[i].bv_val; i++ ) {
610                 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
611                 case LDAP_UNWILLING_TO_PERFORM:
612                         /*
613                          * FIXME: need to check if it may be considered 
614                          * legal to trim values when adding/modifying;
615                          * it should be when searching (e.g. ACLs).
616                          */
617                         LBER_FREE( a_vals[i].bv_val );
618                         if ( last > i ) {
619                                 a_vals[i] = a_vals[last];
620                         }
621                         a_vals[last].bv_val = NULL;
622                         a_vals[last].bv_len = 0;
623                         last--;
624                         break;
625
626                 default:
627                         /* leave attr untouched if massage failed */
628                         if ( bv.bv_val && a_vals[i].bv_val != bv.bv_val ) {
629                                 LBER_FREE( a_vals[i].bv_val );
630                                 a_vals[i] = bv;
631                         }
632                         break;
633                 }
634         }
635
636         return 0;
637 }
638