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