]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
check on escaped rdn separator not needed any more
[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 /*
560  * dnParent - dn's parent, in-place
561  *
562  * note: the incoming dn is assumed to be normalized/prettyfied,
563  * so that escaped rdn/ava separators are in '\'+hexpair form
564  */
565 int
566 dnParent( 
567         const char      *dn, 
568         const char      **pdn )
569 {
570         const char      *p;
571
572         p = strchr( dn, ',' );
573
574         /* one-level dn */
575         if ( p == NULL ) {
576                 *pdn = "";
577                 return LDAP_SUCCESS;
578         }
579
580         assert( DN_SEPARATOR( p[ 0 ] ) );
581         p++;
582
583         assert( ATTR_LEADCHAR( p[ 0 ] ) );
584         *pdn = p;
585
586         return LDAP_SUCCESS;
587 }
588
589 #ifdef SLAP_DN_MIGRATION
590 /*
591  * these routines are provided for migration purposes only!
592  *      dn_normalize is deprecated in favor of dnNormalize
593  *      strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
594  *
595  * other routines are likewise deprecated but may not yet have
596  * replacement functions.
597  */
598
599 /*
600  * dn_normalize - put dn into a canonical form suitable for storing
601  * in a hash database.  this involves normalizing the case as well as
602  * the format.  the dn is normalized in place as well as returned if valid.
603  * Deprecated in favor of dnNormalize()
604  */
605 char *
606 dn_normalize( char *dn )
607 {
608         struct berval val;
609         struct berval *normalized = NULL;
610         int             rc;
611
612         if ( dn == NULL || dn[0] == '\0' ) {
613                 return dn;
614         }
615
616         val.bv_val = dn;
617         val.bv_len = strlen( dn );
618
619         rc = dnNormalize( NULL, &val, &normalized );
620         if ( rc != LDAP_SUCCESS ) {
621                 return NULL;
622         }
623
624         if ( val.bv_len < normalized->bv_len ) {
625                 ber_bvfree( normalized );
626                 return NULL;
627         }
628
629         AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
630         ber_bvfree( normalized );
631
632         return dn;
633 }
634
635 /*
636  * dn_parent - return the dn's parent, in-place
637  * FIXME: should be replaced by dnParent()
638  */
639 char *
640 dn_parent(
641         Backend         *be,
642         const char      *dn )
643 {
644         const char      *pdn;
645
646         if ( dn == NULL ) {
647                 return NULL;
648         }
649
650         while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
651                 dn++;
652         }
653
654         if ( dn[ 0 ] == '\0' ) {
655                 return NULL;
656         }
657
658         if ( be != NULL && be_issuffix( be, dn ) ) {
659                 return NULL;
660         }
661
662         if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
663                 return NULL;
664         }
665         
666         return ( char * )pdn;
667 }
668 #endif /* SLAP_DN_MIGRATION */
669
670
671 int
672 dnExtractRdn( 
673         struct berval   *dn, 
674         struct berval   *rdn )
675 {
676         LDAPRDN         *tmpRDN;
677         const char      *p;
678         int             rc;
679
680         assert( dn );
681         assert( rdn );
682
683         if( dn->bv_len == 0 ) {
684                 return LDAP_OTHER;
685         }
686
687         rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
688         if ( rc != LDAP_SUCCESS ) {
689                 return rc;
690         }
691
692         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
693         ldap_rdnfree( tmpRDN );
694         if ( rc != LDAP_SUCCESS ) {
695                 return rc;
696         }
697
698         return LDAP_SUCCESS;
699 }
700
701 /*
702  * FIXME: should be replaced by dnExtractRdn()
703  */
704 int 
705 dn_rdnlen(
706         Backend         *be,
707         struct berval   *dn_in )
708 {
709         int             rc;
710         const char      *p;
711
712         assert( dn_in );
713
714         if ( dn_in == NULL ) {
715                 return 0;
716         }
717
718         if ( !dn_in->bv_len ) {
719                 return 0;
720         }
721
722         if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
723                 return 0;
724         }
725
726         rc = ldap_str2rdn( dn_in->bv_val, NULL, (char **)&p, 
727                         LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
728         if ( rc != LDAP_SUCCESS ) {
729                 return 0;
730         }
731
732         return p - dn_in->bv_val;
733 }
734
735
736 /* rdnValidate:
737  *
738  * LDAP_SUCCESS if rdn is a legal rdn;
739  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
740  */
741 int
742 rdnValidate( struct berval *rdn )
743 {
744 #if 1
745         /* Major cheat!
746          * input is a pretty or normalized DN
747          * hence, we can just search for ','
748          */
749         if( rdn == NULL || rdn->bv_len == 0 ) {
750                 return LDAP_INVALID_SYNTAX;
751         }
752
753         return strchr( rdn->bv_val, ',' ) == NULL
754                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
755
756 #else
757         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
758         const char      *p;
759         int             rc;
760
761         /*
762          * must be non-empty
763          */
764         if ( rdn == NULL || rdn == '\0' ) {
765                 return 0;
766         }
767
768         /*
769          * must be parsable
770          */
771         rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
772         if ( rc != LDAP_SUCCESS ) {
773                 return 0;
774         }
775
776         /*
777          * Must be one-level
778          */
779         if ( p[ 0 ] != '\0' ) {
780                 return 0;
781         }
782
783         /*
784          * Schema-aware validate
785          */
786         if ( rc == LDAP_SUCCESS ) {
787                 rc = LDAPDN_validate( DN );
788         }
789         ldap_rdnfree( RDN );
790
791         /*
792          * Must validate (there's a repeated parsing ...)
793          */
794         return ( rc == LDAP_SUCCESS );
795 #endif
796 }
797
798
799 /* build_new_dn:
800  *
801  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
802  * renamed.
803  *
804  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
805  */
806
807 void
808 build_new_dn( struct berval * new_dn,
809         struct berval * parent_dn,
810         struct berval * newrdn )
811 {
812         char *ptr;
813
814         if ( parent_dn == NULL ) {
815                 ber_dupbv( new_dn, newrdn );
816                 return;
817         }
818
819         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
820         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
821
822         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
823         *ptr++ = ',';
824         strcpy( ptr, parent_dn->bv_val );
825 }
826
827
828 /*
829  * dnIsSuffix - tells whether suffix is a suffix of dn.
830  * Both dn and suffix must be normalized.
831  */
832 int
833 dnIsSuffix(
834         const struct berval *dn,
835         const struct berval *suffix )
836 {
837         int     d = dn->bv_len - suffix->bv_len;
838
839         assert( dn );
840         assert( suffix );
841
842         /* empty suffix matches any dn */
843         if ( suffix->bv_len == 0 ) {
844                 return 1;
845         }
846
847         /* suffix longer than dn */
848         if ( d < 0 ) {
849                 return 0;
850         }
851
852         /* no rdn separator or escaped rdn separator */
853         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
854                 return 0;
855         }
856
857         /* no possible match or malformed dn */
858         if ( d == 1 ) {
859                 return 0;
860         }
861
862         /* compare */
863         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
864 }