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