]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
dec568d39297276dc4d1f4a60f05f5279508e0a6
[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 schema aware checks
429  */
430 int
431 dnMatch(
432         int *matchp,
433         slap_mask_t flags,
434         Syntax *syntax,
435         MatchingRule *mr,
436         struct berval *value,
437         void *assertedValue )
438 {
439         int match;
440         struct berval *asserted = (struct berval *) assertedValue;
441
442         assert( matchp );
443         assert( value );
444         assert( assertedValue );
445         
446         match = value->bv_len - asserted->bv_len;
447
448         if ( match == 0 ) {
449                 match = strcasecmp( value->bv_val, asserted->bv_val );
450         }
451
452 #ifdef NEW_LOGGING
453         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
454                 "dnMatch: %d\n    %s\n    %s\n", match,
455                 value->bv_val, asserted->bv_val ));
456 #else
457         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
458                 match, value->bv_val, asserted->bv_val );
459 #endif
460
461         *matchp = match;
462         return( LDAP_SUCCESS );
463 }
464
465 #ifdef SLAP_DN_MIGRATION
466 /*
467  * these routines are provided for migration purposes only!
468  *      dn_validate is deprecated in favor of dnValidate
469  *      dn_normalize is deprecated in favor of dnNormalize
470  *      strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
471  *
472  * other routines are likewise deprecated but may not yet have
473  * replacement functions.
474  */
475
476 /*
477  * dn_validate - validate and compress dn.  the dn is
478  * compressed in place are returned if valid.
479  * Deprecated in favor of dnValidate()
480  */
481 char *
482 dn_validate( char *dn )
483 {
484         struct berval val;
485         struct berval *pretty = NULL;
486         int             rc;
487
488         if ( dn == NULL || dn[0] == '\0' ) {
489                 return dn;
490         }
491
492         val.bv_val = dn;
493         val.bv_len = strlen( dn );
494
495         rc = dnPretty( NULL, &val, &pretty );
496         if ( rc != LDAP_SUCCESS ) {
497                 return NULL;
498         }
499
500         if ( val.bv_len < pretty->bv_len ) {
501                 ber_bvfree( pretty );
502                 return NULL;
503         }
504
505         AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
506         ber_bvfree( pretty );
507
508         return dn;
509 }
510
511 /*
512  * dn_normalize - put dn into a canonical form suitable for storing
513  * in a hash database.  this involves normalizing the case as well as
514  * the format.  the dn is normalized in place as well as returned if valid.
515  * Deprecated in favor of dnNormalize()
516  */
517 char *
518 dn_normalize( char *dn )
519 {
520         struct berval val;
521         struct berval *normalized = NULL;
522         int             rc;
523
524         if ( dn == NULL || dn[0] == '\0' ) {
525                 return dn;
526         }
527
528         val.bv_val = dn;
529         val.bv_len = strlen( dn );
530
531         rc = dnNormalize( NULL, &val, &normalized );
532         if ( rc != LDAP_SUCCESS ) {
533                 return NULL;
534         }
535
536         if ( val.bv_len < normalized->bv_len ) {
537                 ber_bvfree( normalized );
538                 return NULL;
539         }
540
541         AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
542         ber_bvfree( normalized );
543
544         return dn;
545 }
546
547 /*
548  * dnParent - dn's parent, in-place
549  */
550 int
551 dnParent( 
552         const char      *dn, 
553         const char      **pdn )
554 {
555         LDAPRDN         *tmpRDN;
556         const char      *p;
557         int             rc;
558
559         rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
560         if ( rc != LDAP_SUCCESS ) {
561                 return rc;
562         }
563         ldap_rdnfree( tmpRDN );
564
565         assert( DN_SEPARATOR( p[ 0 ] ) );
566         p++;
567
568         while ( ASCII_SPACE( p[ 0 ] ) ) {
569                 p++;
570         }
571
572         *pdn = p;
573
574         return LDAP_SUCCESS;
575 }
576
577 /*
578  * dn_parent - return the dn's parent, in-place
579  * FIXME: should be replaced by dnParent()
580  */
581 char *
582 dn_parent(
583         Backend         *be,
584         const char      *dn )
585 {
586         const char      *pdn;
587
588         if ( dn == NULL ) {
589                 return NULL;
590         }
591
592         while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
593                 dn++;
594         }
595
596         if ( dn[ 0 ] == '\0' ) {
597                 return NULL;
598         }
599
600         if ( be != NULL && be_issuffix( be, dn ) ) {
601                 return NULL;
602         }
603
604         if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
605                 return NULL;
606         }
607         
608         return ( char * )pdn;
609 }
610
611 int
612 dnExtractRdn( 
613         const char      *dn, 
614         struct berval   **rdn )
615 {
616         LDAPRDN         *tmpRDN;
617         const char      *p;
618         char            *rdnout;
619         int             rc;
620
621         assert( dn );
622         assert( rdn );
623
624         rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
625         if ( rc != LDAP_SUCCESS ) {
626                 return rc;
627         }
628
629         rc = ldap_rdn2str( tmpRDN, &rdnout, LDAP_DN_FORMAT_LDAPV3 );
630         ldap_rdnfree( tmpRDN );
631         if ( rc != LDAP_SUCCESS ) {
632                 return rc;
633         }
634
635         *rdn = ber_bvstr( rdnout );
636         if ( *rdn == NULL ) {
637                 free( rdnout );
638                 return LDAP_NO_MEMORY;
639         }
640
641         return LDAP_SUCCESS;
642 }
643
644 /*
645  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
646  */
647 int 
648 dn_rdnlen(
649         Backend         *be,
650         const char      *dn_in )
651 {
652         struct berval   *rdn = NULL;
653         int             retval = 0;
654
655         assert( dn_in );
656
657         if ( dn_in == NULL ) {
658                 return 0;
659         }
660
661         while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
662                 dn_in++;
663         }
664
665         if ( dn_in[ 0 ] == '\0' ) {
666                 return 0;
667         }
668
669         if ( be != NULL && be_issuffix( be, dn_in ) ) {
670                 return 0;
671         }
672
673         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
674                 ber_bvfree( rdn );
675                 return 0;
676         }
677
678         retval = rdn->bv_len;
679         ber_bvfree( rdn );
680
681         return retval;
682 }
683
684 /*
685  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
686  */
687 char * dn_rdn(
688         Backend *be,
689         const char      *dn_in )
690 {
691         struct berval   *rdn = NULL;
692         char            *retval;
693
694         assert( dn_in );
695
696         if ( dn_in == NULL ) {
697                 return NULL;
698         }
699
700         while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
701                 dn_in++;
702         }
703
704         if ( dn_in[ 0 ] == '\0' ) {
705                 return NULL;
706         }
707
708         if ( be != NULL && be_issuffix( be, dn_in ) ) {
709                 return NULL;
710         }
711
712         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
713                 ber_bvfree( rdn );
714                 return NULL;
715         }
716
717         retval = rdn->bv_val;
718         free( rdn );
719
720         return retval;
721 }
722
723 /*
724  * return a charray of all subtrees to which the DN resides in
725  */
726 char **dn_subtree(
727         Backend *be,
728         const char      *dn )
729 {
730         char **subtree = NULL;
731         
732         do {
733                 charray_add( &subtree, dn );
734
735                 dn = dn_parent( be, dn );
736
737         } while ( dn != NULL );
738
739         return subtree;
740 }
741
742 /*
743  * dn_issuffix - tells whether suffix is a suffix of dn.
744  * Both dn and suffix must be normalized.
745  *      deprecated in favor of dnIsSuffix()
746  */
747 int
748 dn_issuffix(
749         const char      *dn,
750         const char      *suffix
751 )
752 {
753         struct berval   bvdn, bvsuffix;
754
755         assert( dn );
756         assert( suffix );
757
758         bvdn.bv_val = (char *) dn;
759         bvdn.bv_len = strlen( dn );
760         bvsuffix.bv_val = (char *) suffix;
761         bvsuffix.bv_len = strlen( suffix );
762
763         return dnIsSuffix( &bvdn, &bvsuffix );
764 }
765
766 /* rdn_attr_type:
767  *
768  * Given a string (i.e. an rdn) of the form:
769  *       "attribute_type = attribute_value"
770  * this function returns the type of an attribute, that is the
771  * string "attribute_type" which is placed in newly allocated
772  * memory. The returned string will be null-terminated.
773  *
774  * Deprecated
775  */
776
777 char * rdn_attr_type( const char * s )
778 {
779         char    **attrs, **values, *retval;
780
781         if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
782                 return NULL;
783         }
784
785         retval = ch_strdup( attrs[ 0 ] );
786
787         charray_free( attrs );
788         charray_free( values );
789
790         return retval;
791 }
792
793
794 /* rdn_attr_value:
795  *
796  * Given a string (i.e. an rdn) of the form:
797  *       "attribute_type = attribute_value"
798  * this function returns "attribute_type" which is placed in newly allocated
799  * memory. The returned string will be null-terminated and may contain
800  * spaces (i.e. "John Doe\0").
801  *
802  * Deprecated
803  */
804
805 char *
806 rdn_attr_value( const char * rdn )
807 {
808         char    **attrs, **values, *retval;
809
810         if ( rdn_attrs( rdn, &attrs, &values ) != LDAP_SUCCESS ) {
811                 return NULL;
812         }
813
814         retval = ch_strdup( values[ 0 ] );
815
816         charray_free( attrs );
817         charray_free( values );
818
819         return retval;
820 }
821
822
823 /* rdn_attrs:
824  *
825  * Given a string (i.e. an rdn) of the form:
826  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
827  * this function stores the types of the attributes in ptypes, that is the
828  * array of strings "attribute_type" which is placed in newly allocated
829  * memory, and the values of the attributes in pvalues, that is the
830  * array of strings "attribute_value" which is placed in newly allocated
831  * memory. Returns 0 on success, -1 on failure.
832  *
833  * note: got part of the code from dn_validate
834  *
835  * Deprecated; directly use LDAPRDN from ldap_str2rdn
836  */
837
838 int
839 rdn_attrs( const char * rdn, char ***types, char ***values)
840 {
841         LDAPRDN         *tmpRDN;
842         const char      *p;
843         int             iAVA;
844         int             rc;
845         
846         assert( rdn );
847         assert( types );
848         assert( values );
849
850         rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
851         if ( rc != LDAP_SUCCESS ) {
852                 return rc;
853         }
854
855         for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
856                 LDAPAVA         *ava = tmpRDN[ iAVA ][ 0 ];
857
858                 assert( ava );
859                 assert( ava->la_attr );
860                 assert( ava->la_value );
861
862                 charray_add_n( types, ava->la_attr->bv_val, 
863                                 ava->la_attr->bv_len );
864                 charray_add_n( values, ava->la_value->bv_val, 
865                                 ava->la_value->bv_len );
866         }
867
868         ldap_rdnfree( tmpRDN );
869
870         return LDAP_SUCCESS;
871 }
872
873
874 /* rdn_validate:
875  *
876  * 1 if rdn is a legal rdn;
877  * 0 otherwise (including a sequence of rdns)
878  */
879 int
880 rdn_validate( const char *rdn )
881 {
882         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
883         const char      *p;
884         int             rc;
885
886         /*
887          * must be non-empty
888          */
889         if ( rdn == NULL || rdn == '\0' ) {
890                 return 0;
891         }
892
893         /*
894          * must be parsable
895          */
896         rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
897         if ( rc != LDAP_SUCCESS ) {
898                 return 0;
899         }
900
901         /*
902          * Must be one-level
903          */
904         if ( p[ 0 ] != '\0' ) {
905                 return 0;
906         }
907
908         /*
909          * Schema-aware validate
910          */
911         if ( rc == LDAP_SUCCESS ) {
912                 rc = LDAPDN_validate( DN );
913         }
914         ldap_rdnfree( RDN );
915
916         /*
917          * Must validate (there's a repeated parsing ...)
918          */
919         return ( rc == LDAP_SUCCESS );
920 }
921
922
923 /* build_new_dn:
924  *
925  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
926  * renamed.
927  *
928  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
929  */
930
931 void
932 build_new_dn( char ** new_dn,
933         const char * entry_dn,
934         const char * parent_dn,
935         const char * newrdn )
936 {
937
938         if ( parent_dn == NULL ) {
939                 *new_dn = ch_strdup( newrdn );
940                 return;
941         }
942
943         *new_dn = (char *) ch_malloc(
944                 strlen( parent_dn ) + strlen( newrdn ) + 2 );
945
946         strcpy( *new_dn, newrdn );
947         strcat( *new_dn, "," );
948         strcat( *new_dn, parent_dn );
949 }
950
951 #endif /* SLAP_DN_MIGRATION */
952
953 /*
954  * dnIsSuffix - tells whether suffix is a suffix of dn.
955  * Both dn and suffix must be normalized.
956  */
957 int
958 dnIsSuffix(
959         const struct berval *dn,
960         const struct berval *suffix )
961 {
962         int     d = dn->bv_len - suffix->bv_len;
963
964         assert( dn );
965         assert( suffix );
966
967         /* empty suffix matches any dn */
968         if ( suffix->bv_len == 0 ) {
969                 return 1;
970         }
971
972         /* suffix longer than dn */
973         if ( d < 0 ) {
974                 return 0;
975         }
976
977         /* no rdn separator or escaped rdn separator */
978         if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) 
979                                 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
980                 return 0;
981         }
982
983         /* no possible match or malformed dn */
984         if ( d == 1 ) {
985                 return 0;
986         }
987
988         /* compare */
989         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
990 }