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