]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
3b46620dfba9b65ec147f62e41b4234a4bef05d3
[openldap] / servers / slapd / dn.c
1 /* dn.c - routines for dealing with distinguished names */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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 #define SLAP_LDAPDN_PRETTY 0x1
22
23 /*
24  * The DN syntax-related functions take advantage of the dn representation
25  * handling functions ldap_str2dn/ldap_dn2str.  The latter are not schema-
26  * aware, so the attributes and their values need be validated (and possibly
27  * normalized).  In the current implementation the required validation/nor-
28  * malization/"pretty"ing are done on newly created DN structural represen-
29  * tations; however the idea is to move towards DN handling in structural
30  * representation instead of the current string representation.  To this
31  * purpose, we need to do only the required operations and keep track of
32  * what has been done to minimize their impact on performances.
33  *
34  * Developers are strongly encouraged to use this feature, to speed-up
35  * its stabilization.
36  */
37
38 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
39
40 /*
41  * In-place, schema-aware validation of the
42  * structural representation of a distinguished name.
43  */
44 static int
45 LDAPDN_validate( LDAPDN *dn )
46 {
47         int             iRDN;
48         int             rc;
49
50         assert( dn );
51
52         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
53                 LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
54                 int             iAVA;
55
56                 assert( rdn );
57
58                 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
59                         LDAPAVA                 *ava = rdn[ iAVA ][ 0 ];
60                         AttributeDescription    *ad;
61                         slap_syntax_validate_func *validate = NULL;
62
63                         assert( ava );
64                         
65                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
66                                 const char      *text = NULL;
67
68                                 rc = slap_bv2ad( ava->la_attr, &ad, &text );
69                                 if ( rc != LDAP_SUCCESS ) {
70                                         return LDAP_INVALID_SYNTAX;
71                                 }
72
73                                 ava->la_private = ( void * )ad;
74                         }
75
76                         /* 
77                          * Replace attr oid/name with the canonical name
78                          */
79                         ber_bvfree( ava->la_attr );
80                         ava->la_attr = ber_bvdup( &ad->ad_cname );
81
82                         validate = ad->ad_type->sat_syntax->ssyn_validate;
83
84                         if ( validate ) {
85                                 /*
86                                  * validate value by validate function
87                                  */
88                                 rc = ( *validate )( ad->ad_type->sat_syntax,
89                                         ava->la_value );
90                         
91                                 if ( rc != LDAP_SUCCESS ) {
92                                         return LDAP_INVALID_SYNTAX;
93                                 }
94                         }
95                 }
96         }
97
98         return LDAP_SUCCESS;
99 }
100
101 /*
102  * dn validate routine
103  */
104 int
105 dnValidate(
106         Syntax *syntax,
107         struct berval *in )
108 {
109         int             rc;
110         LDAPDN          *dn = NULL;
111
112         assert( in );
113
114         if ( in->bv_len == 0 ) {
115                 return( LDAP_SUCCESS );
116         }
117
118         rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
119
120         /*
121          * Schema-aware validate
122          */
123         if ( rc == LDAP_SUCCESS ) {
124                 rc = LDAPDN_validate( dn );
125         }
126         
127         ldap_dnfree( dn );
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  * uses a linear search; should be fine since the number of AVAs in
145  * a RDN should be limited.
146  */
147 static void
148 AVA_Sort( LDAPRDN *rdn, int iAVA )
149 {
150         int             i;
151         LDAPAVA         *ava_in = rdn[ iAVA ][ 0 ];
152
153         assert( rdn );
154         assert( ava_in );
155         
156         for ( i = 0; i < iAVA; i++ ) {
157                 LDAPAVA         *ava = rdn[ i ][ 0 ];
158                 int             a, j;
159
160                 assert( ava );
161
162                 a = strcmp( ava_in->la_attr->bv_val, ava->la_attr->bv_val );
163
164                 if ( a > 0 ) {
165                         break;
166                 }
167
168                 while ( a == 0 ) {
169                         int             v, d;
170
171                         d = ava_in->la_value->bv_len - ava->la_value->bv_len;
172
173                         v = memcmp( ava_in->la_value->bv_val, 
174                                         ava->la_value->bv_val,
175                                         d <= 0 ? ava_in->la_value->bv_len 
176                                                 : ava->la_value->bv_len );
177
178                         if ( v == 0 && d != 0 ) {
179                                 v = d;
180                         }
181
182                         if ( v <= 0 ) {
183                                 /* 
184                                  * got it!
185                                  */
186                                 break;
187                         }
188
189                         if ( ++i == iAVA ) {
190                                 /*
191                                  * already sorted
192                                  */
193                                 return;
194                         }
195
196                         ava = rdn[ i ][ 0 ];
197                         a = strcmp( ava_in->la_value->bv_val, 
198                                         ava->la_value->bv_val );
199                 }
200
201                 /*
202                  * move ahead
203                  */
204                 for ( j = iAVA; j > i; j-- ) {
205                         rdn[ j ][ 0 ] = rdn[ j - 1 ][ 0 ];
206                 }
207                 rdn[ i ][ 0 ] = ava_in;
208
209                 return;
210         }
211 }
212
213 /*
214  * In-place, schema-aware normalization / "pretty"ing of the
215  * structural representation of a distinguished name.
216  */
217 static int
218 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
219 {
220         int             iRDN;
221         int             rc;
222
223         assert( dn );
224
225         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
226                 LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
227                 int             iAVA;
228
229                 assert( rdn );
230
231                 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
232                         LDAPAVA                 *ava = rdn[ iAVA ][ 0 ];
233                         AttributeDescription    *ad;
234                         slap_syntax_transform_func *transf = NULL;
235                         MatchingRule *mr;
236                         struct berval           *bv = NULL;
237
238                         assert( ava );
239
240                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
241                                 const char      *text = NULL;
242
243                                 rc = slap_bv2ad( ava->la_attr, &ad, &text );
244                                 if ( rc != LDAP_SUCCESS ) {
245                                         return LDAP_INVALID_SYNTAX;
246                                 }
247                                 
248                                 ava->la_private = ( void * )ad;
249                         }
250
251                         /* 
252                          * Replace attr oid/name with the canonical name
253                          */
254                         ber_bvfree( ava->la_attr );
255                         ava->la_attr = ber_bvdup( &ad->ad_cname );
256
257                         if( flags & SLAP_LDAPDN_PRETTY ) {
258                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
259                                 mr = NULL;
260                         } else {
261                                 transf = ad->ad_type->sat_syntax->ssyn_normalize;
262                                 mr = ad->ad_type->sat_equality;
263                         }
264
265                         if ( transf ) {
266                                 /*
267                                  * transform value by normalize/pretty function
268                                  */
269                                 rc = ( *transf )( ad->ad_type->sat_syntax,
270                                         ava->la_value, &bv );
271                         
272                                 if ( rc != LDAP_SUCCESS ) {
273                                         return LDAP_INVALID_SYNTAX;
274                                 }
275                         }
276
277                         if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
278                                 struct berval *s = bv;
279
280                                 bv = ber_bvstr( UTF8normalize( bv ? bv : ava->la_value, 
281                                         UTF8_CASEFOLD ) );
282
283                                 ber_bvfree( s );
284                         }
285
286                         if( bv ) {
287                                 ber_bvfree( ava->la_value );
288                                 ava->la_value = bv;
289                         }
290
291                         AVA_Sort( rdn, iAVA );
292                 }
293         }
294
295         return LDAP_SUCCESS;
296 }
297
298 /*
299  * dn normalize routine
300  */
301 int
302 dnNormalize(
303         Syntax *syntax,
304         struct berval *val,
305         struct berval **normalized )
306 {
307         struct berval *out = NULL;
308
309         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
310
311         assert( val );
312         assert( normalized );
313
314         assert( *normalized == NULL );
315
316         if ( val->bv_len != 0 ) {
317                 LDAPDN          *dn = NULL;
318                 char            *dn_out = NULL;
319                 int             rc;
320
321                 /*
322                  * Go to structural representation
323                  */
324                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
325                 if ( rc != LDAP_SUCCESS ) {
326                         return LDAP_INVALID_SYNTAX;
327                 }
328
329                 /*
330                  * Schema-aware rewrite
331                  */
332                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
333                         ldap_dnfree( dn );
334                         return LDAP_INVALID_SYNTAX;
335                 }
336
337                 /*
338                  * Back to string representation
339                  */
340                 rc = ldap_dn2str( dn, &dn_out, LDAP_DN_FORMAT_LDAPV3 );
341
342                 ldap_dnfree( dn );
343
344                 if ( rc != LDAP_SUCCESS ) {
345                         return LDAP_INVALID_SYNTAX;
346                 }
347
348                 out = ber_bvstr( dn_out );
349
350         } else {
351                 out = ber_bvdup( val );
352         }
353
354         /* FIXME: cheat! */
355         ldap_pvt_str2upper( out->bv_val );
356
357         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
358
359         *normalized = out;
360         return LDAP_SUCCESS;
361 }
362
363 /*
364  * dn "pretty"ing routine
365  */
366 int
367 dnPretty(
368         Syntax *syntax,
369         struct berval *val,
370         struct berval **pretty)
371 {
372         struct berval *out = NULL;
373
374         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
375
376         assert( val );
377         assert( pretty );
378         assert( *pretty == NULL );
379
380         if ( val->bv_len != 0 ) {
381                 LDAPDN          *dn = NULL;
382                 char            *dn_out = NULL;
383                 int             rc;
384
385                 /* FIXME: should be liberal in what we accept */
386                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
387                 if ( rc != LDAP_SUCCESS ) {
388                         return LDAP_INVALID_SYNTAX;
389                 }
390
391                 /*
392                  * Schema-aware rewrite
393                  */
394                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
395                         ldap_dnfree( dn );
396                         return LDAP_INVALID_SYNTAX;
397                 }
398
399                 /* FIXME: not sure why the default isn't pretty */
400                 /* RE: the default is the form that is used as
401                  * an internal representation; the pretty form
402                  * is a variant */
403                 rc = ldap_dn2str( dn, &dn_out,
404                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
405
406                 ldap_dnfree( dn );
407
408                 if ( rc != LDAP_SUCCESS ) {
409                         return LDAP_INVALID_SYNTAX;
410                 }
411
412                 out = ber_bvstr( dn_out );
413
414         } else {
415                 out = ber_bvdup( val );
416         }
417
418         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
419
420         *pretty = out;
421
422         return LDAP_SUCCESS;
423 }
424
425 /*
426  * dnMatch routine
427  *
428  * FIXME: strcasecmp should be replaced with strcmp once
429  * value normalization issues are sorted out.
430  */
431 int
432 dnMatch(
433         int *matchp,
434         slap_mask_t flags,
435         Syntax *syntax,
436         MatchingRule *mr,
437         struct berval *value,
438         void *assertedValue )
439 {
440         int match;
441         struct berval *asserted = (struct berval *) assertedValue;
442
443         assert( matchp );
444         assert( value );
445         assert( assertedValue );
446         
447         match = value->bv_len - asserted->bv_len;
448
449         if ( match == 0 ) {
450                 match = strcasecmp( value->bv_val, asserted->bv_val );
451         }
452
453 #ifdef NEW_LOGGING
454         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
455                 "dnMatch: %d\n    %s\n    %s\n", match,
456                 value->bv_val, asserted->bv_val ));
457 #else
458         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
459                 match, value->bv_val, asserted->bv_val );
460 #endif
461
462         *matchp = match;
463         return( LDAP_SUCCESS );
464 }
465
466 #ifdef SLAP_DN_MIGRATION
467 /*
468  * these routines are provided for migration purposes only!
469  *      dn_validate is deprecated in favor of dnValidate
470  *      dn_normalize is deprecated in favor of dnNormalize
471  *      strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
472  *
473  * other routines are likewise deprecated but may not yet have
474  * replacement functions.
475  */
476
477 /*
478  * dn_validate - validate and compress dn.  the dn is
479  * compressed in place are returned if valid.
480  * Deprecated in favor of dnValidate()
481  */
482 char *
483 dn_validate( char *dn )
484 {
485         struct berval val;
486         struct berval *pretty = NULL;
487         int             rc;
488
489         if ( dn == NULL || dn[0] == '\0' ) {
490                 return dn;
491         }
492
493         val.bv_val = dn;
494         val.bv_len = strlen( dn );
495
496         rc = dnPretty( NULL, &val, &pretty );
497         if ( rc != LDAP_SUCCESS ) {
498                 return NULL;
499         }
500
501         if ( val.bv_len < pretty->bv_len ) {
502                 ber_bvfree( pretty );
503                 return NULL;
504         }
505
506         AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
507         ber_bvfree( pretty );
508
509         return dn;
510 }
511
512 /*
513  * dn_normalize - put dn into a canonical form suitable for storing
514  * in a hash database.  this involves normalizing the case as well as
515  * the format.  the dn is normalized in place as well as returned if valid.
516  * Deprecated in favor of dnNormalize()
517  */
518 char *
519 dn_normalize( char *dn )
520 {
521         struct berval val;
522         struct berval *normalized = NULL;
523         int             rc;
524
525         if ( dn == NULL || dn[0] == '\0' ) {
526                 return dn;
527         }
528
529         val.bv_val = dn;
530         val.bv_len = strlen( dn );
531
532         rc = dnNormalize( NULL, &val, &normalized );
533         if ( rc != LDAP_SUCCESS ) {
534                 return NULL;
535         }
536
537         if ( val.bv_len < normalized->bv_len ) {
538                 ber_bvfree( normalized );
539                 return NULL;
540         }
541
542         AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
543         ber_bvfree( normalized );
544
545         return dn;
546 }
547
548 /*
549  * dnParent - dn's parent, in-place
550  */
551 int
552 dnParent( 
553         const char      *dn, 
554         const char      **pdn )
555 {
556         LDAPRDN         *tmpRDN;
557         const char      *p;
558         int             rc;
559
560         rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
561         if ( rc != LDAP_SUCCESS ) {
562                 return rc;
563         }
564         ldap_rdnfree( tmpRDN );
565
566         assert( DN_SEPARATOR( p[ 0 ] ) );
567         p++;
568
569         while ( ASCII_SPACE( p[ 0 ] ) ) {
570                 p++;
571         }
572
573         *pdn = p;
574
575         return LDAP_SUCCESS;
576 }
577
578 /*
579  * dn_parent - return the dn's parent, in-place
580  * FIXME: should be replaced by dnParent()
581  */
582 char *
583 dn_parent(
584         Backend         *be,
585         const char      *dn )
586 {
587         const char      *pdn;
588
589         if ( dn == NULL ) {
590                 return NULL;
591         }
592
593         while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
594                 dn++;
595         }
596
597         if ( dn[ 0 ] == '\0' ) {
598                 return NULL;
599         }
600
601         if ( be != NULL && be_issuffix( be, dn ) ) {
602                 return NULL;
603         }
604
605         if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
606                 return NULL;
607         }
608         
609         return ( char * )pdn;
610 }
611
612 int
613 dnExtractRdn( 
614         const char      *dn, 
615         struct berval   **rdn )
616 {
617         LDAPRDN         *tmpRDN;
618         const char      *p;
619         char            *rdnout;
620         int             rc;
621
622         assert( dn );
623         assert( rdn );
624
625         rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
626         if ( rc != LDAP_SUCCESS ) {
627                 return rc;
628         }
629
630         rc = ldap_rdn2str( tmpRDN, &rdnout, LDAP_DN_FORMAT_LDAPV3 );
631         ldap_rdnfree( tmpRDN );
632         if ( rc != LDAP_SUCCESS ) {
633                 return rc;
634         }
635
636         *rdn = ber_bvstr( rdnout );
637         if ( *rdn == NULL ) {
638                 free( rdnout );
639                 return LDAP_NO_MEMORY;
640         }
641
642         return LDAP_SUCCESS;
643 }
644
645 /*
646  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
647  */
648 int 
649 dn_rdnlen(
650         Backend         *be,
651         const char      *dn_in )
652 {
653         struct berval   *rdn = NULL;
654         int             retval = 0;
655
656         assert( dn_in );
657
658         if ( dn_in == NULL ) {
659                 return 0;
660         }
661
662         while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
663                 dn_in++;
664         }
665
666         if ( dn_in[ 0 ] == '\0' ) {
667                 return 0;
668         }
669
670         if ( be != NULL && be_issuffix( be, dn_in ) ) {
671                 return 0;
672         }
673
674         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
675                 ber_bvfree( rdn );
676                 return 0;
677         }
678
679         retval = rdn->bv_len;
680         ber_bvfree( rdn );
681
682         return retval;
683 }
684
685 /*
686  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
687  */
688 char * dn_rdn(
689         Backend *be,
690         const char      *dn_in )
691 {
692         struct berval   *rdn = NULL;
693         char            *retval;
694
695         assert( dn_in );
696
697         if ( dn_in == NULL ) {
698                 return NULL;
699         }
700
701         while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
702                 dn_in++;
703         }
704
705         if ( dn_in[ 0 ] == '\0' ) {
706                 return NULL;
707         }
708
709         if ( be != NULL && be_issuffix( be, dn_in ) ) {
710                 return NULL;
711         }
712
713         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
714                 ber_bvfree( rdn );
715                 return NULL;
716         }
717
718         retval = rdn->bv_val;
719         free( rdn );
720
721         return retval;
722 }
723
724 /*
725  * return a charray of all subtrees to which the DN resides in
726  */
727 char **dn_subtree(
728         Backend *be,
729         const char      *dn )
730 {
731         char **subtree = NULL;
732         
733         do {
734                 charray_add( &subtree, dn );
735
736                 dn = dn_parent( be, dn );
737
738         } while ( dn != NULL );
739
740         return subtree;
741 }
742
743 /*
744  * dn_issuffix - tells whether suffix is a suffix of dn.
745  * Both dn and suffix must be normalized.
746  *      deprecated in favor of dnIsSuffix()
747  */
748 int
749 dn_issuffix(
750         const char      *dn,
751         const char      *suffix
752 )
753 {
754         struct berval   bvdn, bvsuffix;
755
756         assert( dn );
757         assert( suffix );
758
759         bvdn.bv_val = (char *) dn;
760         bvdn.bv_len = strlen( dn );
761         bvsuffix.bv_val = (char *) suffix;
762         bvsuffix.bv_len = strlen( suffix );
763
764         return dnIsSuffix( &bvdn, &bvsuffix );
765 }
766
767 /* rdn_attr_type:
768  *
769  * Given a string (i.e. an rdn) of the form:
770  *       "attribute_type = attribute_value"
771  * this function returns the type of an attribute, that is the
772  * string "attribute_type" which is placed in newly allocated
773  * memory. The returned string will be null-terminated.
774  *
775  * Deprecated
776  */
777
778 char * rdn_attr_type( const char * s )
779 {
780         char    **attrs, **values, *retval;
781
782         if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
783                 return NULL;
784         }
785
786         retval = ch_strdup( attrs[ 0 ] );
787
788         charray_free( attrs );
789         charray_free( values );
790
791         return retval;
792 }
793
794
795 /* rdn_attr_value:
796  *
797  * Given a string (i.e. an rdn) of the form:
798  *       "attribute_type = attribute_value"
799  * this function returns "attribute_type" which is placed in newly allocated
800  * memory. The returned string will be null-terminated and may contain
801  * spaces (i.e. "John Doe\0").
802  *
803  * Deprecated
804  */
805
806 char *
807 rdn_attr_value( const char * rdn )
808 {
809         char    **attrs, **values, *retval;
810
811         if ( rdn_attrs( rdn, &attrs, &values ) != LDAP_SUCCESS ) {
812                 return NULL;
813         }
814
815         retval = ch_strdup( values[ 0 ] );
816
817         charray_free( attrs );
818         charray_free( values );
819
820         return retval;
821 }
822
823
824 /* rdn_attrs:
825  *
826  * Given a string (i.e. an rdn) of the form:
827  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
828  * this function stores the types of the attributes in ptypes, that is the
829  * array of strings "attribute_type" which is placed in newly allocated
830  * memory, and the values of the attributes in pvalues, that is the
831  * array of strings "attribute_value" which is placed in newly allocated
832  * memory. Returns 0 on success, -1 on failure.
833  *
834  * note: got part of the code from dn_validate
835  *
836  * Deprecated; directly use LDAPRDN from ldap_str2rdn
837  */
838
839 int
840 rdn_attrs( const char * rdn, char ***types, char ***values)
841 {
842         LDAPRDN         *tmpRDN;
843         const char      *p;
844         int             iAVA;
845         int             rc;
846         
847         assert( rdn );
848         assert( types );
849         assert( values );
850
851         rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
852         if ( rc != LDAP_SUCCESS ) {
853                 return rc;
854         }
855
856         for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
857                 LDAPAVA         *ava = tmpRDN[ iAVA ][ 0 ];
858
859                 assert( ava );
860                 assert( ava->la_attr );
861                 assert( ava->la_value );
862
863                 charray_add_n( types, ava->la_attr->bv_val, 
864                                 ava->la_attr->bv_len );
865                 charray_add_n( values, ava->la_value->bv_val, 
866                                 ava->la_value->bv_len );
867         }
868
869         ldap_rdnfree( tmpRDN );
870
871         return LDAP_SUCCESS;
872 }
873
874
875 /* rdn_validate:
876  *
877  * 1 if rdn is a legal rdn;
878  * 0 otherwise (including a sequence of rdns)
879  */
880 int
881 rdn_validate( const char *rdn )
882 {
883         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
884         const char      *p;
885         int             rc;
886
887         /*
888          * must be non-empty
889          */
890         if ( rdn == NULL || rdn == '\0' ) {
891                 return 0;
892         }
893
894         /*
895          * must be parsable
896          */
897         rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
898         if ( rc != LDAP_SUCCESS ) {
899                 return 0;
900         }
901
902         /*
903          * Must be one-level
904          */
905         if ( p[ 0 ] != '\0' ) {
906                 return 0;
907         }
908
909         /*
910          * Schema-aware validate
911          */
912         if ( rc == LDAP_SUCCESS ) {
913                 rc = LDAPDN_validate( DN );
914         }
915         ldap_rdnfree( RDN );
916
917         /*
918          * Must validate (there's a repeated parsing ...)
919          */
920         return ( rc == LDAP_SUCCESS );
921 }
922
923
924 /* build_new_dn:
925  *
926  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
927  * renamed.
928  *
929  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
930  */
931
932 void
933 build_new_dn( char ** new_dn,
934         const char * entry_dn,
935         const char * parent_dn,
936         const char * newrdn )
937 {
938
939         if ( parent_dn == NULL ) {
940                 *new_dn = ch_strdup( newrdn );
941                 return;
942         }
943
944         *new_dn = (char *) ch_malloc(
945                 strlen( parent_dn ) + strlen( newrdn ) + 2 );
946
947         strcpy( *new_dn, newrdn );
948         strcat( *new_dn, "," );
949         strcat( *new_dn, parent_dn );
950 }
951
952 #endif /* SLAP_DN_MIGRATION */
953
954 /*
955  * dnIsSuffix - tells whether suffix is a suffix of dn.
956  * Both dn and suffix must be normalized.
957  */
958 int
959 dnIsSuffix(
960         const struct berval *dn,
961         const struct berval *suffix )
962 {
963         int     d = dn->bv_len - suffix->bv_len;
964
965         assert( dn );
966         assert( suffix );
967
968         /* empty suffix matches any dn */
969         if ( suffix->bv_len == 0 ) {
970                 return 1;
971         }
972
973         /* suffix longer than dn */
974         if ( d < 0 ) {
975                 return 0;
976         }
977
978         /* no rdn separator or escaped rdn separator */
979         if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) 
980                                 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
981                 return 0;
982         }
983
984         /* no possible match or malformed dn */
985         if ( d == 1 ) {
986                 return 0;
987         }
988
989         /* compare */
990         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
991 }