]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
38ecca3af38ce367eda11344b337473cb34c7fb9
[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
358         return LDAP_SUCCESS;
359 }
360
361 /*
362  * dn "pretty"ing routine
363  */
364 int
365 dnPretty(
366         Syntax *syntax,
367         struct berval *val,
368         struct berval **pretty)
369 {
370         struct berval *out = NULL;
371
372         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
373
374         assert( val );
375         assert( pretty );
376         assert( *pretty == NULL );
377
378         if ( val->bv_len != 0 ) {
379                 LDAPDN          *dn = NULL;
380                 char            *dn_out = NULL;
381                 int             rc;
382
383                 /* FIXME: should be liberal in what we accept */
384                 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
385                 if ( rc != LDAP_SUCCESS ) {
386                         return LDAP_INVALID_SYNTAX;
387                 }
388
389                 /*
390                  * Schema-aware rewrite
391                  */
392                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
393                         ldap_dnfree( dn );
394                         return LDAP_INVALID_SYNTAX;
395                 }
396
397                 /* FIXME: not sure why the default isn't pretty */
398                 /* RE: the default is the form that is used as
399                  * an internal representation; the pretty form
400                  * is a variant */
401                 rc = ldap_dn2str( dn, &dn_out,
402                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
403
404                 ldap_dnfree( dn );
405
406                 if ( rc != LDAP_SUCCESS ) {
407                         return LDAP_INVALID_SYNTAX;
408                 }
409
410                 out = ber_bvstr( dn_out );
411
412         } else {
413                 out = ber_bvdup( val );
414         }
415
416         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
417
418         *pretty = out;
419
420         return LDAP_SUCCESS;
421 }
422
423 /*
424  * dnMatch routine
425  *
426  * note: uses exact string match (strcmp) because it is supposed to work
427  * on normalized DNs.
428  */
429 int
430 dnMatch(
431         int *matchp,
432         slap_mask_t flags,
433         Syntax *syntax,
434         MatchingRule *mr,
435         struct berval *value,
436         void *assertedValue )
437 {
438         int match;
439         struct berval *asserted = (struct berval *) assertedValue;
440
441         assert( matchp );
442         assert( value );
443         assert( assertedValue );
444         
445         match = value->bv_len - asserted->bv_len;
446
447         if ( match == 0 ) {
448                 match = strcmp( value->bv_val, asserted->bv_val );
449         }
450
451 #ifdef NEW_LOGGING
452         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
453                 "dnMatch: %d\n    %s\n    %s\n", match,
454                 value->bv_val, asserted->bv_val ));
455 #else
456         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
457                 match, value->bv_val, asserted->bv_val );
458 #endif
459
460         *matchp = match;
461         return( LDAP_SUCCESS );
462 }
463
464 #ifdef SLAP_DN_MIGRATION
465 /*
466  * these routines are provided for migration purposes only!
467  *      dn_validate is deprecated in favor of dnValidate
468  *      dn_normalize is deprecated in favor of dnNormalize
469  *      strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
470  *
471  * other routines are likewise deprecated but may not yet have
472  * replacement functions.
473  */
474
475 /*
476  * dn_validate - validate and compress dn.  the dn is
477  * compressed in place are returned if valid.
478  * Deprecated in favor of dnValidate()
479  */
480 char *
481 dn_validate( char *dn )
482 {
483         struct berval val;
484         struct berval *pretty = NULL;
485         int             rc;
486
487         if ( dn == NULL || dn[0] == '\0' ) {
488                 return dn;
489         }
490
491         val.bv_val = dn;
492         val.bv_len = strlen( dn );
493
494         rc = dnPretty( NULL, &val, &pretty );
495         if ( rc != LDAP_SUCCESS ) {
496                 return NULL;
497         }
498
499         if ( val.bv_len < pretty->bv_len ) {
500                 ber_bvfree( pretty );
501                 return NULL;
502         }
503
504         AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
505         ber_bvfree( pretty );
506
507         return dn;
508 }
509
510 /*
511  * dn_normalize - put dn into a canonical form suitable for storing
512  * in a hash database.  this involves normalizing the case as well as
513  * the format.  the dn is normalized in place as well as returned if valid.
514  * Deprecated in favor of dnNormalize()
515  */
516 char *
517 dn_normalize( char *dn )
518 {
519         struct berval val;
520         struct berval *normalized = NULL;
521         int             rc;
522
523         if ( dn == NULL || dn[0] == '\0' ) {
524                 return dn;
525         }
526
527         val.bv_val = dn;
528         val.bv_len = strlen( dn );
529
530         rc = dnNormalize( NULL, &val, &normalized );
531         if ( rc != LDAP_SUCCESS ) {
532                 return NULL;
533         }
534
535         if ( val.bv_len < normalized->bv_len ) {
536                 ber_bvfree( normalized );
537                 return NULL;
538         }
539
540         AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
541         ber_bvfree( normalized );
542
543         return dn;
544 }
545
546 /*
547  * dnParent - dn's parent, in-place
548  */
549 int
550 dnParent( 
551         const char      *dn, 
552         const char      **pdn )
553 {
554         LDAPRDN         *tmpRDN;
555         const char      *p;
556         int             rc;
557
558         rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
559         if ( rc != LDAP_SUCCESS ) {
560                 return rc;
561         }
562         ldap_rdnfree( tmpRDN );
563
564         assert( DN_SEPARATOR( p[ 0 ] ) );
565         p++;
566
567         while ( ASCII_SPACE( p[ 0 ] ) ) {
568                 p++;
569         }
570
571         *pdn = p;
572
573         return LDAP_SUCCESS;
574 }
575
576 /*
577  * dn_parent - return the dn's parent, in-place
578  * FIXME: should be replaced by dnParent()
579  */
580 char *
581 dn_parent(
582         Backend         *be,
583         const char      *dn )
584 {
585 #if 0
586         const char      *s;
587         int     inquote;
588
589         if( dn == NULL ) {
590                 return NULL;
591         }
592
593         while(*dn != '\0' && ASCII_SPACE(*dn)) {
594                 dn++;
595         }
596
597         if( *dn == '\0' ) {
598                 return NULL;
599         }
600
601         if ( be != NULL && be_issuffix( be, dn ) ) {
602                 return NULL;
603         }
604
605         /*
606          * assume it is an X.500-style name, which looks like
607          * foo=bar,sha=baz,...
608          */
609
610         inquote = 0;
611         for ( s = dn; *s; s++ ) {
612                 if ( *s == '\\' ) {
613                         if ( *(s + 1) ) {
614                                 s++;
615                         }
616                         continue;
617                 }
618                 if ( inquote ) {
619                         if ( *s == '"' ) {
620                                 inquote = 0;
621                         }
622                 } else {
623                         if ( *s == '"' ) {
624                                 inquote = 1;
625                         } else if ( DN_SEPARATOR( *s ) ) {
626                                 return (char *)s + 1;
627                         }
628                 }
629         }
630
631         return "";
632 #endif
633         const char      *pdn;
634
635         if ( dn == NULL ) {
636                 return NULL;
637         }
638
639         while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
640                 dn++;
641         }
642
643         if ( dn[ 0 ] == '\0' ) {
644                 return NULL;
645         }
646
647         if ( be != NULL && be_issuffix( be, dn ) ) {
648                 return NULL;
649         }
650
651         if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
652                 return NULL;
653         }
654         
655         return ( char * )pdn;
656 }
657
658 int
659 dnExtractRdn( 
660         const char      *dn, 
661         struct berval   **rdn )
662 {
663         LDAPRDN         *tmpRDN;
664         const char      *p;
665         char            *rdnout;
666         int             rc;
667
668         assert( dn );
669         assert( rdn );
670
671         rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
672         if ( rc != LDAP_SUCCESS ) {
673                 return rc;
674         }
675
676         rc = ldap_rdn2str( tmpRDN, &rdnout, LDAP_DN_FORMAT_LDAPV3 );
677         ldap_rdnfree( tmpRDN );
678         if ( rc != LDAP_SUCCESS ) {
679                 return rc;
680         }
681
682         *rdn = ber_bvstr( rdnout );
683         if ( *rdn == NULL ) {
684                 free( rdnout );
685                 return LDAP_NO_MEMORY;
686         }
687
688         return LDAP_SUCCESS;
689 }
690
691 /*
692  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
693  */
694 int 
695 dn_rdnlen(
696         Backend         *be,
697         const char      *dn_in )
698 {
699 #if 0
700         char    *s;
701         int     inquote;
702
703         if( dn_in == NULL ) {
704                 return 0;
705         }
706
707         while(*dn_in && ASCII_SPACE(*dn_in)) {
708                 dn_in++;
709         }
710
711         if( *dn_in == '\0' ) {
712                 return( 0 );
713         }
714
715         if ( be != NULL && be_issuffix( be, dn_in ) ) {
716                 return( 0 );
717         }
718
719         inquote = 0;
720
721         for ( s = (char *)dn_in; *s; s++ ) {
722                 if ( *s == '\\' ) {
723                         if ( *(s + 1) ) {
724                                 s++;
725                         }
726                         continue;
727                 }
728                 if ( inquote ) {
729                         if ( *s == '"' ) {
730                                 inquote = 0;
731                         }
732                 } else {
733                         if ( *s == '"' ) {
734                                 inquote = 1;
735                         } else if ( DN_SEPARATOR( *s ) ) {
736                                 break;
737                         }
738                 }
739         }
740
741         return( s - dn_in );
742 #endif
743         struct berval   *rdn = NULL;
744         int             retval = 0;
745
746         assert( dn_in );
747
748         if ( dn_in == NULL ) {
749                 return 0;
750         }
751
752         while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
753                 dn_in++;
754         }
755
756         if ( dn_in[ 0 ] == '\0' ) {
757                 return 0;
758         }
759
760         if ( be != NULL && be_issuffix( be, dn_in ) ) {
761                 return 0;
762         }
763
764         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
765                 ber_bvfree( rdn );
766                 return 0;
767         }
768
769         retval = rdn->bv_len;
770         ber_bvfree( rdn );
771
772         return retval;
773 }
774
775 /*
776  * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
777  */
778 char * dn_rdn(
779         Backend *be,
780         const char      *dn_in )
781 {
782 #if 0
783         char *rdn;
784         int i = dn_rdnlen( be, dn_in );
785
786         rdn = ch_malloc( i + 1 );
787         strncpy(rdn, dn_in, i);
788         rdn[i] = '\0';
789         return rdn;
790 #endif 
791         struct berval   *rdn = NULL;
792         char            *retval;
793
794         assert( dn_in );
795
796         if ( dn_in == NULL ) {
797                 return NULL;
798         }
799
800         while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
801                 dn_in++;
802         }
803
804         if ( dn_in[ 0 ] == '\0' ) {
805                 return NULL;
806         }
807
808         if ( be != NULL && be_issuffix( be, dn_in ) ) {
809                 return NULL;
810         }
811
812         if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
813                 ber_bvfree( rdn );
814                 return NULL;
815         }
816
817         retval = rdn->bv_val;
818         free( rdn );
819
820         return retval;
821 }
822
823 /*
824  * return a charray of all subtrees to which the DN resides in
825  */
826 char **dn_subtree(
827         Backend *be,
828         const char      *dn )
829 {
830         char **subtree = NULL;
831         
832         do {
833                 charray_add( &subtree, dn );
834
835                 dn = dn_parent( be, dn );
836
837         } while ( dn != NULL );
838
839         return subtree;
840 }
841
842 /*
843  * dn_issuffix - tells whether suffix is a suffix of dn.
844  * Both dn and suffix must be normalized.
845  *      deprecated in favor of dnIsSuffix()
846  */
847 int
848 dn_issuffix(
849         const char      *dn,
850         const char      *suffix
851 )
852 {
853         struct berval   bvdn, bvsuffix;
854
855         assert( dn );
856         assert( suffix );
857
858         bvdn.bv_val = (char *) dn;
859         bvdn.bv_len = strlen( dn );
860         bvsuffix.bv_val = (char *) suffix;
861         bvsuffix.bv_len = strlen( suffix );
862
863         return dnIsSuffix( &bvdn, &bvsuffix );
864 }
865
866 /*
867  * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
868  * build_new_dn().
869  *
870  * Copyright 1999, Juan C. Gomez, All rights reserved.
871  * This software is not subject to any license of Silicon Graphics
872  * Inc. or Purdue University.
873  *
874  * Redistribution and use in source and binary forms are permitted
875  * without restriction or fee of any kind as long as this notice
876  * is preserved.
877  *
878  */
879
880 /* get_next_substring:
881  *
882  * Gets next substring in s, using d (or the end of the string '\0') as a
883  * string delimiter, and places it in a duplicated memory space. Leading
884  * spaces are ignored. String s **must** be null-terminated.
885  */
886
887 static char *
888 get_next_substring( const char * s, char d )
889 {
890
891         char    *str, *r;
892
893         r = str = ch_malloc( strlen(s) + 1 );
894
895         /* Skip leading spaces */
896         
897         while ( *s && ASCII_SPACE(*s) ) {
898                 s++;
899         }
900         
901         /* Copy word */
902
903         while ( *s && (*s != d) ) {
904
905                 /* Don't stop when you see trailing spaces may be a multi-word
906                 * string, i.e. name=John Doe!
907                 */
908
909                 *str++ = *s++;
910         }
911         
912         *str = '\0';
913         
914         return r;
915         
916 }
917
918
919 /* rdn_attr_type:
920  *
921  * Given a string (i.e. an rdn) of the form:
922  *       "attribute_type = attribute_value"
923  * this function returns the type of an attribute, that is the
924  * string "attribute_type" which is placed in newly allocated
925  * memory. The returned string will be null-terminated.
926  */
927
928 char * rdn_attr_type( const char * s )
929 {
930         return get_next_substring( s, '=' );
931 }
932
933
934 /* rdn_attr_value:
935  *
936  * Given a string (i.e. an rdn) of the form:
937  *       "attribute_type = attribute_value"
938  * this function returns "attribute_type" which is placed in newly allocated
939  * memory. The returned string will be null-terminated and may contain
940  * spaces (i.e. "John Doe\0").
941  */
942
943 char *
944 rdn_attr_value( const char * rdn )
945 {
946
947         const char      *str;
948
949         if ( (str = strchr( rdn, '=' )) != NULL ) {
950                 return get_next_substring(++str, '\0');
951         }
952
953         return NULL;
954
955 }
956
957
958 /* rdn_attrs:
959  *
960  * Given a string (i.e. an rdn) of the form:
961  *   "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
962  * this function stores the types of the attributes in ptypes, that is the
963  * array of strings "attribute_type" which is placed in newly allocated
964  * memory, and the values of the attributes in pvalues, that is the
965  * array of strings "attribute_value" which is placed in newly allocated
966  * memory. Returns 0 on success, -1 on failure.
967  *
968  * note: got part of the code from dn_validate
969  */
970
971 int
972 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
973 {
974         char **parts, **p;
975
976         *ptypes = NULL;
977         *pvalues = NULL;
978
979         /*
980          * explode the rdn in parts
981          */
982         parts = ldap_explode_rdn( rdn_in, 0 );
983
984         if ( parts == NULL ) {
985                 return( -1 );
986         }
987
988         for ( p = parts; p[0]; p++ ) {
989                 char *s, *e, *d;
990                 
991                 /* split each rdn part in type value */
992                 s = strchr( p[0], '=' );
993                 if ( s == NULL ) {
994                         charray_free( *ptypes );
995                         charray_free( *pvalues );
996                         charray_free( parts );
997                         return( -1 );
998                 }
999                 
1000                 /* type should be fine */
1001                 charray_add_n( ptypes, p[0], ( s-p[0] ) );
1002
1003                 /* value needs to be unescaped
1004                  * (maybe this should be moved to ldap_explode_rdn?) */
1005                 for ( e = d = s + 1; e[0]; e++ ) {
1006                         if ( *e != '\\' ) {
1007                                 *d++ = *e;
1008                         }
1009                 }
1010                 d[0] = '\0';
1011                 charray_add( pvalues, s + 1 );
1012         }
1013
1014         /* free array */
1015         charray_free( parts );
1016
1017         return( 0 );
1018 }
1019
1020
1021 /* rdn_validate:
1022  *
1023  * 1 if rdn is a legal rdn;
1024  * 0 otherwise (including a sequence of rdns)
1025  *
1026  * note: got it from dn_rdn; it should be rewritten
1027  * according to dn_validate
1028  */
1029 int
1030 rdn_validate( const char * rdn )
1031 {
1032         int     inquote;
1033
1034         if ( rdn == NULL ) {
1035                 return( 0 );
1036         }
1037
1038         if ( strchr( rdn, '=' ) == NULL ) {
1039                 return( 0 );
1040         }
1041
1042         while ( *rdn && ASCII_SPACE( *rdn ) ) {
1043                 rdn++;
1044         }
1045
1046         if( *rdn == '\0' ) {
1047                 return( 0 );
1048         }
1049
1050         inquote = 0;
1051
1052         for ( ; *rdn; rdn++ ) {
1053                 if ( *rdn == '\\' ) {
1054                         if ( *(rdn + 1) ) {
1055                                 rdn++;
1056                         }
1057                         continue;
1058                 }
1059                 if ( inquote ) {
1060                         if ( *rdn == '"' ) {
1061                                 inquote = 0;
1062                         }
1063                 } else {
1064                         if ( *rdn == '"' ) {
1065                                 inquote = 1;
1066                         } else if ( DN_SEPARATOR( *rdn ) ) {
1067                                 return( 0 );
1068                         }
1069                 }
1070         }
1071
1072         return( 1 );
1073 }
1074
1075
1076 /* build_new_dn:
1077  *
1078  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1079  * renamed.
1080  *
1081  * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
1082  */
1083
1084 void
1085 build_new_dn( char ** new_dn,
1086         const char * entry_dn,
1087         const char * parent_dn,
1088         const char * newrdn )
1089 {
1090
1091         if ( parent_dn == NULL ) {
1092                 *new_dn = ch_strdup( newrdn );
1093                 return;
1094         }
1095
1096         *new_dn = (char *) ch_malloc(
1097                 strlen( parent_dn ) + strlen( newrdn ) + 3 );
1098
1099         strcpy( *new_dn, newrdn );
1100         strcat( *new_dn, "," );
1101         strcat( *new_dn, parent_dn );
1102 }
1103
1104 #endif /* SLAP_DN_MIGRATION */
1105
1106 /*
1107  * dnIsSuffix - tells whether suffix is a suffix of dn.
1108  * Both dn and suffix must be normalized.
1109  */
1110 int
1111 dnIsSuffix(
1112         const struct berval *dn,
1113         const struct berval *suffix )
1114 {
1115         int     d = dn->bv_len - suffix->bv_len;
1116
1117         assert( dn );
1118         assert( suffix );
1119
1120         /* empty suffix matches any dn */
1121         if ( suffix->bv_len == 0 ) {
1122                 return 1;
1123         }
1124
1125         /* suffix longer than dn */
1126         if ( d < 0 ) {
1127                 return 0;
1128         }
1129
1130         /* no rdn separator or escaped rdn separator */
1131         if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) 
1132                                 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1133                 return 0;
1134         }
1135
1136         /* no possible match or malformed dn */
1137         if ( d == 1 ) {
1138                 return 0;
1139         }
1140
1141         /* compare */
1142         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1143 }