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