]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
f5745da168f09f79966421392a134b93d8cac4b4
[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                         free( ava->la_attr.bv_val );
80                         ber_dupbv( &ava->la_attr, &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 = { 0, 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                         free( ava->la_attr.bv_val );
254                         ber_dupbv( &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                         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         assert( DN_SEPARATOR( p[ 0 ] ) );
655         p++;
656
657         while ( ASCII_SPACE( p[ 0 ] ) ) {
658                 p++;
659         }
660
661         *pdn = p;
662
663         return LDAP_SUCCESS;
664 }
665
666 /*
667  * dn_parent - return the dn's parent, in-place
668  * FIXME: should be replaced by dnParent()
669  */
670 char *
671 dn_parent(
672         Backend         *be,
673         const char      *dn )
674 {
675         const char      *pdn;
676
677         if ( dn == NULL ) {
678                 return NULL;
679         }
680
681         while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
682                 dn++;
683         }
684
685         if ( dn[ 0 ] == '\0' ) {
686                 return NULL;
687         }
688
689         if ( be != NULL && be_issuffix( be, dn ) ) {
690                 return NULL;
691         }
692
693         if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
694                 return NULL;
695         }
696         
697         return ( char * )pdn;
698 }
699
700 int
701 dnExtractRdn( 
702         struct berval   *dn, 
703         struct berval   *rdn )
704 {
705         LDAPRDN         *tmpRDN;
706         const char      *p;
707         int             rc;
708
709         assert( dn );
710         assert( rdn );
711
712         if( dn->bv_len == 0 ) {
713                 return LDAP_OTHER;
714         }
715
716         rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
717         if ( rc != LDAP_SUCCESS ) {
718                 return rc;
719         }
720
721         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
722         ldap_rdnfree( tmpRDN );
723         if ( rc != LDAP_SUCCESS ) {
724                 return rc;
725         }
726
727         return LDAP_SUCCESS;
728 }
729
730 /*
731  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
732  */
733 int 
734 dn_rdnlen(
735         Backend         *be,
736         struct berval   *dn_in )
737 {
738         int             rc;
739         const char      *p;
740
741         assert( dn_in );
742
743         if ( dn_in == NULL ) {
744                 return 0;
745         }
746
747         if ( !dn_in->bv_len ) {
748                 return 0;
749         }
750
751         if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
752                 return 0;
753         }
754
755         rc = ldap_str2rdn( dn_in->bv_val, NULL, &p, 
756                         LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
757         if ( rc != LDAP_SUCCESS ) {
758                 return 0;
759         }
760
761         return p - dn_in->bv_val;
762 }
763
764 /*
765  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
766  */
767 char * dn_rdn(
768         Backend *be,
769         struct berval   *dn_in )
770 {
771         struct berval   rdn;
772
773         assert( dn_in );
774
775         if ( dn_in == NULL ) {
776                 return NULL;
777         }
778
779         if ( !dn_in->bv_len ) {
780                 return NULL;
781         }
782
783         if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
784                 return NULL;
785         }
786
787         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
788                 return NULL;
789         }
790
791         return rdn.bv_val;
792 }
793
794 /*
795  * dn_issuffix - tells whether suffix is a suffix of dn.
796  * Both dn and suffix must be normalized.
797  *      deprecated in favor of dnIsSuffix()
798  */
799 int
800 dn_issuffix(
801         const char      *dn,
802         const char      *suffix
803 )
804 {
805         struct berval   bvdn, bvsuffix;
806
807         assert( dn );
808         assert( suffix );
809
810         bvdn.bv_val = (char *) dn;
811         bvdn.bv_len = strlen( dn );
812         bvsuffix.bv_val = (char *) suffix;
813         bvsuffix.bv_len = strlen( suffix );
814
815         return dnIsSuffix( &bvdn, &bvsuffix );
816 }
817
818 /* rdn_attr_type:
819  *
820  * Given a string (i.e. an rdn) of the form:
821  *       "attribute_type = attribute_value"
822  * this function returns the type of an attribute, that is the
823  * string "attribute_type" which is placed in newly allocated
824  * memory. The returned string will be null-terminated.
825  *
826  * Deprecated
827  */
828
829 char * rdn_attr_type( const char * s )
830 {
831         char    **attrs = NULL, **values = NULL, *retval;
832
833         if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
834                 return NULL;
835         }
836
837         retval = ch_strdup( attrs[ 0 ] );
838
839         charray_free( attrs );
840         charray_free( values );
841
842         return retval;
843 }
844
845
846 /* rdn_attr_value:
847  *
848  * Given a string (i.e. an rdn) of the form:
849  *       "attribute_type = attribute_value"
850  * this function returns "attribute_type" which is placed in newly allocated
851  * memory. The returned string will be null-terminated and may contain
852  * spaces (i.e. "John Doe\0").
853  *
854  * Deprecated
855  */
856
857 char *
858 rdn_attr_value( const char * rdn )
859 {
860         char    **values = NULL, *retval;
861
862         if ( rdn_attrs( rdn, NULL, &values ) != LDAP_SUCCESS ) {
863                 return NULL;
864         }
865
866         retval = ch_strdup( values[ 0 ] );
867
868         charray_free( values );
869
870         return retval;
871 }
872
873
874 /* rdn_attrs:
875  *
876  * Given a string (i.e. an rdn) of the form:
877  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
878  * this function stores the types of the attributes in ptypes, that is the
879  * array of strings "attribute_type" which is placed in newly allocated
880  * memory, and the values of the attributes in pvalues, that is the
881  * array of strings "attribute_value" which is placed in newly allocated
882  * memory. Returns 0 on success, -1 on failure.
883  *
884  * note: got part of the code from dn_validate
885  *
886  * Deprecated; directly use LDAPRDN from ldap_str2rdn
887  */
888 int
889 rdn_attrs( const char * rdn, char ***types, char ***values)
890 {
891         LDAPRDN         *tmpRDN;
892         const char      *p;
893         int             iAVA;
894         int             rc;
895         
896         assert( rdn );
897         assert( values );
898         assert( *values == NULL );
899         assert( types == NULL || *types == NULL );
900
901         rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
902         if ( rc != LDAP_SUCCESS ) {
903                 return rc;
904         }
905
906 #if 0
907         /*
908          * FIXME: should we complain if the rdn is actually a dn?
909          */
910         if ( p[ 0 ] != '\0' ) {
911                 ldap_rdnfree( tmpRDN );
912                 return LDAP_INVALID_DN_SYNTAX;
913         }
914 #endif
915
916         for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
917                 LDAPAVA         *ava = tmpRDN[ iAVA ][ 0 ];
918
919                 assert( ava );
920                 assert( ava->la_attr.bv_val );
921                 assert( ava->la_value.bv_val );
922
923                 if ( types ) {
924                         charray_add_n( types, ava->la_attr.bv_val, 
925                                         ava->la_attr.bv_len );
926                 }
927                 charray_add_n( values, ava->la_value.bv_val, 
928                                 ava->la_value.bv_len );
929         }
930
931         ldap_rdnfree( tmpRDN );
932
933         return LDAP_SUCCESS;
934 }
935
936
937 /* rdnValidate:
938  *
939  * LDAP_SUCCESS if rdn is a legal rdn;
940  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
941  */
942 int
943 rdnValidate( struct berval *rdn )
944 {
945 #if 1
946         /* Major cheat!
947          * input is a pretty or normalized DN
948          * hence, we can just search for ','
949          */
950         if( rdn == NULL || rdn->bv_len == 0 ) {
951                 return LDAP_INVALID_SYNTAX;
952         }
953
954         return strchr( rdn->bv_val, ',' ) == NULL
955                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
956
957 #else
958         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
959         const char      *p;
960         int             rc;
961
962         /*
963          * must be non-empty
964          */
965         if ( rdn == NULL || rdn == '\0' ) {
966                 return 0;
967         }
968
969         /*
970          * must be parsable
971          */
972         rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
973         if ( rc != LDAP_SUCCESS ) {
974                 return 0;
975         }
976
977         /*
978          * Must be one-level
979          */
980         if ( p[ 0 ] != '\0' ) {
981                 return 0;
982         }
983
984         /*
985          * Schema-aware validate
986          */
987         if ( rc == LDAP_SUCCESS ) {
988                 rc = LDAPDN_validate( DN );
989         }
990         ldap_rdnfree( RDN );
991
992         /*
993          * Must validate (there's a repeated parsing ...)
994          */
995         return ( rc == LDAP_SUCCESS );
996 #endif
997 }
998
999
1000 /* build_new_dn:
1001  *
1002  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1003  * renamed.
1004  *
1005  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1006  */
1007
1008 void
1009 build_new_dn( struct berval * new_dn,
1010         struct berval * parent_dn,
1011         struct berval * newrdn )
1012 {
1013         char *ptr;
1014
1015         if ( parent_dn == NULL ) {
1016                 ber_dupbv( new_dn, newrdn );
1017                 return;
1018         }
1019
1020         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1021         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
1022
1023         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
1024         *ptr++ = ',';
1025         strcpy( ptr, parent_dn->bv_val );
1026 }
1027
1028 #endif /* SLAP_DN_MIGRATION */
1029
1030 /*
1031  * dnIsSuffix - tells whether suffix is a suffix of dn.
1032  * Both dn and suffix must be normalized.
1033  */
1034 int
1035 dnIsSuffix(
1036         const struct berval *dn,
1037         const struct berval *suffix )
1038 {
1039         int     d = dn->bv_len - suffix->bv_len;
1040
1041         assert( dn );
1042         assert( suffix );
1043
1044         /* empty suffix matches any dn */
1045         if ( suffix->bv_len == 0 ) {
1046                 return 1;
1047         }
1048
1049         /* suffix longer than dn */
1050         if ( d < 0 ) {
1051                 return 0;
1052         }
1053
1054         /* no rdn separator or escaped rdn separator */
1055         if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) 
1056                                 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1057                 return 0;
1058         }
1059
1060         /* no possible match or malformed dn */
1061         if ( d == 1 ) {
1062                 return 0;
1063         }
1064
1065         /* compare */
1066         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1067 }