]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
b776e18e302518d1807c9548d960d218bbf1a633
[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( flags & SLAP_LDAPDN_PRETTY ) {
262                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
263                                 mr = NULL;
264                         } else {
265                                 transf = ad->ad_type->sat_syntax->ssyn_normalize;
266                                 mr = ad->ad_type->sat_equality;
267                         }
268
269                         if ( transf ) {
270                                 /*
271                                  * transform value by normalize/pretty function
272                                  */
273                                 rc = ( *transf )( ad->ad_type->sat_syntax,
274                                         &ava->la_value, &bv );
275                         
276                                 if ( rc != LDAP_SUCCESS ) {
277                                         return LDAP_INVALID_SYNTAX;
278                                 }
279                         }
280
281                         if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
282                                 char *s = bv.bv_val;
283
284                                 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
285                                         : &ava->la_value, LDAP_UTF8_CASEFOLD ),
286                                         0, 0, &bv );
287                                 free( s );
288                         }
289
290                         if( bv.bv_val ) {
291                                 free( ava->la_value.bv_val );
292                                 ava->la_value = bv;
293                         }
294
295                         if( do_sort ) AVA_Sort( rdn, iAVA );
296                 }
297         }
298
299         return LDAP_SUCCESS;
300 }
301
302 /*
303  * dn normalize routine
304  */
305 int
306 dnNormalize(
307         Syntax *syntax,
308         struct berval *val,
309         struct berval **normalized )
310 {
311         struct berval *out;
312         int rc;
313
314         assert( normalized && *normalized == NULL );
315
316         out = ch_malloc( sizeof( struct berval ) );
317         rc = dnNormalize2( syntax, val, out );
318         if ( rc != LDAP_SUCCESS )
319                 free( out );
320         else
321                 *normalized = out;
322         return rc;
323 }
324
325 int
326 dnNormalize2(
327         Syntax *syntax,
328         struct berval *val,
329         struct berval *out )
330 {
331         assert( val );
332         assert( out );
333
334         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
335
336         if ( val->bv_len != 0 ) {
337                 LDAPDN          *dn = NULL;
338                 int             rc;
339
340                 /*
341                  * Go to structural representation
342                  */
343                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
344                 if ( rc != LDAP_SUCCESS ) {
345                         return LDAP_INVALID_SYNTAX;
346                 }
347
348                 /*
349                  * Schema-aware rewrite
350                  */
351                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
352                         ldap_dnfree( dn );
353                         return LDAP_INVALID_SYNTAX;
354                 }
355
356                 /*
357                  * Back to string representation
358                  */
359                 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
360
361                 ldap_dnfree( dn );
362
363                 if ( rc != LDAP_SUCCESS ) {
364                         return LDAP_INVALID_SYNTAX;
365                 }
366         } else {
367                 ber_dupbv( out, val );
368         }
369
370         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
371
372         return LDAP_SUCCESS;
373 }
374
375 /*
376  * dn "pretty"ing routine
377  */
378 int
379 dnPretty(
380         Syntax *syntax,
381         struct berval *val,
382         struct berval **pretty)
383 {
384         struct berval *out;
385         int rc;
386
387         assert( pretty && *pretty == NULL );
388
389         out = ch_malloc( sizeof( struct berval ) );
390         rc = dnPretty2( syntax, val, out );
391         if ( rc != LDAP_SUCCESS )
392                 free( out );
393         else
394                 *pretty = out;
395         return rc;
396 }
397
398 int
399 dnPretty2(
400         Syntax *syntax,
401         struct berval *val,
402         struct berval *out)
403 {
404         assert( val );
405         assert( out );
406
407         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
408
409         if ( val->bv_len != 0 ) {
410                 LDAPDN          *dn = NULL;
411                 int             rc;
412
413                 /* FIXME: should be liberal in what we accept */
414                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
415                 if ( rc != LDAP_SUCCESS ) {
416                         return LDAP_INVALID_SYNTAX;
417                 }
418
419                 /*
420                  * Schema-aware rewrite
421                  */
422                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
423                         ldap_dnfree( dn );
424                         return LDAP_INVALID_SYNTAX;
425                 }
426
427                 /* FIXME: not sure why the default isn't pretty */
428                 /* RE: the default is the form that is used as
429                  * an internal representation; the pretty form
430                  * is a variant */
431                 rc = ldap_dn2bv( dn, out,
432                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
433
434                 ldap_dnfree( dn );
435
436                 if ( rc != LDAP_SUCCESS ) {
437                         return LDAP_INVALID_SYNTAX;
438                 }
439         } else {
440                 ber_dupbv( out, val );
441         }
442
443         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
444
445         return LDAP_SUCCESS;
446 }
447
448 /*
449  * Combination of both dnPretty and dnNormalize
450  */
451 int
452 dnPrettyNormal(
453         Syntax *syntax,
454         struct berval *val,
455         struct berval *pretty,
456         struct berval *normal)
457 {
458         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
459
460         assert( val );
461         assert( pretty );
462         assert( normal );
463
464         if ( val->bv_len != 0 ) {
465                 LDAPDN          *dn = NULL;
466                 int             rc;
467
468                 pretty->bv_val = NULL;
469                 normal->bv_val = NULL;
470                 pretty->bv_len = 0;
471                 normal->bv_len = 0;
472
473                 /* FIXME: str2dn should take a bv and handle this */
474                 if( strlen( val->bv_val ) != val->bv_len ) {
475                         return LDAP_INVALID_SYNTAX;
476                 }
477
478                 /* FIXME: should be liberal in what we accept */
479                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
480                 if ( rc != LDAP_SUCCESS ) {
481                         return LDAP_INVALID_SYNTAX;
482                 }
483
484                 /*
485                  * Schema-aware rewrite
486                  */
487                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
488                         ldap_dnfree( dn );
489                         return LDAP_INVALID_SYNTAX;
490                 }
491
492                 rc = ldap_dn2bv( dn, pretty,
493                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
494
495                 if ( rc != LDAP_SUCCESS ) {
496                         ldap_dnfree( dn );
497                         return LDAP_INVALID_SYNTAX;
498                 }
499
500                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
501                         ldap_dnfree( dn );
502                         free( pretty->bv_val );
503                         pretty->bv_val = NULL;
504                         pretty->bv_len = 0;
505                         return LDAP_INVALID_SYNTAX;
506                 }
507
508                 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
509
510                 ldap_dnfree( dn );
511                 if ( rc != LDAP_SUCCESS ) {
512                         free( pretty->bv_val );
513                         pretty->bv_val = NULL;
514                         pretty->bv_len = 0;
515                         return LDAP_INVALID_SYNTAX;
516                 }
517         } else {
518                 ber_dupbv( pretty, val );
519                 ber_dupbv( normal, val );
520         }
521
522         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
523                 pretty->bv_val, normal->bv_val, 0 );
524
525         return LDAP_SUCCESS;
526 }
527
528 /*
529  * dnMatch routine
530  */
531 int
532 dnMatch(
533         int *matchp,
534         slap_mask_t flags,
535         Syntax *syntax,
536         MatchingRule *mr,
537         struct berval *value,
538         void *assertedValue )
539 {
540         int match;
541         struct berval *asserted = (struct berval *) assertedValue;
542
543         assert( matchp );
544         assert( value );
545         assert( assertedValue );
546         
547         match = value->bv_len - asserted->bv_len;
548
549         if ( match == 0 ) {
550                 match = strcmp( value->bv_val, asserted->bv_val );
551         }
552
553 #ifdef NEW_LOGGING
554         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
555                 "dnMatch: %d\n    %s\n    %s\n", match,
556                 value->bv_val, asserted->bv_val ));
557 #else
558         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
559                 match, value->bv_val, asserted->bv_val );
560 #endif
561
562         *matchp = match;
563         return( LDAP_SUCCESS );
564 }
565
566 /*
567  * dnParent - dn's parent, in-place
568  *
569  * note: the incoming dn is assumed to be normalized/prettyfied,
570  * so that escaped rdn/ava separators are in '\'+hexpair form
571  */
572 void
573 dnParent( 
574         struct berval   *dn, 
575         struct berval   *pdn )
576 {
577         char    *p;
578
579         p = strchr( dn->bv_val, ',' );
580
581         /* one-level dn */
582         if ( p == NULL ) {
583                 *pdn = slap_empty_bv;
584                 return;
585         }
586
587         assert( DN_SEPARATOR( p[ 0 ] ) );
588         p++;
589
590         assert( ATTR_LEADCHAR( p[ 0 ] ) );
591         pdn->bv_val = p;
592         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
593
594         return;
595 }
596
597 int
598 dnExtractRdn( 
599         struct berval   *dn, 
600         struct berval   *rdn )
601 {
602         LDAPRDN         *tmpRDN;
603         const char      *p;
604         int             rc;
605
606         assert( dn );
607         assert( rdn );
608
609         if( dn->bv_len == 0 ) {
610                 return LDAP_OTHER;
611         }
612
613         rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
614         if ( rc != LDAP_SUCCESS ) {
615                 return rc;
616         }
617
618         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
619         ldap_rdnfree( tmpRDN );
620         if ( rc != LDAP_SUCCESS ) {
621                 return rc;
622         }
623
624         return LDAP_SUCCESS;
625 }
626
627 /*
628  * We can assume the input is a prettied or normalized DN
629  */
630 int 
631 dn_rdnlen(
632         Backend         *be,
633         struct berval   *dn_in )
634 {
635         const char      *p;
636
637         assert( dn_in );
638
639         if ( dn_in == NULL ) {
640                 return 0;
641         }
642
643         if ( !dn_in->bv_len ) {
644                 return 0;
645         }
646
647         if ( be != NULL && be_issuffix( be, dn_in ) ) {
648                 return 0;
649         }
650
651         p = strchr( dn_in->bv_val, ',' );
652
653         return p ? p - dn_in->bv_val : dn_in->bv_len;
654 }
655
656
657 /* rdnValidate:
658  *
659  * LDAP_SUCCESS if rdn is a legal rdn;
660  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
661  */
662 int
663 rdnValidate( struct berval *rdn )
664 {
665 #if 1
666         /* Major cheat!
667          * input is a pretty or normalized DN
668          * hence, we can just search for ','
669          */
670         if( rdn == NULL || rdn->bv_len == 0 ) {
671                 return LDAP_INVALID_SYNTAX;
672         }
673
674         return strchr( rdn->bv_val, ',' ) == NULL
675                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
676
677 #else
678         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
679         const char      *p;
680         int             rc;
681
682         /*
683          * must be non-empty
684          */
685         if ( rdn == NULL || rdn == '\0' ) {
686                 return 0;
687         }
688
689         /*
690          * must be parsable
691          */
692         rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
693         if ( rc != LDAP_SUCCESS ) {
694                 return 0;
695         }
696
697         /*
698          * Must be one-level
699          */
700         if ( p[ 0 ] != '\0' ) {
701                 return 0;
702         }
703
704         /*
705          * Schema-aware validate
706          */
707         if ( rc == LDAP_SUCCESS ) {
708                 rc = LDAPDN_validate( DN );
709         }
710         ldap_rdnfree( RDN );
711
712         /*
713          * Must validate (there's a repeated parsing ...)
714          */
715         return ( rc == LDAP_SUCCESS );
716 #endif
717 }
718
719
720 /* build_new_dn:
721  *
722  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
723  * renamed.
724  *
725  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
726  */
727
728 void
729 build_new_dn( struct berval * new_dn,
730         struct berval * parent_dn,
731         struct berval * newrdn )
732 {
733         char *ptr;
734
735         if ( parent_dn == NULL ) {
736                 ber_dupbv( new_dn, newrdn );
737                 return;
738         }
739
740         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
741         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
742
743         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
744         *ptr++ = ',';
745         strcpy( ptr, parent_dn->bv_val );
746 }
747
748
749 /*
750  * dnIsSuffix - tells whether suffix is a suffix of dn.
751  * Both dn and suffix must be normalized.
752  */
753 int
754 dnIsSuffix(
755         const struct berval *dn,
756         const struct berval *suffix )
757 {
758         int     d = dn->bv_len - suffix->bv_len;
759
760         assert( dn );
761         assert( suffix );
762
763         /* empty suffix matches any dn */
764         if ( suffix->bv_len == 0 ) {
765                 return 1;
766         }
767
768         /* suffix longer than dn */
769         if ( d < 0 ) {
770                 return 0;
771         }
772
773         /* no rdn separator or escaped rdn separator */
774         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
775                 return 0;
776         }
777
778         /* no possible match or malformed dn */
779         if ( d == 1 ) {
780                 return 0;
781         }
782
783         /* compare */
784         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
785 }