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