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