]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
806852172dc93eef3355a13ca89e0ea2a83aaa73
[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         if ( val->bv_len != 0 ) {
315                 LDAPDN          *dn = NULL;
316                 char            *dn_out = NULL;
317                 int             rc;
318
319                 /*
320                  * Go to structural representation
321                  */
322                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
323                 if ( rc != LDAP_SUCCESS ) {
324                         return LDAP_INVALID_SYNTAX;
325                 }
326
327                 /*
328                  * Schema-aware rewrite
329                  */
330                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
331                         ldap_dnfree( dn );
332                         return LDAP_INVALID_SYNTAX;
333                 }
334
335                 /*
336                  * Back to string representation
337                  */
338                 rc = ldap_dn2str( dn, &dn_out, LDAP_DN_FORMAT_LDAPV3 );
339
340                 ldap_dnfree( dn );
341
342                 if ( rc != LDAP_SUCCESS ) {
343                         return LDAP_INVALID_SYNTAX;
344                 }
345
346                 out = ber_bvstr( dn_out );
347
348         } else {
349                 out = ber_bvdup( val );
350         }
351
352         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
353
354         *normalized = out;
355
356         return LDAP_SUCCESS;
357 }
358
359 /*
360  * dn "pretty"ing routine
361  */
362 int
363 dnPretty(
364         Syntax *syntax,
365         struct berval *val,
366         struct berval **pretty)
367 {
368         struct berval *out = NULL;
369
370         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
371
372         assert( val );
373         assert( pretty );
374
375         if ( val->bv_len != 0 ) {
376                 LDAPDN          *dn = NULL;
377                 char            *dn_out = NULL;
378                 int             rc;
379
380                 /* FIXME: should be liberal in what we accept */
381                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
382                 if ( rc != LDAP_SUCCESS ) {
383                         return LDAP_INVALID_SYNTAX;
384                 }
385
386                 /*
387                  * Schema-aware rewrite
388                  */
389                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
390                         ldap_dnfree( dn );
391                         return LDAP_INVALID_SYNTAX;
392                 }
393
394                 /* FIXME: not sure why the default isn't pretty */
395                 /* RE: the default is the form that is used as
396                  * an internal representation; the pretty form
397                  * is a variant */
398                 rc = ldap_dn2str( dn, &dn_out,
399                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
400
401                 ldap_dnfree( dn );
402
403                 if ( rc != LDAP_SUCCESS ) {
404                         return LDAP_INVALID_SYNTAX;
405                 }
406
407                 out = ber_bvstr( dn_out );
408
409         } else {
410                 out = ber_bvdup( val );
411         }
412
413         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
414
415         *pretty = out;
416
417         return LDAP_SUCCESS;
418 }
419
420 /*
421  * dnMatch routine
422  *
423  * note: uses exact string match (strcmp) because it is supposed to work
424  * on normalized DNs.
425  */
426 int
427 dnMatch(
428         int *matchp,
429         slap_mask_t flags,
430         Syntax *syntax,
431         MatchingRule *mr,
432         struct berval *value,
433         void *assertedValue )
434 {
435         int match;
436         struct berval *asserted = (struct berval *) assertedValue;
437
438         assert( matchp );
439         assert( value );
440         assert( assertedValue );
441         
442         match = value->bv_len - asserted->bv_len;
443
444         if ( match == 0 ) {
445                 match = strcmp( value->bv_val, asserted->bv_val );
446         }
447
448 #ifdef NEW_LOGGING
449         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
450                 "dnMatch: %d\n    %s\n    %s\n", match,
451                 value->bv_val, asserted->bv_val ));
452 #else
453         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
454                 match, value->bv_val, asserted->bv_val );
455 #endif
456
457         *matchp = match;
458         return( LDAP_SUCCESS );
459 }
460
461 #ifdef SLAP_DN_MIGRATION
462 /*
463  * these routines are provided for migration purposes only!
464  *      dn_validate is deprecated in favor of dnValidate
465  *      dn_normalize is deprecated in favor of dnNormalize
466  *      strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
467  *
468  * other routines are likewise deprecated but may not yet have
469  * replacement functions.
470  */
471
472 /*
473  * dn_validate - validate and compress dn.  the dn is
474  * compressed in place are returned if valid.
475  * Deprecated in favor of dnValidate()
476  */
477 char *
478 dn_validate( char *dn )
479 {
480         struct berval val, *pretty;
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, *normalized;
516         int             rc;
517
518         if ( dn == NULL || dn[0] == '\0' ) {
519                 return dn;
520         }
521
522         val.bv_val = dn;
523         val.bv_len = strlen( dn );
524
525         rc = dnNormalize( NULL, &val, &normalized );
526         if ( rc != LDAP_SUCCESS ) {
527                 return NULL;
528         }
529
530         if ( val.bv_len < normalized->bv_len ) {
531                 ber_bvfree( normalized );
532                 return NULL;
533         }
534
535         AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
536         ber_bvfree( normalized );
537
538         return dn;
539 }
540
541
542 /*
543  * dn_parent - return the dn's parent, in-place
544  */
545 char *
546 dn_parent(
547         Backend *be,
548         const char      *dn )
549 {
550         const char      *s;
551         int     inquote;
552
553         if( dn == NULL ) {
554                 return NULL;
555         }
556
557         while(*dn != '\0' && ASCII_SPACE(*dn)) {
558                 dn++;
559         }
560
561         if( *dn == '\0' ) {
562                 return NULL;
563         }
564
565         if ( be != NULL && be_issuffix( be, dn ) ) {
566                 return NULL;
567         }
568
569         /*
570          * assume it is an X.500-style name, which looks like
571          * foo=bar,sha=baz,...
572          */
573
574         inquote = 0;
575         for ( s = dn; *s; s++ ) {
576                 if ( *s == '\\' ) {
577                         if ( *(s + 1) ) {
578                                 s++;
579                         }
580                         continue;
581                 }
582                 if ( inquote ) {
583                         if ( *s == '"' ) {
584                                 inquote = 0;
585                         }
586                 } else {
587                         if ( *s == '"' ) {
588                                 inquote = 1;
589                         } else if ( DN_SEPARATOR( *s ) ) {
590                                 return (char *)s + 1;
591                         }
592                 }
593         }
594
595         return "";
596 }
597
598 int dn_rdnlen(
599         Backend *be,
600         const char      *dn_in )
601 {
602         char    *s;
603         int     inquote;
604
605         if( dn_in == NULL ) {
606                 return 0;
607         }
608
609         while(*dn_in && ASCII_SPACE(*dn_in)) {
610                 dn_in++;
611         }
612
613         if( *dn_in == '\0' ) {
614                 return( 0 );
615         }
616
617         if ( be != NULL && be_issuffix( be, dn_in ) ) {
618                 return( 0 );
619         }
620
621         inquote = 0;
622
623         for ( s = (char *)dn_in; *s; s++ ) {
624                 if ( *s == '\\' ) {
625                         if ( *(s + 1) ) {
626                                 s++;
627                         }
628                         continue;
629                 }
630                 if ( inquote ) {
631                         if ( *s == '"' ) {
632                                 inquote = 0;
633                         }
634                 } else {
635                         if ( *s == '"' ) {
636                                 inquote = 1;
637                         } else if ( DN_SEPARATOR( *s ) ) {
638                                 break;
639                         }
640                 }
641         }
642
643         return( s - dn_in );
644 }
645
646 char * dn_rdn(
647         Backend *be,
648         const char      *dn_in )
649 {
650         char *rdn;
651         int i = dn_rdnlen( be, dn_in );
652
653         rdn = ch_malloc( i + 1 );
654         strncpy(rdn, dn_in, i);
655         rdn[i] = '\0';
656         return rdn;
657 }
658
659 /*
660  * return a charray of all subtrees to which the DN resides in
661  */
662 char **dn_subtree(
663         Backend *be,
664         const char      *dn )
665 {
666         char **subtree = NULL;
667         
668         do {
669                 charray_add( &subtree, dn );
670
671                 dn = dn_parent( be, dn );
672
673         } while ( dn != NULL );
674
675         return subtree;
676 }
677
678 /*
679  * dn_issuffix - tells whether suffix is a suffix of dn.
680  * Both dn and suffix must be normalized.
681  *      deprecated in favor of dnIsSuffix()
682  */
683 int
684 dn_issuffix(
685         const char      *dn,
686         const char      *suffix
687 )
688 {
689         struct berval   bvdn, bvsuffix;
690
691         assert( dn );
692         assert( suffix );
693
694         bvdn.bv_val = (char *) dn;
695         bvdn.bv_len = strlen( dn );
696         bvsuffix.bv_val = (char *) suffix;
697         bvsuffix.bv_len = strlen( suffix );
698
699         return dnIsSuffix( &bvdn, &bvsuffix );
700 }
701
702 /*
703  * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
704  * build_new_dn().
705  *
706  * Copyright 1999, Juan C. Gomez, All rights reserved.
707  * This software is not subject to any license of Silicon Graphics
708  * Inc. or Purdue University.
709  *
710  * Redistribution and use in source and binary forms are permitted
711  * without restriction or fee of any kind as long as this notice
712  * is preserved.
713  *
714  */
715
716 /* get_next_substring:
717  *
718  * Gets next substring in s, using d (or the end of the string '\0') as a
719  * string delimiter, and places it in a duplicated memory space. Leading
720  * spaces are ignored. String s **must** be null-terminated.
721  */
722
723 static char *
724 get_next_substring( const char * s, char d )
725 {
726
727         char    *str, *r;
728
729         r = str = ch_malloc( strlen(s) + 1 );
730
731         /* Skip leading spaces */
732         
733         while ( *s && ASCII_SPACE(*s) ) {
734                 s++;
735         }
736         
737         /* Copy word */
738
739         while ( *s && (*s != d) ) {
740
741                 /* Don't stop when you see trailing spaces may be a multi-word
742                 * string, i.e. name=John Doe!
743                 */
744
745                 *str++ = *s++;
746         }
747         
748         *str = '\0';
749         
750         return r;
751         
752 }
753
754
755 /* rdn_attr_type:
756  *
757  * Given a string (i.e. an rdn) of the form:
758  *       "attribute_type = attribute_value"
759  * this function returns the type of an attribute, that is the
760  * string "attribute_type" which is placed in newly allocated
761  * memory. The returned string will be null-terminated.
762  */
763
764 char * rdn_attr_type( const char * s )
765 {
766         return get_next_substring( s, '=' );
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
779 char *
780 rdn_attr_value( const char * rdn )
781 {
782
783         const char      *str;
784
785         if ( (str = strchr( rdn, '=' )) != NULL ) {
786                 return get_next_substring(++str, '\0');
787         }
788
789         return NULL;
790
791 }
792
793
794 /* rdn_attrs:
795  *
796  * Given a string (i.e. an rdn) of the form:
797  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
798  * this function stores the types of the attributes in ptypes, that is the
799  * array of strings "attribute_type" which is placed in newly allocated
800  * memory, and the values of the attributes in pvalues, that is the
801  * array of strings "attribute_value" which is placed in newly allocated
802  * memory. Returns 0 on success, -1 on failure.
803  *
804  * note: got part of the code from dn_validate
805  */
806
807 int
808 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
809 {
810         char **parts, **p;
811
812         *ptypes = NULL;
813         *pvalues = NULL;
814
815         /*
816          * explode the rdn in parts
817          */
818         parts = ldap_explode_rdn( rdn_in, 0 );
819
820         if ( parts == NULL ) {
821                 return( -1 );
822         }
823
824         for ( p = parts; p[0]; p++ ) {
825                 char *s, *e, *d;
826                 
827                 /* split each rdn part in type value */
828                 s = strchr( p[0], '=' );
829                 if ( s == NULL ) {
830                         charray_free( *ptypes );
831                         charray_free( *pvalues );
832                         charray_free( parts );
833                         return( -1 );
834                 }
835                 
836                 /* type should be fine */
837                 charray_add_n( ptypes, p[0], ( s-p[0] ) );
838
839                 /* value needs to be unescaped
840                  * (maybe this should be moved to ldap_explode_rdn?) */
841                 for ( e = d = s + 1; e[0]; e++ ) {
842                         if ( *e != '\\' ) {
843                                 *d++ = *e;
844                         }
845                 }
846                 d[0] = '\0';
847                 charray_add( pvalues, s + 1 );
848         }
849
850         /* free array */
851         charray_free( parts );
852
853         return( 0 );
854 }
855
856
857 /* rdn_validate:
858  *
859  * 1 if rdn is a legal rdn;
860  * 0 otherwise (including a sequence of rdns)
861  *
862  * note: got it from dn_rdn; it should be rewritten
863  * according to dn_validate
864  */
865 int
866 rdn_validate( const char * rdn )
867 {
868         int     inquote;
869
870         if ( rdn == NULL ) {
871                 return( 0 );
872         }
873
874         if ( strchr( rdn, '=' ) == NULL ) {
875                 return( 0 );
876         }
877
878         while ( *rdn && ASCII_SPACE( *rdn ) ) {
879                 rdn++;
880         }
881
882         if( *rdn == '\0' ) {
883                 return( 0 );
884         }
885
886         inquote = 0;
887
888         for ( ; *rdn; rdn++ ) {
889                 if ( *rdn == '\\' ) {
890                         if ( *(rdn + 1) ) {
891                                 rdn++;
892                         }
893                         continue;
894                 }
895                 if ( inquote ) {
896                         if ( *rdn == '"' ) {
897                                 inquote = 0;
898                         }
899                 } else {
900                         if ( *rdn == '"' ) {
901                                 inquote = 1;
902                         } else if ( DN_SEPARATOR( *rdn ) ) {
903                                 return( 0 );
904                         }
905                 }
906         }
907
908         return( 1 );
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(s) + rdn (newrdn) + null.
918  */
919
920 void
921 build_new_dn( char ** new_dn,
922         const char *e_dn,
923         const char * p_dn,
924         const char * newrdn )
925 {
926
927         if ( p_dn == NULL ) {
928                 *new_dn = ch_strdup( newrdn );
929                 return;
930         }
931
932         *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
933
934         strcpy( *new_dn, newrdn );
935         strcat( *new_dn, "," );
936         strcat( *new_dn, p_dn );
937 }
938
939 #endif /* SLAP_DN_MIGRATION */
940
941 /*
942  * dnIsSuffix - tells whether suffix is a suffix of dn.
943  * Both dn and suffix must be normalized.
944  */
945 int
946 dnIsSuffix(
947         const struct berval *dn,
948         const struct berval *suffix )
949 {
950         int     d = dn->bv_len - suffix->bv_len;
951
952         assert( dn );
953         assert( suffix );
954
955         /* empty suffix matches any dn */
956         if ( suffix->bv_len == 0 ) {
957                 return 1;
958         }
959
960         /* suffix longer than dn */
961         if ( d < 0 ) {
962                 return 0;
963         }
964
965         /* no rdn separator or escaped rdn separator */
966         if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) 
967                                 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
968                 return 0;
969         }
970
971         /* no possible match or malformed dn */
972         if ( d == 1 ) {
973                 return 0;
974         }
975
976         /* compare */
977         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
978 }