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