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