]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
d1eca3f2639f8b8f701c879bc0cfbe3d69f5a06e
[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                 ldap_dnfree( dn );
126         }
127         
128         if ( rc != LDAP_SUCCESS ) {
129                 return( LDAP_INVALID_SYNTAX );
130         }
131
132         return( LDAP_SUCCESS );
133 }
134
135 /*
136  * AVA sorting inside a RDN
137  *
138  * rule: sort attributeTypes in alphabetical order; in case of multiple
139  * occurrences of the same attributeType, sort values in byte order
140  * (use memcmp, which implies alphabetical order in case of IA5 value;
141  * this should guarantee the repeatability of the operation).
142  *
143  * uses a linear search; should be fine since the number of AVAs in
144  * a RDN should be limited.
145  */
146 static void
147 AVA_Sort( LDAPRDN *rdn, int iAVA )
148 {
149         int             i;
150         LDAPAVA         *ava_in = rdn[ iAVA ][ 0 ];
151
152         assert( rdn );
153         assert( ava_in );
154         
155         for ( i = 0; i < iAVA; i++ ) {
156                 LDAPAVA         *ava = rdn[ i ][ 0 ];
157                 int             a, j;
158
159                 assert( ava );
160
161                 a = strcmp( ava_in->la_attr->bv_val, ava->la_attr->bv_val );
162
163                 if ( a > 0 ) {
164                         break;
165                 }
166
167                 while ( a == 0 ) {
168                         int             v, d;
169
170                         d = ava_in->la_value->bv_len - ava->la_value->bv_len;
171
172                         v = memcmp( ava_in->la_value->bv_val, 
173                                         ava->la_value->bv_val,
174                                         d <= 0 ? ava_in->la_value->bv_len 
175                                                 : ava->la_value->bv_len );
176
177                         if ( v == 0 && d != 0 ) {
178                                 v = d;
179                         }
180
181                         if ( v <= 0 ) {
182                                 /* 
183                                  * got it!
184                                  */
185                                 break;
186                         }
187
188                         if ( ++i == iAVA ) {
189                                 /*
190                                  * already sorted
191                                  */
192                                 return;
193                         }
194
195                         ava = rdn[ i ][ 0 ];
196                         a = strcmp( ava_in->la_value->bv_val, 
197                                         ava->la_value->bv_val );
198                 }
199
200                 /*
201                  * move ahead
202                  */
203                 for ( j = iAVA; j > i; j-- ) {
204                         rdn[ j ][ 0 ] = rdn[ j - 1 ][ 0 ];
205                 }
206                 rdn[ i ][ 0 ] = ava_in;
207
208                 return;
209         }
210 }
211
212 /*
213  * In-place, schema-aware normalization / "pretty"ing of the
214  * structural representation of a distinguished name.
215  */
216 static int
217 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
218 {
219         int             iRDN;
220         int             rc;
221
222         assert( dn );
223
224         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
225                 LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
226                 int             iAVA;
227
228                 assert( rdn );
229
230                 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
231                         LDAPAVA                 *ava = rdn[ iAVA ][ 0 ];
232                         AttributeDescription    *ad;
233                         slap_syntax_transform_func *transf = NULL;
234                         MatchingRule *mr;
235                         struct berval           *bv = NULL;
236
237                         assert( ava );
238
239                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
240                                 const char      *text = NULL;
241
242                                 rc = slap_bv2ad( ava->la_attr, &ad, &text );
243                                 if ( rc != LDAP_SUCCESS ) {
244                                         return LDAP_INVALID_SYNTAX;
245                                 }
246                                 
247                                 ava->la_private = ( void * )ad;
248                         }
249
250                         /* 
251                          * Replace attr oid/name with the canonical name
252                          */
253                         ber_bvfree( ava->la_attr );
254                         ava->la_attr = ber_bvdup( &ad->ad_cname );
255
256                         if( flags & SLAP_LDAPDN_PRETTY ) {
257                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
258                                 mr = NULL;
259                         } else {
260                                 transf = ad->ad_type->sat_syntax->ssyn_normalize;
261                                 mr = ad->ad_type->sat_equality;
262                         }
263
264                         if ( transf ) {
265                                 /*
266                                  * transform value by normalize/pretty function
267                                  */
268                                 rc = ( *transf )( ad->ad_type->sat_syntax,
269                                         ava->la_value, &bv );
270                         
271                                 if ( rc != LDAP_SUCCESS ) {
272                                         return LDAP_INVALID_SYNTAX;
273                                 }
274                         }
275
276                         if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
277                                 struct berval *s = bv;
278
279                                 bv = ber_bvstr( UTF8normalize( bv ? bv : ava->la_value, 
280                                         UTF8_CASEFOLD ) );
281
282                                 ber_bvfree( s );
283                         }
284
285                         if( bv ) {
286                                 ber_bvfree( ava->la_value );
287                                 ava->la_value = bv;
288                         }
289
290                         AVA_Sort( rdn, iAVA );
291                 }
292         }
293
294         return LDAP_SUCCESS;
295 }
296
297 /*
298  * dn normalize routine
299  */
300 int
301 dnNormalize(
302         Syntax *syntax,
303         struct berval *val,
304         struct berval **normalized )
305 {
306         struct berval *out = NULL;
307
308         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
309
310         assert( val );
311         assert( normalized );
312
313         assert( *normalized == NULL );
314
315         if ( val->bv_len != 0 ) {
316                 LDAPDN          *dn = 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                 out = ch_malloc( sizeof(struct berval));
339
340                 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
341
342                 ldap_dnfree( dn );
343
344                 if ( rc != LDAP_SUCCESS ) {
345                         free( out );
346                         return LDAP_INVALID_SYNTAX;
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         return LDAP_SUCCESS;
356 }
357
358 /*
359  * dn "pretty"ing routine
360  */
361 int
362 dnPretty(
363         Syntax *syntax,
364         struct berval *val,
365         struct berval **pretty)
366 {
367         struct berval *out = NULL;
368
369         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
370
371         assert( val );
372         assert( pretty );
373         assert( *pretty == NULL );
374
375         if ( val->bv_len != 0 ) {
376                 LDAPDN          *dn = NULL;
377                 int             rc;
378
379                 /* FIXME: should be liberal in what we accept */
380                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
381                 if ( rc != LDAP_SUCCESS ) {
382                         return LDAP_INVALID_SYNTAX;
383                 }
384
385                 /*
386                  * Schema-aware rewrite
387                  */
388                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
389                         ldap_dnfree( dn );
390                         return LDAP_INVALID_SYNTAX;
391                 }
392
393                 /* FIXME: not sure why the default isn't pretty */
394                 /* RE: the default is the form that is used as
395                  * an internal representation; the pretty form
396                  * is a variant */
397
398                 out = ch_malloc( sizeof(struct berval));
399
400                 rc = ldap_dn2bv( dn, out,
401                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
402
403                 ldap_dnfree( dn );
404
405                 if ( rc != LDAP_SUCCESS ) {
406                         free( out );
407                         return LDAP_INVALID_SYNTAX;
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  * Combination of both dnPretty and dnNormalize
422  */
423 int
424 dnPrettyNormal(
425         Syntax *syntax,
426         struct berval *val,
427         struct berval *pretty,
428         struct berval *normal)
429 {
430         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
431
432         assert( val );
433         assert( pretty );
434         assert( normal );
435
436         if ( val->bv_len != 0 ) {
437                 LDAPDN          *dn = NULL;
438                 int             rc;
439
440                 pretty->bv_val = NULL;
441                 normal->bv_val = NULL;
442                 pretty->bv_len = 0;
443                 normal->bv_len = 0;
444
445                 /* FIXME: should be liberal in what we accept */
446                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
447                 if ( rc != LDAP_SUCCESS ) {
448                         return LDAP_INVALID_SYNTAX;
449                 }
450
451                 /*
452                  * Schema-aware rewrite
453                  */
454                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
455                         ldap_dnfree( dn );
456                         return LDAP_INVALID_SYNTAX;
457                 }
458
459                 rc = ldap_dn2bv( dn, pretty,
460                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
461
462                 if ( rc != LDAP_SUCCESS ) {
463                         ldap_dnfree( dn );
464                         return LDAP_INVALID_SYNTAX;
465                 }
466
467                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
468                         ldap_dnfree( dn );
469                         free( pretty->bv_val );
470                         pretty->bv_val = NULL;
471                         pretty->bv_len = 0;
472                         return LDAP_INVALID_SYNTAX;
473                 }
474
475                 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
476
477                 ldap_dnfree( dn );
478                 if ( rc != LDAP_SUCCESS ) {
479                         free( pretty->bv_val );
480                         pretty->bv_val = NULL;
481                         pretty->bv_len = 0;
482                         return LDAP_INVALID_SYNTAX;
483                 }
484         } else {
485                 ber_dupbv( pretty, val );
486                 ber_dupbv( normal, val );
487         }
488
489         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
490                 pretty->bv_val, normal->bv_val, 0 );
491
492         return LDAP_SUCCESS;
493 }
494
495 /*
496  * dnMatch routine
497  */
498 int
499 dnMatch(
500         int *matchp,
501         slap_mask_t flags,
502         Syntax *syntax,
503         MatchingRule *mr,
504         struct berval *value,
505         void *assertedValue )
506 {
507         int match;
508         struct berval *asserted = (struct berval *) assertedValue;
509
510         assert( matchp );
511         assert( value );
512         assert( assertedValue );
513         
514         match = value->bv_len - asserted->bv_len;
515
516         if ( match == 0 ) {
517                 match = strcmp( value->bv_val, asserted->bv_val );
518         }
519
520 #ifdef NEW_LOGGING
521         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
522                 "dnMatch: %d\n    %s\n    %s\n", match,
523                 value->bv_val, asserted->bv_val ));
524 #else
525         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
526                 match, value->bv_val, asserted->bv_val );
527 #endif
528
529         *matchp = match;
530         return( LDAP_SUCCESS );
531 }
532
533 #ifdef SLAP_DN_MIGRATION
534 /*
535  * these routines are provided for migration purposes only!
536  *      dn_validate is deprecated in favor of dnValidate
537  *      dn_normalize is deprecated in favor of dnNormalize
538  *      strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
539  *
540  * other routines are likewise deprecated but may not yet have
541  * replacement functions.
542  */
543
544 /*
545  * dn_validate - validate and compress dn.  the dn is
546  * compressed in place are returned if valid.
547  * Deprecated in favor of dnValidate()
548  */
549 char *
550 dn_validate( char *dn )
551 {
552         struct berval val;
553         struct berval *pretty = NULL;
554         int             rc;
555
556         if ( dn == NULL || dn[0] == '\0' ) {
557                 return dn;
558         }
559
560         val.bv_val = dn;
561         val.bv_len = strlen( dn );
562
563         rc = dnPretty( NULL, &val, &pretty );
564         if ( rc != LDAP_SUCCESS ) {
565                 return NULL;
566         }
567
568         if ( val.bv_len < pretty->bv_len ) {
569                 ber_bvfree( pretty );
570                 return NULL;
571         }
572
573         AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
574         ber_bvfree( pretty );
575
576         return dn;
577 }
578
579 /*
580  * dn_normalize - put dn into a canonical form suitable for storing
581  * in a hash database.  this involves normalizing the case as well as
582  * the format.  the dn is normalized in place as well as returned if valid.
583  * Deprecated in favor of dnNormalize()
584  */
585 char *
586 dn_normalize( char *dn )
587 {
588         struct berval val;
589         struct berval *normalized = NULL;
590         int             rc;
591
592         if ( dn == NULL || dn[0] == '\0' ) {
593                 return dn;
594         }
595
596         val.bv_val = dn;
597         val.bv_len = strlen( dn );
598
599         rc = dnNormalize( NULL, &val, &normalized );
600         if ( rc != LDAP_SUCCESS ) {
601                 return NULL;
602         }
603
604         if ( val.bv_len < normalized->bv_len ) {
605                 ber_bvfree( normalized );
606                 return NULL;
607         }
608
609         AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
610         ber_bvfree( normalized );
611
612         return dn;
613 }
614
615 /*
616  * dnParent - dn's parent, in-place
617  */
618 int
619 dnParent( 
620         const char      *dn, 
621         const char      **pdn )
622 {
623         const char      *p;
624         int             rc;
625
626         rc = ldap_str2rdn( dn, NULL, &p, LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
627         if ( rc != LDAP_SUCCESS ) {
628                 return rc;
629         }
630
631         assert( DN_SEPARATOR( p[ 0 ] ) );
632         p++;
633
634         while ( ASCII_SPACE( p[ 0 ] ) ) {
635                 p++;
636         }
637
638         *pdn = p;
639
640         return LDAP_SUCCESS;
641 }
642
643 /*
644  * dn_parent - return the dn's parent, in-place
645  * FIXME: should be replaced by dnParent()
646  */
647 char *
648 dn_parent(
649         Backend         *be,
650         const char      *dn )
651 {
652         const char      *pdn;
653
654         if ( dn == NULL ) {
655                 return NULL;
656         }
657
658         while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
659                 dn++;
660         }
661
662         if ( dn[ 0 ] == '\0' ) {
663                 return NULL;
664         }
665
666         if ( be != NULL && be_issuffix( be, dn ) ) {
667                 return NULL;
668         }
669
670         if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
671                 return NULL;
672         }
673         
674         return ( char * )pdn;
675 }
676
677 int
678 dnExtractRdn( 
679         struct berval   *dn, 
680         struct berval   *rdn )
681 {
682         LDAPRDN         *tmpRDN;
683         const char      *p;
684         int             rc;
685
686         assert( dn );
687         assert( rdn );
688
689         if( dn->bv_len == 0 ) {
690                 return LDAP_OTHER;
691         }
692
693         rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
694         if ( rc != LDAP_SUCCESS ) {
695                 return rc;
696         }
697
698         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
699         ldap_rdnfree( tmpRDN );
700         if ( rc != LDAP_SUCCESS ) {
701                 return rc;
702         }
703
704         return LDAP_SUCCESS;
705 }
706
707 /*
708  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
709  */
710 int 
711 dn_rdnlen(
712         Backend         *be,
713         struct berval   *dn_in )
714 {
715         int             rc;
716         const char      *p;
717
718         assert( dn_in );
719
720         if ( dn_in == NULL ) {
721                 return 0;
722         }
723
724         if ( !dn_in->bv_len ) {
725                 return 0;
726         }
727
728         if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
729                 return 0;
730         }
731
732         rc = ldap_str2rdn( dn_in->bv_val, NULL, &p, 
733                         LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
734         if ( rc != LDAP_SUCCESS ) {
735                 return 0;
736         }
737
738         return p - dn_in->bv_val;
739 }
740
741 /*
742  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
743  */
744 char * dn_rdn(
745         Backend *be,
746         struct berval   *dn_in )
747 {
748         struct berval   rdn;
749
750         assert( dn_in );
751
752         if ( dn_in == NULL ) {
753                 return NULL;
754         }
755
756         if ( !dn_in->bv_len ) {
757                 return NULL;
758         }
759
760         if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
761                 return NULL;
762         }
763
764         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
765                 return NULL;
766         }
767
768         return rdn.bv_val;
769 }
770
771 /*
772  * dn_issuffix - tells whether suffix is a suffix of dn.
773  * Both dn and suffix must be normalized.
774  *      deprecated in favor of dnIsSuffix()
775  */
776 int
777 dn_issuffix(
778         const char      *dn,
779         const char      *suffix
780 )
781 {
782         struct berval   bvdn, bvsuffix;
783
784         assert( dn );
785         assert( suffix );
786
787         bvdn.bv_val = (char *) dn;
788         bvdn.bv_len = strlen( dn );
789         bvsuffix.bv_val = (char *) suffix;
790         bvsuffix.bv_len = strlen( suffix );
791
792         return dnIsSuffix( &bvdn, &bvsuffix );
793 }
794
795 /* rdn_attr_type:
796  *
797  * Given a string (i.e. an rdn) of the form:
798  *       "attribute_type = attribute_value"
799  * this function returns the type of an attribute, that is the
800  * string "attribute_type" which is placed in newly allocated
801  * memory. The returned string will be null-terminated.
802  *
803  * Deprecated
804  */
805
806 char * rdn_attr_type( const char * s )
807 {
808         char    **attrs = NULL, **values = NULL, *retval;
809
810         if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
811                 return NULL;
812         }
813
814         retval = ch_strdup( attrs[ 0 ] );
815
816         charray_free( attrs );
817         charray_free( values );
818
819         return retval;
820 }
821
822
823 /* rdn_attr_value:
824  *
825  * Given a string (i.e. an rdn) of the form:
826  *       "attribute_type = attribute_value"
827  * this function returns "attribute_type" which is placed in newly allocated
828  * memory. The returned string will be null-terminated and may contain
829  * spaces (i.e. "John Doe\0").
830  *
831  * Deprecated
832  */
833
834 char *
835 rdn_attr_value( const char * rdn )
836 {
837         char    **values = NULL, *retval;
838
839         if ( rdn_attrs( rdn, NULL, &values ) != LDAP_SUCCESS ) {
840                 return NULL;
841         }
842
843         retval = ch_strdup( values[ 0 ] );
844
845         charray_free( values );
846
847         return retval;
848 }
849
850
851 /* rdn_attrs:
852  *
853  * Given a string (i.e. an rdn) of the form:
854  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
855  * this function stores the types of the attributes in ptypes, that is the
856  * array of strings "attribute_type" which is placed in newly allocated
857  * memory, and the values of the attributes in pvalues, that is the
858  * array of strings "attribute_value" which is placed in newly allocated
859  * memory. Returns 0 on success, -1 on failure.
860  *
861  * note: got part of the code from dn_validate
862  *
863  * Deprecated; directly use LDAPRDN from ldap_str2rdn
864  */
865 int
866 rdn_attrs( const char * rdn, char ***types, char ***values)
867 {
868         LDAPRDN         *tmpRDN;
869         const char      *p;
870         int             iAVA;
871         int             rc;
872         
873         assert( rdn );
874         assert( values );
875         assert( *values == NULL );
876         assert( types == NULL || *types == NULL );
877
878         rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
879         if ( rc != LDAP_SUCCESS ) {
880                 return rc;
881         }
882
883 #if 0
884         /*
885          * FIXME: should we complain if the rdn is actually a dn?
886          */
887         if ( p[ 0 ] != '\0' ) {
888                 ldap_rdnfree( tmpRDN );
889                 return LDAP_INVALID_DN_SYNTAX;
890         }
891 #endif
892
893         for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
894                 LDAPAVA         *ava = tmpRDN[ iAVA ][ 0 ];
895
896                 assert( ava );
897                 assert( ava->la_attr );
898                 assert( ava->la_value );
899
900                 if ( types ) {
901                         charray_add_n( types, ava->la_attr->bv_val, 
902                                         ava->la_attr->bv_len );
903                 }
904                 charray_add_n( values, ava->la_value->bv_val, 
905                                 ava->la_value->bv_len );
906         }
907
908         ldap_rdnfree( tmpRDN );
909
910         return LDAP_SUCCESS;
911 }
912
913
914 /* rdnValidate:
915  *
916  * LDAP_SUCCESS if rdn is a legal rdn;
917  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
918  */
919 int
920 rdnValidate( struct berval *rdn )
921 {
922 #if 1
923         /* Major cheat!
924          * input is a pretty or normalized DN
925          * hence, we can just search for ','
926          */
927         if( rdn == NULL || rdn->bv_len == 0 ) {
928                 return LDAP_INVALID_SYNTAX;
929         }
930
931         return strchr( rdn->bv_val, ',' ) == NULL
932                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
933
934 #else
935         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
936         const char      *p;
937         int             rc;
938
939         /*
940          * must be non-empty
941          */
942         if ( rdn == NULL || rdn == '\0' ) {
943                 return 0;
944         }
945
946         /*
947          * must be parsable
948          */
949         rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
950         if ( rc != LDAP_SUCCESS ) {
951                 return 0;
952         }
953
954         /*
955          * Must be one-level
956          */
957         if ( p[ 0 ] != '\0' ) {
958                 return 0;
959         }
960
961         /*
962          * Schema-aware validate
963          */
964         if ( rc == LDAP_SUCCESS ) {
965                 rc = LDAPDN_validate( DN );
966         }
967         ldap_rdnfree( RDN );
968
969         /*
970          * Must validate (there's a repeated parsing ...)
971          */
972         return ( rc == LDAP_SUCCESS );
973 #endif
974 }
975
976
977 /* build_new_dn:
978  *
979  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
980  * renamed.
981  *
982  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
983  */
984
985 void
986 build_new_dn( struct berval * new_dn,
987         struct berval * parent_dn,
988         struct berval * newrdn )
989 {
990         char *ptr;
991
992         if ( parent_dn == NULL ) {
993                 ber_dupbv( new_dn, newrdn );
994                 return;
995         }
996
997         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
998         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
999
1000         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
1001         *ptr++ = ',';
1002         strcpy( ptr, parent_dn->bv_val );
1003 }
1004
1005 #endif /* SLAP_DN_MIGRATION */
1006
1007 /*
1008  * dnIsSuffix - tells whether suffix is a suffix of dn.
1009  * Both dn and suffix must be normalized.
1010  */
1011 int
1012 dnIsSuffix(
1013         const struct berval *dn,
1014         const struct berval *suffix )
1015 {
1016         int     d = dn->bv_len - suffix->bv_len;
1017
1018         assert( dn );
1019         assert( suffix );
1020
1021         /* empty suffix matches any dn */
1022         if ( suffix->bv_len == 0 ) {
1023                 return 1;
1024         }
1025
1026         /* suffix longer than dn */
1027         if ( d < 0 ) {
1028                 return 0;
1029         }
1030
1031         /* no rdn separator or escaped rdn separator */
1032         if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) 
1033                                 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1034                 return 0;
1035         }
1036
1037         /* no possible match or malformed dn */
1038         if ( d == 1 ) {
1039                 return 0;
1040         }
1041
1042         /* compare */
1043         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1044 }