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