]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
b1168901c001dac8fadb72f57778ef1e983be181
[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                                 if( ava->la_value.bv_len == 0 ) {
271                                         /* BER encoding is empty */
272                                         return LDAP_INVALID_SYNTAX;
273                                 }
274
275                                 /* AVA is binary encoded, don't muck with it */
276                         } else if( flags & SLAP_LDAPDN_PRETTY ) {
277                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
278                                 if( !transf ) {
279                                         validf = ad->ad_type->sat_syntax->ssyn_validate;
280                                 }
281                         } else { /* normalization */
282                                 validf = ad->ad_type->sat_syntax->ssyn_validate;
283                                 mr = ad->ad_type->sat_equality;
284 #ifdef SLAP_NVALUES
285                                 if( mr ) normf = mr->smr_normalize;
286 #else
287                                 transf = ad->ad_type->sat_syntax->ssyn_normalize;
288 #endif
289                         }
290
291                         if ( validf ) {
292                                 /* validate value before normalization */
293                                 rc = ( *validf )( ad->ad_type->sat_syntax,
294                                         ava->la_value.bv_len
295                                                 ? &ava->la_value
296                                                 : (struct berval *) &slap_empty_bv );
297
298                                 if ( rc != LDAP_SUCCESS ) {
299                                         return LDAP_INVALID_SYNTAX;
300                                 }
301                         }
302
303                         if ( transf ) {
304                                 /*
305 #ifdef SLAP_NVALUES
306                                  * transform value by pretty function
307 #else
308                                  * transform value by normalize/pretty function
309 #endif
310                                  *      if value is empty, use empty_bv
311                                  */
312                                 rc = ( *transf )( ad->ad_type->sat_syntax,
313                                         ava->la_value.bv_len
314                                                 ? &ava->la_value
315                                                 : (struct berval *) &slap_empty_bv,
316                                         &bv );
317                         
318                                 if ( rc != LDAP_SUCCESS ) {
319                                         return LDAP_INVALID_SYNTAX;
320                                 }
321                         }
322
323 #ifdef SLAP_NVALUES
324                         if ( normf ) {
325                                 /*
326                                  * normalize value
327                                  *      if value is empty, use empty_bv
328                                  */
329                                 rc = ( *normf )(
330                                         0,
331                                         ad->ad_type->sat_syntax,
332                                         mr,
333                                         ava->la_value.bv_len
334                                                 ? &ava->la_value
335                                                 : (struct berval *) &slap_empty_bv,
336                                         &bv );
337                         
338                                 if ( rc != LDAP_SUCCESS ) {
339                                         return LDAP_INVALID_SYNTAX;
340                                 }
341                         }
342
343 #else
344                         if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
345                                 char *s = bv.bv_val;
346
347                                 if ( UTF8bvnormalize( &bv, &bv, 
348                                                 LDAP_UTF8_CASEFOLD ) == NULL ) {
349                                         return LDAP_INVALID_SYNTAX;
350                                 }
351                                 free( s );
352                         }
353 #endif
354
355                         if( bv.bv_val ) {
356                                 free( ava->la_value.bv_val );
357                                 ava->la_value = bv;
358                         }
359
360                         if( do_sort ) AVA_Sort( rdn, iAVA );
361                 }
362         }
363
364         return LDAP_SUCCESS;
365 }
366
367 int
368 #ifdef SLAP_NVALUES
369 dnNormalize(
370     slap_mask_t use,
371     Syntax *syntax,
372     MatchingRule *mr,
373     struct berval *val,
374     struct berval *out )
375 #else
376 dnNormalize(
377     Syntax *syntax,
378     struct berval *val,
379     struct berval *out )
380 #endif
381 {
382         assert( val );
383         assert( out );
384
385         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
386
387         if ( val->bv_len != 0 ) {
388                 LDAPDN          *dn = NULL;
389                 int             rc;
390
391                 /*
392                  * Go to structural representation
393                  */
394                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
395                 if ( rc != LDAP_SUCCESS ) {
396                         return LDAP_INVALID_SYNTAX;
397                 }
398
399                 assert( strlen( val->bv_val ) == val->bv_len );
400
401                 /*
402                  * Schema-aware rewrite
403                  */
404                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
405                         ldap_dnfree( dn );
406                         return LDAP_INVALID_SYNTAX;
407                 }
408
409                 /*
410                  * Back to string representation
411                  */
412                 rc = ldap_dn2bv( dn, out,
413                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
414
415                 ldap_dnfree( dn );
416
417                 if ( rc != LDAP_SUCCESS ) {
418                         return LDAP_INVALID_SYNTAX;
419                 }
420         } else {
421                 ber_dupbv( out, val );
422         }
423
424         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
425
426         return LDAP_SUCCESS;
427 }
428
429 #if 0
430 /*
431  * dn "pretty"ing routine
432  */
433 int
434 dnPretty(
435         Syntax *syntax,
436         struct berval *val,
437         struct berval **pretty)
438 {
439         struct berval *out;
440         int rc;
441
442         assert( pretty && *pretty == NULL );
443
444         out = ch_malloc( sizeof( struct berval ) );
445         rc = dnPretty2( syntax, val, out );
446         if ( rc != LDAP_SUCCESS )
447                 free( out );
448         else
449                 *pretty = out;
450         return rc;
451 }
452 #endif
453
454 int
455 dnPretty2(
456         Syntax *syntax,
457         struct berval *val,
458         struct berval *out)
459 {
460         assert( val );
461         assert( out );
462
463 #ifdef NEW_LOGGING
464         LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
465 #else
466         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
467 #endif
468
469         if ( val->bv_len == 0 ) {
470                 ber_dupbv( out, val );
471
472         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
473                 return LDAP_INVALID_SYNTAX;
474
475         } else {
476                 LDAPDN          *dn = NULL;
477                 int             rc;
478
479                 /* FIXME: should be liberal in what we accept */
480                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
481                 if ( rc != LDAP_SUCCESS ) {
482                         return LDAP_INVALID_SYNTAX;
483                 }
484
485                 assert( strlen( val->bv_val ) == val->bv_len );
486
487                 /*
488                  * Schema-aware rewrite
489                  */
490                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
491                         ldap_dnfree( dn );
492                         return LDAP_INVALID_SYNTAX;
493                 }
494
495                 /* FIXME: not sure why the default isn't pretty */
496                 /* RE: the default is the form that is used as
497                  * an internal representation; the pretty form
498                  * is a variant */
499                 rc = ldap_dn2bv( dn, out,
500                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
501
502                 ldap_dnfree( dn );
503
504                 if ( rc != LDAP_SUCCESS ) {
505                         return LDAP_INVALID_SYNTAX;
506                 }
507         }
508
509         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
510
511         return LDAP_SUCCESS;
512 }
513
514 int
515 dnPrettyNormalDN(
516         Syntax *syntax,
517         struct berval *val,
518         LDAPDN **dn,
519         int flags )
520 {
521         assert( val );
522         assert( dn );
523
524 #ifdef NEW_LOGGING
525         LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n", 
526                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
527                         val->bv_val, 0 );
528 #else
529         Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n", 
530                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
531                         val->bv_val, 0 );
532 #endif
533
534         if ( val->bv_len == 0 ) {
535                 return LDAP_SUCCESS;
536
537         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
538                 return LDAP_INVALID_SYNTAX;
539
540         } else {
541                 int             rc;
542
543                 /* FIXME: should be liberal in what we accept */
544                 rc = ldap_bv2dn( val, dn, LDAP_DN_FORMAT_LDAP );
545                 if ( rc != LDAP_SUCCESS ) {
546                         return LDAP_INVALID_SYNTAX;
547                 }
548
549                 assert( strlen( val->bv_val ) == val->bv_len );
550
551                 /*
552                  * Schema-aware rewrite
553                  */
554                 if ( LDAPDN_rewrite( *dn, flags ) != LDAP_SUCCESS ) {
555                         ldap_dnfree( *dn );
556                         *dn = NULL;
557                         return LDAP_INVALID_SYNTAX;
558                 }
559         }
560
561         Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n", 
562                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
563                         0, 0 );
564
565         return LDAP_SUCCESS;
566 }
567
568 /*
569  * Combination of both dnPretty and dnNormalize
570  */
571 int
572 dnPrettyNormal(
573         Syntax *syntax,
574         struct berval *val,
575         struct berval *pretty,
576         struct berval *normal)
577 {
578 #ifdef NEW_LOGGING
579         LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
580 #else
581         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
582 #endif
583
584         assert( val );
585         assert( pretty );
586         assert( normal );
587
588         if ( val->bv_len == 0 ) {
589                 ber_dupbv( pretty, val );
590                 ber_dupbv( normal, val );
591
592         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
593                 /* too big */
594                 return LDAP_INVALID_SYNTAX;
595
596         } else {
597                 LDAPDN          *dn = NULL;
598                 int             rc;
599
600                 pretty->bv_val = NULL;
601                 normal->bv_val = NULL;
602                 pretty->bv_len = 0;
603                 normal->bv_len = 0;
604
605                 /* FIXME: should be liberal in what we accept */
606                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
607                 if ( rc != LDAP_SUCCESS ) {
608                         return LDAP_INVALID_SYNTAX;
609                 }
610
611                 assert( strlen( val->bv_val ) == val->bv_len );
612
613                 /*
614                  * Schema-aware rewrite
615                  */
616                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
617                         ldap_dnfree( dn );
618                         return LDAP_INVALID_SYNTAX;
619                 }
620
621                 rc = ldap_dn2bv( dn, pretty,
622                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
623
624                 if ( rc != LDAP_SUCCESS ) {
625                         ldap_dnfree( dn );
626                         return LDAP_INVALID_SYNTAX;
627                 }
628
629                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
630                         ldap_dnfree( dn );
631                         free( pretty->bv_val );
632                         pretty->bv_val = NULL;
633                         pretty->bv_len = 0;
634                         return LDAP_INVALID_SYNTAX;
635                 }
636
637                 rc = ldap_dn2bv( dn, normal,
638                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
639
640                 ldap_dnfree( dn );
641                 if ( rc != LDAP_SUCCESS ) {
642                         free( pretty->bv_val );
643                         pretty->bv_val = NULL;
644                         pretty->bv_len = 0;
645                         return LDAP_INVALID_SYNTAX;
646                 }
647         }
648
649 #ifdef NEW_LOGGING
650         LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
651                 pretty->bv_val, normal->bv_val, 0  );
652 #else
653         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
654                 pretty->bv_val, normal->bv_val, 0 );
655 #endif
656
657         return LDAP_SUCCESS;
658 }
659
660 /*
661  * dnMatch routine
662  */
663 int
664 dnMatch(
665         int *matchp,
666         slap_mask_t flags,
667         Syntax *syntax,
668         MatchingRule *mr,
669         struct berval *value,
670         void *assertedValue )
671 {
672         int match;
673         struct berval *asserted = (struct berval *) assertedValue;
674
675         assert( matchp );
676         assert( value );
677         assert( assertedValue );
678         
679         match = value->bv_len - asserted->bv_len;
680
681         if ( match == 0 ) {
682                 match = memcmp( value->bv_val, asserted->bv_val, 
683                                 value->bv_len );
684         }
685
686 #ifdef NEW_LOGGING
687         LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n    %s\n    %s\n", 
688                 match, value->bv_val, asserted->bv_val  );
689 #else
690         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
691                 match, value->bv_val, asserted->bv_val );
692 #endif
693
694         *matchp = match;
695         return( LDAP_SUCCESS );
696 }
697
698 /*
699  * dnParent - dn's parent, in-place
700  *
701  * note: the incoming dn is assumed to be normalized/prettyfied,
702  * so that escaped rdn/ava separators are in '\'+hexpair form
703  */
704 void
705 dnParent( 
706         struct berval   *dn, 
707         struct berval   *pdn )
708 {
709         char    *p;
710
711         p = strchr( dn->bv_val, ',' );
712
713         /* one-level dn */
714         if ( p == NULL ) {
715                 pdn->bv_len = 0;
716                 pdn->bv_val = dn->bv_val + dn->bv_len;
717                 return;
718         }
719
720         assert( DN_SEPARATOR( p[ 0 ] ) );
721         p++;
722
723         assert( ATTR_LEADCHAR( p[ 0 ] ) );
724         pdn->bv_val = p;
725         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
726
727         return;
728 }
729
730 int
731 dnExtractRdn( 
732         struct berval   *dn, 
733         struct berval   *rdn )
734 {
735         LDAPRDN         *tmpRDN;
736         const char      *p;
737         int             rc;
738
739         assert( dn );
740         assert( rdn );
741
742         if( dn->bv_len == 0 ) {
743                 return LDAP_OTHER;
744         }
745
746         rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
747         if ( rc != LDAP_SUCCESS ) {
748                 return rc;
749         }
750
751         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
752
753         ldap_rdnfree( tmpRDN );
754         if ( rc != LDAP_SUCCESS ) {
755                 return rc;
756         }
757
758         return LDAP_SUCCESS;
759 }
760
761 /*
762  * We can assume the input is a prettied or normalized DN
763  */
764 int 
765 dn_rdnlen(
766         Backend         *be,
767         struct berval   *dn_in )
768 {
769         const char      *p;
770
771         assert( dn_in );
772
773         if ( dn_in == NULL ) {
774                 return 0;
775         }
776
777         if ( !dn_in->bv_len ) {
778                 return 0;
779         }
780
781         if ( be != NULL && be_issuffix( be, dn_in ) ) {
782                 return 0;
783         }
784
785         p = strchr( dn_in->bv_val, ',' );
786
787         return p ? p - dn_in->bv_val : dn_in->bv_len;
788 }
789
790
791 /* rdnValidate:
792  *
793  * LDAP_SUCCESS if rdn is a legal rdn;
794  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
795  */
796 int
797 rdnValidate( struct berval *rdn )
798 {
799 #if 1
800         /* Major cheat!
801          * input is a pretty or normalized DN
802          * hence, we can just search for ','
803          */
804         if( rdn == NULL || rdn->bv_len == 0 ||
805                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
806         {
807                 return LDAP_INVALID_SYNTAX;
808         }
809
810         return strchr( rdn->bv_val, ',' ) == NULL
811                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
812
813 #else
814         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
815         const char      *p;
816         int             rc;
817
818         /*
819          * must be non-empty
820          */
821         if ( rdn == NULL || rdn == '\0' ) {
822                 return 0;
823         }
824
825         /*
826          * must be parsable
827          */
828         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
829         if ( rc != LDAP_SUCCESS ) {
830                 return 0;
831         }
832
833         /*
834          * Must be one-level
835          */
836         if ( p[ 0 ] != '\0' ) {
837                 return 0;
838         }
839
840         /*
841          * Schema-aware validate
842          */
843         if ( rc == LDAP_SUCCESS ) {
844                 rc = LDAPDN_validate( DN );
845         }
846         ldap_rdnfree( RDN );
847
848         /*
849          * Must validate (there's a repeated parsing ...)
850          */
851         return ( rc == LDAP_SUCCESS );
852 #endif
853 }
854
855
856 /* build_new_dn:
857  *
858  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
859  * renamed.
860  *
861  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
862  */
863
864 void
865 build_new_dn( struct berval * new_dn,
866         struct berval * parent_dn,
867         struct berval * newrdn )
868 {
869         char *ptr;
870
871         if ( parent_dn == NULL ) {
872                 ber_dupbv( new_dn, newrdn );
873                 return;
874         }
875
876         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
877         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
878
879         ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
880         *ptr++ = ',';
881         strcpy( ptr, parent_dn->bv_val );
882 }
883
884
885 /*
886  * dnIsSuffix - tells whether suffix is a suffix of dn.
887  * Both dn and suffix must be normalized.
888  */
889 int
890 dnIsSuffix(
891         const struct berval *dn,
892         const struct berval *suffix )
893 {
894         int     d = dn->bv_len - suffix->bv_len;
895
896         assert( dn );
897         assert( suffix );
898
899         /* empty suffix matches any dn */
900         if ( suffix->bv_len == 0 ) {
901                 return 1;
902         }
903
904         /* suffix longer than dn */
905         if ( d < 0 ) {
906                 return 0;
907         }
908
909         /* no rdn separator or escaped rdn separator */
910         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
911                 return 0;
912         }
913
914         /* no possible match or malformed dn */
915         if ( d == 1 ) {
916                 return 0;
917         }
918
919         /* compare */
920         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
921 }
922
923 #ifdef HAVE_TLS
924 /*
925  * Convert an X.509 DN into a normalized LDAP DN
926  */
927 int
928 dnX509normalize( void *x509_name, struct berval *out )
929 {
930         /* Invoke the LDAP library's converter with our schema-rewriter */
931         return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
932 }
933
934 /*
935  * Get the TLS session's peer's DN into a normalized LDAP DN
936  */
937 int
938 dnX509peerNormalize( void *ssl, struct berval *dn )
939 {
940
941         return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
942 }
943 #endif