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