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