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