]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
Misc. DN fixes and cleanups, namely don't muck with AVA_BINARY values
[openldap] / servers / slapd / dn.c
1 /* dn.c - routines for dealing with distinguished names */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20
21 const struct berval slap_empty_bv = { 0, "" };
22
23 #define SLAP_LDAPDN_PRETTY 0x1
24
25 /*
26  * The DN syntax-related functions take advantage of the dn representation
27  * handling functions ldap_str2dn/ldap_dn2str.  The latter are not schema-
28  * aware, so the attributes and their values need be validated (and possibly
29  * normalized).  In the current implementation the required validation/nor-
30  * malization/"pretty"ing are done on newly created DN structural represen-
31  * tations; however the idea is to move towards DN handling in structural
32  * representation instead of the current string representation.  To this
33  * purpose, we need to do only the required operations and keep track of
34  * what has been done to minimize their impact on performances.
35  *
36  * Developers are strongly encouraged to use this feature, to speed-up
37  * its stabilization.
38  */
39
40 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
41
42 /*
43  * In-place, schema-aware validation of the
44  * structural representation of a distinguished name.
45  */
46 static int
47 LDAPDN_validate( LDAPDN *dn )
48 {
49         int             iRDN;
50         int             rc;
51
52         assert( dn );
53
54         for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
55                 LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
56                 int             iAVA;
57
58                 assert( rdn );
59
60                 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
61                         LDAPAVA                 *ava = rdn[ 0 ][ iAVA ];
62                         AttributeDescription    *ad;
63                         slap_syntax_validate_func *validate = NULL;
64
65                         assert( ava );
66                         
67                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
68                                 const char      *text = NULL;
69
70                                 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
71                                 if ( rc != LDAP_SUCCESS ) {
72                                         return LDAP_INVALID_SYNTAX;
73                                 }
74
75                                 ava->la_private = ( void * )ad;
76                         }
77
78                         /* 
79                          * Replace attr oid/name with the canonical name
80                          */
81                         ava->la_attr = ad->ad_cname;
82
83                         validate = ad->ad_type->sat_syntax->ssyn_validate;
84
85                         if ( validate ) {
86                                 /*
87                                  * validate value by validate function
88                                  */
89                                 rc = ( *validate )( ad->ad_type->sat_syntax,
90                                         &ava->la_value );
91                         
92                                 if ( rc != LDAP_SUCCESS ) {
93                                         return LDAP_INVALID_SYNTAX;
94                                 }
95                         }
96                 }
97         }
98
99         return LDAP_SUCCESS;
100 }
101
102 /*
103  * dn validate routine
104  */
105 int
106 dnValidate(
107         Syntax *syntax,
108         struct berval *in )
109 {
110         int             rc;
111         LDAPDN          *dn = NULL;
112
113         assert( in );
114
115         if ( in->bv_len == 0 ) {
116                 return( LDAP_SUCCESS );
117         }
118
119         rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
120
121         /*
122          * Schema-aware validate
123          */
124         if ( rc == LDAP_SUCCESS ) {
125                 rc = LDAPDN_validate( dn );
126                 ldap_dnfree( dn );
127         }
128         
129         if ( rc != LDAP_SUCCESS ) {
130                 return( LDAP_INVALID_SYNTAX );
131         }
132
133         return( LDAP_SUCCESS );
134 }
135
136 /*
137  * AVA sorting inside a RDN
138  *
139  * rule: sort attributeTypes in alphabetical order; in case of multiple
140  * occurrences of the same attributeType, sort values in byte order
141  * (use memcmp, which implies alphabetical order in case of IA5 value;
142  * this should guarantee the repeatability of the operation).
143  *
144  * Note: the sorting can be slightly improved by sorting first
145  * by attribute type length, then by alphabetical order.
146  *
147  * uses a linear search; should be fine since the number of AVAs in
148  * a RDN should be limited.
149  */
150 static void
151 AVA_Sort( LDAPRDN *rdn, int iAVA )
152 {
153         int             i;
154         LDAPAVA         *ava_in = rdn[ 0 ][ iAVA ];
155
156         assert( rdn );
157         assert( ava_in );
158         
159         for ( i = 0; i < iAVA; i++ ) {
160                 LDAPAVA         *ava = rdn[ 0 ][ i ];
161                 int             a, j;
162
163                 assert( ava );
164
165                 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
166
167                 if ( a > 0 ) {
168                         break;
169                 }
170
171                 while ( a == 0 ) {
172                         int             v, d;
173
174                         d = ava_in->la_value.bv_len - ava->la_value.bv_len;
175
176                         v = memcmp( ava_in->la_value.bv_val, 
177                                         ava->la_value.bv_val,
178                                         d <= 0 ? ava_in->la_value.bv_len 
179                                                 : ava->la_value.bv_len );
180
181                         if ( v == 0 && d != 0 ) {
182                                 v = d;
183                         }
184
185                         if ( v <= 0 ) {
186                                 /* 
187                                  * got it!
188                                  */
189                                 break;
190                         }
191
192                         if ( ++i == iAVA ) {
193                                 /*
194                                  * already sorted
195                                  */
196                                 return;
197                         }
198
199                         ava = rdn[ 0 ][ i ];
200                         a = strcmp( ava_in->la_attr.bv_val, 
201                                         ava->la_attr.bv_val );
202                 }
203
204                 /*
205                  * move ahead
206                  */
207                 for ( j = iAVA; j > i; j-- ) {
208                         rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
209                 }
210                 rdn[ 0 ][ i ] = ava_in;
211
212                 return;
213         }
214 }
215
216 /*
217  * In-place, schema-aware normalization / "pretty"ing of the
218  * structural representation of a distinguished name.
219  */
220 static int
221 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
222 {
223         int             iRDN;
224         int             rc;
225
226         assert( dn );
227
228         for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
229                 LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
230                 int             iAVA;
231
232                 assert( rdn );
233
234                 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
235                         LDAPAVA                 *ava = rdn[ 0 ][ iAVA ];
236                         AttributeDescription    *ad;
237                         slap_syntax_transform_func *transf = NULL;
238                         MatchingRule *mr;
239                         struct berval           bv = { 0, NULL };
240                         int                     do_sort = 0;
241
242                         assert( ava );
243
244                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
245                                 const char      *text = NULL;
246
247                                 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
248                                 if ( rc != LDAP_SUCCESS ) {
249                                         return LDAP_INVALID_SYNTAX;
250                                 }
251                                 
252                                 ava->la_private = ( void * )ad;
253                                 do_sort = 1;
254                         }
255
256                         /* 
257                          * Replace attr oid/name with the canonical name
258                          */
259                         ava->la_attr = ad->ad_cname;
260
261                         if( ava->la_flags & LDAP_AVA_BINARY ) {
262                                 /* AVA is binary encoded, don't muck with it */
263                                 transf = NULL;
264                                 mr = NULL;
265
266                         } else if( flags & SLAP_LDAPDN_PRETTY ) {
267                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
268                                 mr = NULL;
269                         } else {
270                                 transf = ad->ad_type->sat_syntax->ssyn_normalize;
271                                 mr = ad->ad_type->sat_equality;
272                         }
273
274                         if ( transf ) {
275                                 /*
276                                  * transform value by normalize/pretty function
277                                  *      if value is empty, use empty_bv
278                                  */
279                                 rc = ( *transf )( ad->ad_type->sat_syntax,
280                                         ava->la_value.bv_len
281                                                 ? &ava->la_value
282                                                 : (struct berval *) &slap_empty_bv,
283                                         &bv );
284                         
285                                 if ( rc != LDAP_SUCCESS ) {
286                                         return LDAP_INVALID_SYNTAX;
287                                 }
288                         }
289
290                         if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
291                                 char *s = bv.bv_val;
292
293                                 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
294                                         : &ava->la_value, LDAP_UTF8_CASEFOLD ),
295                                         0, 0, &bv );
296                                 free( s );
297                         }
298
299                         if( bv.bv_val ) {
300                                 free( ava->la_value.bv_val );
301                                 ava->la_value = bv;
302                         }
303
304                         if( do_sort ) AVA_Sort( rdn, iAVA );
305                 }
306         }
307
308         return LDAP_SUCCESS;
309 }
310
311 /*
312  * dn normalize routine
313  */
314 int
315 dnNormalize(
316         Syntax *syntax,
317         struct berval *val,
318         struct berval **normalized )
319 {
320         struct berval *out;
321         int rc;
322
323         assert( normalized && *normalized == NULL );
324
325         out = ch_malloc( sizeof( struct berval ) );
326         rc = dnNormalize2( syntax, val, out );
327         if ( rc != LDAP_SUCCESS )
328                 free( out );
329         else
330                 *normalized = out;
331         return rc;
332 }
333
334 int
335 dnNormalize2(
336         Syntax *syntax,
337         struct berval *val,
338         struct berval *out )
339 {
340         assert( val );
341         assert( out );
342
343         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
344
345         if ( val->bv_len != 0 ) {
346                 LDAPDN          *dn = NULL;
347                 int             rc;
348
349                 /*
350                  * Go to structural representation
351                  */
352                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
353                 if ( rc != LDAP_SUCCESS ) {
354                         return LDAP_INVALID_SYNTAX;
355                 }
356
357                 /*
358                  * Schema-aware rewrite
359                  */
360                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
361                         ldap_dnfree( dn );
362                         return LDAP_INVALID_SYNTAX;
363                 }
364
365                 /*
366                  * Back to string representation
367                  */
368                 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
369
370                 ldap_dnfree( dn );
371
372                 if ( rc != LDAP_SUCCESS ) {
373                         return LDAP_INVALID_SYNTAX;
374                 }
375         } else {
376                 ber_dupbv( out, val );
377         }
378
379         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
380
381         return LDAP_SUCCESS;
382 }
383
384 /*
385  * dn "pretty"ing routine
386  */
387 int
388 dnPretty(
389         Syntax *syntax,
390         struct berval *val,
391         struct berval **pretty)
392 {
393         struct berval *out;
394         int rc;
395
396         assert( pretty && *pretty == NULL );
397
398         out = ch_malloc( sizeof( struct berval ) );
399         rc = dnPretty2( syntax, val, out );
400         if ( rc != LDAP_SUCCESS )
401                 free( out );
402         else
403                 *pretty = out;
404         return rc;
405 }
406
407 int
408 dnPretty2(
409         Syntax *syntax,
410         struct berval *val,
411         struct berval *out)
412 {
413         assert( val );
414         assert( out );
415
416         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
417
418         if ( val->bv_len != 0 ) {
419                 LDAPDN          *dn = NULL;
420                 int             rc;
421
422                 /* FIXME: should be liberal in what we accept */
423                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
424                 if ( rc != LDAP_SUCCESS ) {
425                         return LDAP_INVALID_SYNTAX;
426                 }
427
428                 /*
429                  * Schema-aware rewrite
430                  */
431                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
432                         ldap_dnfree( dn );
433                         return LDAP_INVALID_SYNTAX;
434                 }
435
436                 /* FIXME: not sure why the default isn't pretty */
437                 /* RE: the default is the form that is used as
438                  * an internal representation; the pretty form
439                  * is a variant */
440                 rc = ldap_dn2bv( dn, out,
441                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
442
443                 ldap_dnfree( dn );
444
445                 if ( rc != LDAP_SUCCESS ) {
446                         return LDAP_INVALID_SYNTAX;
447                 }
448         } else {
449                 ber_dupbv( out, val );
450         }
451
452         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
453
454         return LDAP_SUCCESS;
455 }
456
457 /*
458  * Combination of both dnPretty and dnNormalize
459  */
460 int
461 dnPrettyNormal(
462         Syntax *syntax,
463         struct berval *val,
464         struct berval *pretty,
465         struct berval *normal)
466 {
467         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
468
469         assert( val );
470         assert( pretty );
471         assert( normal );
472
473         if ( val->bv_len != 0 ) {
474                 LDAPDN          *dn = NULL;
475                 int             rc;
476
477                 pretty->bv_val = NULL;
478                 normal->bv_val = NULL;
479                 pretty->bv_len = 0;
480                 normal->bv_len = 0;
481
482                 /* FIXME: str2dn should take a bv and handle this */
483                 if( strlen( val->bv_val ) != val->bv_len ) {
484                         return LDAP_INVALID_SYNTAX;
485                 }
486
487                 /* FIXME: should be liberal in what we accept */
488                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
489                 if ( rc != LDAP_SUCCESS ) {
490                         return LDAP_INVALID_SYNTAX;
491                 }
492
493                 /*
494                  * Schema-aware rewrite
495                  */
496                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
497                         ldap_dnfree( dn );
498                         return LDAP_INVALID_SYNTAX;
499                 }
500
501                 rc = ldap_dn2bv( dn, pretty,
502                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
503
504                 if ( rc != LDAP_SUCCESS ) {
505                         ldap_dnfree( dn );
506                         return LDAP_INVALID_SYNTAX;
507                 }
508
509                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
510                         ldap_dnfree( dn );
511                         free( pretty->bv_val );
512                         pretty->bv_val = NULL;
513                         pretty->bv_len = 0;
514                         return LDAP_INVALID_SYNTAX;
515                 }
516
517                 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
518
519                 ldap_dnfree( dn );
520                 if ( rc != LDAP_SUCCESS ) {
521                         free( pretty->bv_val );
522                         pretty->bv_val = NULL;
523                         pretty->bv_len = 0;
524                         return LDAP_INVALID_SYNTAX;
525                 }
526         } else {
527                 ber_dupbv( pretty, val );
528                 ber_dupbv( normal, val );
529         }
530
531         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
532                 pretty->bv_val, normal->bv_val, 0 );
533
534         return LDAP_SUCCESS;
535 }
536
537 /*
538  * dnMatch routine
539  */
540 int
541 dnMatch(
542         int *matchp,
543         slap_mask_t flags,
544         Syntax *syntax,
545         MatchingRule *mr,
546         struct berval *value,
547         void *assertedValue )
548 {
549         int match;
550         struct berval *asserted = (struct berval *) assertedValue;
551
552         assert( matchp );
553         assert( value );
554         assert( assertedValue );
555         
556         match = value->bv_len - asserted->bv_len;
557
558         if ( match == 0 ) {
559                 match = strcmp( value->bv_val, asserted->bv_val );
560         }
561
562 #ifdef NEW_LOGGING
563         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
564                 "dnMatch: %d\n    %s\n    %s\n", match,
565                 value->bv_val, asserted->bv_val ));
566 #else
567         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
568                 match, value->bv_val, asserted->bv_val );
569 #endif
570
571         *matchp = match;
572         return( LDAP_SUCCESS );
573 }
574
575 /*
576  * dnParent - dn's parent, in-place
577  *
578  * note: the incoming dn is assumed to be normalized/prettyfied,
579  * so that escaped rdn/ava separators are in '\'+hexpair form
580  */
581 void
582 dnParent( 
583         struct berval   *dn, 
584         struct berval   *pdn )
585 {
586         char    *p;
587
588         p = strchr( dn->bv_val, ',' );
589
590         /* one-level dn */
591         if ( p == NULL ) {
592                 *pdn = slap_empty_bv;
593                 return;
594         }
595
596         assert( DN_SEPARATOR( p[ 0 ] ) );
597         p++;
598
599         assert( ATTR_LEADCHAR( p[ 0 ] ) );
600         pdn->bv_val = p;
601         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
602
603         return;
604 }
605
606 int
607 dnExtractRdn( 
608         struct berval   *dn, 
609         struct berval   *rdn )
610 {
611         LDAPRDN         *tmpRDN;
612         const char      *p;
613         int             rc;
614
615         assert( dn );
616         assert( rdn );
617
618         if( dn->bv_len == 0 ) {
619                 return LDAP_OTHER;
620         }
621
622         rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
623         if ( rc != LDAP_SUCCESS ) {
624                 return rc;
625         }
626
627         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
628         ldap_rdnfree( tmpRDN );
629         if ( rc != LDAP_SUCCESS ) {
630                 return rc;
631         }
632
633         return LDAP_SUCCESS;
634 }
635
636 /*
637  * We can assume the input is a prettied or normalized DN
638  */
639 int 
640 dn_rdnlen(
641         Backend         *be,
642         struct berval   *dn_in )
643 {
644         const char      *p;
645
646         assert( dn_in );
647
648         if ( dn_in == NULL ) {
649                 return 0;
650         }
651
652         if ( !dn_in->bv_len ) {
653                 return 0;
654         }
655
656         if ( be != NULL && be_issuffix( be, dn_in ) ) {
657                 return 0;
658         }
659
660         p = strchr( dn_in->bv_val, ',' );
661
662         return p ? p - dn_in->bv_val : dn_in->bv_len;
663 }
664
665
666 /* rdnValidate:
667  *
668  * LDAP_SUCCESS if rdn is a legal rdn;
669  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
670  */
671 int
672 rdnValidate( struct berval *rdn )
673 {
674 #if 1
675         /* Major cheat!
676          * input is a pretty or normalized DN
677          * hence, we can just search for ','
678          */
679         if( rdn == NULL || rdn->bv_len == 0 ) {
680                 return LDAP_INVALID_SYNTAX;
681         }
682
683         return strchr( rdn->bv_val, ',' ) == NULL
684                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
685
686 #else
687         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
688         const char      *p;
689         int             rc;
690
691         /*
692          * must be non-empty
693          */
694         if ( rdn == NULL || rdn == '\0' ) {
695                 return 0;
696         }
697
698         /*
699          * must be parsable
700          */
701         rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
702         if ( rc != LDAP_SUCCESS ) {
703                 return 0;
704         }
705
706         /*
707          * Must be one-level
708          */
709         if ( p[ 0 ] != '\0' ) {
710                 return 0;
711         }
712
713         /*
714          * Schema-aware validate
715          */
716         if ( rc == LDAP_SUCCESS ) {
717                 rc = LDAPDN_validate( DN );
718         }
719         ldap_rdnfree( RDN );
720
721         /*
722          * Must validate (there's a repeated parsing ...)
723          */
724         return ( rc == LDAP_SUCCESS );
725 #endif
726 }
727
728
729 /* build_new_dn:
730  *
731  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
732  * renamed.
733  *
734  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
735  */
736
737 void
738 build_new_dn( struct berval * new_dn,
739         struct berval * parent_dn,
740         struct berval * newrdn )
741 {
742         char *ptr;
743
744         if ( parent_dn == NULL ) {
745                 ber_dupbv( new_dn, newrdn );
746                 return;
747         }
748
749         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
750         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
751
752         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
753         *ptr++ = ',';
754         strcpy( ptr, parent_dn->bv_val );
755 }
756
757
758 /*
759  * dnIsSuffix - tells whether suffix is a suffix of dn.
760  * Both dn and suffix must be normalized.
761  */
762 int
763 dnIsSuffix(
764         const struct berval *dn,
765         const struct berval *suffix )
766 {
767         int     d = dn->bv_len - suffix->bv_len;
768
769         assert( dn );
770         assert( suffix );
771
772         /* empty suffix matches any dn */
773         if ( suffix->bv_len == 0 ) {
774                 return 1;
775         }
776
777         /* suffix longer than dn */
778         if ( d < 0 ) {
779                 return 0;
780         }
781
782         /* no rdn separator or escaped rdn separator */
783         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
784                 return 0;
785         }
786
787         /* no possible match or malformed dn */
788         if ( d == 1 ) {
789                 return 0;
790         }
791
792         /* compare */
793         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
794 }