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