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