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