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